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