12 open my $fh, $file or die "can't open '$file': $!";
18 #-- testing numeric fields in all variants (WL)
22 local $^A = ""; # don't litter, use a local bin
23 formline( $format, @_ );
28 # [ format, value1, expected1, value2, expected2, .... ]
29 [ '@###', 0, ' 0', 1, ' 1', 9999.6, '####',
30 9999.4999, '9999', -999.6, '####', 1e+100, '####' ],
32 [ '@0##', 0, '0000', 1, '0001', 9999.6, '####',
33 -999.4999, '-999', -999.6, '####', 1e+100, '####' ],
35 [ '^###', 0, ' 0', undef, ' ' ],
37 [ '^0##', 0, '0000', undef, ' ' ],
39 [ '@###.', 0, ' 0.', 1, ' 1.', 9999.6, '#####',
40 9999.4999, '9999.', -999.6, '#####' ],
42 [ '@##.##', 0, ' 0.00', 1, ' 1.00', 999.996, '######',
43 999.99499, '999.99', -100, '######' ],
45 [ '@0#.##', 0, '000.00', 1, '001.00', 10, '010.00',
46 -0.0001, qr/^[\-0]00\.00$/ ],
52 for my $tref ( @NumTests ){
53 $num_tests += (@$tref - 1)/2;
55 #---------------------------------------------------------
57 # number of tests in section 1
60 # number of tests in section 3
63 printf "1..%d\n", $bas_tests + $num_tests + $hmb_tests;
81 now @<<the@>>>> for all@|||||men to come @<<<<
83 'i' . 's', "time\n", $good, 'to'
87 open(OUT, '>Op_write.tmp') || die "Can't create Op_write.tmp";
88 END { 1 while unlink 'Op_write.tmp' }
92 $multiline = "forescore\nand\nseven years\n";
93 $foo = 'when in the course of human events it becomes necessary';
95 close OUT or die "Could not close: $!";
106 now is the time for all good men to come to\n";
108 if (cat('Op_write.tmp') eq $right)
109 { print "ok 1\n"; 1 while unlink 'Op_write.tmp'; }
111 { print "not ok 1\n"; }
113 $fox = 'wolfishness';
114 my $fox = 'foxiness'; # Test a lexical variable.
124 now @<<the@>>>> for all@|||||men to come @<<<<
125 'i' . 's', "time\n", $good, 'to'
128 open OUT2, '>Op_write.tmp' or die "Can't create Op_write.tmp";
131 $multiline = "forescore\nand\nseven years\n";
132 $foo = 'when in the course of human events it becomes necessary';
134 close OUT2 or die "Could not close: $!";
148 now is the time for all good men to come to\n";
150 if (cat('Op_write.tmp') eq $right)
151 { print "ok 2\n"; 1 while unlink 'Op_write.tmp'; }
153 { print "not ok 2\n"; }
165 now @<<the@>>>> for all@|||||men to come @<<<<
166 'i' . 's', "time\n", $good, 'to'
170 open(OUT2, '>Op_write.tmp') || die "Can't create Op_write.tmp";
174 $multiline = "forescore\nand\nseven years\n";
175 $foo = 'when in the course of human events it becomes necessary';
177 close OUT2 or die "Could not close: $!";
192 now is the time for all good men to come to\n";
194 if (cat('Op_write.tmp') eq $right)
195 { print "ok 3\n"; 1 while unlink 'Op_write.tmp'; }
197 { print "not ok 3\n"; }
219 my $format1 = '@' . '>' x $_;
220 formline $format1, 'abc';
221 $was1 .= "$format1 $^A\n";
224 local $format2 = '@' . '>' x $_;
225 formline $format2, 'abc';
226 $was2 .= "$format2 $^A\n";
228 print $was1 eq $mustbe ? "ok 4\n" : "not ok 4\n";
229 print $was2 eq $mustbe ? "ok 5\n" : "not ok 5\n";
240 open(OUT3, '>Op_write.tmp') || die "Can't create Op_write.tmp";
244 close OUT3 or die "Could not close: $!";
249 if (cat('Op_write.tmp') eq $right)
250 { print "ok 6\n"; 1 while unlink 'Op_write.tmp'; }
252 { print "not ok 6\n"; }
254 # test lexicals and globals
262 open(LEX, ">&STDOUT") or die;
266 close LEX or die "Could not close: $!";
268 # LEX_INTERPNORMAL test
274 open OUT4, ">Op_write.tmp" or die "Can't create Op_write.tmp";
276 close OUT4 or die "Could not close: $!";
277 if (cat('Op_write.tmp') eq "1\n") {
279 1 while unlink "Op_write.tmp";
292 open(OUT10, '>Op_write.tmp') || die "Can't create Op_write.tmp";
296 close OUT10 or die "Could not close: $!";
298 $right = " 12.95 00012.95\n";
299 if (cat('Op_write.tmp') eq $right)
300 { print "ok 10\n"; 1 while unlink 'Op_write.tmp'; }
302 { print "not ok 10\n"; }
315 open(OUT11, '>Op_write.tmp') || die "Can't create Op_write.tmp";
319 close OUT11 or die "Could not close: $!";
325 if (cat('Op_write.tmp') eq $right)
326 { print "ok 11\n"; 1 while unlink 'Op_write.tmp'; }
328 { print "not ok 11\n"; }
333 ok ^<<<<<<<<<<<<<<~~ # sv_chop() naze
336 my %hash = (12 => 3);
337 open(OUT12, '>Op_write.tmp') || die "Can't create Op_write.tmp";
339 for $el (keys %hash) {
342 close OUT12 or die "Could not close: $!";
343 print cat('Op_write.tmp');
348 # Bug report and testcase by Alexey Tourbin
351 tie $v, 'Tie::StdScalar';
357 open(OUT13, '>Op_write.tmp') || die "Can't create Op_write.tmp";
359 close OUT13 or die "Could not close: $!";
360 print cat('Op_write.tmp');
364 # Bug #24774 format without trailing \n failed assertion, but this
365 # must fail since we have a trailing ; in the eval'ed string (WL)
367 eval "format OUT14 = \n@\n\@v";
368 print $@ ? "ok 14\n" : "not ok 14\n";
373 # text lost in ^<<< field with \r in value (WL)
374 my $txt = "line 1\rline 2";
381 open(OUT15, '>Op_write.tmp') || die "Can't create Op_write.tmp";
383 close OUT15 or die "Could not close: $!";
384 my $res = cat('Op_write.tmp');
385 print $res eq "line 1\nline 2\n" ? "ok 15\n" : "not ok 15\n";
388 { # test 16: multiple use of a variable in same line with ^<
389 my $txt = "this_is_block_1 this_is_block_2 this_is_block_3 this_is_block_4";
391 ^<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<
393 ^<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<
396 open(OUT16, '>Op_write.tmp') || die "Can't create Op_write.tmp";
398 close OUT16 or die "Could not close: $!";
399 my $res = cat('Op_write.tmp');
400 print $res eq <<EOD ? "ok 16\n" : "not ok 16\n";
401 this_is_block_1 this_is_block_2
402 this_is_block_3 this_is_block_4
406 { # test 17: @* "should be on a line of its own", but it should work
407 # cleanly with literals before and after. (WL)
409 my $txt = "This is line 1.\nThis is the second line.\nThird and last.\n";
411 Here we go: @* That's all, folks!
414 open(OUT17, '>Op_write.tmp') || die "Can't create Op_write.tmp";
416 close OUT17 or die "Could not close: $!";
417 my $res = cat('Op_write.tmp');
420 Here we go: $txt That's all, folks!
422 print $res eq $exp ? "ok 17\n" : "not ok 17\n";
425 { # test 18: @# and ~~ would cause runaway format, but we now
426 # catch this while compiling (WL)
432 open(OUT18, '>Op_write.tmp') || die "Can't create Op_write.tmp";
433 eval { write(OUT18); };
434 print $@ ? "ok 18\n" : "not ok 18\n";
435 close OUT18 or die "Could not close: $!";
438 { # test 19: \0 in an evel'ed format, doesn't cause empty lines (WL)
440 eval "format OUT19 = \n" .
445 open(OUT19, '>Op_write.tmp') || die "Can't create Op_write.tmp";
447 close OUT19 or die "Could not close: $!";
448 my $res = cat('Op_write.tmp');
449 print $res eq <<EOD ? "ok 19\n" : "not ok 19\n";
455 { # test 20: hash accesses; single '}' must not terminate format '}' (WL)
456 my %h = ( xkey => 'xval', ykey => 'yval' );
468 while( my( $k, $v ) = each( %h ) ){
469 $exp .= sprintf( "%5s %s\n", $k, $v );
471 $exp .= sprintf( "%5s %s\n", $h{xkey}, $h{ykey} );
472 $exp .= sprintf( "%5s %s\n", $h{xkey}, $h{ykey} );
474 open(OUT20, '>Op_write.tmp') || die "Can't create Op_write.tmp";
476 close OUT20 or die "Could not close: $!";
477 my $res = cat('Op_write.tmp');
478 print $res eq $exp ? "ok 20\n" : "not ok 20 res=[$res]exp=[$exp]\n";
482 #####################
484 ## numeric formatting
485 #####################
488 for my $tref ( @NumTests ){
489 my $writefmt = shift( @$tref );
491 my $val = shift @$tref;
492 my $expected = shift @$tref;
493 my $writeres = swrite( $writefmt, $val );
495 my $ok = ref($expected)
496 ? $writeres =~ $expected
497 : $writeres eq $expected;
501 : "not ok $nt\n# f=[$writefmt] exp=[$expected] got=[$writeres]\n";
506 #####################################
508 ## Easiest to add new tests above here
509 #######################################
511 # scary format testing from H.Merijn Brand
513 my $test = $bas_tests + $num_tests + 1;
514 my $tests = $bas_tests + $num_tests + $hmb_tests;
516 if ($^O eq 'VMS' || $^O eq 'MSWin32' || $^O eq 'dos' || $^O eq 'MacOS' ||
517 ($^O eq 'os2' and not eval '$OS2::can_fork')) {
518 foreach ($test..$tests) {
519 print "ok $_ # skipped: '|-' and '-|' not supported\n";
525 use strict; # Amazed that this hackery can be made strict ...
527 # Just a complete test for format, including top-, left- and bottom marging
528 # and format detection through glob entries
539 # [ID 20020227.005] format bug with undefined _TOP
541 open STDOUT_DUP, ">&STDOUT";
542 my $oldfh = select STDOUT_DUP;
544 { local $~ = "Comment";
548 ? "ok $test # TODO\n" : "not ok $test # TODO \$- = $- instead of 9\n";
550 print $^ eq "STDOUT_DUP_TOP"
551 ? "ok $test\n" : "not ok $test\n# \$^ = $^ instead of 'STDOUT_DUP_TOP'\n";
557 $= = 7; # Page length
559 my $ps = $^L; $^L = ""; # Catch the page separator
560 my $tm = 1; # Top margin (empty lines before first output)
561 my $bm = 2; # Bottom marging (empty lines between last text and footer)
562 my $lm = 4; # Left margin (indent in spaces)
564 select ((select (STDOUT), $| = 1)[0]);
565 if ($lm > 0 and !open STDOUT, "|-") { # Left margin (in this test ALWAYS set)
566 select ((select (STDOUT), $| = 1)[0]);
570 print + ($_ eq <DATA> ? "" : "not "), "ok ", $test++, "\n";
573 print + (<DATA>?"not ":""), "ok ", $test++, "\n";
578 $= -= $bm + 1; # count one for the trailing "----"
591 $% == 1 and return "";
593 $lastmin < $= and print "\n" x $lastmin;
594 print "\n" x $bm, "----\n", $ps;
599 # Yes, this is sick ;-)
609 @{(shift @E)||["",""]}
619 exists $::{$fmt} or return 0;
620 $^O eq "MSWin32" or return defined *{$::{$fmt}}{FORMAT};
621 open my $null, "> /dev/null" or die;
622 my $fh = select $null;
629 $^ = has_format ("TOP") ? "TOP" : "EMPTY";
630 has_format ("ENTRY") or die "No format defined for ENTRY";
631 foreach my $e ( [ map { [ $_, "Test$_" ] } 1 .. 7 ],
632 [ map { [ $_, "${_}tseT" ] } 1 .. 5 ]) {
636 has_format ("EOR") or next;
640 if (has_format ("EOF")) {