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