Proto/Base.pm

filter: Statementstmt 100% 96 / 96 Branchbran 85.7% 12 / 14 Conditioncond 83.3% 5 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 24 / 24 Podpod 100% 6 / 6 totaltotal 96.6% 145 / 150 CC 11 SCAR 7.8
CC
11
Cov
97%
validate
5.0
ok
4.1
run_tests
3.0
CRAP 2.2
« Proto/Role/Tagged.pm ↑ Summary Proto/Object.pm »
1 package Test::Proto::Base;
2 13 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 13 ▶use Test::Proto::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 13 ▶use Test::Proto::TestRunner;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 13 ▶use Test::Proto::Formatter::TestBuilder;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 13 ▶use Test::Proto::TestCase;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 13 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 use overload
11 13 ▶ '&' => \&_overload_AND,
Subroutine BEGIN: called CC=1 SCAR=0.0
12 '|' => \&_overload_OR,
13 '^' => \&_overload_XOR,
14 '!' => \&_overload_NOT,
15 13 ;
16 13 with('Test::Proto::Role::Value');
17 13 with('Test::Proto::Role::Tagged');
18 13 our $VERSION = '0.027';
19  
20 =pod
21  
22 =head1 NAME
23  
24 Test::Proto::Base - Base Class for Test Prototypes
25  
26 =head1 SYNOPSIS
27  
28 my $p = Test::Proto::Base->new->is_eq(-5);
29 $p->ok ($temperature) # will fail unless $temperature is -5
30 $p->ok ($score) # you can use the same test multple times
31 ok($p->validate($score)) # If you like your "ok"s first
32  
33 This is a base class for test prototypes.
34  
35 Throughout this documentation, C<p> will be used as a shorthand for C<< Test::Proto::Base->new >>.
36  
37 =cut
38  
39 =head1 METHODS
40  
41 =head2 PUBLIC METHODS
42  
43 These are the methods intended for use when execcuting tests. All the methods for writing tests can be found at L<Test::Proto::Role::Value>.
44  
45 =cut
46  
47 =head3 validate
48  
49 my $result = $p->validate($subject);
50 warn $result unless $result;
51  
52 Runs through the tests in the prototype and checks that they all pass. It returns a TestRunner which evaluates true or false accordingly.
53  
54 If you have an existing TestRunner, you can pass it that as well;
55  
56 my $result = $p->validate($subject, $context);
57  
58 EXPERIMENTAL: If no argument is passed, $_ will be used.
59  
60 =cut
61  
62 3123 ▶sub validate {
Subroutine validate: called CC=5 SCAR=16.1
Pod documented
63 3123 my ( $self, $subject, $context ) = @_;
64 3123 ▶ $subject = $_ unless exists $_[1];
Branch: 1 branch
BranchTrueFalseTotal
unless exists $_[1] 1 3122 3123
65 3123 ▶ if ( !defined $context or !CORE::ref($context) ) { # if context is not a TestRunner
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $context && CORE::ref $context) { } 959 2164 3123
Condition100%3/3
defined $context && CORE::ref $context
!ll&&!rl&&r
95812164
MC/DC100%2/2
defined $context && CORE::ref $context
defined $context CORE::ref $context
Truth table100%3/3
A && B
A= defined $contextB= CORE::ref $context
ABresult
0X0
100
111
66 959 my $reason = $context;
67 959 $context = Test::Proto::TestRunner->new( subject => $subject );
68 959 ▶ if ( defined $reason ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $reason) 1 958 959
69 1 $context->subtest->diag($reason);
70 }
71 }
72 else {
73 2164 $context->subject($subject);
74 }
75 3123 $self->run_tests($context);
76 3123 $context->done;
77 3123 return $context;
78 }
79  
80 =head3 ok
81  
82 $p->ok($subject, $context)
83  
84 Works like validate, only produces a Test::Builder-compatible TAP output.
85  
86 =cut
87  
88 2 ▶sub ok {
Subroutine ok: called CC=4 SCAR=14.0
Pod documented
89 2 my ( $self, $subject, $context ) = @_;
90 2 my $reason = $context;
91 2 ▶ $context = Test::Proto::TestRunner->new( formatter => Test::Proto::Formatter::TestBuilder->new() ) unless ( ( defined $context ) and ( CORE::ref $context ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $context and CORE::ref $context 2 0 2
Condition66%2/3
defined $context and CORE::ref $context
!ll&&!rl&&r
110
MC/DC0%0/2
defined $context and CORE::ref $context
defined $context CORE::ref $context
Truth table66%2/3
A and B
A= defined $contextB= CORE::ref $context
ABresult
0X0
100
111
92 2 ▶ if ( defined $reason ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $reason) 1 1 2
93 1 $context->subtest->diag($reason);
94 }
95 2 $self->validate( $subject, $context );
96 }
97  
98 =head3 clone
99  
100 This method returns a copy of the current object. The new object can have tests added without affecting the existing test. However, existing tests are not cloned, so if you want to tag them, you will need to clone them too.
101  
102 =cut
103  
104 4 ▶sub clone {
Subroutine clone: called CC=1 SCAR=0.0
Pod documented
105 4 my $self = shift;
106 4 my $pkg = CORE::ref $self;
107 4 my %args = ( map { $_ => [ @{ $self->$_ } ] } qw(natural_script user_script tags) );
108 4 return $pkg->new(%args);
109 }
110  
111 =head2 OPERATOR OVERLOADING
112  
113 Prototypes can be combined with the operators C<&>, C<|>, C<^>, and negated: C<!>. In all cases, a new prototype is returned.
114  
115 $x & $y => p->all_of([$x, $y])
116 $x | $y => p->any_of([$x, $y])
117 $x ^ $y => p->some_of([$x, $y], 1)
118 !$x => p->none_of([$x])
119  
120 Remember that this only works with prototypes. C<'A' & 'B'> still returns C<'@'>.
121  
122 =cut
123  
124 =head2 PROTOTYPER METHODS
125  
126 These are for documentation purposes only.
127  
128 =cut
129  
130 =head3 natural_type
131  
132 This roughly corresponds to C<ref>. Useful for indicating what sort of element you're expecting.
133  
134 This is documented for information purposes only and is not intended to be used except in the maintainance of C<Test::Proto> itself.
135  
136 =cut
137  
138 has natural_type => (
139 is => 'rw',
140 3048 ▶ default => sub { '' },
Subroutine __ANON__: called CC=1 SCAR=0.0
141 ,
142 13 ); # roughly corresponds to ref.
143  
144 =head3 natural_script
145  
146 These are tests common to the whole prototype which need not be repeated if two similar scripts are joined together. Normally, this should only be modified by the prototype class.
147  
148 This is documented for information purposes only and is not intended to be used except in the maintainance of C<Test::Proto> itself.
149  
150 =cut
151  
152 has natural_script => (
153 is => 'rw',
154 3044 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
155 13 );
156  
157 =head3 user_script
158  
159 These are the tests which the user (specifically, the test script author) has added by a method call. Normally, these should empty in a class but may be present in an instance of an object.
160  
161 This is documented for information purposes only and is not intended to be used except in the maintainance of C<Test::Proto> itself.
162  
163 =cut
164  
165 has user_script => (
166 is => 'rw',
167 3044 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
168 13 );
169  
170 =head3 script
171  
172 This method returns an arrayref containing the contents of the C<natural_script> and the C<user_script>, i.e. all the tests in the object that are due to be run when C<< ->ok() >> is called.
173  
174 =cut
175  
176 3123 ▶sub script {
Subroutine script: called CC=1 SCAR=0.0
Pod documented
177 3123 my $self = shift;
178 3123 return [ @{ $self->natural_script }, @{ $self->user_script }, ];
179 }
180  
181 =head3 add_test
182  
183 This method adds a test to the current object, specifically to the C<user_script>, and returns the prototype object.
184  
185 This is documented for information purposes only and is not intended to be used except in the maintainance of C<Test::Proto> itself.
186  
187 =cut
188  
189 4063 ▶sub add_test {
Subroutine add_test: called CC=1 SCAR=0.0
Pod documented
190 4063 my ( $self, $name, $data, $reason ) = @_;
191 4063 my $package = CORE::ref($self);
192  
193 4063 my $testMethodName = $package . '::' . ${Test::Proto::Common::TEST_PREFIX} . $name;
194 4065 ▶ my $code = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
195 4065 my $runner = shift;
196 4065 my $subject = $runner->subject;
197 {
198 13 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
199 4065 eval { &{$testMethodName}( $runner, $data, $reason ); };
200 4065 ▶ $runner->exception( "Failed during $name\n" . $@ ) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 4064 4065
201 }
202 4063 };
203 4063 push @{ $self->user_script },
204 Test::Proto::TestCase->new(
205 name => $name,
206 code => $code,
207 data => $data,
208 reason => $reason,
209 );
210 4063 return $self;
211 }
212  
213 =head3 run_tests
214  
215 $self->run_tests($subject, $context);
216  
217 This method runs all the tests in the prototype object's script (simply calling the C<< ->run_test >> method on each), and returns the prototype object.
218  
219 This is documented for information purposes only and is not intended to be used except in the maintainance of C<Test::Proto> itself.
220  
221 =cut
222  
223 3123 ▶sub run_tests {
Subroutine run_tests: called CC=3 SCAR=11.0
Pod documented
224 3123 my ( $self, $context ) = @_;
225 3123 my $runner = $context->subtest( test_case => $self );
226 3123 foreach my $test ( @{ $self->script } ) {
227  
228 # $self->run_test($test, $runner);
229 4067 $runner->run_test( $test, $self );
230 4067 ▶ return $self if $runner->is_exception;
Branch: 1 branch
BranchTrueFalseTotal
if $runner->is_exception 0 4067 4067
231 }
232 3123 $runner->done( "A " . ( ref $self ) . " must pass all its subtests." );
233 3123 return $self;
234 }
235  
236 =head1 OTHER INFORMATION
237  
238 For author, version, bug reports, support, etc, please see L<Test::Proto>.
239  
240 =cut
241  
242 2 ▶sub _overload_AND {
Subroutine _overload_AND: called CC=1 SCAR=0.0
243 2 my ( $left, $right ) = @_;
244 2 return __PACKAGE__->new->all_of( [ $left, $right ] );
245 }
246  
247 2 ▶sub _overload_OR {
Subroutine _overload_OR: called CC=1 SCAR=0.0
248 2 my ( $left, $right ) = @_;
249 2 return __PACKAGE__->new->any_of( [ $left, $right ] );
250 }
251  
252 2 ▶sub _overload_XOR {
Subroutine _overload_XOR: called CC=1 SCAR=0.0
253 2 my ( $left, $right ) = @_;
254 2 return __PACKAGE__->new->some_of( [ $left, $right ], 1 );
255 }
256  
257 2 ▶sub _overload_NOT {
Subroutine _overload_NOT: called CC=1 SCAR=0.0
258 2 my ($left) = @_;
259 2 return __PACKAGE__->new->none_of( [$left], 1 );
260 }
261  
262 13 1;
« Proto/Role/Tagged.pm ↑ Summary Proto/Object.pm »