Commit | Line | Data |
52e8a34c |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
6 | use Test::More no_plan => 2; |
7 | |
8 | BEGIN { |
9 | use_ok('Class::MOP'); |
10 | use_ok('t::lib::InsideOutClass'); |
11 | } |
12 | |
13 | { |
14 | package Foo; |
15 | |
16 | sub meta { InsideOutClass->initialize($_[0]) } |
17 | |
18 | Foo->meta->add_attribute( |
19 | InsideOutAttribute->new('foo' => ( |
20 | accessor => 'foo', |
21 | predicate => 'has_foo', |
22 | )) |
23 | ); |
24 | |
25 | Foo->meta->add_attribute( |
26 | InsideOutAttribute->new('bar' => ( |
27 | reader => 'get_bar', |
28 | writer => 'set_bar', |
29 | default => 'FOO is BAR' |
30 | )) |
31 | ); |
32 | |
33 | sub new { |
34 | my $class = shift; |
35 | bless $class->meta->construct_instance() => $class; |
36 | } |
37 | } |
38 | |
39 | my $foo = Foo->new(); |
40 | isa_ok($foo, 'Foo'); |
41 | |
42 | can_ok($foo, 'foo'); |
43 | can_ok($foo, 'has_foo'); |
44 | can_ok($foo, 'get_bar'); |
45 | can_ok($foo, 'set_bar'); |
46 | |
47 | ok(!$foo->has_foo, '... Foo::foo is not defined yet'); |
48 | is($foo->foo(), undef, '... Foo::foo is not defined yet'); |
49 | is($foo->get_bar(), 'FOO is BAR', '... Foo::bar has been initialized'); |
50 | |
51 | $foo->foo('This is Foo'); |
52 | |
53 | ok($foo->has_foo, '... Foo::foo is defined now'); |
54 | is($foo->foo(), 'This is Foo', '... Foo::foo == "This is Foo"'); |
55 | |
56 | $foo->set_bar(42); |
57 | is($foo->get_bar(), 42, '... Foo::bar == 42'); |
58 | |
59 | my $foo2 = Foo->new(); |
60 | isa_ok($foo2, 'Foo'); |
61 | |
62 | ok(!$foo2->has_foo, '... Foo2::foo is not defined yet'); |
63 | is($foo2->foo(), undef, '... Foo2::foo is not defined yet'); |
64 | is($foo2->get_bar(), 'FOO is BAR', '... Foo2::bar has been initialized'); |
65 | |
66 | $foo2->set_bar('DONT PANIC'); |
67 | is($foo2->get_bar(), 'DONT PANIC', '... Foo2::bar == DONT PANIC'); |
68 | |
69 | is($foo->get_bar(), 42, '... Foo::bar == 42'); |