Perltidy this code a bit.
[gitmo/Moose.git] / t / 030_roles / 006_role_exclusion.t
CommitLineData
d79e62fd 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
9c429218 6use Test::More tests => 24;
d79e62fd 7use Test::Exception;
8
9BEGIN {
10 use_ok('Moose');
11 use_ok('Moose::Role');
12}
13
14=pod
15
16The idea and examples for this feature are taken
17from the Fortress spec.
18
19http://research.sun.com/projects/plrg/fortress0903.pdf
20
21trait OrganicMolecule extends Molecule
22 excludes { InorganicMolecule }
23end
24trait 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
44ok(Molecule::Organic->meta->excludes_role('Molecule::Inorganic'), '... Molecule::Organic exludes Molecule::Inorganic');
45is_deeply(
46 [ Molecule::Organic->meta->get_excluded_roles_list() ],
47 [ 'Molecule::Inorganic' ],
48 '... Molecule::Organic exludes Molecule::Inorganic');
49
9c429218 50=pod
51
52Check some basic conflicts when combining
53the 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
86ok(My::Test1->does('Molecule::Organic'), '... My::Test1 does Molecule::Organic');
9c429218 87ok(My::Test1->does('Molecule'), '... My::Test1 does Molecule');
d79e62fd 88ok(My::Test1->meta->excludes_role('Molecule::Inorganic'), '... My::Test1 excludes Molecule::Organic');
9c429218 89
d79e62fd 90ok(!My::Test2->does('Molecule::Organic'), '... ! My::Test2 does Molecule::Organic');
91ok(!My::Test2->does('Molecule::Inorganic'), '... ! My::Test2 does Molecule::Inorganic');
9c429218 92
d79e62fd 93ok(My::Test3->does('Molecule::Organic'), '... My::Test3 does Molecule::Organic');
9c429218 94ok(My::Test3->does('Molecule'), '... My::Test1 does Molecule');
d79e62fd 95ok(My::Test3->meta->excludes_role('Molecule::Inorganic'), '... My::Test3 excludes Molecule::Organic');
96ok(!My::Test3->does('Molecule::Inorganic'), '... ! My::Test3 does Molecule::Inorganic');
97
9c429218 98=pod
d79e62fd 99
9c429218 100Check some basic conflicts when combining
101the 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 122ok(Methane->does('Molecule::Organic'), '... Methane does Molecule::Organic');
123ok(My::Test4->isa('Methane'), '... My::Test4 isa Methane');
124ok(My::Test4->does('Molecule::Organic'), '... My::Test4 does Molecule::Organic');
125ok(My::Test4->meta->does_role('Molecule::Organic'), '... My::Test4 meat does_role Molecule::Organic');
126ok(My::Test4->meta->excludes_role('Molecule::Inorganic'), '... My::Test4 meta excludes Molecule::Organic');
127ok(!My::Test4->does('Molecule::Inorganic'), '... My::Test4 does Molecule::Inorganic');
d79e62fd 128