| 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
Condition16%1/6
@_ == 1 and not ref $_[0]
@_ == 1 and ref $_[0] eq "HASH"
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]
Truth table0%0/3
A and B
A= @_ == 1B= ref $_[0] eq "HASH"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition50%1/2
$flags || {}
MC/DC0%0/1
$flags || {}
$flags
Truth table50%1/2
A
A= $flags
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | # at end of template, or beginning of a reference to something | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 129 | ▶ | last unless /\G(?=\{)/gc; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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 $_
MC/DC100%2/2
ref $_ || length $_
ref $_
length $_
Truth table100%3/3
A || B
A= ref $_B= length $_
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 100 | ▶ | for (my $i= $#parts - 1; $i >= 0; --$i) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 8 | ▶ | if (!ref $parts[$i] and !ref $parts[$i+1]) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
ref $parts[$i] or ref $parts[$i + 1]
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]
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 5 | $parts[$i] .= splice(@parts, $i+1, 1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 100 | ▶ | if ($_[0]{compile}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 100 | ▶ | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@parts == 1 and not ref $parts[0]
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]
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 20 | my $generator_name= $1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 20 | my (@named_param, @pos_param); | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 20 | ▶ | if (/\G[ \t]+/gc) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 13 | ▶ | while (!/\G\}/gc) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 14 | ▶ | if (/\G(\w+)=/gc) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 19 | ▶ | if ($_[0]{compile}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | 9 | ▶ | return chr hex $1 if /\G\{ [#] ([0-9A-Za-z]+) \}/xgc; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | 2 | ▶ | return '' if /\G\{\}/xgc; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 1 | die "Invalid template notation\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | 12 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||