Commit | Line | Data |
8d62bf6d |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
b7835731 |
6 | use Test::More tests => 28; |
8d62bf6d |
7 | use Test::Exception; |
8 | |
9 | BEGIN { |
10 | use_ok('Moose'); |
11 | } |
12 | |
13 | =pod |
14 | |
15 | This basically just makes sure that using +name |
dd14f8e8 |
16 | on role attributes works right. |
8d62bf6d |
17 | |
18 | =cut |
19 | |
20 | { |
21 | package Foo::Role; |
22 | use Moose::Role; |
23 | |
24 | has 'bar' => ( |
25 | is => 'rw', |
26 | isa => 'Int', |
27 | default => sub { 10 }, |
28 | ); |
29 | |
30 | package Foo; |
31 | use Moose; |
32 | |
33 | with 'Foo::Role'; |
34 | |
35 | ::lives_ok { |
36 | has '+bar' => (default => sub { 100 }); |
37 | } '... extended the attribute successfully'; |
38 | } |
39 | |
40 | my $foo = Foo->new; |
41 | isa_ok($foo, 'Foo'); |
42 | |
43 | is($foo->bar, 100, '... got the extended attribute'); |
44 | |
b7835731 |
45 | |
dd14f8e8 |
46 | { |
47 | package Bar::Role; |
48 | use Moose::Role; |
49 | |
50 | has 'foo' => ( |
51 | is => 'rw', |
52 | isa => 'Str | Int', |
53 | ); |
54 | |
55 | package Bar; |
56 | use Moose; |
57 | |
58 | with 'Bar::Role'; |
59 | |
60 | ::lives_ok { |
61 | has '+foo' => ( |
62 | isa => 'Int', |
63 | ) |
64 | } "... narrowed the role's type constraint successfully"; |
65 | } |
66 | |
dd14f8e8 |
67 | my $bar = Bar->new(foo => 42); |
68 | isa_ok($bar, 'Bar'); |
69 | is($bar->foo, 42, '... got the extended attribute'); |
70 | $bar->foo(100); |
71 | is($bar->foo, 100, "... can change the attribute's value to an Int"); |
72 | |
73 | throws_ok { $bar->foo("baz") } qr/^Attribute \(foo\) does not pass the type constraint because: Validation failed for 'Int' failed with value baz at /; |
74 | is($bar->foo, 100, "... still has the old Int value"); |
75 | |
b7835731 |
76 | |
68e9fbfc |
77 | { |
78 | package Baz::Role; |
79 | use Moose::Role; |
80 | |
81 | has 'baz' => ( |
82 | is => 'rw', |
b7835731 |
83 | isa => 'Value', |
68e9fbfc |
84 | ); |
85 | |
86 | package Baz; |
87 | use Moose; |
88 | |
89 | with 'Baz::Role'; |
90 | |
91 | ::lives_ok { |
92 | has '+baz' => ( |
b7835731 |
93 | isa => 'Int | ClassName', |
68e9fbfc |
94 | ) |
95 | } "... narrowed the role's type constraint successfully"; |
96 | } |
97 | |
68e9fbfc |
98 | my $baz = Baz->new(baz => 99); |
99 | isa_ok($baz, 'Baz'); |
100 | is($baz->baz, 99, '... got the extended attribute'); |
b7835731 |
101 | $baz->baz('Foo'); |
102 | is($baz->baz, 'Foo', "... can change the attribute's value to a ClassName"); |
103 | |
104 | throws_ok { $baz->baz("zonk") } qr/^Attribute \(baz\) does not pass the type constraint because: Validation failed for 'Int \| ClassName' failed with value zonk at /; |
105 | is_deeply($baz->baz, 'Foo', "... still has the old ClassName value"); |
68e9fbfc |
106 | |
68e9fbfc |
107 | |
657f212a |
108 | { |
109 | package Quux::Role; |
110 | use Moose::Role; |
111 | |
112 | has 'quux' => ( |
113 | is => 'rw', |
114 | isa => 'Str | Int | Ref', |
115 | ); |
116 | |
117 | package Quux; |
118 | use Moose; |
119 | use Moose::Util::TypeConstraints; |
120 | |
121 | with 'Quux::Role'; |
122 | |
123 | subtype 'Positive' |
124 | => as 'Int' |
125 | => where { $_ > 0 }; |
126 | |
127 | ::lives_ok { |
128 | has '+quux' => ( |
129 | isa => 'Positive | ArrayRef', |
130 | ) |
131 | } "... narrowed the role's type constraint successfully"; |
132 | } |
133 | |
657f212a |
134 | my $quux = Quux->new(quux => 99); |
135 | isa_ok($quux, 'Quux'); |
136 | is($quux->quux, 99, '... got the extended attribute'); |
137 | $quux->quux(100); |
138 | is($quux->quux, 100, "... can change the attribute's value to an Int"); |
139 | $quux->quux(["hi"]); |
140 | is_deeply($quux->quux, ["hi"], "... can change the attribute's value to an ArrayRef"); |
141 | |
142 | throws_ok { $quux->quux("quux") } qr/^Attribute \(quux\) does not pass the type constraint because: Validation failed for 'Positive \| ArrayRef' failed with value quux at /; |
143 | is_deeply($quux->quux, ["hi"], "... still has the old ArrayRef value"); |
144 | |
145 | throws_ok { $quux->quux({a => 1}) } qr/^Attribute \(quux\) does not pass the type constraint because: Validation failed for 'Positive \| ArrayRef' failed with value HASH\(\w+\) at /; |
146 | is_deeply($quux->quux, ["hi"], "... still has the old ArrayRef value"); |
147 | |
b7835731 |
148 | |
657f212a |
149 | { |
150 | package Err::Role; |
151 | use Moose::Role; |
152 | |
5e98d2b6 |
153 | for (1..3) { |
154 | has "err$_" => ( |
155 | isa => 'Str | Int', |
156 | ); |
157 | } |
657f212a |
158 | |
159 | package Err; |
160 | use Moose; |
161 | |
162 | with 'Err::Role'; |
163 | |
5e98d2b6 |
164 | ::lives_ok { |
165 | has '+err1' => (isa => 'Defined'); |
166 | } "can get less specific in the subclass"; |
657f212a |
167 | |
5e98d2b6 |
168 | ::lives_ok { |
169 | has '+err2' => (isa => 'Bool'); |
170 | } "or change the type completely"; |
657f212a |
171 | |
5e98d2b6 |
172 | ::lives_ok { |
173 | has '+err3' => (isa => 'Str | ArrayRef'); |
174 | } "or add new types to the union"; |
657f212a |
175 | } |
176 | |