Commit | Line | Data |
7a9d14bd |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
8abe9636 |
6 | use Test::More tests => 24; |
7a9d14bd |
7 | use Test::Exception; |
8 | |
9 | BEGIN { |
10 | use_ok('Moose'); |
11 | } |
12 | |
13 | { |
14 | package Foo; |
15 | use Moose; |
16 | |
17 | has 'foo' => ( |
18 | reader => 'get_foo', |
19 | writer => 'set_foo', |
20 | initializer => sub { |
29ed9dd9 |
21 | my ($self, $value, $callback, $attr) = @_; |
9df136d0 |
22 | |
23 | ::isa_ok($attr, 'Moose::Meta::Attribute'); |
24 | ::is($attr->name, 'foo', '... got the right name'); |
25 | |
7a9d14bd |
26 | $callback->($value * 2); |
27 | }, |
28 | ); |
29 | |
30 | has 'lazy_foo' => ( |
9df136d0 |
31 | reader => 'get_lazy_foo', |
32 | lazy => 1, |
33 | default => 10, |
7a9d14bd |
34 | initializer => sub { |
29ed9dd9 |
35 | my ($self, $value, $callback, $attr) = @_; |
9df136d0 |
36 | |
37 | ::isa_ok($attr, 'Moose::Meta::Attribute'); |
38 | ::is($attr->name, 'lazy_foo', '... got the right name'); |
39 | |
7a9d14bd |
40 | $callback->($value * 2); |
41 | }, |
42 | ); |
9df136d0 |
43 | |
44 | has 'lazy_foo_w_type' => ( |
45 | reader => 'get_lazy_foo_w_type', |
46 | isa => 'Int', |
47 | lazy => 1, |
48 | default => 20, |
49 | initializer => sub { |
50 | my ($self, $value, $callback, $attr) = @_; |
51 | |
52 | ::isa_ok($attr, 'Moose::Meta::Attribute'); |
53 | ::is($attr->name, 'lazy_foo_w_type', '... got the right name'); |
54 | |
55 | $callback->($value * 2); |
56 | }, |
57 | ); |
58 | |
59 | has 'lazy_foo_builder' => ( |
60 | reader => 'get_lazy_foo_builder', |
61 | builder => 'get_foo_builder', |
62 | initializer => sub { |
63 | my ($self, $value, $callback, $attr) = @_; |
64 | |
65 | ::isa_ok($attr, 'Moose::Meta::Attribute'); |
66 | ::is($attr->name, 'lazy_foo_builder', '... got the right name'); |
67 | |
68 | $callback->($value * 2); |
69 | }, |
70 | ); |
71 | |
72 | has 'lazy_foo_builder_w_type' => ( |
73 | reader => 'get_lazy_foo_builder_w_type', |
8abe9636 |
74 | isa => 'Int', |
9df136d0 |
75 | builder => 'get_foo_builder_w_type', |
76 | initializer => sub { |
77 | my ($self, $value, $callback, $attr) = @_; |
78 | |
79 | ::isa_ok($attr, 'Moose::Meta::Attribute'); |
80 | ::is($attr->name, 'lazy_foo_builder_w_type', '... got the right name'); |
81 | |
82 | $callback->($value * 2); |
83 | }, |
84 | ); |
85 | |
86 | sub get_foo_builder { 100 } |
87 | sub get_foo_builder_w_type { 1000 } |
7a9d14bd |
88 | } |
89 | |
90 | { |
91 | my $foo = Foo->new(foo => 10); |
92 | isa_ok($foo, 'Foo'); |
93 | |
9df136d0 |
94 | is($foo->get_foo, 20, 'initial value set to 2x given value'); |
95 | is($foo->get_lazy_foo, 20, 'initial lazy value set to 2x given value'); |
96 | is($foo->get_lazy_foo_w_type, 40, 'initial lazy value with type set to 2x given value'); |
97 | is($foo->get_lazy_foo_builder, 200, 'initial lazy value with builder set to 2x given value'); |
98 | is($foo->get_lazy_foo_builder_w_type, 2000, 'initial lazy value with builder and type set to 2x given value'); |
99 | } |
100 | |
101 | { |
102 | package Bar; |
103 | use Moose; |
104 | |
105 | has 'foo' => ( |
106 | reader => 'get_foo', |
107 | writer => 'set_foo', |
108 | initializer => sub { |
109 | my ($self, $value, $callback, $attr) = @_; |
110 | |
111 | ::isa_ok($attr, 'Moose::Meta::Attribute'); |
112 | ::is($attr->name, 'foo', '... got the right name'); |
113 | |
114 | $callback->($value * 2); |
115 | }, |
116 | ); |
117 | |
5a3217de |
118 | __PACKAGE__->meta->make_immutable; |
7a9d14bd |
119 | } |
120 | |
9df136d0 |
121 | { |
122 | my $bar = Bar->new(foo => 10); |
123 | isa_ok($bar, 'Bar'); |
124 | |
125 | is($bar->get_foo, 20, 'initial value set to 2x given value'); |
126 | } |
127 | |
8abe9636 |
128 | { |
129 | package Fail::Bar; |
130 | use Moose; |
131 | |
132 | has 'foo' => ( |
133 | reader => 'get_foo', |
134 | writer => 'set_foo', |
135 | isa => 'Int', |
136 | initializer => sub { |
137 | my ($self, $value, $callback, $attr) = @_; |
138 | |
139 | ::isa_ok($attr, 'Moose::Meta::Attribute'); |
140 | ::is($attr->name, 'foo', '... got the right name'); |
141 | |
142 | $callback->("Hello $value World"); |
143 | }, |
144 | ); |
145 | |
5a3217de |
146 | __PACKAGE__->meta->make_immutable; |
8abe9636 |
147 | } |
148 | |
149 | dies_ok { |
150 | Fail::Bar->new(foo => 10) |
151 | } '... this fails, because initializer returns a bad type'; |
152 | |