line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mock::Data::Plugin::Text; |
2
|
2
|
|
|
2
|
|
919
|
use Mock::Data::Plugin -exporter_setup => 1; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
15
|
|
3
|
2
|
|
|
2
|
|
367
|
use Mock::Data::Charset; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
40
|
|
4
|
2
|
|
|
2
|
|
10
|
use Mock::Data::Util 'coerce_generator'; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
10
|
|
5
|
2
|
|
|
2
|
|
291
|
use Scalar::Util 'blessed'; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
81
|
|
6
|
2
|
|
|
2
|
|
20
|
use Carp; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
1515
|
|
7
|
|
|
|
|
|
|
our @CARP_NOT= qw( Mock::Data Mock::Data::Util ); |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
# ABSTRACT: Mock::Data plugin that provides text-related generators |
10
|
|
|
|
|
|
|
our $VERSION = '0.03'; # VERSION |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
our $word_generator= Mock::Data::Charset->new( |
14
|
|
|
|
|
|
|
notation => 'a-z', |
15
|
|
|
|
|
|
|
str_len => sub { 1 + int(rand 3 + rand 3 + rand 4) } |
16
|
|
|
|
|
|
|
); |
17
|
|
|
|
|
|
|
*word= $word_generator->compile; |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
export(qw( word words lorem_ipsum join )); |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub apply_mockdata_plugin { |
22
|
4
|
|
|
4
|
0
|
11
|
my ($class, $mock)= @_; |
23
|
4
|
|
|
|
|
18
|
$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
|
|
|
|
|
|
|
sub join { |
33
|
33
|
|
|
33
|
1
|
62
|
my $mockdata= shift; |
34
|
33
|
50
|
33
|
|
|
157
|
my $opts= @_ && ref $_[0] eq 'HASH'? shift : undef; |
35
|
33
|
|
33
|
|
|
171
|
my $sep= $_[0] // ($opts && $opts->{sep}) // ' '; |
|
|
|
33
|
|
|
|
|
|
|
|
50
|
|
|
|
|
36
|
33
|
|
33
|
|
|
149
|
my $source= $_[1] // ($opts && $opts->{source}) // Carp::croak("Parameter 'source' is required"); |
|
|
|
33
|
|
|
|
|
|
|
|
33
|
|
|
|
|
37
|
33
|
|
66
|
|
|
183
|
my $count= $_[2] // ($opts && $opts->{count}) // 1; |
|
|
|
66
|
|
|
|
|
|
|
|
100
|
|
|
|
|
38
|
33
|
50
|
|
|
|
62
|
my $len= $opts? $opts->{len} : undef; |
39
|
33
|
50
|
|
|
|
58
|
my $max_len= $opts? $opts->{max_len} : undef; |
40
|
|
|
|
|
|
|
|
41
|
33
|
50
|
|
|
|
128
|
my @source_generators= map coerce_generator($_), ref $source eq 'ARRAY'? @$source : ( $source ); |
42
|
33
|
|
|
|
|
101
|
my $buf= $source_generators[0]->generate($mockdata); |
43
|
33
|
|
|
|
|
58
|
my $src_i= 1; |
44
|
33
|
100
|
|
|
|
58
|
if (defined $len) { |
45
|
32
|
|
66
|
|
|
116
|
while (length($buf) < $len && (!defined $max_len or length($buf) + length($sep) < $max_len)) { |
|
|
|
100
|
|
|
|
|
46
|
45
|
|
|
|
|
112
|
$buf .= $sep . $source_generators[$src_i++ % scalar @source_generators]->generate($mockdata); |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
} else { |
49
|
1
|
|
|
|
|
3
|
my $lim= $count * scalar @source_generators; |
50
|
1
|
|
33
|
|
|
7
|
while ($src_i < $lim && (!defined $max_len or length($buf) + length($sep) < $max_len)) { |
|
|
|
66
|
|
|
|
|
51
|
4
|
|
|
|
|
12
|
$buf .= $sep . $source_generators[$src_i++ % scalar @source_generators]->generate($mockdata); |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
} |
54
|
33
|
100
|
100
|
|
|
128
|
substr($buf, $max_len)= '' |
55
|
|
|
|
|
|
|
if defined $max_len && length($buf) > $max_len; |
56
|
33
|
|
|
|
|
193
|
return $buf; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
sub words { |
61
|
3
|
|
|
3
|
1
|
6
|
my $mockdata= shift; |
62
|
3
|
100
|
66
|
|
|
17
|
my %opts= @_ && ref $_[0] eq 'HASH'? %{ shift() } : (); |
|
1
|
|
|
|
|
4
|
|
63
|
3
|
100
|
|
|
|
9
|
if (@_) { |
64
|
2
|
100
|
|
|
|
6
|
@opts{'len','max_len'}= ref $_[0] eq 'ARRAY'? @{$_[0]} : @_[0,0]; |
|
1
|
|
|
|
|
3
|
|
65
|
|
|
|
|
|
|
} |
66
|
3
|
|
33
|
|
|
14
|
$opts{source} //= $mockdata->generators->{word}; |
67
|
|
|
|
|
|
|
|
68
|
3
|
|
|
|
|
11
|
$mockdata->call('join', \%opts); |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
sub lorem_ipsum { |
81
|
1
|
|
|
1
|
1
|
3
|
my $mockdata= shift; |
82
|
1
|
0
|
|
|
|
5
|
my $len= @_ > 1? $_[1] : !ref $_[0]? $_[0] : ref $_[0] eq 'HASH'? $_[0]{len} : undef; |
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
83
|
1
|
50
|
|
|
|
4
|
return $lorem_ipsum_classic |
84
|
|
|
|
|
|
|
unless defined $len; |
85
|
1
|
|
|
|
|
6
|
my $ret= $lorem_ipsum_classic x int(1+($len/length $lorem_ipsum_classic)); |
86
|
1
|
|
|
|
|
4
|
substr($ret, $len)= ''; |
87
|
1
|
|
|
|
|
6
|
$ret =~ s/\s+$//; |
88
|
1
|
|
|
|
|
6
|
return $ret; |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
1; |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
__END__ |