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