complete re-organization of the test suite
[gitmo/Moose.git] / t / 030_roles / 006_role_exclusion.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 24;
7 use Test::Exception;
8
9 BEGIN {
10     use_ok('Moose');
11     use_ok('Moose::Role');    
12 }
13
14 =pod
15
16 The idea and examples for this feature are taken
17 from the Fortress spec.
18
19 http://research.sun.com/projects/plrg/fortress0903.pdf
20
21 trait OrganicMolecule extends Molecule 
22     excludes { InorganicMolecule } 
23 end 
24 trait InorganicMolecule extends Molecule end 
25
26 =cut
27
28 {
29     package Molecule;
30     use Moose::Role;
31
32     package Molecule::Organic;
33     use Moose::Role;
34     
35     with 'Molecule';
36     excludes 'Molecule::Inorganic';
37     
38     package Molecule::Inorganic;
39     use Moose::Role;     
40     
41     with 'Molecule';       
42 }
43
44 ok(Molecule::Organic->meta->excludes_role('Molecule::Inorganic'), '... Molecule::Organic exludes Molecule::Inorganic');
45 is_deeply(
46    [ Molecule::Organic->meta->get_excluded_roles_list() ], 
47    [ 'Molecule::Inorganic' ],
48    '... Molecule::Organic exludes Molecule::Inorganic');
49
50 =pod
51
52 Check some basic conflicts when combining  
53 the roles into the same class
54
55 =cut
56
57 {
58     package My::Test1;
59     use Moose;
60     
61     ::lives_ok {
62         with 'Molecule::Organic';
63     } '... adding the role (w/ excluded roles) okay';
64
65     package My::Test2;
66     use Moose;
67     
68     ::throws_ok {
69         with 'Molecule::Organic', 'Molecule::Inorganic';
70     } qr/Conflict detected: .+ excludes role \'Molecule::Inorganic\'/, 
71     '... adding the role w/ excluded role conflict dies okay';    
72     
73     package My::Test3;
74     use Moose;
75     
76     ::lives_ok {
77         with 'Molecule::Organic';
78     } '... adding the role (w/ excluded roles) okay';   
79     
80     ::throws_ok {
81         with 'Molecule::Inorganic';
82     } qr/Conflict detected: My::Test3 excludes role 'Molecule::Inorganic'/, 
83     '... adding the role w/ excluded role conflict dies okay'; 
84 }
85
86 ok(My::Test1->does('Molecule::Organic'), '... My::Test1 does Molecule::Organic');
87 ok(My::Test1->does('Molecule'), '... My::Test1 does Molecule');
88 ok(My::Test1->meta->excludes_role('Molecule::Inorganic'), '... My::Test1 excludes Molecule::Organic');
89
90 ok(!My::Test2->does('Molecule::Organic'), '... ! My::Test2 does Molecule::Organic');
91 ok(!My::Test2->does('Molecule::Inorganic'), '... ! My::Test2 does Molecule::Inorganic');
92
93 ok(My::Test3->does('Molecule::Organic'), '... My::Test3 does Molecule::Organic');
94 ok(My::Test3->does('Molecule'), '... My::Test1 does Molecule');
95 ok(My::Test3->meta->excludes_role('Molecule::Inorganic'), '... My::Test3 excludes Molecule::Organic');
96 ok(!My::Test3->does('Molecule::Inorganic'), '... ! My::Test3 does Molecule::Inorganic');
97
98 =pod
99
100 Check some basic conflicts when combining  
101 the roles into the a superclass
102
103 =cut
104
105 {
106     package Methane;
107     use Moose;
108     
109     with 'Molecule::Organic';
110     
111     package My::Test4;
112     use Moose;
113     
114     extends 'Methane';    
115     
116     ::throws_ok {
117         with 'Molecule::Inorganic';    
118     } qr/Conflict detected: My::Test4 excludes role \'Molecule::Inorganic\'/,
119     '... cannot add exculded role into class which extends Methane';
120 }
121
122 ok(Methane->does('Molecule::Organic'), '... Methane does Molecule::Organic');
123 ok(My::Test4->isa('Methane'), '... My::Test4 isa Methane');
124 ok(My::Test4->does('Molecule::Organic'), '... My::Test4 does Molecule::Organic');
125 ok(My::Test4->meta->does_role('Molecule::Organic'), '... My::Test4 meat does_role Molecule::Organic');
126 ok(My::Test4->meta->excludes_role('Molecule::Inorganic'), '... My::Test4 meta excludes Molecule::Organic');
127 ok(!My::Test4->does('Molecule::Inorganic'), '... My::Test4 does Molecule::Inorganic');
128