Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

59 additional tests ported over from set.t.

  • Loading branch information...
commit 9da8a25e98e885f1fd73a2296f34b0582c36ebea 1 parent a35fbd5
colomon authored February 10, 2012

Showing 1 changed file with 151 additions and 2 deletions. Show diff stats Hide diff stats

  1. 153  S02-types/keyset.t
153  S02-types/keyset.t
... ...
@@ -1,7 +1,7 @@
1 1
 use v6;
2 2
 use Test;
3 3
 
4  
-plan 49;
  4
+plan 108;
5 5
 
6 6
 # L<S02/Mutable types/"KeyHash of Bool">
7 7
 
@@ -17,10 +17,21 @@ sub showset($s) { $s.keys.sort.join(' ') }
17 17
     is showset($s), 'a b foo', '...with the right elements';
18 18
 
19 19
     is $s<a>, True, 'Single-key subscript (existing element)';
  20
+    isa_ok $s<a>, Bool, 'Single-key subscript has correct type (existing element)';
20 21
     is $s<santa>, False, 'Single-key subscript (nonexistent element)';
  22
+    isa_ok $s<santa>, Bool, 'Single-key subscript has correct type (nonexistent element)';
21 23
     is $s.exists('a'), True, '.exists with existing element';
22 24
     is $s.exists('santa'), False, '.exists with nonexistent element';
23 25
 
  26
+    ok ?$s, "Bool returns True if there is something in the KeySet";
  27
+    nok ?Set.new(), "Bool returns False if there is nothing in the KeySet";
  28
+
  29
+    my $hash;
  30
+    lives_ok { $hash = $s.hash }, ".hash doesn't die";
  31
+    isa_ok $hash, Hash, "...and it returned a Hash";
  32
+    is showset($hash), 'a b foo', '...with the right elements';
  33
+    is $hash.values.grep({ ($_ ~~ Bool) && $_ }).elems, 3, "...and values";
  34
+
24 35
     dies_ok { $s.keys = <c d> }, "Can't assign to .keys";
25 36
     dies_ok { $s.values = <True False> }, "Can't assign to .values";
26 37
 
@@ -56,7 +67,6 @@ sub showset($s) { $s.keys.sort.join(' ') }
56 67
     is showset($s), 'a foo', '... but only if they were there to start with';
57 68
 }
58 69
 
59  
-
60 70
 {
61 71
     my %h := KeySet.new(<a c>);
62 72
     is +%h.elems, 2, 'Inititalization worked';
@@ -79,6 +89,145 @@ sub showset($s) { $s.keys.sort.join(' ') }
79 89
     # is %h.keys.sort.join, 'PQR', '... works as expected';
80 90
 }
81 91
 
  92
+{
  93
+    my $s = KeySet.new(<foo bar foo bar baz foo>);
  94
+    is showset($s), 'bar baz foo', 'KeySet.new discards duplicates';
  95
+}
  96
+
  97
+{
  98
+    my $b = KeySet.new([ foo => 10, bar => 17, baz => 42 ]);
  99
+    isa_ok $b, KeySet, 'KeySet.new given an array of pairs produces a KeySet';
  100
+    is showset($b), 'bar baz foo', '... with the right elements';
  101
+}
  102
+
  103
+{
  104
+    my $b = KeySet.new({ foo => 10, bar => 17, baz => 42 }.hash);
  105
+    isa_ok $b, KeySet, 'KeySet.new given a Hash produces a KeySet';
  106
+    is showset($b), 'bar baz foo', '... with the right elements';
  107
+}
  108
+
  109
+{
  110
+    my $b = KeySet.new({ foo => 10, bar => 17, baz => 42 });
  111
+    isa_ok $b, KeySet, 'KeySet.new given a Hash produces a KeySet';
  112
+    is showset($b), 'bar baz foo', '... with the right elements';
  113
+}
  114
+
  115
+{
  116
+    my $b = KeySet.new(set <foo bar foo bar baz foo>);
  117
+    isa_ok $b, KeySet, 'KeySet.new given a Set produces a KeySet';
  118
+    is showset($b), 'bar baz foo', '... with the right elements';
  119
+}
  120
+
  121
+{
  122
+    my $b = KeySet.new(KeySet.new(<foo bar foo bar baz foo>));
  123
+    isa_ok $b, KeySet, 'KeySet.new given a KeySet produces a KeySet';
  124
+    is showset($b), 'bar baz foo', '... with the right elements';
  125
+}
  126
+
  127
+{
  128
+    my $b = KeySet.new(KeyBag.new(<foo bar foo bar baz foo>));
  129
+    isa_ok $b, KeySet, 'KeySet.new given a KeyBag produces a KeySet';
  130
+    is showset($b), 'bar baz foo', '... with the right elements';
  131
+}
  132
+
  133
+{
  134
+    my $b = KeySet.new(bag <foo bar foo bar baz foo>);
  135
+    isa_ok $b, KeySet, 'KeySet given a Bag produces a KeySet';
  136
+    is showset($b), 'bar baz foo', '... with the right elements';
  137
+}
  138
+
  139
+{
  140
+    my $s = KeySet.new(<foo bar baz>);
  141
+    isa_ok $s.list.elems, 3, ".list returns 3 things";
  142
+    is $s.list.grep(Str).elems, 3, "... all of which are Str";
  143
+    is $s.iterator.grep(Str).elems, 3, ".iterator yields three Strs";
  144
+}
  145
+
  146
+{
  147
+    my $s = KeySet.new(<foo bar baz>);
  148
+    my $str;
  149
+    my $c;
  150
+    lives_ok { $str = $s.perl }, ".perl lives";
  151
+    isa_ok $str, Str, "... and produces a string";
  152
+    lives_ok { $c = eval $str }, ".perl.eval lives";
  153
+    isa_ok $c, KeySet, "... and produces a KeySet";
  154
+    is showset($c), showset($s), "... and it has the correct values";
  155
+}
  156
+
  157
+{
  158
+    my $s = KeySet.new(<foo bar baz>);
  159
+    my $s;
  160
+    lives_ok { $s = $s.Str }, ".Str lives";
  161
+    isa_ok $s, Str, "... and produces a string";
  162
+    ok $s ~~ /foo/, "... which mentions foo";
  163
+    ok $s ~~ /bar/, "... which mentions bar";
  164
+    ok $s ~~ /baz/, "... which mentions baz";
  165
+}
  166
+
  167
+{
  168
+    my $s = KeySet.new(<foo bar baz>);
  169
+    my $s;
  170
+    lives_ok { $s = $s.gist }, ".gist lives";
  171
+    isa_ok $s, Str, "... and produces a string";
  172
+    ok $s ~~ /foo/, "... which mentions foo";
  173
+    ok $s ~~ /bar/, "... which mentions bar";
  174
+    ok $s ~~ /baz/, "... which mentions baz";
  175
+}
  176
+
  177
+# L<S02/Names and Variables/'C<%x> may be bound to'>
  178
+
  179
+{
  180
+    my %s := KeySet.new(<a b c b>);
  181
+    isa_ok %s, KeySet, 'A KeySet bound to a %var is a KeySet';
  182
+    is showset(%s), 'a b c', '...with the right elements';
  183
+
  184
+    is %s<a>, True, 'Single-key subscript (existing element)';
  185
+    is %s<santa>, False, 'Single-key subscript (nonexistent element)';
  186
+
  187
+    lives_ok { %s<a> = True }, "Can assign to an element (KeySets are immutable)";
  188
+}
  189
+
  190
+# L<S32::Containers/KeySet/roll>
  191
+
  192
+{
  193
+    my $s = KeySet.new(<a b c>);
  194
+
  195
+    my $a = $s.roll;
  196
+    ok $a eq "a" || $a eq "b" || $a eq "c", "We got one of the three choices";
  197
+
  198
+    my @a = $s.roll(2);
  199
+    is +@a, 2, '.roll(2) returns the right number of items';
  200
+    is @a.grep(* eq 'a' | 'b' | 'c').elems, 2, '.roll(2) returned "a"s, "b"s, and "c"s';
  201
+
  202
+    my @a = $s.roll: 100;
  203
+    is +@a, 100, '.roll(100) returns 100 items';
  204
+    is @a.grep(* eq 'a' | 'b' | 'c').elems, 100, '.roll(100) returned "a"s, "b"s, and "c"s';
  205
+}
  206
+
  207
+# L<S32::Containers/KeySet/pick>
  208
+
  209
+{
  210
+    my $s = KeySet.new(<a b c d e f g h>);
  211
+    my @a = $s.pick: *;
  212
+    is @a.sort.join, 'abcdefgh', 'KeySet.pick(*) gets all elements';
  213
+    isnt @a.join, 'abcdefgh', 'KeySet.pick(*) returns elements in a random order';
  214
+      # There's only a 1/40_320 chance of that test failing by chance alone.
  215
+}
  216
+
  217
+{
  218
+    my $s = KeySet.new(<a b c>);
  219
+
  220
+    my $a = $s.pick;
  221
+    ok $a eq "a" || $a eq "b" || $a eq "c", "We got one of the three choices";
  222
+
  223
+    my @a = $s.pick(2);
  224
+    is +@a, 2, '.pick(2) returns the right number of items';
  225
+    is @a.grep(* eq 'a' | 'b' | 'c').elems, 2, '.pick(2) returned "a"s, "b"s, and "c"s';
  226
+    ok @a.grep(* eq 'a').elems <= 1, '.pick(2) returned at most one "a"';
  227
+    ok @a.grep(* eq 'b').elems <= 1, '.pick(2) returned at most one "b"';
  228
+    ok @a.grep(* eq 'c').elems <= 1, '.pick(2) returned at most one "c"';
  229
+}
  230
+
82 231
 #?niecza skip "is KeySet doesn't work yet"
83 232
 {
84 233
     my %h is KeySet = a => True, b => False, c => True;

0 notes on commit 9da8a25

Please sign in to comment.
Something went wrong with that request. Please try again.