Generator/Liar.pm untested

filter: Statementstmt 0.0% 0 / 74 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 10 Podpod n/a totaltotal 0.0% 0 / 109 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::Liar;
2  
3 =pod
4  
5 =head1 NAME
6  
7 B<ABNF::Generator::Liar> - class to generate invalid messages for ABNF-based generators
8  
9 =head1 INHERITANCE
10  
11 B<ABNF::Generator::Liar>
12 isa B<BNF::Generator>
13  
14 =head1 DESCRIPTION
15  
16 =head1 METHODS
17  
18 =cut
19  
20 use 5.014;
21  
22 use strict;
23 use warnings;
24  
25 use Readonly;
26 use Data::Dumper;
27 use Carp;
28  
29 use POSIX;
30  
31 use base qw(ABNF::Generator Exporter);
32  
33 use Method::Signatures; #some bug in B<Devel::Declare>...
34  
35 use ABNF::Grammar qw(splitRule $BASIC_RULES);
36  
37 Readonly my $STRING_LEN => 20;
38 Readonly my $CHARS => [map { chr($_) } (0 .. 0x0D - 1), (0x0D + 1 .. 255)];
39 Readonly my $ACHARS => [('A'..'Z', 'a'..'z')];
40 Readonly our $ENDLESS => 513 * 1024 / 4; # 513 kB of chars
41  
42 our @EXPORT_OK = qw(Liar);
43  
44 =pod
45  
46 =head1 ABNF::Generator::Liar->C<new>($grammar, $validator?)
47  
48 Creates a new B<ABNF::Generator::Liar> 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 $liar->C<generate>($rule, $tail="")
63  
64 Generates one invalid 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 _rule($rule, $recursion) {
77 my $result = "";
78  
79 if ( my $prefix = splitRule($rule) ) {
80 do {
81 $result = _stringRand($ACHARS);
82 } while $self->{_validator}->validateArguments($rule->{name}, $result);
83 $result = $prefix . $result;
84 } else {
85 do {
86 $result = _stringRand($ACHARS);
87 } while $self->{_validator}->validate($rule->{name}, $result);
88 }
89  
90 return {class => "Atom", value => $result};
91 }
92  
93 func _stringRand($chars, $len?) {
94 $len ||= rand($STRING_LEN) + 1;
95 my @gen = ();
96 for ( my $i = 0; $i < $len; $i++ ) {
97 push(@gen, @$chars[rand @$chars]);
98 }
99 return join("", @gen);
100 }
101  
102 =pod
103  
104 =head1 $liar->C<withoutArguments>($name, $tail="")
105  
106 Return a string starts like command $name and without arguments if it possible.
107  
108 Return an empty string if command may have no arguments.
109  
110 $tail is a string added to result if it absent.
111  
112 dies if there is no command like $rule.
113  
114 =cut
115  
116 method withoutArguments(Str $name, Str $tail="") {
117 my $result = $self->SUPER::withoutArguments($name, $tail);
118 return $self->{_validator}->validate($name, $result) ? "" : $result;
119 }
120  
121 =pod
122  
123 =head1 $liar->C<unExistedCommand>()
124  
125 Return an string starts with char sequence that doesn't match any command
126  
127 $tail is a string added to result if it absent.
128  
129 dies if there is no command like $rule.
130  
131 =cut
132  
133 method unExistedCommand(Str $tail="") {
134 my $result = "";
135 do {
136 $result = _stringRand($ACHARS);
137 } while $self->{_validator}->validateCommand($result);
138  
139 my $rx = eval { qr/$tail$/ };
140 croak "Bad tail" if $@;
141 return $result =~ $rx ? $result : $result . $tail;
142 }
143  
144 =pod
145  
146 =head1 $liar->C<endlessCommand>($name)
147  
148 Return an string starts like command $name and length more then $ENDLESS = 513 * 1024 / 4
149  
150 $tail is a string added to result if it absent.
151  
152 dies if there is no command like $rule.
153  
154 =cut
155  
156 method endlessCommand($name, Str $tail="") {
157 croak "Unexisted commadn $name" unless $self->hasCommand($name);
158 my $prefix = splitRule($self->{_grammar}->rule($name));
159 my $result = $prefix . _stringRand($ACHARS, $ENDLESS);
160 my $rx = eval { qr/$tail$/ };
161 croak "Bad tail" if $@;
162 return $result =~ $rx ? $result : $result . $tail;
163 }
164  
165 =pod
166  
167 =head1 FUNCTIONS
168  
169 =head1 C<Liar>()
170  
171 Return __PACKAGE__ to reduce class name :3
172  
173 =cut
174  
175 func Liar() {
176 return __PACKAGE__;
177 }
178  
179 1;
180  
181 =pod
182  
183 =head1 AUTHOR / COPYRIGHT / LICENSE
184  
185 Copyright (c) 2013 Arseny Krasikov <nyaapa@cpan.org>.
186  
187 This module is licensed under the same terms as Perl itself.
188  
189 =cut