1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
|
#!perl -T
use Test::More;
my $regexp_tests = [
{
input => {
regexp => '',
},
output => undef,
message => 'empty regexp'
},
{
input => {
regexp => { regexp => qr/(a)(.+?)(\1)/ },
},
output => undef,
message => 'regexp is a hashref'
},
{
input => {
regexp => [ qr/(a)(.+?)(\1)/ ],
},
output => undef,
message => 'regexp is an arrayref'
},
{
input => {
regexp => qr/(a)(.+?)(\1)/,
},
output => {
regexp => ( $] < 5.013006 ) ? '(?-xism:(a)(.+?)(\1))' : '(?^:(a)(.+?)(\1))'
},
message => 'regexp is a regexp object'
},
{
input => {
regexp => '(a)(.+?)(\1)',
},
output => {
regexp => '(?sm:(a)(.+?)(\1))'
},
message => 'regexp is a scalar'
},
{
input => {
regexp => qr/(a)(.+?)(\1)/,
modifier => 's'
},
output => {
regexp => '(?s:(a)(.+?)(\1))'
},
message => 'regexp is a regexp object and modifier is set'
},
{
input => {
regexp => '(a)(.+?)(\1)',
modifier => 's'
},
output => {
regexp => '(?s:(a)(.+?)(\1))'
},
message => 'regexp is a scalar and modifier is set'
},
];
my $replacement_tests = [
{
input => {
regexp => '(a)(.+?)(\1)',
},
output => {
replacement => undef
},
message => 'empty replacement'
},
{
input => {
regexp => '(a)(.+?)(\1)',
replacement => [ 'foo' ],
},
output => undef,
message => 'replacement is an arrayref'
},
{
input => {
regexp => '(a)(.+?)(\1)',
replacement => { bar => 'foo' },
},
output => undef,
message => 'replacement is a hashref'
},
{
input => {
regexp => '(a)(.+?)(\1)',
replacement => 'foo'
},
output => {
replacement => 'foo'
},
message => 'replacement is a scalar'
},
{
input => {
regexp => '(a)(.+?)(\1)',
replacement => sub { return 'foo'; }
},
output => {
replacement => 'foo'
},
message => 'replacement is a coderef'
},
{
input => {
regexp => '(a)(.+?)(\1)',
replacement => sub { return 'foo'; },
store => 'bar'
},
output => {
replacement => sprintf( "\x01%d\x01", 1 )
},
message => 'replacement is a coderef and store is set'
},
{
input => {
regexp => '(a)(.+?)(\1)',
replacement => sub { return 'foo'; },
store => 'bar',
restore_pattern => 'baz'
},
output => {
replacement => 'foo'
},
message => 'replacement is a coderef and store and restore_pattern are set'
},
];
my $store_tests = [
{
input => {
regexp => '(a)(.+?)(\1)',
},
output => {
store => undef
},
message => 'store is undefined'
},
{
input => {
regexp => '(a)(.+?)(\1)',
store => '',
},
output => {
store => ''
},
message => 'store is empty'
},
{
input => {
regexp => '(a)(.+?)(\1)',
store => { regexp => qr/(a)(.+?)(\1)/ },
},
output => undef,
message => 'store is a hashref'
},
{
input => {
regexp => '(a)(.+?)(\1)',
store => [ qr/(a)(.+?)(\1)/ ],
},
output => undef,
message => 'store is an arrayref'
},
{
input => {
regexp => qr/(a)(.+?)(\1)/,
store => sub { return 'foo'; }
},
output => {
store => 'foo'
},
message => 'store is a coderef'
},
{
input => {
regexp => '(a)(.+?)(\1)',
store => 'bar'
},
output => {
store => 'bar'
},
message => 'store is a scalar'
}
];
my $modifier_tests = [
{
input => {
regexp => '(a)(.+?)(\1)',
modifier => { regexp => qr/(a)(.+?)(\1)/ },
},
output => undef,
message => 'modifier is a hashref'
},
{
input => {
regexp => '(a)(.+?)(\1)',
modifier => \'xsm',
},
output => undef,
message => 'modifier is a scalarref'
},
{
input => {
regexp => '(a)(.+?)(\1)',
},
output => {
regexp => '(?sm:(a)(.+?)(\1))'
},
message => 'modifier is undefined and regexp is a scalar'
},
{
input => {
regexp => qr/(a)(.+?)(\1)/,
},
output => {
regexp => ( $] < 5.013006 ) ? '(?-xism:(a)(.+?)(\1))' : '(?^:(a)(.+?)(\1))'
},
message => 'modifier is undefined and regexp is a regexp object'
},
];
my $restore_pattern_tests = [
{
input => {
regexp => '(a)(.+?)(\1)'
},
output => {
restore_pattern => ( $] < 5.013006 ) ? '(?-xism:\x01(\d+)\x01)' : '(?^:\x01(\d+)\x01)'
},
message => 'restore_pattern is undefined'
},
{
input => {
regexp => '(a)(.+?)(\1)',
restore_pattern => { regexp => qr/(a)(.+?)(\1)/ },
},
output => undef,
message => 'restore_pattern is a hashref'
},
{
input => {
regexp => '(a)(.+?)(\1)',
restore_pattern => [ qr/(a)(.+?)(\1)/ ],
},
output => undef,
message => 'restore_pattern is an arrayref'
},
{
input => {
regexp => '(a)(.+?)(\1)',
restore_pattern => qr/(a)(.+?)(\1)/,
},
output => {
restore_pattern => ( $] < 5.013006 ) ? '(?-xism:(a)(.+?)(\1))' : '(?^:(a)(.+?)(\1))'
},
message => 'restore_pattern is a regexp object'
},
{
input => {
regexp => '(a)(.+?)(\1)',
restore_pattern => '(a)(.+?)(\1)',
},
output => {
restore_pattern => ( $] < 5.013006 ) ? '(?-xism:(a)(.+?)(\1))' : '(?^:(a)(.+?)(\1))'
},
message => 'restore_pattern is a scalar'
},
];
SKIP: {
my $not = 1;
foreach ( @$regexp_tests, @$replacement_tests, @$store_tests, @$modifier_tests, @$restore_pattern_tests ) {
$not += 1;
$not += scalar( keys( %{$_->{output}} ) ) if ( $_->{output} );
}
eval( 'use Regexp::RegGrp::Data' );
skip( 'Regexp::RegGrp::Data not installed!', $not ) if ( $@ );
plan tests => $not;
my $data = Regexp::RegGrp::Data->new();
ok( ! $data, 'Regexp::RegGrp::Data->new() without args' );
foreach my $test ( @$regexp_tests, @$store_tests, @$replacement_tests, @$modifier_tests, @$restore_pattern_tests ) {
$data = Regexp::RegGrp::Data->new( $test->{input} );
ok(
! ( $data xor $test->{output} ),
'Data object ' . ( $test->{output} ? '' : 'not ' ) . 'created' . ( $test->{message} ? ' - ' . $test->{message} : '' )
);
if ( $test->{output} ) {
foreach my $accessor ( keys( %{$test->{output}} ) ) {
if ( defined( $test->{output}->{$accessor} ) ) {
if ( ref( $data->$accessor() ) eq 'CODE' ) {
my $args;
$args = { store_index => 1 } if ( $accessor eq 'replacement' );
cmp_ok(
$data->$accessor()->( $args ), 'eq', $test->{output}->{$accessor},
'Field "' . $accessor . '" correctly set' . ( $test->{message} ? ' - ' . $test->{message} : '' )
);
}
else {
cmp_ok(
$data->$accessor(), 'eq', $test->{output}->{$accessor},
'Field "' . $accessor . '" correctly set' . ( $test->{message} ? ' - ' . $test->{message} : '' )
);
}
}
else {
ok(
! $data->$accessor(),
'Field "' . $accessor . '" correctly set' . ( $test->{message} ? ' - ' . $test->{message} : '' )
);
}
}
}
}
}
|