6 use Test::More tests => 57;
18 use Moose::Util::TypeConstraints;
26 => via { 'FooArrayRef' };
28 has 'bar' => (is => 'ro', isa => 'Str', default => 'Foo::bar');
29 has 'baz' => (is => 'rw', isa => 'Ref');
30 has 'foo' => (is => 'rw', isa => 'FooStr');
32 has 'gorch' => (is => 'ro');
34 # this one will work here ....
35 has 'fail' => (isa => 'CodeRef');
45 has '+bar' => (default => 'Bar::bar');
46 has '+baz' => (isa => 'ArrayRef');
48 has '+foo' => (coerce => 1);
49 has '+gorch' => (required => 1);
51 # this one will *not* work here ....
53 has '+fail' => (isa => 'Ref');
54 } '... cannot create an attribute with an improper subtype relation';
56 has '+other_fail' => (trigger => sub {});
57 } '... cannot create an attribute with an illegal option';
59 has '+other_fail' => (weak_ref => 1);
60 } '... cannot create an attribute with an illegal option';
62 has '+other_fail' => (lazy => 1);
63 } '... cannot create an attribute with an illegal option';
70 is($foo->foo, undef, '... got the right undef default value');
71 lives_ok { $foo->foo('FooString') } '... assigned foo correctly';
72 is($foo->foo, 'FooString', '... got the right value for foo');
74 dies_ok { $foo->foo([]) } '... foo is not coercing (as expected)';
76 is($foo->bar, 'Foo::bar', '... got the right default value');
77 dies_ok { $foo->bar(10) } '... Foo::bar is a read/only attr';
79 is($foo->baz, undef, '... got the right undef default value');
83 lives_ok { $foo->baz($hash_ref) } '... Foo::baz accepts hash refs';
84 is($foo->baz, $hash_ref, '... got the right value assigned to baz');
87 lives_ok { $foo->baz($array_ref) } '... Foo::baz accepts an array ref';
88 is($foo->baz, $array_ref, '... got the right value assigned to baz');
90 my $scalar_ref = \(my $var);
91 lives_ok { $foo->baz($scalar_ref) } '... Foo::baz accepts scalar ref';
92 is($foo->baz, $scalar_ref, '... got the right value assigned to baz');
94 my $code_ref = sub { 1 };
95 lives_ok { $foo->baz($code_ref) } '... Foo::baz accepts a code ref';
96 is($foo->baz, $code_ref, '... got the right value assigned to baz');
101 } '... cannot create Bar without required gorch param';
103 my $bar = Bar->new(gorch => 'Bar::gorch');
107 is($bar->foo, undef, '... got the right undef default value');
108 lives_ok { $bar->foo('FooString') } '... assigned foo correctly';
109 is($bar->foo, 'FooString', '... got the right value for foo');
110 lives_ok { $bar->foo([]) } '... assigned foo correctly';
111 is($bar->foo, 'FooArrayRef', '... got the right value for foo');
113 is($bar->gorch, 'Bar::gorch', '... got the right default value');
115 is($bar->bar, 'Bar::bar', '... got the right default value');
116 dies_ok { $bar->bar(10) } '... Bar::bar is a read/only attr';
118 is($bar->baz, undef, '... got the right undef default value');
122 dies_ok { $bar->baz($hash_ref) } '... Bar::baz does not accept hash refs';
125 lives_ok { $bar->baz($array_ref) } '... Bar::baz can accept an array ref';
126 is($bar->baz, $array_ref, '... got the right value assigned to baz');
128 my $scalar_ref = \(my $var);
129 dies_ok { $bar->baz($scalar_ref) } '... Bar::baz does not accept a scalar ref';
131 my $code_ref = sub { 1 };
132 dies_ok { $bar->baz($code_ref) } '... Bar::baz does not accept a code ref';
135 # check some meta-stuff
137 ok(Bar->meta->has_attribute('foo'), '... Bar has a foo attr');
138 ok(Bar->meta->has_attribute('bar'), '... Bar has a bar attr');
139 ok(Bar->meta->has_attribute('baz'), '... Bar has a baz attr');
140 ok(Bar->meta->has_attribute('gorch'), '... Bar has a gorch attr');
141 ok(!Bar->meta->has_attribute('fail'), '... Bar does not have a fail attr');
142 ok(!Bar->meta->has_attribute('other_fail'), '... Bar does not have a fail attr');
144 isnt(Foo->meta->get_attribute('foo'),
145 Bar->meta->get_attribute('foo'),
146 '... Foo and Bar have different copies of foo');
147 isnt(Foo->meta->get_attribute('bar'),
148 Bar->meta->get_attribute('bar'),
149 '... Foo and Bar have different copies of bar');
150 isnt(Foo->meta->get_attribute('baz'),
151 Bar->meta->get_attribute('baz'),
152 '... Foo and Bar have different copies of baz');
153 isnt(Foo->meta->get_attribute('gorch'),
154 Bar->meta->get_attribute('gorch'),
155 '... Foo and Bar have different copies of gorch');
157 ok(Bar->meta->get_attribute('bar')->has_type_constraint,
158 '... Bar::bar inherited the type constraint too');
159 ok(Bar->meta->get_attribute('baz')->has_type_constraint,
160 '... Bar::baz inherited the type constraint too');
162 is(Bar->meta->get_attribute('bar')->type_constraint->name,
163 'Str', '... Bar::bar inherited the right type constraint too');
165 is(Foo->meta->get_attribute('baz')->type_constraint->name,
166 'Ref', '... Foo::baz inherited the right type constraint too');
167 is(Bar->meta->get_attribute('baz')->type_constraint->name,
168 'ArrayRef', '... Bar::baz inherited the right type constraint too');
170 ok(!Foo->meta->get_attribute('gorch')->is_required,
171 '... Foo::gorch is not a required attr');
172 ok(Bar->meta->get_attribute('gorch')->is_required,
173 '... Bar::gorch is a required attr');
175 ok(!Foo->meta->get_attribute('foo')->should_coerce,
176 '... Foo::foo should not coerce');
177 ok(Bar->meta->get_attribute('foo')->should_coerce,
178 '... Bar::foo should coerce');