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