Class/Role/Meta/Class.pm

filter: Statementstmt 85.3% 76 / 89 Branchbran 54.1% 13 / 24 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 90.4% 19 / 21 Podpod 100% 4 / 4 totaltotal 81.1% 112 / 138 CC 14 SCAR 16.5
CC
14
Cov
79%
_get_mock_metaclass_instance_roles
20.0
_get_mock_metaclasses
12.0
_construct_mock_class
5.0
CRAP 5.2
« Class/Role/Object.pm ↑ Summary Class.pm »
1 #!/usr/bin/perl -c
2  
3 package Test::Mock::Class::Role::Meta::Class;
4  
5 =head1 NAME
6  
7 Test::Mock::Class::Role::Meta::Class - Metaclass for mock class
8  
9 =head1 DESCRIPTION
10  
11 This role provides an API for defining and changing behavior of mock class.
12  
13 =cut
14  
15 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 1 our $VERSION = '0.0303';
21  
22 1 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24  
25 1 ▶use Moose::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 1 ▶use Symbol ();
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 1 ▶use Test::Assert ':all';
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 1 ▶use Exception::Base (
Subroutine BEGIN: called CC=1 SCAR=0.0
32 1 '+ignore_package' => [__PACKAGE__],
33 1 );
34  
35  
36 =head1 ATTRIBUTES
37  
38 =over
39  
40 =item B<mock_base_object_role> : Str = "Test::Mock::Class::Role::Object"
41  
42 Base object role for mock class. The default is
43 L<Test::Mock::Class::Role::Object>.
44  
45 =cut
46  
47 1 has 'mock_base_object_role' => (
48 is => 'rw',
49 default => 'Test::Mock::Class::Role::Object',
50 );
51  
52 =item B<mock_ignore_methods_regexp> : RegexpRef = "/^(_?mock_|(can|DEMOLISHALL|DESTROY|DOES|does|isa|VERSION)$)/"
53  
54 Regexp matches method names which are not created automatically for mock
55 class.
56  
57 =cut
58  
59 has 'mock_ignore_methods_regexp' => (
60 is => 'rw',
61 55 ▶ default => sub { qr/^(_?mock_|(can|DEMOLISHALL|DESTROY|DOES|does|meta|isa|VERSION)$)/ },
Subroutine __ANON__: called CC=1 SCAR=0.0
62 1 );
63  
64 =item B<mock_constructor_methods_regexp> : RegexpRef = "/^new$/"
65  
66 Regexp matches method names which are constructors rather than normal methods.
67  
68 =back
69  
70 =cut
71  
72 has 'mock_constructor_methods_regexp' => (
73 is => 'rw',
74 55 ▶ default => sub { qr/^new$/ },
Subroutine __ANON__: called CC=1 SCAR=0.0
75 1 );
76  
77  
78 1 ▶use namespace::clean -except => 'meta';
Subroutine BEGIN: called CC=1 SCAR=0.0
79  
80  
81 ## no critic qw(RequireCheckingReturnValueOfEval)
82  
83 =head1 CONSTRUCTORS
84  
85 =over
86  
87 =item B<create_mock_class>( I<name> : Str, :I<class> : Str, I<args> : Hash ) : Moose::Meta::Class
88  
89 Creates new L<Moose::Meta::Class> object which represents named mock class.
90 It automatically adds all methods which exists in original class, except those
91 which matches C<mock_ignore_methods_regexp> attribute.
92  
93 If C<new> method exists in original class, it is created as constructor.
94  
95 The method takes additional arguments:
96  
97 =over
98  
99 =item class
100  
101 Optional I<class> parameter is a name of original class and its methods will
102 be created for new mock class.
103  
104 =item methods
105  
106 List of additional methods to create.
107  
108 =back
109  
110 The constructor returns metaclass object.
111  
112 Test::Mock::Class->create_mock_class(
113 'IO::File::Mock' => ( class => 'IO::File' )
114 );
115  
116 =cut
117  
118 4 ▶sub create_mock_class {
Subroutine create_mock_class: called CC=1 SCAR=0.0
Pod documented
119 4 my ($class, $name, %args) = @_;
120 4 my $self = $class->create($name, %args);
121 4 $self->_construct_mock_class(%args);
122 4 return $self;
123 };
124  
125  
126 =item B<create_mock_anon_class>( :I<class> : Str, I<args> : Hash ) : Moose::Meta::Class
127  
128 Creates new L<Moose::Meta::Class> object which represents anonymous mock
129 class. Optional I<class> parameter is a name of original class and its
130 methods will be created for new mock class.
131  
132 Anonymous classes are destroyed once the metaclass they are attached to goes
133 out of scope.
134  
135 The constructor returns metaclass object.
136  
137 my $meta = Test::Mock::Class->create_mock_anon_class(
138 class => 'File::Temp'
139 );
140  
141 =back
142  
143 =cut
144  
145 51 ▶sub create_mock_anon_class {
Subroutine create_mock_anon_class: called CC=1 SCAR=0.0
Pod documented
146 51 my ($class, %args) = @_;
147 51 my $self = $class->create_anon_class(%args);
148 51 $self->_construct_mock_class(%args);
149 51 return $self;
150 };
151  
152  
153 =head1 METHODS
154  
155 =over
156  
157 =item B<add_mock_method>( I<method> : Str ) : Self
158  
159 Adds new I<method> to mock class. The behavior of this method can be changed
160 with C<mock_return> and other methods.
161  
162 =cut
163  
164 518 ▶sub add_mock_method {
Subroutine add_mock_method: called CC=1 SCAR=0.0
Pod documented
165 518 my ($self, $method) = @_;
166 142 ▶ $self->add_method( $method => sub {
Subroutine dump: called CC=1 SCAR=0.0
167 142 my $method_self = shift;
168 142 return $method_self->mock_invoke($method, @_);
169 518 } );
170 518 return $self;
171 };
172  
173  
174 =item B<add_mock_constructor>( I<method> : Str ) : Self
175  
176 Adds new constructor to mock class. This is almost the same as
177 C<add_mock_method> but it returns new object rather than defined value.
178  
179 The calls counter is set to C<1> for new object's constructor.
180  
181 =cut
182  
183 56 ▶sub add_mock_constructor {
Subroutine add_mock_constructor: called CC=1 SCAR=0.0
Pod documented
184 56 my ($self, $constructor) = @_;
185 2 ▶ $self->add_method( $constructor => sub {
Subroutine new: called CC=2 SCAR=6.9
186 2 my $method_class = shift;
187 2 ▶ $method_class->mock_invoke($constructor, @_) if blessed $method_class;
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($method_class) 1 1 2
188 2 my $new_object = $method_class->meta->new_object(@_);
189 2 $new_object->mock_invoke($constructor, @_);
190 2 return $new_object;
191 56 } );
192 56 return $self;
193 };
194  
195  
196 =item B<_construct_mock_class>( :I<class> : Str, :I<methods> : ArrayRef ) : Self
197  
198 Constructs mock class based on original class. Adds the same methods as in
199 original class. If original class has C<new> method, the constructor with
200 this name is created.
201  
202 =cut
203  
204 55 ▶sub _construct_mock_class {
Subroutine _construct_mock_class: called CC=5 SCAR=16.1
205 55 my ($self, %args) = @_;
206  
207 55 Moose::Util::apply_all_roles(
208 $self,
209 $self->mock_base_object_role,
210 );
211  
212 55 $self->superclasses( $self->_get_mock_superclasses($args{class}) );
213  
214 0 ▶ my @methods = defined $args{methods} ? @{ $args{methods} } : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'methods'} ? : 0 55 55
215  
216 55 my @mock_methods = do {
217 55 my %uniq = map { $_ => 1 }
218 (
219 $self->get_all_method_names,
220 @methods,
221 );
222 55 keys %uniq;
223 };
224  
225 55 foreach my $method (@mock_methods) {
226 2112 ▶ if ($method =~ $self->mock_ignore_methods_regexp) {
Branch: 2 branches
BranchTrueFalseTotal
if ($method =~ $self->mock_ignore_methods_regexp) { } 1540 572 2112
elsif ($method =~ $self->mock_constructor_methods_regexp) { } 55 517 572
227 # ignore destructor and basic instrospection methods
228 }
229 elsif ($method =~ $self->mock_constructor_methods_regexp) {
230 55 $self->add_mock_constructor($method);
231 }
232 else {
233 517 $self->add_mock_method($method);
234 };
235 };
236  
237 55 return $self;
238 };
239  
240  
241 55 ▶sub _get_mock_superclasses {
Subroutine _get_mock_superclasses: called CC=4 SCAR=13.9
242 55 my ($self, $class) = @_;
243  
244 55 ▶ return ('Moose::Object') unless defined $class;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $class 1 54 55
245  
246 my @superclasses = (
247 $class->can('meta')
248 ? $class->meta->superclasses
249 54 ▶ : @{ *{Symbol::qualify_to_ref('ISA', $class)} },
Branch: 1 branch
BranchTrueFalseTotal
$class->can('meta') ? : 51 3 54
250 );
251  
252 unshift @superclasses, 'Moose::Object'
253 54 ▶ unless grep { $_ eq 'Moose::Object' } @superclasses;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq "Moose::Object";} @superclasses 3 51 54
254  
255 54 unshift @superclasses, $class;
256  
257 54 return @superclasses;
258 };
259  
260  
261 0 ▶sub _get_mock_metaclasses {
Subroutine _get_mock_metaclasses: not called CC=3 SCAR=24.8
262 0 my ($self, $class) = @_;
263  
264 0 ▶ return () unless defined $class;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $class 0 0 0
265 0 ▶ return () unless $class->can('meta');
Branch: 1 branch
BranchTrueFalseTotal
unless $class->can("meta") 0 0 0
266  
267 return (
268 0 attribute_metaclass => $class->meta->attribute_metaclass,
269 instance_metaclass => $class->meta->instance_metaclass,
270 method_metaclass => $class->meta->method_metaclass,
271 );
272 };
273  
274  
275 0 ▶sub _get_mock_metaclass_instance_roles {
Subroutine _get_mock_metaclass_instance_roles: not called CC=4 SCAR=30.0
276 0 my ($self, $class) = @_;
277  
278 0 ▶ return () unless defined $class;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $class 0 0 0
279 0 ▶ return () unless $class->can('meta');
Branch: 1 branch
BranchTrueFalseTotal
unless $class->can("meta") 0 0 0
280  
281 0 my $metaclass_instance = $class->meta->get_meta_instance->meta;
282  
283 0 ▶ return () unless $metaclass_instance->can('roles');
Branch: 1 branch
BranchTrueFalseTotal
unless $metaclass_instance->can("roles") 0 0 0
284  
285 0 return map { $_->name }
286 0 @{ $metaclass_instance->roles };
287 };
288  
289  
290 1 1;
291  
292  
293 =back
294  
295 =begin umlwiki
296  
297 = Class Diagram =
298  
299 [ <<role>>
300 Test::Mock::Class::Role::Meta::Class
301 ------------------------------------------------------------------------------
302 +mock_base_object_role = "Test::Mock::Class::Role::Object"
303 +mock_ignore_methods_regexp : RegexpRef = "/^(can|DEMOLISHALL|DESTROY|DOES|does|isa|VERSION)$/"
304 +mock_constructor_methods_regexp : RegexpRef = "/^new$/"
305 ------------------------------------------------------------------------------
306 +create_mock_class( name : Str, :class : Str, args : Hash ) : Moose::Meta::Class
307 +create_mock_anon_class( :class : Str, args : Hash ) : Moose::Meta::Class
308 +add_mock_method( method : Str ) : Self
309 +add_mock_constructor( method : Str ) : Self
310 ]
311  
312 =end umlwiki
313  
314 =head1 SEE ALSO
315  
316 L<Test::Mock::Class>.
317  
318 =head1 BUGS
319  
320 The API is not stable yet and can be changed in future.
321  
322 =head1 AUTHOR
323  
324 Piotr Roszatycki <dexter@cpan.org>
325  
326 =head1 LICENSE
327  
328 Based on SimpleTest, an open source unit test framework for the PHP
329 programming language, created by Marcus Baker, Jason Sweat, Travis Swicegood,
330 Perrick Penet and Edward Z. Yang.
331  
332 Copyright (c) 2009, 2010 Piotr Roszatycki <dexter@cpan.org>.
333  
334 This program is free software; you can redistribute it and/or modify it
335 under GNU Lesser General Public License.
« Class/Role/Object.pm ↑ Summary Class.pm »