Commit | Line | Data |
013b1897 |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
efd3d14c |
6 | use Test::More tests => 27; |
013b1897 |
7 | use Test::Exception; |
8 | |
efd3d14c |
9 | BEGIN {use Class::MOP;use Class::MOP::Attribute; |
013b1897 |
10 | } |
11 | |
148b4697 |
12 | # most values are static |
013b1897 |
13 | |
14 | { |
148b4697 |
15 | dies_ok { |
16 | Class::MOP::Attribute->new('$test' => ( |
17 | default => qr/hello (.*)/ |
18 | )); |
19 | } '... no refs for defaults'; |
1d68af04 |
20 | |
148b4697 |
21 | dies_ok { |
22 | Class::MOP::Attribute->new('$test' => ( |
23 | default => [] |
24 | )); |
1d68af04 |
25 | } '... no refs for defaults'; |
26 | |
148b4697 |
27 | dies_ok { |
28 | Class::MOP::Attribute->new('$test' => ( |
29 | default => {} |
30 | )); |
1d68af04 |
31 | } '... no refs for defaults'; |
32 | |
33 | |
148b4697 |
34 | dies_ok { |
35 | Class::MOP::Attribute->new('$test' => ( |
36 | default => \(my $var) |
37 | )); |
1d68af04 |
38 | } '... no refs for defaults'; |
148b4697 |
39 | |
40 | dies_ok { |
41 | Class::MOP::Attribute->new('$test' => ( |
42 | default => bless {} => 'Foo' |
43 | )); |
44 | } '... no refs for defaults'; |
45 | |
013b1897 |
46 | } |
47 | |
1d68af04 |
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 | |
013b1897 |
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 | { |
1d68af04 |
104 | my $attr = Class::MOP::Attribute->new('$test'); |
013b1897 |
105 | dies_ok { |
106 | $attr->attach_to_class(); |
107 | } '... attach_to_class died as expected'; |
1d68af04 |
108 | |
013b1897 |
109 | dies_ok { |
110 | $attr->attach_to_class('Fail'); |
1d68af04 |
111 | } '... attach_to_class died as expected'; |
112 | |
013b1897 |
113 | dies_ok { |
114 | $attr->attach_to_class(bless {} => 'Fail'); |
1d68af04 |
115 | } '... attach_to_class died as expected'; |
013b1897 |
116 | } |
117 | |
118 | { |
119 | my $attr = Class::MOP::Attribute->new('$test' => ( |
120 | reader => [ 'whoops, this wont work' ] |
121 | )); |
1d68af04 |
122 | |
013b1897 |
123 | $attr->attach_to_class(Class::MOP::Class->initialize('Foo')); |
124 | |
125 | dies_ok { |
126 | $attr->install_accessors; |
1d68af04 |
127 | } '... bad reader format'; |
013b1897 |
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 | )); |
1d68af04 |
149 | |
013b1897 |
150 | dies_ok { |
151 | $attr->install_accessors; |
1d68af04 |
152 | } '... failed to generate accessors correctly'; |
013b1897 |
153 | } |
154 | |
155 | { |
156 | my $attr = Class::MOP::Attribute->new('$test' => ( |
157 | predicate => 'has_test' |
158 | )); |
1d68af04 |
159 | |
88dd563c |
160 | my $Bar = Class::MOP::Class->create('Bar'); |
013b1897 |
161 | isa_ok($Bar, 'Class::MOP::Class'); |
1d68af04 |
162 | |
013b1897 |
163 | $Bar->add_attribute($attr); |
1d68af04 |
164 | |
013b1897 |
165 | can_ok('Bar', 'has_test'); |
1d68af04 |
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'); |
013b1897 |
170 | } |
171 | |
172 | |
173 | { |
174 | # NOTE: |
1d68af04 |
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 |
013b1897 |
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', |
1d68af04 |
200 | reader => 'get_foo', |
013b1897 |
201 | writer => 'set_foo', |
202 | )); |
203 | } '... can create accessors with reader/writers'; |
204 | } |