blib/lib/MooseX/Test/Role.pm

filter: Statementstmt 38.1% 42 / 110 Branchbran 2.6% 1 / 38 Conditioncond 8.3% 1 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 56.5% 13 / 23 Podpod 80.0% 4 / 5 totaltotal 31.1% 61 / 196 CC 26 SCAR 28.4
CC
26
Cov
28%
_add_methods
44.1
_required_methods
42.0
_apply_role
20.0
CRAP 17.2
↑ Summary
1 package MooseX::Test::Role;
2  
3 7 our $VERSION = '0.07';
4  
5 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 7 ▶use Carp qw( confess );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use Class::Load qw( try_load_class );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use List::Util qw( first );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 7 ▶use Test::Builder;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 7 ▶use Exporter qw( import unimport );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 7 our @EXPORT = qw( requires_ok consumer_of consuming_object consuming_class );
15  
16 1 ▶sub requires_ok {
Subroutine requires_ok: called CC=4 SCAR=25.3
Pod documented
17 1 my ( $role, @required ) = @_;
18 1 my $msg = "$role requires " . join( ', ', @required );
19  
20 1 my $role_type = _derive_role_type($role);
21 0 ▶ if (!$role_type) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($role_type) 0 0 0
22 0 ok( 0, $msg );
23 0 return;
24 }
25  
26 0 foreach my $req (@required) {
27 0 ▶ unless ( first { $_ eq $req } _required_methods($role_type, $role) ) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
unless (&first(sub { $_ eq $req; } , _required_methods($role_type, $role))) 0 0 0
28 0 ok( 0, $msg );
29 0 return;
30 }
31 }
32 0 ok( 1, $msg );
33 }
34  
35 3 ▶sub consuming_class {
Subroutine consuming_class: called CC=3 SCAR=17.9
Pod documented
36 3 my ( $role, %args ) = @_;
37  
38 0 ▶ my %methods = exists $args{methods} ? %{ $args{methods} } : ();
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'methods'} ? : 0 3 3
39  
40 3 my $role_type = _derive_role_type($role);
41 0 ▶ confess 'first argument should be a role' unless $role_type;
Branch: 1 branch
BranchTrueFalseTotal
unless $role_type 0 0 0
42  
43 0 my $package = _package_name();
44 0 _add_methods(
45 package => $package,
46 role_type => $role_type,
47 role => $role,
48 methods => \%methods,
49 );
50  
51 0 _apply_role(
52 package => $package,
53 role_type => $role_type,
54 role => $role,
55 );
56  
57 0 return $package;
58 }
59  
60 1 ▶sub consuming_object {
Subroutine consuming_object: called CC=2 SCAR=13.0
Pod documented
61 1 my $class = consuming_class(@_);
62  
63 # Moose and Moo can be instantiated and should be. Role::Tiny however isn't
64 # a full OO implementation and so doesn't provide a "new" method.
65 0 ▶ return $class->can('new') ? $class->new() : $class;
Branch: 1 branch
BranchTrueFalseTotal
$class->can('new') ? : 0 0 0
66 }
67  
68 1 ▶sub consumer_of {
Subroutine consumer_of: called CC=2 SCAR=10.5
Pod documented
69 1 my ( $role, %methods ) = @_;
70  
71 1 ▶ confess 'first argument to consumer_of should be a role' unless _derive_role_type($role);
Branch: 1 branch
BranchTrueFalseTotal
unless _derive_role_type($role) 0 0 0
72  
73 0 return consuming_object( $role, methods => \%methods );
74 }
75  
76 0 ▶sub _required_methods {
Subroutine _required_methods: not called CC=6 SCAR=37.4
77 0 my ($role_type, $role) = @_;
78 0 my @methods;
79  
80 0 ▶ if ($role_type eq 'Moose::Role') {
Branch: 2 branches
BranchTrueFalseTotal
if ($role_type eq 'Moose::Role') { } 0 0 0
elsif ($role_type eq 'Role::Tiny') { } 0 0 0
81 0 @methods = $role->meta->get_required_method_list();
82 }
83 elsif ($role_type eq 'Role::Tiny') {
84 0 my $info = _role_tiny_info($role);
85 0 ▶ if ($info && ref($info->{requires}) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($info and ref $info->{"requires"} eq "ARRAY") 0 0 0
Condition0%0/3
$info and ref $info->{"requires"} eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
$info and ref $info->{"requires"} eq "ARRAY"
$info ref $info->{"requires"} eq "ARRAY"
Truth table0%0/3
A and B
A= $infoB= ref $info->{"requires"} eq "ARRAY"
ABresult
0X0
100
111
86 0 @methods = @{$info->{requires}};
87 }
88 }
89  
90 0 ▶ return wantarray ? @methods : \@methods;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
91 }
92  
93 5 ▶sub _derive_role_type {
Subroutine _derive_role_type: called CC=5 SCAR=27.8
94 5 my $role = shift;
95  
96 5 try_load_class($role);
97  
98 5 ▶ if ($role->can('meta') && $role->meta()->isa('Moose::Meta::Role')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($role->can("meta") and $role->meta->isa("Moose::Meta::Role")) 0 0 0
Condition33%1/3
$role->can("meta") and $role->meta->isa("Moose::Meta::Role")
!ll&&!rl&&r
050
MC/DC0%0/2
$role->can("meta") and $role->meta->isa("Moose::Meta::Role")
$role->can("meta") $role->meta->isa("Moose::Meta::Role")
Truth table33%1/3
A and B
A= $role->can("meta")B= $role->meta->isa("Moose::Meta::Role")
ABresult
0X0
100
111
99 # Also covers newer Moo::Roles
100 0 return 'Moose::Role';
101 }
102  
103 0 ▶ if (try_load_class('Role::Tiny') && _role_tiny_info($role)) {
Branch: 1 branch
BranchTrueFalseTotal
if (try_load_class("Role::Tiny") and _role_tiny_info($role)) 0 0 0
Condition0%0/3
try_load_class("Role::Tiny") and _role_tiny_info($role)
!ll&&!rl&&r
000
MC/DC0%0/2
try_load_class("Role::Tiny") and _role_tiny_info($role)
try_load_class("Role::Tiny") _role_tiny_info($role)
Truth table0%0/3
A and B
A= try_load_class("Role::Tiny")B= _role_tiny_info($role)
ABresult
0X0
100
111
104 # Also covers older Moo::Roles
105 0 return 'Role::Tiny';
106 }
107  
108 0 return;
109 }
110  
111 7 my $package_counter = 0;
112 0 ▶sub _package_name {
Subroutine _package_name: not called CC=1 SCAR=6.9
113 0 return 'MooseX::Test::Role::Consumer' . $package_counter++;
114 }
115  
116 0 ▶sub _apply_role {
Subroutine _apply_role: not called CC=4 SCAR=30.0
117 0 my %args = @_;
118  
119 0 my $package = $args{package};
120 0 my $role_type = $args{role_type};
121 0 my $role = $args{role};
122  
123 # We'll need a thing that exports a "with" sub
124 0 my $with_exporter;
125 0 ▶ if ($role_type eq 'Moose::Role') {
Branch: 2 branches
BranchTrueFalseTotal
if ($role_type eq 'Moose::Role') { } 0 0 0
elsif ($role_type eq 'Role::Tiny') { } 0 0 0
126 0 $with_exporter = 'Moose';
127 }
128 elsif ($role_type eq 'Role::Tiny') {
129 0 $with_exporter = 'Role::Tiny::With';
130 }
131 else {
132 0 confess "Unknown role type $role_type";
133 }
134  
135 0 my $source = qq{
136 package $package;
137  
138 use $with_exporter;
139 with('$role');
140 };
141  
142 #warn $source;
143  
144 0 eval($source);
145 0 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
146  
147 0 return $package;
148 }
149  
150 0 ▶sub _add_methods {
Subroutine _add_methods: not called CC=7 SCAR=37.9
151 0 my %args = @_;
152  
153 0 my $role_type = $args{role_type};
154 0 my $package = $args{package};
155 0 my $role = $args{role};
156 0 my $methods = $args{methods};
157  
158 0 ▶ $methods->{$_} ||= sub { undef } for _required_methods( $role_type, $role );
Subroutine __ANON__: not called CC=1 SCAR=6.9
Condition0%0/3
$methods->{$_} ||= sub { undef; }
l!l&&r!l&&!r
000
MC/DC0%0/2
$methods->{$_} ||= sub { undef; }
$methods->{$_} sub { undef; }
Truth table0%0/3
A ||= B
A= $methods->{$_}B= sub { undef; }
ABresult
1X1
011
000
159  
160 0 my $meta;
161 0 ▶ $meta = Moose::Meta::Class->create($package) if $role_type eq 'Moose::Role';
Branch: 1 branch
BranchTrueFalseTotal
if $role_type eq "Moose::Role" 0 0 0
162  
163 0 ▶ while ( my ( $method, $subref ) = each(%{$methods}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$methods;}) 0 0 0
164 # This allows us to have scalar values without an anonymous sub in the
165 # definition, similar to Moose's default values. We need to make a lexical
166 # copy of $subref so we do not build a circle where we return itself as a
167 # coderef later on.
168 0 ▶ if ( !ref $subref ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $subref) 0 0 0
169 0 my $value = $subref;
170 0 ▶ $subref = sub { $value };
Subroutine __ANON__: not called CC=1 SCAR=6.9
171 }
172  
173 0 ▶ if ($meta) {
Branch: 1 branch
BranchTrueFalseTotal
if ($meta) { } 0 0 0
174 0 $meta->add_method($method => $subref);
175 }
176 else {
177 7 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
178 #no warnings 'redefine';
179 0 *{ $package . '::' . $method } = $subref;
180 }
181 }
182  
183 0 return;
184 }
185  
186 0 ▶sub _role_tiny_info {
Subroutine _role_tiny_info: not called CC=1 SCAR=6.9
187 # This seems brittle, but there aren't many options to get this data.
188 # Moo relies on %INFO too, so it seems like it would be a hard thing
189 # for to move away from.
190  
191 0 my $role = shift;
192 0 return $Role::Tiny::INFO{$role};
193 }
194  
195 7 my $Test = Test::Builder->new();
196  
197 # Done this way for easier testing
198 0 ▶our $ok = sub { $Test->ok(@_) };
Subroutine __ANON__: not called CC=1 SCAR=1.2
199 0 ▶sub ok { $ok->(@_) }
Subroutine ok: not called CC=1 SCAR=6.9
Pod undocumented
200  
201 7 1;
202  
203 =pod
204  
205 =head1 NAME
206  
207 MooseX::Test::Role - Test functions for Moose roles
208  
209 =head1 SYNOPSIS
210  
211 use MooseX::Test::Role;
212 use Test::More tests => 3;
213  
214 requires_ok('MyRole', qw/method1 method2/);
215  
216 my $consumer = consuming_object(
217 'MyRole',
218 methods => {
219 method1 => 1,
220 method2 => sub { shift->method1() },
221 }
222 );
223 ok( $consumer->myrole_method );
224 is( $consumer->method1, 1 );
225 is( $consumer->method2, 1 );
226  
227 my $consuming_class = consuming_class('MyRole');
228 ok( $consuming_class->class_method() );
229  
230 =head1 DESCRIPTION
231  
232 Provides functions for testing roles. Supports roles created with
233 L<Moose::Role>, L<Moo::Role> or L<Role::Tiny>.
234  
235 =head1 BACKGROUND
236  
237 Unit testing a role can be hard. A major problem is creating classes that
238 consume the role.
239  
240 One could side-step the problem entirely and just call the subroutines in the
241 role's package directly. For example,
242  
243 Fooable->bar();
244  
245 That only works until C<Fooable> calls another method in the consuming class
246 though. Mock objects are a tempting way to solve that problem:
247  
248 my $consumer = Test::MockObject->new();
249 $consumer->set_always('baz', 1);
250 Fooable::bar($consumer);
251  
252 But if C<Fooable::bar> happens to call another method in the role then
253 the mock consumer will have to mock that method too.
254  
255 A better way is to create a class to consume the role:
256  
257 package FooableTest;
258  
259 use Moose;
260 with 'Fooable';
261  
262 sub required_method {}
263  
264 package main;
265  
266 my $consumer = FooableTest->new();
267 $consumer->bar();
268  
269 This can work well for some roles. Unfortunately, if several variations have to
270 be tested, it may be necessary to create several consuming test classes, which
271 gets tedious.
272  
273 Moose can create anonymous classes which consume roles:
274  
275 my $consumer = Moose::Meta::Class->create_anon_class(
276 roles => ['Fooable'],
277 methods => {
278 required_method => sub {},
279 }
280 )->new_object();
281 $consumer->bar();
282  
283 This can still be tedious, especially for roles that require lots of methods.
284 C<MooseX::Test::Role> simply makes this easier to do.
285  
286 =head1 EXPORTED FUNCTIONS
287  
288 =over 4
289  
290 =item C<consuming_class($role, methods => \%methods)>
291  
292 Creates a class which consumes the role, and returns it's package name.
293  
294 C<$role> must be the package name of a role. L<Moose::Role>, L<Moo::Role> and
295 L<Role::Tiny> are supported.
296  
297 Any method required by the role will be stubbed. To override the default stub
298 methods, or to add additional methods, specify the name and a coderef:
299  
300 consuming_class('MyRole',
301 method1 => sub { 'one' },
302 method2 => sub { 'two' },
303 required_method => sub { 'required' },
304 );
305  
306 For methods that should just return a fixed scalar value, you can ommit the
307 coderef.
308  
309 consuming_class('MyRole',
310 method1 => 'one',
311 method_uc1 => sub {
312 my ($self) = @_;
313 lc $self->one;
314 },
315 );
316  
317 =item C<consuming_object($role, methods => \%methods)>
318  
319 Creates a class which consumes the role, and returns an instance of it.
320  
321 If the class does not have a C<new()> method (which is commonly the case for
322 L<Role::Tiny>), then the package name will be returned instead.
323  
324 See C<consuming_class> for arguments. C<consuming_object> is essentially
325 equivalent to:
326  
327 consuming_class(@_)->new();
328  
329 =item C<consumer_of ($role, %methods)>
330  
331 Alias of C<consuming_object>, without named arguments. This is left in for
332 compatibility, new code should use C<consuming_object>.
333  
334 =item C<requires_ok ($role, @methods)>
335  
336 Tests if role requires one or more methods.
337  
338 =back
339  
340 =head1 GITHUB
341  
342 Patches, comments or mean-spirited code reviews are all welcomed on GitHub:
343  
344 L<https://github.com/pboyd/MooseX-Test-Role>
345  
346 =head1 AUTHOR
347  
348 Paul Boyd <boyd.paul2@gmail.com>
349  
350 =head1 COPYRIGHT AND LICENSE
351  
352 This software is copyright (c) 2014 by Paul Boyd.
353  
354 This is free software; you can redistribute it and/or modify it under
355 the same terms as the Perl 5 programming language system itself.
356  
357 =cut
↑ Summary