Require Dist::Zilla 4.200016+
[gitmo/Moose.git] / t / basics / inner_and_augment.t
CommitLineData
b6fe348f 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
a28e50e4 6use Test::More;
b10dde3a 7use Test::Fatal;
b6fe348f 8
7ff56534 9
b6fe348f 10{
11 package Foo;
b6fe348f 12 use Moose;
d03bd989 13
159da176 14 sub foo { 'Foo::foo(' . (inner() || '') . ')' }
d03bd989 15 sub bar { 'Foo::bar(' . (inner() || '') . ')' }
16 sub baz { 'Foo::baz(' . (inner() || '') . ')' }
17
b6fe348f 18 package Bar;
b6fe348f 19 use Moose;
d03bd989 20
b6fe348f 21 extends 'Foo';
d03bd989 22
23 augment foo => sub { 'Bar::foo(' . (inner() || '') . ')' };
24 augment bar => sub { 'Bar::bar' };
52c7c330 25
26 no Moose; # ensure inner() still works after unimport
d03bd989 27
b6fe348f 28 package Baz;
b6fe348f 29 use Moose;
d03bd989 30
b6fe348f 31 extends 'Bar';
159da176 32
d03bd989 33 augment foo => sub { 'Baz::foo' };
34 augment baz => sub { 'Baz::baz' };
35
36 # this will actually never run,
159da176 37 # because Bar::bar does not call inner()
d03bd989 38 augment bar => sub { 'Baz::bar' };
b6fe348f 39}
40
41my $baz = Baz->new();
42isa_ok($baz, 'Baz');
43isa_ok($baz, 'Bar');
44isa_ok($baz, 'Foo');
45
46is($baz->foo(), 'Foo::foo(Bar::foo(Baz::foo))', '... got the right value from &foo');
47is($baz->bar(), 'Foo::bar(Bar::bar)', '... got the right value from &bar');
48is($baz->baz(), 'Foo::baz(Baz::baz)', '... got the right value from &baz');
49
159da176 50my $bar = Bar->new();
51isa_ok($bar, 'Bar');
52isa_ok($bar, 'Foo');
53
54is($bar->foo(), 'Foo::foo(Bar::foo())', '... got the right value from &foo');
55is($bar->bar(), 'Foo::bar(Bar::bar)', '... got the right value from &bar');
56is($bar->baz(), 'Foo::baz()', '... got the right value from &baz');
57
58my $foo = Foo->new();
59isa_ok($foo, 'Foo');
60
61is($foo->foo(), 'Foo::foo()', '... got the right value from &foo');
62is($foo->bar(), 'Foo::bar()', '... got the right value from &bar');
63is($foo->baz(), 'Foo::baz()', '... got the right value from &baz');
d05cd563 64
8320d292 65# test saved state when crossing objects
66
67{
68 package X;
69 use Moose;
70 has name => (is => 'rw');
71 sub run {
72 "$_[0]->{name}.X", inner()
73 }
74
75 package Y;
76 use Moose;
77 extends 'X';
78 augment 'run' => sub {
79 "$_[0]->{name}.Y", ($_[1] ? $_[1]->() : ()), inner();
80 };
81
82 package Z;
83 use Moose;
84 extends 'Y';
85 augment 'run' => sub {
86 "$_[0]->{name}.Z"
87 }
88}
89
90is('a.X a.Y b.X b.Y b.Z a.Z',
91 do {
92 my $a = Z->new(name => 'a');
93 my $b = Z->new(name => 'b');
94 join(' ', $a->run(sub { $b->run }))
95 },
96 'State is saved when cross-calling augmented methods on different objects');
97
d05cd563 98# some error cases
99
100{
101 package Bling;
d05cd563 102 use Moose;
d03bd989 103
d05cd563 104 sub bling { 'Bling::bling' }
d03bd989 105
d05cd563 106 package Bling::Bling;
d05cd563 107 use Moose;
d03bd989 108
d05cd563 109 extends 'Bling';
d03bd989 110
111 sub bling { 'Bling::bling' }
112
b10dde3a 113 ::isnt( ::exception {
d05cd563 114 augment 'bling' => sub {};
b10dde3a 115 }, undef, '... cannot augment a method which has a local equivalent' );
d03bd989 116
d05cd563 117}
118
a28e50e4 119done_testing;