"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "t/spec/S12-methods/multi.t" between
rakudo-2020.09.tar.gz and rakudo-2020.10.tar.gz

About: Rakudo is a compiler for the Raku Pogramming Language (formerly known as Perl 6).

multi.t  (rakudo-2020.09):multi.t  (rakudo-2020.10)
use v6.d; use v6.d;
use Test; use Test;
plan 31; plan 32;
# L<S12/"Multisubs and Multimethods"> # L<S12/"Multisubs and Multimethods">
# L<S12/"Trusts"> # L<S12/"Trusts">
class Foo { class Foo {
multi method bar() { multi method bar() {
return "Foo.bar() called with no args"; return "Foo.bar() called with no args";
} }
multi method bar(Str $str) { multi method bar(Str $str) {
skipping to change at line 40 skipping to change at line 40
} }
my $foo = Foo.new(); my $foo = Foo.new();
is($foo.bar(), 'Foo.bar() called with no args', '... multi-method dispatched on no args'); is($foo.bar(), 'Foo.bar() called with no args', '... multi-method dispatched on no args');
is($foo.bar("Hello"), 'Foo.bar() called with Str : Hello', '... multi-method dis patched on Str'); is($foo.bar("Hello"), 'Foo.bar() called with Str : Hello', '... multi-method dis patched on Str');
is($foo.bar(5), 'Foo.bar() called with Int : 5', '... multi-method dispatched on Int'); is($foo.bar(5), 'Foo.bar() called with Int : 5', '... multi-method dispatched on Int');
is($foo.bar(4.2), 'Foo.bar() called with Numeric : 4.2', '... multi-method dispa tched on Numeric'); is($foo.bar(4.2), 'Foo.bar() called with Numeric : 4.2', '... multi-method dispa tched on Numeric');
#?rakudo todo 'RT #66006' throws-like { $foo.baz() }, X::Multi::NoMatch,
try { EVAL '$foo.baz()' }; "Call with wrong number of args results in no matching candidate exception";
ok ~$! ~~ /:i argument[s?]/, 'Call with wrong number of args should complain abo
ut args';
class Foo2 { class Foo2 {
multi method a($d) { #OK not used multi method a($d) { #OK not used
"Any-method in Foo"; "Any-method in Foo";
} }
} }
class Bar is Foo2 { class Bar is Foo2 {
multi method a(Int $d) { #OK not used multi method a(Int $d) { #OK not used
"Int-method in Bar"; "Int-method in Bar";
} }
skipping to change at line 115 skipping to change at line 114
class M does RoleS does RoleI { class M does RoleS does RoleI {
multi method d( Any $x ) { 'any' } #OK not used multi method d( Any $x ) { 'any' } #OK not used
} }
my M $m .= new; my M $m .= new;
is $m.d( 876 ), 'integer', 'dispatch to one role'; is $m.d( 876 ), 'integer', 'dispatch to one role';
is $m.d( '7' ), 'string', 'dispatch to other role'; is $m.d( '7' ), 'string', 'dispatch to other role';
is $m.d( 1.2 ), 'any', 'dispatch to the class with the roles'; is $m.d( 1.2 ), 'any', 'dispatch to the class with the roles';
my @multi_method = $m.^methods.grep({ ~$_ eq 'd' }); my @multi_method = $m.^methods.grep({ .name eq 'd' });
is @multi_method.elems, 1, '.^methods returns one element for a multi'; is @multi_method.elems, 1, '.^methods returns one element for a multi';
my $routine = @multi_method[0]; my $routine = @multi_method[0];
ok $routine ~~ Routine, 'multi method from ^methods is a Routine'; ok $routine ~~ Routine, 'multi method from ^methods is a Routine';
my @candies = $routine.candidates; my @candies = $routine.candidates;
is @candies.elems, 3, 'got three candidates for multi method'; is @candies.elems, 3, 'got three candidates for multi method';
ok @candies[0] ~~ Method, 'candidate 0 is a method'; ok @candies[0] ~~ Method, 'candidate 0 is a method';
ok @candies[1] ~~ Method, 'candidate 1 is a method'; ok @candies[1] ~~ Method, 'candidate 1 is a method';
ok @candies[2] ~~ Method, 'candidate 2 is a method'; ok @candies[2] ~~ Method, 'candidate 2 is a method';
skipping to change at line 191 skipping to change at line 190
multi method bar() { } multi method bar() { }
} }
lives-ok { B.new.foo() }, lives-ok { B.new.foo() },
'multis with different names but same signatures are not ambiguous'; 'multis with different names but same signatures are not ambiguous';
} }
# RT #74646 # RT #74646
{ {
my class A { my class A {
multi method foo($a) { "general" }
multi submethod foo(Str $a) { "specific" } multi submethod foo(Str $a) { "specific" }
} }
my class B is A {} my class B is A {}
is A.new.foo("OH HAI"), 'specific', 'multi submethod can be called on exact instance'; is A.new.foo("OH HAI"), 'specific', 'multi submethod can be called on exact instance';
is B.new.foo("OH HAI"), 'general', 'multi submethod is not inherited'; throws-like { B.new.foo("OH HAI") }, X::Method::NotFound, 'multi submethod i s not inherited';
} }
# vim: ft=perl6 # GH rakudo/rakudo#3976 https://github.com/rakudo/rakudo/issues/3976
{
eval-lives-ok q:to/MULTI-SUBMETHOD/, "Declaring both proto and multi for a s
ubmethod doesn't die";
my class A {
proto submethod foo(|) {*}
multi submethod foo() { }
}
MULTI-SUBMETHOD
}
# vim: expandtab shiftwidth=4
 End of changes. 6 change blocks. 
8 lines changed or deleted 5 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)