Faker/Maker.pm untested

filter: Statementstmt 0.0% 0 / 100 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 27 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 6 Podpod n/a totaltotal 0.0% 0 / 157 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 package Faker::Maker;
2  
3 use 5.014;
4  
5 use strict;
6 use warnings;
7  
8 use registry;
9 use routines;
10  
11 use Data::Object::Role;
12  
13 with 'Data::Object::Role::Pluggable';
14 with 'Data::Object::Role::Throwable';
15  
16 requires 'plugin';
17 requires 'throw';
18  
19 method format_lex_markers(Str $string) {
20 $string =~ s/\?/$self->random_letter/eg;
21  
22 return $string;
23 }
24  
25 method format_line_markers(Str $string) {
26 $string =~ s/\\n/\n/g;
27  
28 return $string;
29 }
30  
31 method format_number_markers(Str $string = '###') {
32 $string =~ s/\#/$self->random_digit/eg;
33 $string =~ s/\%/$self->random_digit_not_zero/eg;
34  
35 return $string;
36 }
37  
38 method parse_format(Str $string = '') {
39 $string =~ s/\{\{\s?([#\.\w]+)\s?\}\}/$self->process_format($1)/eg;
40  
41 return $string;
42 }
43  
44 method process(Tuple[Str, Str] $lookup, Maybe[HashRef[Int]] $options) {
45 my $string;
46  
47 $string = $self->process_lookup($lookup);
48  
49 if ($options->{all_markers}) {
50 $string = $self->process_markers($string);
51 }
52 if ($options->{lex_markers}) {
53 $string = $self->format_lex_markers($string);
54 }
55 if ($options->{line_markers}) {
56 $string = $self->format_line_markers($string);
57 }
58 if ($options->{number_markers}) {
59 $string = $self->format_number_markers($string);
60 }
61  
62 return $string;
63 }
64  
65 method process_format(Str $token) {
66 my ($source, $content) = split /[#\.]/, $token;
67  
68 my $plugin = $self->plugin($source, faker => $self);
69  
70 return $plugin->$content if $plugin->can($content);
71  
72 return $self->process_lookup([$source, $content]);
73 }
74  
75 method process_lookup(Tuple[Str, Str] $lookup) {
76 my ($source, $datatype) = @$lookup;
77  
78 my @samples = (
79 $datatype,
80 "data_for_${datatype}",
81 "format_for_${datatype}"
82 );
83  
84 my $plugin = $self->plugin($source, faker => $self);
85  
86 for my $sample (@samples) {
87 $plugin->can($sample) or next;
88  
89 my $content = $plugin->$sample;
90 my $format = $content->[rand @$content];
91  
92 $format = join ' ', @$format if ref $format eq 'ARRAY';
93  
94 return $self->parse_format($format);
95 }
96  
97 $plugin = $self->plugin("${source}_${datatype}", faker => $self);
98  
99 return $plugin->execute;
100 }
101  
102 method process_markers(Str $string = '') {
103 my @markers = qw(
104 lex_markers
105 line_markers
106 number_markers
107 );
108  
109 for my $marker (@markers) {
110 my $filter = "format_${marker}";
111  
112 $string = $self->$filter($string);
113 }
114  
115 return $string;
116 }
117  
118 method random_between(Maybe[Int] $from, Maybe[Int] $to) {
119 my $max = 2147483647;
120  
121 $from = 0 if !$from || $from > $max;
122 $to = $max if !$to || $to > $max;
123  
124 return $from + int rand($to - $from);
125 }
126  
127 method random_digit() {
128  
129 return int rand(10);
130 }
131  
132 method random_digit_not_zero() {
133  
134 return 1 + int rand(9);
135 }
136  
137 method random_float(Maybe[Int] $place, Maybe[Int] $min, Maybe[Int] $max) {
138 $min //= 0;
139 $max //= $self->random_number;
140  
141 my $tmp; $tmp = $min and $min = $max and $max = $tmp if $min > $max;
142  
143 $place //= $self->random_digit_not_zero;
144  
145 return sprintf "%.${place}f", $min + rand() * ($max - $min);
146 }
147  
148 method random_item(ArrayRef | HashRef $items) {
149 return $self->random_array_item($items) if 'ARRAY' eq ref $items;
150  
151 return $self->random_hash_item($items) if 'HASH' eq ref $items;
152  
153 return undef;
154 }
155  
156 method random_array_item(ArrayRef $items) {
157  
158 return $items->[$self->random_between(0, scalar(@$items))];
159 }
160  
161 method random_hash_item(HashRef $items) {
162  
163 return $items->{$self->random_item([keys %$items])};
164 }
165  
166 method random_letter() {
167  
168 return chr $self->random_between(97, 122);
169 }
170  
171 method random_number(Maybe[Int] $from, Maybe[Int] $to) {
172 $to //= 0;
173 $from //= $self->random_digit;
174  
175 return $self->random_between($from, $to) if $to;
176  
177 return int rand 10 ** $from - 1;
178 }
179  
180 1;