Skip to content

Commit

Permalink
Spec Pair.invert
Browse files Browse the repository at this point in the history
  • Loading branch information
zoffixznet committed May 23, 2017
1 parent e86264f commit f4828aa
Showing 1 changed file with 42 additions and 1 deletion.
43 changes: 42 additions & 1 deletion S02-types/pair.t
Expand Up @@ -2,7 +2,7 @@ use v6;

use Test;

plan 4 * 19 + 108;
plan 4 * 19 + 109;

# L<S02/Mutable types/A single key-to-value association>
# basic Pair
Expand Down Expand Up @@ -419,4 +419,45 @@ subtest 'Pair.ACCEPTS' => {
'Pair.new with wrong named args does not go to Mu.new';
}


subtest 'Pair.invert' => {
my @tests = [ a => 42, (42 => 'a',).Seq ], [ 42 => 70, (70 => 42, ).Seq ],
[ foo => (bar => meow => 42), ((bar => meow => 42) => 'foo',).Seq ],
[ # .invert expands Iterables
<a b c> => <d e f>,
(:d(<a b c>), :e(<a b c>), :f(<a b c>)).Seq,
],;

plan 3 + @tests;
is-deeply .[0].invert, .[1], .[0].perl for @tests;


# Hashes are also Iterables, but don't guarantee order here:
is-deeply (%(<a b c d>) => %(<e f g h>)).invert.sort,
(:e<f> => %(<a b c d>), :g<h> => %(<a b c d>)).Seq.sort,
(%(<a b c d>) => %(<e f g h>)).perl;

subtest '(Any) => (Mu)' => {
plan 4;
given ((Any) => (Mu)).invert {
.cache;
isa-ok $_, Seq, "return's type";
is-deeply .elems, 1, "return's number of elements";
is .[0].key, Mu, '.key';
is .[0].value, Any, '.value';
}
}

subtest '(Mu) => (Any)' => {
plan 4;
given ((Mu) => (Any)).invert {
.cache;
isa-ok $_, Seq, "return's type";
is-deeply .elems, 1, "return's number of elements";
is .[0].key, Any, '.key';
is .[0].value, Mu, '.value';
}
}
}

# vim: ft=perl6

0 comments on commit f4828aa

Please sign in to comment.