Proto/Common.pm

filter: Statementstmt 78.7% 63 / 80 Branchbran 78.9% 30 / 38 Conditioncond 50.0% 6 / 12 MC/DCmcdc 28.5% 2 / 7 Subroutinesub 75.0% 12 / 16 Podpod 100% 6 / 6 totaltotal 74.8% 119 / 159 CC 24 SCAR 19.6
CC
24
Cov
76%
upgrade
12.5
upgrade_comparison
9.1
define_simple_test
6.0
CRAP 7.1
↑ Summary Proto/Role/ArrayRef.pm »
1 package Test::Proto::Common;
2 15 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 15 ▶use Sub::Name;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 15 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 15 ▶use Scalar::Util qw(blessed looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 15 our @EXPORT = qw(define_test define_simple_test simple_test upgrade upgrade_comparison);
9  
10 15 our $TEST_PREFIX = '_TEST_'; #~ this is used when creating internal methods.
11  
12 =head1 NAME
13  
14 Test::Proto::Common - Provides common functions for Test::Proto development
15  
16 =head1 SYNOPSIS
17  
18 use Test::Proto::Common; # exports all functions automatically
19  
20 Provides functions used to build a Prototype class.
21  
22 =cut
23  
24 =head1 FUNCTIONS
25  
26 All these functions are for use in prototype classes, not in scripts.
27  
28 =head3 define_test
29  
30 define_test 'is_uppercase', sub {
31 my ($self, $data, $reason) = @_; # self is the runner, NOT the prototype
32 if ($self->subject =~ !/[a-z]/){
33 return $self->pass;
34 }
35 return $self->fail;
36 };
37  
38 Adds a test definition to the class. This allows you to create user-facing test methods which interact with the test definition. The name you provide is the name of the test definition, which usually matches the test method (but is not required to).
39  
40 Optionally, you can set the package to which this method is to be added as a third argument.
41  
42 =cut
43  
44 814 ▶sub define_test {
Subroutine define_test: called CC=2 SCAR=6.9
Pod documented
45 814 my ( $testName, $testSub, $customPackage ) = @_;
46 814 my ( $package, $filename, $line ) = caller;
47 814 ▶ $package = $customPackage if defined $customPackage;
Branch: 1 branch
BranchTrueFalseTotal
if defined $customPackage 0 814 814
48 {
49 15 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
50 814 my $fullName = $package . '::' . $TEST_PREFIX . $testName;
51 814 *$fullName = subname( $TEST_PREFIX . $testName, $testSub ); #~ Consider Sub::Install here, per Khisanth on irc.freenode.net#perl
52 }
53  
54 #~ return value of this not specified
55 }
56  
57 =head3 define_simple_test
58  
59 Adds a test definition to the class. In this case, the subroutine passed evaluates the subject against the expected data.
60  
61 =cut
62  
63 0 ▶sub define_simple_test {
Subroutine define_simple_test: not called CC=2 SCAR=17.9
Pod documented
64 0 my ( $testName, $testSub, $customPackage ) = @_;
65 0 my ( $package, $filename, $line ) = caller;
66 0 ▶ $package = $customPackage if defined $customPackage;
Branch: 1 branch
BranchTrueFalseTotal
if defined $customPackage 0 0 0
67 define_test(
68 $testName,
69 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
70 0 my ( $self, $data, $reason ) = ( shift, shift, shift ); # self is the runner, NOT the prototype
71 0 ▶ if ( $testSub->( $self->subject, $data->{expected} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$testSub($self->subject, $data->{'expected'})) { } 0 0 0
72 0 return $self->pass;
73 }
74 else {
75 0 return $self->fail;
76 }
77 },
78 0 $package
79 );
80 }
81  
82 =head3 simple_test
83  
84 simple_test 'lc_eq', sub {
85 return lc ($_[0]) eq $_[1];
86 };
87
88 ...
89
90 p->lc_eq('yes')->ok('Yes');
91  
92 Adds a test method to the class. The first argument is the name of that method, the second argument is the code to be executed - however, the code should return only a true or false value, and is passed only the test subject and the expected value, not the runner or full data.
93  
94 The test method itself takes one argument, the expected value.
95  
96 =cut
97  
98 0 ▶sub simple_test {
Subroutine simple_test: not called CC=1 SCAR=3.5
Pod documented
99 0 my ( $testName, $testSub ) = @_;
100 0 my ( $package, $filename, $line ) = caller;
101 {
102 0 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.2
103 {
104 #package $package;
105 0 define_simple_test( $testName, $testSub, $package );
106 }
107 0 my $fullName = $package . '::' . $testName;
108 *$fullName = subname(
109 $testName,
110 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
111 0 my ( $self, $expected, $reason ) = ( shift, shift, shift );
112 0 $self->add_test( $testName, { expected => $expected }, $reason );
113 }
114 0 ); # Consider Sub::Install here, per Khisanth on irc.freenode.net#perl
115 }
116 }
117  
118 =head3 upgrade
119  
120 upgrade('NONE'); # returns Test::Proto::Base->new()->eq('NONE')
121 upgrade(1); # returns Test::Proto::Base->new()->num_eq(1)
122 upgrade(['foo']); # returns Test::Proto::ArrayRef->new()->array_eq(['foo'])
123 upgrade({'foo'=>'bar'}); # returns Test::Proto::HashRef->new()->hash_of({'foo'=>'bar'})
124 upgrade(sub {return $_ * 2 == 4}); Test::Proto::Base->new()->try(...)
125  
126 Returns a Prototype which corresponds to the data in the first argument.
127  
128 If the first argument is already a prototype, this does nothing.
129  
130 Use this when you have a parameter and want to validate data against it, but you do not know if it is a prototype or 'natural data'.
131  
132 =cut
133  
134 2242 ▶sub upgrade {
Subroutine upgrade: called CC=12 SCAR=25.3
Pod documented
135 2242 my ( $expected, $noref ) = @_;
136 {
137 2242 require Test::Proto::Base;
138 2242 require Test::Proto::HashRef;
139 2242 require Test::Proto::ArrayRef;
140  
141 2242 ▶ if ( defined ref $expected ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined ref $expected) 2242 0 2242
142 2242 ▶ if ( blessed $expected) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($expected)) 1185 1057 2242
143 1185 ▶ return Test::Proto::ArrayRef->new()->array->contains_only($expected)
Branch: 1 branch
BranchTrueFalseTotal
if $expected->isa("Test::Proto::Series") or $expected->isa("Test::Proto::Repeatable") or $expected->isa("Test::Proto::Alternation") 0 1185 1185
Condition33%2/6
$expected->isa("Test::Proto::Series") or $expected->isa("Test::Proto::Repeatable") or $expected->isa("Test::Proto::Alternation")
l!l&&r!l&&!r
001185
$expected->isa("Test::Proto::Series") or $expected->isa("Test::Proto::Repeatable")
l!l&&r!l&&!r
001185
MC/DC0%0/3
$expected->isa("Test::Proto::Series") or $expected->isa("Test::Proto::Repeatable") or $expected->isa("Test::Proto::Alternation")
$expected->isa("Test::Proto::Series") $expected->isa("Test::Proto::Repeatable") $expected->isa("Test::Proto::Alternation")
Truth table25%1/4
A or B or C
A= $expected->isa("Test::Proto::Series")B= $expected->isa("Test::Proto::Repeatable")C= $expected->isa("Test::Proto::Alternation")
ABCresult
1XX1
01X1
0011
0000
144 if $expected->isa('Test::Proto::Series')
145 or $expected->isa('Test::Proto::Repeatable')
146 or $expected->isa('Test::Proto::Alternation');
147 1185 ▶ return $expected if $expected->isa('Test::Proto::Base');
Branch: 1 branch
BranchTrueFalseTotal
if $expected->isa("Test::Proto::Base") 1160 25 1185
148 }
149 1082 ▶ return Test::Proto::ArrayRef->new()->array->array_eq($expected) if ref $expected eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $expected eq "ARRAY" 108 974 1082
150 974 ▶ return Test::Proto::HashRef->new()->hash->superhash_of($expected) if ref $expected eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $expected eq "HASH" 3 971 974
151 971 ▶ return Test::Proto::Base->new()->like($expected) if ref $expected eq 'Regexp';
Branch: 1 branch
BranchTrueFalseTotal
if ref $expected eq "Regexp" 25 946 971
152 946 ▶ return Test::Proto::Base->new()->try($expected) if ref $expected eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $expected eq "CODE" 33 913 946
153 }
154 913 ▶ return Test::Proto::Base->new()->scalar->num_eq($expected) if Scalar::Util::looks_like_number($expected);
Branch: 1 branch
BranchTrueFalseTotal
if &Scalar::Util::looks_like_number($expected) 383 530 913
155  
156 #return Test::Proto::Base->new()->eq($expected) if $noref;
157 530 return Test::Proto::Base->new()->scalar->eq($expected);
158 }
159 }
160  
161 =head3 upgrade_comparison
162  
163 upgrade_comparison(sub {lc shift cmp lc shift}, 'Lowercase Comparison');
164  
165 This creates a Test::Proto::Compare object using the code provided in the first argument. The second argument, if present, is used as the summary.
166  
167 If the first argument is either of the strings 'cmp' or '<=>', it will return the appropriate string or numeric comparison, as these are special-cased.
168  
169 =cut
170  
171 60 ▶sub upgrade_comparison {
Subroutine upgrade_comparison: called CC=9 SCAR=22.1
Pod documented
172 60 require Test::Proto::Compare;
173 60 require Test::Proto::Compare::Numeric;
174 60 my ( $comparison, $summary ) = @_;
175 60 ▶ $summary = 'Unknown comparison' unless defined $summary; #:5.8
Branch: 1 branch
BranchTrueFalseTotal
unless defined $summary 59 1 60
176 60 ▶ if ( ref $comparison eq 'CODE' ) {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $comparison eq 'CODE') { } 27 33 60
elsif (builtin::blessed($comparison) and $comparison->isa('Test::Proto::Compare')) { } 3 30 33
elsif (defined $comparison and not ref $comparison) { } 2 56 58
177 27 return Test::Proto::Compare->new($comparison)->summary($summary);
178 }
179 ▶ elsif ( blessed $comparison and $comparison->isa('Test::Proto::Compare') ) {
Condition66%2/3
builtin::blessed($comparison) and $comparison->isa("Test::Proto::Compare")
!ll&&!rl&&r
3003
MC/DC50%1/2
builtin::blessed($comparison) and $comparison->isa("Test::Proto::Compare")
builtin::blessed($comparison) $comparison->isa("Test::Proto::Compare")
Truth table66%2/3
A and B
A= builtin::blessed($comparison)B= $comparison->isa("Test::Proto::Compare")
ABresult
0X0
100
111
180 3 return $comparison;
181 }
182 ▶ elsif ( defined $comparison and !ref $comparison ) {
Condition66%2/3
defined $comparison and not ref $comparison
!ll&&!rl&&r
2802
MC/DC50%1/2
defined $comparison and not ref $comparison
defined $comparison not ref $comparison
Truth table66%2/3
A and not(B)
A= defined $comparisonB= not ref $comparison
ABresult
0X0
100
111
183 2 ▶ return Test::Proto::Compare->new if $comparison eq 'cmp';
Branch: 1 branch
BranchTrueFalseTotal
if $comparison eq "cmp" 1 1 2
184 1 ▶ return Test::Proto::Compare::Numeric->new if $comparison eq '<=>';
Branch: 1 branch
BranchTrueFalseTotal
if $comparison eq "<=>" 1 0 1
185 }
186 28 return Test::Proto::Compare->new;
187 }
188  
189 =head3 chainable
190  
191 around 'attr', 'other_attr', \&Test::Proto::Common::chainable;
192  
193 ...
194  
195 $object->attr(2)->some_method;
196  
197 Use this to make a Moo attribute chainable.
198
199 =cut
200  
201 182370 ▶sub chainable {
Subroutine chainable: called CC=2 SCAR=6.9
Pod documented
202 182370 my $orig = shift;
203 182370 my $self = shift;
204 182370 ▶ if ( exists $_[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $_[0]) { } 18118 164252 182370
205  
206 #~ when setting, return self
207 18118 $orig->( $self, @_ );
208 18118 return $self;
209 }
210 else {
211 #~ when getting, return value
212 164252 return $orig->( $self, @_ );
213 }
214 }
215  
216 15 1;
↑ Summary Proto/Role/ArrayRef.pm »