Skip to content

Commit

Permalink
Added test to exercise sequentially resolved multis (See issue Raku#765)
Browse files Browse the repository at this point in the history
  • Loading branch information
doomvox committed Oct 26, 2021
1 parent 3049084 commit 9905d40
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions S12-subset/multi-sequential.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use v6;
use Test;
use lib $?FILE.IO.parent(2).add("packages/Test-Helpers");
use Test::Util;

=begin description
Tests the cases where multi-dispatch is resolved sequentially (e.g. ambiguous subsets/where clauses)
=end description

# L<S12/"Types and Subtypes">

plan 1;

# https://github.com/Raku/roast/issues/765
group-of 2 => 'ambiguous subset matches resolved sequentially' => {
# note: godzilla is both a monster and a hero
my @monsters = < godzilla gammera ghidra golem >;
my @heroes = < godzilla beowulf ultraman inframan >;
subset Monster of Str where { $_ eq any( @monsters ) };
subset Hero of Str where { $_ eq any( @heroes ) };

group-of 2 => 'two multis based on subsets' => {
multi sub classify (Monster $name) {
return "$name is a monster";
}
multi sub classify (Hero $name) {
return "$name is a hero";
}
my $c1 = classify('ultraman');
my $c2 = classify('godzilla');
is( $c1, "ultraman is a hero",
"Testing that the multi with the only subset match runs.");
is( $c2, "godzilla is a monster",
"Testing ambiguous case runs first multi that matches.");
}
group-of 2 => 'two multis reversed' => {
# here the same multis are defined in a different order
multi sub classify (Hero $name) {
return "$name is a hero";
}
multi sub classify (Monster $name) {
return "$name is a monster";
}
my $c1 = classify('ultraman');
my $c2 = classify('godzilla');
is( $c1, "ultraman is a hero",
"Testing that the multi with the only subset match runs.");
is( $c2, "godzilla is a hero",
"Testing ambiguous case runs first multi that matches.");
}
}

0 comments on commit 9905d40

Please sign in to comment.