Deprecate get_attribute_map
[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     # These are no longer errors
93     lives_ok {
94         Class::MOP::Attribute->new('');
95     } '... bad name argument';
96
97     lives_ok {
98         Class::MOP::Attribute->new(0);
99     } '... bad name argument';
100 }
101
102 {
103     my $attr = Class::MOP::Attribute->new('$test');
104     dies_ok {
105         $attr->attach_to_class();
106     } '... attach_to_class died as expected';
107
108     dies_ok {
109         $attr->attach_to_class('Fail');
110     } '... attach_to_class died as expected';
111
112     dies_ok {
113         $attr->attach_to_class(bless {} => 'Fail');
114     } '... attach_to_class died as expected';
115 }
116
117 {
118     my $attr = Class::MOP::Attribute->new('$test' => (
119         reader => [ 'whoops, this wont work' ]
120     ));
121
122     $attr->attach_to_class(Class::MOP::Class->initialize('Foo'));
123
124     dies_ok {
125         $attr->install_accessors;
126     } '... bad reader format';
127 }
128
129 {
130     my $attr = Class::MOP::Attribute->new('$test');
131
132     dies_ok {
133         $attr->_process_accessors('fail', 'my_failing_sub');
134     } '... cannot find "fail" type generator';
135 }
136
137
138 {
139     {
140         package My::Attribute;
141         our @ISA = ('Class::MOP::Attribute');
142         sub generate_reader_method { eval { die } }
143     }
144
145     my $attr = My::Attribute->new('$test' => (
146         reader => 'test'
147     ));
148
149     dies_ok {
150         $attr->install_accessors;
151     } '... failed to generate accessors correctly';
152 }
153
154 {
155     my $attr = Class::MOP::Attribute->new('$test' => (
156         predicate => 'has_test'
157     ));
158
159     my $Bar = Class::MOP::Class->create('Bar');
160     isa_ok($Bar, 'Class::MOP::Class');
161
162     $Bar->add_attribute($attr);
163
164     can_ok('Bar', 'has_test');
165
166     is($attr, $Bar->remove_attribute('$test'), '... removed the $test attribute');
167
168     ok(!Bar->can('has_test'), '... Bar no longer has the "has_test" method');
169 }
170
171
172 {
173     # NOTE:
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.
181
182     lives_ok {
183         Class::MOP::Attribute->new('$foo', (
184             accessor => 'foo',
185             reader   => 'get_foo',
186         ));
187     } '... can create accessors with reader/writers';
188
189     lives_ok {
190         Class::MOP::Attribute->new('$foo', (
191             accessor => 'foo',
192             writer   => 'set_foo',
193         ));
194     } '... can create accessors with reader/writers';
195
196     lives_ok {
197         Class::MOP::Attribute->new('$foo', (
198             accessor => 'foo',
199             reader   => 'get_foo',
200             writer   => 'set_foo',
201         ));
202     } '... can create accessors with reader/writers';
203 }