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