4 use Test::More tests => 27;
7 BEGIN {use Class::MOP;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';
87 { # bad construtor args
89 Class::MOP::Attribute->new();
90 } '... no name argument';
92 # These are no longer errors
94 Class::MOP::Attribute->new('');
95 } '... bad name argument';
98 Class::MOP::Attribute->new(0);
99 } '... bad name argument';
103 my $attr = Class::MOP::Attribute->new('$test');
105 $attr->attach_to_class();
106 } '... attach_to_class died as expected';
109 $attr->attach_to_class('Fail');
110 } '... attach_to_class died as expected';
113 $attr->attach_to_class(bless {} => 'Fail');
114 } '... attach_to_class died as expected';
118 my $attr = Class::MOP::Attribute->new('$test' => (
119 reader => [ 'whoops, this wont work' ]
122 $attr->attach_to_class(Class::MOP::Class->initialize('Foo'));
125 $attr->install_accessors;
126 } '... bad reader format';
130 my $attr = Class::MOP::Attribute->new('$test');
133 $attr->_process_accessors('fail', 'my_failing_sub');
134 } '... cannot find "fail" type generator';
140 package My::Attribute;
141 our @ISA = ('Class::MOP::Attribute');
142 sub generate_reader_method { eval { die } }
145 my $attr = My::Attribute->new('$test' => (
150 $attr->install_accessors;
151 } '... failed to generate accessors correctly';
155 my $attr = Class::MOP::Attribute->new('$test' => (
156 predicate => 'has_test'
159 my $Bar = Class::MOP::Class->create('Bar');
160 isa_ok($Bar, 'Class::MOP::Class');
162 $Bar->add_attribute($attr);
164 can_ok('Bar', 'has_test');
166 is($attr, $Bar->remove_attribute('$test'), '... removed the $test attribute');
168 ok(!Bar->can('has_test'), '... Bar no longer has the "has_test" method');
174 # the next three tests once tested that
175 # the code would fail, but we lifted the
176 # restriction so you can have an accessor
177 # along with a reader/writer pair (I mean
178 # why not really). So now they test that
179 # it works, which is kinda silly, but it
180 # tests the API change, so I keep it.
183 Class::MOP::Attribute->new('$foo', (
187 } '... can create accessors with reader/writers';
190 Class::MOP::Attribute->new('$foo', (
194 } '... can create accessors with reader/writers';
197 Class::MOP::Attribute->new('$foo', (
202 } '... can create accessors with reader/writers';