1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
use strict;
use warnings;
# HARNESS-NO-FORMATTER
BEGIN { require "t/tools.pl" };
#########################
#
# This test us here to insure that Ok, Diag, and Note events render the way
# Test::More renders them, trailing whitespace and all.
#
#########################
use Test2::API qw/test2_stack/;
use Test::Builder::Formatter;
sub capture(&) {
my $code = shift;
my ($err, $out) = ("", "");
my $handles = test2_stack->top->format->handles;
my ($ok, $e);
{
my ($out_fh, $err_fh);
($ok, $e) = try {
open($out_fh, '>', \$out) or die "Failed to open a temporary STDOUT: $!";
open($err_fh, '>', \$err) or die "Failed to open a temporary STDERR: $!";
test2_stack->top->format->set_handles([$out_fh, $err_fh, $out_fh]);
$code->();
};
}
test2_stack->top->format->set_handles($handles);
die $e unless $ok;
$err =~ s/ $/_/mg;
$out =~ s/ $/_/mg;
return {
STDOUT => $out,
STDERR => $err,
};
}
# The tools in tools.pl have some intentional differences from the Test::More
# versions, these behave more like Test::More which is important for
# back-compat.
sub tm_ok($;$) {
my ($bool, $name) = @_;
my $ctx = context;
my $ok = bless {
pass => $bool,
name => $name,
effective_pass => 1,
trace => $ctx->trace->snapshot,
}, 'Test2::Event::Ok';
# Do not call init
$ctx->hub->send($ok);
$ctx->release;
return $bool;
}
# Test::More actually does a bit more, but for this test we just want to see
# what happens when message is a specific string, or undef.
sub tm_diag {
my $ctx = context();
$ctx->diag(@_);
$ctx->release;
}
sub tm_note {
my $ctx = context();
$ctx->note(@_);
$ctx->release;
}
# Ensure the top hub is generated
test2_stack->top;
my $temp_hub = test2_stack->new_hub();
my $diag = capture {
tm_diag(undef);
tm_diag("");
tm_diag(" ");
tm_diag("A");
tm_diag("\n");
tm_diag("\nB");
tm_diag("C\n");
tm_diag("\nD\n");
tm_diag("E\n\n");
};
my $note = capture {
tm_note(undef);
tm_note("");
tm_note(" ");
tm_note("A");
tm_note("\n");
tm_note("\nB");
tm_note("C\n");
tm_note("\nD\n");
tm_note("E\n\n");
};
my $ok = capture {
tm_ok(1);
tm_ok(1, "");
tm_ok(1, " ");
tm_ok(1, "A");
tm_ok(1, "\n");
tm_ok(1, "\nB");
tm_ok(1, "C\n");
tm_ok(1, "\nD\n");
tm_ok(1, "E\n\n");
};
test2_stack->pop($temp_hub);
is($diag->{STDOUT}, "", "STDOUT is empty for diag");
is($diag->{STDERR}, <<EOT, "STDERR for diag looks right");
# undef
#_
# _
# A
#_
#_
# B
# C
#_
# D
# E
#_
EOT
is($note->{STDERR}, "", "STDERR for note is empty");
is($note->{STDOUT}, <<EOT, "STDOUT looks right for note");
# undef
#_
# _
# A
#_
#_
# B
# C
#_
# D
# E
#_
EOT
is($ok->{STDERR}, "", "STDERR for ok is empty");
is($ok->{STDOUT}, <<EOT, "STDOUT looks right for ok");
ok 1
ok 2 -_
ok 3 - _
ok 4 - A
ok 5 -_
#_
ok 6 -_
# B
ok 7 - C
#_
ok 8 -_
# D
#_
ok 9 - E
#_
#_
EOT
done_testing;
|