5 s2p.t - test suite for s2p/psed
11 (a) run psed with a sed script and input data to obtain some output
12 (b) run s2p with a sed script creating a Perl program and then run the
13 Perl program with the input data, again producing output
15 Both final outputs should be identical to the expected output.
17 A $testcase{<name>} contains entries (after the comment ### <name> ###):
19 - script: the sed script
20 - input: the key of the input data, stored in $input{<input>}
21 - expect: the expected output
22 - datfil: an additional file [ <path>, <data> ] (if required)
24 Temporary files are created in the working directory (embedding $$
25 in the name), and removed after the test.
27 Except for bin2dec (which indeed converts binary to decimal) none of the
28 sed scripts is doing something useful.
30 Author: Wolfgang Laun.
45 $ENV{PSEDEXTBRE} = '<>wW';
68 # This script should be run on itself
74 /^#\(..\)\(..\)\2\1*$/p
75 /^#[abc]\{1,\}[def]\{1,\}$/p
94 script => <<'[TheEnd]',
96 s/^[ ]*\([01]\{1,\}\)[ ]*/\1/
99 is not a binary number
102 # expand binary to Xs
107 s/^\(X\{1,\}\)0/\1\1/
108 s/^\(X\{1,\}\)1/\1\1X/
111 # count Xs in decimal
128 expect => <<'[TheEnd]',
139 script => <<'[TheEnd]',
144 expect => <<'[TheEnd]',
160 script => <<'[TheEnd]',
174 expect => <<'[TheEnd]',
200 script => <<'[TheEnd]',
208 expect => <<'[TheEnd]',
224 script => <<'[TheEnd]',
236 expect => <<'[TheEnd]',
250 script => <<'[TheEnd]',
259 expect => <<'[TheEnd]',
267 script => <<'[TheEnd]',
279 expect => <<'[TheEnd]',
297 script => <<'[TheEnd]',
305 expect => <<'[TheEnd]',
317 script => "#no autoprint\n1,3{\n=\np\n}",
319 expect => <<'[TheEnd]',
331 script => <<'[TheEnd]',
348 expect => <<'[TheEnd]',
362 script => <<'[TheEnd]',
376 expect => <<'[TheEnd]',
387 script => <<'[TheEnd]',
389 replace all except line 3
393 expect => <<'[TheEnd]',
394 replace all except line 3
395 replace all except line 3
397 replace all except line 3
398 replace all except line 3
399 replace all except line 3
400 replace all except line 3
401 replace all except line 3
407 script => <<'[TheEnd]',
409 replace all except 1-4
412 replace all except 5-8
415 expect => <<'[TheEnd]',
416 replace all except 5-8
417 replace all except 5-8
418 replace all except 5-8
419 replace all except 5-8
420 replace all except 1-4
421 replace all except 1-4
422 replace all except 1-4
423 replace all except 1-4
429 script => <<'[TheEnd]',
430 # d delete pattern space, start next cycle
435 expect => <<'[TheEnd]',
445 script => <<'[TheEnd]',
453 expect => <<'[TheEnd]',
464 script => <<'[TheEnd]',
475 expect => <<'[TheEnd]',
493 script => <<'[TheEnd]',
505 expect => <<'[TheEnd]',
521 script => <<'[TheEnd]',
527 expect => <<'[TheEnd]',
541 script => <<'[TheEnd]',
545 append to line 3 - should not appear in output
549 expect => <<'[TheEnd]',
559 datfil => [ 'r.txt', "r.txt line 1\nr.txt line 2\nr.txt line 3\n" ],
560 script => <<'[TheEnd]',
565 expect => <<'[TheEnd]',
585 script => <<'[TheEnd]',
586 # enclose any `(a)'.. `(c)' in `-'
592 expect => <<'[TheEnd]',
606 script => <<'[TheEnd]',
612 # this is literal { }
619 expect => <<'[TheEnd]',
633 script => join( "\n",
634 '#no autoprint', 's/./X/p', 's/foo/bar/p', 't bye', '=', 'p', ':bye' ),
636 expect => <<'[TheEnd]',
650 datfil => [ 'w.txt', '' ],
651 script => <<'[TheEnd]',
655 expect => <<'[TheEnd]',
669 script => <<'[TheEnd]',
676 expect => <<'[TheEnd]',
696 script => <<'[TheEnd]',
697 y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/
702 expect => <<'[TheEnd]',
716 script => <<'[TheEnd]',
719 # delete line, append NL to hold space
724 # last line only: get hold
744 expect => <<'[TheEnd]',
751 script => <<'[TheEnd]',
753 # This script should be run on itself
759 /^#\(..\)\(..\)\2\1*$/p
760 /^#[abc]\{1,\}[def]\{1,\}$/p
773 expect => <<'[TheEnd]',
781 my $ntc = 2 * keys %testcase;
784 # temporary file names
785 my $script = "s2pt$$.sed";
786 my $stdin = "s2pt$$.in";
787 my $plsed = "s2pt$$.pl";
789 # various command lines for
790 my $s2p = File::Spec->catfile( File::Spec->updir(), 'x2p', 's2p' );
791 my $psed = File::Spec->catfile( File::Spec->curdir(), 'psed' );
793 # default in the .com extenson if it's not already there
794 $s2p = VMS::Filespec::rmsexpand($s2p, '.com');
795 $psed = VMS::Filespec::rmsexpand($psed, '.com');
797 my $sedcmd = [ $psed, '-f', $script, $stdin ];
798 my $s2pcmd = [ $s2p, '-f', $script ];
799 my $plcmd = [ $plsed, $stdin ];
802 $switches = ['-x'] if $^O eq 'MacOS';
804 # psed: we create a local copy as linking may not work on some systems.
808 # process all testcases
811 for my $tc ( sort keys %testcase ){
812 my( $psedres, $s2pres );
816 open( SED, ">$script" ) || goto FAIL_BOTH;
817 my $script = $testcase{$tc}{script};
819 # additional files for r, w: patch script, inserting temporary names
820 if( exists( $testcase{$tc}{datfil} ) ){
821 my( $datnam, $datdat ) = @{$testcase{$tc}{datfil}};
822 my $datfil = "s2pt$$" . $datnam;
823 push( @aux, $datfil );
824 open( DAT, ">$datfil" ) || goto FAIL_BOTH;
827 $script =~ s/\%$datnam\%/$datfil/eg;
830 close( SED ) || goto FAIL_BOTH;
834 if( $indat ne $testcase{$tc}{input} ){
835 $indat = $testcase{$tc}{input};
836 open( IN, ">$stdin" ) || goto FAIL_BOTH;
837 print IN $input{$indat};
838 close( IN ) || goto FAIL_BOTH;
841 # on VMS, runperl eats blank lines to work around
842 # spurious newlines in pipes
843 $testcase{$tc}{expect} =~ s/\n\n/\n/ if $^O eq 'VMS';
847 $psedres = runperl( args => $sedcmd, switches => $switches );
848 is( $psedres, $testcase{$tc}{expect}, "psed $tc" );
851 # translate the sed script to a Perl program
853 my $perlprog = runperl( args => $s2pcmd, switches => $switches );
854 open( PP, ">$plsed" ) || goto FAIL_S2P;
856 close( PP ) || goto FAIL_S2P;
858 # execute generated Perl program, compare
859 $s2pres = runperl( args => $plcmd, switches => $switches );
860 is( $s2pres, $testcase{$tc}{expect}, "s2p $tc" );
870 for my $f ( $script, $stdin, $plsed, @aux ){
871 1 while unlink( $f ); # hats off to VMS...