We accept false attribute names now, update the test
[gitmo/Class-MOP.git] / t / 021_attribute_errors_and_edge_cases.t
CommitLineData
013b1897 1use strict;
2use warnings;
3
efd3d14c 4use Test::More tests => 27;
013b1897 5use Test::Exception;
6
efd3d14c 7BEGIN {use Class::MOP;use Class::MOP::Attribute;
013b1897 8}
9
148b4697 10# most values are static
013b1897 11
12{
148b4697 13 dies_ok {
14 Class::MOP::Attribute->new('$test' => (
15 default => qr/hello (.*)/
16 ));
17 } '... no refs for defaults';
1d68af04 18
148b4697 19 dies_ok {
20 Class::MOP::Attribute->new('$test' => (
21 default => []
22 ));
1d68af04 23 } '... no refs for defaults';
24
148b4697 25 dies_ok {
26 Class::MOP::Attribute->new('$test' => (
27 default => {}
28 ));
1d68af04 29 } '... no refs for defaults';
30
31
148b4697 32 dies_ok {
33 Class::MOP::Attribute->new('$test' => (
34 default => \(my $var)
35 ));
1d68af04 36 } '... no refs for defaults';
148b4697 37
38 dies_ok {
39 Class::MOP::Attribute->new('$test' => (
40 default => bless {} => 'Foo'
41 ));
42 } '... no refs for defaults';
43
013b1897 44}
45
1d68af04 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
013b1897 87{ # bad construtor args
88 dies_ok {
89 Class::MOP::Attribute->new();
90 } '... no name argument';
91
dbd0814d 92 # These are no longer errors
93 lives_ok {
013b1897 94 Class::MOP::Attribute->new('');
95 } '... bad name argument';
96
dbd0814d 97 lives_ok {
013b1897 98 Class::MOP::Attribute->new(0);
99 } '... bad name argument';
100}
101
102{
1d68af04 103 my $attr = Class::MOP::Attribute->new('$test');
013b1897 104 dies_ok {
105 $attr->attach_to_class();
106 } '... attach_to_class died as expected';
1d68af04 107
013b1897 108 dies_ok {
109 $attr->attach_to_class('Fail');
1d68af04 110 } '... attach_to_class died as expected';
111
013b1897 112 dies_ok {
113 $attr->attach_to_class(bless {} => 'Fail');
1d68af04 114 } '... attach_to_class died as expected';
013b1897 115}
116
117{
118 my $attr = Class::MOP::Attribute->new('$test' => (
119 reader => [ 'whoops, this wont work' ]
120 ));
1d68af04 121
013b1897 122 $attr->attach_to_class(Class::MOP::Class->initialize('Foo'));
123
124 dies_ok {
125 $attr->install_accessors;
1d68af04 126 } '... bad reader format';
013b1897 127}
128
129{
130 my $attr = Class::MOP::Attribute->new('$test');
131
132 dies_ok {
45a183fb 133 $attr->_process_accessors('fail', 'my_failing_sub');
013b1897 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 ));
1d68af04 148
013b1897 149 dies_ok {
150 $attr->install_accessors;
1d68af04 151 } '... failed to generate accessors correctly';
013b1897 152}
153
154{
155 my $attr = Class::MOP::Attribute->new('$test' => (
156 predicate => 'has_test'
157 ));
1d68af04 158
88dd563c 159 my $Bar = Class::MOP::Class->create('Bar');
013b1897 160 isa_ok($Bar, 'Class::MOP::Class');
1d68af04 161
013b1897 162 $Bar->add_attribute($attr);
1d68af04 163
013b1897 164 can_ok('Bar', 'has_test');
1d68af04 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');
013b1897 169}
170
171
172{
173 # NOTE:
1d68af04 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
013b1897 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',
1d68af04 199 reader => 'get_foo',
013b1897 200 writer => 'set_foo',
201 ));
202 } '... can create accessors with reader/writers';
203}