lib/Generic/Assertions.pm

filter: Statementstmt 97.0% 99 / 102 Branchbran 91.1% 31 / 34 Conditioncond 100% 5 / 5 MC/DCmcdc 100% 3 / 3 Subroutinesub 100% 25 / 25 Podpod 50.0% 1 / 2 totaltotal 95.9% 164 / 171 CC 23 SCAR 10.0
CC
23
Cov
96%
BUILD
6.0
new
4.0
_tests
4.0
CRAP 2.7
« blib/lib/Generic/Assertions.pm ↑ Summary
1 7 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package Generic::Assertions;
6  
7 7 our $VERSION = '0.001001';
8  
9 # ABSTRACT: A Generic Assertion checking class
10  
11 7 our $AUTHORITY = 'cpan:KENTNL'; # AUTHORITY
12  
13 7 ▶use Carp qw( croak carp );
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 sub new {
16 50 ▶ my ( $class, @args ) = @_;
Subroutine new: called CC=4 SCAR=13.9
Pod documented
17 50 ▶ if ( @args % 2 == 1 and not ref $args[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args % 2 == 1 and not ref $args[0]) 2 48 50
Condition100%3/3
@args % 2 == 1 and not ref $args[0]
!ll&&!rl&&r
4352
MC/DC100%2/2
@args % 2 == 1 and not ref $args[0]
@args % 2 == 1 not ref $args[0]
Truth table100%3/3
A and not(B)
A= @args % 2 == 1B= not ref $args[0]
ABresult
0X0
100
111
18 2 croak '->new() expects even number of arguments or a hash reference, got ' . scalar @args . ' argument(s)';
19 }
20 48 my $hash;
21 48 ▶ if ( ref $args[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args[0]) { } 5 43 48
22 5 $hash = { args => $args[0] };
23 }
24 else {
25 43 $hash = { args => {@args} };
26 }
27 48 my $self = bless $hash, $class;
28 48 $self->BUILD;
29 43 return $self;
30 }
31  
32  
33  
34  
35  
36 sub BUILD {
37 48 ▶ my ($self) = @_;
Subroutine BUILD: called CC=6 SCAR=17.9
Pod undocumented
38 48 my $tests = $self->_tests;
39 48 for my $test ( keys %{$tests} ) {
40 31 ▶ croak 'test ' . $test . ' must be a CodeRef' if not 'CODE' eq ref $tests->{$test};
Branch: 1 branch
BranchTrueFalseTotal
unless "CODE" eq ref $tests->{$test} 3 28 31
41 }
42 45 my $handlers = $self->_handlers;
43 45 for my $handler ( keys %{$handlers} ) {
44 270 ▶ croak 'handler ' . $handler . ' must be a CodeRef' if not 'CODE' eq ref $handlers->{$handler};
Branch: 1 branch
BranchTrueFalseTotal
unless "CODE" eq ref $handlers->{$handler} 2 268 270
45 }
46 43 ▶ croak 'input_transformer must be a CodeRef' if not 'CODE' eq ref $self->_input_transformer;
Branch: 1 branch
BranchTrueFalseTotal
unless "CODE" eq ref $self->_input_transformer 0 43 43
47 43 return;
48 }
49  
50 sub _args {
51 223 ▶ my ($self) = @_;
Subroutine _args: called CC=2 SCAR=8.1
52 223 ▶ return $self->{args} if exists $self->{args};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"args"} 223 0 223
53 0 return ( $self->{args} = {} );
54 }
55  
56 sub _tests {
57 76 ▶ my ( $self, ) = @_;
Subroutine _tests: called CC=4 SCAR=13.9
58 76 ▶ return $self->{tests} if exists $self->{tests};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"tests"} 28 48 76
59 48 my %tests;
60 48 for my $key ( grep { !/\A-/msx } keys %{ $self->_args } ) {
61 27 $tests{$key} = $self->_args->{$key};
62 }
63 48 ▶ return ( $self->{tests} = { %tests, %{ $self->_args->{'-tests'} || {} } } );
Branch: 1 branch
BranchTrueFalseTotal
$self->_args->{"-tests"} or {} 44 4 48
64 }
65  
66 sub _handlers {
67 83 ▶ my ( $self, ) = @_;
Subroutine _handlers: called CC=3 SCAR=11.0
68 83 ▶ return $self->{handlers} if exists $self->{handlers};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"handlers"} 38 45 83
69 45 ▶ return ( $self->{handlers} = { %{ $self->_handler_defaults }, %{ $self->_args->{'-handlers'} || {} } } );
Branch: 1 branch
BranchTrueFalseTotal
$self->_args->{"-handlers"} or {} 42 3 45
70 }
71  
72 sub _handler_defaults {
73 return {
74 test => sub {
75 6 ▶ my ($status) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
76 6 return $status;
77 },
78 log => sub {
79 6 ▶ my ( $status, $message, $name, @slurpy ) = @_;
Subroutine __ANON__: called CC=2 SCAR=6.9
80 6 ▶ carp sprintf 'Assertion < log %s > = %s : %s', $name, ( $status || '0' ), $message;
Condition100%2/2
$status || '0'
l!l
33
MC/DC100%1/1
$status || '0'
$status
Truth table100%2/2
A
A= $status
Aresult
11
00
81 6 return $slurpy[0];
82 },
83 should => sub {
84 6 ▶ my ( $status, $message, $name, @slurpy ) = @_;
Subroutine __ANON__: called CC=2 SCAR=6.9
85 6 ▶ carp "Assertion < should $name > failed: $message" unless $status;
Branch: 1 branch
BranchTrueFalseTotal
unless $status 3 3 6
86 6 return $slurpy[0];
87 },
88 should_not => sub {
89 6 ▶ my ( $status, $message, $name, @slurpy ) = @_;
Subroutine __ANON__: called CC=2 SCAR=6.9
90 6 ▶ carp "Assertion < should_not $name > failed: $message" if $status;
Branch: 1 branch
BranchTrueFalseTotal
if $status 3 3 6
91 6 return $slurpy[0];
92 },
93 must => sub {
94 6 ▶ my ( $status, $message, $name, @slurpy ) = @_;
Subroutine __ANON__: called CC=2 SCAR=6.9
95 6 ▶ croak "Assertion < must $name > failed: $message" unless $status;
Branch: 1 branch
BranchTrueFalseTotal
unless $status 3 3 6
96 3 return $slurpy[0];
97 },
98 must_not => sub {
99 6 ▶ my ( $status, $message, $name, @slurpy ) = @_;
Subroutine __ANON__: called CC=2 SCAR=6.9
100 6 ▶ croak "Assertion < must_not $name > failed: $message" if $status;
Branch: 1 branch
BranchTrueFalseTotal
if $status 3 3 6
101 3 return $slurpy[0];
102 },
103 45 ▶ };
Subroutine _handler_defaults: called CC=1 SCAR=0.0
104 }
105  
106 sub _transform_input {
107 24 ▶ my ( $self, $name, @slurpy ) = @_;
Subroutine _transform_input: called CC=1 SCAR=0.0
108 24 return $self->_input_transformer->( $name, @slurpy );
109 }
110  
111 sub _input_transformer {
112 67 ▶ my ( $self, ) = @_;
Subroutine _input_transformer: called CC=3 SCAR=11.0
113 67 ▶ return $self->{input_transformer} if exists $self->{input_transformer};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"input_transformer"} 24 43 67
114 43 ▶ if ( exists $self->_args->{'-input_transformer'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->_args->{"-input_transformer"}) 12 31 43
115 12 return ( $self->{input_transformer} = $self->_args->{'-input_transformer'} );
116 }
117 31 return ( $self->{input_transformer} = $self->_input_transformer_default );
118 }
119  
120 sub _input_transformer_default {
121 12 ▶ return sub { shift; return @_ };
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine _input_transformer_default: called CC=1 SCAR=0.0
122 }
123  
124 # Dispatch the result of test name $test_name
125 sub _handle { ## no critic (Subroutines::ProhibitManyArgs)
126 36 ▶ my ( $self, $handler_name, $status_code, $message, $test_name, @slurpy ) = @_;
Subroutine _handle: called CC=1 SCAR=0.0
127 36 return $self->_handlers->{$handler_name}->( $status_code, $message, $test_name, @slurpy );
128 }
129  
130 # Perform $test_name and return its result
131 sub _test {
132 28 ▶ my ( $self, $test_name, @slurpy ) = @_;
Subroutine _test: called CC=2 SCAR=7.9
133 28 my $tests = $self->_tests;
134 28 ▶ if ( not exists $tests->{$test_name} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $tests->{$test_name}) 0 28 28
135 0 croak sprintf q[INVALID ASSERTION %s ( avail: %s )], $test_name, ( join q[,], keys %{$tests} );
136 }
137 28 return $tests->{$test_name}->(@slurpy);
138 }
139  
140 # Long form
141 # ->_assert( should => exist => path('./foo'))
142 # ->should( exist => path('./foo'))
143 sub _assert {
144 24 ▶ my ( $self, $handler_name, $test_name, @slurpy ) = @_;
Subroutine _assert: called CC=1 SCAR=0.0
145 24 my (@input) = $self->_transform_input( $test_name, @slurpy );
146 24 my ( $status, $message ) = $self->_test( $test_name, @input );
147 24 return $self->_handle( $handler_name, $status, $message, $test_name, @input );
148 }
149  
150 7 for my $handler (qw( should must should_not must_not test log )) {
151 my $code = sub {
152 24 ▶ my ( $self, $name, @slurpy ) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
153 24 return $self->_assert( $handler, $name, @slurpy );
154 42 };
155 {
156 ## no critic (TestingAndDebugging::ProhibitNoStrict])
157 7 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
158 42 *{ __PACKAGE__ . q[::] . $handler } = $code;
159 }
160 }
161  
162 7 1;
163  
164 __END__
« blib/lib/Generic/Assertions.pm ↑ Summary