Skip to content

Commit cf643f6

Browse files
committed
Add 21 new tests. Still needs more.
1 parent 2ee3f57 commit cf643f6

File tree

1 file changed

+50
-11
lines changed

1 file changed

+50
-11
lines changed

S02-types/keybag.t

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use v6;
22
use Test;
33

4-
plan 69;
4+
plan 90;
55

66
# L<S02/Mutable types/KeyHash of UInt>
77

@@ -19,10 +19,22 @@ sub showkv($x) {
1919
is showkv($b), 'a:5 b:1 foo:2', '...with the right elements';
2020

2121
is $b<a>, 5, 'Single-key subscript (existing element)';
22+
isa_ok $b<a>, Int, 'Single-key subscript yields an Int';
2223
is $b<santa>, 0, 'Single-key subscript (nonexistent element)';
24+
isa_ok $b<santa>, Int, 'Single-key subscript yields an Int (nonexistent element)';
2325
ok $b.exists('a'), '.exists with existing element';
2426
nok $b.exists('santa'), '.exists with nonexistent element';
2527

28+
is $b.values.elems, 3, "Values returns the correct number of values";
29+
is ([+] $b.values), 8, "Values returns the correct sum";
30+
ok ?$b, "Bool returns True if there is something in the KeyBag";
31+
nok ?KeyBag.new(), "Bool returns False if there is nothing in the KeyBag";
32+
33+
my $hash;
34+
lives_ok { $hash = $b.hash }, ".hash doesn't die";
35+
isa_ok $hash, Hash, "...and it returned a Hash";
36+
is showkv($hash), 'a:5 b:1 foo:2', '...with the right elements';
37+
2638
dies_ok { $b.keys = <c d> }, "Can't assign to .keys";
2739
dies_ok { $b.values = 3, 4 }, "Can't assign to .values";
2840

@@ -37,6 +49,10 @@ sub showkv($x) {
3749
is $b<a>, 42, "... and assignment takes effect";
3850
lives_ok { $b<brady> = 12 }, "Can assign to a new element";
3951
is $b<brady>, 12, "... and assignment takes effect";
52+
lives_ok { $b<spiderman> = 0 }, "Can assign zero to a nonexistent element";
53+
nok $b.exists("spiderman"), "... and that didn't create the element";
54+
lives_ok { $b<brady> = 0 }, "Can assign zero to a existing element";
55+
nok $b.exists("brady"), "... and it goes away";
4056

4157
lives_ok { $b<a>++ }, "Can ++ an existing element";
4258
is $b<a>, 43, "... and the increment happens";
@@ -69,40 +85,63 @@ sub showkv($x) {
6985
}
7086

7187
{
72-
my $b = bag set <foo bar foo bar baz foo>;
73-
isa_ok $b, Bag, '&bag given a Set produces a Bag';
88+
my $b = KeyBag.new(set <foo bar foo bar baz foo>);
89+
isa_ok $b, KeyBag, '&KeyBag.new given a Set produces a KeyBag';
7490
is showkv($b), 'bar:1 baz:1 foo:1', '... with the right elements';
7591
}
7692

93+
{
94+
my $b = KeyBag.new(KeySet.new(set <foo bar foo bar baz foo>));
95+
isa_ok $b, KeyBag, '&KeyBag.new given a KeySet produces a KeyBag';
96+
is showkv($b), 'bar:1 baz:1 foo:1', '... with the right elements';
97+
}
98+
99+
{
100+
my $b = KeyBag.new(bag set <foo bar foo bar baz foo>);
101+
isa_ok $b, KeyBag, '&KeyBag.new given a Bag produces a KeyBag';
102+
is showkv($b), 'bar:1 baz:1 foo:1', '... with the right elements';
103+
}
104+
105+
{
106+
my $b = KeyBag.new(set <foo bar foo bar baz foo>);
107+
$b<bar> += 2;
108+
my $c = KeyBag.new($b);
109+
isa_ok $c, KeyBag, '&KeyBag.new given a KeyBag produces a KeyBag';
110+
is showkv($c), 'bar:3 baz:1 foo:1', '... with the right elements';
111+
$c<manning> = 10;
112+
is showkv($c), 'bar:3 baz:1 foo:1 manning:10', 'Creating a new element works';
113+
is showkv($b), 'bar:3 baz:1 foo:1', '... and does not affect the original KeyBag';
114+
}
115+
77116
# L<S02/Names and Variables/'C<%x> may be bound to'>
78117

79118
{
80-
my %b := bag <a b c b>;
81-
isa_ok %b, Bag, 'A Bag bound to a %var is a Bag';
119+
my %b := KeyBag.new("a", "b", "c", "b");
120+
isa_ok %b, KeyBag, 'A KeyBag bound to a %var is a KeyBag';
82121
is showkv(%b), 'a:1 b:2 c:1', '...with the right elements';
83122

84123
is %b<b>, 2, 'Single-key subscript (existing element)';
85124
is %b<santa>, 0, 'Single-key subscript (nonexistent element)';
86125

87-
dies_ok { %b<a> = 1 }, "Can't assign to an element (Bags are immutable)";
88-
dies_ok { %b = bag <a b> }, "Can't assign to a %var implemented by Bag";
126+
lives_ok { %b<a> = 4 }, "Assign to an element";
127+
is %b<a>, 4, "... and gets the correct value";
89128
}
90129

91-
# L<S32::Containers/Bag/pick>
130+
# L<S32::Containers/KeyBag/pick>
92131

93132
{
94-
my $b = bag <a b b>;
133+
my $b = KeyBag.new("a", "b", "b");
95134

96135
my @a = $b.pick: *;
97136
is +@a, 3, '.pick(*) returns the right number of items';
98137
is @a.grep(* eq 'a').elems, 1, '.pick(*) (1)';
99138
is @a.grep(* eq 'b').elems, 2, '.pick(*) (2)';
100139
}
101140

102-
# L<S32::Containers/Bag/roll>
141+
# L<S32::Containers/KeyBag/roll>
103142

104143
{
105-
my $b = bag <a b b>;
144+
my $b = KeyBag.new("a", "b", "b");
106145

107146
my @a = $b.roll: 100;
108147
is +@a, 100, '.roll(100) returns 100 items';

0 commit comments

Comments
 (0)