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