the real fix for t/lib/b-stash.t
[p5sagit/p5-mst-13.2.git] / t / lib / filter-util.t
CommitLineData
2c4bb738 1BEGIN {
2 chdir('t') if -d 't';
3 @INC = '.';
4 push @INC, '../lib';
5 require Config; import Config;
5e506771 6 if ($Config{'extensions'} !~ m{\bFilter/Util/Call\b}) {
7 print "1..0 # Skip: Filter::Util::Call was not built\n";
2c4bb738 8 exit 0;
9 }
5e506771 10 require 'lib/filter-util.pl';
2c4bb738 11}
12
4176d4e4 13use strict;
14use warnings;
15
16use vars qw($Inc $Perl);
17
2c4bb738 18print "1..28\n" ;
19
20$Perl = "$Perl -w" ;
21
22use Cwd ;
4176d4e4 23my $here = getcwd ;
2c4bb738 24
2c4bb738 25
4176d4e4 26my $filename = "call.tst" ;
27my $filenamebin = "call.bin" ;
28my $module = "MyTest" ;
29my $module2 = "MyTest2" ;
30my $module3 = "MyTest3" ;
31my $module4 = "MyTest4" ;
32my $module5 = "MyTest5" ;
33my $nested = "nested" ;
34my $block = "block" ;
2c4bb738 35
36# Test error cases
37##################
38
39# no filter function in module
40###############################
41
42writeFile("${module}.pm", <<EOM) ;
43package ${module} ;
44
45use Filter::Util::Call ;
46
47sub import { filter_add(bless []) }
48
491 ;
50EOM
51
4176d4e4 52my $a = `$Perl "-I." $Inc -e "use ${module} ;" 2>&1` ;
2c4bb738 53ok(1, (($? >>8) != 0 or ($^O eq 'MSWin32' && $? != 0))) ;
54ok(2, $a =~ /^Can't locate object method "filter" via package "MyTest"/) ;
55
56# no reference parameter in filter_add
57######################################
58
59writeFile("${module}.pm", <<EOM) ;
60package ${module} ;
61
62use Filter::Util::Call ;
63
64sub import { filter_add() }
65
661 ;
67EOM
68
03ec374d 69$a = `$Perl "-I." $Inc -e "use ${module} ;" 2>&1` ;
2c4bb738 70ok(3, (($? >>8) != 0 or ($^O eq 'MSWin32' && $? != 0))) ;
71#ok(4, $a =~ /^usage: filter_add\(ref\) at ${module}.pm/) ;
72ok(4, $a =~ /^Not enough arguments for Filter::Util::Call::filter_add/) ;
73
74
75
76
77# non-error cases
78#################
79
80
81# a simple filter, using a closure
82#################
83
84writeFile("${module}.pm", <<EOM, <<'EOM') ;
85package ${module} ;
86
87EOM
88use Filter::Util::Call ;
89sub import {
90 filter_add(
91 sub {
92
93 my ($status) ;
94
95 if (($status = filter_read()) > 0) {
96 s/ABC/DEF/g
97 }
98 $status ;
99 } ) ;
100}
101
1021 ;
103EOM
104
105writeFile($filename, <<EOM, <<'EOM') ;
106
107use $module ;
108EOM
109
110use Cwd ;
111$here = getcwd ;
112print "I am $here\n" ;
113print "some letters ABC\n" ;
114$y = "ABCDEF" ;
115print <<EOF ;
116Alphabetti Spagetti ($y)
117EOF
118
119EOM
120
03ec374d 121$a = `$Perl "-I." $Inc $filename 2>&1` ;
2c4bb738 122ok(5, ($? >>8) == 0) ;
123ok(6, $a eq <<EOM) ;
124I am $here
125some letters DEF
126Alphabetti Spagetti (DEFDEF)
127EOM
128
129# a simple filter, not using a closure
130#################
131
132writeFile("${module}.pm", <<EOM, <<'EOM') ;
133package ${module} ;
134
135EOM
136use Filter::Util::Call ;
137sub import { filter_add(bless []) }
138
139sub filter
140{
141 my ($self) = @_ ;
142 my ($status) ;
143
144 if (($status = filter_read()) > 0) {
145 s/ABC/DEF/g
146 }
147 $status ;
148}
149
150
1511 ;
152EOM
153
154writeFile($filename, <<EOM, <<'EOM') ;
155
156use $module ;
157EOM
158
159use Cwd ;
160$here = getcwd ;
161print "I am $here\n" ;
162print "some letters ABC\n" ;
163$y = "ABCDEF" ;
164print <<EOF ;
165Alphabetti Spagetti ($y)
166EOF
167
168EOM
169
03ec374d 170$a = `$Perl "-I." $Inc $filename 2>&1` ;
2c4bb738 171ok(7, ($? >>8) == 0) ;
172ok(8, $a eq <<EOM) ;
173I am $here
174some letters DEF
175Alphabetti Spagetti (DEFDEF)
176EOM
177
178
179# nested filters
180################
181
182
183writeFile("${module2}.pm", <<EOM, <<'EOM') ;
184package ${module2} ;
185use Filter::Util::Call ;
186
187EOM
188sub import { filter_add(bless []) }
189
190sub filter
191{
192 my ($self) = @_ ;
193 my ($status) ;
194
195 if (($status = filter_read()) > 0) {
196 s/XYZ/PQR/g
197 }
198 $status ;
199}
200
2011 ;
202EOM
203
204writeFile("${module3}.pm", <<EOM, <<'EOM') ;
205package ${module3} ;
206use Filter::Util::Call ;
207
208EOM
209sub import { filter_add(
210
211 sub
212 {
213 my ($status) ;
214
215 if (($status = filter_read()) > 0) {
216 s/Fred/Joe/g
217 }
218 $status ;
219 } ) ;
220}
221
2221 ;
223EOM
224
225writeFile("${module4}.pm", <<EOM) ;
226package ${module4} ;
227
228use $module5 ;
229
230print "I'm feeling used!\n" ;
231print "Fred Joe ABC DEF PQR XYZ\n" ;
232print "See you Today\n" ;
2331;
234EOM
235
236writeFile("${module5}.pm", <<EOM, <<'EOM') ;
237package ${module5} ;
238use Filter::Util::Call ;
239
240EOM
241sub import { filter_add(bless []) }
242
243sub filter
244{
245 my ($self) = @_ ;
246 my ($status) ;
247
248 if (($status = filter_read()) > 0) {
249 s/Today/Tomorrow/g
250 }
251 $status ;
252}
253
2541 ;
255EOM
256
257writeFile($filename, <<EOM, <<'EOM') ;
258
259# two filters for this file
260use $module ;
261use $module2 ;
262require "$nested" ;
263use $module4 ;
264EOM
265
266print "some letters ABCXYZ\n" ;
267$y = "ABCDEFXYZ" ;
268print <<EOF ;
269Fred likes Alphabetti Spagetti ($y)
270EOF
271
272EOM
273
274writeFile($nested, <<EOM, <<'EOM') ;
275use $module3 ;
276EOM
277
278print "This is another file XYZ\n" ;
279print <<EOF ;
280Where is Fred?
281EOF
282
283EOM
284
03ec374d 285$a = `$Perl "-I." $Inc $filename 2>&1` ;
2c4bb738 286ok(9, ($? >>8) == 0) ;
287ok(10, $a eq <<EOM) ;
288I'm feeling used!
289Fred Joe ABC DEF PQR XYZ
290See you Tomorrow
291This is another file XYZ
292Where is Joe?
293some letters DEFPQR
294Fred likes Alphabetti Spagetti (DEFDEFPQR)
295EOM
296
297# using the module context (with a closure)
298###########################################
299
300
301writeFile("${module2}.pm", <<EOM, <<'EOM') ;
302package ${module2} ;
303use Filter::Util::Call ;
304
305EOM
306sub import
307{
308 my ($type) = shift ;
309 my (@strings) = @_ ;
310
311
312 filter_add (
313
314 sub
315 {
316 my ($status) ;
317 my ($pattern) ;
318
319 if (($status = filter_read()) > 0) {
320 foreach $pattern (@strings)
321 { s/$pattern/PQR/g }
322 }
323
324 $status ;
325 }
326 )
327
328}
3291 ;
330EOM
331
332
333writeFile($filename, <<EOM, <<'EOM') ;
334
335use $module2 qw( XYZ KLM) ;
336use $module2 qw( ABC NMO) ;
337EOM
338
339print "some letters ABCXYZ KLM NMO\n" ;
340$y = "ABCDEFXYZKLMNMO" ;
341print <<EOF ;
342Alphabetti Spagetti ($y)
343EOF
344
345EOM
346
03ec374d 347$a = `$Perl "-I." $Inc $filename 2>&1` ;
2c4bb738 348ok(11, ($? >>8) == 0) ;
349ok(12, $a eq <<EOM) ;
350some letters PQRPQR PQR PQR
351Alphabetti Spagetti (PQRDEFPQRPQRPQR)
352EOM
353
354
355
356# using the module context (without a closure)
357##############################################
358
359
360writeFile("${module2}.pm", <<EOM, <<'EOM') ;
361package ${module2} ;
362use Filter::Util::Call ;
363
364EOM
365sub import
366{
367 my ($type) = shift ;
368 my (@strings) = @_ ;
369
370
371 filter_add (bless [@strings])
372}
373
374sub filter
375{
376 my ($self) = @_ ;
377 my ($status) ;
378 my ($pattern) ;
379
380 if (($status = filter_read()) > 0) {
381 foreach $pattern (@$self)
382 { s/$pattern/PQR/g }
383 }
384
385 $status ;
386}
387
3881 ;
389EOM
390
391
392writeFile($filename, <<EOM, <<'EOM') ;
393
394use $module2 qw( XYZ KLM) ;
395use $module2 qw( ABC NMO) ;
396EOM
397
398print "some letters ABCXYZ KLM NMO\n" ;
399$y = "ABCDEFXYZKLMNMO" ;
400print <<EOF ;
401Alphabetti Spagetti ($y)
402EOF
403
404EOM
405
03ec374d 406$a = `$Perl "-I." $Inc $filename 2>&1` ;
2c4bb738 407ok(13, ($? >>8) == 0) ;
408ok(14, $a eq <<EOM) ;
409some letters PQRPQR PQR PQR
410Alphabetti Spagetti (PQRDEFPQRPQRPQR)
411EOM
412
413# multi line test
414#################
415
416
417writeFile("${module2}.pm", <<EOM, <<'EOM') ;
418package ${module2} ;
419use Filter::Util::Call ;
420
421EOM
422sub import
423{
424 my ($type) = shift ;
425 my (@strings) = @_ ;
426
427
428 filter_add(bless [])
429}
430
431sub filter
432{
433 my ($self) = @_ ;
434 my ($status) ;
435
436 # read first line
437 if (($status = filter_read()) > 0) {
438 chop ;
439 s/\r$//;
440 # and now the second line (it will append)
441 $status = filter_read() ;
442 }
443
444 $status ;
445}
446
4471 ;
448EOM
449
450
451writeFile($filename, <<EOM, <<'EOM') ;
452
453use $module2 ;
454EOM
455print "don't cut me
456in half\n" ;
457print
458<<EOF ;
459appen
460ded
461EO
462F
463
464EOM
465
03ec374d 466$a = `$Perl "-I." $Inc $filename 2>&1` ;
2c4bb738 467ok(15, ($? >>8) == 0) ;
468ok(16, $a eq <<EOM) ;
469don't cut me in half
470appended
471EOM
472
473# Block test
474#############
475
476writeFile("${block}.pm", <<EOM, <<'EOM') ;
477package ${block} ;
478use Filter::Util::Call ;
479
480EOM
481sub import
482{
483 my ($type) = shift ;
484 my (@strings) = @_ ;
485
486
487 filter_add (bless [@strings] )
488}
489
490sub filter
491{
492 my ($self) = @_ ;
493 my ($status) ;
494 my ($pattern) ;
495
496 filter_read(20) ;
497}
498
4991 ;
500EOM
501
4176d4e4 502my $string = <<'EOM' ;
2c4bb738 503print "hello mum\n" ;
504$x = 'me ' x 3 ;
505print "Who wants it?\n$x\n" ;
506EOM
507
508
509writeFile($filename, <<EOM, $string ) ;
510use $block ;
511EOM
512
03ec374d 513$a = `$Perl "-I." $Inc $filename 2>&1` ;
2c4bb738 514ok(17, ($? >>8) == 0) ;
515ok(18, $a eq <<EOM) ;
516hello mum
517Who wants it?
518me me me
519EOM
520
521# use in the filter
522####################
523
524writeFile("${block}.pm", <<EOM, <<'EOM') ;
525package ${block} ;
526use Filter::Util::Call ;
527
528EOM
529use Cwd ;
530
531sub import
532{
533 my ($type) = shift ;
534 my (@strings) = @_ ;
535
536
537 filter_add(bless [@strings] )
538}
539
540sub filter
541{
542 my ($self) = @_ ;
543 my ($status) ;
1fcbfbda 544 my ($here) = quotemeta getcwd ;
2c4bb738 545
546 if (($status = filter_read()) > 0) {
547 s/DIR/$here/g
548 }
549 $status ;
550}
551
5521 ;
553EOM
554
555writeFile($filename, <<EOM, <<'EOM') ;
556use $block ;
557EOM
558print "We are in DIR\n" ;
559EOM
560
03ec374d 561$a = `$Perl "-I." $Inc $filename 2>&1` ;
2c4bb738 562ok(19, ($? >>8) == 0) ;
563ok(20, $a eq <<EOM) ;
564We are in $here
565EOM
566
567
568# filter_del
569#############
570
571writeFile("${block}.pm", <<EOM, <<'EOM') ;
572package ${block} ;
573use Filter::Util::Call ;
574
575EOM
576
577sub import
578{
579 my ($type) = shift ;
580 my ($count) = @_ ;
581
582
583 filter_add(bless \$count )
584}
585
586sub filter
587{
588 my ($self) = @_ ;
589 my ($status) ;
590
591 s/HERE/THERE/g
592 if ($status = filter_read()) > 0 ;
593
594 -- $$self ;
595 filter_del() if $$self <= 0 ;
596
597 $status ;
598}
599
6001 ;
601EOM
602
603writeFile($filename, <<EOM, <<'EOM') ;
604use $block (3) ;
605EOM
606print "
607HERE I am
608I am HERE
609HERE today gone tomorrow\n" ;
610EOM
611
03ec374d 612$a = `$Perl "-I." $Inc $filename 2>&1` ;
2c4bb738 613ok(21, ($? >>8) == 0) ;
614ok(22, $a eq <<EOM) ;
615
616THERE I am
617I am THERE
618HERE today gone tomorrow
619EOM
620
621
622# filter_read_exact
623####################
624
625writeFile("${block}.pm", <<EOM, <<'EOM') ;
626package ${block} ;
627use Filter::Util::Call ;
628
629EOM
630
631sub import
632{
633 my ($type) = shift ;
634
635 filter_add(bless [] )
636}
637
638sub filter
639{
640 my ($self) = @_ ;
641 my ($status) ;
642
643 if (($status = filter_read_exact(9)) > 0) {
644 s/HERE/THERE/g
645 }
646
647 $status ;
648}
649
6501 ;
651EOM
652
653writeFile($filenamebin, <<EOM, <<'EOM') ;
654use $block ;
655EOM
656print "
657HERE I am
658I'm HERE
659HERE today gone tomorrow\n" ;
660EOM
661
03ec374d 662$a = `$Perl "-I." $Inc $filenamebin 2>&1` ;
2c4bb738 663ok(23, ($? >>8) == 0) ;
664ok(24, $a eq <<EOM) ;
665
666HERE I am
667I'm THERE
668THERE today gone tomorrow
669EOM
670
671{
672
673# Check __DATA__
674####################
675
676writeFile("${block}.pm", <<EOM, <<'EOM') ;
677package ${block} ;
678use Filter::Util::Call ;
679
680EOM
681
682sub import
683{
684 my ($type) = shift ;
685
686 filter_add(bless [] )
687}
688
689sub filter
690{
691 my ($self) = @_ ;
692 my ($status) ;
693
694 if (($status = filter_read()) > 0) {
695 s/HERE/THERE/g
696 }
697
698 $status ;
699}
700
7011 ;
702EOM
703
704writeFile($filename, <<EOM, <<'EOM') ;
705use $block ;
706EOM
707print "HERE HERE\n";
708@a = <DATA>;
709print @a;
710__DATA__
711HERE I am
712I'm HERE
713HERE today gone tomorrow
714EOM
715
03ec374d 716$a = `$Perl "-I." $Inc $filename 2>&1` ;
2c4bb738 717ok(25, ($? >>8) == 0) ;
718ok(26, $a eq <<EOM) ;
719THERE THERE
720HERE I am
721I'm HERE
722HERE today gone tomorrow
723EOM
724
725}
726
727{
728
729# Check __END__
730####################
731
732writeFile("${block}.pm", <<EOM, <<'EOM') ;
733package ${block} ;
734use Filter::Util::Call ;
735
736EOM
737
738sub import
739{
740 my ($type) = shift ;
741
742 filter_add(bless [] )
743}
744
745sub filter
746{
747 my ($self) = @_ ;
748 my ($status) ;
749
750 if (($status = filter_read()) > 0) {
751 s/HERE/THERE/g
752 }
753
754 $status ;
755}
756
7571 ;
758EOM
759
760writeFile($filename, <<EOM, <<'EOM') ;
761use $block ;
762EOM
763print "HERE HERE\n";
764@a = <DATA>;
765print @a;
766__END__
767HERE I am
768I'm HERE
769HERE today gone tomorrow
770EOM
771
03ec374d 772$a = `$Perl "-I." $Inc $filename 2>&1` ;
2c4bb738 773ok(27, ($? >>8) == 0) ;
774ok(28, $a eq <<EOM) ;
775THERE THERE
776HERE I am
777I'm HERE
778HERE today gone tomorrow
779EOM
780
781}
782
783END {
95fa9585 784 1 while unlink $filename ;
785 1 while unlink $filenamebin ;
786 1 while unlink "${module}.pm" ;
787 1 while unlink "${module2}.pm" ;
788 1 while unlink "${module3}.pm" ;
789 1 while unlink "${module4}.pm" ;
790 1 while unlink "${module5}.pm" ;
791 1 while unlink $nested ;
792 1 while unlink "${block}.pm" ;
2c4bb738 793}
794
795