Add a DB::sub test to xt/
[gitmo/Mouse.git] / t / 030_roles / 010_run_time_role_composition.t
CommitLineData
67199842 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use Test::More tests => 27;
7
8use Scalar::Util qw(blessed);
9
10
11
12=pod
13
14This test can be used as a basis for the runtime role composition.
6cfa1e5e 15Apparently it is not as simple as just making an anon class. One of
67199842 16the problems is the way that anon classes are DESTROY-ed, which is
17not very compatible with how instances are dealt with.
18
19=cut
20
21{
22 package Bark;
23 use Mouse::Role;
24
25 sub talk { 'woof' }
26
27 package Sleeper;
28 use Mouse::Role;
29
30 sub sleep { 'snore' }
31 sub talk { 'zzz' }
32
33 package My::Class;
34 use Mouse;
35
36 sub sleep { 'nite-nite' }
37}
38
39my $obj = My::Class->new;
6cfa1e5e 40isa_ok($obj, 'My::Class');
41
67199842 42my $obj2 = My::Class->new;
6cfa1e5e 43isa_ok($obj2, 'My::Class');
67199842 44
45{
46 ok(!$obj->can( 'talk' ), "... the role is not composed yet");
6cfa1e5e 47
67199842 48 ok(!$obj->does('Bark'), '... we do not do any roles yet');
6cfa1e5e 49
67199842 50 Bark->meta->apply($obj);
51
52 ok($obj->does('Bark'), '... we now do the Bark role');
6cfa1e5e 53 ok(!My::Class->does('Bark'), '... the class does not do the Bark role');
67199842 54
55 isa_ok($obj, 'My::Class');
56 isnt(blessed($obj), 'My::Class', '... but it is no longer blessed into My::Class');
57
58 ok(!My::Class->can('talk'), "... the role is not composed at the class level");
59 ok($obj->can('talk'), "... the role is now composed at the object level");
6cfa1e5e 60
67199842 61 is($obj->talk, 'woof', '... got the right return value for the newly composed method');
62}
63
64{
65 ok(!$obj2->does('Bark'), '... we do not do any roles yet');
6cfa1e5e 66
67199842 67 Bark->meta->apply($obj2);
6cfa1e5e 68
67199842 69 ok($obj2->does('Bark'), '... we now do the Bark role');
70 is(blessed($obj), blessed($obj2), '... they share the same anon-class/role thing');
71}
72
73{
74 is($obj->sleep, 'nite-nite', '... the original method responds as expected');
75
76 ok(!$obj->does('Sleeper'), '... we do not do the Sleeper role');
77
78 Sleeper->meta->apply($obj);
79
80 ok($obj->does('Bark'), '... we still do the Bark role');
6cfa1e5e 81 ok($obj->does('Sleeper'), '... we now do the Sleeper role too');
82
83 ok(!My::Class->does('Sleeper'), '... the class does not do the Sleeper role');
84
85 isnt(blessed($obj), blessed($obj2), '... they no longer share the same anon-class/role thing');
86
67199842 87 isa_ok($obj, 'My::Class');
88
89 is(My::Class->sleep, 'nite-nite', '... the original method still responds as expected');
90
91 is($obj->sleep, 'snore', '... got the right return value for the newly composed method');
6cfa1e5e 92 is($obj->talk, 'zzz', '... got the right return value for the newly composed method');
67199842 93}
94
95{
96 ok(!$obj2->does('Sleeper'), '... we do not do any roles yet');
6cfa1e5e 97
67199842 98 Sleeper->meta->apply($obj2);
6cfa1e5e 99
67199842 100 ok($obj2->does('Sleeper'), '... we now do the Bark role');
101 is(blessed($obj), blessed($obj2), '... they share the same anon-class/role thing again');
102}
103
104
105
106