A bunch of tests for includes_type
[gitmo/Moose.git] / t / 030_roles / 017_extending_role_attrs.t
CommitLineData
8d62bf6d 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
b7835731 6use Test::More tests => 28;
8d62bf6d 7use Test::Exception;
8
9BEGIN {
10 use_ok('Moose');
11}
12
13=pod
14
15This basically just makes sure that using +name
dd14f8e8 16on 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
40my $foo = Foo->new;
41isa_ok($foo, 'Foo');
42
43is($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 67my $bar = Bar->new(foo => 42);
68isa_ok($bar, 'Bar');
69is($bar->foo, 42, '... got the extended attribute');
70$bar->foo(100);
71is($bar->foo, 100, "... can change the attribute's value to an Int");
72
73throws_ok { $bar->foo("baz") } qr/^Attribute \(foo\) does not pass the type constraint because: Validation failed for 'Int' failed with value baz at /;
74is($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 98my $baz = Baz->new(baz => 99);
99isa_ok($baz, 'Baz');
100is($baz->baz, 99, '... got the extended attribute');
b7835731 101$baz->baz('Foo');
102is($baz->baz, 'Foo', "... can change the attribute's value to a ClassName");
103
104throws_ok { $baz->baz("zonk") } qr/^Attribute \(baz\) does not pass the type constraint because: Validation failed for 'Int \| ClassName' failed with value zonk at /;
105is_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 134my $quux = Quux->new(quux => 99);
135isa_ok($quux, 'Quux');
136is($quux->quux, 99, '... got the extended attribute');
137$quux->quux(100);
138is($quux->quux, 100, "... can change the attribute's value to an Int");
139$quux->quux(["hi"]);
140is_deeply($quux->quux, ["hi"], "... can change the attribute's value to an ArrayRef");
141
142throws_ok { $quux->quux("quux") } qr/^Attribute \(quux\) does not pass the type constraint because: Validation failed for 'Positive \| ArrayRef' failed with value quux at /;
143is_deeply($quux->quux, ["hi"], "... still has the old ArrayRef value");
144
145throws_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 /;
146is_deeply($quux->quux, ["hi"], "... still has the old ArrayRef value");
147
b7835731 148
657f212a 149{
150 package Err::Role;
151 use Moose::Role;
152
153 has "err" => (
154 isa => 'Str | Int',
155 );
156
157 package Err;
158 use Moose;
159
160 with 'Err::Role';
161
162 my $error = qr/New type constraint setting must be a subtype of inherited one, or included in the inherited constraint/;
163
164 ::throws_ok {
165 has '+err' => (isa => 'Defined');
166 } $error, "must get more specific, not less specific";
167
168 ::throws_ok {
169 has '+err' => (isa => 'Bool');
170 } $error, "the type has to be a part of the union";
171
172 ::throws_ok {
173 has '+err' => (isa => 'Str | ArrayRef');
174 } $error, "can't add new types to the union";
175}
176