6 use Test::More tests => 27;
9 BEGIN {use Class::MOP;use Class::MOP::Attribute;
12 # most values are static
16 Class::MOP::Attribute->new('$test' => (
17 default => qr/hello (.*)/
19 } '... no refs for defaults';
22 Class::MOP::Attribute->new('$test' => (
25 } '... no refs for defaults';
28 Class::MOP::Attribute->new('$test' => (
31 } '... no refs for defaults';
35 Class::MOP::Attribute->new('$test' => (
38 } '... no refs for defaults';
41 Class::MOP::Attribute->new('$test' => (
42 default => bless {} => 'Foo'
44 } '... no refs for defaults';
50 Class::MOP::Attribute->new('$test' => (
51 builder => qr/hello (.*)/
53 } '... no refs for builders';
56 Class::MOP::Attribute->new('$test' => (
59 } '... no refs for builders';
62 Class::MOP::Attribute->new('$test' => (
65 } '... no refs for builders';
69 Class::MOP::Attribute->new('$test' => (
72 } '... no refs for builders';
75 Class::MOP::Attribute->new('$test' => (
76 builder => bless {} => 'Foo'
78 } '... no refs for builders';
81 Class::MOP::Attribute->new('$test' => (
82 builder => 'Foo', default => 'Foo'
84 } '... no default AND builder';
89 { # bad construtor args
91 Class::MOP::Attribute->new();
92 } '... no name argument';
95 Class::MOP::Attribute->new('');
96 } '... bad name argument';
99 Class::MOP::Attribute->new(0);
100 } '... bad name argument';
104 my $attr = Class::MOP::Attribute->new('$test');
106 $attr->attach_to_class();
107 } '... attach_to_class died as expected';
110 $attr->attach_to_class('Fail');
111 } '... attach_to_class died as expected';
114 $attr->attach_to_class(bless {} => 'Fail');
115 } '... attach_to_class died as expected';
119 my $attr = Class::MOP::Attribute->new('$test' => (
120 reader => [ 'whoops, this wont work' ]
123 $attr->attach_to_class(Class::MOP::Class->initialize('Foo'));
126 $attr->install_accessors;
127 } '... bad reader format';
131 my $attr = Class::MOP::Attribute->new('$test');
134 $attr->process_accessors('fail', 'my_failing_sub');
135 } '... cannot find "fail" type generator';
141 package My::Attribute;
142 our @ISA = ('Class::MOP::Attribute');
143 sub generate_reader_method { eval { die } }
146 my $attr = My::Attribute->new('$test' => (
151 $attr->install_accessors;
152 } '... failed to generate accessors correctly';
156 my $attr = Class::MOP::Attribute->new('$test' => (
157 predicate => 'has_test'
160 my $Bar = Class::MOP::Class->create('Bar');
161 isa_ok($Bar, 'Class::MOP::Class');
163 $Bar->add_attribute($attr);
165 can_ok('Bar', 'has_test');
167 is($attr, $Bar->remove_attribute('$test'), '... removed the $test attribute');
169 ok(!Bar->can('has_test'), '... Bar no longer has the "has_test" method');
175 # the next three tests once tested that
176 # the code would fail, but we lifted the
177 # restriction so you can have an accessor
178 # along with a reader/writer pair (I mean
179 # why not really). So now they test that
180 # it works, which is kinda silly, but it
181 # tests the API change, so I keep it.
184 Class::MOP::Attribute->new('$foo', (
188 } '... can create accessors with reader/writers';
191 Class::MOP::Attribute->new('$foo', (
195 } '... can create accessors with reader/writers';
198 Class::MOP::Attribute->new('$foo', (
203 } '... can create accessors with reader/writers';