Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add typecheck tests for BagHash/MixHash
  • Loading branch information
lizmat committed Oct 4, 2015
1 parent 877e74c commit 44feca7
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
31 changes: 30 additions & 1 deletion S02-types/baghash.t
@@ -1,7 +1,7 @@
use v6;
use Test;

plan 246;
plan 254;

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

Expand Down Expand Up @@ -532,3 +532,32 @@ sub showkv($x) {
ok $b3.elems == 0,
'named argument is happily eaten by .new method';
}

{
my $b = <a>.BagHash;
$b<a> = 42;
is $b<a>, 42, 'did we set an Int value';
throws-like { $b<a> = "foo" },
X::Multi::NoMatch, # X::TypeCheck::Assignment ???
'Make sure we cannot assign Str on a key';

$_ = 666 for $b.values;
is $b<a>, 666, 'did we set an Int value from a .values alias';
throws-like { $_ = "foo" for $b.values },
X::TypeCheck::Assignment,
'Make sure we cannot assign Str on a .values alias';

.value = 999 for $b.pairs;
is $b<a>, 999, 'did we set an Int value from a .pairs alias';
throws-like { .value = "foo" for $b.pairs },
X::TypeCheck::Assignment,
'Make sure we cannot assign Str on a .pairs alias';

for $b.kv -> \k, \v { v = 22 };
is $b<a>, 22, 'did we set an Int value from a .kv alias';
throws-like { for $b.kv -> \k, \v { v = "foo" } },
X::TypeCheck::Assignment,
'Make sure we cannot assign Str on a .kv alias';
}

# vim: ft=perl6
29 changes: 28 additions & 1 deletion S02-types/mixhash.t
@@ -1,7 +1,7 @@
use v6;
use Test;

plan 209;
plan 217;

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

Expand Down Expand Up @@ -450,4 +450,31 @@ sub showkv($x) {
is $e.fmt('%s,%s',':'), "", '.fmt(%s%s,sep) works (empty)';
}

{
my $m = <a>.MixHash;
$m<a> = 42.1;
is $m<a>, 42.1, 'did we set a Real value';
throws-like { $m<a> = "foo" },
X::Str::Numeric, # X::TypeCheck::Assignment ???
'Make sure we cannot assign Str on a key';

$_ = 666.1 for $m.values;
is $m<a>, 666.1, 'did we set a Real value from a .values alias';
throws-like { $_ = "foo" for $m.values },
X::TypeCheck::Assignment,
'Make sure we cannot assign Str on a .values alias';

.value = 999.1 for $m.pairs;
is $m<a>, 999.1, 'did we set a Real value from a .pairs alias';
throws-like { .value = "foo" for $m.pairs },
X::TypeCheck::Assignment,
'Make sure we cannot assign Str on a .pairs alias';

for $m.kv -> \k, \v { v = 22.1 };
is $m<a>, 22.1, 'did we set a Real value from a .kv alias';
throws-like { for $m.kv -> \k, \v { v = "foo" } },
X::TypeCheck::Assignment,
'Make sure we cannot assign Str on a .kv alias';
}

# vim: ft=perl6

0 comments on commit 44feca7

Please sign in to comment.