added default {} keyword
[gitmo/Moose.git] / t / 038_attribute_inherited_slot_specs.t
CommitLineData
1d768fb1 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
ce0e8d63 6use Test::More tests => 57;
1d768fb1 7use Test::Exception;
8
9BEGIN {
ce0e8d63 10 use_ok('Moose');
1d768fb1 11}
12
1d768fb1 13{
14 package Foo;
1d768fb1 15 use Moose;
ce0e8d63 16 use Moose::Util::TypeConstraints;
17
18 subtype 'FooStr'
19 => as 'Str'
20 => where { /Foo/ };
21
22 coerce 'FooStr'
23 => from ArrayRef
24 => via { 'FooArrayRef' };
1d768fb1 25
26 has 'bar' => (is => 'ro', isa => 'Str', default => 'Foo::bar');
ce0e8d63 27 has 'baz' => (is => 'rw', isa => 'Ref');
28 has 'foo' => (is => 'rw', isa => 'FooStr');
29
30 has 'gorch' => (is => 'ro');
31
32 # this one will work here ....
33 has 'fail' => (isa => 'CodeRef');
34 has 'other_fail';
1d768fb1 35
36 package Bar;
1d768fb1 37 use Moose;
38
39 extends 'Foo';
40
41 has '+bar' => (default => 'Bar::bar');
ce0e8d63 42 has '+baz' => (isa => 'ArrayRef');
43
44 has '+foo' => (coerce => 1);
45 has '+gorch' => (required => 1);
46
47 # this one will *not* work here ....
48 ::dies_ok {
49 has '+fail' => (isa => 'Ref');
50 } '... cannot create an attribute with an improper subtype relation';
51 ::dies_ok {
52 has '+other_fail' => (trigger => sub {});
53 } '... cannot create an attribute with an illegal option';
54 ::dies_ok {
55 has '+other_fail' => (weak_ref => 1);
56 } '... cannot create an attribute with an illegal option';
57 ::dies_ok {
58 has '+other_fail' => (lazy => 1);
59 } '... cannot create an attribute with an illegal option';
60
1d768fb1 61}
62
63my $foo = Foo->new;
64isa_ok($foo, 'Foo');
65
ce0e8d63 66is($foo->foo, undef, '... got the right undef default value');
67lives_ok { $foo->foo('FooString') } '... assigned foo correctly';
68is($foo->foo, 'FooString', '... got the right value for foo');
1d768fb1 69
ce0e8d63 70dies_ok { $foo->foo([]) } '... foo is not coercing (as expected)';
71
72is($foo->bar, 'Foo::bar', '... got the right default value');
1d768fb1 73dies_ok { $foo->bar(10) } '... Foo::bar is a read/only attr';
74
ce0e8d63 75is($foo->baz, undef, '... got the right undef default value');
76
77{
78 my $hash_ref = {};
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');
81
82 my $array_ref = [];
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');
85
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');
89
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');
93}
94
95dies_ok {
96 Bar->new;
97} '... cannot create Bar without required gorch param';
98
99my $bar = Bar->new(gorch => 'Bar::gorch');
1d768fb1 100isa_ok($bar, 'Bar');
101isa_ok($bar, 'Foo');
102
ce0e8d63 103is($bar->foo, undef, '... got the right undef default value');
104lives_ok { $bar->foo('FooString') } '... assigned foo correctly';
105is($bar->foo, 'FooString', '... got the right value for foo');
106lives_ok { $bar->foo([]) } '... assigned foo correctly';
107is($bar->foo, 'FooArrayRef', '... got the right value for foo');
108
109is($bar->gorch, 'Bar::gorch', '... got the right default value');
1d768fb1 110
ce0e8d63 111is($bar->bar, 'Bar::bar', '... got the right default value');
1d768fb1 112dies_ok { $bar->bar(10) } '... Bar::bar is a read/only attr';
113
ce0e8d63 114is($bar->baz, undef, '... got the right undef default value');
115
116{
117 my $hash_ref = {};
118 dies_ok { $bar->baz($hash_ref) } '... Bar::baz does not accept hash refs';
119
120 my $array_ref = [];
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');
123
124 my $scalar_ref = \(my $var);
125 dies_ok { $bar->baz($scalar_ref) } '... Bar::baz does not accept a scalar ref';
126
127 my $code_ref = sub { 1 };
128 dies_ok { $bar->baz($code_ref) } '... Bar::baz does not accept a code ref';
129}
130
1d768fb1 131# check some meta-stuff
132
ce0e8d63 133ok(Bar->meta->has_attribute('foo'), '... Bar has a foo attr');
1d768fb1 134ok(Bar->meta->has_attribute('bar'), '... Bar has a bar attr');
ce0e8d63 135ok(Bar->meta->has_attribute('baz'), '... Bar has a baz attr');
136ok(Bar->meta->has_attribute('gorch'), '... Bar has a gorch attr');
137ok(!Bar->meta->has_attribute('fail'), '... Bar does not have a fail attr');
138ok(!Bar->meta->has_attribute('other_fail'), '... Bar does not have a fail attr');
139
140isnt(Foo->meta->get_attribute('foo'),
141 Bar->meta->get_attribute('foo'),
142 '... Foo and Bar have different copies of foo');
1d768fb1 143isnt(Foo->meta->get_attribute('bar'),
144 Bar->meta->get_attribute('bar'),
145 '... Foo and Bar have different copies of bar');
ce0e8d63 146isnt(Foo->meta->get_attribute('baz'),
147 Bar->meta->get_attribute('baz'),
148 '... Foo and Bar have different copies of baz');
149isnt(Foo->meta->get_attribute('gorch'),
150 Bar->meta->get_attribute('gorch'),
151 '... Foo and Bar have different copies of gorch');
152
daea75c9 153ok(Bar->meta->get_attribute('bar')->has_type_constraint,
154 '... Bar::bar inherited the type constraint too');
ce0e8d63 155ok(Bar->meta->get_attribute('baz')->has_type_constraint,
156 '... Bar::baz inherited the type constraint too');
1d768fb1 157
daea75c9 158is(Bar->meta->get_attribute('bar')->type_constraint->name,
159 'Str', '... Bar::bar inherited the right type constraint too');
1d768fb1 160
ce0e8d63 161is(Foo->meta->get_attribute('baz')->type_constraint->name,
162 'Ref', '... Foo::baz inherited the right type constraint too');
163is(Bar->meta->get_attribute('baz')->type_constraint->name,
164 'ArrayRef', '... Bar::baz inherited the right type constraint too');
165
166ok(!Foo->meta->get_attribute('gorch')->is_required,
167 '... Foo::gorch is not a required attr');
168ok(Bar->meta->get_attribute('gorch')->is_required,
169 '... Bar::gorch is a required attr');
170
171ok(!Foo->meta->get_attribute('foo')->should_coerce,
172 '... Foo::foo should not coerce');
173ok(Bar->meta->get_attribute('foo')->should_coerce,
174 '... Bar::foo should coerce');
175
1d768fb1 176