Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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