6 use Test::More tests => 28;
15 This basically just makes sure that using +name
16 on role attributes works right.
27 default => sub { 10 },
36 has '+bar' => (default => sub { 100 });
37 } '... extended the attribute successfully';
43 is($foo->bar, 100, '... got the extended attribute');
64 } "... narrowed the role's type constraint successfully";
67 my $bar = Bar->new(foo => 42);
69 is($bar->foo, 42, '... got the extended attribute');
71 is($bar->foo, 100, "... can change the attribute's value to an Int");
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");
93 isa => 'Int | ClassName',
95 } "... narrowed the role's type constraint successfully";
98 my $baz = Baz->new(baz => 99);
100 is($baz->baz, 99, '... got the extended attribute');
102 is($baz->baz, 'Foo', "... can change the attribute's value to a ClassName");
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");
114 isa => 'Str | Int | Ref',
119 use Moose::Util::TypeConstraints;
129 isa => 'Positive | ArrayRef',
131 } "... narrowed the role's type constraint successfully";
134 my $quux = Quux->new(quux => 99);
135 isa_ok($quux, 'Quux');
136 is($quux->quux, 99, '... got the extended attribute');
138 is($quux->quux, 100, "... can change the attribute's value to an Int");
140 is_deeply($quux->quux, ["hi"], "... can change the attribute's value to an ArrayRef");
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");
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");
165 has '+err1' => (isa => 'Defined');
166 } "can get less specific in the subclass";
169 has '+err2' => (isa => 'Bool');
170 } "or change the type completely";
173 has '+err3' => (isa => 'Str | ArrayRef');
174 } "or add new types to the union";