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 is cat('Op_write.tmp'), $right and do { 1 while unlink 'Op_write.tmp'; };
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 is cat('Op_write.tmp'), $right and do { 1 while unlink 'Op_write.tmp'; };
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 is cat('Op_write.tmp'), $right and do { 1 while unlink 'Op_write.tmp' };
216 my $format1 = '@' . '>' x $_;
217 formline $format1, 'abc';
218 $was1 .= "$format1 $^A\n";
221 local $format2 = '@' . '>' x $_;
222 formline $format2, 'abc';
223 $was2 .= "$format2 $^A\n";
237 open(OUT3, '>Op_write.tmp') || die "Can't create Op_write.tmp";
241 close OUT3 or die "Could not close: $!";
246 is cat('Op_write.tmp'), $right and do { 1 while unlink 'Op_write.tmp' };
249 # test lexicals and globals
251 my $test = curr_test();
258 open(LEX, ">&STDOUT") or die;
262 close LEX or die "Could not close: $!";
263 curr_test($test + 1);
265 # LEX_INTERPNORMAL test
271 open OUT4, ">Op_write.tmp" or die "Can't create Op_write.tmp";
273 close OUT4 or die "Could not close: $!";
274 is cat('Op_write.tmp'), "1\n" and do { 1 while unlink "Op_write.tmp" };
283 open(OUT10, '>Op_write.tmp') || die "Can't create Op_write.tmp";
287 close OUT10 or die "Could not close: $!";
289 $right = " 12.95 00012.95\n";
290 is cat('Op_write.tmp'), $right and do { 1 while unlink 'Op_write.tmp' };
303 open(OUT11, '>Op_write.tmp') || die "Can't create Op_write.tmp";
307 close OUT11 or die "Could not close: $!";
313 is cat('Op_write.tmp'), $right and do { 1 while unlink 'Op_write.tmp' };
316 my $test = curr_test();
319 ok ^<<<<<<<<<<<<<<~~ # sv_chop() naze
322 my %hash = ($test => 3);
323 open(OUT12, '>Op_write.tmp') || die "Can't create Op_write.tmp";
325 for $el (keys %hash) {
328 close OUT12 or die "Could not close: $!";
329 print cat('Op_write.tmp');
330 curr_test($test + 1);
334 my $test = curr_test();
335 # Bug report and testcase by Alexey Tourbin
338 tie $v, 'Tie::StdScalar';
344 open(OUT13, '>Op_write.tmp') || die "Can't create Op_write.tmp";
346 close OUT13 or die "Could not close: $!";
347 print cat('Op_write.tmp');
348 curr_test($test + 1);
352 # Bug #24774 format without trailing \n failed assertion, but this
353 # must fail since we have a trailing ; in the eval'ed string (WL)
355 eval "format OUT14 = \n@\n\@v";
356 like $@, qr/Format not terminated/;
360 # text lost in ^<<< field with \r in value (WL)
361 my $txt = "line 1\rline 2";
368 open(OUT15, '>Op_write.tmp') || die "Can't create Op_write.tmp";
370 close OUT15 or die "Could not close: $!";
371 my $res = cat('Op_write.tmp');
372 is $res, "line 1\nline 2\n";
375 { # test 16: multiple use of a variable in same line with ^<
376 my $txt = "this_is_block_1 this_is_block_2 this_is_block_3 this_is_block_4";
378 ^<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<
380 ^<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<
383 open(OUT16, '>Op_write.tmp') || die "Can't create Op_write.tmp";
385 close OUT16 or die "Could not close: $!";
386 my $res = cat('Op_write.tmp');
388 this_is_block_1 this_is_block_2
389 this_is_block_3 this_is_block_4
393 { # test 17: @* "should be on a line of its own", but it should work
394 # cleanly with literals before and after. (WL)
396 my $txt = "This is line 1.\nThis is the second line.\nThird and last.\n";
398 Here we go: @* That's all, folks!
401 open(OUT17, '>Op_write.tmp') || die "Can't create Op_write.tmp";
403 close OUT17 or die "Could not close: $!";
404 my $res = cat('Op_write.tmp');
407 Here we go: $txt That's all, folks!
412 { # test 18: @# and ~~ would cause runaway format, but we now
413 # catch this while compiling (WL)
419 open(OUT18, '>Op_write.tmp') || die "Can't create Op_write.tmp";
420 eval { write(OUT18); };
421 like $@, qr/Repeated format line will never terminate/;
422 close OUT18 or die "Could not close: $!";
425 { # test 19: \0 in an evel'ed format, doesn't cause empty lines (WL)
427 eval "format OUT19 = \n" .
432 open(OUT19, '>Op_write.tmp') || die "Can't create Op_write.tmp";
434 close OUT19 or die "Could not close: $!";
435 my $res = cat('Op_write.tmp');
442 { # test 20: hash accesses; single '}' must not terminate format '}' (WL)
443 my %h = ( xkey => 'xval', ykey => 'yval' );
455 while( my( $k, $v ) = each( %h ) ){
456 $exp .= sprintf( "%5s %s\n", $k, $v );
458 $exp .= sprintf( "%5s %s\n", $h{xkey}, $h{ykey} );
459 $exp .= sprintf( "%5s %s\n", $h{xkey}, $h{ykey} );
461 open(OUT20, '>Op_write.tmp') || die "Can't create Op_write.tmp";
463 close OUT20 or die "Could not close: $!";
464 my $res = cat('Op_write.tmp');
469 #####################
471 ## numeric formatting
472 #####################
474 curr_test($bas_tests + 1);
476 for my $tref ( @NumTests ){
477 my $writefmt = shift( @$tref );
479 my $val = shift @$tref;
480 my $expected = shift @$tref;
481 my $writeres = swrite( $writefmt, $val );
483 like $writeres, $expected, $writefmt;
485 is $writeres, $expected, $writefmt;
491 #####################################
493 ## Easiest to add new tests just here
494 #####################################
496 use strict; # Amazed that this hackery can be made strict ...
498 # DAPM. Exercise a couple of error codepaths
503 like $@, qr/Not a format reference/, 'format reference';
507 like $@, qr/Undefined format/, 'no such format';
514 my $test = curr_test();
522 # [ID 20020227.005] format bug with undefined _TOP
524 open STDOUT_DUP, ">&STDOUT";
525 my $oldfh = select STDOUT_DUP;
528 local $~ = "Comment";
530 curr_test($test + 1);
532 local $::TODO = '[ID 20020227.005] format bug with undefined _TOP';
535 is $^, "STDOUT_DUP_TOP";
540 #############################
542 ## Add new tests *above* here
543 #############################
545 # scary format testing from H.Merijn Brand
547 # Just a complete test for format, including top-, left- and bottom marging
548 # and format detection through glob entries
550 if ($^O eq 'VMS' || $^O eq 'MSWin32' || $^O eq 'dos' || $^O eq 'MacOS' ||
551 ($^O eq 'os2' and not eval '$OS2::can_fork')) {
554 skip "'|-' and '-|' not supported", $tests - $test + 1;
561 $= = 7; # Page length
563 my $ps = $^L; $^L = ""; # Catch the page separator
564 my $tm = 1; # Top margin (empty lines before first output)
565 my $bm = 2; # Bottom marging (empty lines between last text and footer)
566 my $lm = 4; # Left margin (indent in spaces)
568 # -----------------------------------------------------------------------
570 # execute the rest of the script in a child process. The parent reads the
571 # output from the child and compares it with <DATA>.
575 select ((select (STDOUT), $| = 1)[0]); # flush STDOUT
577 my $opened = open FROM_CHILD, "-|";
578 unless (defined $opened) {
588 while (<FROM_CHILD>) {
590 fail 'too much output';
594 my $exp = shift @data;
598 is "@data", "", "correct length of output";
605 select ((select (STDOUT), $| = 1)[0]);
607 $= -= $bm + 1; # count one for the trailing "----"
620 $% == 1 and return "";
622 $lastmin < $= and print "\n" x $lastmin;
623 print "\n" x $bm, "----\n", $ps;
628 # Yes, this is sick ;-)
638 @{(shift @E)||["",""]}
648 exists $::{$fmt} or return 0;
649 $^O eq "MSWin32" or return defined *{$::{$fmt}}{FORMAT};
650 open my $null, "> /dev/null" or die;
651 my $fh = select $null;
658 $^ = has_format ("TOP") ? "TOP" : "EMPTY";
659 has_format ("ENTRY") or die "No format defined for ENTRY";
660 foreach my $e ( [ map { [ $_, "Test$_" ] } 1 .. 7 ],
661 [ map { [ $_, "${_}tseT" ] } 1 .. 5 ]) {
665 has_format ("EOR") or next;
669 if (has_format ("EOF")) {