blib/lib/Test/Flatten.pm

filter: Statementstmt 93.3% 112 / 120 Branchbran 71.0% 27 / 38 Conditioncond 77.7% 7 / 9 MC/DCmcdc 66.6% 4 / 6 Subroutinesub 100% 18 / 18 Podpod 100% 1 / 1 totaltotal 88.0% 169 / 192 CC 22 SCAR 19.8
CC
22
Cov
87%
__ANON__
14.0
subtest
11.0
END
2.0
CRAP 7.3
↑ Summary
1 package Test::Flatten;
2  
3 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use Test::More ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 11 ▶use Test::Builder ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 11 ▶use Term::ANSIColor qw(colored);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 11 our $VERSION = '0.11';
10  
11 11 our $BORDER_COLOR = [qw|cyan bold|];
12 11 our $BORDER_CHAR = '-';
13 11 our $BORDER_LENGTH = 78;
14 11 our $CAPTION_COLOR = ['clear'];
15 11 our $NOTE_COLOR = ['yellow'];
16  
17 11 our $ORG_SUBTEST = Test::Builder->can('subtest');
18 11 our $ORG_TEST_MORE_SUBTEST = Test::More->can('subtest');
19  
20 11 ▶$ENV{ANSI_COLORS_DISABLED} = 1 if $^O eq 'MSWin32';
Branch: 1 branch
BranchTrueFalseTotal
if $^O eq "MSWin32" 0 11 11
21  
22 11 ▶sub import {
Subroutine import: called CC=1 SCAR=0.0
23 11 my $class = caller(0);
24 11 ▶ no warnings qw(redefine prototype);
Subroutine BEGIN: called CC=1 SCAR=0.0
25 11 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
26 11 *Test::Builder::subtest = \&subtest;
27  
28 # backward campatibility
29 11 *{"$class\::subtest"} = Test::More->can('subtest');
30 }
31  
32 11 my $TEST_DIFF = 0;
33 11 ▶END {
Subroutine END: called CC=2 SCAR=7.0
34 11 ▶ if ($TEST_DIFF) {
Branch: 1 branch
BranchTrueFalseTotal
if ($TEST_DIFF) 1 10 11
35 1 my $builder = Test::More->builder;
36 1 _diag_plan($builder->{Curr_Test} - $TEST_DIFF, $builder->{Curr_Test});
37 1 Test::Builder::_my_exit(255); # report fail
38 0 undef $Test::Builder::Test; # disabled original END{} block
39 }
40 }
41  
42 16 ▶sub subtest {
Subroutine subtest: called CC=11 SCAR=24.0
Pod documented
43 16 my ($self, $caption, $test, @args) = @_;
44  
45 16 my $builder = Test::More->builder;
46 16 ▶ unless (ref $test eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $test eq "CODE") 0 16 16
47 0 $builder->croak("subtest()'s second argument must be a code ref");
48 }
49  
50 # copying original setting
51 16 my $current_test = $builder->{Curr_Test};
52 16 my $skip_all = $builder->{Skip_All};
53 16 my $have_plan = $builder->{Have_Plan};
54 16 my $no_plan = $builder->{No_Plan};
55 16 my $in_filter = $builder->{__in_filter__};
56  
57 ## this idea from http://d.hatena.ne.jp/tokuhirom/20111017/1318831330
58 16 ▶ if (my $filter = $ENV{SUBTEST_FILTER}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $filter = $ENV{"SUBTEST_FILTER"}) 2 14 16
59 2 ▶ if ($caption =~ qr{$filter} || $in_filter) {
Branch: 1 branch
BranchTrueFalseTotal
if ($caption =~ qr/$filter/ or $in_filter) { } 1 1 2
Condition66%2/3
$caption =~ qr/$filter/ or $in_filter
l!l&&r!l&&!r
101
MC/DC50%1/2
$caption =~ qr/$filter/ or $in_filter
$caption =~ qr/$filter/ $in_filter
Truth table66%2/3
A or B
A= $caption =~ qr/$filter/B= $in_filter
ABresult
1X1
011
000
60 1 $builder->{__in_filter__} = 1;
61 }
62 else {
63 1 $builder->note(colored $NOTE_COLOR, "SKIP: $caption by SUBTEST_FILTER");
64 1 return;
65 }
66 }
67  
68 15 $builder->note(colored $BORDER_COLOR, $BORDER_CHAR x $BORDER_LENGTH);
69 15 $builder->note(colored $CAPTION_COLOR, $caption);
70 15 $builder->note(colored $BORDER_COLOR, $BORDER_CHAR x $BORDER_LENGTH);
71  
72 # reset
73 15 $builder->{Have_Plan} = 0;
74  
75 11 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
76 11 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
77 15 local *{ref($builder).'::plan'} = _fake_plan(\my $tests, \my $is_skip_all);
78 15 ▶ local *{ref($builder).'::done_testing'} = sub {}; # temporary disabled
Subroutine __ANON__: called CC=1 SCAR=0.0
79  
80 11 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
81 11 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
82  
83 15 local $Test::Builder::Level = $Test::Builder::Level = 1;
84 15 my $is_passing = eval { $test->(@args); 1 };
85 14 my $e = $@;
86  
87 14 ▶ die $e if $e && !eval { $e->isa('Test::Builder::Exception') };
Branch: 1 branch
BranchTrueFalseTotal
if $e and not eval { do { $e->isa("Test::Builder::Exception") } } 3 11 14
Condition100%3/3
$e and not eval { do { $e->isa("Test::Builder::Exception") } }
!ll&&!rl&&r
1013
MC/DC100%2/2
$e and not eval { do { $e->isa("Test::Builder::Exception") } }
$e not eval { do { $e->isa("Test::Builder::Exception") } }
Truth table100%3/3
A and not(B)
A= $eB= not eval { do { $e->isa("Test::Builder::Exception") } }
ABresult
0X0
100
111
88  
89 11 ▶ if ($is_skip_all) {
Branch: 3 branches
BranchTrueFalseTotal
if ($is_skip_all) { } 1 10 11
elsif ($tests and $builder->{'Curr_Test'} != $current_test + $tests) { } 2 8 10
elsif ($builder->{'Curr_Test'} == $current_test) { } 8 0 8
90 1 $builder->{Skip_All} = $skip_all;
91 }
92 ▶ elsif ($tests && $builder->{Curr_Test} != $current_test + $tests) {
Condition66%2/3
$tests and $builder->{"Curr_Test"} != $current_test + $tests
!ll&&!rl&&r
802
MC/DC50%1/2
$tests and $builder->{"Curr_Test"} != $current_test + $tests
$tests $builder->{"Curr_Test"} != $current_test + $tests
Truth table66%2/3
A and B
A= $testsB= $builder->{"Curr_Test"} != $current_test + $tests
ABresult
0X0
100
111
93 2 _diag_plan($tests, $builder->{Curr_Test} - $current_test);
94 2 $TEST_DIFF = $builder->{Curr_Test} - $current_test - $tests;
95 2 $is_passing = $builder->is_passing(0);
96 }
97 elsif ($builder->{Curr_Test} == $current_test) {
98 8 $builder->croak("No tests run for subtest $caption");
99 }
100  
101 # restore
102 3 $builder->{Have_Plan} = $have_plan;
103 3 $builder->{No_Plan} = $no_plan;
104 3 $builder->{__in_filter__} = $in_filter;
105  
106 3 return $is_passing;
107 }
108  
109 3 ▶sub _diag_plan {
Subroutine _diag_plan: called CC=2 SCAR=6.9
110 3 my ($plan, $ran) = @_;
111 3 ▶ my $s = $plan == 1 ? '' : 's';
Branch: 1 branch
BranchTrueFalseTotal
$plan == 1 ? : 1 2 3
112 3 Test::More->builder->diag(sprintf 'Looks like you planned %d test%s but ran %d.',
113 $plan, $s, $ran,
114 );
115 }
116  
117 15 ▶sub _fake_plan {
Subroutine _fake_plan: called CC=1 SCAR=0.3
118 15 my ($tests, $is_skip_all) = @_;
119  
120 4 ▶ return sub {
Subroutine __ANON__: called CC=10 SCAR=26.4
121 4 my ($self, $cmd, $arg) = @_;
122 4 ▶ return unless $cmd;
Branch: 1 branch
BranchTrueFalseTotal
unless $cmd 0 4 4
123 4 local $Test::Builder::Level = $Test::Builder::Level + 2;
124 4 ▶ $self->croak("You tried to plan twice") if $self->{Have_Plan};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"Have_Plan"} 0 4 4
125  
126 4 ▶ if ($cmd eq 'no_plan') {
Branch: 3 branches
BranchTrueFalseTotal
if ($cmd eq 'no_plan') { } 1 3 4
elsif ($cmd eq 'skip_all') { } 1 2 3
elsif ($cmd eq 'tests') { } 2 0 2
127 1 local $Test::Builder::Level = $Test::Builder::Level + 1;
128 1 $self->no_plan($arg);
129 }
130 elsif ($cmd eq 'skip_all') {
131 1 local $Test::Builder::Level = $Test::Builder::Level + 1;
132 1 $self->{Skip_All} = 1;
133 1 ▶ $self->note(join q{ }, 'SKIP:', $arg) unless $self->no_header;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->no_header 1 0 1
134 1 $$is_skip_all = 1; # set flag
135 1 die bless {}, 'Test::Builder::Exception';
136 }
137 elsif ($cmd eq 'tests') {
138 2 ▶ if($arg) {
Branch: 2 branches
BranchTrueFalseTotal
if ($arg) { } 2 0 2
elsif (not defined $arg) { } 0 0 0
139 2 local $Test::Builder::Level = $Test::Builder::Level + 1;
140 2 ▶ unless ($arg =~ /^\+?\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($arg =~ /^\+?\d+$/) 0 2 2
141 0 $self->croak("Number of tests must be a positive integer. You gave it '$arg'");
142 }
143 2 $$tests = $arg; # set tests
144 }
145 elsif( !defined $arg ) {
146 0 $self->croak("Got an undefined number of tests");
147 }
148 else {
149 0 $self->croak("You said to run 0 tests");
150 }
151 }
152 else {
153 0 my @args = grep { defined } ( $cmd, $arg );
154 0 $self->croak("plan() doesn't understand @args");
155 }
156 3 return 1;
157 15 };
158 }
159  
160 11 1;
161 __END__
↑ Summary