line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mock::Data::Plugin::Text; |
2
|
1
|
|
|
1
|
|
546
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
32
|
|
3
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
25
|
|
4
|
1
|
|
|
1
|
|
6
|
use Carp; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
67
|
|
5
|
|
|
|
|
|
|
our @CARP_NOT= qw( Mock::Data Mock::Data::Util ); |
6
|
1
|
|
|
1
|
|
6
|
use Scalar::Util 'blessed'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
53
|
|
7
|
1
|
|
|
1
|
|
7
|
use Mock::Data::Charset; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
24
|
|
8
|
1
|
|
|
1
|
|
5
|
use Mock::Data::Util 'coerce_generator'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
836
|
|
9
|
|
|
|
|
|
|
require Exporter; |
10
|
|
|
|
|
|
|
our @ISA= qw( Exporter ); |
11
|
|
|
|
|
|
|
our @EXPORT_OK= qw( word words lorem_ipsum join ); |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
# ABSTRACT: Mock::Data plugin that provides text-related generators |
14
|
|
|
|
|
|
|
our $VERSION = '0.02'; # VERSION |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
our $word_generator; |
18
|
|
|
|
|
|
|
sub apply_mockdata_plugin { |
19
|
3
|
|
|
3
|
0
|
7
|
my ($class, $mockdata)= @_; |
20
|
3
|
|
|
|
|
15
|
$mockdata->add_generators( |
21
|
|
|
|
|
|
|
'Text::join' => \&join, |
22
|
|
|
|
|
|
|
'Text::word' => $word_generator, |
23
|
|
|
|
|
|
|
'Text::words' => \&words, |
24
|
|
|
|
|
|
|
'Text::lorem_ipsum' => \&lorem_ipsum, |
25
|
|
|
|
|
|
|
); |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
sub join { |
30
|
3
|
|
|
3
|
1
|
4
|
my $mockdata= shift; |
31
|
3
|
50
|
33
|
|
|
15
|
my $opts= @_ && ref $_[0] eq 'HASH'? shift : undef; |
32
|
3
|
|
33
|
|
|
30
|
my $sep= $_[0] // ($opts && $opts->{sep}) // ' '; |
|
|
|
33
|
|
|
|
|
|
|
|
50
|
|
|
|
|
33
|
3
|
|
33
|
|
|
16
|
my $source= $_[1] // ($opts && $opts->{source}) // Carp::croak("Parameter 'source' is required"); |
|
|
|
33
|
|
|
|
|
|
|
|
33
|
|
|
|
|
34
|
3
|
|
66
|
|
|
16
|
my $count= $_[2] // ($opts && $opts->{count}) // 1; |
|
|
|
66
|
|
|
|
|
|
|
|
100
|
|
|
|
|
35
|
3
|
50
|
|
|
|
7
|
my $len= $opts? $opts->{len} : undef; |
36
|
3
|
50
|
|
|
|
7
|
my $max_len= $opts? $opts->{max_len} : undef; |
37
|
|
|
|
|
|
|
|
38
|
3
|
50
|
|
|
|
15
|
my @source_generators= map coerce_generator($_), ref $source eq 'ARRAY'? @$source : ( $source ); |
39
|
3
|
|
|
|
|
12
|
my $buf= $source_generators[0]->generate($mockdata); |
40
|
3
|
|
|
|
|
6
|
my $src_i= 1; |
41
|
3
|
100
|
|
|
|
7
|
if (defined $len) { |
42
|
2
|
|
33
|
|
|
24
|
while (length($buf) < $len && (!defined $max_len or length($buf) + length($sep) < $max_len)) { |
|
|
|
66
|
|
|
|
|
43
|
17
|
|
|
|
|
48
|
$buf .= $sep . $source_generators[$src_i++ % scalar @source_generators]->generate($mockdata); |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
} else { |
46
|
1
|
|
|
|
|
3
|
my $lim= $count * scalar @source_generators; |
47
|
1
|
|
33
|
|
|
9
|
while ($src_i < $lim && (!defined $max_len or length($buf) + length($sep) < $max_len)) { |
|
|
|
66
|
|
|
|
|
48
|
4
|
|
|
|
|
12
|
$buf .= $sep . $source_generators[$src_i++ % scalar @source_generators]->generate($mockdata); |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
} |
51
|
3
|
100
|
100
|
|
|
12
|
substr($buf, $max_len)= '' |
52
|
|
|
|
|
|
|
if defined $max_len && length($buf) > $max_len; |
53
|
3
|
|
|
|
|
29
|
return $buf; |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
$word_generator= Mock::Data::Charset->new( |
58
|
|
|
|
|
|
|
notation => 'a-z', |
59
|
|
|
|
|
|
|
str_len => sub { 1 + int(rand 3 + rand 3 + rand 4) } |
60
|
|
|
|
|
|
|
); |
61
|
|
|
|
|
|
|
*word= $word_generator->compile; |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
sub words { |
64
|
3
|
|
|
3
|
1
|
7
|
my $mockdata= shift; |
65
|
3
|
100
|
66
|
|
|
19
|
my %opts= @_ && ref $_[0] eq 'HASH'? %{ shift() } : (); |
|
1
|
|
|
|
|
5
|
|
66
|
3
|
100
|
|
|
|
8
|
if (@_) { |
67
|
2
|
100
|
|
|
|
7
|
@opts{'len','max_len'}= ref $_[0] eq 'ARRAY'? @{$_[0]} : @_[0,0]; |
|
1
|
|
|
|
|
4
|
|
68
|
|
|
|
|
|
|
} |
69
|
3
|
|
33
|
|
|
15
|
$opts{source} //= $mockdata->generators->{word}; |
70
|
|
|
|
|
|
|
|
71
|
3
|
|
|
|
|
10
|
$mockdata->call('join', \%opts); |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
my $lorem_ipsum_classic= |
76
|
|
|
|
|
|
|
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor " |
77
|
|
|
|
|
|
|
."incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis " |
78
|
|
|
|
|
|
|
."nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. " |
79
|
|
|
|
|
|
|
."Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu " |
80
|
|
|
|
|
|
|
."fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in " |
81
|
|
|
|
|
|
|
."culpa qui officia deserunt mollit anim id est laborum. "; |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
sub lorem_ipsum { |
84
|
1
|
|
|
1
|
1
|
3
|
my $mockdata= shift; |
85
|
1
|
0
|
|
|
|
6
|
my $len= @_ > 1? $_[1] : !ref $_[0]? $_[0] : ref $_[0] eq 'HASH'? $_[0]{len} : undef; |
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
86
|
1
|
50
|
|
|
|
3
|
return $lorem_ipsum_classic |
87
|
|
|
|
|
|
|
unless defined $len; |
88
|
1
|
|
|
|
|
8
|
my $ret= $lorem_ipsum_classic x int(1+($len/length $lorem_ipsum_classic)); |
89
|
1
|
|
|
|
|
4
|
substr($ret, $len)= ''; |
90
|
1
|
|
|
|
|
6
|
$ret =~ s/\s+$//; |
91
|
1
|
|
|
|
|
6
|
return $ret; |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
1; |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
__END__ |