6 use Test::More tests => 29;
11 use_ok('Class::MOP::Attribute');
14 # most values are static
18 Class::MOP::Attribute->new('$test' => (
19 default => qr/hello (.*)/
21 } '... no refs for defaults';
24 Class::MOP::Attribute->new('$test' => (
27 } '... no refs for defaults';
30 Class::MOP::Attribute->new('$test' => (
33 } '... no refs for defaults';
37 Class::MOP::Attribute->new('$test' => (
40 } '... no refs for defaults';
43 Class::MOP::Attribute->new('$test' => (
44 default => bless {} => 'Foo'
46 } '... no refs for defaults';
52 Class::MOP::Attribute->new('$test' => (
53 builder => qr/hello (.*)/
55 } '... no refs for builders';
58 Class::MOP::Attribute->new('$test' => (
61 } '... no refs for builders';
64 Class::MOP::Attribute->new('$test' => (
67 } '... no refs for builders';
71 Class::MOP::Attribute->new('$test' => (
74 } '... no refs for builders';
77 Class::MOP::Attribute->new('$test' => (
78 builder => bless {} => 'Foo'
80 } '... no refs for builders';
83 Class::MOP::Attribute->new('$test' => (
84 builder => 'Foo', default => 'Foo'
86 } '... no default AND builder';
91 { # bad construtor args
93 Class::MOP::Attribute->new();
94 } '... no name argument';
97 Class::MOP::Attribute->new('');
98 } '... bad name argument';
101 Class::MOP::Attribute->new(0);
102 } '... bad name argument';
106 my $attr = Class::MOP::Attribute->new('$test');
108 $attr->attach_to_class();
109 } '... attach_to_class died as expected';
112 $attr->attach_to_class('Fail');
113 } '... attach_to_class died as expected';
116 $attr->attach_to_class(bless {} => 'Fail');
117 } '... attach_to_class died as expected';
121 my $attr = Class::MOP::Attribute->new('$test' => (
122 reader => [ 'whoops, this wont work' ]
125 $attr->attach_to_class(Class::MOP::Class->initialize('Foo'));
128 $attr->install_accessors;
129 } '... bad reader format';
133 my $attr = Class::MOP::Attribute->new('$test');
136 $attr->process_accessors('fail', 'my_failing_sub');
137 } '... cannot find "fail" type generator';
143 package My::Attribute;
144 our @ISA = ('Class::MOP::Attribute');
145 sub generate_reader_method { eval { die } }
148 my $attr = My::Attribute->new('$test' => (
153 $attr->install_accessors;
154 } '... failed to generate accessors correctly';
158 my $attr = Class::MOP::Attribute->new('$test' => (
159 predicate => 'has_test'
162 my $Bar = Class::MOP::Class->create('Bar');
163 isa_ok($Bar, 'Class::MOP::Class');
165 $Bar->add_attribute($attr);
167 can_ok('Bar', 'has_test');
169 is($attr, $Bar->remove_attribute('$test'), '... removed the $test attribute');
171 ok(!Bar->can('has_test'), '... Bar no longer has the "has_test" method');
177 # the next three tests once tested that
178 # the code would fail, but we lifted the
179 # restriction so you can have an accessor
180 # along with a reader/writer pair (I mean
181 # why not really). So now they test that
182 # it works, which is kinda silly, but it
183 # tests the API change, so I keep it.
186 Class::MOP::Attribute->new('$foo', (
190 } '... can create accessors with reader/writers';
193 Class::MOP::Attribute->new('$foo', (
197 } '... can create accessors with reader/writers';
200 Class::MOP::Attribute->new('$foo', (
205 } '... can create accessors with reader/writers';