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