Require Dist::Zilla 4.200016+
[gitmo/Moose.git] / t / roles / role_exclusion.t
CommitLineData
d79e62fd 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
a28e50e4 6use Test::More;
b10dde3a 7use Test::Fatal;
d79e62fd 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
b10dde3a 56 ::is( ::exception {
d79e62fd 57 with 'Molecule::Organic';
b10dde3a 58 }, undef, '... adding the role (w/ excluded roles) okay' );
d79e62fd 59
60 package My::Test2;
d79e62fd 61 use Moose;
d03bd989 62
b10dde3a 63 ::like( ::exception {
d79e62fd 64 with 'Molecule::Organic', 'Molecule::Inorganic';
b10dde3a 65 }, qr/Conflict detected: Role Molecule::Organic excludes role 'Molecule::Inorganic'/, '... adding the role w/ excluded role conflict dies okay' );
d03bd989 66
d79e62fd 67 package My::Test3;
d79e62fd 68 use Moose;
d03bd989 69
b10dde3a 70 ::is( ::exception {
d79e62fd 71 with 'Molecule::Organic';
b10dde3a 72 }, undef, '... adding the role (w/ excluded roles) okay' );
d03bd989 73
b10dde3a 74 ::like( ::exception {
d79e62fd 75 with 'Molecule::Inorganic';
b10dde3a 76 }, qr/Conflict detected: My::Test3 excludes role 'Molecule::Inorganic'/, '... adding the role w/ excluded role conflict dies okay' );
d79e62fd 77}
78
79ok(My::Test1->does('Molecule::Organic'), '... My::Test1 does Molecule::Organic');
9c429218 80ok(My::Test1->does('Molecule'), '... My::Test1 does Molecule');
d79e62fd 81ok(My::Test1->meta->excludes_role('Molecule::Inorganic'), '... My::Test1 excludes Molecule::Organic');
9c429218 82
d79e62fd 83ok(!My::Test2->does('Molecule::Organic'), '... ! My::Test2 does Molecule::Organic');
84ok(!My::Test2->does('Molecule::Inorganic'), '... ! My::Test2 does Molecule::Inorganic');
9c429218 85
d79e62fd 86ok(My::Test3->does('Molecule::Organic'), '... My::Test3 does Molecule::Organic');
9c429218 87ok(My::Test3->does('Molecule'), '... My::Test1 does Molecule');
d79e62fd 88ok(My::Test3->meta->excludes_role('Molecule::Inorganic'), '... My::Test3 excludes Molecule::Organic');
89ok(!My::Test3->does('Molecule::Inorganic'), '... ! My::Test3 does Molecule::Inorganic');
90
9c429218 91=pod
d79e62fd 92
d03bd989 93Check some basic conflicts when combining
9c429218 94the roles into the a superclass
d79e62fd 95
9c429218 96=cut
d79e62fd 97
9c429218 98{
99 package Methane;
9c429218 100 use Moose;
d03bd989 101
9c429218 102 with 'Molecule::Organic';
d03bd989 103
9c429218 104 package My::Test4;
9c429218 105 use Moose;
d03bd989 106
107 extends 'Methane';
108
b10dde3a 109 ::like( ::exception {
d03bd989 110 with 'Molecule::Inorganic';
b10dde3a 111 }, qr/Conflict detected: My::Test4 excludes role \'Molecule::Inorganic\'/, '... cannot add exculded role into class which extends Methane' );
9c429218 112}
d79e62fd 113
9c429218 114ok(Methane->does('Molecule::Organic'), '... Methane does Molecule::Organic');
115ok(My::Test4->isa('Methane'), '... My::Test4 isa Methane');
116ok(My::Test4->does('Molecule::Organic'), '... My::Test4 does Molecule::Organic');
117ok(My::Test4->meta->does_role('Molecule::Organic'), '... My::Test4 meat does_role Molecule::Organic');
118ok(My::Test4->meta->excludes_role('Molecule::Inorganic'), '... My::Test4 meta excludes Molecule::Organic');
119ok(!My::Test4->does('Molecule::Inorganic'), '... My::Test4 does Molecule::Inorganic');
d79e62fd 120
a28e50e4 121done_testing;