error_tests
[gitmo/Moose.git] / t / 042_apply_role.t
CommitLineData
78cd1d3b 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
ef333f17 6use Test::More tests => 36;
78cd1d3b 7use Test::Exception;
8
9BEGIN {
10 use_ok('Moose::Role');
11}
12
13{
14 package FooRole;
15 use strict;
16 use warnings;
17 use Moose::Role;
18
19 has 'bar' => (is => 'rw', isa => 'FooClass');
20 has 'baz' => (is => 'ro');
21
22 sub goo { 'FooRole::goo' }
23 sub foo { 'FooRole::foo' }
24
25 override 'boo' => sub { 'FooRole::boo -> ' . super() };
26
27 around 'blau' => sub {
28 my $c = shift;
29 'FooRole::blau -> ' . $c->();
30 };
31
32 package BarClass;
33 use strict;
34 use warnings;
35 use Moose;
36
37 sub boo { 'BarClass::boo' }
38 sub foo { 'BarClass::foo' } # << the role overrides this ...
39
40 package FooClass;
41 use strict;
42 use warnings;
43 use Moose;
44
45 extends 'BarClass';
46 with 'FooRole';
47
48 sub blau { 'FooClass::blau' }
49
50 sub goo { 'FooClass::goo' } # << overrides the one from the role ...
51}
52
53my $foo_class_meta = FooClass->meta;
54isa_ok($foo_class_meta, 'Moose::Meta::Class');
55
ef333f17 56ok($foo_class_meta->does_role('FooRole'), '... the FooClass->meta does_role FooRole');
57ok(!$foo_class_meta->does_role('OtherRole'), '... the FooClass->meta !does_role OtherRole');
58
78cd1d3b 59foreach my $method_name (qw(bar baz foo boo blau goo)) {
60 ok($foo_class_meta->has_method($method_name), '... FooClass has the method ' . $method_name);
61}
62
63foreach my $attr_name (qw(bar baz)) {
64 ok($foo_class_meta->has_attribute($attr_name), '... FooClass has the attribute ' . $attr_name);
65}
66
ef333f17 67can_ok('FooClass', 'does');
68ok(FooClass->does('FooRole'), '... the FooClass does FooRole');
69ok(!FooClass->does('OtherRole'), '... the FooClass does not do OtherRole');
70
78cd1d3b 71my $foo = FooClass->new();
72isa_ok($foo, 'FooClass');
73
ef333f17 74can_ok($foo, 'does');
75ok($foo->does('FooRole'), '... an instance of FooClass does FooRole');
76ok(!$foo->does('OtherRole'), '... and instance of FooClass does not do OtherRole');
77
78cd1d3b 78can_ok($foo, 'bar');
79can_ok($foo, 'baz');
80can_ok($foo, 'foo');
81can_ok($foo, 'boo');
82can_ok($foo, 'goo');
83can_ok($foo, 'blau');
84
85is($foo->foo, 'FooRole::foo', '... got the right value of foo');
86is($foo->goo, 'FooClass::goo', '... got the right value of goo');
87
88ok(!defined($foo->baz), '... $foo->baz is undefined');
89ok(!defined($foo->bar), '... $foo->bar is undefined');
90
91dies_ok {
92 $foo->baz(1)
93} '... baz is a read-only accessor';
94
95dies_ok {
96 $foo->bar(1)
97} '... bar is a read-write accessor with a type constraint';
98
99my $foo2 = FooClass->new();
100isa_ok($foo2, 'FooClass');
101
102lives_ok {
103 $foo->bar($foo2)
104} '... bar is a read-write accessor with a type constraint';
105
106is($foo->bar, $foo2, '... got the right value for bar now');
107
108is($foo->boo, 'FooRole::boo -> BarClass::boo', '... got the right value from ->boo');
109is($foo->blau, 'FooRole::blau -> FooClass::blau', '... got the right value from ->blau');
110