Don't use $_ as loop variable when calling arbitrary code (RT#81072)
[gitmo/Moo.git] / xt / moose-lazy.t
1 use strictures 1;
2 use Test::More;
3 use Test::Exception;
4
5 use Moo::HandleMoose;
6
7 {
8    package LazyFrew;
9
10    our $default_ran = 0;
11    our $quoted_default_ran = 0;
12    our $builder_ran = 0;
13
14    use Moo::Role;
15    use Sub::Quote;
16
17    has frooh => (
18       is => 'rw',
19       default => sub {
20          $default_ran = 1;
21          'test frooh'
22       },
23       lazy => 1,
24    );
25
26    has frew => (
27       is => 'rw',
28       default => quote_sub(q{
29          $$quoted_default_ran = 1;
30          'test frew'
31       }, { '$quoted_default_ran' => \\$quoted_default_ran }),
32       lazy => 1,
33    );
34
35    has frioux => (
36       is => 'rw',
37       builder => 'build_frioux',
38       lazy => 1,
39    );
40
41    sub build_frioux {
42       $builder_ran = 1;
43       'test frioux'
44    }
45
46    package Bar;
47    use Moose;
48    with 'LazyFrew';
49 }
50
51 my $x = Bar->new;
52 ok(!$LazyFrew::default_ran, 'default has not run yet');
53 ok(!$LazyFrew::quoted_default_ran, 'quoted default has not run yet');
54 ok(!$LazyFrew::builder_ran, 'builder has not run yet');
55
56 is($x->frooh, 'test frooh', 'frooh defaulted correctly');
57
58 ok($LazyFrew::default_ran, 'default ran');
59 ok(!$LazyFrew::quoted_default_ran, 'quoted default has not run yet');
60 ok(!$LazyFrew::builder_ran, 'builder has not run yet');
61
62 is($x->frew, 'test frew', 'frew defaulted correctly');
63
64 ok($LazyFrew::default_ran, 'default ran');
65 ok($LazyFrew::quoted_default_ran, 'quoted default ran');
66 ok(!$LazyFrew::builder_ran, 'builder has not run yet');
67
68 is($x->frioux, 'test frioux', 'frioux built correctly');
69
70 ok($LazyFrew::default_ran, 'default ran');
71 ok($LazyFrew::quoted_default_ran, 'quoted default ran');
72 ok($LazyFrew::builder_ran, 'builder ran');
73
74 done_testing;