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