refactoring nested ifs
[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
b1eebd55 33 use Moo;
625d6219 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;
b1eebd55 44 use Moo;
625d6219 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;
b1eebd55 55 use Moo;
8c6626cf 56
57 has one => (
58 is => 'rw',
59 trigger => quote_sub(q{ push @{$tr}, $_[1] }, { '$tr' => \\@::tr })
60 );
61}
62
63run_for 'Baz';
64
3fc078d5 65{
66 package Default;
67
68 use Sub::Quote;
69 use Moo;
70
71 has one => (
72 is => 'rw',
73 trigger => quote_sub(q{ push @{$tr}, $_[1] }, { '$tr' => \\@::tr }),
74 default => sub { 0 }
75 );
76}
77
78run_for 'Default';
79
80{
81 package LazyDefault;
82
83 use Sub::Quote;
84 use Moo;
85
86 has one => (
87 is => 'rw',
88 trigger => quote_sub(q{ push @{$tr}, $_[1] }, { '$tr' => \\@::tr }),
89 default => sub { 0 },
90 lazy => 1
91 );
92}
93
94run_for 'LazyDefault';
95
e8dc5201 96{
97 package Shaz;
98
99 use Moo;
100
101 has one => (is => 'rw', trigger => 1 );
102
2bb6aaa3 103 sub _trigger_one { push @::tr, $_[1] }
e8dc5201 104}
105
106run_for 'Shaz';
107
a16d301e 108done_testing;