add tests for wrapping a method metaobject
[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
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{
1d68af04 102 my $attr = Class::MOP::Attribute->new('$test');
013b1897 103 dies_ok {
104 $attr->attach_to_class();
105 } '... attach_to_class died as expected';
1d68af04 106
013b1897 107 dies_ok {
108 $attr->attach_to_class('Fail');
1d68af04 109 } '... attach_to_class died as expected';
110
013b1897 111 dies_ok {
112 $attr->attach_to_class(bless {} => 'Fail');
1d68af04 113 } '... attach_to_class died as expected';
013b1897 114}
115
116{
117 my $attr = Class::MOP::Attribute->new('$test' => (
118 reader => [ 'whoops, this wont work' ]
119 ));
1d68af04 120
013b1897 121 $attr->attach_to_class(Class::MOP::Class->initialize('Foo'));
122
123 dies_ok {
124 $attr->install_accessors;
1d68af04 125 } '... bad reader format';
013b1897 126}
127
128{
129 my $attr = Class::MOP::Attribute->new('$test');
130
131 dies_ok {
45a183fb 132 $attr->_process_accessors('fail', 'my_failing_sub');
013b1897 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 ));
1d68af04 147
013b1897 148 dies_ok {
149 $attr->install_accessors;
1d68af04 150 } '... failed to generate accessors correctly';
013b1897 151}
152
153{
154 my $attr = Class::MOP::Attribute->new('$test' => (
155 predicate => 'has_test'
156 ));
1d68af04 157
88dd563c 158 my $Bar = Class::MOP::Class->create('Bar');
013b1897 159 isa_ok($Bar, 'Class::MOP::Class');
1d68af04 160
013b1897 161 $Bar->add_attribute($attr);
1d68af04 162
013b1897 163 can_ok('Bar', 'has_test');
1d68af04 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');
013b1897 168}
169
170
171{
172 # NOTE:
1d68af04 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
013b1897 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',
1d68af04 198 reader => 'get_foo',
013b1897 199 writer => 'set_foo',
200 ));
201 } '... can create accessors with reader/writers';
202}