update Distar url
[p5sagit/curry.git] / t / curry-weak-packagevar.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use Scalar::Util qw(weaken);
6 use curry::weak;
7
8 sub dispose_ok($;$) {
9         weaken(my $copy = $_[0]);
10         fail("variable is not a ref") unless ref $_[0];
11         undef $_[0];
12         ok(!defined($copy), $_[1]);
13 }
14
15 {
16         package Foo;
17         sub new { bless {}, shift }
18 }
19
20 { # basic behaviour - can we call without args?
21         my $foo = Foo->new;
22
23         my $called;
24         my $code = $foo->$curry::weak(sub {
25                 ok(shift->isa('Foo'), '$curry::weak object is correct class');
26                 ok(!@_, '$curry::weak did not pick up any stray parameters on the way in');
27                 ++$called;
28         });
29         fail('$curry::weak::curry did not give us a coderef') unless ref($code) eq 'CODE';
30         $code->();
31         ok($called, 'curried code was called');
32         dispose_ok($foo, '$foo departs without a fight');
33         $called = 0;
34         $code->();
35         ok(!$called, '... and we can still use the coderef as a no-op');
36 }
37
38 { # parameter passthrough
39         my $foo = Foo->new;
40
41         my $called;
42         my $code = $foo->$curry::weak(sub {
43                 ok(shift->isa('Foo'), '$curry::weak object is correct class');
44                 is_deeply(\@_, [qw(stashed parameters one two three)], 'args passed as expected');
45                 ++$called;
46         }, qw(stashed parameters));
47         fail('$curry::weak::curry did not give us a coderef') unless ref($code) eq 'CODE';
48         $code->(qw(one two three));
49         ok($called, 'curried code was called');
50         dispose_ok($foo, '$foo departs without a fight');
51         $called = 0;
52         $code->();
53         ok(!$called, '... and we can still use the coderef as a no-op');
54 }
55
56 done_testing;