Commit | Line | Data |
4060c871 |
1 | #!/usr/bin/perl |
1f5ce14a |
2 | # This is automatically generated by author/import-moose-test.pl. |
3 | # DO NOT EDIT THIS FILE. ANY CHANGES WILL BE LOST!!! |
4 | use t::lib::MooseCompat; |
4060c871 |
5 | |
6 | use strict; |
7 | use warnings; |
8 | |
1f5ce14a |
9 | use Test::More; |
4060c871 |
10 | use Test::Exception; |
11 | |
12 | use Scalar::Util 'isweak'; |
13 | |
14 | |
4060c871 |
15 | { |
16 | package Foo; |
17 | use Mouse; |
18 | |
19 | eval { |
20 | has 'foo' => ( |
21 | accessor => 'foo', |
22 | ); |
23 | }; |
24 | ::ok(!$@, '... created the accessor method okay'); |
25 | |
26 | eval { |
27 | has 'lazy_foo' => ( |
28 | accessor => 'lazy_foo', |
29 | lazy => 1, |
30 | default => sub { 10 } |
31 | ); |
32 | }; |
33 | ::ok(!$@, '... created the lazy accessor method okay'); |
34 | |
35 | |
36 | eval { |
37 | has 'foo_required' => ( |
38 | accessor => 'foo_required', |
39 | required => 1, |
40 | ); |
41 | }; |
42 | ::ok(!$@, '... created the required accessor method okay'); |
43 | |
44 | eval { |
45 | has 'foo_int' => ( |
46 | accessor => 'foo_int', |
47 | isa => 'Int', |
48 | ); |
49 | }; |
50 | ::ok(!$@, '... created the accessor method with type constraint okay'); |
51 | |
52 | eval { |
53 | has 'foo_weak' => ( |
54 | accessor => 'foo_weak', |
55 | weak_ref => 1 |
56 | ); |
57 | }; |
58 | ::ok(!$@, '... created the accessor method with weak_ref okay'); |
59 | |
60 | eval { |
61 | has 'foo_deref' => ( |
62 | accessor => 'foo_deref', |
63 | isa => 'ArrayRef', |
64 | auto_deref => 1, |
65 | ); |
66 | }; |
67 | ::ok(!$@, '... created the accessor method with auto_deref okay'); |
68 | |
69 | eval { |
70 | has 'foo_deref_ro' => ( |
71 | reader => 'foo_deref_ro', |
72 | isa => 'ArrayRef', |
73 | auto_deref => 1, |
74 | ); |
75 | }; |
76 | ::ok(!$@, '... created the reader method with auto_deref okay'); |
77 | |
78 | eval { |
79 | has 'foo_deref_hash' => ( |
80 | accessor => 'foo_deref_hash', |
81 | isa => 'HashRef', |
82 | auto_deref => 1, |
83 | ); |
84 | }; |
85 | ::ok(!$@, '... created the reader method with auto_deref okay'); |
86 | } |
87 | |
88 | { |
89 | my $foo = Foo->new(foo_required => 'required'); |
90 | isa_ok($foo, 'Foo'); |
91 | |
92 | # regular accessor |
93 | |
94 | can_ok($foo, 'foo'); |
95 | is($foo->foo(), undef, '... got an unset value'); |
96 | lives_ok { |
97 | $foo->foo(100); |
98 | } '... foo wrote successfully'; |
99 | is($foo->foo(), 100, '... got the correct set value'); |
100 | |
101 | ok(!isweak($foo->{foo}), '... it is not a weak reference'); |
102 | |
103 | # required writer |
104 | |
105 | dies_ok { |
106 | Foo->new; |
107 | } '... cannot create without the required attribute'; |
108 | |
109 | can_ok($foo, 'foo_required'); |
110 | is($foo->foo_required(), 'required', '... got an unset value'); |
111 | lives_ok { |
112 | $foo->foo_required(100); |
113 | } '... foo_required wrote successfully'; |
114 | is($foo->foo_required(), 100, '... got the correct set value'); |
115 | |
116 | lives_ok { |
117 | $foo->foo_required(undef); |
118 | } '... foo_required did not die with undef'; |
119 | |
120 | is($foo->foo_required, undef, "value is undef"); |
121 | |
122 | ok(!isweak($foo->{foo_required}), '... it is not a weak reference'); |
123 | |
124 | # lazy |
125 | |
126 | ok(!exists($foo->{lazy_foo}), '... no value in lazy_foo slot'); |
127 | |
128 | can_ok($foo, 'lazy_foo'); |
129 | is($foo->lazy_foo(), 10, '... got an deferred value'); |
130 | |
131 | # with type constraint |
132 | |
133 | can_ok($foo, 'foo_int'); |
134 | is($foo->foo_int(), undef, '... got an unset value'); |
135 | lives_ok { |
136 | $foo->foo_int(100); |
137 | } '... foo_int wrote successfully'; |
138 | is($foo->foo_int(), 100, '... got the correct set value'); |
139 | |
140 | dies_ok { |
141 | $foo->foo_int("Foo"); |
142 | } '... foo_int died successfully'; |
143 | |
144 | ok(!isweak($foo->{foo_int}), '... it is not a weak reference'); |
145 | |
146 | # with weak_ref |
147 | |
148 | my $test = []; |
149 | |
150 | can_ok($foo, 'foo_weak'); |
151 | is($foo->foo_weak(), undef, '... got an unset value'); |
152 | lives_ok { |
153 | $foo->foo_weak($test); |
154 | } '... foo_weak wrote successfully'; |
155 | is($foo->foo_weak(), $test, '... got the correct set value'); |
156 | |
157 | ok(isweak($foo->{foo_weak}), '... it is a weak reference'); |
158 | |
159 | can_ok( $foo, 'foo_deref'); |
160 | is_deeply( [$foo->foo_deref()], [], '... default default value'); |
161 | my @list; |
162 | lives_ok { |
163 | @list = $foo->foo_deref(); |
164 | } "... doesn't deref undef value"; |
165 | is_deeply( \@list, [], "returns empty list in list context"); |
166 | |
167 | lives_ok { |
168 | $foo->foo_deref( [ qw/foo bar gorch/ ] ); |
169 | } '... foo_deref wrote successfully'; |
170 | |
171 | is( Scalar::Util::reftype( scalar $foo->foo_deref() ), "ARRAY", "returns an array reference in scalar context" ); |
172 | is_deeply( scalar($foo->foo_deref()), [ qw/foo bar gorch/ ], "correct array" ); |
173 | |
174 | is( scalar( () = $foo->foo_deref() ), 3, "returns list in list context" ); |
175 | is_deeply( [ $foo->foo_deref() ], [ qw/foo bar gorch/ ], "correct list" ); |
176 | |
177 | |
178 | can_ok( $foo, 'foo_deref' ); |
179 | is_deeply( [$foo->foo_deref_ro()], [], "... default default value" ); |
180 | |
181 | dies_ok { |
182 | $foo->foo_deref_ro( [] ); |
183 | } "... read only"; |
184 | |
185 | $foo->{foo_deref_ro} = [qw/la la la/]; |
186 | |
187 | is_deeply( scalar($foo->foo_deref_ro()), [qw/la la la/], "scalar context ro" ); |
188 | is_deeply( [ $foo->foo_deref_ro() ], [qw/la la la/], "list context ro" ); |
189 | |
190 | can_ok( $foo, 'foo_deref_hash' ); |
191 | is_deeply( { $foo->foo_deref_hash() }, {}, "... default default value" ); |
192 | |
193 | my %hash; |
194 | lives_ok { |
195 | %hash = $foo->foo_deref_hash(); |
196 | } "... doesn't deref undef value"; |
197 | is_deeply( \%hash, {}, "returns empty list in list context"); |
198 | |
199 | lives_ok { |
200 | $foo->foo_deref_hash( { foo => 1, bar => 2 } ); |
201 | } '... foo_deref_hash wrote successfully'; |
202 | |
203 | is_deeply( scalar($foo->foo_deref_hash), { foo => 1, bar => 2 }, "scalar context" ); |
204 | |
205 | %hash = $foo->foo_deref_hash; |
206 | is_deeply( \%hash, { foo => 1, bar => 2 }, "list context"); |
207 | } |
208 | |
1f5ce14a |
209 | done_testing; |