Class.pm

filter: Statementstmt 100% 29 / 29 Branchbran 50.0% 2 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 10 / 10 Podpod n/a totaltotal 95.3% 41 / 43 CC 3 SCAR 3.3
CC
3
Cov
94%
__ANON__
2.1
__ANON__
2.1
__ANON__
1.0
CRAP 1.4
« Class/Role/Object.pm ↑ Summary Class/Role/Meta/Class.pm »
1 #!/usr/bin/perl -c
2  
3 package Test::Builder::Mock::Class;
4  
5 =head1 NAME
6  
7 Test::Builder::Mock::Class - Simulating other classes for Test::Builder
8  
9 =head1 SYNOPSIS
10  
11 use Test::Builder::Mock::Class ':all';
12 use Test::More 'no_plan';
13  
14 # concrete mock class
15 mock_class 'Net::FTP' => 'Net::FTP::Mock';
16 my $mock_object1 = Net::FTP::Mock->new;
17 $mock_object1->mock_tally;
18  
19 # anonymous mocked class
20 my $metamock2 = mock_anon_class 'Net::FTP';
21 my $mock_object2 = $metamock2->new_object;
22 $mock_object2->mock_tally;
23  
24 # anonymous class with role applied
25 my $metamock3 = Test::Builder::Mock::Class->create_mock_anon_class(
26 class => 'Net::FTP',
27 roles => [ 'My::Handler::Role' ],
28 );
29 my $mock_object3 = $metamock3->new_object;
30 $mock_object3->mock_tally;
31  
32 =head1 DESCRIPTION
33  
34 This module adds support for standard L<Test::Builder> framework
35 (L<Test::Simple> or L<Test::More>) to L<Test::Mock::Class>.
36  
37 Mock class can be used to create mock objects which can simulate the behavior
38 of complex, real (non-mock) objects and are therefore useful when a real
39 object is impractical or impossible to incorporate into a unit test.
40  
41 See L<Test::Mock::Class> for more detailed documentation.
42  
43 =for readme stop
44  
45 =cut
46  
47 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
48  
49 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
51  
52 2 our $VERSION = '0.0203';
53  
54 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
55  
56  
57  
58 =head1 INHERITANCE
59  
60 =over 2
61  
62 =item *
63  
64 extends L<Moose::Meta::Class>
65  
66 =cut
67  
68 2 extends 'Moose::Meta::Class';
69  
70 =item *
71  
72 with L<Test::Builder::Mock::Class::Role::Meta::Class>
73  
74 =back
75  
76 =cut
77  
78 2 with 'Test::Builder::Mock::Class::Role::Meta::Class';
79  
80  
81 2 ▶use namespace::clean -except => 'meta';
Subroutine BEGIN: called CC=1 SCAR=0.0
82  
83  
84 =head1 FUNCTIONS
85  
86 =over
87  
88 =cut
89  
90 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
91 2 my %exports = ();
92  
93 =item B<mock_class>( I<class> : Str, I<mock_class> : Str = undef ) : Moose::Meta::Class
94  
95 Creates the concrete mock class based on original I<class>. If the name of
96 I<mock_class> is undefined, its name is created based on name of original
97 I<class> with added C<::Mock> suffix.
98  
99 The function returns the metaclass object of new I<mock_class>.
100  
101 =cut
102  
103 2 ▶ $exports{mock_class} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.2
104 1 ▶ sub ($;$) {
Subroutine __ANON__: called CC=2 SCAR=7.6
105 1 ▶ return __PACKAGE__->create_mock_class(
Branch: 1 branch
BranchTrueFalseTotal
defined $_[1] ? : 1 0 1
106 defined $_[1] ? $_[1] : $_[0] . '::Mock',
107 class => $_[0],
108 );
109 2 };
110 2 };
111  
112 =item B<mock_anon_class>( I<class> : Str = undef ) : Moose::Meta::Class
113  
114 Creates an anonymous mock class based on original I<class>. The name of this
115 class is automatically generated. If I<class> argument not defined, the empty
116 mock class is created.
117  
118 The function returns the metaobject of new mock class.
119  
120 =back
121  
122 =cut
123  
124 2 ▶ $exports{mock_anon_class} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.2
125 1 ▶ sub (;$) {
Subroutine __ANON__: called CC=2 SCAR=7.6
126 1 ▶ return __PACKAGE__->create_mock_anon_class(
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 1 0 1
127 defined $_[0] ? (class => $_[0]) : (),
128 );
129 2 };
130 2 };
131  
132 =head1 IMPORTS
133  
134 =over
135  
136 =cut
137  
138 2 my %groups = ();
139  
140 =item Test::Builder::Mock::Class ':all';
141  
142 Imports all functions into caller's namespace.
143  
144 =back
145  
146 =cut
147  
148 2 $groups{all} = [ keys %exports ];
149  
150 2 require Sub::Exporter;
151 2 Sub::Exporter->import(
152 -setup => {
153 exports => [ %exports ],
154 groups => \%groups,
155 },
156 );
157 };
158  
159  
160 2 1;
161  
162  
163 =begin umlwiki
164  
165 = Class Diagram =
166  
167 [ Test::Builder::Mock::Class
168 ------------------------------------------------------------------------------------
169 ------------------------------------------------------------------------------------
170 <<utility>> mock_class( class : Str, mock_class : Str = undef ) : Moose::Meta::Class
171 <<utility>> mock_anon_class( class : Str ) : Moose::Meta::Class
172 ]
173  
174 [Test::Builder::Mock::Class] ---|> [Moose::Meta::Class] [<<role>> Test::Builder::Mock::Class::Role::Meta::Class]
175  
176 =end umlwiki
177  
178 =head1 EXAMPLE
179  
180 The C<Test::Builder::Mock::Class> fits perfectly to L<Test::Builder>
181 (L<Test::Simple> or L<Test::More>) tests. It adds automatically the tests for
182 each C<mock_invoke> (which is called implicitly by all mock methods) and
183 C<mock_tally>. It means that you need to add these tests to your test plan.
184  
185 Example code:
186  
187 package My::ExampleTest;
188  
189 use Test::More 'no_plan';
190 use Test::Builder::Mock::Class ':all';
191  
192 require 'IO::File';
193 my $mock = mock_anon_class 'IO::File';
194 my $io = $mock->new_object;
195 $io->mock_return( open => 1, args => [qr//, 'r'] );
196  
197 ok( $io->open('/etc/passwd', 'r'), '$io->open' );
198 $io->mock_tally;
199  
200 =head1 BUGS
201  
202 L<Moose> after version 1.05 calls C<BUILDALL> method automatically, so this
203 is one more test to whole plan.
204  
205 L<Test::More> needs an exact count of all tests and it will be different for
206 L<Moose> before and after version 1.05. There are following workarounds:
207  
208 # No plan at all
209 use Test::More 'no_plan';
210  
211 # Different plans depend on Moose version
212 use Test::More;
213 require Moose;
214 plan tests => (Moose->VERSION >= 1.05 ? 10 : 8);
215  
216 # Require Moose >= 1.05
217 use Moose 1.05 ();
218 use Test::More tests => 10;
219  
220 =head1 SEE ALSO
221  
222 Mock metaclass API: L<Test::Builder::Mock::Class::Role::Meta::Class>,
223 L<Moose::Meta::Class>.
224  
225 Mock object methods: L<Test::Builder::Mock::Class::Role::Object>.
226  
227 Perl standard testing: L<Test::Builder>, L<Test::Simple>, L<Test::More>.
228  
229 Mock classes for xUnit-like testing (L<Test::Unit::Lite>):
230 L<Test::Mock::Class>.
231  
232 Other implementations: L<Test::MockObject>, L<Test::MockClass>.
233  
234 =for readme continue
235  
236 =head1 BUGS
237  
238 The API is not stable yet and can be changed in future.
239  
240 =head1 AUTHOR
241  
242 Piotr Roszatycki <dexter@cpan.org>
243  
244 =head1 LICENSE
245  
246 Based on SimpleTest, an open source unit test framework for the PHP
247 programming language, created by Marcus Baker, Jason Sweat, Travis Swicegood,
248 Perrick Penet and Edward Z. Yang.
249  
250 Copyright (c) 2009, 2010 Piotr Roszatycki <dexter@cpan.org>.
251  
252 This program is free software; you can redistribute it and/or modify it
253 under GNU Lesser General Public License.
« Class/Role/Object.pm ↑ Summary Class/Role/Meta/Class.pm »