add BUILDALL support
[gitmo/Moo.git] / t / accessor-trigger.t
CommitLineData
a16d301e 1use strictures 1;
2use Test::More;
3
625d6219 4our @tr;
a16d301e 5
625d6219 6sub run_for {
7 my $class = shift;
a16d301e 8
625d6219 9 @tr = ();
a16d301e 10
625d6219 11 my $obj = $class->new;
12
13 ok(!@tr, "${class}: trigger not fired with no value");
a16d301e 14
625d6219 15 $obj = $class->new(one => 1);
a16d301e 16
625d6219 17 is_deeply(\@tr, [ 1 ], "${class}: trigger fired on new");
a16d301e 18
625d6219 19 my $res = $obj->one(2);
a16d301e 20
625d6219 21 is_deeply(\@tr, [ 1, 2 ], "${class}: trigger fired on set");
a16d301e 22
625d6219 23 is($res, 2, "${class}: return from set ok");
a16d301e 24
625d6219 25 is($obj->one, 2, "${class}: return from accessor ok");
a16d301e 26
625d6219 27 is_deeply(\@tr, [ 1, 2 ], "${class}: trigger not fired for accessor as get");
28}
a16d301e 29
625d6219 30{
31 package Foo;
32
33 use Class::Tiny;
34
35 has one => (is => 'rw', trigger => sub { push @::tr, $_[1] });
36}
37
38run_for 'Foo';
39
40{
41 package Bar;
42
43 use Sub::Quote;
44 use Class::Tiny;
45
46 has one => (is => 'rw', trigger => quote_sub q{ push @::tr, $_[1] });
47}
a16d301e 48
625d6219 49run_for 'Bar';
a16d301e 50
8c6626cf 51{
52 package Baz;
53
54 use Sub::Quote;
55 use Class::Tiny;
56
57 has one => (
58 is => 'rw',
59 trigger => quote_sub(q{ push @{$tr}, $_[1] }, { '$tr' => \\@::tr })
60 );
61}
62
63run_for 'Baz';
64
a16d301e 65done_testing;