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