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