DataDriven/Plugin.pm

filter: Statementstmt 99.0% 101 / 102 Branchbran 77.2% 17 / 22 Conditioncond 50.0% 13 / 26 MC/DCmcdc 26.6% 4 / 15 Subroutinesub 100% 18 / 18 Podpod 83.3% 5 / 6 totaltotal 83.6% 158 / 189 CC 27 SCAR 16.3
CC
27
Cov
87%
register
9.0
_apply_filter
7.8
_dispatch
6.4
CRAP 5.1
↑ Summary DataDriven.pm »
1 package Test::DataDriven::Plugin;
2  
3 =head1 NAME
4  
5 Test::DataDriven::Plugin - when Test::Base is not enough
6  
7 =head1 SYNOPSIS
8  
9 See C<Test::DataDriven>
10  
11 =cut
12  
13 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 9 ▶use Class::Spiffy -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 9 ▶use Test::DataDriven ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 9 our @EXPORT = qw(test_name);
20  
21 9 my %attributes;
22 my %dispatch;
23  
24 =head1 METHODS
25  
26 =cut
27  
28 77 ▶sub MODIFY_CODE_ATTRIBUTES {
Subroutine MODIFY_CODE_ATTRIBUTES: called CC=3 SCAR=11.0
29 77 my( $class, $code, @attrs ) = @_;
30 77 my( @known, @unknown );
31  
32 77 foreach ( @attrs ) {
33 98 ▶ /^(?:Begin|Run|End|Endc|Filter)\s*(?:$|\()/ ?
Branch: 1 branch
BranchTrueFalseTotal
/^(?:Begin|Run|End|Endc|Filter)\s*(?:$|\()/ ? : 98 0 98
34 push @known, $_ : push @unknown, $_;
35 }
36  
37 77 $attributes{$class}{$code} = [ $code, \@known ];
38  
39 77 return @unknown;
40 }
41  
42 9 our $test_name;
43  
44 =head2 test_name
45  
46 my $test_name = test_name();
47  
48 This function is exported by default. The test name is
49 "$block - $action - $section".b
50  
51 =cut
52  
53 7 ▶sub test_name() { $test_name }
Subroutine test_name: called CC=1 SCAR=0.0
Pod documented
54  
55 77 ▶sub _parse {
Subroutine _parse: called CC=2 SCAR=7.0
56 77 my( @attributes ) = @_;
57  
58 77 ▶ return map { m/^(\w+)\(\s*(\w+)\s*\)/ or die $_;
Branch: 1 branch
BranchTrueFalseTotal
/^(\w+)\(\s*(\w+)\s*\)/ or die $_ 98 0 98
59 98 [ lc( $1 ), $2 ]
60 }
61 @attributes;
62 }
63  
64 =head2 register
65  
66 __PACKAGE__->register;
67  
68 This method must be called by every C<Test::DataDriven::Plugin>
69 subclass in order to register the section handlers with
70 C<Test::DataDriven>.
71  
72 =cut
73  
74 3 ▶sub _apply_filter {
Subroutine _apply_filter: called CC=7 SCAR=20.6
75 3 my( $self, $filter, @value ) = @_;
76 3 local $_;
77 # cut'n'pasted from Test::Base (this sucks)
78 3 ▶ $Test::Base::Filter::arguments =
Branch: 1 branch
BranchTrueFalseTotal
$filter =~ s/=(.*)$// ? : 0 3 3
79 $filter =~ s/=(.*)$// ? $1 : undef;
80 3 my $function = "main::$filter";
81 9 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
82 3 ▶ if (defined &$function) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined &$function) { } 1 2 3
83 1 $_ = join '', @value;
84 1 @value = &$function(@value);
85 1 ▶ if (not(@value) or
Branch: 1 branch
BranchTrueFalseTotal
if (not @value or @value == 1 and $value[0] =~ /\A(\d+|)\z/) 0 1 1
Condition33%2/6
not @value or @value == 1 and $value[0] =~ /\A(\d+|)\z/
l!l&&r!l&&!r
001
@value == 1 and $value[0] =~ /\A(\d+|)\z/
!ll&&!rl&&r
100
MC/DC0%0/3
not @value or @value == 1 and $value[0] =~ /\A(\d+|)\z/
not @value @value == 1 $value[0] =~ /\A(\d+|)\z/
Truth table25%1/4
not(A) or B and C
A= not @valueB= @value == 1C= $value[0] =~ /\A(\d+|)\z/
ABCresult
1XX1
0111
00X0
0100
86 @value == 1 and $value[0] =~ /\A(\d+|)\z/
87 ) {
88 0 @value = ($_);
89 }
90 }
91 else {
92 2 my $filter_object = $self->blocks_object->filter_class->new;
93 2 ▶ die "Can't find a function or method for '$filter' filter\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $filter_object->can($filter) 0 2 2
94 unless $filter_object->can($filter);
95 2 $filter_object->current_block($self);
96 2 @value = $filter_object->$filter(@value);
97 }
98  
99 3 return @value;
100 }
101  
102 7 ▶sub register {
Subroutine register: called CC=9 SCAR=22.0
Pod documented
103 7 my( $self, $pluggable ) = @_;
104 7 ▶ my $class = ref( $self ) || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
070
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
105 7 my @attributes = values %{$attributes{$class}};
106 7 my %keys;
107  
108 7 foreach my $attr ( @attributes ) {
109 77 my( $sub, $attrs ) = @$attr;
110 77 my @parsed = _parse @$attrs;
111 # filter subroutines
112 77 ▶ if( @parsed == 1 && $parsed[0][0] eq 'filter' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@parsed == 1 and $parsed[0][0] eq "filter") 14 63 77
Condition100%3/3
@parsed == 1 and $parsed[0][0] eq "filter"
!ll&&!rl&&r
75614
MC/DC100%2/2
@parsed == 1 and $parsed[0][0] eq "filter"
@parsed == 1 $parsed[0][0] eq "filter"
Truth table100%3/3
A and B
A= @parsed == 1B= $parsed[0][0] eq "filter"
ABresult
0X0
100
111
113 9 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
114 14 *{'main::' . $parsed[0][1]} = $sub;
115 14 next;
116 }
117 # handle per-subroutine filters
118 63 foreach my $h ( grep $_->[0] eq 'filter', reverse @parsed ) {
119 21 my( $oldsub, $filter ) = ( $sub, $h->[1] );
120 3 ▶ $sub = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
121 3 my( $block, $section, @a ) = @_;
122 3 @a = _apply_filter( $block, $filter, @a );
123 3 &$oldsub( $block, $section, @a );
124 21 };
125 }
126 # handle begin/run/end
127 63 foreach my $h ( grep $_->[0] ne 'filter', @parsed ) {
128 63 $keys{$h->[1]} = 1;
129 63 push @{$dispatch{$class}{$h->[0]}{$h->[1]}}, $sub;
130 }
131 }
132  
133 7 ▶ $pluggable ||= 'Test::DataDriven';
Condition50%1/2
$pluggable ||= "Test::DataDriven"
l!l
07
MC/DC0%0/1
$pluggable ||= "Test::DataDriven"
$pluggable
Truth table50%1/2
A
A= $pluggable
Aresult
11
00
134 7 foreach my $key ( keys %keys ) {
135 49 $pluggable->register( plugin => $self,
136 tag => $key,
137 );
138 }
139 }
140  
141 87 ▶sub _dispatch {
Subroutine _dispatch: called CC=6 SCAR=18.6
142 87 my( $act, $self, $block, $section, @a ) = @_;
143 87 ▶ my $class = ref( $self ) || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
0870
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
144  
145 return unless exists $dispatch{$class}
146 && exists $dispatch{$class}{$act}
147 87 ▶ && exists $dispatch{$class}{$act}{$section};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $dispatch{$class} and exists $dispatch{$class}{$act} and exists $dispatch{$class}{$act}{$section} 53 34 87
Condition50%3/6
exists $dispatch{$class} and exists $dispatch{$class}{$act} and exists $dispatch{$class}{$act}{$section}
!ll&&!rl&&r
05334
exists $dispatch{$class} and exists $dispatch{$class}{$act}
!ll&&!rl&&r
0087
MC/DC33%1/3
exists $dispatch{$class} and exists $dispatch{$class}{$act} and exists $dispatch{$class}{$act}{$section}
exists $dispatch{$class} exists $dispatch{$class}{$act} exists $dispatch{$class}{$act}{$section}
Truth table50%2/4
A and B and C
A= exists $dispatch{$class}B= exists $dispatch{$class}{$act}C= exists $dispatch{$class}{$act}{$section}
ABCresult
0XX0
10X0
1100
1111
148  
149 34 local $Test::Builder::Level = 1;
150 34 local $test_name = join ' - ', $block->name, $act, $section;
151  
152 34 my $run_one = 0;
153 34 foreach my $sub ( @{$dispatch{$class}{$act}{$section}} ) {
154 34 &$sub( $block, $section, @a );
155 34 $run_one = 1;
156 }
157  
158 34 return $run_one;
159 }
160  
161 =head2 begin, run, end
162  
163 Dispatch to the subroutines registered with attributes
164 C<Begin()>, C<Run()>, C<End()>, passing as parameters
165 the block object, section name and the section data.
166  
167 =cut
168  
169 29 ▶sub begin { _dispatch( 'begin', @_ ); }
Subroutine begin: called CC=1 SCAR=0.0
Pod documented
170 29 ▶sub run { _dispatch( 'run', @_ ); }
Subroutine run: called CC=1 SCAR=0.0
Pod documented
171 24 ▶sub end { _dispatch( 'end', @_ ); }
Subroutine end: called CC=1 SCAR=0.0
Pod documented
172  
173 5 ▶sub endc {
Subroutine endc: called CC=1 SCAR=0.0
Pod undocumented
174 5 my( $self, $block, $section, @v ) = @_;
175  
176 5 _dispatch( 'endc', @_ );
177 5 _serialize_back( @_ );
178 }
179  
180 9 my %started;
181  
182 5 ▶sub _serialize_back {
Subroutine _serialize_back: called CC=5 SCAR=16.1
183 5 my( $self, $block, $section, @v ) = @_;
184 5 my $create_fh = Test::DataDriven->_create_fh;
185  
186 5 ▶ print $create_fh "=== ", $block->name, "\n" unless $started{$block};
Branch: 1 branch
BranchTrueFalseTotal
unless $started{$block} 2 3 5
187 5 ▶ if( defined $block->description && $block->description ne $block->name ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $block->description and $block->description ne $block->name) 1 4 5
Condition66%2/3
defined $block->description and $block->description ne $block->name
!ll&&!rl&&r
041
MC/DC50%1/2
defined $block->description and $block->description ne $block->name
defined $block->description $block->description ne $block->name
Truth table66%2/3
A and B
A= defined $block->descriptionB= $block->description ne $block->name
ABresult
0X0
100
111
188 1 print $create_fh $block->description , "\n" ;
189 }
190 5 print $create_fh "--- ", $section;
191 5 my $filters = $block->_section_map->{$section}{filters};
192 5 ▶ if( $filters ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filters) 4 1 5
193 4 print $create_fh ' ', $filters;
194 }
195 5 print $create_fh "\n";
196 5 print $create_fh $block->original_values->{$section};
197  
198 5 $started{$block} = 1;
199 }
200  
201 =head1 BUGS
202  
203 Needs more documentation and examples.
204  
205 =head1 AUTHOR
206  
207 Mattia Barbon <mbarbon@cpan.org>
208  
209 =head1 LICENSE
210  
211 This program is free software; you can redistribute it and/or
212 modify it under the same terms as Perl itself.
213  
214 =cut
215  
216 9 1;
↑ Summary DataDriven.pm »