Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add test files for with/without modifiers
  • Loading branch information
TimToady committed Aug 8, 2015
1 parent a56dd50 commit 8a752ca
Show file tree
Hide file tree
Showing 2 changed files with 263 additions and 0 deletions.
207 changes: 207 additions & 0 deletions S04-statement-modifiers/with.t
@@ -0,0 +1,207 @@
use v6;

use Test;

plan 34;

# Test the topical aspects of the with statement modifier

# test the ``with'' statement modifier
{
my $a = 0;
$a = $_ with 2 * 3;
is($a, 6, "post with");
}

# test the ``with'' statement modifier
{
my $a;
$a = $_ with 2 * 3;
is($a, 6, "post with");
}

{
my $a = '';
$a = $_ with 'a';
is($a, 'a', "post with");
}

# RT #121049
{
my $a = '';
for ^2 { my $b = $_ with 'a'; $a ~= $b; }
is($a, 'aa', 'post with in a loop');
}

# L<S04/The C<for> statement/for and with privately temporize>
{
my $i = 0;
$_ = 10;
$i += $_ with $_+3;
is $_, 10, 'outer $_ did not get updated in lhs of with';
is $i, 13, 'postfix with worked';
}

# RT #100746
{
$_ = 'bogus';
my @r = gather { take "{$_}" with 'cool' }
is @r[0], 'cool', 'with modifies the $_ that is visible to the {} interpolator';
}

# RT #111704
{
my $a = 'many ';
try { $a ~= $_ } with 'pelmeni';
is $a, 'many pelmeni', 'Correct $_ in try block in statement-modifying with';
}

{
my $a;
{ $a = $^x } with 69;
is $a, 69, 'with modifier with $_-using block runs block with correct arg';
}

{
my $a;
{ $a = $^x } with 42;
is $a, 42, 'with modifier with placeholder block runs block with correct arg';
}

{
# Covers a bug where the block to first got compiled in the 'with' thunk
my @a;
for ^2 -> \c { 1 with first { @a.push(c); 0 }, ^2; };
is @a, (0, 0, 1, 1), 'with thunk does not mess up statement modifier closures';
}

# Test the conditional of the with statement modifier

{
my $a = 1;
$a = 2 with 'a' eq 'a';
is($a, 2, "post with True");
}

{
my $a = 1;
$a = 3 with 'a' eq 'b';
is($a, 3, "post with False");
}

{
my $a = 1;
$a = 3 with Int;
is($a, 1, "post with type object");
}

{
my $answer = 1;
my @x = 41, (42 with $answer), 43;
my @y = 41, ($answer andthen 42), 43;
my @z = 41, 42, 43;
is @y, @z, "sanity check";
is @x, @y, "with expr on true cond";
}

{
my $answer = 0;
my @x = 41, (42 with $answer), 43;
my @y = 41, ($answer andthen 42), 43;
my @z = 41, 42, 43;
is @y, @z, "sanity check";
is @x, @y, "with expr on true cond";
}

{
my $answer = Nil;
my @x = 41, (42 with $answer), 43;
my @y = 41, ($answer andthen 42), 43;
my @z = 41, 43;
is @y, @z, "sanity check";
#?niecza todo "empty list as element not flattened - https://github.com/sorear/niecza/issues/180"
is @x, @y, "with expr on false cond";
}

{
my $answer = Failure.new;
my @x = 41, (42 with $answer), 43;
my @y = 41, ($answer andthen 42), 43;
my @z = 41, 43;
is @y, @z, "sanity check";
#?niecza todo "empty list as element not flattened - https://github.com/sorear/niecza/issues/180"
is @x, @y, "with expr on false cond";
}

{
sub foo() {
return () with 1;
123;
}

my $ok = 1;
for foo() -> @foo {
$ok = 0;
}
ok $ok, "condition in statement level respects context"
}

{
my $x = (3 with 1);
my $y = (3 with 0);
my $z = (3 with Nil);
is $x, 3, '(3 with 1) worked in scalar context';
is $y, 3, '(3 with 0) worked in scalar context';
ok !$z, 'my $y = (3 with Nil) leaves $y false';
}

{
is (42 with Nil), Empty, '"$something with Nil" is Empty';
}

{
my $a = 'oops';
{ $a = 'ok' } with 1;
is $a, 'ok', 'Statement-modifier with runs bare block';
}

{
my $a = 'oops';
$a = $_ with 2;
is $a, 2, 'Statement-modifier with runs bare code with topic';
}

{
my $a = 'oops';
{ $a = $^x } with 100;
is $a, 100, 'Statement-modifier with runs block with placeholder';
}

{
my $ok = 0;
my @a = 1; $ok = 1 with @a;
ok $ok, "with treats @a with elems as defined";
}

# List comprehensions

{
my @x;
(push @x, .abs with 12 div $_) for 0..4;
is @x, (12,6,4,3), 'with/for list comprehension works with parens';
}

#?rakudo todo "parenless with/for doesn't work yet"
{
my @x;
try push @x, .abs with 12 div $_ for 0..4;
is @x, (12,6,4,3), 'with/for list comprehension works without parens';
}

{
my @x;
(push @x, .WHAT.gist without 12 div $_) for 0..4;
is @x, '(Failure)', 'without/for list comprehension works with parens';
}

# vim: ft=perl6
56 changes: 56 additions & 0 deletions S04-statement-modifiers/without.t
@@ -0,0 +1,56 @@
use v6;

use Test;

plan 8;

# test the unless statement modifier

{
my $a = 1;
$a = 4 without 'a' eq 'a';
is($a, 1, "post without True");
}

{
my $a = 1;
$a = 5 without Nil;
is($a, 5, "post without Nil");
}

{
my $a = 1;
$a = $_ without Int;
is($a, Int, "post without type object");
}

{
my $answer = 1;
my @x = 41, (42 without $answer), 43;
my @z = 41, 43;
#?niecza todo "empty list as element not flattened - https://github.com/sorear/niecza/issues/180"
is @x, @z, "without expr on true cond";
}

{
my $answer = Failure.new;
my @x = 41, (42 without $answer), 43;
my @y = 41, (!$answer ?? 42 !! ()), 43;
my @z = 41, 42, 43;
is @y, @z, "sanity check";
is @x, @y, "without expr on false cond";
}

{
my $a = 'oops';
{ $a = 'ok' } without Nil;
is $a, 'ok', 'Statement-modifier without runs bare block';
}

{
my $a = 'oops';
{ $a = $^x } without Failure.new;
is $a.WHAT, Failure, 'Statement-modifier without runs block with placeholder';
}

# vim: ft=perl6

0 comments on commit 8a752ca

Please sign in to comment.