Commit | Line | Data |
d79e62fd |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
9c429218 |
6 | use Test::More tests => 24; |
d79e62fd |
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; |
d79e62fd |
30 | use Moose::Role; |
31 | |
32 | package Molecule::Organic; |
d79e62fd |
33 | use Moose::Role; |
34 | |
35 | with 'Molecule'; |
36 | excludes 'Molecule::Inorganic'; |
37 | |
38 | package Molecule::Inorganic; |
d79e62fd |
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 | |
9c429218 |
50 | =pod |
51 | |
52 | Check some basic conflicts when combining |
53 | the roles into the same class |
54 | |
55 | =cut |
56 | |
d79e62fd |
57 | { |
58 | package My::Test1; |
d79e62fd |
59 | use Moose; |
60 | |
61 | ::lives_ok { |
62 | with 'Molecule::Organic'; |
63 | } '... adding the role (w/ excluded roles) okay'; |
64 | |
65 | package My::Test2; |
d79e62fd |
66 | use Moose; |
67 | |
68 | ::throws_ok { |
69 | with 'Molecule::Organic', 'Molecule::Inorganic'; |
68efb014 |
70 | } qr/Conflict detected: .+ excludes role \'Molecule::Inorganic\'/, |
d79e62fd |
71 | '... adding the role w/ excluded role conflict dies okay'; |
72 | |
73 | package My::Test3; |
d79e62fd |
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'); |
9c429218 |
87 | ok(My::Test1->does('Molecule'), '... My::Test1 does Molecule'); |
d79e62fd |
88 | ok(My::Test1->meta->excludes_role('Molecule::Inorganic'), '... My::Test1 excludes Molecule::Organic'); |
9c429218 |
89 | |
d79e62fd |
90 | ok(!My::Test2->does('Molecule::Organic'), '... ! My::Test2 does Molecule::Organic'); |
91 | ok(!My::Test2->does('Molecule::Inorganic'), '... ! My::Test2 does Molecule::Inorganic'); |
9c429218 |
92 | |
d79e62fd |
93 | ok(My::Test3->does('Molecule::Organic'), '... My::Test3 does Molecule::Organic'); |
9c429218 |
94 | ok(My::Test3->does('Molecule'), '... My::Test1 does Molecule'); |
d79e62fd |
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 | |
9c429218 |
98 | =pod |
d79e62fd |
99 | |
9c429218 |
100 | Check some basic conflicts when combining |
101 | the roles into the a superclass |
d79e62fd |
102 | |
9c429218 |
103 | =cut |
d79e62fd |
104 | |
9c429218 |
105 | { |
106 | package Methane; |
9c429218 |
107 | use Moose; |
108 | |
109 | with 'Molecule::Organic'; |
110 | |
111 | package My::Test4; |
9c429218 |
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 | } |
d79e62fd |
121 | |
9c429218 |
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'); |
d79e62fd |
128 | |