File Coverage

blib/lib/Parse/RandGen/Literal.pm
Criterion Covered Total %
statement 42 45 93.3
branch 16 22 72.7
condition 5 6 83.3
subroutine 7 8 87.5
pod 1 3 33.3
total 71 84 84.5


line stmt bran cond sub pod time code
1             # $Revision: #3 $$Date: 2005/08/31 $$Author: jd150722 $
2             ######################################################################
3             #
4             # This program is Copyright 2003-2005 by Jeff Dutton.
5             #
6             # This program is free software; you can redistribute it and/or modify
7             # it under the terms of either the GNU General Public License or the
8             # Perl Artistic License.
9             #
10             # This program is distributed in the hope that it will be useful,
11             # but WITHOUT ANY WARRANTY; without even the implied warranty of
12             # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13             # GNU General Public License for more details.
14             #
15             # If you do not have a copy of the GNU General Public License write to
16             # the Free Software Foundation, Inc., 675 Mass Ave, Cambridge,
17             # MA 02139, USA.
18             ######################################################################
19              
20             package Parse::RandGen::Literal;
21              
22             require 5.006_001;
23 4     4   20 use Carp;
  4         8  
  4         298  
24 4     4   21 use Parse::RandGen qw($Debug);
  4         6  
  4         424  
25 4     4   19 use strict;
  4         8  
  4         134  
26 4     4   21 use vars qw(@ISA $ValidLiteralRE $Debug);
  4         5  
  4         8737  
27             @ISA = ('Parse::RandGen::Condition');
28              
29             # This regular expression defines whether a given regexp condition is valid (i.e. can be understood by the Condition module)
30             # To be valid, the regexp must
31             $ValidLiteralRE = qr /
32             ([\'\"]) # Match either a single- or double-quote delimiter
33             (?: [^\\\1] | \\. )+ # In the middle of the regexp, match either <1> a character that is not a backslash or the delimiter or <2> a backslash followed by any character
34             \1 # Match the original delimiter that was found
35             /x;
36              
37             sub _newDerived {
38 7 50   7   21 my $self = shift or confess("%Error: Cannot call without a valid object!");
39 7         14 my $type = ref($self);
40 7         28 my $elemRef = ref($self->element());
41 7 50       37 (!$elemRef) or confess("%Error: $type has an element is a reference (ref=\"$elemRef\") instead of a literal scalar!");
42             }
43              
44             sub dump {
45 400 50   400 0 1050 my $self = shift or confess("%Error: Cannot call without a valid object!");
46 400         1037 return ("'" . $self->element() . "'");
47             }
48              
49             sub pick {
50 805 50   805 1 2002 my $self = shift or confess("%Error: Cannot call without a valid object!");
51 805         2830 my %args = ( match=>1, # Default is to pick matching data
52             @_ );
53 805         2402 my $val = $self->element(); # Reset to element before each attempt
54 805         1500 my $keepTrying = 10;
55 805         2351 my $length = length($self->element());
56 805 50       3748 confess "Literal length is 0! This should never be!\n" unless ($length);
57              
58 805         1070 my ($method, $char);
59 805   66     2919 while (!$args{match} && $keepTrying-- && ($val eq $self->element())) {
      100        
60 76         211 $val = $self->element(); # Reset to element before each corruption attempt
61 76         461 $method = int(rand(4)); # Method of corruption
62 76         110 $char = int(rand($length)); # Which character
63              
64 76 100       425 if ($method == 0) {
    100          
    100          
65             # Try changing the case of first character
66 17         64 substr($val, $char, 1) = lc(substr($val, $char, 1));
67 17 50       45 substr($val, $char, 1) = uc(substr($val, $char, 1)) unless ($val ne $self->element());
68             } elsif ($method == 1) {
69             # Randomly change the value of one of the characters
70 23         681 substr($val, $char, 1) = chr( (ord(substr($val, $char, 1)) + int(rand(256))) % 256 );
71             } elsif ($method == 2) {
72             # Insert a random character into the literal
73 16         35 $char = int(rand($length+1)); # Where to insert character
74 16         118 substr($val, $char, 0) = int(rand(256)) # Insert random character
75             } else {
76             # Remove a character
77 20         147 substr($val, $char, 1) = '';
78             }
79             }
80              
81 805         2664 my $elem = $self->element();
82 805 100       1789 if ($Debug) {
83 798 100       1685 if ($args{match}) {
84 722         3804 print ("Parse::RandGen::Literal($elem)::pick(match=>$args{match}) with value of ", $self->dumpVal($val), "\n");
85             } else {
86 76         390 print ("Parse::RandGen::Literal($elem)::pick(match=>$args{match}, method=>$method, char=>$char) with value of ", $self->dumpVal($val), "\n");
87             }
88             }
89 805         87135 return ($val);
90             }
91              
92             sub stripLiteral {
93 0     0 0   my $lit = shift;
94 0           $lit =~ s/([\'\"])(.+)\1/$2/;
95 0           return ($lit);
96             }
97              
98             ######################################################################
99             #### Package return
100             1;
101             __END__