Skip to content

Commit

Permalink
[t] move lazy.t to spec/
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.pugscode.org/pugs@22121 c213334d-75ef-0310-aa23-eaa082d1ae64
  • Loading branch information
moritz committed Sep 3, 2008
1 parent 21f2f52 commit 2b21ee3
Showing 1 changed file with 76 additions and 0 deletions.
76 changes: 76 additions & 0 deletions S04-statements/lazy.t
@@ -0,0 +1,76 @@
use v6;

use Test;

=begin pod
=head1 DESCRIPTION
This test tests nothingmuch's C<lazy> proposal.
This proposal was accepted on 2005-08-23 in a p6l post by Larry
L<"http://www.nntp.perl.org/group/perl.perl6.language/22890">:
> Which already seems to be there with
>
> lazy {...}
>
> which is, I presume, mostly syntactic sugar for something like:
>
> sub is cached {...}
=end pod

plan 15;

{
my $was_in_lazy;

my $var = lazy { $was_in_lazy++; 42 };

ok !$was_in_lazy, 'our lazy block wasn\'t yet executed (1)';

is $var, 42, 'our lazy var has the correct value';
ok $was_in_lazy, 'our lazy block was executed';

is $var, 42, 'our lazy var still has the correct value';
is $was_in_lazy, 1, 'our lazy block was not executed again';
}

# Same, but passing the lazy value around before accessing it:
{
my $was_in_lazy;

my $var = lazy { $was_in_lazy++; 42 };
my $sub = -> Num $v, Bool $access { $access and +$v };

ok !$was_in_lazy, 'our lazy block wasn\'t yet executed (2)';
$sub($var, 0);
ok !$was_in_lazy, 'our lazy block has still not been executed';
$sub($var, 1);
ok $was_in_lazy, 'our lazy block has been executed now';
}

# dies_ok/lives_ok tests:
{
my $was_in_lazy;
my $lazy = lazy { $was_in_lazy++; 42 };
lives_ok { $lazy = 23 }, "reassigning our var containing a lazy worked (1)";
is $lazy, 23, "reassigning our var containing a lazy worked (2)";
ok !$was_in_lazy, "reassigning our var containing a lazy worked (3)";
}

{
my $was_in_lazy;
my $lazy = lazy { $was_in_lazy++; 42 };
lives_ok { $lazy := 23 }, "rebinding our var containing a lazy worked (1)";
is $lazy, 23, "rebinding our var containing a lazy worked (2)";
ok !$was_in_lazy, "rebinding our var containing a lazy worked (3)";
}

{
dies_ok { (lazy { 42 }) = 23 },
"directly assigning to a lazy var does not work";
}

# Arguably, we should remove the $was_in_lazy tests, as it doesn't really
# matter when the lazy {...} block is actually executed.

0 comments on commit 2b21ee3

Please sign in to comment.