start of the new user-level API
[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
17 sub meta { InsideOutClass->initialize($_[0]) }
18
2e41896e 19 Foo->meta->add_attribute('foo' => (
20 accessor => 'foo',
21 predicate => 'has_foo',
22 ));
52e8a34c 23
2e41896e 24 Foo->meta->add_attribute('bar' => (
25 reader => 'get_bar',
26 writer => 'set_bar',
27 default => 'FOO is BAR'
28 ));
52e8a34c 29
30 sub new {
31 my $class = shift;
d6fbcd05 32 bless $class->meta->construct_instance(@_) => $class;
52e8a34c 33 }
34}
35
36my $foo = Foo->new();
37isa_ok($foo, 'Foo');
38
39can_ok($foo, 'foo');
40can_ok($foo, 'has_foo');
41can_ok($foo, 'get_bar');
42can_ok($foo, 'set_bar');
43
44ok(!$foo->has_foo, '... Foo::foo is not defined yet');
45is($foo->foo(), undef, '... Foo::foo is not defined yet');
46is($foo->get_bar(), 'FOO is BAR', '... Foo::bar has been initialized');
47
48$foo->foo('This is Foo');
49
50ok($foo->has_foo, '... Foo::foo is defined now');
51is($foo->foo(), 'This is Foo', '... Foo::foo == "This is Foo"');
52
53$foo->set_bar(42);
54is($foo->get_bar(), 42, '... Foo::bar == 42');
55
56my $foo2 = Foo->new();
57isa_ok($foo2, 'Foo');
58
59ok(!$foo2->has_foo, '... Foo2::foo is not defined yet');
60is($foo2->foo(), undef, '... Foo2::foo is not defined yet');
61is($foo2->get_bar(), 'FOO is BAR', '... Foo2::bar has been initialized');
62
63$foo2->set_bar('DONT PANIC');
64is($foo2->get_bar(), 'DONT PANIC', '... Foo2::bar == DONT PANIC');
65
66is($foo->get_bar(), 42, '... Foo::bar == 42');