Generator/Honest.pm untested

filter: Statementstmt 0.0% 0 / 82 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 11 Podpod n/a totaltotal 0.0% 0 / 120 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 package ABNF::Generator::Honest;
2  
3 =pod
4  
5 =head1 NAME
6  
7 B<ABNF::Generator::Honest> - class to generate valid messages for ABNF-based generators
8  
9 It have $RECURSION_LIMIT = 16. You can change it to increase lower alarm bound on choices and repetition recursion.
10 but use it carefully!
11  
12 =head1 INHERITANCE
13  
14 B<ABNF::Generator::Honest>
15 isa B<ABNF::Generator>
16  
17 =head1 DESCRIPTION
18  
19 =head1 METHODS
20  
21 =cut
22  
23 use 5.014;
24  
25 use strict;
26 use warnings;
27 no warnings "recursion";
28  
29 use Data::Dumper;
30 use Readonly;
31 use List::Util qw(reduce);
32  
33 use POSIX;
34  
35 use base qw(ABNF::Generator Exporter);
36  
37 use Method::Signatures; #some bug in B<Devel::Declare>...
38  
39 use ABNF::Generator qw($CONVERTERS);
40  
41 our @EXPORT_OK = qw(Honest);
42 our $RECURSION_LIMIT = 16;
43  
44 =pod
45  
46 =head1 ABNF::Generator::Honest->C<new>($grammar, $validator?)
47  
48 Creates a new B<ABNF::Generator::Honest> object.
49  
50 $grammar isa B<ABNF::Grammar>.
51  
52 $validator isa B<ABNF::Validator>.
53  
54 =cut
55  
56 method new(ABNF::Grammar $grammar, ABNF::Validator $validator?) {
57 $self->SUPER::new($grammar, $validator ? $validator : ());
58 }
59  
60 =pod
61  
62 =head1 $honest->C<generate>($rule, $tail="")
63  
64 Generates one valid sequence string for command $rule.
65  
66 Using cache $self->{_cache}->{$rule} for this rule, that speeds up this call.
67  
68 $rule is a command name.
69  
70 $tail is a string added to result if it absent.
71  
72 dies if there is no command like $rule.
73  
74 =cut
75  
76 method _range($rule, $recursion) {
77 my $converter = $CONVERTERS->{$rule->{type}};
78 my $min = $converter->($rule->{min});
79 my $max = $converter->($rule->{max});
80 return {class => "Atom", value => chr($min + int(rand($max - $min + 1)))};
81 }
82  
83 method _string($rule, $recursion) {
84 my $converter = $CONVERTERS->{$rule->{type}};
85 return {
86 class => "Atom",
87 value => join("", map { chr($converter->($_)) } @{$rule->{value}})
88 };
89 }
90  
91 method _literal($rule, $recursion) {
92 return {class => "Atom", value => $rule->{value}};
93 }
94  
95 method _repetition($rule, $recursion) {
96 my $min = $rule->{min};
97 my $count = ($rule->{max} || LONG_MAX) - $min;
98 my @result = ();
99  
100 push(@result, $self->_generateChain($rule->{value}, $recursion)) while $min--;
101 if ( $recursion->{level} < $RECURSION_LIMIT ) {
102 push(@result, $self->_generateChain($rule->{value}, $recursion)) while $count-- && int(rand(2));
103 }
104  
105 return {class => "Sequence", value => \@result};
106 }
107  
108 method _proseValue($rule, $recursion) {
109 return $self->_generateChain($rule->{name}, $recursion);
110 }
111  
112 method _reference($rule, $recursion) {
113 return $self->_generateChain($rule->{name}, $recursion);
114 }
115  
116 method _group($rule, $recursion) {
117 my @result = ();
118 foreach my $elem ( @{$rule->{value}} ) {
119 push(@result, $self->_generateChain($elem, $recursion));
120 }
121  
122 return {class => "Sequence", value => \@result};
123 }
124  
125 method _choice($rule, $recursion) {
126 $recursion->{level}++;
127 my @result = ();
128 if ( $recursion->{level} < $RECURSION_LIMIT ) {
129 foreach my $choice ( @{$rule->{value}} ) {
130 push(@result, $self->_generateChain($choice, $recursion));
131 }
132 } else {
133 $recursion->{choices} ||= {};
134 my $candidate = reduce {
135 if ( not exists($recursion->{choices}->{$a}) ) {
136 $b
137 } elsif ( not exists($recursion->{choices}->{$b}) ) {
138 $a
139 } else {
140 $recursion->{choices}->{$a} <=> $recursion->{choices}->{$b}
141 }
142 } @{$rule->{value}};
143 $recursion->{choices}->{$candidate}++;
144 push(@result, $self->_generateChain( $candidate, $recursion));
145 $recursion->{choices}->{$candidate}--;
146 }
147 $recursion->{level}--;
148  
149 return {class => "Choice", value => \@result};
150 }
151  
152 method _rule($rule, $recursion) {
153 return $self->_generateChain($rule->{value}, $recursion);
154 }
155  
156 =pod
157  
158 =head1 $honest->C<withoutArguments>($name, $tail="")
159  
160 Return a string starts like command $name and without arguments if command may have no arguments.
161  
162 Return an empty string otherwise.
163  
164 $tail is a string added to result if it absent.
165  
166 dies if there is no command like $rule.
167  
168 =cut
169  
170 method withoutArguments(Str $name, Str $tail="") {
171 my $result = $self->SUPER::withoutArguments($name, $tail);
172 return $self->{_validator}->validate($name, $result) ? $result : "";
173 }
174  
175 =pod
176  
177 =head1 FUNCTIONS
178  
179 =head1 C<Honest>()
180  
181 Return __PACKAGE__ to reduce class name :3
182  
183 =cut
184  
185 func Honest() {
186 return __PACKAGE__;
187 }
188  
189 1;
190  
191 =pod
192  
193 =head1 AUTHOR / COPYRIGHT / LICENSE
194  
195 Copyright (c) 2013 Arseny Krasikov <nyaapa@cpan.org>.
196  
197 This module is licensed under the same terms as Perl itself.
198  
199 =cut