8 use Class::MOP::Attribute;
10 # most values are static
14 Class::MOP::Attribute->new('$test' => (
15 default => qr/hello (.*)/
17 }, '... no refs for defaults';
20 Class::MOP::Attribute->new('$test' => (
23 }, '... no refs for defaults';
26 Class::MOP::Attribute->new('$test' => (
29 }, '... no refs for defaults';
33 Class::MOP::Attribute->new('$test' => (
36 }, '... no refs for defaults';
39 Class::MOP::Attribute->new('$test' => (
40 default => bless {} => 'Foo'
42 }, '... no refs for defaults';
48 Class::MOP::Attribute->new('$test' => (
49 builder => qr/hello (.*)/
51 }, '... no refs for builders';
54 Class::MOP::Attribute->new('$test' => (
57 }, '... no refs for builders';
60 Class::MOP::Attribute->new('$test' => (
63 }, '... no refs for builders';
67 Class::MOP::Attribute->new('$test' => (
70 }, '... no refs for builders';
73 Class::MOP::Attribute->new('$test' => (
74 builder => bless {} => 'Foo'
76 }, '... no refs for builders';
79 Class::MOP::Attribute->new('$test' => (
80 builder => 'Foo', default => 'Foo'
82 }, '... no default AND builder';
86 $undef_attr = Class::MOP::Attribute->new('$test' => (
88 predicate => 'has_test',
90 }, '... undef as a default is okay';
91 ok($undef_attr->has_default, '... and it counts as an actual default');
92 ok(!Class::MOP::Attribute->new('$test')->has_default,
93 '... but attributes with no default have no default');
95 Class::MOP::Class->create(
97 attributes => [$undef_attr],
100 my $obj = Foo->meta->new_object;
101 ok($obj->has_test, '... and the default is populated');
102 is($obj->meta->get_attribute('$test')->get_value($obj), undef, '... with the right value');
104 ok ! exception { Foo->meta->make_immutable },
105 '... and it can be inlined';
108 ok($obj->has_test, '... and the default is populated');
109 is($obj->meta->get_attribute('$test')->get_value($obj), undef, '... with the right value');
115 { # bad construtor args
117 Class::MOP::Attribute->new();
118 }, '... no name argument';
120 # These are no longer errors
122 Class::MOP::Attribute->new('');
123 }, '... bad name argument';
126 Class::MOP::Attribute->new(0);
127 }, '... bad name argument';
131 my $attr = Class::MOP::Attribute->new('$test');
133 $attr->attach_to_class();
134 }, '... attach_to_class died as expected';
137 $attr->attach_to_class('Fail');
138 }, '... attach_to_class died as expected';
141 $attr->attach_to_class(bless {} => 'Fail');
142 }, '... attach_to_class died as expected';
146 my $attr = Class::MOP::Attribute->new('$test' => (
147 reader => [ 'whoops, this wont work' ]
150 $attr->attach_to_class(Class::MOP::Class->initialize('Foo'));
153 $attr->install_accessors;
154 }, '... bad reader format';
158 my $attr = Class::MOP::Attribute->new('$test');
161 $attr->_process_accessors('fail', 'my_failing_sub');
162 }, '... cannot find "fail" type generator';
168 package My::Attribute;
169 our @ISA = ('Class::MOP::Attribute');
170 sub generate_reader_method { eval { die } }
173 my $attr = My::Attribute->new('$test' => (
178 $attr->install_accessors;
179 }, '... failed to generate accessors correctly';
183 my $attr = Class::MOP::Attribute->new('$test' => (
184 predicate => 'has_test'
187 my $Bar = Class::MOP::Class->create('Bar');
188 isa_ok($Bar, 'Class::MOP::Class');
190 $Bar->add_attribute($attr);
192 can_ok('Bar', 'has_test');
194 is($attr, $Bar->remove_attribute('$test'), '... removed the $test attribute');
196 ok(!Bar->can('has_test'), '... Bar no longer has the "has_test" method');
202 # the next three tests once tested that
203 # the code would fail, but we lifted the
204 # restriction so you can have an accessor
205 # along with a reader/writer pair (I mean
206 # why not really). So now they test that
207 # it works, which is kinda silly, but it
208 # tests the API change, so I keep it.
211 Class::MOP::Attribute->new('$foo', (
215 }, '... can create accessors with reader/writers';
218 Class::MOP::Attribute->new('$foo', (
222 }, '... can create accessors with reader/writers';
225 Class::MOP::Attribute->new('$foo', (
230 }, '... can create accessors with reader/writers';