summaryrefslogtreecommitdiff
path: root/t/roles/role_exclusion.t
blob: d6cb80a9f772db550ab8a0f4362121a1b056ee6b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
use strict;
use warnings;

use Test::More;
use Test::Fatal;

=pod

The idea and examples for this feature are taken
from the Fortress spec.

http://research.sun.com/projects/plrg/fortress0903.pdf

trait OrganicMolecule extends Molecule
    excludes { InorganicMolecule }
end
trait InorganicMolecule extends Molecule end

=cut

{
    package Molecule;
    use Moose::Role;

    package Molecule::Organic;
    use Moose::Role;

    with 'Molecule';
    excludes 'Molecule::Inorganic';

    package Molecule::Inorganic;
    use Moose::Role;

    with 'Molecule';
}

ok(Molecule::Organic->meta->excludes_role('Molecule::Inorganic'), '... Molecule::Organic exludes Molecule::Inorganic');
is_deeply(
   [ Molecule::Organic->meta->get_excluded_roles_list() ],
   [ 'Molecule::Inorganic' ],
   '... Molecule::Organic exludes Molecule::Inorganic');

=pod

Check some basic conflicts when combining
the roles into the same class

=cut

{
    package My::Test1;
    use Moose;

    ::is( ::exception {
        with 'Molecule::Organic';
    }, undef, '... adding the role (w/ excluded roles) okay' );

    package My::Test2;
    use Moose;

    ::like( ::exception {
        with 'Molecule::Organic', 'Molecule::Inorganic';
    }, qr/Conflict detected: Role Molecule::Organic excludes role 'Molecule::Inorganic'/, '... adding the role w/ excluded role conflict dies okay' );

    package My::Test3;
    use Moose;

    ::is( ::exception {
        with 'Molecule::Organic';
    }, undef, '... adding the role (w/ excluded roles) okay' );

    ::like( ::exception {
        with 'Molecule::Inorganic';
    }, qr/Conflict detected: My::Test3 excludes role 'Molecule::Inorganic'/, '... adding the role w/ excluded role conflict dies okay' );
}

ok(My::Test1->does('Molecule::Organic'), '... My::Test1 does Molecule::Organic');
ok(My::Test1->does('Molecule'), '... My::Test1 does Molecule');
ok(My::Test1->meta->excludes_role('Molecule::Inorganic'), '... My::Test1 excludes Molecule::Organic');

ok(!My::Test2->does('Molecule::Organic'), '... ! My::Test2 does Molecule::Organic');
ok(!My::Test2->does('Molecule::Inorganic'), '... ! My::Test2 does Molecule::Inorganic');

ok(My::Test3->does('Molecule::Organic'), '... My::Test3 does Molecule::Organic');
ok(My::Test3->does('Molecule'), '... My::Test1 does Molecule');
ok(My::Test3->meta->excludes_role('Molecule::Inorganic'), '... My::Test3 excludes Molecule::Organic');
ok(!My::Test3->does('Molecule::Inorganic'), '... ! My::Test3 does Molecule::Inorganic');

=pod

Check some basic conflicts when combining
the roles into the a superclass

=cut

{
    package Methane;
    use Moose;

    with 'Molecule::Organic';

    package My::Test4;
    use Moose;

    extends 'Methane';

    ::like( ::exception {
        with 'Molecule::Inorganic';
    }, qr/Conflict detected: My::Test4 excludes role \'Molecule::Inorganic\'/, '... cannot add exculded role into class which extends Methane' );
}

ok(Methane->does('Molecule::Organic'), '... Methane does Molecule::Organic');
ok(My::Test4->isa('Methane'), '... My::Test4 isa Methane');
ok(My::Test4->does('Molecule::Organic'), '... My::Test4 does Molecule::Organic');
ok(My::Test4->meta->does_role('Molecule::Organic'), '... My::Test4 meat does_role Molecule::Organic');
ok(My::Test4->meta->excludes_role('Molecule::Inorganic'), '... My::Test4 meta excludes Molecule::Organic');
ok(!My::Test4->does('Molecule::Inorganic'), '... My::Test4 does Molecule::Inorganic');

done_testing;