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 # number of tests in section 4
67 my $tests = $bas_tests + $num_tests + $bug_tests + $hmb_tests;
87 now @<<the@>>>> for all@|||||men to come @<<<<
89 'i' . 's', "time\n", $good, 'to'
93 open(OUT, '>Op_write.tmp') || die "Can't create Op_write.tmp";
94 END { 1 while unlink 'Op_write.tmp' }
98 $multiline = "forescore\nand\nseven years\n";
99 $foo = 'when in the course of human events it becomes necessary';
101 close OUT or die "Could not close: $!";
112 now is the time for all good men to come to\n";
114 if (cat('Op_write.tmp') eq $right)
115 { print "ok 1\n"; 1 while unlink 'Op_write.tmp'; }
117 { print "not ok 1\n"; }
119 $fox = 'wolfishness';
120 my $fox = 'foxiness'; # Test a lexical variable.
130 now @<<the@>>>> for all@|||||men to come @<<<<
131 'i' . 's', "time\n", $good, 'to'
134 open OUT2, '>Op_write.tmp' or die "Can't create Op_write.tmp";
137 $multiline = "forescore\nand\nseven years\n";
138 $foo = 'when in the course of human events it becomes necessary';
140 close OUT2 or die "Could not close: $!";
154 now is the time for all good men to come to\n";
156 if (cat('Op_write.tmp') eq $right)
157 { print "ok 2\n"; 1 while unlink 'Op_write.tmp'; }
159 { print "not ok 2\n"; }
171 now @<<the@>>>> for all@|||||men to come @<<<<
172 'i' . 's', "time\n", $good, 'to'
176 open(OUT2, '>Op_write.tmp') || die "Can't create Op_write.tmp";
180 $multiline = "forescore\nand\nseven years\n";
181 $foo = 'when in the course of human events it becomes necessary';
183 close OUT2 or die "Could not close: $!";
198 now is the time for all good men to come to\n";
200 if (cat('Op_write.tmp') eq $right)
201 { print "ok 3\n"; 1 while unlink 'Op_write.tmp'; }
203 { print "not ok 3\n"; }
225 my $format1 = '@' . '>' x $_;
226 formline $format1, 'abc';
227 $was1 .= "$format1 $^A\n";
230 local $format2 = '@' . '>' x $_;
231 formline $format2, 'abc';
232 $was2 .= "$format2 $^A\n";
234 print $was1 eq $mustbe ? "ok 4\n" : "not ok 4\n";
235 print $was2 eq $mustbe ? "ok 5\n" : "not ok 5\n";
246 open(OUT3, '>Op_write.tmp') || die "Can't create Op_write.tmp";
250 close OUT3 or die "Could not close: $!";
255 if (cat('Op_write.tmp') eq $right)
256 { print "ok 6\n"; 1 while unlink 'Op_write.tmp'; }
258 { print "not ok 6\n"; }
260 # test lexicals and globals
268 open(LEX, ">&STDOUT") or die;
272 close LEX or die "Could not close: $!";
274 # LEX_INTERPNORMAL test
280 open OUT4, ">Op_write.tmp" or die "Can't create Op_write.tmp";
282 close OUT4 or die "Could not close: $!";
283 if (cat('Op_write.tmp') eq "1\n") {
285 1 while unlink "Op_write.tmp";
298 open(OUT10, '>Op_write.tmp') || die "Can't create Op_write.tmp";
302 close OUT10 or die "Could not close: $!";
304 $right = " 12.95 00012.95\n";
305 if (cat('Op_write.tmp') eq $right)
306 { print "ok 10\n"; 1 while unlink 'Op_write.tmp'; }
308 { print "not ok 10\n"; }
321 open(OUT11, '>Op_write.tmp') || die "Can't create Op_write.tmp";
325 close OUT11 or die "Could not close: $!";
331 if (cat('Op_write.tmp') eq $right)
332 { print "ok 11\n"; 1 while unlink 'Op_write.tmp'; }
334 { print "not ok 11\n"; }
339 ok ^<<<<<<<<<<<<<<~~ # sv_chop() naze
342 my %hash = (12 => 3);
343 open(OUT12, '>Op_write.tmp') || die "Can't create Op_write.tmp";
345 for $el (keys %hash) {
348 close OUT12 or die "Could not close: $!";
349 print cat('Op_write.tmp');
354 # Bug report and testcase by Alexey Tourbin
357 tie $v, 'Tie::StdScalar';
363 open(OUT13, '>Op_write.tmp') || die "Can't create Op_write.tmp";
365 close OUT13 or die "Could not close: $!";
366 print cat('Op_write.tmp');
370 # Bug #24774 format without trailing \n failed assertion, but this
371 # must fail since we have a trailing ; in the eval'ed string (WL)
373 eval "format OUT14 = \n@\n\@v";
374 print +($@ && $@ =~ /Format not terminated/)
375 ? "ok 14\n" : "not ok 14 $@\n";
380 # text lost in ^<<< field with \r in value (WL)
381 my $txt = "line 1\rline 2";
388 open(OUT15, '>Op_write.tmp') || die "Can't create Op_write.tmp";
390 close OUT15 or die "Could not close: $!";
391 my $res = cat('Op_write.tmp');
392 print $res eq "line 1\nline 2\n" ? "ok 15\n" : "not ok 15\n";
395 { # test 16: multiple use of a variable in same line with ^<
396 my $txt = "this_is_block_1 this_is_block_2 this_is_block_3 this_is_block_4";
398 ^<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<
400 ^<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<
403 open(OUT16, '>Op_write.tmp') || die "Can't create Op_write.tmp";
405 close OUT16 or die "Could not close: $!";
406 my $res = cat('Op_write.tmp');
407 print $res eq <<EOD ? "ok 16\n" : "not ok 16\n";
408 this_is_block_1 this_is_block_2
409 this_is_block_3 this_is_block_4
413 { # test 17: @* "should be on a line of its own", but it should work
414 # cleanly with literals before and after. (WL)
416 my $txt = "This is line 1.\nThis is the second line.\nThird and last.\n";
418 Here we go: @* That's all, folks!
421 open(OUT17, '>Op_write.tmp') || die "Can't create Op_write.tmp";
423 close OUT17 or die "Could not close: $!";
424 my $res = cat('Op_write.tmp');
427 Here we go: $txt That's all, folks!
429 print $res eq $exp ? "ok 17\n" : "not ok 17\n";
432 { # test 18: @# and ~~ would cause runaway format, but we now
433 # catch this while compiling (WL)
439 open(OUT18, '>Op_write.tmp') || die "Can't create Op_write.tmp";
440 eval { write(OUT18); };
441 print +($@ && $@ =~ /Repeated format line will never terminate/)
442 ? "ok 18\n" : "not ok 18: $@\n";
443 close OUT18 or die "Could not close: $!";
446 { # test 19: \0 in an evel'ed format, doesn't cause empty lines (WL)
448 eval "format OUT19 = \n" .
453 open(OUT19, '>Op_write.tmp') || die "Can't create Op_write.tmp";
455 close OUT19 or die "Could not close: $!";
456 my $res = cat('Op_write.tmp');
457 print $res eq <<EOD ? "ok 19\n" : "not ok 19\n";
463 { # test 20: hash accesses; single '}' must not terminate format '}' (WL)
464 my %h = ( xkey => 'xval', ykey => 'yval' );
476 while( my( $k, $v ) = each( %h ) ){
477 $exp .= sprintf( "%5s %s\n", $k, $v );
479 $exp .= sprintf( "%5s %s\n", $h{xkey}, $h{ykey} );
480 $exp .= sprintf( "%5s %s\n", $h{xkey}, $h{ykey} );
482 open(OUT20, '>Op_write.tmp') || die "Can't create Op_write.tmp";
484 close OUT20 or die "Could not close: $!";
485 my $res = cat('Op_write.tmp');
486 print $res eq $exp ? "ok 20\n" : "not ok 20 res=[$res]exp=[$exp]\n";
490 #####################
492 ## numeric formatting
493 #####################
496 for my $tref ( @NumTests ){
497 my $writefmt = shift( @$tref );
499 my $val = shift @$tref;
500 my $expected = shift @$tref;
501 my $writeres = swrite( $writefmt, $val );
503 my $ok = ref($expected)
504 ? $writeres =~ $expected
505 : $writeres eq $expected;
508 ? "ok $nt - $writefmt\n"
509 : "not ok $nt\n# f=[$writefmt] exp=[$expected] got=[$writeres]\n";
514 #####################################
516 ## Easiest to add new tests just here
517 #######################################
519 curr_test($bas_tests + $num_tests + 1);
521 use strict; # Amazed that this hackery can be made strict ...
523 # DAPM. Exercise a couple of error codepaths
528 like $@, qr/Not a format reference/, 'format reference';
532 like $@, qr/Undefined format/, 'no such format';
539 my $test = curr_test();
547 # [ID 20020227.005] format bug with undefined _TOP
549 open STDOUT_DUP, ">&STDOUT";
550 my $oldfh = select STDOUT_DUP;
553 local $~ = "Comment";
555 curr_test($test + 1);
557 local $::TODO = '[ID 20020227.005] format bug with undefined _TOP';
560 is $^, "STDOUT_DUP_TOP";
565 # scary format testing from H.Merijn Brand
567 # Just a complete test for format, including top-, left- and bottom marging
568 # and format detection through glob entries
570 if (1 || $^O eq 'VMS' || $^O eq 'MSWin32' || $^O eq 'dos' || $^O eq 'MacOS' ||
571 ($^O eq 'os2' and not eval '$OS2::can_fork')) {
574 skip "'|-' and '-|' not supported", $tests - $test + 1;
581 $= = 7; # Page length
583 my $ps = $^L; $^L = ""; # Catch the page separator
584 my $tm = 1; # Top margin (empty lines before first output)
585 my $bm = 2; # Bottom marging (empty lines between last text and footer)
586 my $lm = 4; # Left margin (indent in spaces)
588 # -----------------------------------------------------------------------
590 # execute the rest of the script in a child process. The parent reads the
591 # output from the child and compares it with <DATA>.
595 select ((select (STDOUT), $| = 1)[0]); # flush STDOUT
597 my $opened = open FROM_CHILD, "-|";
598 unless (defined $opened) {
608 while (<FROM_CHILD>) {
610 fail 'too much output';
614 my $exp = shift @data;
618 is "@data", "", "correct length of output";
625 select ((select (STDOUT), $| = 1)[0]);
627 $= -= $bm + 1; # count one for the trailing "----"
640 $% == 1 and return "";
642 $lastmin < $= and print "\n" x $lastmin;
643 print "\n" x $bm, "----\n", $ps;
648 # Yes, this is sick ;-)
658 @{(shift @E)||["",""]}
668 exists $::{$fmt} or return 0;
669 $^O eq "MSWin32" or return defined *{$::{$fmt}}{FORMAT};
670 open my $null, "> /dev/null" or die;
671 my $fh = select $null;
678 $^ = has_format ("TOP") ? "TOP" : "EMPTY";
679 has_format ("ENTRY") or die "No format defined for ENTRY";
680 foreach my $e ( [ map { [ $_, "Test$_" ] } 1 .. 7 ],
681 [ map { [ $_, "${_}tseT" ] } 1 .. 5 ]) {
685 has_format ("EOR") or next;
689 if (has_format ("EOF")) {