13 open my $fh, $file or die "can't open '$file': $!";
19 #-- testing numeric fields in all variants (WL)
23 local $^A = ""; # don't litter, use a local bin
24 formline( $format, @_ );
29 # [ format, value1, expected1, value2, expected2, .... ]
30 [ '@###', 0, ' 0', 1, ' 1', 9999.6, '####',
31 9999.4999, '9999', -999.6, '####', 1e+100, '####' ],
33 [ '@0##', 0, '0000', 1, '0001', 9999.6, '####',
34 -999.4999, '-999', -999.6, '####', 1e+100, '####' ],
36 [ '^###', 0, ' 0', undef, ' ' ],
38 [ '^0##', 0, '0000', undef, ' ' ],
40 [ '@###.', 0, ' 0.', 1, ' 1.', 9999.6, '#####',
41 9999.4999, '9999.', -999.6, '#####' ],
43 [ '@##.##', 0, ' 0.00', 1, ' 1.00', 999.996, '######',
44 999.99499, '999.99', -100, '######' ],
46 [ '@0#.##', 0, '000.00', 1, '001.00', 10, '010.00',
47 -0.0001, qr/^[\-0]00\.00$/ ],
53 for my $tref ( @NumTests ){
54 $num_tests += (@$tref - 1)/2;
56 #---------------------------------------------------------
58 # number of tests in section 1
61 # number of tests in section 3
64 my $tests = $bas_tests + $num_tests + $hmb_tests;
84 now @<<the@>>>> for all@|||||men to come @<<<<
86 'i' . 's', "time\n", $good, 'to'
90 open(OUT, '>Op_write.tmp') || die "Can't create Op_write.tmp";
91 END { 1 while unlink 'Op_write.tmp' }
95 $multiline = "forescore\nand\nseven years\n";
96 $foo = 'when in the course of human events it becomes necessary';
98 close OUT or die "Could not close: $!";
109 now is the time for all good men to come to\n";
111 if (cat('Op_write.tmp') eq $right)
112 { print "ok 1\n"; 1 while unlink 'Op_write.tmp'; }
114 { print "not ok 1\n"; }
116 $fox = 'wolfishness';
117 my $fox = 'foxiness'; # Test a lexical variable.
127 now @<<the@>>>> for all@|||||men to come @<<<<
128 'i' . 's', "time\n", $good, 'to'
131 open OUT2, '>Op_write.tmp' or die "Can't create Op_write.tmp";
134 $multiline = "forescore\nand\nseven years\n";
135 $foo = 'when in the course of human events it becomes necessary';
137 close OUT2 or die "Could not close: $!";
151 now is the time for all good men to come to\n";
153 if (cat('Op_write.tmp') eq $right)
154 { print "ok 2\n"; 1 while unlink 'Op_write.tmp'; }
156 { print "not ok 2\n"; }
168 now @<<the@>>>> for all@|||||men to come @<<<<
169 'i' . 's', "time\n", $good, 'to'
173 open(OUT2, '>Op_write.tmp') || die "Can't create Op_write.tmp";
177 $multiline = "forescore\nand\nseven years\n";
178 $foo = 'when in the course of human events it becomes necessary';
180 close OUT2 or die "Could not close: $!";
195 now is the time for all good men to come to\n";
197 if (cat('Op_write.tmp') eq $right)
198 { print "ok 3\n"; 1 while unlink 'Op_write.tmp'; }
200 { print "not ok 3\n"; }
222 my $format1 = '@' . '>' x $_;
223 formline $format1, 'abc';
224 $was1 .= "$format1 $^A\n";
227 local $format2 = '@' . '>' x $_;
228 formline $format2, 'abc';
229 $was2 .= "$format2 $^A\n";
231 print $was1 eq $mustbe ? "ok 4\n" : "not ok 4\n";
232 print $was2 eq $mustbe ? "ok 5\n" : "not ok 5\n";
243 open(OUT3, '>Op_write.tmp') || die "Can't create Op_write.tmp";
247 close OUT3 or die "Could not close: $!";
252 if (cat('Op_write.tmp') eq $right)
253 { print "ok 6\n"; 1 while unlink 'Op_write.tmp'; }
255 { print "not ok 6\n"; }
257 # test lexicals and globals
265 open(LEX, ">&STDOUT") or die;
269 close LEX or die "Could not close: $!";
271 # LEX_INTERPNORMAL test
277 open OUT4, ">Op_write.tmp" or die "Can't create Op_write.tmp";
279 close OUT4 or die "Could not close: $!";
280 if (cat('Op_write.tmp') eq "1\n") {
282 1 while unlink "Op_write.tmp";
295 open(OUT10, '>Op_write.tmp') || die "Can't create Op_write.tmp";
299 close OUT10 or die "Could not close: $!";
301 $right = " 12.95 00012.95\n";
302 if (cat('Op_write.tmp') eq $right)
303 { print "ok 10\n"; 1 while unlink 'Op_write.tmp'; }
305 { print "not ok 10\n"; }
318 open(OUT11, '>Op_write.tmp') || die "Can't create Op_write.tmp";
322 close OUT11 or die "Could not close: $!";
328 if (cat('Op_write.tmp') eq $right)
329 { print "ok 11\n"; 1 while unlink 'Op_write.tmp'; }
331 { print "not ok 11\n"; }
336 ok ^<<<<<<<<<<<<<<~~ # sv_chop() naze
339 my %hash = (12 => 3);
340 open(OUT12, '>Op_write.tmp') || die "Can't create Op_write.tmp";
342 for $el (keys %hash) {
345 close OUT12 or die "Could not close: $!";
346 print cat('Op_write.tmp');
351 # Bug report and testcase by Alexey Tourbin
354 tie $v, 'Tie::StdScalar';
360 open(OUT13, '>Op_write.tmp') || die "Can't create Op_write.tmp";
362 close OUT13 or die "Could not close: $!";
363 print cat('Op_write.tmp');
367 # Bug #24774 format without trailing \n failed assertion, but this
368 # must fail since we have a trailing ; in the eval'ed string (WL)
370 eval "format OUT14 = \n@\n\@v";
371 print +($@ && $@ =~ /Format not terminated/)
372 ? "ok 14\n" : "not ok 14 $@\n";
377 # text lost in ^<<< field with \r in value (WL)
378 my $txt = "line 1\rline 2";
385 open(OUT15, '>Op_write.tmp') || die "Can't create Op_write.tmp";
387 close OUT15 or die "Could not close: $!";
388 my $res = cat('Op_write.tmp');
389 print $res eq "line 1\nline 2\n" ? "ok 15\n" : "not ok 15\n";
392 { # test 16: multiple use of a variable in same line with ^<
393 my $txt = "this_is_block_1 this_is_block_2 this_is_block_3 this_is_block_4";
395 ^<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<
397 ^<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<
400 open(OUT16, '>Op_write.tmp') || die "Can't create Op_write.tmp";
402 close OUT16 or die "Could not close: $!";
403 my $res = cat('Op_write.tmp');
404 print $res eq <<EOD ? "ok 16\n" : "not ok 16\n";
405 this_is_block_1 this_is_block_2
406 this_is_block_3 this_is_block_4
410 { # test 17: @* "should be on a line of its own", but it should work
411 # cleanly with literals before and after. (WL)
413 my $txt = "This is line 1.\nThis is the second line.\nThird and last.\n";
415 Here we go: @* That's all, folks!
418 open(OUT17, '>Op_write.tmp') || die "Can't create Op_write.tmp";
420 close OUT17 or die "Could not close: $!";
421 my $res = cat('Op_write.tmp');
424 Here we go: $txt That's all, folks!
426 print $res eq $exp ? "ok 17\n" : "not ok 17\n";
429 { # test 18: @# and ~~ would cause runaway format, but we now
430 # catch this while compiling (WL)
436 open(OUT18, '>Op_write.tmp') || die "Can't create Op_write.tmp";
437 eval { write(OUT18); };
438 print +($@ && $@ =~ /Repeated format line will never terminate/)
439 ? "ok 18\n" : "not ok 18: $@\n";
440 close OUT18 or die "Could not close: $!";
443 { # test 19: \0 in an evel'ed format, doesn't cause empty lines (WL)
445 eval "format OUT19 = \n" .
450 open(OUT19, '>Op_write.tmp') || die "Can't create Op_write.tmp";
452 close OUT19 or die "Could not close: $!";
453 my $res = cat('Op_write.tmp');
454 print $res eq <<EOD ? "ok 19\n" : "not ok 19\n";
460 { # test 20: hash accesses; single '}' must not terminate format '}' (WL)
461 my %h = ( xkey => 'xval', ykey => 'yval' );
473 while( my( $k, $v ) = each( %h ) ){
474 $exp .= sprintf( "%5s %s\n", $k, $v );
476 $exp .= sprintf( "%5s %s\n", $h{xkey}, $h{ykey} );
477 $exp .= sprintf( "%5s %s\n", $h{xkey}, $h{ykey} );
479 open(OUT20, '>Op_write.tmp') || die "Can't create Op_write.tmp";
481 close OUT20 or die "Could not close: $!";
482 my $res = cat('Op_write.tmp');
483 print $res eq $exp ? "ok 20\n" : "not ok 20 res=[$res]exp=[$exp]\n";
487 #####################
489 ## numeric formatting
490 #####################
493 for my $tref ( @NumTests ){
494 my $writefmt = shift( @$tref );
496 my $val = shift @$tref;
497 my $expected = shift @$tref;
498 my $writeres = swrite( $writefmt, $val );
500 my $ok = ref($expected)
501 ? $writeres =~ $expected
502 : $writeres eq $expected;
505 ? "ok $nt - $writefmt\n"
506 : "not ok $nt\n# f=[$writefmt] exp=[$expected] got=[$writeres]\n";
511 #####################################
513 ## Easiest to add new tests above here
514 #######################################
516 # scary format testing from H.Merijn Brand
518 my $test = $bas_tests + $num_tests + 1;
521 if ($^O eq 'VMS' || $^O eq 'MSWin32' || $^O eq 'dos' || $^O eq 'MacOS' ||
522 ($^O eq 'os2' and not eval '$OS2::can_fork')) {
524 skip "'|-' and '-|' not supported", $tests - $test + 1;
530 use strict; # Amazed that this hackery can be made strict ...
532 # DAPM. Exercise a couple of error codepaths
537 like $@, qr/Not a format reference/, 'format reference';
541 like $@, qr/Undefined format/, 'no such format';
544 # Just a complete test for format, including top-, left- and bottom marging
545 # and format detection through glob entries
558 # [ID 20020227.005] format bug with undefined _TOP
560 open STDOUT_DUP, ">&STDOUT";
561 my $oldfh = select STDOUT_DUP;
564 local $~ = "Comment";
566 curr_test($test + 1);
568 local $::TODO = '[ID 20020227.005] format bug with undefined _TOP';
571 is $^, "STDOUT_DUP_TOP";
577 $= = 7; # Page length
579 my $ps = $^L; $^L = ""; # Catch the page separator
580 my $tm = 1; # Top margin (empty lines before first output)
581 my $bm = 2; # Bottom marging (empty lines between last text and footer)
582 my $lm = 4; # Left margin (indent in spaces)
584 # -----------------------------------------------------------------------
586 # execute the rest of the script in a child process. The parent reads the
587 # output from the child and compares it with <DATA>.
591 select ((select (STDOUT), $| = 1)[0]); # flush STDOUT
593 my $opened = open FROM_CHILD, "-|";
594 unless (defined $opened) {
604 while (<FROM_CHILD>) {
606 fail 'too much output';
610 my $exp = shift @data;
614 is "@data", "", "correct length of output";
621 select ((select (STDOUT), $| = 1)[0]);
623 $= -= $bm + 1; # count one for the trailing "----"
636 $% == 1 and return "";
638 $lastmin < $= and print "\n" x $lastmin;
639 print "\n" x $bm, "----\n", $ps;
644 # Yes, this is sick ;-)
654 @{(shift @E)||["",""]}
664 exists $::{$fmt} or return 0;
665 $^O eq "MSWin32" or return defined *{$::{$fmt}}{FORMAT};
666 open my $null, "> /dev/null" or die;
667 my $fh = select $null;
674 $^ = has_format ("TOP") ? "TOP" : "EMPTY";
675 has_format ("ENTRY") or die "No format defined for ENTRY";
676 foreach my $e ( [ map { [ $_, "Test$_" ] } 1 .. 7 ],
677 [ map { [ $_, "${_}tseT" ] } 1 .. 5 ]) {
681 has_format ("EOR") or next;
685 if (has_format ("EOF")) {