File: 140_trait_runtime_class.t

package info (click to toggle)
libclass-trait-perl 0.31-1
  • links: PTS
  • area: main
  • in suites: squeeze, wheezy
  • size: 364 kB
  • ctags: 156
  • sloc: perl: 2,467; makefile: 46
file content (172 lines) | stat: -rw-r--r-- 4,786 bytes parent folder | download | duplicates (4)
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/perl

use strict;
use warnings;
no warnings 'redefine';

#use Test::More tests => 33;
use Test::More qw/no_plan/;
use Class::Trait;

sub clean_inc {
    my @packages = qw(
      Circle
      Extra::TSpouse
      Foo
      Polygamy
      TBomb
      TDisallowed
      TSpouse
      TestTraits
    );

    foreach my $package (@packages) {
        no strict 'refs';
        clean_package( $package, keys %{"${package}::"} );
    }
    my @includes = map { s{::}{/}g; "$_.pm" } @packages;
    delete @INC{@includes};

    eval <<'    END_FOO';
        package Foo;
        sub new { bless {}, shift }
        sub name    { 'Foo' }
        sub explode { 'Foo explodes' }
    }
    END_FOO
}

sub clean_package {
    my ( $package, @globs ) = @_;
    no strict 'refs';
    foreach my $glob (@globs) {
        undef *{"${package}::$glob"};
    }
    Class::Trait::_clear_all_caches();
    Class::Trait::_clear_does_cache();    # internal testing hook
    no warnings 'once';
    undef $Foo::TRAITS;
}

local $SIG{__WARN__} = sub {
    my $message = shift;
    return if $message =~ /Too late to run INIT block/;
    warn $message;
};

BEGIN {
    chdir 't' if -d 't';
    unshift @INC => ( '../lib', 'test_lib' );
}

#
# appling simple runtime traits to instances
#

clean_inc();
Class::Trait->apply(Foo => 'TSpouse');
my $foo = Foo->new;
isa_ok $foo, 'Foo', '... and the object it returns';
can_ok 'Foo', 'does';
ok +Foo->does('TSpouse'), '... and it should be able to do the new trait';
can_ok $foo, 'name';
is $foo->name, 'Foo', '... original methods should still be available';
can_ok $foo, 'explode';
is $foo->explode, 'Spouse explodes',
  '... but corresponding trait methods should override them';
can_ok $foo, 'fuse';
is $foo->fuse, 'Spouse fuse',
  '... and the new trait methods should be available';

my $foo2 = Foo->new;
is $foo2->explode, 'Spouse explodes',
  'Different instances of classes should share runtime traits applied to classes';

#
# conflicting runtime traits to instances
#

clean_inc();
can_ok 'Class::Trait', 'apply';
eval { Class::Trait->apply( 'Foo', 'TSpouse', 'TBomb' ) };
ok $@,   'Trying to apply conflicting traits at runtime should fail';
like $@, qr/Package (?:\w|::)+ has conflicting methods \(.*\)/,
  '... with an appropriate error message';


# let's resolve those conflicts

clean_inc();
Class::Trait->apply(
    'Foo',
    TSpouse => { exclude => 'fuse' },
    TBomb   => { exclude => 'explode' },
);
$foo = Foo->new;
ok $foo->does('TSpouse'), '... and it should be able to do the first trait';
ok $foo->does('TBomb'),   '... and it should be able to do the second trait';
can_ok $foo, 'name';
is $foo->name, 'Foo', '... original methods should still be available';
can_ok $foo, 'explode';
is $foo->explode, 'Spouse explodes',
  '... but corresponding trait methods should override them';
can_ok $foo, 'fuse';
is $foo->fuse, 'Bomb fuse', '... and the new trait methods should be available';


#
# let's apply the traits separately
#

clean_inc();
Class::Trait->apply( Foo => 'TSpouse' );
$foo = Foo->new;
ok !$foo->does('TBomb'),
  'Trait information should not persist innappropriately';

Class::Trait->apply( Foo => 'TBomb' );
ok $foo->does('TBomb'), '... but we should have that info when it is available';
ok $foo->does('TSpouse'),
  '... and it should still report the other traits it can do';
can_ok $foo, 'name';
is $foo->name, 'Foo', '... original methods should still be available';
can_ok $foo, 'explode';
is $foo->explode, 'Bomb explodes',
  '... but later trait methods should override earlier ones';
can_ok $foo, 'fuse';
is $foo->fuse, 'Bomb fuse', '... and the new trait methods should be available';
my @does = sort $foo->does;

is_deeply \@does, [qw/TBomb TSpouse/],
    '... and it should be able to report all traits it can do';

#
# let's apply runtime composite traits with requirements
#

clean_inc();
eval { Class::Trait->apply(Foo => 'Extra::TSpouse') };
ok $@,   'Trying to apply a runtime trait with unmet requirements should fail';
like $@, qr/Requirement \(alimony\) for Extra::TSpouse not in Foo/,
  '... with an appropriate error message';

clean_inc();
{
    no warnings 'once';
    *Foo::alimony = sub { 'Foo alimony' };
}
eval { Class::Trait->apply(Foo => 'Extra::TSpouse') };
ok $@,   'Trying to apply a runtime trait with unmet requirements should fail';
like $@, qr/Requirement \(lawyer\) for Extra::TSpouse not in Foo/,
  '... with an appropriate error message';

clean_inc();
{
    no warnings 'once';
    *Foo::alimony = sub { 'Foo alimony' };
    *Foo::lawyer = sub { 'Foo lawyer' };
}
ok +Class::Trait->apply(Foo => 'Extra::TSpouse'),
  'Satisfying all requirements of runtime traits should succeed';
is $foo->explode, 'Extra spouse explodes',
  '... and the correct method should be overridden';