6 use Test::More tests => 23;
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';
50 { # bad construtor args
52 Class::MOP::Attribute->new();
53 } '... no name argument';
56 Class::MOP::Attribute->new('');
57 } '... bad name argument';
60 Class::MOP::Attribute->new(0);
61 } '... bad name argument';
65 my $attr = Class::MOP::Attribute->new('$test');
67 $attr->attach_to_class();
68 } '... attach_to_class died as expected';
71 $attr->attach_to_class('Fail');
72 } '... attach_to_class died as expected';
75 $attr->attach_to_class(bless {} => 'Fail');
76 } '... attach_to_class died as expected';
80 my $attr = Class::MOP::Attribute->new('$test' => (
81 reader => [ 'whoops, this wont work' ]
84 $attr->attach_to_class(Class::MOP::Class->initialize('Foo'));
87 $attr->install_accessors;
88 } '... bad reader format';
92 my $attr = Class::MOP::Attribute->new('$test');
95 $attr->process_accessors('fail', 'my_failing_sub');
96 } '... cannot find "fail" type generator';
102 package My::Attribute;
103 our @ISA = ('Class::MOP::Attribute');
104 sub generate_reader_method { eval { die } }
107 my $attr = My::Attribute->new('$test' => (
112 $attr->install_accessors;
113 } '... failed to generate accessors correctly';
117 my $attr = Class::MOP::Attribute->new('$test' => (
118 predicate => 'has_test'
121 my $Bar = Class::MOP::Class->create('Bar');
122 isa_ok($Bar, 'Class::MOP::Class');
124 $Bar->add_attribute($attr);
126 can_ok('Bar', 'has_test');
128 is($attr, $Bar->remove_attribute('$test'), '... removed the $test attribute');
130 ok(!Bar->can('has_test'), '... Bar no longer has the "has_test" method');
136 # the next three tests once tested that
137 # the code would fail, but we lifted the
138 # restriction so you can have an accessor
139 # along with a reader/writer pair (I mean
140 # why not really). So now they test that
141 # it works, which is kinda silly, but it
142 # tests the API change, so I keep it.
145 Class::MOP::Attribute->new('$foo', (
149 } '... can create accessors with reader/writers';
152 Class::MOP::Attribute->new('$foo', (
156 } '... can create accessors with reader/writers';
159 Class::MOP::Attribute->new('$foo', (
164 } '... can create accessors with reader/writers';