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';
93 Class::MOP::Attribute->new('');
94 } '... bad name argument';
97 Class::MOP::Attribute->new(0);
98 } '... bad name argument';
102 my $attr = Class::MOP::Attribute->new('$test');
104 $attr->attach_to_class();
105 } '... attach_to_class died as expected';
108 $attr->attach_to_class('Fail');
109 } '... attach_to_class died as expected';
112 $attr->attach_to_class(bless {} => 'Fail');
113 } '... attach_to_class died as expected';
117 my $attr = Class::MOP::Attribute->new('$test' => (
118 reader => [ 'whoops, this wont work' ]
121 $attr->attach_to_class(Class::MOP::Class->initialize('Foo'));
124 $attr->install_accessors;
125 } '... bad reader format';
129 my $attr = Class::MOP::Attribute->new('$test');
132 $attr->process_accessors('fail', 'my_failing_sub');
133 } '... cannot find "fail" type generator';
139 package My::Attribute;
140 our @ISA = ('Class::MOP::Attribute');
141 sub generate_reader_method { eval { die } }
144 my $attr = My::Attribute->new('$test' => (
149 $attr->install_accessors;
150 } '... failed to generate accessors correctly';
154 my $attr = Class::MOP::Attribute->new('$test' => (
155 predicate => 'has_test'
158 my $Bar = Class::MOP::Class->create('Bar');
159 isa_ok($Bar, 'Class::MOP::Class');
161 $Bar->add_attribute($attr);
163 can_ok('Bar', 'has_test');
165 is($attr, $Bar->remove_attribute('$test'), '... removed the $test attribute');
167 ok(!Bar->can('has_test'), '... Bar no longer has the "has_test" method');
173 # the next three tests once tested that
174 # the code would fail, but we lifted the
175 # restriction so you can have an accessor
176 # along with a reader/writer pair (I mean
177 # why not really). So now they test that
178 # it works, which is kinda silly, but it
179 # tests the API change, so I keep it.
182 Class::MOP::Attribute->new('$foo', (
186 } '... can create accessors with reader/writers';
189 Class::MOP::Attribute->new('$foo', (
193 } '... can create accessors with reader/writers';
196 Class::MOP::Attribute->new('$foo', (
201 } '... can create accessors with reader/writers';