1#!perl -w
2
3use strict;
4
5BEGIN {
6    if( $ENV{PERL_CORE} ) {
7        chdir 't';
8        @INC = ('../lib', 'lib');
9    }
10    else {
11        unshift @INC, 't/lib';
12    }
13}
14chdir 't';
15
16use Test::Builder;
17
18# The real Test::Builder
19my $Test = Test::Builder->new;
20$Test->plan( tests => 6 );
21
22
23# The one we're going to test.
24my $tb = Test::Builder->create();
25
26my $tmpfile = 'foo.tmp';
27END { 1 while unlink($tmpfile) }
28
29# Test output to a file
30{
31    my $out = $tb->output($tmpfile);
32    $Test->ok( defined $out );
33
34    print $out "hi!\n";
35    close *$out;
36
37    undef $out;
38    open(IN, $tmpfile) or die $!;
39    chomp(my $line = <IN>);
40    close IN;
41
42    $Test->is_eq($line, 'hi!');
43}
44
45
46# Test output to a filehandle
47{
48    open(FOO, ">>$tmpfile") or die $!;
49    my $out = $tb->output(\*FOO);
50    my $old = select *$out;
51    print "Hello!\n";
52    close *$out;
53    undef $out;
54    select $old;
55    open(IN, $tmpfile) or die $!;
56    my @lines = <IN>;
57    close IN;
58
59    $Test->like($lines[1], qr/Hello!/);
60}
61
62
63# Test output to a scalar ref
64{
65    my $scalar = '';
66    my $out = $tb->output(\$scalar);
67
68    print $out "Hey hey hey!\n";
69    $Test->is_eq($scalar, "Hey hey hey!\n");
70}
71
72
73# Test we can output to the same scalar ref
74{
75    my $scalar = '';
76    my $out = $tb->output(\$scalar);
77    my $err = $tb->failure_output(\$scalar);
78
79    print $out "To output ";
80    print $err "and beyond!";
81
82    $Test->is_eq($scalar, "To output and beyond!", "One scalar, two filehandles");
83}
84
85
86# Ensure stray newline in name escaping works.
87{
88    my $fakeout = '';
89    my $out = $tb->output(\$fakeout);
90    $tb->exported_to(__PACKAGE__);
91    $tb->no_ending(1);
92    $tb->plan(tests => 5);
93
94    $tb->ok(1, "ok");
95    $tb->ok(1, "ok\n");
96    $tb->ok(1, "ok, like\nok");
97    $tb->skip("wibble\nmoof");
98    $tb->todo_skip("todo\nskip\n");
99
100    $Test->is_eq( $fakeout, <<OUTPUT ) || print STDERR $fakeout;
1011..5
102ok 1 - ok
103ok 2 - ok
104#
105ok 3 - ok, like
106# ok
107ok 4 # skip wibble
108# moof
109not ok 5 # TODO & SKIP todo
110# skip
111#
112OUTPUT
113}
114