Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
More tests for when/default.
To exercise semantics update coming in S04 design doc.
  • Loading branch information
jnthn committed Jul 9, 2015
1 parent 5aa33ab commit c43e6a7
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions S04-statements/when.t
@@ -1,6 +1,6 @@
use Test;

plan 12;
plan 18;

my $c = { when 1 { 'one' }; when 2 { 'two!' }; default { 'many' } };
is $c(1), 'one', 'when works in a circumfix:<{ }> (1)';
Expand All @@ -12,14 +12,36 @@ is $p(1), 'one', 'when works in a pointy block declaring $_ (1)';
is $p(2), 'two!', 'when works in a pointy block declaring $_ (2)';
is $p(3), 'many', 'default works in a pointy block declaring $_';

my $pi = -> { $_ = 4; when 1 { 'one' }; when 2 { 'two!' }; default { 'many' } };
is $pi(), 'many', 'when works in a pointy block using its implicit $_';

sub foo($_) { when 1 { 'one' }; when 2 { 'two!' }; default { 'many' } }
is foo(1), 'one', 'when works in a sub declaring $_ (1)';
is foo(2), 'two!', 'when works in a sub declaring $_ (2)';
is foo(3), 'many', 'default works in a sub declaring $_';

sub bar() { $_ = 2; when 1 { 'one' }; when 2 { 'two!' }; default { 'many' } }
is bar(), 'two!', 'when works in sub using its implicit $_';

class C {
method m($_) { when 1 { 'one' }; when 2 { 'two!' }; default { 'many' } }
method i() { $_ = 1; when 1 { 'one' }; when 2 { 'two!' }; default { 'many' } }
}
is C.m(1), 'one', 'when works in a method declaring $_ (1)';
is C.m(2), 'two!', 'when works in a method declaring $_ (2)';
is C.m(3), 'many', 'default works in a method declaring $_';
is C.i, 'one', 'when works in a method using its implicit $_';

my $nest = sub ($n) {
$_ = $n * 2;
when * > 2 {
when 4 { 'four!' }
default { 'huge' }
}
default {
'little'
}
}
is C.m(1), 'one', 'when works in a sub declaring $_ (1)';
is C.m(2), 'two!', 'when works in a sub declaring $_ (2)';
is C.m(3), 'many', 'default works in a sub declaring $_';
is $nest(1), 'little', 'nested when in a sub works (1)';
is $nest(2), 'four!', 'nested when in a sub works (2)';
is $nest(3), 'huge', 'nested when in a sub works (3)';

0 comments on commit c43e6a7

Please sign in to comment.