update repo to point to github
[gitmo/Moo.git] / t / lazy_isa.t
1 use strictures 1;
2 use Test::More;
3 use Test::Fatal;
4
5 my $isa_called = 0;
6 {
7   package FooISA;
8
9   use Moo;
10
11   my $isa = sub {
12     $isa_called++;
13     die "I want to die" unless $_[0] eq 'live';
14   };
15
16   has a_lazy_attr => (
17     is => 'ro',
18     isa => $isa,
19     lazy => 1,
20     builder => '_build_attr',
21   );
22
23   has non_lazy => (
24     is => 'ro',
25     isa => $isa,
26     builder => '_build_attr',
27   );
28
29   sub _build_attr { 'die' }
30 }
31
32 ok my $lives = FooISA->new(a_lazy_attr=>'live', non_lazy=>'live'),
33   'expect to live when both attrs are set to live in init';
34
35 my $called_pre = $isa_called;
36 $lives->a_lazy_attr;
37 is $called_pre, $isa_called, 'isa is not called on access when value already exists';
38
39 like(
40   exception { FooISA->new(a_lazy_attr=>'live', non_lazy=>'die') },
41   qr/I want to die/,
42   'expect to die when non lazy is set to die in init',
43 );
44
45 like(
46   exception { FooISA->new(a_lazy_attr=>'die', non_lazy=>'die') },
47   qr/I want to die/,
48   'expect to die when non lazy and lazy is set to die in init',
49 );
50
51 like(
52   exception { FooISA->new(a_lazy_attr=>'die', non_lazy=>'live') },
53   qr/I want to die/,
54   'expect to die when lazy is set to die in init',
55 );
56
57 like(
58   exception { FooISA->new() },
59   qr/I want to die/,
60   'expect to die when both lazy and non lazy are allowed to default',
61 );
62
63 like(
64   exception { FooISA->new(a_lazy_attr=>'live') },
65   qr/I want to die/,
66   'expect to die when lazy is set to live but non lazy is allowed to default',
67 );
68
69 is(
70   exception { FooISA->new(non_lazy=>'live') },
71   undef,
72   'ok when non lazy is set to something valid but lazy is allowed to default',
73 );
74
75 done_testing;