Fix some composite role application 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 => 7;
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 => 'rw',
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 TODO: {
53     local $TODO = "role-role application for parameterized roles doesn't work yet";
54     ok(MyDoubleConsumer->can('foo'), 'first role in composite applied successfully');
55     ok(MyDoubleConsumer->can('bar'), 'second role in composite applied successfully');
56 };
57
58 do {
59     package MyExtendingRole;
60     use MooseX::Role::Parameterized;
61
62     parameter foo => (
63         isa => 'Int',
64     );
65
66     role {
67         my $p = shift;
68
69         with 'MyCompositeRoleA', { attribute => 'bar' };
70
71         has foo => (
72             is => 'rw',
73             default => sub { $p->foo },
74         );
75     };
76 };
77
78 do {
79     package MyExtendedConsumer;
80     use Moose;
81     with MyCompositeRoleA => { attribute => 'baz' },
82          MyExtendingRole  => { foo => 23 };
83 };
84
85 TODO: {
86     local $TODO = "role-role application for parameterized roles doesn't work yet";
87     ok(MyExtendedConsumer->can('baz'), 'role composed directly applied successfully');
88     ok(MyExtendedConsumer->can('bar'), 'role composed through other role applied successfully');
89     is(eval { MyExtendedConsumer->new->foo }, 23, 'role composing other role applied successfully');
90 };;
91
92 do {
93     package MyRoleProxy;
94     use MooseX::Role::Parameterized;
95
96     parameter rolename   => (isa => "Str");
97     parameter roleparams => (isa => "HashRef");
98
99     role {
100         my $p = shift;
101
102         with $p->rolename, $p->roleparams;
103     };
104 };
105
106 do {
107     package MyProxyConsumer;
108     use Moose;
109     with(
110         MyRoleProxy => { 
111             rolename   => 'MyCompositeRoleA',
112             roleparams => { attribute => 'baz' },
113         },
114         MyCompositeRoleB => {
115             accessor => 'qux',
116         },
117     );
118 };
119
120 TODO: {
121     local $TODO = "role-role application for parameterized roles doesn't work yet";
122     ok(MyProxyConsumer->can('baz'), 'proxied role got applied successfully');
123     ok(MyProxyConsumer->can('qux'), 'other role besides proxied one got applied successfully');
124 };
125