Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 269 lines (213 sloc) 9.247 kb
71b520d [t/spec/S02-builtin_data_types/keyset.t] added some basic tests
masak authored
1 use v6;
2 use Test;
264f008 Additions, corrections, and reformatting for KeySet, KeyBag, and KeyWeig...
Kodi Arfer authored
3
014f5f8 Solomon Foster Modify tests for Set.Str and KeySet.Str to conform to TimToady's suggest...
colomon authored
4 plan 110;
71b520d [t/spec/S02-builtin_data_types/keyset.t] added some basic tests
masak authored
5
fdd7628 [t/spec] more smartlink fixes
moritz authored
6 # L<S02/Mutable types/"KeyHash of Bool">
71b520d [t/spec/S02-builtin_data_types/keyset.t] added some basic tests
masak authored
7
8 # A KeySet is a KeyHash of Bool, i.e. the values are Bool
9
f2903a0 Solomon Foster Lots more KeySet tests, inspired by the Set tests. Also fudge for Niecz...
colomon authored
10 sub showset($s) { $s.keys.sort.join(' ') }
11
12 # L<S02/Immutable types/'the set listop'>
13
14 {
15 my $s = KeySet.new(<a b foo>);
16 isa_ok $s, KeySet, 'KeySet.new produces a KeySet';
17 is showset($s), 'a b foo', '...with the right elements';
18
19 is $s<a>, True, 'Single-key subscript (existing element)';
9da8a25 Solomon Foster 59 additional tests ported over from set.t.
colomon authored
20 isa_ok $s<a>, Bool, 'Single-key subscript has correct type (existing element)';
f2903a0 Solomon Foster Lots more KeySet tests, inspired by the Set tests. Also fudge for Niecz...
colomon authored
21 is $s<santa>, False, 'Single-key subscript (nonexistent element)';
9da8a25 Solomon Foster 59 additional tests ported over from set.t.
colomon authored
22 isa_ok $s<santa>, Bool, 'Single-key subscript has correct type (nonexistent element)';
f2903a0 Solomon Foster Lots more KeySet tests, inspired by the Set tests. Also fudge for Niecz...
colomon authored
23 is $s.exists('a'), True, '.exists with existing element';
24 is $s.exists('santa'), False, '.exists with nonexistent element';
25
9da8a25 Solomon Foster 59 additional tests ported over from set.t.
colomon authored
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
f2903a0 Solomon Foster Lots more KeySet tests, inspired by the Set tests. Also fudge for Niecz...
colomon authored
35 dies_ok { $s.keys = <c d> }, "Can't assign to .keys";
36 dies_ok { $s.values = <True False> }, "Can't assign to .values";
37
38 is ($s<a b>).grep(?*).elems, 2, 'Multiple-element access';
39 is ($s<a santa b easterbunny>).grep(?*).elems, 2, 'Multiple-element access (with nonexistent elements)';
40
41 is $s.elems, 3, '.elems gives number of keys';
42 is +$s, 3, '+$set gives number of keys';
43
44 $s<baz> = True;
45 lives_ok { $s<baz> = True }, 'can set an item to True';
46 is showset($s), 'a b baz foo', '...and it adds it to the KeySet';
47 lives_ok { $s<baz> = True }, 'can set the same item to True';
48 is showset($s), 'a b baz foo', '...and it does nothing';
49
50 lives_ok { $s<baz> = False }, 'can set an item to False';
51 is showset($s), 'a b foo', 'and it removes it';
52 lives_ok { $s<baz> = False }, 'can set an item which does not exist to False';
53 is showset($s), 'a b foo', '... and it is not added to the set';
54
55 lives_ok { $s<foo> = False }, 'can set an item to False';
56 is $s.elems, 2, '... and an item is gone';
57 is showset($s), 'a b', '... and the right one is gone';
58
59 lives_ok { $s<foo>++ }, 'can ++ an item';
60 is showset($s), 'a b foo', '++ on an item reinstates it';
61 lives_ok { $s<foo>++ }, 'can ++ an item';
62 is showset($s), 'a b foo', '++ on an existing item does nothing';
63
64 lives_ok { $s<b>-- }, 'can -- an item';
65 is showset($s), 'a foo', '-- on an item removes it';
66 lives_ok { $s<bar>-- }, 'can -- an item';
67 is showset($s), 'a foo', '... but only if they were there to start with';
68 }
69
71f1532 Tadeusz Sośnierz Tune and fudge bag, keybag, keyset and set tests for Rakudo and for Grea...
tadzik authored
70 #?rakudo skip ':exists and :delete NYI'
f2903a0 Solomon Foster Lots more KeySet tests, inspired by the Set tests. Also fudge for Niecz...
colomon authored
71 {
df27e0d Solomon Foster Add tests for Set et al :exists and :delete.
colomon authored
72 my $s = KeySet.new(<a b foo>);
73 is $s<a>:exists, True, ':exists with existing element';
74 is $s<santa>:exists, False, ':exists with nonexistent element';
75 is $s<a>:delete, True, ':delete returns current value on set';
76 is showset($s), 'b foo', '...and actually deletes';
77 }
78
79 {
f2903a0 Solomon Foster Lots more KeySet tests, inspired by the Set tests. Also fudge for Niecz...
colomon authored
80 my %h := KeySet.new(<a c>);
81 is +%h.elems, 2, 'Inititalization worked';
82
83 lives_ok { %h<c> = False }, 'can set an item to False';
84 is %h.elems, 1, '... and an item is gone';
85 is ~%h.keys, 'a', '... and the right one is gone';
86
87 %h<c>++;
88 is %h.keys.sort.join, 'ac', '++ on an item reinstates it';
89 %h<c>++;
90 is %h.keys.sort.join, 'ac', '++ on an existing item does nothing';
91
92 %h<a>--;
93 is ~%h.keys, 'c', '-- removes items';
94 %h<b>--;
95 is ~%h.keys, 'c', '... but only if they were there from the beginning';
96
97 # lives_ok { %h = set <Q P R> }, 'Assigning a Set to a KeySet';
98 # is %h.keys.sort.join, 'PQR', '... works as expected';
99 }
100
9da8a25 Solomon Foster 59 additional tests ported over from set.t.
colomon authored
101 {
102 my $s = KeySet.new(<foo bar foo bar baz foo>);
103 is showset($s), 'bar baz foo', 'KeySet.new discards duplicates';
104 }
105
106 {
107 my $b = KeySet.new([ foo => 10, bar => 17, baz => 42 ]);
108 isa_ok $b, KeySet, 'KeySet.new given an array of pairs produces a KeySet';
109 is showset($b), 'bar baz foo', '... with the right elements';
110 }
111
112 {
113 my $b = KeySet.new({ foo => 10, bar => 17, baz => 42 }.hash);
114 isa_ok $b, KeySet, 'KeySet.new given a Hash produces a KeySet';
115 is showset($b), 'bar baz foo', '... with the right elements';
116 }
117
118 {
119 my $b = KeySet.new({ foo => 10, bar => 17, baz => 42 });
120 isa_ok $b, KeySet, 'KeySet.new given a Hash produces a KeySet';
121 is showset($b), 'bar baz foo', '... with the right elements';
122 }
123
124 {
125 my $b = KeySet.new(set <foo bar foo bar baz foo>);
126 isa_ok $b, KeySet, 'KeySet.new given a Set produces a KeySet';
127 is showset($b), 'bar baz foo', '... with the right elements';
128 }
129
130 {
131 my $b = KeySet.new(KeySet.new(<foo bar foo bar baz foo>));
132 isa_ok $b, KeySet, 'KeySet.new given a KeySet produces a KeySet';
133 is showset($b), 'bar baz foo', '... with the right elements';
134 }
135
136 {
137 my $b = KeySet.new(KeyBag.new(<foo bar foo bar baz foo>));
138 isa_ok $b, KeySet, 'KeySet.new given a KeyBag produces a KeySet';
139 is showset($b), 'bar baz foo', '... with the right elements';
140 }
141
142 {
143 my $b = KeySet.new(bag <foo bar foo bar baz foo>);
144 isa_ok $b, KeySet, 'KeySet given a Bag produces a KeySet';
145 is showset($b), 'bar baz foo', '... with the right elements';
146 }
147
148 {
149 my $s = KeySet.new(<foo bar baz>);
150 isa_ok $s.list.elems, 3, ".list returns 3 things";
151 is $s.list.grep(Str).elems, 3, "... all of which are Str";
152 is $s.iterator.grep(Str).elems, 3, ".iterator yields three Strs";
153 }
154
155 {
156 my $s = KeySet.new(<foo bar baz>);
157 my $str;
158 my $c;
159 lives_ok { $str = $s.perl }, ".perl lives";
160 isa_ok $str, Str, "... and produces a string";
161 lives_ok { $c = eval $str }, ".perl.eval lives";
162 isa_ok $c, KeySet, "... and produces a KeySet";
163 is showset($c), showset($s), "... and it has the correct values";
164 }
165
166 {
014f5f8 Solomon Foster Modify tests for Set.Str and KeySet.Str to conform to TimToady's suggest...
colomon authored
167 my $s = set <foo bar baz>;
9da8a25 Solomon Foster 59 additional tests ported over from set.t.
colomon authored
168 lives_ok { $s = $s.Str }, ".Str lives";
169 isa_ok $s, Str, "... and produces a string";
014f5f8 Solomon Foster Modify tests for Set.Str and KeySet.Str to conform to TimToady's suggest...
colomon authored
170 is $s.split(" ").sort.join(" "), "bar baz foo", "... which only contains bar baz and foo separated by spaces";
9da8a25 Solomon Foster 59 additional tests ported over from set.t.
colomon authored
171 }
172
173 {
174 my $s = KeySet.new(<foo bar baz>);
175 lives_ok { $s = $s.gist }, ".gist lives";
176 isa_ok $s, Str, "... and produces a string";
177 ok $s ~~ /foo/, "... which mentions foo";
178 ok $s ~~ /bar/, "... which mentions bar";
179 ok $s ~~ /baz/, "... which mentions baz";
180 }
181
182 # L<S02/Names and Variables/'C<%x> may be bound to'>
183
184 {
185 my %s := KeySet.new(<a b c b>);
186 isa_ok %s, KeySet, 'A KeySet bound to a %var is a KeySet';
187 is showset(%s), 'a b c', '...with the right elements';
188
189 is %s<a>, True, 'Single-key subscript (existing element)';
190 is %s<santa>, False, 'Single-key subscript (nonexistent element)';
191
192 lives_ok { %s<a> = True }, "Can assign to an element (KeySets are immutable)";
193 }
194
195 # L<S32::Containers/KeySet/roll>
196
197 {
198 my $s = KeySet.new(<a b c>);
199
200 my $a = $s.roll;
201 ok $a eq "a" || $a eq "b" || $a eq "c", "We got one of the three choices";
202
203 my @a = $s.roll(2);
204 is +@a, 2, '.roll(2) returns the right number of items';
205 is @a.grep(* eq 'a' | 'b' | 'c').elems, 2, '.roll(2) returned "a"s, "b"s, and "c"s';
206
71f1532 Tadeusz Sośnierz Tune and fudge bag, keybag, keyset and set tests for Rakudo and for Grea...
tadzik authored
207 @a = $s.roll: 100;
9da8a25 Solomon Foster 59 additional tests ported over from set.t.
colomon authored
208 is +@a, 100, '.roll(100) returns 100 items';
209 is @a.grep(* eq 'a' | 'b' | 'c').elems, 100, '.roll(100) returned "a"s, "b"s, and "c"s';
210 }
211
212 # L<S32::Containers/KeySet/pick>
213
214 {
215 my $s = KeySet.new(<a b c d e f g h>);
216 my @a = $s.pick: *;
217 is @a.sort.join, 'abcdefgh', 'KeySet.pick(*) gets all elements';
218 isnt @a.join, 'abcdefgh', 'KeySet.pick(*) returns elements in a random order';
219 # There's only a 1/40_320 chance of that test failing by chance alone.
220 }
221
222 {
223 my $s = KeySet.new(<a b c>);
224
225 my $a = $s.pick;
226 ok $a eq "a" || $a eq "b" || $a eq "c", "We got one of the three choices";
227
228 my @a = $s.pick(2);
229 is +@a, 2, '.pick(2) returns the right number of items';
230 is @a.grep(* eq 'a' | 'b' | 'c').elems, 2, '.pick(2) returned "a"s, "b"s, and "c"s';
231 ok @a.grep(* eq 'a').elems <= 1, '.pick(2) returned at most one "a"';
232 ok @a.grep(* eq 'b').elems <= 1, '.pick(2) returned at most one "b"';
233 ok @a.grep(* eq 'c').elems <= 1, '.pick(2) returned at most one "c"';
234 }
235
f2903a0 Solomon Foster Lots more KeySet tests, inspired by the Set tests. Also fudge for Niecz...
colomon authored
236 #?niecza skip "is KeySet doesn't work yet"
71b520d [t/spec/S02-builtin_data_types/keyset.t] added some basic tests
masak authored
237 {
264f008 Additions, corrections, and reformatting for KeySet, KeyBag, and KeyWeig...
Kodi Arfer authored
238 my %h is KeySet = a => True, b => False, c => True;
71f1532 Tadeusz Sośnierz Tune and fudge bag, keybag, keyset and set tests for Rakudo and for Grea...
tadzik authored
239 #?rakudo todo 'todo'
71b520d [t/spec/S02-builtin_data_types/keyset.t] added some basic tests
masak authored
240 is +%h.elems, 2, 'Inititalization worked';
264f008 Additions, corrections, and reformatting for KeySet, KeyBag, and KeyWeig...
Kodi Arfer authored
241
242 lives_ok { %h<c> = False }, 'can set an item to False';
71f1532 Tadeusz Sośnierz Tune and fudge bag, keybag, keyset and set tests for Rakudo and for Grea...
tadzik authored
243 #?rakudo todo 'todo'
71b520d [t/spec/S02-builtin_data_types/keyset.t] added some basic tests
masak authored
244 is %h.elems, 1, '... and an item is gone';
71f1532 Tadeusz Sośnierz Tune and fudge bag, keybag, keyset and set tests for Rakudo and for Grea...
tadzik authored
245 #?rakudo todo 'todo'
264f008 Additions, corrections, and reformatting for KeySet, KeyBag, and KeyWeig...
Kodi Arfer authored
246 is ~%h.keys, 'a', '... and the right one is gone';
247
71b520d [t/spec/S02-builtin_data_types/keyset.t] added some basic tests
masak authored
248 %h<c>++;
71f1532 Tadeusz Sośnierz Tune and fudge bag, keybag, keyset and set tests for Rakudo and for Grea...
tadzik authored
249 #?rakudo todo 'todo'
49d046f .join now defaults to ''
lwall authored
250 is %h.keys.sort.join, 'ac', '++ on an item reinstates it';
d703da4 [t/spec] one more test for keyset.t; add smartlink
moritz authored
251 %h<c>++;
71f1532 Tadeusz Sośnierz Tune and fudge bag, keybag, keyset and set tests for Rakudo and for Grea...
tadzik authored
252 #?rakudo todo 'todo'
49d046f .join now defaults to ''
lwall authored
253 is %h.keys.sort.join, 'ac', '++ on an existing item does nothing';
264f008 Additions, corrections, and reformatting for KeySet, KeyBag, and KeyWeig...
Kodi Arfer authored
254
71b520d [t/spec/S02-builtin_data_types/keyset.t] added some basic tests
masak authored
255 %h<a>--;
71f1532 Tadeusz Sośnierz Tune and fudge bag, keybag, keyset and set tests for Rakudo and for Grea...
tadzik authored
256 #?rakudo todo 'todo'
71b520d [t/spec/S02-builtin_data_types/keyset.t] added some basic tests
masak authored
257 is ~%h.keys, 'c', '-- removes items';
258 %h<b>--;
71f1532 Tadeusz Sośnierz Tune and fudge bag, keybag, keyset and set tests for Rakudo and for Grea...
tadzik authored
259 #?rakudo todo 'todo'
71b520d [t/spec/S02-builtin_data_types/keyset.t] added some basic tests
masak authored
260 is ~%h.keys, 'c', '... but only if they were there from the beginning';
264f008 Additions, corrections, and reformatting for KeySet, KeyBag, and KeyWeig...
Kodi Arfer authored
261
71f1532 Tadeusz Sośnierz Tune and fudge bag, keybag, keyset and set tests for Rakudo and for Grea...
tadzik authored
262 #?rakudo todo 'todo'
264f008 Additions, corrections, and reformatting for KeySet, KeyBag, and KeyWeig...
Kodi Arfer authored
263 lives_ok { %h = set <Q P R> }, 'Assigning a Set to a KeySet';
71f1532 Tadeusz Sośnierz Tune and fudge bag, keybag, keyset and set tests for Rakudo and for Grea...
tadzik authored
264 #?rakudo todo 'todo'
264f008 Additions, corrections, and reformatting for KeySet, KeyBag, and KeyWeig...
Kodi Arfer authored
265 is %h.keys.sort.join, 'PQR', '... works as expected';
71b520d [t/spec/S02-builtin_data_types/keyset.t] added some basic tests
masak authored
266 }
267
268 # vim: ft=perl6
Something went wrong with that request. Please try again.