Test::Simple/More/Builder/Tutorial 0.41
[p5sagit/p5-mst-13.2.git] / lib / Test / Simple / t / diag.t
1 #!perl -w
2
3 BEGIN {
4     if( $ENV{PERL_CORE} ) {
5         chdir 't';
6         @INC = '../lib';
7     }
8 }
9
10 use strict;
11
12 use Test::More tests => 5;
13
14 my $Test = Test::More->builder;
15
16 # now make a filehandle where we can send data
17 my $output;
18 tie *FAKEOUT, 'FakeOut', \$output;
19
20 # force diagnostic output to a filehandle, glad I added this to Test::Builder :)
21 my @lines;
22 {
23     local $TODO = 1;
24     $Test->todo_output(\*FAKEOUT);
25
26     diag("a single line");
27
28     push @lines, $output;
29     $output = '';
30
31     diag("multiple\n", "lines");
32     push @lines, split(/\n/, $output);
33 }
34
35 is( @lines, 3,              'diag() should send messages to its filehandle' );
36 like( $lines[0], '/^#\s+/', '    should add comment mark to all lines' );
37 is( $lines[0], "# a single line\n",   '    should send exact message' );
38 is( $output, "# multiple\n# lines\n", '    should append multi messages');
39
40 {
41     local $TODO = 1;
42     $output = '';
43     diag("# foo");
44 }
45 is( $output, "# # foo\n",   "diag() adds a # even if there's one already" );
46
47
48 package FakeOut;
49
50 sub TIEHANDLE {
51         bless( $_[1], $_[0] );
52 }
53
54 sub PRINT {
55         my $self = shift;
56         $$self .= join('', @_);
57 }
58 #!perl -w
59
60 BEGIN {
61     if( $ENV{PERL_CORE} ) {
62         chdir 't';
63         @INC = '../lib';
64     }
65 }
66
67 use strict;
68
69 use Test::More tests => 5;
70
71 my $Test = Test::More->builder;
72
73 # now make a filehandle where we can send data
74 my $output;
75 tie *FAKEOUT, 'FakeOut', \$output;
76
77 # force diagnostic output to a filehandle, glad I added this to Test::Builder :)
78 my @lines;
79 {
80     local $TODO = 1;
81     $Test->todo_output(\*FAKEOUT);
82
83     diag("a single line");
84
85     push @lines, $output;
86     $output = '';
87
88     diag("multiple\n", "lines");
89     push @lines, split(/\n/, $output);
90 }
91
92 is( @lines, 3,              'diag() should send messages to its filehandle' );
93 like( $lines[0], '/^#\s+/', '    should add comment mark to all lines' );
94 is( $lines[0], "# a single line\n",   '    should send exact message' );
95 is( $output, "# multiple\n# lines\n", '    should append multi messages');
96
97 {
98     local $TODO = 1;
99     $output = '';
100     diag("# foo");
101 }
102 is( $output, "# # foo\n",   "diag() adds a # even if there's one already" );
103
104
105 package FakeOut;
106
107 sub TIEHANDLE {
108         bless( $_[1], $_[0] );
109 }
110
111 sub PRINT {
112         my $self = shift;
113         $$self .= join('', @_);
114 }