* Moose
[gitmo/Moose.git] / t / 013_inner_and_augment.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 17;
7 use Test::Exception;
8
9 BEGIN {
10     use_ok('Moose');           
11 }
12
13 {
14     package Foo;
15     use Moose;
16     
17     sub foo { 'Foo::foo(' . (inner() || '') . ')' }
18     sub bar { 'Foo::bar(' . (inner() || '') . ')' }    
19     sub baz { 'Foo::baz(' . (inner() || '') . ')' }        
20     
21     package Bar;
22     use Moose;
23     
24     extends 'Foo';
25     
26     augment foo => sub { 'Bar::foo(' . (inner() || '') . ')' };   
27     augment bar => sub { 'Bar::bar' };       
28
29     no Moose; # ensure inner() still works after unimport
30     
31     package Baz;
32     use Moose;
33     
34     extends 'Bar';
35     
36     augment foo => sub { 'Baz::foo' }; 
37     augment baz => sub { 'Baz::baz' };       
38
39     # this will actually never run, 
40     # because Bar::bar does not call inner()
41     augment bar => sub { 'Baz::bar' };  
42 }
43
44 my $baz = Baz->new();
45 isa_ok($baz, 'Baz');
46 isa_ok($baz, 'Bar');
47 isa_ok($baz, 'Foo');
48
49 is($baz->foo(), 'Foo::foo(Bar::foo(Baz::foo))', '... got the right value from &foo');
50 is($baz->bar(), 'Foo::bar(Bar::bar)', '... got the right value from &bar');
51 is($baz->baz(), 'Foo::baz(Baz::baz)', '... got the right value from &baz');
52
53 my $bar = Bar->new();
54 isa_ok($bar, 'Bar');
55 isa_ok($bar, 'Foo');
56
57 is($bar->foo(), 'Foo::foo(Bar::foo())', '... got the right value from &foo');
58 is($bar->bar(), 'Foo::bar(Bar::bar)', '... got the right value from &bar');
59 is($bar->baz(), 'Foo::baz()', '... got the right value from &baz');
60
61 my $foo = Foo->new();
62 isa_ok($foo, 'Foo');
63
64 is($foo->foo(), 'Foo::foo()', '... got the right value from &foo');
65 is($foo->bar(), 'Foo::bar()', '... got the right value from &bar');
66 is($foo->baz(), 'Foo::baz()', '... got the right value from &baz');
67
68 # some error cases
69
70 {
71     package Bling;
72     use Moose;
73     
74     sub bling { 'Bling::bling' }
75     
76     package Bling::Bling;
77     use Moose;
78     
79     extends 'Bling';
80     
81     sub bling { 'Bling::bling' }    
82     
83     ::dies_ok {
84         augment 'bling' => sub {};
85     } '... cannot augment a method which has a local equivalent';
86     
87 }
88