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