Commit | Line | Data |
d6fbcd05 |
1 | use strict; |
2 | use warnings; |
3 | |
efd3d14c |
4 | use Test::More tests => 28; |
d6fbcd05 |
5 | use File::Spec; |
6 | |
efd3d14c |
7 | BEGIN {use Class::MOP; |
10dd437b |
8 | require_ok(File::Spec->catfile('examples', 'ClassEncapsulatedAttributes.pod')); |
d6fbcd05 |
9 | } |
10 | |
11 | { |
12 | package Foo; |
13 | |
677eb158 |
14 | use metaclass 'ClassEncapsulatedAttributes'; |
d6fbcd05 |
15 | |
2e41896e |
16 | Foo->meta->add_attribute('foo' => ( |
17 | accessor => 'foo', |
18 | predicate => 'has_foo', |
19 | default => 'init in FOO' |
20 | )); |
d6fbcd05 |
21 | |
2e41896e |
22 | Foo->meta->add_attribute('bar' => ( |
23 | reader => 'get_bar', |
24 | writer => 'set_bar', |
25 | default => 'init in FOO' |
26 | )); |
d6fbcd05 |
27 | |
28 | sub new { |
29 | my $class = shift; |
5659d76e |
30 | $class->meta->new_object(@_); |
d6fbcd05 |
31 | } |
32 | |
33 | package Bar; |
34 | our @ISA = ('Foo'); |
35 | |
2e41896e |
36 | Bar->meta->add_attribute('foo' => ( |
37 | accessor => 'foo', |
38 | predicate => 'has_foo', |
39 | default => 'init in BAR' |
40 | )); |
d6fbcd05 |
41 | |
2e41896e |
42 | Bar->meta->add_attribute('bar' => ( |
43 | reader => 'get_bar', |
44 | writer => 'set_bar', |
45 | default => 'init in BAR' |
46 | )); |
d6fbcd05 |
47 | |
48 | sub SUPER_foo { (shift)->SUPER::foo(@_) } |
49 | sub SUPER_has_foo { (shift)->SUPER::foo(@_) } |
50 | sub SUPER_get_bar { (shift)->SUPER::get_bar() } |
51 | sub SUPER_set_bar { (shift)->SUPER::set_bar(@_) } |
52 | |
53 | } |
54 | |
55 | { |
56 | my $foo = Foo->new(); |
57 | isa_ok($foo, 'Foo'); |
58 | |
59 | can_ok($foo, 'foo'); |
60 | can_ok($foo, 'has_foo'); |
61 | can_ok($foo, 'get_bar'); |
62 | can_ok($foo, 'set_bar'); |
63 | |
64 | my $bar = Bar->new(); |
65 | isa_ok($bar, 'Bar'); |
66 | |
67 | can_ok($bar, 'foo'); |
68 | can_ok($bar, 'has_foo'); |
69 | can_ok($bar, 'get_bar'); |
70 | can_ok($bar, 'set_bar'); |
71 | |
72 | ok($foo->has_foo, '... Foo::has_foo == 1'); |
73 | ok($bar->has_foo, '... Bar::has_foo == 1'); |
74 | |
75 | is($foo->foo, 'init in FOO', '... got the right default value for Foo::foo'); |
76 | is($bar->foo, 'init in BAR', '... got the right default value for Bar::foo'); |
77 | |
78 | is($bar->SUPER_foo(), 'init in FOO', '... got the right default value for Bar::SUPER::foo'); |
79 | |
80 | $bar->SUPER_foo(undef); |
81 | |
82 | is($bar->SUPER_foo(), undef, '... successfully set Foo::foo through Bar::SUPER::foo'); |
83 | ok(!$bar->SUPER_has_foo, '... BAR::SUPER::has_foo == 0'); |
84 | |
85 | ok($foo->has_foo, '... Foo::has_foo (is still) 1'); |
86 | } |
87 | |
88 | { |
89 | my $bar = Bar->new( |
90 | 'Foo' => { 'foo' => 'Foo::foo' }, |
91 | 'Bar' => { 'foo' => 'Bar::foo' } |
92 | ); |
93 | isa_ok($bar, 'Bar'); |
94 | |
95 | can_ok($bar, 'foo'); |
96 | can_ok($bar, 'has_foo'); |
97 | can_ok($bar, 'get_bar'); |
98 | can_ok($bar, 'set_bar'); |
99 | |
100 | ok($bar->has_foo, '... Bar::has_foo == 1'); |
101 | ok($bar->SUPER_has_foo, '... Bar::SUPER_has_foo == 1'); |
102 | |
103 | is($bar->foo, 'Bar::foo', '... got the right default value for Bar::foo'); |
104 | is($bar->SUPER_foo(), 'Foo::foo', '... got the right default value for Bar::SUPER::foo'); |
105 | } |
106 | |