Commit | Line | Data |
2eb717d5 |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
c9e77dbb |
6 | use Test::More tests => 33; |
2eb717d5 |
7 | use Test::Exception; |
8 | |
9 | BEGIN { |
aa448b16 |
10 | use_ok('Class::MOP'); |
2eb717d5 |
11 | } |
12 | |
13 | my $FOO_ATTR = Class::MOP::Attribute->new('$foo'); |
14 | my $BAR_ATTR = Class::MOP::Attribute->new('$bar' => ( |
15 | accessor => 'bar' |
16 | )); |
17 | my $BAZ_ATTR = Class::MOP::Attribute->new('$baz' => ( |
18 | reader => 'get_baz', |
19 | writer => 'set_baz', |
20 | )); |
21 | |
22 | { |
23 | package Foo; |
aa448b16 |
24 | use metaclass; |
2eb717d5 |
25 | |
727919c5 |
26 | my $meta = Foo->meta; |
2eb717d5 |
27 | ::lives_ok { |
28 | $meta->add_attribute($FOO_ATTR); |
29 | } '... we added an attribute to Foo successfully'; |
30 | ::ok($meta->has_attribute('$foo'), '... Foo has $foo attribute'); |
31 | ::is($meta->get_attribute('$foo'), $FOO_ATTR, '... got the right attribute back for Foo'); |
32 | |
33 | ::ok(!$meta->has_method('foo'), '... no accessor created'); |
34 | } |
35 | { |
36 | package Bar; |
37 | our @ISA = ('Foo'); |
38 | |
727919c5 |
39 | my $meta = Bar->meta; |
2eb717d5 |
40 | ::lives_ok { |
41 | $meta->add_attribute($BAR_ATTR); |
42 | } '... we added an attribute to Bar successfully'; |
43 | ::ok($meta->has_attribute('$bar'), '... Bar has $bar attribute'); |
44 | ::is($meta->get_attribute('$bar'), $BAR_ATTR, '... got the right attribute back for Bar'); |
45 | |
46 | ::ok($meta->has_method('bar'), '... an accessor has been created'); |
47 | ::isa_ok($meta->get_method('bar'), 'Class::MOP::Attribute::Accessor'); |
48 | } |
49 | { |
50 | package Baz; |
51 | our @ISA = ('Bar'); |
52 | |
727919c5 |
53 | my $meta = Baz->meta; |
2eb717d5 |
54 | ::lives_ok { |
55 | $meta->add_attribute($BAZ_ATTR); |
56 | } '... we added an attribute to Baz successfully'; |
57 | ::ok($meta->has_attribute('$baz'), '... Baz has $baz attribute'); |
58 | ::is($meta->get_attribute('$baz'), $BAZ_ATTR, '... got the right attribute back for Baz'); |
59 | |
60 | ::ok($meta->has_method('get_baz'), '... a reader has been created'); |
61 | ::ok($meta->has_method('set_baz'), '... a writer has been created'); |
62 | |
63 | ::isa_ok($meta->get_method('get_baz'), 'Class::MOP::Attribute::Accessor'); |
64 | ::isa_ok($meta->get_method('set_baz'), 'Class::MOP::Attribute::Accessor'); |
65 | } |
66 | |
67 | { |
68 | my $meta = Baz->meta; |
69 | isa_ok($meta, 'Class::MOP::Class'); |
70 | |
71 | is_deeply( |
c9e77dbb |
72 | [ sort { $a->name cmp $b->name } $meta->compute_all_applicable_attributes() ], |
2eb717d5 |
73 | [ |
c9e77dbb |
74 | $BAR_ATTR, |
75 | $BAZ_ATTR, |
76 | $FOO_ATTR, |
2eb717d5 |
77 | ], |
78 | '... got the right list of applicable attributes for Baz'); |
c9e77dbb |
79 | |
80 | is_deeply( |
81 | [ map { $_->associated_class } sort { $a->name cmp $b->name } $meta->compute_all_applicable_attributes() ], |
82 | [ Bar->meta, Baz->meta, Foo->meta ], |
83 | '... got the right list of associated classes from the applicable attributes for Baz'); |
cbd9f942 |
84 | |
85 | my $attr; |
86 | lives_ok { |
87 | $attr = $meta->remove_attribute('$baz'); |
88 | } '... removed the $baz attribute successfully'; |
89 | is($attr, $BAZ_ATTR, '... got the right attribute back for Baz'); |
90 | |
91 | ok(!$meta->has_attribute('$baz'), '... Baz no longer has $baz attribute'); |
92 | |
93 | ok(!$meta->has_method('get_baz'), '... a reader has been removed'); |
94 | ok(!$meta->has_method('set_baz'), '... a writer has been removed'); |
95 | |
96 | is_deeply( |
c9e77dbb |
97 | [ sort { $a->name cmp $b->name } $meta->compute_all_applicable_attributes() ], |
cbd9f942 |
98 | [ |
c9e77dbb |
99 | $BAR_ATTR, |
100 | $FOO_ATTR, |
cbd9f942 |
101 | ], |
102 | '... got the right list of applicable attributes for Baz'); |
103 | |
c9e77dbb |
104 | is_deeply( |
105 | [ map { $_->associated_class } sort { $a->name cmp $b->name } $meta->compute_all_applicable_attributes() ], |
106 | [ Bar->meta, Foo->meta ], |
107 | '... got the right list of associated classes from the applicable attributes for Baz'); |
108 | |
cbd9f942 |
109 | { |
110 | my $attr; |
111 | lives_ok { |
112 | $attr = Bar->meta->remove_attribute('$bar'); |
113 | } '... removed the $bar attribute successfully'; |
114 | is($attr, $BAR_ATTR, '... got the right attribute back for Bar'); |
2eb717d5 |
115 | |
cbd9f942 |
116 | ok(!Bar->meta->has_attribute('$bar'), '... Bar no longer has $bar attribute'); |
2eb717d5 |
117 | |
cbd9f942 |
118 | ok(!Bar->meta->has_method('bar'), '... a accessor has been removed'); |
119 | } |
120 | |
121 | is_deeply( |
c9e77dbb |
122 | [ sort { $a->name cmp $b->name } $meta->compute_all_applicable_attributes() ], |
cbd9f942 |
123 | [ |
c9e77dbb |
124 | $FOO_ATTR, |
cbd9f942 |
125 | ], |
126 | '... got the right list of applicable attributes for Baz'); |
127 | |
c9e77dbb |
128 | is_deeply( |
129 | [ map { $_->associated_class } sort { $a->name cmp $b->name } $meta->compute_all_applicable_attributes() ], |
130 | [ Foo->meta ], |
131 | '... got the right list of associated classes from the applicable attributes for Baz'); |
132 | |
cbd9f942 |
133 | } |