[t/spec] Unfudge infix:<cmp> tests on pairs, also rewrite two of the tests so they...
[pugs.git] / t / spec / S32-hash / pairs.t
blob5e0f9153ddad28a31739556f1ab4c7b526112bc4
1 use v6;
3 use Test;
5 plan 21;
7 =begin description
9 Basic C<pairs> tests, see S32::Containers.
11 =end description
13 # L<S32::Containers/"Hash"/=item pairs>
16   my %hash = (a => 1, b => 2, c => 3);
17   my @pairs;
18   ok((@pairs = %hash.pairs),    "pairs on hashes");
19   ok((@pairs = @pairs.sort),    'Can sort list of pairs');
20   is +@pairs, 3,                "pairs on hashes returned the correct number of elems";
21   if +@pairs != 3 {
22     skip 6, "skipped tests which depend on a test which failed";
23   } else {
24     is @pairs[0].key,   "a",      "value of pair returned by hash.pairs was correct (1)";
25     is @pairs[1].key,   "b",      "value of pair returned by hash.pairs was correct (2)";
26     is @pairs[2].key,   "c",      "value of pair returned by hash.pairs was correct (3)";
27     is @pairs[0].value,   1,      "key of pair returned by hash.pairs was correct (1)";
28     is @pairs[1].value,   2,      "key of pair returned by hash.pairs was correct (2)";
29     is @pairs[2].value,   3,      "key of pair returned by hash.pairs was correct (3)";
30   }
33 # Following stated by Larry on p6l
35   my $pair  = (a => 1);
36   my @pairs;
37   ok((@pairs = $pair.pairs), "pairs on a pair");
38   is +@pairs, 1,           "pairs on a pair returned one elem";
39   if +@pairs != 1 {
40     skip 2, "skipped tests which depend on a test which failed";
41   } else {
42     is @pairs[0].key,   "a", "key of pair returned by pair.pairs";
43     is @pairs[0].value,   1, "value of pair returned by pair.pairs";
44   }
47 # This next group added by Darren Duncan following discovery while debugging ext/Locale-KeyedText:
49   my $hash_of_2_pairs = {'a'=>'b','c'=>'d'};
50   my $hash_of_1_pair = {'a'=>'b'};
51   #?pugs 2 todo 'feature'
52   is( $hash_of_2_pairs.pairs.sort.join( ',' ), "a\tb,c\td",
53     "pairs() on 2-elem hash, 1-depth joined");
54   is( $hash_of_1_pair.pairs.sort.join( ',' ), "a\tb",
55     "pairs() on 1-elem hash, 1-depth joined");
56   is( $hash_of_2_pairs.pairs.sort.map({ .key~'='~.value }).join( ',' ), 'a=b,c=d', 
57     "pairs() on 2-elem hash, 2-depth joined" );
58   is( try { $hash_of_1_pair.pairs.sort.map({ .key~'='~.value }).join( ',' ) }, 'a=b', 
59     "pairs() on 1-elem hash, 2-depth joined" );
63     my %hash = (:a(1), :b(2), :c(3));
65     lives_ok { for %hash.pairs -> $pair {
66         $pair.value += 100;
67     } }, 'aliases returned by %hash.pairs should be rw (1)';
69     #?rakudo todo "Rakudo seems to make a copy rather than a reference"
70     is %hash<b>, 102, 'aliases returned by %hash.pairs should be rw (2)';
73 #?pugs todo 'bug'
75     my $pair = (a => 42);
77     lives_ok { for $pair.pairs -> $p {
78         $p.value += 100;
79     } }, 'aliases returned by $pair.value should be rw (1)';
81     is $pair.value, 142, 'aliases returned by $pair.kv should be rw (2)';
84 # vim: ft=perl6