Skip to content

Commit 207ad2b

Browse files
committed
Test autovivification of Setties and Baggies works right
1 parent 37c8fd4 commit 207ad2b

File tree

6 files changed

+90
-6
lines changed

6 files changed

+90
-6
lines changed

S02-types/bag.t

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

4-
plan 206;
4+
plan 207;
55

66
sub showkv($x) {
77
$x.keys.sort.map({ $^k ~ ':' ~ $x{$k} }).join(' ')
@@ -530,4 +530,9 @@ subtest '.hash does not cause keys to be stringified' => {
530530
is bag($(<a b>)).hash.keys[0][0], 'a', 'bag()';
531531
}
532532

533+
{
534+
throws-like { my Bag $b; $b<as> = 2 }, X::Assignment::RO,
535+
'autovivification of of Bag:U complains about immutability';
536+
}
537+
533538
# vim: ft=perl6

S02-types/baghash.t

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

4-
plan 264;
4+
plan 265;
55

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

@@ -596,4 +596,28 @@ subtest '.hash does not cause keys to be stringified' => {
596596
is-deeply $bh.Mix, Mix.new(<a a b>), '.Mix values are correct';
597597
}
598598

599+
subtest 'BagHash autovivification of non-existent keys' => {
600+
my BagHash $bh1;
601+
is-deeply $bh1<poinc>++, 0, 'correct return of postfix ++';
602+
is-deeply $bh1<poinc>, 1, 'correct result of postfix ++';
603+
604+
my BagHash $bh2;
605+
is-deeply $bh2<podec>--, 0, 'correct return of postfix --';
606+
# Bags don't have negatives, so 0 is the expected result:
607+
is-deeply $bh2<podec>, 0, 'correct result of postfix --';
608+
609+
my BagHash $bh3;
610+
is-deeply ++$bh3<princ>, 1, 'correct return of prefix ++';
611+
is-deeply $bh3<princ>, 1, 'correct result of prefix ++';
612+
613+
my BagHash $bh4;
614+
# Bags don't have negatives, so 0 is the expected result:
615+
is-deeply --$bh4<prdec>, 0, 'correct return of prefix --';
616+
is-deeply $bh4<prdec>, 0, 'correct result of prefix --';
617+
618+
my BagHash $bh5;
619+
is-deeply ($bh5<as> = 2), 2, 'correct return of assignment';
620+
is-deeply $bh5<as>, 2, 'correct result of assignment';
621+
}
622+
599623
# vim: ft=perl6

S02-types/mix.t

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

4-
plan 195;
4+
plan 196;
55

66
sub showkv($x) {
77
$x.keys.sort.map({ $^k ~ ':' ~ $x{$k} }).join(' ')
@@ -479,4 +479,9 @@ subtest '.hash does not cause keys to be stringified' => {
479479
'.BagHash coercer';
480480
}
481481

482+
{
483+
throws-like { my Mix $m; $m<as> = 2 }, X::Assignment::RO,
484+
'autovivification of of Mix:U complains about immutability';
485+
}
486+
482487
# vim: ft=perl6

S02-types/mixhash.t

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

4-
plan 232;
4+
plan 233;
55

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

@@ -520,4 +520,26 @@ subtest '.hash does not cause keys to be stringified' => {
520520
'.BagHash coercer';
521521
}
522522

523+
subtest 'MixHash autovivification of non-existent keys' => {
524+
my MixHash $mh1;
525+
is-deeply $mh1<poinc>++, 0, 'correct return of postfix ++';
526+
is-deeply $mh1<poinc>, 1, 'correct result of postfix ++';
527+
528+
my MixHash $mh2;
529+
is-deeply $mh2<podec>--, 0, 'correct return of postfix --';
530+
is-deeply $mh2<podec>, -1, 'correct result of postfix --';
531+
532+
my MixHash $mh3;
533+
is-deeply ++$mh3<princ>, 1, 'correct return of prefix ++';
534+
is-deeply $mh3<princ>, 1, 'correct result of prefix ++';
535+
536+
my MixHash $mh4;
537+
is-deeply --$mh4<prdec>, -1, 'correct return of prefix --';
538+
is-deeply $mh4<prdec>, -1, 'correct result of prefix --';
539+
540+
my MixHash $mh5;
541+
is-deeply ($mh5<as> = 2), 2, 'correct return of assignment';
542+
is-deeply $mh5<as>, 2, 'correct result of assignment';
543+
}
544+
523545
# vim: ft=perl6

S02-types/set.t

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

4-
plan 178;
4+
plan 179;
55

66
sub showset($s) { $s.keys.sort.join(' ') }
77

@@ -424,4 +424,9 @@ subtest '.hash does not cause keys to be stringified' => {
424424
is set($(<a b>),).hash.keys[0][0], 'a', 'set()';
425425
}
426426

427+
{
428+
throws-like { my Set $s; $s<as> = 2 }, X::Assignment::RO,
429+
'autovivification of of Set:U complains about immutability';
430+
}
431+
427432
# vim: ft=perl6

S02-types/sethash.t

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

4-
plan 208;
4+
plan 209;
55

66
# L<S02/Mutable types/"QuantHash of Bool">
77

@@ -453,5 +453,28 @@ subtest '.hash does not cause keys to be stringified' => {
453453
is-deeply $sh.SetHash, $sh, '.SetHash returns self';
454454
}
455455

456+
subtest 'SetHash autovivification of non-existent keys' => {
457+
# Sets' values are just True/False, so all of the following operations
458+
# simply control existence of a key
459+
my SetHash $sh1;
460+
is-deeply $sh1<poinc>++, Bool::False, 'correct return of postfix ++';
461+
is-deeply $sh1<poinc>, Bool::True, 'correct result of postfix ++';
462+
463+
my SetHash $sh2;
464+
is-deeply $sh2<podec>--, Bool::False, 'correct return of postfix --';
465+
is-deeply $sh2<podec>, Bool::False, 'correct result of postfix --';
466+
467+
my SetHash $sh3;
468+
is-deeply ++$sh3<princ>, Bool::True, 'correct return of prefix ++';
469+
is-deeply $sh3<princ>, Bool::True, 'correct result of prefix ++';
470+
471+
my SetHash $sh4;
472+
is-deeply --$sh4<prdec>, Bool::False, 'correct return of prefix --';
473+
is-deeply $sh4<prdec>, Bool::False, 'correct result of prefix --';
474+
475+
my SetHash $sh5;
476+
is-deeply ($sh5<as> = 2), Bool::True, 'correct return of assignment';
477+
is-deeply $sh5<as>, Bool::True, 'correct result of assignment';
478+
}
456479

457480
# vim: ft=perl6

0 commit comments

Comments
 (0)