make process_accessors private
[gitmo/Class-MOP.git] / t / 021_attribute_errors_and_edge_cases.t
1 use strict;
2 use warnings;
3
4 use Test::More tests => 27;
5 use Test::Exception;
6
7 BEGIN {use Class::MOP;use Class::MOP::Attribute;
8 }
9
10 # most values are static
11
12 {
13     dies_ok {
14         Class::MOP::Attribute->new('$test' => (
15             default => qr/hello (.*)/
16         ));
17     } '... no refs for defaults';
18
19     dies_ok {
20         Class::MOP::Attribute->new('$test' => (
21             default => []
22         ));
23     } '... no refs for defaults';
24
25     dies_ok {
26         Class::MOP::Attribute->new('$test' => (
27             default => {}
28         ));
29     } '... no refs for defaults';
30
31
32     dies_ok {
33         Class::MOP::Attribute->new('$test' => (
34             default => \(my $var)
35         ));
36     } '... no refs for defaults';
37
38     dies_ok {
39         Class::MOP::Attribute->new('$test' => (
40             default => bless {} => 'Foo'
41         ));
42     } '... no refs for defaults';
43
44 }
45
46 {
47     dies_ok {
48         Class::MOP::Attribute->new('$test' => (
49             builder => qr/hello (.*)/
50         ));
51     } '... no refs for builders';
52
53     dies_ok {
54         Class::MOP::Attribute->new('$test' => (
55             builder => []
56         ));
57     } '... no refs for builders';
58
59     dies_ok {
60         Class::MOP::Attribute->new('$test' => (
61             builder => {}
62         ));
63     } '... no refs for builders';
64
65
66     dies_ok {
67         Class::MOP::Attribute->new('$test' => (
68             builder => \(my $var)
69         ));
70     } '... no refs for builders';
71
72     dies_ok {
73         Class::MOP::Attribute->new('$test' => (
74             builder => bless {} => 'Foo'
75         ));
76     } '... no refs for builders';
77
78     dies_ok {
79         Class::MOP::Attribute->new('$test' => (
80             builder => 'Foo', default => 'Foo'
81         ));
82     } '... no default AND builder';
83
84 }
85
86
87 { # bad construtor args
88     dies_ok {
89         Class::MOP::Attribute->new();
90     } '... no name argument';
91
92     dies_ok {
93         Class::MOP::Attribute->new('');
94     } '... bad name argument';
95
96     dies_ok {
97         Class::MOP::Attribute->new(0);
98     } '... bad name argument';
99 }
100
101 {
102     my $attr = Class::MOP::Attribute->new('$test');
103     dies_ok {
104         $attr->attach_to_class();
105     } '... attach_to_class died as expected';
106
107     dies_ok {
108         $attr->attach_to_class('Fail');
109     } '... attach_to_class died as expected';
110
111     dies_ok {
112         $attr->attach_to_class(bless {} => 'Fail');
113     } '... attach_to_class died as expected';
114 }
115
116 {
117     my $attr = Class::MOP::Attribute->new('$test' => (
118         reader => [ 'whoops, this wont work' ]
119     ));
120
121     $attr->attach_to_class(Class::MOP::Class->initialize('Foo'));
122
123     dies_ok {
124         $attr->install_accessors;
125     } '... bad reader format';
126 }
127
128 {
129     my $attr = Class::MOP::Attribute->new('$test');
130
131     dies_ok {
132         $attr->_process_accessors('fail', 'my_failing_sub');
133     } '... cannot find "fail" type generator';
134 }
135
136
137 {
138     {
139         package My::Attribute;
140         our @ISA = ('Class::MOP::Attribute');
141         sub generate_reader_method { eval { die } }
142     }
143
144     my $attr = My::Attribute->new('$test' => (
145         reader => 'test'
146     ));
147
148     dies_ok {
149         $attr->install_accessors;
150     } '... failed to generate accessors correctly';
151 }
152
153 {
154     my $attr = Class::MOP::Attribute->new('$test' => (
155         predicate => 'has_test'
156     ));
157
158     my $Bar = Class::MOP::Class->create('Bar');
159     isa_ok($Bar, 'Class::MOP::Class');
160
161     $Bar->add_attribute($attr);
162
163     can_ok('Bar', 'has_test');
164
165     is($attr, $Bar->remove_attribute('$test'), '... removed the $test attribute');
166
167     ok(!Bar->can('has_test'), '... Bar no longer has the "has_test" method');
168 }
169
170
171 {
172     # NOTE:
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.
180
181     lives_ok {
182         Class::MOP::Attribute->new('$foo', (
183             accessor => 'foo',
184             reader   => 'get_foo',
185         ));
186     } '... can create accessors with reader/writers';
187
188     lives_ok {
189         Class::MOP::Attribute->new('$foo', (
190             accessor => 'foo',
191             writer   => 'set_foo',
192         ));
193     } '... can create accessors with reader/writers';
194
195     lives_ok {
196         Class::MOP::Attribute->new('$foo', (
197             accessor => 'foo',
198             reader   => 'get_foo',
199             writer   => 'set_foo',
200         ));
201     } '... can create accessors with reader/writers';
202 }