-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path06_general.t.php
492 lines (398 loc) · 13.8 KB
/
06_general.t.php
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
<?php
require_once("Regexp_Assemble.php");
require_once("testutil.php");
{
$r = new Regexp_Assemble();
$r->add( 'a b' );
is( $r->dump(), "[a ' ' b]", 'dump path with space' );
$r->insert( 'a', ' ', 'b', 'c', 'd' );
is( $r->dump(), "[a ' ' b {* c=>[c d]}]",
'dump path with space 2' );
}
/*
# 06_general.t
#
# Test suite for Regexp::Assemble
# Check out the general functionality, now that all the subsystems have been exercised
#
# copyright (C) 2004-2007 David Landgren
use strict;
use Regexp::Assemble;
eval qq{use Test::More tests => 142 };
if( $@ ) {
warn "# Test::More not available, no tests performed\n";
print "1..1\nok 1\n";
exit 0;
}
use constant NR_GOOD => 45;
use constant NR_BAD => 529;
use constant NR_ERROR => 0;
my $fixed = 'The scalar remains the same';
$_ = $fixed;
*/
$ra = new Regexp_Assemble();
$ra->add( 'foo' ,'bar', 'rat' );
foreach(array('unfooled', 'disembark', 'vibration') as $target){
like( $target, "/$ra/", "match ok $target" );
}
ok( ! $ra->source(), 'source() undefined' );
foreach(array('unfooled', 'disembark', 'vibration') as $target){
unlike( $target, "/^$ra/", "anchored match not ok $target" );
}
$ra->reset();
foreach(array('unfooled', 'disembark', 'vibration') as $target){
unlike( $target, "/$ra/", "fail after reset $target" );
}
$ra->add( 'who', 'what', 'where', 'why', 'when' );
foreach(array('unfooled', 'disembark', 'vibration') as $target){
unlike( $target, "/$ra/", "fail ok $target" );
}
foreach(array('snowhouse', 'somewhat', 'nowhereness', 'whyever', 'nowhence') as $target){
like( $target, "/$ra/", "new match ok $target" );
}
$ra->reset()->mutable(1);
unlike( 'nothing', "/$ra/", "match nothing after reset" );
$ra->add( '^foo\\d+' );
like( 'foo12', "/$ra/", "match 1 ok foo12" );
unlike( 'nfoo12', "/$ra/", "match 1 nok nfoo12" );
unlike( 'bar6', "/$ra/", "match 1 nok bar6" );
ok( ! $ra->mvar(), 'mvar() undefined' );
$ra->add( 'bar\\d+' );
like( 'foo12', "/$ra/", "match 2 ok foo12" );
unlike( 'nfoo12', "/$ra/", "match 2 nok nfoo12" );
like( 'bar6', "/$ra/", "match 2 ok bar6" );
$ra->reset()->filter( function($arr) {
$r = [];
foreach($arr as $_) {
if ( preg_match("/\d /" , $_) ) {
$r[] = $_;
}
}
return ! count($r);
} );
$ra->add( '1 2 4' );
$ra->insert( '1', '2', '8*' );
unlike( '3 4 1 2', "/$ra/", 'filter nok 3 4 1 2' );
like( '3 1 2 4', "/$ra/", 'filter ok 3 1 2 4' );
unlike( '5 2 3 4', "/$ra/", 'filter ok 5 2 3 4' );
$ra->add( '2 3 a+' );
$ra->insert( '2', ' ', '3', ' ', 'a+' );
unlike( '5 2 3 4', "/$ra/", 'filter ok 5 2 3 4 (2)' );
//unlike( '5 2 3 aaa', "/$ra/", 'filter nok 5 2 3 a+' );
$ra->reset()->filter( NULL );
$ra->add( '1 2 a+' );
like( '5 1 2 aaaa', "/$ra/", 'filter now ok 5 1 2 a+' );
$ra->reset()->pre_filter( function ($p1){
$r = [];
if ( preg_match("/^#/" , $p1) ) {
return false;
}
return true;
} );
$ra->add( '#de' );
$ra->add( 'abc' );
unlike( '#de', "/^$ra$/", '#de not matched by comment-filtered assembly' );
like( 'abc', "/^$ra$/", 'abc matched by comment-filtered assembly' );
/* PHP CLONE するから意味はない
SKIP: {
skip( "is_deeply is broken in this version of Test::More (v$Test::More::VERSION)", 5 )
unless $Test::More::VERSION > 0.47;
{
my $orig = Regexp::Assemble->new;
my $clone = $orig->clone;
is_deeply( $orig, $clone, 'clone empty' );
}
{
my $orig = Regexp::Assemble->new->add( qw/ dig dug dog / );
my $clone = $orig->clone;
is_deeply( $orig, $clone, 'clone path' );
}
{
my $orig = Regexp::Assemble->new->add( qw/ dig dug dog / );
my $clone = $orig->clone;
$orig->add( 'digger' );
$clone->add( 'digger' );
is_deeply( $orig, $clone, 'clone then add' );
}
{
my $orig = Regexp::Assemble->new
->add( qw/ bird cat dog elephant fox/ );
my $clone = $orig->clone;
is_deeply( $orig, $clone, 'clone node' );
}
{
my $orig = Regexp::Assemble->new
->add( qw/ after alter amber cheer steer / );
my $clone = $orig->clone;
is_deeply( $orig, $clone, 'clone more' );
}
}
SKIP: {
# If the Storable module is available, we will have used
# that above, however, we will not have tested the pure-Perl
# fallback routines.
skip( 'Pure-Perl clone() already tested', 5 )
unless $Regexp::Assemble::have_Storable;
skip( "is_deeply is broken in this version of Test::More (v$Test::More::VERSION)", 5 )
unless $Test::More::VERSION > 0.47;
local $Regexp::Assemble::have_Storable = 0;
{
my $orig = Regexp::Assemble->new;
my $clone = $orig->clone;
is_deeply( $orig, $clone, 'clone empty' );
}
{
my $orig = Regexp::Assemble->new->add( qw/ dig dug dog / );
my $clone = $orig->clone;
is_deeply( $orig, $clone, 'clone path' );
}
{
my $orig = Regexp::Assemble->new->add( qw/ dig dug dog / );
my $clone = $orig->clone;
$orig->add( 'digger' );
$clone->add( 'digger' );
is_deeply( $orig, $clone, 'clone then add' );
}
{
my $orig = Regexp::Assemble->new
->add( qw/ bird cat dog elephant fox/ );
my $clone = $orig->clone;
is_deeply( $orig, $clone, 'clone node' );
}
{
my $orig = Regexp::Assemble->new
->add( qw/ after alter amber cheer steer / );
my $clone = $orig->clone;
is_deeply( $orig, $clone, 'clone more' );
}
}
*/
{
$r = new Regexp_Assemble();
$r->add( 'dig', 'dug' );
is( $r->dump(), '[d {i=>[i g] u=>[u g]}]', 'dump path' );
}
{
$r = new Regexp_Assemble();
$r->add( 'a b' );
is( $r->dump(), "[a ' ' b]", 'dump path with space' );
$r->insert( 'a', ' ', 'b', 'c', 'd' );
is( $r->dump(), "[a ' ' b {* c=>[c d]}]",
'dump path with space 2' );
}
{
$r = new Regexp_Assemble();
$r->add( 'dog', 'cat' );
is( $r->dump(), '[{c=>[c a t] d=>[d o g]}]', 'dump node' );
}
{
$r = new Regexp_Assemble();
$r->add( 'house', 'home' );
$r->insert();
is( $r->dump(), '[{* h=>[h o {m=>[m e] u=>[u s e]}]}]',
'add opt to path' );
}
{
$r = new Regexp_Assemble();
$r->add('dog', 'cat');
$r->insert();
is( $r->dump(), '[{* c=>[c a t] d=>[d o g]}]',
'add opt to node' );
}
{
$slide = new Regexp_Assemble();
is( $slide->add( 'schoolkids', 'acids', 'acidoids' )->as_string(),
'(?:ac(?:ido)?|schoolk)ids', 'schoolkids acids acidoids' );
is( $slide->add( 'schoolkids', 'acidoids' )->as_string(),
'(?:schoolk|acido)ids', 'schoolkids acidoids' );
is( $slide->add( 'nonschoolkids', 'nonacidoids' )->as_string(),
'non(?:schoolk|acido)ids', 'nonschoolkids nonacidoids' );
}
{
$r = new Regexp_Assemble();
is( $r
->add( 'sing', 'singing')
->as_string(), 'sing(?:ing)?', 'super slide sing singing' # no sliding done
);
$r = new Regexp_Assemble();
is( $r
->add( 'sing', 'singing', 'sling')
->as_string(), 's(?:(?:ing)?|l)ing',
'super slide sing singing sling'
);
$r = new Regexp_Assemble();
is( $r
->add( 'sing', 'singing', 'sling', 'slinging')
->as_string(), 'sl?(?:ing)?ing',
'super slide sing singing sling slinging'
);
$r = new Regexp_Assemble();
is( $r
->add( 'sing', 'singing', 'sling', 'slinging', 'sting', 'stinging')
->as_string(), 's[lt]?(?:ing)?ing',
'super slide sing singing sling slinging sting stinging'
);
$r = new Regexp_Assemble();
is( $r
->add( 'sing','singing','sling','slinging','sting','stinging','string','stringing','swing','swinging' )
->as_string(), 's(?:[lw]|tr?)?(?:ing)?ing',
'super slide sing singing sling slinging sting stinging string stringing swing swinging'
);
}
{
$re = new Regexp_Assemble([ 'flags' => 'i' ]);
$re->add( '^ab', '^are', 'de' );
like( 'able', "/$re/", '{^ab ^are de} /i matches able' );
like( 'About', "/$re/", '{^ab ^are de} /i matches About' );
unlike( 'bare', "/$re/", '{^ab ^are de} /i fails bare' );
like( 'death', "/$re/", '{^ab ^are de} /i matches death' );
like( 'DEEP', "/$re/", '{^ab ^are de} /i matches DEEP' );
}
{
$re = new Regexp_Assemble();
$re->add( 'abc', 'def', 'ghi' );
is( $re->stats_add(), 3, "stats add 3x3" );
is( $re->stats_raw(), 9, "stats raw 3x3" );
is( $re->stats_cooked(), 9, "stats cooked 3x3" );
ok( ! $re->stats_dup() , "stats dup 3x3" );
$re->add( 'de' );
is( $re->stats_add(), 4, "stats add 3x3 +1" );
is( $re->stats_raw(), 11, "stats raw 3x3 +1" );
is( $re->stats_cooked(), 11, "stats cooked 3x3 +1" );
}
{
$re = new Regexp_Assemble();
$re->add( '\\Qabc.def.ghi\\E' );
is( $re->stats_add(), 1, "stats add qm" );
is( $re->stats_raw(), 15, "stats raw qm" );
is( $re->stats_cooked(), 13, "stats cooked qm" );
ok( ! $re->stats_dup() , "stats dup qm" );
}
{
$re = new Regexp_Assemble();
$re->add( 'abc\\,def', 'abc\\,def' );
is( $re->stats_add(), 1, "stats add unqm dup" );
is( $re->stats_raw(), 16, "stats raw unqm dup" );
is( $re->stats_cooked(), 7, "stats cooked unqm dup" );
is( $re->stats_dup(), 1, "stats dup unqm dup" );
is( $re->stats_length(), 0, "stats_length unqm dup" );
$str = $re->as_string();
is( $str, 'abc,def', "stats str unqm dup" );
is( $re->stats_length(), 7, "stats len unqm dup" );
}
{
$re = new Regexp_Assemble();
$re->add( '' );
is( $re->stats_add(), 1, "stats add empty" );
is( $re->stats_raw(), 0, "stats raw empty" );
ok( ! $re->stats_cooked() , "stats cooked empty" );
ok( ! $re->stats_dup() , "stats dup empty" );
}
{
$re = new Regexp_Assemble();
is( $re->stats_add(), 0, "stats_add empty" );
is( $re->stats_raw(), 0, "stats_raw empty" );
is( $re->stats_cooked(), 0, "stats_cooked empty" );
is( $re->stats_dup(), 0, "stats_dup empty" );
is( $re->stats_length(), 0, "stats_length empty" );
$str = $re->as_string();
is( $str, Regexp_Assemble::Always_Fail, "stats str empty" ); # tricky!
is( $re->stats_length(), 0, "stats len empty" );
}
{
$re = new Regexp_Assemble();
$re->add( '\\Q.+\\E', '\\Q.+\\E', '\\Q.*\\E' );
is( $re->stats_add(), 2, "stats_add 2" );
is( $re->stats_raw(), 18, "stats_raw 2" );
is( $re->stats_cooked(), 8, "stats_cooked 2" );
is( $re->stats_dup(), 1, "stats_dup 2" );
is( $re->stats_length(), 0, "stats_length 2" );
$str = $re->as_string();
is( $str, '\\.[*+]', "stats str 2" );
is( $re->stats_length(), 6, "stats len 2 <$str>" );
}
{
# CPAN bug #24171
# given a list of strings
$str = [ 'a b', 'awb', 'a1b', 'bar', "a\nb" ];
foreach( array('s', 'w', 'd') as $meta) {
# given a list of patterns
$re_list = [ "a\\{$meta}b", "a\\@{[uc{$meta}]}b" ];
# produce an assembled pattern
$r = new Regexp_Assemble();
$re = $r->add($re_list)->re();
$r2 = new Regexp_Assemble();
$re_fold = $r2->fold_meta_pairs(0)->add($re_list)->re();
# test it against the strings
foreach($str as $s) {
# any match?
$ok = 0;
foreach($re_list as $_) {
if ( preg_match("/$_/" , $s) ) {
$ok = 1;
}
}
# does the assemble regexp match as well?
$ptr = $s;
// $ptr =~ s/\\/\\\\/;
$ptr = preg_replace('/\\\\/' , '/\\\\\\\\/' , $ptr);
// $ptr =~ s/\n/\\n/;
$ptr = preg_replace('/\n/' , '/\\\\n/' , $ptr);
// my $bug_success = ($s =~ /\n/) ? 0 : 1;
$bug_success = (preg_match("/\n/" , $s)) ? 0 : 1;
$bug_fail = 1 - $bug_success;
////保留
// is( preg_match("/$re/" , $s ) ? $bug_success : $bug_fail, $ok,
// "Folded meta pairs behave as list for \\$meta ($ptr,ok=$ok/$bug_success/$bug_fail)"
// );
is( ( preg_match("/$re_fold/",$s)) ? 1 : 0, $ok,
"Unfolded meta pairs behave as list for \\$meta ($ptr,ok=$ok)"
);
}
}
}
{
$u = new Regexp_Assemble(['unroll_plus' => 1]);
$u->add( "a+b", 'ac' );
$str = $u->as_string();
is( $str, 'a(?:a*b|c)', 'unroll plus a+b ac' );
$u->add( "\\LA+B", "ac" );
$str = $u->as_string();
is( $str, 'a(?:a*b|c)', 'unroll plus \\LA+B ac' );
$u->add( "\\Ua+?b", "AC" );
$str = $u->as_string();
is( $str, 'A(?:A*?B|C)', 'unroll plus \\Ua+?b AC' );
$u->add( '\\d+d', '\\de', '\\w+?x', '\\wy');
$str = $u->as_string();
is( $str, '(?:\\w(?:\\w*?x|y)|\\d(?:\d*d|e))', 'unroll plus \\d and \\w' );
//
//PHPではUTF-8以外のマルチバイトは扱えません。
// $u->add( '\\xab+f', '\\xabg', '\\xcd+?h', '\\xcdi');
// $str = $u->as_string();
// is( $str, "(?:\xcd(?:\xcd*?h|i)|\xab(?:\xab*f|g))", 'unroll plus meta x' );
$u->add( '[a-e]+h', '[a-e]i', '[f-j]+?k', '[f-j]m');
$str = $u->as_string();
is( $str, "(?:[f-j](?:[f-j]*?k|m)|[a-e](?:[a-e]*h|i))", 'unroll plus class' );
$u->add( "a+b" );
$str = $u->as_string();
is( $str, "a+b", 'reroll a+b' );
$u->add( "a+b", "a+" );
$str = $u->as_string();
is( $str, "a+b?", 'reroll a+b?' );
$u->add( "a+?b", "a+?" );
$str = $u->as_string();
is( $str, "a+?b?", 'reroll a+?b?' );
$u->unroll_plus(0)->add( '1+2', '13' );
$str = $u->as_string();
is( $str, "(?:1+2|13)", 'no unrolling' );
$u->unroll_plus()->add( '1+2', '13' );
$str = $u->as_string();
is( $str, "1(?:1*2|3)", 'unrolling again via implicit' );
$u->add('d+ldrt', 'd+ndrt', 'd+ldt', 'd+ndt', 'd+x');
$str = $u->as_string();
is( $str, 'd+(?:[ln]dr?t|x)', 'visit ARRAY codepath' );
}
/*
is( $_, 'eq', $fixed, '$_ has not been altered' );
*/
echo "===OK===\n";