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