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