added Test::Exception prereq, replaced eval's in tests
[gitmo/MooseX-Role-Parameterized.git] / t / 150-composite-role-application.t
1 #!/usr/bin/env perl
2 use strict;
3 use warnings;
4
5 use Test::More tests => 9;
6 use Test::Exception;
7
8 do {
9     package MyCompositeRoleA;
10     use MooseX::Role::Parameterized;
11
12     parameter attribute => (
13         isa => 'Str',
14         required => 1,
15     );
16
17     role {
18         my $p = shift;
19
20         has $p->attribute => (
21             is => 'Int',
22         );
23     };
24 };
25
26 do {
27     package MyCompositeRoleB;
28     use MooseX::Role::Parameterized;
29
30     parameter accessor => (
31         isa => 'Str',
32         required => 1,
33     );
34
35     role {
36         my $p = shift;
37
38         has $p->accessor => (
39             is => 'rw',
40             isa => 'Int',
41         );
42     };
43 };
44
45 do {
46     package MyDoubleConsumer;
47     use Moose;
48     with MyCompositeRoleA => { attribute => 'foo' },
49          MyCompositeRoleB => { accessor  => 'bar' };
50 };
51
52 lives_ok {
53     ok(MyDoubleConsumer->can('foo'), 'first role in composite applied successfully');
54     ok(MyDoubleConsumer->can('bar'), 'second role in composite applied successfully');
55 } 'testing composite roles lived';
56
57 do {
58     package MyExtendingRole;
59     use MooseX::Role::Parameterized;
60
61     parameter foo => (
62         isa => 'Int',
63     );
64
65     role {
66         my $p = shift;
67
68         with 'MyCompositeRoleA', { attribute => 'bar' };
69
70         has foo => (
71             is => 'rw',
72             default => sub { $p->foo },
73         );
74     };
75 };
76
77 do {
78     package MyExtendedConsumer;
79     use Moose;
80     with MyCompositeRoleA => { attribute => 'bar' },
81          MyExtendingRole  => { foo => 23 };
82 };
83
84 lives_ok {
85     ok(MyExtendedConsumer->can('bar'), 'role composed through other role applied successfully');
86     is(MyExtendedConsumer->new->foo, 23, 'role composing other role applied successfully');
87 } 'testing role through application through other role lived';
88
89 do {
90     package MyRoleProxy;
91     use MooseX::Role::Parameterized;
92
93     parameter rolename   => (isa => "Str");
94     parameter roleparams => (isa => "HashRef");
95
96     role {
97         my $p = shift;
98
99         with $p->rolename, $p->roleparams;
100     };
101 };
102
103 do {
104     package MyProxyConsumer;
105     use Moose;
106     with(
107         MyRoleProxy => { 
108             rolename   => 'MyCompositeRoleA',
109             roleparams => { attribute => 'baz' },
110         },
111         MyCompositeRoleB => {
112             accessor => 'qux',
113         },
114     );
115 };
116
117 lives_ok {
118     ok(MyProxyConsumer->can('baz'), 'proxied role got applied successfully');
119     ok(MyProxyConsumer->can('qux'), 'other role besides proxied one got applied successfully');
120 } 'testing proxied roles lived';
121