|
| 1 | +#!/usr/bin/env perl |
| 2 | + |
| 3 | +use strict; |
| 4 | +use warnings; |
| 5 | +use Test::More; |
| 6 | + |
| 7 | +BEGIN { |
| 8 | + use_ok('Data::Random::String::Matches'); |
| 9 | +} |
| 10 | + |
| 11 | +# =========================================================================== |
| 12 | +# suggest_simpler_pattern() tests |
| 13 | +# =========================================================================== |
| 14 | + |
| 15 | +subtest 'suggest_simpler_pattern - simple patterns need no changes' => sub { |
| 16 | + my $gen = Data::Random::String::Matches->new(qr/\d{4}/); |
| 17 | + my $suggestion = $gen->suggest_simpler_pattern(); |
| 18 | + |
| 19 | + is($suggestion, undef, 'Simple pattern returns undef'); |
| 20 | +}; |
| 21 | + |
| 22 | +subtest 'suggest_simpler_pattern - very complex patterns' => sub { |
| 23 | + my $gen = Data::Random::String::Matches->new( |
| 24 | + qr/(?<id>\d{3})-(\w+)-\k<id>|[A-Z]{10}(?=\d)(?!X)/ |
| 25 | + ); |
| 26 | + my $suggestion = $gen->suggest_simpler_pattern(); |
| 27 | + |
| 28 | + ok(defined $suggestion, 'Returns suggestion for complex pattern'); |
| 29 | + ok(exists $suggestion->{reason}, 'Has reason'); |
| 30 | + ok(exists $suggestion->{tips}, 'Has tips'); |
| 31 | + like($suggestion->{reason}, qr/complex/i, 'Mentions complexity'); |
| 32 | +}; |
| 33 | + |
| 34 | +subtest 'suggest_simpler_pattern - large quantifier ranges' => sub { |
| 35 | + my $gen = Data::Random::String::Matches->new(qr/\d{5,25}/); |
| 36 | + my $suggestion = $gen->suggest_simpler_pattern(); |
| 37 | + |
| 38 | + ok(defined $suggestion, 'Suggests simplification for large range'); |
| 39 | + ok(defined $suggestion->{pattern}, 'Provides alternative pattern'); |
| 40 | + like($suggestion->{pattern}, qr/\{\d+\}/, 'Suggests fixed quantifier'); |
| 41 | + like($suggestion->{reason}, qr/range/i, 'Explains about range'); |
| 42 | +}; |
| 43 | + |
| 44 | +subtest 'suggest_simpler_pattern - small quantifier ranges ok' => sub { |
| 45 | + my $gen = Data::Random::String::Matches->new(qr/\d{3,5}/); |
| 46 | + my $suggestion = $gen->suggest_simpler_pattern(); |
| 47 | + |
| 48 | + # Small ranges might not trigger suggestion, or might for other reasons |
| 49 | + ok(1, 'Handles small ranges'); |
| 50 | +}; |
| 51 | + |
| 52 | +subtest 'suggest_simpler_pattern - too many alternations' => sub { |
| 53 | + my $pattern = '(' . join('|', ('a'..'z')) . ')'; # 26 alternations |
| 54 | + my $gen = Data::Random::String::Matches->new(qr/$pattern/); |
| 55 | + my $suggestion = $gen->suggest_simpler_pattern(); |
| 56 | + |
| 57 | + ok(defined $suggestion, 'Suggests simplification for many alternations'); |
| 58 | + like($suggestion->{reason}, qr/alternation/i, 'Mentions alternations'); |
| 59 | + ok(ref($suggestion->{tips}) eq 'ARRAY', 'Provides tips array'); |
| 60 | +}; |
| 61 | + |
| 62 | +subtest 'suggest_simpler_pattern - character class suggestion' => sub { |
| 63 | + my $gen = Data::Random::String::Matches->new(qr/(a|b|c)/); |
| 64 | + my $suggestion = $gen->suggest_simpler_pattern(); |
| 65 | + |
| 66 | + if (defined $suggestion) { |
| 67 | + like($suggestion->{pattern}, qr/\[abc\]/, 'Suggests character class'); |
| 68 | + like($suggestion->{reason}, qr/character class/i, 'Explains character class benefit'); |
| 69 | + } else { |
| 70 | + ok(1, 'Pattern acceptable as-is'); |
| 71 | + } |
| 72 | +}; |
| 73 | + |
| 74 | +subtest 'suggest_simpler_pattern - backreferences' => sub { |
| 75 | + my $gen = Data::Random::String::Matches->new(qr/(\w{3})-\1/); |
| 76 | + my $suggestion = $gen->suggest_simpler_pattern(); |
| 77 | + |
| 78 | + if (defined $suggestion) { |
| 79 | + like($suggestion->{reason}, qr/backreference/i, 'Mentions backreferences'); |
| 80 | + ok(ref($suggestion->{tips}) eq 'ARRAY', 'Provides tips'); |
| 81 | + ok(scalar @{$suggestion->{tips}} > 0, 'Has at least one tip'); |
| 82 | + } else { |
| 83 | + ok(1, 'Pattern acceptable as-is'); |
| 84 | + } |
| 85 | +}; |
| 86 | + |
| 87 | +subtest 'suggest_simpler_pattern - lookaheads' => sub { |
| 88 | + my $gen = Data::Random::String::Matches->new(qr/\d{3}(?=[A-Z])/); |
| 89 | + my $suggestion = $gen->suggest_simpler_pattern(); |
| 90 | + |
| 91 | + ok(defined $suggestion, 'Suggests removing lookahead'); |
| 92 | + is($suggestion->{pattern}, '(?^:\d{3})', 'Removes lookahead from pattern'); |
| 93 | + like($suggestion->{reason}, qr/lookahead/i, 'Explains lookahead issue'); |
| 94 | +}; |
| 95 | + |
| 96 | +subtest 'suggest_simpler_pattern - lookbehinds' => sub { |
| 97 | + my $gen = Data::Random::String::Matches->new(qr/(?<=PRE)\d{3}/); |
| 98 | + my $suggestion = $gen->suggest_simpler_pattern(); |
| 99 | + |
| 100 | + ok(defined $suggestion, 'Suggests removing lookbehind'); |
| 101 | + is($suggestion->{pattern}, '(?^:\d{3})', 'Removes lookbehind from pattern'); |
| 102 | + like($suggestion->{reason}, qr/lookbehind/i, 'Explains lookbehind issue'); |
| 103 | +}; |
| 104 | + |
| 105 | +subtest 'suggest_simpler_pattern - unicode to ascii' => sub { |
| 106 | + my $gen = Data::Random::String::Matches->new(qr/\p{L}{5}/); |
| 107 | + my $suggestion = $gen->suggest_simpler_pattern(); |
| 108 | + |
| 109 | + ok(defined $suggestion, 'Suggests ASCII alternative'); |
| 110 | + like($suggestion->{pattern}, qr/\[A-Za-z\]/, 'Suggests ASCII character class'); |
| 111 | + like($suggestion->{reason}, qr/ASCII/i, 'Explains ASCII benefit'); |
| 112 | +}; |
| 113 | + |
| 114 | +subtest 'suggest_simpler_pattern - return structure' => sub { |
| 115 | + my $gen = Data::Random::String::Matches->new(qr/\d{5,25}/); |
| 116 | + my $suggestion = $gen->suggest_simpler_pattern(); |
| 117 | + |
| 118 | + if (defined $suggestion) { |
| 119 | + is(ref($suggestion), 'HASH', 'Returns hashref'); |
| 120 | + ok(exists $suggestion->{reason}, 'Has reason key'); |
| 121 | + ok(exists $suggestion->{tips}, 'Has tips key'); |
| 122 | + |
| 123 | + is(ref($suggestion->{tips}), 'ARRAY', 'Tips is arrayref'); |
| 124 | + ok(scalar @{$suggestion->{tips}} > 0, 'Tips not empty'); |
| 125 | + } |
| 126 | +}; |
| 127 | + |
| 128 | +subtest 'suggest_simpler_pattern - multiple issues' => sub { |
| 129 | + my $gen = Data::Random::String::Matches->new(qr/\p{L}{5,50}(?=[A-Z])/); |
| 130 | + my $suggestion = $gen->suggest_simpler_pattern(); |
| 131 | + |
| 132 | + # Should catch at least one issue |
| 133 | + ok(defined $suggestion, 'Detects issues in pattern with multiple problems'); |
| 134 | +}; |
| 135 | + |
| 136 | +subtest 'suggest_simpler_pattern - various simple patterns' => sub { |
| 137 | + my @simple = ( |
| 138 | + qr/\d{4}/, |
| 139 | + qr/[A-Z]{3}/, |
| 140 | + qr/\w{5}/, |
| 141 | + qr/[a-z]{2,4}/, |
| 142 | + ); |
| 143 | + |
| 144 | + for my $pattern (@simple) { |
| 145 | + my $gen = Data::Random::String::Matches->new($pattern); |
| 146 | + my $suggestion = $gen->suggest_simpler_pattern(); |
| 147 | + |
| 148 | + # These should either return undef or have valid suggestions |
| 149 | + if (defined $suggestion) { |
| 150 | + ok(exists $suggestion->{reason}, "Pattern $pattern: has reason if suggesting"); |
| 151 | + } else { |
| 152 | + pass("Pattern $pattern: no suggestion needed"); |
| 153 | + } |
| 154 | + } |
| 155 | +}; |
| 156 | + |
| 157 | +subtest 'suggest_simpler_pattern - tip structure' => sub { |
| 158 | + my $gen = Data::Random::String::Matches->new(qr/\d{5,25}/); |
| 159 | + my $suggestion = $gen->suggest_simpler_pattern(); |
| 160 | + |
| 161 | + if (defined $suggestion && exists $suggestion->{tips}) { |
| 162 | + for my $tip (@{$suggestion->{tips}}) { |
| 163 | + ok(length($tip) > 0, 'Tip is not empty string'); |
| 164 | + unlike($tip, qr/^\s*$/, 'Tip is not just whitespace'); |
| 165 | + } |
| 166 | + } |
| 167 | +}; |
| 168 | + |
| 169 | +subtest 'suggest_simpler_pattern - pattern key validity' => sub { |
| 170 | + my @patterns_with_suggestions = ( |
| 171 | + qr/\d{5,25}/, |
| 172 | + qr/\d{3}(?=[A-Z])/, |
| 173 | + qr/\p{L}{5}/, |
| 174 | + ); |
| 175 | + |
| 176 | + for my $pattern (@patterns_with_suggestions) { |
| 177 | + my $gen = Data::Random::String::Matches->new($pattern); |
| 178 | + my $suggestion = $gen->suggest_simpler_pattern(); |
| 179 | + |
| 180 | + if (defined $suggestion && defined $suggestion->{pattern}) { |
| 181 | + # Try to compile the suggested pattern |
| 182 | + eval { qr/$suggestion->{pattern}/ }; |
| 183 | + ok(!$@, "Suggested pattern is valid regex: $suggestion->{pattern}"); |
| 184 | + } |
| 185 | + } |
| 186 | +}; |
| 187 | + |
| 188 | +subtest 'suggest_simpler_pattern - integration with pattern_info' => sub { |
| 189 | + my $gen = Data::Random::String::Matches->new(qr/\d{10,50}/); |
| 190 | + |
| 191 | + my $info = $gen->pattern_info(); |
| 192 | + my $suggestion = $gen->suggest_simpler_pattern(); |
| 193 | + |
| 194 | + ok(defined $info, 'Can get pattern info'); |
| 195 | + if (defined $suggestion) { |
| 196 | + ok(defined $suggestion->{reason}, 'Suggestion based on pattern analysis'); |
| 197 | + } |
| 198 | +}; |
| 199 | + |
| 200 | +done_testing(); |
0 commit comments