File: 102_InsideOutClass_test.t

package info (click to toggle)
libclass-mop-perl 1.04-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 1,244 kB
  • ctags: 1,272
  • sloc: perl: 5,192; ansic: 241; makefile: 2
file content (224 lines) | stat: -rw-r--r-- 7,035 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
use strict;
use warnings;

use Test::More;
use File::Spec;
use Scalar::Util 'reftype';

BEGIN {use Class::MOP;
    require_ok(File::Spec->catfile('examples', 'InsideOutClass.pod'));
}

{
    package Foo;

    use strict;
    use warnings;

    use metaclass (
        'attribute_metaclass' => 'InsideOutClass::Attribute',
        'instance_metaclass'  => 'InsideOutClass::Instance'
    );

    Foo->meta->add_attribute('foo' => (
        accessor  => 'foo',
        predicate => 'has_foo',
    ));

    Foo->meta->add_attribute('bar' => (
        reader  => 'get_bar',
        writer  => 'set_bar',
        default => 'FOO is BAR'
    ));

    sub new  {
        my $class = shift;
        $class->meta->new_object(@_);
    }

    package Bar;
    use metaclass (
        'attribute_metaclass' => 'InsideOutClass::Attribute',
        'instance_metaclass'  => 'InsideOutClass::Instance'
    );

    use strict;
    use warnings;

    use base 'Foo';

    Bar->meta->add_attribute('baz' => (
        accessor  => 'baz',
        predicate => 'has_baz',
    ));

    package Baz;

    use strict;
    use warnings;
    use metaclass (
        'attribute_metaclass' => 'InsideOutClass::Attribute',
        'instance_metaclass'  => 'InsideOutClass::Instance'
    );

    Baz->meta->add_attribute('bling' => (
        accessor  => 'bling',
        default   => 'Baz::bling'
    ));

    package Bar::Baz;
    use metaclass (
        'attribute_metaclass' => 'InsideOutClass::Attribute',
        'instance_metaclass'  => 'InsideOutClass::Instance'
    );

    use strict;
    use warnings;

    use base 'Bar', 'Baz';
}

my $foo = Foo->new();
isa_ok($foo, 'Foo');

is(reftype($foo), 'SCALAR', '... Foo is made with SCALAR');

can_ok($foo, 'foo');
can_ok($foo, 'has_foo');
can_ok($foo, 'get_bar');
can_ok($foo, 'set_bar');

ok(!$foo->has_foo, '... Foo::foo is not defined yet');
is($foo->foo(), undef, '... Foo::foo is not defined yet');
is($foo->get_bar(), 'FOO is BAR', '... Foo::bar has been initialized');

$foo->foo('This is Foo');

ok($foo->has_foo, '... Foo::foo is defined now');
is($foo->foo(), 'This is Foo', '... Foo::foo == "This is Foo"');

$foo->set_bar(42);
is($foo->get_bar(), 42, '... Foo::bar == 42');

my $foo2 = Foo->new();
isa_ok($foo2, 'Foo');

is(reftype($foo2), 'SCALAR', '... Foo is made with SCALAR');

ok(!$foo2->has_foo, '... Foo2::foo is not defined yet');
is($foo2->foo(), undef, '... Foo2::foo is not defined yet');
is($foo2->get_bar(), 'FOO is BAR', '... Foo2::bar has been initialized');

$foo2->set_bar('DONT PANIC');
is($foo2->get_bar(), 'DONT PANIC', '... Foo2::bar == DONT PANIC');

is($foo->get_bar(), 42, '... Foo::bar == 42');

# now Bar ...

my $bar = Bar->new();
isa_ok($bar, 'Bar');
isa_ok($bar, 'Foo');

is(reftype($bar), 'SCALAR', '... Bar is made with SCALAR');

can_ok($bar, 'foo');
can_ok($bar, 'has_foo');
can_ok($bar, 'get_bar');
can_ok($bar, 'set_bar');
can_ok($bar, 'baz');
can_ok($bar, 'has_baz');

ok(!$bar->has_foo, '... Bar::foo is not defined yet');
is($bar->foo(), undef, '... Bar::foo is not defined yet');
is($bar->get_bar(), 'FOO is BAR', '... Bar::bar has been initialized');
ok(!$bar->has_baz, '... Bar::baz is not defined yet');
is($bar->baz(), undef, '... Bar::baz is not defined yet');

$bar->foo('This is Bar::foo');

ok($bar->has_foo, '... Bar::foo is defined now');
is($bar->foo(), 'This is Bar::foo', '... Bar::foo == "This is Bar"');
is($bar->get_bar(), 'FOO is BAR', '... Bar::bar has been initialized');

$bar->baz('This is Bar::baz');

ok($bar->has_baz, '... Bar::baz is defined now');
is($bar->baz(), 'This is Bar::baz', '... Bar::foo == "This is Bar"');
is($bar->foo(), 'This is Bar::foo', '... Bar::foo == "This is Bar"');
is($bar->get_bar(), 'FOO is BAR', '... Bar::bar has been initialized');

# now Baz ...

my $baz = Bar::Baz->new();
isa_ok($baz, 'Bar::Baz');
isa_ok($baz, 'Bar');
isa_ok($baz, 'Foo');
isa_ok($baz, 'Baz');

is(reftype($baz), 'SCALAR', '... Bar::Baz is made with SCALAR');

can_ok($baz, 'foo');
can_ok($baz, 'has_foo');
can_ok($baz, 'get_bar');
can_ok($baz, 'set_bar');
can_ok($baz, 'baz');
can_ok($baz, 'has_baz');
can_ok($baz, 'bling');

is($baz->get_bar(), 'FOO is BAR', '... Bar::Baz::bar has been initialized');
is($baz->bling(), 'Baz::bling', '... Bar::Baz::bling has been initialized');

ok(!$baz->has_foo, '... Bar::Baz::foo is not defined yet');
is($baz->foo(), undef, '... Bar::Baz::foo is not defined yet');
ok(!$baz->has_baz, '... Bar::Baz::baz is not defined yet');
is($baz->baz(), undef, '... Bar::Baz::baz is not defined yet');

$baz->foo('This is Bar::Baz::foo');

ok($baz->has_foo, '... Bar::Baz::foo is defined now');
is($baz->foo(), 'This is Bar::Baz::foo', '... Bar::Baz::foo == "This is Bar"');
is($baz->get_bar(), 'FOO is BAR', '... Bar::Baz::bar has been initialized');
is($baz->bling(), 'Baz::bling', '... Bar::Baz::bling has been initialized');

$baz->baz('This is Bar::Baz::baz');

ok($baz->has_baz, '... Bar::Baz::baz is defined now');
is($baz->baz(), 'This is Bar::Baz::baz', '... Bar::Baz::foo == "This is Bar"');
is($baz->foo(), 'This is Bar::Baz::foo', '... Bar::Baz::foo == "This is Bar"');
is($baz->get_bar(), 'FOO is BAR', '... Bar::Baz::bar has been initialized');
is($baz->bling(), 'Baz::bling', '... Bar::Baz::bling has been initialized');

{
    no strict 'refs';

    ok(*{'Foo::foo'}{HASH}, '... there is a foo package variable in Foo');
    ok(*{'Foo::bar'}{HASH}, '... there is a bar package variable in Foo');

    is(scalar(keys(%{'Foo::foo'})), 4, '... got the right number of entries for Foo::foo');
    is(scalar(keys(%{'Foo::bar'})), 4, '... got the right number of entries for Foo::bar');

    ok(!*{'Bar::foo'}{HASH}, '... no foo package variable in Bar');
    ok(!*{'Bar::bar'}{HASH}, '... no bar package variable in Bar');
    ok(*{'Bar::baz'}{HASH}, '... there is a baz package variable in Bar');

    is(scalar(keys(%{'Bar::foo'})), 0, '... got the right number of entries for Bar::foo');
    is(scalar(keys(%{'Bar::bar'})), 0, '... got the right number of entries for Bar::bar');
    is(scalar(keys(%{'Bar::baz'})), 2, '... got the right number of entries for Bar::baz');

    ok(*{'Baz::bling'}{HASH}, '... there is a bar package variable in Baz');

    is(scalar(keys(%{'Baz::bling'})), 1, '... got the right number of entries for Baz::bling');

    ok(!*{'Bar::Baz::foo'}{HASH}, '... no foo package variable in Bar::Baz');
    ok(!*{'Bar::Baz::bar'}{HASH}, '... no bar package variable in Bar::Baz');
    ok(!*{'Bar::Baz::baz'}{HASH}, '... no baz package variable in Bar::Baz');
    ok(!*{'Bar::Baz::bling'}{HASH}, '... no bar package variable in Baz::Baz');

    is(scalar(keys(%{'Bar::Baz::foo'})), 0, '... got the right number of entries for Bar::Baz::foo');
    is(scalar(keys(%{'Bar::Baz::bar'})), 0, '... got the right number of entries for Bar::Baz::bar');
    is(scalar(keys(%{'Bar::Baz::baz'})), 0, '... got the right number of entries for Bar::Baz::baz');
    is(scalar(keys(%{'Bar::Baz::bling'})), 0, '... got the right number of entries for Bar::Baz::bling');
}

done_testing;