Stenciller/Wrap.pm

filter: Statementstmt 94.8% 237 / 250 Branchbran 65.6% 67 / 102 Conditioncond 59.5% 25 / 42 MC/DCmcdc 8.7% 2 / 23 Subroutinesub 97.1% 34 / 35 Podpod n/a totaltotal 80.7% 365 / 452 SCAR 44.5
CC
66
Cov
84%
parse
28.3
:around
17.3
transform
16.2
CRAP 85.6
↑ Summary Stenciller/Stencil.pm »
1 7 ▶use Stenciller::Standard;
Subroutine __ANON__: called CC=1 SCAR=0.0
2 7 ▶use Stenciller::Stencil;
Subroutine __ANON__: called CC=1 SCAR=0.0
3 7 ▶use strict;
Subroutine __ANON__: called CC=1 SCAR=0.0
4 7 ▶use warnings;
Subroutine __ANON__: called CC=1 SCAR=0.0
5  
6 # PODNAME: Stenciller::Wrap
7  
8 7 ▶use Module::Pluggable search_path => ['Stenciller::Plugin'];
Subroutine __ANON__: called CC=1 SCAR=0.0
9 7  
10 7 ▶class Stenciller::Wrap using Moose with Stenciller::Utils {
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine __ANON__: called CC=1 SCAR=0.0
11  
12 7 our $VERSION = '0.1302'; # VERSION
13  
14 7 ▶ use Module::Load;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 7 ▶ use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 7 has filepath => (
18 is => 'ro',
19 isa => File,
20 required => 1,
21 coerce => 1,
22 documentation => 'The textfile to parse.',
23 );
24 7 has is_utf8 => (
25 is => 'ro',
26 isa => Bool,
27 default => 1,
28 documentation => 'Determines how the stencil file is read.'
29 );
30 has stencils => (
31 is => 'ro',
32 isa => ArrayRef[Stencil],
33 traits => ['Array'],
34 7 ▶ default => sub { [ ] },
Subroutine __ANON__: called CC=1 SCAR=0.0
35 7 documentation => 'After parsing, this contains all parsed stencils.',
36 init_arg => undef,
37 handles => {
38 add_stencil => 'push',
39 all_stencils => 'elements',
40 get_stencil => 'get',
41 count_stencils => 'count',
42 has_stencils => 'count',
43 },
44 );
45 has header_lines => (
46 is => 'ro',
47 isa => ArrayRef[Str],
48 traits => ['Array'],
49 7 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
50 7 init_arg => undef,
51 documentation => 'After parsing, this contains all lines in the header.',
52 handles => {
53 add_header_line => 'push',
54 all_header_lines => 'elements',
55 },
56 );
57 7 has skip_if_input_empty => (
58 is => 'ro',
59 isa => Bool,
60 default => 1,
61 documentation => 'If a stencil has no input content, skip entire stencil.',
62 );
63 7 has skip_if_output_empty => (
64 is => 'ro',
65 isa => Bool,
66 default => 1,
67 documentation => 'If a stencil has no output content, skip entire stencil.',
68 );
69  
70 7 ▶ around BUILDARGS($next: $class, @args) {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine :around: called CC=7 SCAR=21.8
Branch: 4 branches
BranchTrueFalseTotal
@_ ? : 7 0 7
if @_ < 1 0 7 7
$#_ >= 0 ? : 7 0 7
$#_ >= 1 ? : 7 0 7
71 7 ▶ if(scalar @args == 1 && ref $args[0] eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @args == 1 and ref $args[0] eq 'HASH') { } 0 7 7
Condition33%1/3
scalar @args == 1 and ref $args[0] eq "HASH"
!ll&&!rl&&r
700
MC/DC0%0/2
scalar @args == 1 and ref $args[0] eq "HASH"
scalar @args == 1 ref $args[0] eq "HASH"
Truth table33%1/3
A and B
A= scalar @args == 1B= ref $args[0] eq "HASH"
ABresult
0X0
100
111
72 0 $class->$next(%{ $args[0] });
73 }
74 else {
75 7 $class->$next(@args);
76 }
77 }
78  
79 7 ▶ method BUILD {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BUILD: called CC=2 SCAR=7.0
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 7 0 7
80 7 $self->parse;
81 }
82 7 ▶ around has_stencils($next: $self) {
Subroutine BEGIN: called CC=1 SCAR=3.3
Subroutine :around: not called CC=5 SCAR=28.5
Branch: 3 branches
BranchTrueFalseTotal
@_ ? : 0 0 0
if @_ != 1 0 0 0
$#_ >= 0 ? : 0 0 0
83 0 my $count = $self->$next;
84 0 ▶ return !!$count || 0;
Condition0%0/2
!!$count || 0
l!l
00
MC/DC0%0/1
!!$count || 0
!!$count
Truth table0%0/2
A
A= !!$count
Aresult
11
00
85 }
86  
87 method transform(Str :$plugin_name! does doc('Plugin to read contents with.'),
88 HashRef :$constructor_args? does doc('Constructor arguments for the plugin.') = {},
89 HashRef :$transform_args? does doc('Settings for the specific transformation.') = {}, ...
90 --> Str but assumed does doc('The transformed content.')
91 7 ▶ ) {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine transform: called CC=15 SCAR=27.9
Branch: 11 branches
BranchTrueFalseTotal
@_ ? : 7 0 7
if ($#_ == 0 and ref $_[0] eq 'HASH') { } 0 7 7
if ($slice_length % 2 != 0) 0 7 7
if ($i <= $#_) 12 7 19
exists $_{'plugin_name'} ? : 7 0 7
if (exists $_{"plugin_name"}) 7 0 7
Type::Tiny::XS::Str($plugin_name) or _failed_check(9, "Str", $plugin_name, "exception_class", "Error::TypeTiny::Assertion") 0 7 7
exists $_{'constructor_args'} ? : 2 5 7
Ref::Util::XS::is_plain_hashref($constructor_args) or _failed_check(22, "HashRef", $constructor_args, "exception_class", "Error::TypeTiny::Assertion") 0 7 7
exists $_{'transform_args'} ? : 3 4 7
Ref::Util::XS::is_plain_hashref($transform_args) or _failed_check(22, "HashRef", $transform_args, "exception_class", "Error::TypeTiny::Assertion") 0 7 7
Condition33%1/3
$#_ == 0 and ref $_[0] eq "HASH"
!ll&&!rl&&r
700
MC/DC0%0/2
$#_ == 0 and ref $_[0] eq "HASH"
$#_ == 0 ref $_[0] eq "HASH"
Truth table33%1/3
A and B
A= $#_ == 0B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
92  
93 7 my $plugin_class = "Stenciller::Plugin::$plugin_name";
94 7 Module::Load::load($plugin_class);
95 7 ▶ die sprintf "Can't load %s: %s", $plugin_class, $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 7 7
96  
97 7 ▶ if(!$plugin_class->does('Stenciller::Transformer')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($plugin_class->does("Stenciller::Transformer")) 0 7 7
98 0 croak("[$plugin_name] doesn't do the Stenciller::Transformer role. Quitting.");
99 }
100 7 return $plugin_class->new(stenciller => $self, %{ $constructor_args })->transform($transform_args);
101 }
102  
103 7 ▶ method parse {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine parse: called CC=27 SCAR=33.4
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 7 0 7
104 7 ▶ my @contents = split /\v/ => $self->is_utf8 ? $self->filepath->slurp_utf8 : $self->filepath->slurp;
Branch: 1 branch
BranchTrueFalseTotal
$self->is_utf8 ? : 7 0 7
105  
106 7 my $stencil_start = qr/^== +stencil +(\{.*\} +)?==$/;
107 7 my $input_start = qr/^--+input--+$/;
108 7 my $input_end = qr/^--+end input--+$/;
109 7 my $output_start = qr/^--+output--+$/;
110 7 my $output_end = qr/^--+end output--+$/;
111  
112 7 my $environment = 'header';
113 7 my $line_count = 0;
114  
115 7 my $stencil = undef;
116  
117 LINE:
118 7 foreach my $line (@contents) {
119 276 ▶ ++$line_count if $environment ne 'next_stencil'; # because then we are redo-ing the line
Branch: 1 branch
BranchTrueFalseTotal
if $environment ne "next_stencil" 268 8 276
120  
121 276 ▶ if(any { $environment eq $_ } (qw/header next_stencil/)) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 6 branches
BranchTrueFalseTotal
if (&any(sub { $environment eq $_; } , 'header', 'next_stencil')) { } 36 240 276
elsif ($environment eq 'before_input') { } 56 184 240
elsif ($environment eq 'input') { } 56 128 184
elsif ($environment eq 'between') { } 53 75 128
elsif ($environment eq 'output') { } 56 19 75
elsif ($environment eq 'after_output') { } 19 0 19
122 36 ▶ $self->add_header_line($line) and next LINE if $line !~ $stencil_start;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /$stencil_start/u 21 15 36
Condition75%3/4
$line =~ /$stencil_start/u || $self->add_header_line($line) && (next LINE)
l!l
1521
$self->add_header_line($line) and next LINE
!ll
021
MC/DC0%0/2
$line =~ /$stencil_start/u || $self->add_header_line($line) && (next LINE)
$line =~ /$stencil_start/u $self->add_header_line($line)
Truth table66%2/3
A || B
A= $line =~ /$stencil_start/uB= $self->add_header_line($line)
ABresult
1X1
011
000
123  
124 15 my $possible_hash = $1;
125 15 ▶ my $settings = defined $possible_hash && $possible_hash =~ m{\{.*\}}
Branch: 1 branch
BranchTrueFalseTotal
defined $possible_hash && $possible_hash =~ /\{.*\}/u ? : 11 4 15
Condition66%2/3
defined $possible_hash and $possible_hash =~ /\{.*\}/u
!ll&&!rl&&r
4011
MC/DC50%1/2
defined $possible_hash and $possible_hash =~ /\{.*\}/u
defined $possible_hash $possible_hash =~ /\{.*\}/u
Truth table66%2/3
A and B
A= defined $possible_hashB= $possible_hash =~ /\{.*\}/u
ABresult
0X0
100
111
126 ? $self->eval_to_hashref($possible_hash, $self->filepath)
127 : {}
128 ;
129  
130 15 ▶ my $stencil_name = exists $settings->{'name'} ? delete $settings->{'name'} : $self->filepath->basename(qr/\..*/) . "-$line_count";
Branch: 1 branch
BranchTrueFalseTotal
exists $settings->{'name'} ? : 3 12 15
131 15 $stencil_name =~ s{[. -]}{_}g;
132 15 $stencil_name =~ s{[^a-zA-Z0-9_]}{}g;
133  
134 $stencil = Stenciller::Stencil->new(
135 stencil_name => $stencil_name,
136 loop_values => delete $settings->{'loop'},
137 line_number => $line_count,
138 maybe skip => delete $settings->{'skip'},
139 15 provided scalar keys %{ $settings }, extra_settings => $settings,
140 );
141 15 $environment = 'before_input';
142 }
143 elsif($environment eq 'before_input') {
144 56 ▶ $stencil->add_before_input($line) and next LINE if $line !~ $input_start;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /$input_start/u 41 15 56
Condition75%3/4
$line =~ /$input_start/u || $stencil->add_before_input($line) && (next LINE)
l!l
1541
$stencil->add_before_input($line) and next LINE
!ll
041
MC/DC0%0/2
$line =~ /$input_start/u || $stencil->add_before_input($line) && (next LINE)
$line =~ /$input_start/u $stencil->add_before_input($line)
Truth table66%2/3
A || B
A= $line =~ /$input_start/uB= $stencil->add_before_input($line)
ABresult
1X1
011
000
145 15 $environment = 'input';
146 }
147 elsif($environment eq 'input') {
148 56 ▶ $stencil->add_input($line) and next LINE if $line !~ $input_end;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /$input_end/u 41 15 56
Condition75%3/4
$line =~ /$input_end/u || $stencil->add_input($line) && (next LINE)
l!l
1541
$stencil->add_input($line) and next LINE
!ll
041
MC/DC0%0/2
$line =~ /$input_end/u || $stencil->add_input($line) && (next LINE)
$line =~ /$input_end/u $stencil->add_input($line)
Truth table66%2/3
A || B
A= $line =~ /$input_end/uB= $stencil->add_input($line)
ABresult
1X1
011
000
149 15 $environment = 'between';
150 }
151 elsif($environment eq 'between') {
152 53 ▶ $stencil->add_between($line) and next LINE if $line !~ $output_start;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /$output_start/u 38 15 53
Condition75%3/4
$line =~ /$output_start/u || $stencil->add_between($line) && (next LINE)
l!l
1538
$stencil->add_between($line) and next LINE
!ll
038
MC/DC0%0/2
$line =~ /$output_start/u || $stencil->add_between($line) && (next LINE)
$line =~ /$output_start/u $stencil->add_between($line)
Truth table66%2/3
A || B
A= $line =~ /$output_start/uB= $stencil->add_between($line)
ABresult
1X1
011
000
153 15 $environment = 'output';
154 }
155 elsif($environment eq 'output') {
156 56 ▶ $stencil->add_output($line) and next LINE if $line !~ $output_end;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /$output_end/u 41 15 56
Condition75%3/4
$line =~ /$output_end/u || $stencil->add_output($line) && (next LINE)
l!l
1541
$stencil->add_output($line) and next LINE
!ll
041
MC/DC0%0/2
$line =~ /$output_end/u || $stencil->add_output($line) && (next LINE)
$line =~ /$output_end/u $stencil->add_output($line)
Truth table66%2/3
A || B
A= $line =~ /$output_end/uB= $stencil->add_output($line)
ABresult
1X1
011
000
157 15 $environment = 'after_output';
158 }
159 elsif($environment eq 'after_output') {
160 19 ▶ $stencil->add_after_output($line) and next LINE if $line !~ $stencil_start;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /$stencil_start/u 11 8 19
Condition60%3/5
$line =~ /$stencil_start/u || $stencil->add_after_output($line) && (next LINE)
l!l
8110
$stencil->add_after_output($line) and next LINE
!ll
011
MC/DC0%0/2
$line =~ /$stencil_start/u || $stencil->add_after_output($line) && (next LINE)
$line =~ /$stencil_start/u $stencil->add_after_output($line)
Truth table66%2/3
A || B
A= $line =~ /$stencil_start/uB= $stencil->add_after_output($line)
ABresult
1X1
011
000
161 8 $self->handle_completed_stencil($stencil);
162 8 $environment = 'next_stencil';
163 8 redo LINE;
164 }
165 }
166 7 ▶ if($environment ne 'after_output') {
Branch: 1 branch
BranchTrueFalseTotal
if ($environment ne "after_output") 0 7 7
167 0 croak (sprintf 'File <%s> appears malformed. Ended on <%s>', $self->filepath, $environment);
168 }
169 7 $self->handle_completed_stencil($stencil);
170 }
171  
172 7 ▶ method handle_completed_stencil(Maybe[Stencil] $stencil) {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine handle_completed_stencil: called CC=14 SCAR=27.7
Branch: 5 branches
BranchTrueFalseTotal
@_ ? : 15 0 15
if @_ != 1 0 15 15
$#_ >= 0 ? : 15 0 15
if ($#_ >= 0) 15 0 15
Type::Tiny::XS::AUTO::TC5($stencil) or _failed_check(62, "Maybe[Stencil]", $stencil, "exception_class", "Error::TypeTiny::Assertion") 0 15 15
173 15 ▶ return if !Stencil->check($stencil);
Branch: 1 branch
BranchTrueFalseTotal
unless &Stencil()->check($stencil) 0 15 15
174 15 ▶ return if $stencil->skip;
Branch: 1 branch
BranchTrueFalseTotal
if $stencil->skip 0 15 15
175 15 ▶ return if !$stencil->has_input && $self->skip_if_input_empty;
Branch: 1 branch
BranchTrueFalseTotal
if not $stencil->has_input and $self->skip_if_input_empty 2 13 15
Condition66%2/3
not $stencil->has_input and $self->skip_if_input_empty
!ll&&!rl&&r
1302
MC/DC50%1/2
not $stencil->has_input and $self->skip_if_input_empty
not $stencil->has_input $self->skip_if_input_empty
Truth table66%2/3
not(A) and B
A= not $stencil->has_inputB= $self->skip_if_input_empty
ABresult
0X0
100
111
176 13 ▶ return if !$stencil->has_output && $self->skip_if_output_empty;
Branch: 1 branch
BranchTrueFalseTotal
if not $stencil->has_output and $self->skip_if_output_empty 0 13 13
Condition33%1/3
not $stencil->has_output and $self->skip_if_output_empty
!ll&&!rl&&r
1300
MC/DC0%0/2
not $stencil->has_output and $self->skip_if_output_empty
not $stencil->has_output $self->skip_if_output_empty
Truth table33%1/3
not(A) and B
A= not $stencil->has_outputB= $self->skip_if_output_empty
ABresult
0X0
100
111
177  
178 13 ▶ if(!$stencil->has_loop_values) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($stencil->has_loop_values) 12 1 13
179 12 $self->add_stencil($stencil);
180 12 return;
181 }
182  
183 1 foreach my $loop_value ($stencil->all_loop_values) {
184 2 my $clone = $stencil->clone_with_loop_value($loop_value);
185 2 $self->add_stencil($clone);
186 }
187 }
188  
189 7 ▶ method max_stencil_index {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine max_stencil_index: called CC=2 SCAR=7.0
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 9 0 9
190 9 return $self->count_stencils - 1;
191 }
192 }
193  
194 7 1;
195  
196 __END__
197  
198 =pod
199  
200 =encoding UTF-8
201  
202 =head1 NAME
203  
204 Stenciller::Wrap
205  
206 =head1 VERSION
207  
208 Version 0.1302, released 2015-11-28.
209  
210 =head1 SOURCE
211  
212 L<https://github.com/Csson/p5-Stenciller>
213  
214 =head1 HOMEPAGE
215  
216 L<https://metacpan.org/release/Stenciller>
217  
218 =head1 AUTHOR
219  
220 Erik Carlsson <info@code301.com>
221  
222 =head1 COPYRIGHT AND LICENSE
223  
224 This software is copyright (c) 2015 by Erik Carlsson.
225  
226 This is free software; you can redistribute it and/or modify it under
227 the same terms as the Perl 5 programming language system itself.
228  
229 =cut
↑ Summary Stenciller/Stencil.pm »