File Coverage

blib/lib/Mock/Data/Plugin/Text.pm
Criterion Covered Total %
statement 50 50 100.0
branch 17 26 65.3
condition 25 49 51.0
subroutine 9 9 100.0
pod 3 4 75.0
total 104 138 75.3


line stmt bran cond sub pod time code
1             package Mock::Data::Plugin::Text;
2 2     2   1398 use Mock::Data::Plugin -exporter_setup => 1;
  2         5  
  2         19  
3 2     2   526 use Mock::Data::Charset;
  2         4  
  2         54  
4 2     2   7 use Mock::Data::Util 'coerce_generator';
  2         27  
  2         10  
5 2     2   314 use Scalar::Util 'blessed';
  2         3  
  2         127  
6 2     2   10 use Carp;
  2         3  
  2         1764  
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.04'; # 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 13 my ($class, $mock)= @_;
23 4         22 $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 56 my $mockdata= shift;
34 33 50 33     112 my $opts= @_ && ref $_[0] eq 'HASH'? shift : undef;
35 33   33     160 my $sep= $_[0] // ($opts && $opts->{sep}) // ' ';
      33        
      50        
36 33   33     113 my $source= $_[1] // ($opts && $opts->{source}) // Carp::croak("Parameter 'source' is required");
      33        
      33        
37 33   66     116 my $count= $_[2] // ($opts && $opts->{count}) // 1;
      66        
      100        
38 33 50       54 my $len= $opts? $opts->{len} : undef;
39 33 50       53 my $max_len= $opts? $opts->{max_len} : undef;
40            
41 33 50       107 my @source_generators= map coerce_generator($_), ref $source eq 'ARRAY'? @$source : ( $source );
42 33         132 my $buf= $source_generators[0]->generate($mockdata);
43 33         38 my $src_i= 1;
44 33 100       49 if (defined $len) {
45 32   33     118 while (length($buf) < $len && (!defined $max_len or length($buf) + length($sep) < $max_len)) {
      66        
46 40         95 $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         13 $buf .= $sep . $source_generators[$src_i++ % scalar @source_generators]->generate($mockdata);
52             }
53             }
54 33 100 100     103 substr($buf, $max_len)= ''
55             if defined $max_len && length($buf) > $max_len;
56 33         176 return $buf;
57             }
58              
59              
60             sub words {
61 3     3 1 7 my $mockdata= shift;
62 3 100 66     19 my %opts= @_ && ref $_[0] eq 'HASH'? %{ shift() } : ();
  1         6  
63 3 100       7 if (@_) {
64 2 100       9 @opts{'len','max_len'}= ref $_[0] eq 'ARRAY'? @{$_[0]} : @_[0,0];
  1         4  
65             }
66 3   33     18 $opts{source} //= $mockdata->generators->{word};
67              
68 3         12 $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       36 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         7 my $ret= $lorem_ipsum_classic x int(1+($len/length $lorem_ipsum_classic));
86 1         4 substr($ret, $len)= '';
87 1         8 $ret =~ s/\s+$//;
88 1         10 return $ret;
89             }
90              
91             1;
92              
93             __END__