remove trailing whitespace
[gitmo/Moose.git] / t / 030_roles / 006_role_exclusion.t
CommitLineData
d79e62fd 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
7ff56534 6use Test::More tests => 22;
d79e62fd 7use Test::Exception;
8
d79e62fd 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
d03bd989 16trait OrganicMolecule extends Molecule
17 excludes { InorganicMolecule }
18end
19trait 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
39ok(Molecule::Organic->meta->excludes_role('Molecule::Inorganic'), '... Molecule::Organic exludes Molecule::Inorganic');
40is_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 47Check some basic conflicts when combining
9c429218 48the 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
81ok(My::Test1->does('Molecule::Organic'), '... My::Test1 does Molecule::Organic');
9c429218 82ok(My::Test1->does('Molecule'), '... My::Test1 does Molecule');
d79e62fd 83ok(My::Test1->meta->excludes_role('Molecule::Inorganic'), '... My::Test1 excludes Molecule::Organic');
9c429218 84
d79e62fd 85ok(!My::Test2->does('Molecule::Organic'), '... ! My::Test2 does Molecule::Organic');
86ok(!My::Test2->does('Molecule::Inorganic'), '... ! My::Test2 does Molecule::Inorganic');
9c429218 87
d79e62fd 88ok(My::Test3->does('Molecule::Organic'), '... My::Test3 does Molecule::Organic');
9c429218 89ok(My::Test3->does('Molecule'), '... My::Test1 does Molecule');
d79e62fd 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
9c429218 93=pod
d79e62fd 94
d03bd989 95Check some basic conflicts when combining
9c429218 96the 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 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');
d79e62fd 123