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