Commit | Line | Data |
d79e62fd |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
a28e50e4 |
6 | use Test::More; |
d79e62fd |
7 | use Test::Exception; |
8 | |
d79e62fd |
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 | |
d03bd989 |
16 | trait OrganicMolecule extends Molecule |
17 | excludes { InorganicMolecule } |
18 | end |
19 | trait InorganicMolecule extends Molecule end |
d79e62fd |
20 | |
21 | =cut |
22 | |
23 | { |
24 | package Molecule; |
d79e62fd |
25 | use Moose::Role; |
26 | |
27 | package Molecule::Organic; |
d79e62fd |
28 | use Moose::Role; |
d03bd989 |
29 | |
d79e62fd |
30 | with 'Molecule'; |
31 | excludes 'Molecule::Inorganic'; |
d03bd989 |
32 | |
d79e62fd |
33 | package Molecule::Inorganic; |
d03bd989 |
34 | use Moose::Role; |
35 | |
36 | with 'Molecule'; |
d79e62fd |
37 | } |
38 | |
39 | ok(Molecule::Organic->meta->excludes_role('Molecule::Inorganic'), '... Molecule::Organic exludes Molecule::Inorganic'); |
40 | is_deeply( |
d03bd989 |
41 | [ Molecule::Organic->meta->get_excluded_roles_list() ], |
d79e62fd |
42 | [ 'Molecule::Inorganic' ], |
43 | '... Molecule::Organic exludes Molecule::Inorganic'); |
44 | |
9c429218 |
45 | =pod |
46 | |
d03bd989 |
47 | Check some basic conflicts when combining |
9c429218 |
48 | the roles into the same class |
49 | |
50 | =cut |
51 | |
d79e62fd |
52 | { |
53 | package My::Test1; |
d79e62fd |
54 | use Moose; |
d03bd989 |
55 | |
d79e62fd |
56 | ::lives_ok { |
57 | with 'Molecule::Organic'; |
58 | } '... adding the role (w/ excluded roles) okay'; |
59 | |
60 | package My::Test2; |
d79e62fd |
61 | use Moose; |
d03bd989 |
62 | |
d79e62fd |
63 | ::throws_ok { |
64 | with 'Molecule::Organic', 'Molecule::Inorganic'; |
d03bd989 |
65 | } qr/Conflict detected: Role Molecule::Organic excludes role 'Molecule::Inorganic'/, |
66 | '... adding the role w/ excluded role conflict dies okay'; |
67 | |
d79e62fd |
68 | package My::Test3; |
d79e62fd |
69 | use Moose; |
d03bd989 |
70 | |
d79e62fd |
71 | ::lives_ok { |
72 | with 'Molecule::Organic'; |
d03bd989 |
73 | } '... adding the role (w/ excluded roles) okay'; |
74 | |
d79e62fd |
75 | ::throws_ok { |
76 | with 'Molecule::Inorganic'; |
d03bd989 |
77 | } qr/Conflict detected: My::Test3 excludes role 'Molecule::Inorganic'/, |
78 | '... adding the role w/ excluded role conflict dies okay'; |
d79e62fd |
79 | } |
80 | |
81 | ok(My::Test1->does('Molecule::Organic'), '... My::Test1 does Molecule::Organic'); |
9c429218 |
82 | ok(My::Test1->does('Molecule'), '... My::Test1 does Molecule'); |
d79e62fd |
83 | ok(My::Test1->meta->excludes_role('Molecule::Inorganic'), '... My::Test1 excludes Molecule::Organic'); |
9c429218 |
84 | |
d79e62fd |
85 | ok(!My::Test2->does('Molecule::Organic'), '... ! My::Test2 does Molecule::Organic'); |
86 | ok(!My::Test2->does('Molecule::Inorganic'), '... ! My::Test2 does Molecule::Inorganic'); |
9c429218 |
87 | |
d79e62fd |
88 | ok(My::Test3->does('Molecule::Organic'), '... My::Test3 does Molecule::Organic'); |
9c429218 |
89 | ok(My::Test3->does('Molecule'), '... My::Test1 does Molecule'); |
d79e62fd |
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 | |
9c429218 |
93 | =pod |
d79e62fd |
94 | |
d03bd989 |
95 | Check some basic conflicts when combining |
9c429218 |
96 | the roles into the a superclass |
d79e62fd |
97 | |
9c429218 |
98 | =cut |
d79e62fd |
99 | |
9c429218 |
100 | { |
101 | package Methane; |
9c429218 |
102 | use Moose; |
d03bd989 |
103 | |
9c429218 |
104 | with 'Molecule::Organic'; |
d03bd989 |
105 | |
9c429218 |
106 | package My::Test4; |
9c429218 |
107 | use Moose; |
d03bd989 |
108 | |
109 | extends 'Methane'; |
110 | |
9c429218 |
111 | ::throws_ok { |
d03bd989 |
112 | with 'Molecule::Inorganic'; |
9c429218 |
113 | } qr/Conflict detected: My::Test4 excludes role \'Molecule::Inorganic\'/, |
114 | '... cannot add exculded role into class which extends Methane'; |
115 | } |
d79e62fd |
116 | |
9c429218 |
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'); |
d79e62fd |
123 | |
a28e50e4 |
124 | done_testing; |