Data/Plugin/Text.pm

filter: Statementstmt 100% 58 / 58 Branchbran 70.0% 21 / 30 Conditioncond 56.2% 27 / 48 MC/DCmcdc 30.4% 7 / 23 Subroutinesub 100% 10 / 10 Podpod 75.0% 3 / 4 totaltotal 72.8% 126 / 173 CC 33 SCAR 31.5
CC
33
Cov
78%
join
38.0
words
6.1
lorem_ipsum
5.9
CRAP 23.4
« Data/Charset.pm ↑ Summary Data/Set.pm »
1 package Mock::Data::Plugin::Text;
2 2 ▶use Mock::Data::Plugin -exporter_setup => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use Mock::Data::Charset;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Mock::Data::Util 'coerce_generator';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 our @CARP_NOT= qw( Mock::Data Mock::Data::Util );
8  
9 # ABSTRACT: Mock::Data plugin that provides text-related generators
10 2 our $VERSION = '0.04'; # VERSION
11  
12  
13 our $word_generator= Mock::Data::Charset->new(
14 notation => 'a-z',
15 25 ▶ str_len => sub { 1 + int(rand 3 + rand 3 + rand 4) }
Subroutine __ANON__: called CC=1 SCAR=0.0
16 2 );
17 2 *word= $word_generator->compile;
18  
19 2 export(qw( word words lorem_ipsum join ));
20  
21 4 ▶sub apply_mockdata_plugin {
Subroutine apply_mockdata_plugin: called CC=1 SCAR=0.0
Pod undocumented
22 4 my ($class, $mock)= @_;
23 4 $mock->add_generators(
24 'Text::join' => \&join,
25 'Text::word' => $word_generator,
26 'Text::words' => \&words,
27 'Text::lorem_ipsum' => \&lorem_ipsum,
28 );
29 }
30  
31  
32 33 ▶sub join {
Subroutine join: called CC=24 SCAR=36.4
Pod documented
33 33 my $mockdata= shift;
34 33 ▶ my $opts= @_ && ref $_[0] eq 'HASH'? shift : undef;
Branch: 1 branch
BranchTrueFalseTotal
@_ && ref $_[0] eq 'HASH' ? : 33 0 33
Condition33%1/3
@_ and ref $_[0] eq "HASH"
!ll&&!rl&&r
0033
MC/DC0%0/2
@_ and ref $_[0] eq "HASH"
@_ ref $_[0] eq "HASH"
Truth table33%1/3
A and B
A= @_B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
35 33 ▶ my $sep= $_[0] // ($opts && $opts->{sep}) // ' ';
Condition37%3/8
($_[0] // $opts && $opts->{"sep"}) // " "
l!l
033
$_[0] // $opts && $opts->{"sep"}
l!l&&r!l&&!r
0033
$opts and $opts->{"sep"}
!ll&&!rl&&r
0330
MC/DC0%0/3
($_[0] // $opts && $opts->{"sep"}) // " "
$_[0] $opts $opts->{"sep"}
Truth table25%1/4
A // B and C
A= $_[0]B= $optsC= $opts->{"sep"}
ABCresult
1XX1
0111
00X0
0100
36 33 ▶ my $source= $_[1] // ($opts && $opts->{source}) // Carp::croak("Parameter 'source' is required");
Condition37%3/8
($_[1] // $opts && $opts->{"source"}) // Carp::croak("Parameter 'source' is required")
l!l
330
$_[1] // $opts && $opts->{"source"}
l!l&&r!l&&!r
0330
$opts and $opts->{"source"}
!ll&&!rl&&r
0033
MC/DC0%0/3
($_[1] // $opts && $opts->{"source"}) // Carp::croak("Parameter 'source' is required")
$_[1] $opts $opts->{"source"}
Truth table25%1/4
A // B and C
A= $_[1]B= $optsC= $opts->{"source"}
ABCresult
1XX1
0111
00X0
0100
37 33 ▶ my $count= $_[2] // ($opts && $opts->{count}) // 1;
Condition75%6/8
($_[2] // $opts && $opts->{"count"}) // 1
l!l
132
$_[2] // $opts && $opts->{"count"}
l!l&&r!l&&!r
0132
$opts and $opts->{"count"}
!ll&&!rl&&r
0321
MC/DC33%1/3
($_[2] // $opts && $opts->{"count"}) // 1
$_[2] $opts $opts->{"count"}
Truth table50%2/4
A // B and C
A= $_[2]B= $optsC= $opts->{"count"}
ABCresult
1XX1
0111
00X0
0100
38 33 ▶ my $len= $opts? $opts->{len} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$opts ? : 33 0 33
39 33 ▶ my $max_len= $opts? $opts->{max_len} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$opts ? : 33 0 33
40
41 33 ▶ my @source_generators= map coerce_generator($_), ref $source eq 'ARRAY'? @$source : ( $source );
Branch: 1 branch
BranchTrueFalseTotal
ref $source eq 'ARRAY' ? : 0 33 33
42 33 my $buf= $source_generators[0]->generate($mockdata);
43 33 my $src_i= 1;
44 33 ▶ if (defined $len) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $len) { } 32 1 33
45 32 ▶ while (length($buf) < $len && (!defined $max_len or length($buf) + length($sep) < $max_len)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $buf < $len and !defined($max_len) || length($buf) + length($sep) < $max_len) 39 32 71
Condition83%5/6
length $buf < $len and !defined($max_len) || length($buf) + length($sep) < $max_len
!ll&&!rl&&r
31139
not defined $max_len or length($buf) + length($sep) < $max_len
l!l&&r!l&&!r
0391
MC/DC66%2/3
length $buf < $len and !defined($max_len) || length($buf) + length($sep) < $max_len
length $buf < $len not defined $max_len length($buf) + length($sep) < $max_len
Truth table75%3/4
A and not(B) or C
A= length $buf < $lenB= not defined $max_lenC= length($buf) + length($sep) < $max_len
ABCresult
0XX0
1000
11X1
1011
46 39 $buf .= $sep . $source_generators[$src_i++ % scalar @source_generators]->generate($mockdata);
47 }
48 } else {
49 1 my $lim= $count * scalar @source_generators;
50 1 ▶ while ($src_i < $lim && (!defined $max_len or length($buf) + length($sep) < $max_len)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($src_i < $lim and !defined($max_len) || length($buf) + length($sep) < $max_len) 4 1 5
Condition50%3/6
$src_i < $lim and !defined($max_len) || length($buf) + length($sep) < $max_len
!ll&&!rl&&r
104
not defined $max_len or length($buf) + length($sep) < $max_len
l!l&&r!l&&!r
400
MC/DC33%1/3
$src_i < $lim and !defined($max_len) || length($buf) + length($sep) < $max_len
$src_i < $lim not defined $max_len length($buf) + length($sep) < $max_len
Truth table50%2/4
A and not(B) or C
A= $src_i < $limB= not defined $max_lenC= length($buf) + length($sep) < $max_len
ABCresult
0XX0
1000
11X1
1011
51 4 $buf .= $sep . $source_generators[$src_i++ % scalar @source_generators]->generate($mockdata);
52 }
53 }
54 33 ▶ substr($buf, $max_len)= ''
Branch: 1 branch
BranchTrueFalseTotal
if defined $max_len and length $buf > $max_len 12 21 33
Condition100%3/3
defined $max_len and length $buf > $max_len
!ll&&!rl&&r
12012
MC/DC100%2/2
defined $max_len and length $buf > $max_len
defined $max_len length $buf > $max_len
Truth table100%3/3
A and B
A= defined $max_lenB= length $buf > $max_len
ABresult
0X0
100
111
55 if defined $max_len && length($buf) > $max_len;
56 33 return $buf;
57 }
58  
59  
60 3 ▶sub words {
Subroutine words: called CC=6 SCAR=18.1
Pod documented
61 3 my $mockdata= shift;
62 3 ▶ my %opts= @_ && ref $_[0] eq 'HASH'? %{ shift() } : ();
Branch: 1 branch
BranchTrueFalseTotal
@_ && ref $_[0] eq 'HASH' ? : 1 2 3
Condition66%2/3
@_ and ref $_[0] eq "HASH"
!ll&&!rl&&r
021
MC/DC50%1/2
@_ and ref $_[0] eq "HASH"
@_ ref $_[0] eq "HASH"
Truth table66%2/3
A and B
A= @_B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
63 3 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 2 1 3
64 2 ▶ @opts{'len','max_len'}= ref $_[0] eq 'ARRAY'? @{$_[0]} : @_[0,0];
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 1 1 2
65 }
66 3 ▶ $opts{source} //= $mockdata->generators->{word};
Condition33%1/3
$opts{"source"} //= $mockdata->generators->{"word"}
l!l&&r!l&&!r
030
MC/DC0%0/2
$opts{"source"} //= $mockdata->generators->{"word"}
$opts{"source"} $mockdata->generators->{"word"}
Truth table33%1/3
A //= B
A= $opts{"source"}B= $mockdata->generators->{"word"}
ABresult
1X1
011
000
67  
68 3 $mockdata->call('join', \%opts);
69 }
70  
71  
72 2 my $lorem_ipsum_classic=
73 "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor "
74 ."incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis "
75 ."nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. "
76 ."Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu "
77 ."fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in "
78 ."culpa qui officia deserunt mollit anim id est laborum. ";
79  
80 1 ▶sub lorem_ipsum {
Subroutine lorem_ipsum: called CC=5 SCAR=17.8
Pod documented
81 1 my $mockdata= shift;
82 1 ▶ my $len= @_ > 1? $_[1] : !ref $_[0]? $_[0] : ref $_[0] eq 'HASH'? $_[0]{len} : undef;
Branch: 3 branches
BranchTrueFalseTotal
@_ > 1 ? : 0 1 1
!ref($_[0]) ? : 1 0 1
ref $_[0] eq 'HASH' ? : 0 0 0
83 1 ▶ return $lorem_ipsum_classic
Branch: 1 branch
BranchTrueFalseTotal
unless defined $len 0 1 1
84 unless defined $len;
85 1 my $ret= $lorem_ipsum_classic x int(1+($len/length $lorem_ipsum_classic));
86 1 substr($ret, $len)= '';
87 1 $ret =~ s/\s+$//;
88 1 return $ret;
89 }
90  
91 2 1;
92  
93 __END__
« Data/Charset.pm ↑ Summary Data/Set.pm »