Data/Template.pm

filter: Statementstmt 96.2% 77 / 80 Branchbran 82.0% 41 / 50 Conditioncond 64.7% 11 / 17 MC/DCmcdc 54.5% 6 / 11 Subroutinesub 100% 17 / 17 Podpod 100% 6 / 6 totaltotal 87.2% 158 / 181 CC 35 SCAR 20.7
CC
35
Cov
88%
new
15.8
_parse_template
12.0
_parse_template_reference
11.1
CRAP 7.9
« Data.pm ↑ Summary Data/Plugin/SQLTypes.pm »
1 package Mock::Data::Template;
2 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use overload '""' => sub { shift->to_string };
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
5 12 require Carp;
6 12 ▶use Mock::Data::Util qw( _parse_context _escape_str );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 12 require Mock::Data::Generator;
8 12 our @ISA= qw( Mock::Data::Generator );
9  
10 # ABSTRACT: Create a generator that plugs other templates into a string
11 12 our $VERSION = '0.04'; # VERSION
12  
13  
14 88 ▶sub new {
Subroutine new: called CC=6 SCAR=27.6
Pod documented
15 88 my $class= shift;
16 my %self= (@_ == 1 && !ref $_[0])? ( template => $_[0] )
17 0 ▶ : (@_ == 1 && ref $_[0] eq 'HASH')? %{$_[0]}
Branch: 3 branches
BranchTrueFalseTotal
@_ == 1 && !ref($_[0]) ? : 88 0 88
@_ == 1 && ref $_[0] eq 'HASH' ? : 0 0 0
@_ > 1 ? : 0 0 0
Condition16%1/6
@_ == 1 and not ref $_[0]
!ll&&!rl&&r
0088
@_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
@_ == 1 and not ref $_[0]
@_ == 1 not ref $_[0]
MC/DC0%0/2
@_ == 1 and ref $_[0] eq "HASH"
@_ == 1 ref $_[0] eq "HASH"
Truth table33%1/3
A and not(B)
A= @_ == 1B= not ref $_[0]
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= @_ == 1B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
18 : @_ > 1? @_
19 : Carp::croak("Invalid constructor arguments to $class");
20 # Parse now, to report errors
21 88 $self{_compiled}= $class->parse_template($self{template}, { compile => 1 });
22 86 bless \%self, $class;
23 }
24  
25  
26 16 ▶sub template { shift->{template} }
Subroutine template: called CC=1 SCAR=0.0
Pod documented
27  
28  
29 4 ▶sub compile {
Subroutine compile: called CC=2 SCAR=6.9
Pod documented
30 4 my $cmp= $_[0]{_compiled};
31 4 ▶ return ref $cmp? $cmp : sub { $cmp };
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
ref $cmp ? : 2 2 4
32 }
33  
34 106 ▶sub generate {
Subroutine generate: called CC=2 SCAR=6.9
Pod documented
35 106 my $cmp= shift->{_compiled};
36 106 ▶ return ref $cmp? $cmp->(@_) : $cmp;
Branch: 1 branch
BranchTrueFalseTotal
ref $cmp ? : 16 90 106
37 }
38  
39  
40 16 ▶sub to_string {
Subroutine to_string: called CC=1 SCAR=0.0
Pod documented
41 16 "template('" . shift->template . "')";
42 }
43  
44  
45 88 ▶sub parse_template {
Subroutine parse_template: called CC=3 SCAR=11.0
Pod documented
46 88 my ($self, $str, $flags)= @_;
47 88 local $_= $str;
48 88 pos= 0;
49 88 my $ret;
50 88 local $@;
51 88 ▶ defined eval { $ret= _parse_template($flags || {}) }
Branch: 1 branch
BranchTrueFalseTotal
defined eval { do { $ret = _parse_template($flags || {}) } } or Carp::croak("$@ at " . _parse_context()) 86 2 88
Condition50%1/2
$flags || {}
l!l
880
MC/DC0%0/1
$flags || {}
$flags
Truth table50%1/2
A
A= $flags
Aresult
11
00
52 or Carp::croak("$@ at "._parse_context);
53 86 return $ret;
54 }
55  
56 # Parse a template string in $_ from pos($_)
57 102 ▶sub _parse_template {
Subroutine _parse_template: called CC=12 SCAR=24.9
58 102 my @parts;
59 102 my $outer= !$_[0]{inner};
60 102 ▶ local $_[0]{inner}= 1 if $outer;
Branch: 1 branch
BranchTrueFalseTotal
if $outer 88 14 102
61 102 while (1) {
62 # Consume run of literal characters
63 129 ▶ push @parts, $1 if $outer? /\G([^{]+)/gc : /\G([^ \t\{\}]+)/gc;
Branch: 2 branches
BranchTrueFalseTotal
if $outer ? /\G([^{]+)/cg : /\G([^ \t\{\}]+)/cg 81 48 129
$outer ? : 108 21 129
64 # at end of template, or beginning of a reference to something
65 129 ▶ last unless /\G(?=\{)/gc;
Branch: 1 branch
BranchTrueFalseTotal
unless /\G(?=\{)/cg 100 29 129
66 29 push @parts, _parse_template_reference(@_);
67 }
68 # Combine adjacent scalars in the list
69 100 ▶ @parts= grep ref $_ || length, @parts;
Condition100%3/3
ref $_ || length $_
l!l&&r!l&&!r
19881
MC/DC100%2/2
ref $_ || length $_
ref $_ length $_
Truth table100%3/3
A || B
A= ref $_B= length $_
ABresult
1X1
011
000
70 100 ▶ for (my $i= $#parts - 1; $i >= 0; --$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0) 8 100 108
71 8 ▶ if (!ref $parts[$i] and !ref $parts[$i+1]) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $parts[$i] or ref $parts[$i + 1]) 5 3 8
Condition100%3/3
ref $parts[$i] or ref $parts[$i + 1]
l!l&&r!l&&!r
215
MC/DC100%2/2
ref $parts[$i] or ref $parts[$i + 1]
ref $parts[$i] ref $parts[$i + 1]
Truth table100%3/3
A or B
A= ref $parts[$i]B= ref $parts[$i + 1]
ABresult
1X1
011
000
72 5 $parts[$i] .= splice(@parts, $i+1, 1);
73 }
74 }
75 100 ▶ if ($_[0]{compile}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]{'compile'}) { } 100 0 100
76 return @parts == 1 && !ref $parts[0]? $parts[0]
77 19 ▶ : sub { join '', map +(ref($_)? $_->(@_) : $_), @parts }
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 20 1 21
78 100 ▶ } else {
Branch: 1 branch
BranchTrueFalseTotal
@parts == 1 && !ref($parts[0]) ? : 82 18 100
Condition100%3/3
@parts == 1 and not ref $parts[0]
!ll&&!rl&&r
31582
MC/DC100%2/2
@parts == 1 and not ref $parts[0]
@parts == 1 not ref $parts[0]
Truth table100%3/3
A and not(B)
A= @parts == 1B= not ref $parts[0]
ABresult
0X0
100
111
79 0 return \@parts;
80 }
81 }
82  
83 # Parse one of the curly-brace notations
84 29 ▶sub _parse_template_reference {
Subroutine _parse_template_reference: called CC=11 SCAR=24.0
85 29 ▶ if (/\G\{([\w:]+)/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if (/\G\{([\w:]+)/cg) 20 9 29
86 20 my $generator_name= $1;
87 20 my (@named_param, @pos_param);
88 20 ▶ if (/\G[ \t]+/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if (/\G[ \t]+/cg) { } 13 7 20
89 13 ▶ while (!/\G\}/gc) {
Branch: 1 branch
BranchTrueFalseTotal
unless (/\G\}/cg) 14 13 27
90 14 ▶ if (/\G(\w+)=/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if (/\G(\w+)=/cg) { } 6 8 14
91 6 push @named_param, $1, _parse_template(@_);
92 } else {
93 8 push @pos_param, _parse_template(@_);
94 }
95 14 /\G[ \t]*/gc;
96 }
97 } else {
98 7 ▶ /\G\}/gc or die "Expected '}'";
Branch: 1 branch
BranchTrueFalseTotal
/\G\}/cg or die "Expected '}'" 6 1 7
99 }
100 19 ▶ if ($_[0]{compile}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]{'compile'}) { } 19 0 19
101 # compile by making a list of which params are function calls, and update lists for only those positions
102 19 my @named_literal= @named_param;
103 19 my @dynamic_named= grep ref $named_param[$_], 0 .. $#named_param;
104 19 my @pos_literal= @pos_param;
105 19 my @dynamic_pos= grep ref $pos_literal[$_], 0 .. $#pos_param;
106 19 ▶ if (@named_param) {
Branch: 1 branch
BranchTrueFalseTotal
if (@named_param) { } 6 13 19
107 6 ▶ return sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
108 6 $named_literal[$_]= $named_param[$_]->(@_) for @dynamic_named;
109 6 $pos_literal[$_]= $pos_param[$_]->(@_) for @dynamic_pos;
110 6 $_[0]->call($generator_name, { @named_literal }, @pos_literal);
111 }
112 6 } else {
113 14 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
114 14 $pos_literal[$_]= $pos_param[$_]->(@_) for @dynamic_pos;
115 14 $_[0]->call($generator_name, @pos_literal);
116 }
117 13 }
118 } else {
119 0 ▶ return [ $generator_name, (@named_param? { @named_param }:()), @pos_param ];
Branch: 1 branch
BranchTrueFalseTotal
@named_param ? : 0 0 0
120 }
121 }
122 9 ▶ return chr hex $1 if /\G\{ [#] ([0-9A-Za-z]+) \}/xgc;
Branch: 1 branch
BranchTrueFalseTotal
if /\G\{ [#] ([0-9A-Za-z]+) \}/cgx 7 2 9
123 2 ▶ return '' if /\G\{\}/xgc;
Branch: 1 branch
BranchTrueFalseTotal
if /\G\{\}/cgx 1 1 2
124 1 die "Invalid template notation\n";
125 }
126  
127 12 1;
128  
129 __END__
« Data.pm ↑ Summary Data/Plugin/SQLTypes.pm »