line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
=head1 NAME |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
Test::Role - test that a class or object performs a role |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
=head1 SYNOPSIS |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
use Test::Role; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
use Foo; |
10
|
|
|
|
|
|
|
does_ok(Foo, 'bar'); |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
my $foo = Foo->new; |
13
|
|
|
|
|
|
|
does_ok($foo, 'bar'); |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
=head1 DESCRIPTION |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
Test::Role provides a test for classes and object that implement roles |
18
|
|
|
|
|
|
|
using the Class::Roles module. |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
Test::Role exports a single function B. B takes two |
21
|
|
|
|
|
|
|
required arguments: a class or object and a role which that class or object |
22
|
|
|
|
|
|
|
must perform. A third optional argument may be used to provide a more |
23
|
|
|
|
|
|
|
specific name of what is being testing (for example 'Test customer'). in the |
24
|
|
|
|
|
|
|
absence of this argument, 'the object' will be used instead. |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
Test::Role is implemented using Test::Builder, so it's tests integrate |
27
|
|
|
|
|
|
|
seamlessly with other test modules such as Test::More and Test::Exception. |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
=begin testing |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
package Bar; |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
use Class::Roles role => 'bar'; |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub bar { 'bar' } |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
package Foo; |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
use Class::Roles does => 'bar'; |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
sub new { return bless {}, $_[0] } |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
package main; |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
BEGIN { |
46
|
|
|
|
|
|
|
use_ok('Test::Role'); |
47
|
|
|
|
|
|
|
use_ok('Test::Builder::Tester'); |
48
|
|
|
|
|
|
|
}; |
49
|
|
|
|
|
|
|
ok( defined(&does_ok), "function 'does_ok' is exported"); |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
does_ok('Foo', 'bar'); |
52
|
|
|
|
|
|
|
does_ok('Foo', 'bar', 'the Foo class'); |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
my $foo = Foo->new; |
55
|
|
|
|
|
|
|
does_ok($foo, 'bar'); |
56
|
|
|
|
|
|
|
does_ok($foo, 'bar', 'the $foo object'); |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
test_out("ok 1 - the object performs the bar role"); |
59
|
|
|
|
|
|
|
does_ok('Foo', 'bar'); |
60
|
|
|
|
|
|
|
test_test("does_ok works with default name"); |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
test_out("ok 1 - the Foo class performs the bar role"); |
63
|
|
|
|
|
|
|
does_ok('Foo', 'bar', 'the Foo class'); |
64
|
|
|
|
|
|
|
test_test("does_ok works with explicit name"); |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
test_out("not ok 1 - an undefined object performs the foo role"); |
67
|
|
|
|
|
|
|
test_fail(+2); |
68
|
|
|
|
|
|
|
test_diag(" an undefined object isn't defined"); |
69
|
|
|
|
|
|
|
does_ok(undef, 'foo', 'an undefined object'); |
70
|
|
|
|
|
|
|
test_test("does_ok fails with undefined invocant"); |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
test_out("not ok 1 - the object performs the foo role"); |
73
|
|
|
|
|
|
|
test_fail(+2); |
74
|
|
|
|
|
|
|
test_diag(" the object doesn't perform the foo role"); |
75
|
|
|
|
|
|
|
does_ok('Foo', 'foo'); |
76
|
|
|
|
|
|
|
test_test("does_ok fails for a class without a name"); |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
test_out("not ok 1 - the Foo class performs the foo role"); |
79
|
|
|
|
|
|
|
test_fail(+2); |
80
|
|
|
|
|
|
|
test_diag(" the Foo class doesn't perform the foo role"); |
81
|
|
|
|
|
|
|
does_ok('Foo', 'foo', 'the Foo class'); |
82
|
|
|
|
|
|
|
test_test("does_ok fails for a class with a name"); |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
test_out("not ok 1 - the object performs the foo role"); |
85
|
|
|
|
|
|
|
test_fail(+2); |
86
|
|
|
|
|
|
|
test_diag(" the object doesn't perform the foo role"); |
87
|
|
|
|
|
|
|
does_ok($foo, 'foo'); |
88
|
|
|
|
|
|
|
test_test("does_ok fails for an object without a name"); |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
test_out('not ok 1 - the $foo object performs the foo role'); |
91
|
|
|
|
|
|
|
test_fail(+2); |
92
|
|
|
|
|
|
|
test_diag(" the \$foo object doesn't perform the foo role"); |
93
|
|
|
|
|
|
|
does_ok($foo, 'foo', 'the $foo object'); |
94
|
|
|
|
|
|
|
test_test("does_ok fails for an object with a name"); |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
=end testing |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=cut |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
package Test::Role; |
101
|
|
|
|
|
|
|
|
102
|
1
|
|
|
1
|
|
39689
|
use strict; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
48
|
|
103
|
|
|
|
|
|
|
|
104
|
1
|
|
|
1
|
|
6
|
use Test::Builder; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
23
|
|
105
|
1
|
|
|
1
|
|
5
|
use Class::Roles; |
|
1
|
|
|
|
|
8
|
|
|
1
|
|
|
|
|
22
|
|
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
require Exporter; |
108
|
1
|
|
|
1
|
|
46
|
use vars qw($VERSION @ISA @EXPORT %EXPORT_TAGS); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
668
|
|
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
$VERSION = 0.012_000; |
111
|
|
|
|
|
|
|
@ISA = 'Exporter'; |
112
|
|
|
|
|
|
|
@EXPORT = qw|does_ok|; |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
my $Test = Test::Builder->new; |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
sub does_ok($$;$) |
117
|
|
|
|
|
|
|
{ |
118
|
|
|
|
|
|
|
|
119
|
11
|
|
|
11
|
0
|
5202
|
my($object, $role, $obj_name) = @_; |
120
|
11
|
100
|
|
|
|
35
|
$obj_name = 'the object' unless defined $obj_name; |
121
|
11
|
|
|
|
|
28
|
my $name = "$obj_name performs the $role role"; |
122
|
11
|
|
|
|
|
12
|
my $diag; |
123
|
11
|
100
|
|
|
|
32
|
if( !defined $object ) { |
124
|
1
|
|
|
|
|
4
|
$diag = "$obj_name isn't defined"; |
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
else { |
127
|
10
|
|
|
|
|
33
|
local($@, $!); # eval sometimes resets $! |
128
|
10
|
|
|
|
|
16
|
my $rslt = eval { UNIVERSAL::does($object, $role) }; |
|
10
|
|
|
|
|
33
|
|
129
|
10
|
50
|
|
|
|
168
|
if( $@ ) { |
|
|
100
|
|
|
|
|
|
130
|
0
|
|
|
|
|
0
|
die <
|
131
|
|
|
|
|
|
|
WHOA! I tried to call UNIVERSAL::does on your object and got some weird |
132
|
|
|
|
|
|
|
error. This should never happen. Please contact the author immediately. |
133
|
|
|
|
|
|
|
Here's the error. |
134
|
|
|
|
|
|
|
$@ |
135
|
|
|
|
|
|
|
WHOA |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
elsif( !$rslt ) { |
138
|
4
|
|
|
|
|
15
|
$diag = "$obj_name doesn't perform the $role role"; |
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
|
142
|
11
|
|
|
|
|
14
|
my $ok; |
143
|
11
|
100
|
|
|
|
20
|
if( $diag ) { |
144
|
5
|
|
|
|
|
16
|
$ok = $Test->ok(0, $name); |
145
|
5
|
|
|
|
|
3366
|
$Test->diag(" $diag\n"); |
146
|
|
|
|
|
|
|
} |
147
|
|
|
|
|
|
|
else { |
148
|
6
|
|
|
|
|
20
|
$ok = $Test->ok(1, $name); |
149
|
|
|
|
|
|
|
} |
150
|
|
|
|
|
|
|
|
151
|
11
|
|
|
|
|
2015
|
return $ok; |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
} |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
# keep require happy |
156
|
|
|
|
|
|
|
1; |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
__END__ |