6 use Test::More tests => 57;
16 use Moose::Util::TypeConstraints;
24 => via { 'FooArrayRef' };
26 has 'bar' => (is => 'ro', isa => 'Str', default => 'Foo::bar');
27 has 'baz' => (is => 'rw', isa => 'Ref');
28 has 'foo' => (is => 'rw', isa => 'FooStr');
30 has 'gorch' => (is => 'ro');
32 # this one will work here ....
33 has 'fail' => (isa => 'CodeRef');
41 has '+bar' => (default => 'Bar::bar');
42 has '+baz' => (isa => 'ArrayRef');
44 has '+foo' => (coerce => 1);
45 has '+gorch' => (required => 1);
47 # this one will *not* work here ....
49 has '+fail' => (isa => 'Ref');
50 } '... cannot create an attribute with an improper subtype relation';
52 has '+other_fail' => (trigger => sub {});
53 } '... cannot create an attribute with an illegal option';
55 has '+other_fail' => (weak_ref => 1);
56 } '... cannot create an attribute with an illegal option';
58 has '+other_fail' => (lazy => 1);
59 } '... cannot create an attribute with an illegal option';
66 is($foo->foo, undef, '... got the right undef default value');
67 lives_ok { $foo->foo('FooString') } '... assigned foo correctly';
68 is($foo->foo, 'FooString', '... got the right value for foo');
70 dies_ok { $foo->foo([]) } '... foo is not coercing (as expected)';
72 is($foo->bar, 'Foo::bar', '... got the right default value');
73 dies_ok { $foo->bar(10) } '... Foo::bar is a read/only attr';
75 is($foo->baz, undef, '... got the right undef default value');
79 lives_ok { $foo->baz($hash_ref) } '... Foo::baz accepts hash refs';
80 is($foo->baz, $hash_ref, '... got the right value assigned to baz');
83 lives_ok { $foo->baz($array_ref) } '... Foo::baz accepts an array ref';
84 is($foo->baz, $array_ref, '... got the right value assigned to baz');
86 my $scalar_ref = \(my $var);
87 lives_ok { $foo->baz($scalar_ref) } '... Foo::baz accepts scalar ref';
88 is($foo->baz, $scalar_ref, '... got the right value assigned to baz');
90 my $code_ref = sub { 1 };
91 lives_ok { $foo->baz($code_ref) } '... Foo::baz accepts a code ref';
92 is($foo->baz, $code_ref, '... got the right value assigned to baz');
97 } '... cannot create Bar without required gorch param';
99 my $bar = Bar->new(gorch => 'Bar::gorch');
103 is($bar->foo, undef, '... got the right undef default value');
104 lives_ok { $bar->foo('FooString') } '... assigned foo correctly';
105 is($bar->foo, 'FooString', '... got the right value for foo');
106 lives_ok { $bar->foo([]) } '... assigned foo correctly';
107 is($bar->foo, 'FooArrayRef', '... got the right value for foo');
109 is($bar->gorch, 'Bar::gorch', '... got the right default value');
111 is($bar->bar, 'Bar::bar', '... got the right default value');
112 dies_ok { $bar->bar(10) } '... Bar::bar is a read/only attr';
114 is($bar->baz, undef, '... got the right undef default value');
118 dies_ok { $bar->baz($hash_ref) } '... Bar::baz does not accept hash refs';
121 lives_ok { $bar->baz($array_ref) } '... Bar::baz can accept an array ref';
122 is($bar->baz, $array_ref, '... got the right value assigned to baz');
124 my $scalar_ref = \(my $var);
125 dies_ok { $bar->baz($scalar_ref) } '... Bar::baz does not accept a scalar ref';
127 my $code_ref = sub { 1 };
128 dies_ok { $bar->baz($code_ref) } '... Bar::baz does not accept a code ref';
131 # check some meta-stuff
133 ok(Bar->meta->has_attribute('foo'), '... Bar has a foo attr');
134 ok(Bar->meta->has_attribute('bar'), '... Bar has a bar attr');
135 ok(Bar->meta->has_attribute('baz'), '... Bar has a baz attr');
136 ok(Bar->meta->has_attribute('gorch'), '... Bar has a gorch attr');
137 ok(!Bar->meta->has_attribute('fail'), '... Bar does not have a fail attr');
138 ok(!Bar->meta->has_attribute('other_fail'), '... Bar does not have a fail attr');
140 isnt(Foo->meta->get_attribute('foo'),
141 Bar->meta->get_attribute('foo'),
142 '... Foo and Bar have different copies of foo');
143 isnt(Foo->meta->get_attribute('bar'),
144 Bar->meta->get_attribute('bar'),
145 '... Foo and Bar have different copies of bar');
146 isnt(Foo->meta->get_attribute('baz'),
147 Bar->meta->get_attribute('baz'),
148 '... Foo and Bar have different copies of baz');
149 isnt(Foo->meta->get_attribute('gorch'),
150 Bar->meta->get_attribute('gorch'),
151 '... Foo and Bar have different copies of gorch');
153 ok(Bar->meta->get_attribute('bar')->has_type_constraint,
154 '... Bar::bar inherited the type constraint too');
155 ok(Bar->meta->get_attribute('baz')->has_type_constraint,
156 '... Bar::baz inherited the type constraint too');
158 is(Bar->meta->get_attribute('bar')->type_constraint->name,
159 'Str', '... Bar::bar inherited the right type constraint too');
161 is(Foo->meta->get_attribute('baz')->type_constraint->name,
162 'Ref', '... Foo::baz inherited the right type constraint too');
163 is(Bar->meta->get_attribute('baz')->type_constraint->name,
164 'ArrayRef', '... Bar::baz inherited the right type constraint too');
166 ok(!Foo->meta->get_attribute('gorch')->is_required,
167 '... Foo::gorch is not a required attr');
168 ok(Bar->meta->get_attribute('gorch')->is_required,
169 '... Bar::gorch is a required attr');
171 ok(!Foo->meta->get_attribute('foo')->should_coerce,
172 '... Foo::foo should not coerce');
173 ok(Bar->meta->get_attribute('foo')->should_coerce,
174 '... Bar::foo should coerce');