summaryrefslogtreecommitdiff
path: root/cpan/Test-Simple/t/Test-Stream-API.t
blob: 318af7e06ba7de84e171ce9b47c71370594acf30 (plain)
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
use strict;
use warnings;

use Test::Stream;
use Test::More;
use Test::Stream::Tester qw/events_are event directive check/;
use Test::MostlyLike;

require Test::Builder;
require Test::CanFork;

use Test::Stream::API qw{
    listen munge follow_up
    enable_forking cull
    peek_todo push_todo pop_todo set_todo inspect_todo
    is_tester init_tester
    is_modern set_modern
    context peek_context clear_context set_context
    intercept
    state_count state_failed state_plan state_ended is_passing
    current_stream

    disable_tap enable_tap subtest_tap_instant subtest_tap_delayed tap_encoding
    enable_numbers disable_numbers set_tap_outputs get_tap_outputs
};

can_ok(__PACKAGE__, qw{
    listen munge follow_up
    enable_forking cull
    peek_todo push_todo pop_todo set_todo inspect_todo
    is_tester init_tester
    is_modern set_modern
    context peek_context clear_context set_context
    intercept
    state_count state_failed state_plan state_ended is_passing
    current_stream

    disable_tap enable_tap subtest_tap_instant subtest_tap_delayed tap_encoding
    enable_numbers disable_numbers set_tap_outputs get_tap_outputs
});

ok(!is_tester('My::Tester'), "Not a tester");
isa_ok(init_tester('My::Tester'), 'Test::Stream::Meta');
isa_ok(is_tester('My::Tester'), 'Test::Stream::Meta');

ok(!is_modern('My::Tester'), "Not a modern tester");
set_modern('My::Tester', 1);
ok(is_modern('My::Tester'), "a modern tester");
set_modern('My::Tester', 0);
ok(!is_modern('My::Tester'), "Not a modern tester");

ok(my $ctx = context(), "Got context");
isa_ok($ctx, 'Test::Stream::Context');
is(context(), $ctx, "Got the same instance again");
is(peek_context(), $ctx, "peek");
my $ref = "$ctx";

clear_context();
my $ne = context() . "" ne $ref;
ok($ne, "cleared");

set_context($ctx);
is(context(), $ctx, "Got the same instance again");

$ctx = undef;
$ne = context() . "" ne $ref;
ok($ne, "New instance");

isa_ok(current_stream(), 'Test::Stream');

my @munge;
my @listen;
my @follow;
intercept {
    munge  { push @munge  => $_[1] };
    listen { push @listen => $_[1] };

    follow_up { push @follow => $_[0]->snapshot };

    ok(1, "pass");
    diag "foo";

    done_testing;
};

is(@listen, 3, "listen got 3 events");
is(@munge,  3, "munge got 3 events");
is(@follow, 1, "Follow was triggered");

my $want = check {
    event ok => { bool => 1 };
    event diag => { message => 'foo' };
    event plan => { max => 1 };
    directive 'end';
};
events_are( \@listen, $want, "Listen events" );
events_are( \@munge, $want, "Munge events" );
isa_ok($follow[0], 'Test::Stream::Context');

my $events = intercept {
    Test::CanFork->import;

    enable_forking;

    my $pid = fork();
    if ($pid) { # Parent
        waitpid($pid, 0);
        cull;
        ok(1, "From Parent");
    }
    else { # child
        ok(1, "From Child");
        exit 0;
    }
};

if (@$events == 1) {
    events_are (
        $events,
        check {
            event plan => {};
        },
        "Not testing forking"
    );
}
else {
    events_are (
        $events,
        check {
            event ok => { name => 'From Child' };
            event ok => { name => 'From Parent' };
        },
        "Got forked events"
    );
}

events_are(
    intercept {
        ok(0, "fail");
        push_todo('foo');
        ok(0, "fail");
        push_todo('bar');
        ok(0, "fail");
        is(peek_todo(), 'bar', "peek works");
        pop_todo();
        ok(0, "fail");
        pop_todo();
        ok(0, "fail");
    },
    check {
        event ok => {todo => '',    in_todo   => 0};
        event ok => {todo => 'foo', in_todo   => 1};
        event ok => {todo => 'bar', in_todo   => 1};
        event ok => {bool => 1,     real_bool => 1}; # Verify peek
        event ok => {todo => 'foo', in_todo   => 1};
        event ok => {todo => '',    in_todo   => 0};
    },
    "Verified TODO stack"
);

my $meta = init_tester('My::Tester');
ok(!$meta->todo, "Package is not in todo");
set_todo('My::Tester', 'foo');
is($meta->todo, 'foo', "Package is in todo");

my @todos = (
    inspect_todo,
    inspect_todo('My::Tester'),
);
push_todo('foo');
push_todo('bar');
Test::Builder->new->todo_start('tb todo');
$My::Tester::TODO = 'pkg todo';
push @todos => inspect_todo, inspect_todo('My::Tester');
$My::Tester::TODO = undef;
Test::Builder->new->todo_end();
pop_todo;
pop_todo;
set_todo('My::Tester', undef);
push @todos => inspect_todo, inspect_todo('My::Tester');

is_deeply(
    \@todos,
    [
        {
            TB   => undef,
            TODO => [],
        },
        {
            META => 'foo',
            PKG  => undef,
            TB   => undef,
            TODO => [],
        },
        {
            TB   => 'tb todo',
            TODO => [qw/foo bar/],
        },
        {
            META => 'foo',
            PKG  => 'pkg todo',
            TB   => 'tb todo',
            TODO => [qw/foo bar/],
        },
        {
            TB   => undef,
            TODO => [],
        },
        {
            META => undef,
            PKG  => undef,
            TB   => undef,
            TODO => [],
        }
    ],
    "Todo state from inspect todo"
);

my @state;
intercept {
    plan tests => 3;
    ok(1, "pass");
    ok(2, "pass");

    push @state => {
        count   => state_count()  || 0,
        failed  => state_failed() || 0,
        plan    => state_plan()   || undef,
        ended   => state_ended()  || undef,
        passing => is_passing(),
    };

    ok(0, "fail");
    done_testing;

    push @state => {
        count   => state_count()  || 0,
        failed  => state_failed() || 0,
        plan    => state_plan()   || undef,
        ended   => state_ended()  || undef,
        passing => is_passing(),
    };
};

mostly_like(
    \@state,
    [
        { count => 2, failed => 0, passing => 1, ended => undef },
        { count => 3, failed => 1, passing => 0 },
    ],
    "Verified Test state"
);

events_are(
    [ $state[0]->{plan}, $state[1]->{plan} ],
    check {
        event plan => { max => 3 };
        event plan => { max => 3 };
    },
    "Parts of state that are events check out."
);

isa_ok( $state[1]->{ended}, 'Test::Stream::Context' );

my $got;
my $results = "";
my $utf8 = "";
open( my $fh, ">>", \$results ) || die "Could not open handle to scalar!";
open( my $fh_utf8, ">>", \$utf8 ) || die "Could not open handle to scalar!";

intercept {
    enable_tap(); # Disabled by default in intercept()
    set_tap_outputs( std => $fh, err => $fh, todo => $fh );
    $got = get_tap_outputs();

    ok(1, "pass");

    disable_tap();
    ok(0, "fail");

    enable_tap();
    tap_encoding('utf8');
    set_tap_outputs( encoding => 'utf8', std => $fh_utf8, err => $fh_utf8, todo => $fh_utf8 );
    ok(1, "pass");
    tap_encoding('legacy');

    disable_numbers();
    ok(1, "pass");
    enable_numbers();
    ok(1, "pass");

    subtest_tap_instant();
    subtest foo => sub { ok(1, 'pass') };

    subtest_tap_delayed();
    subtest foo => sub { ok(1, 'pass') };
};

is_deeply(
    $got,
    { encoding => 'legacy', std => $fh, err => $fh, todo => $fh },
    "Got outputs"
);

is( $results, <<EOT, "got TAP output");
ok 1 - pass
ok - pass
ok 5 - pass
# Subtest: foo
    ok 1 - pass
    1..1
ok 6 - foo
ok 7 - foo {
    ok 1 - pass
    1..1
}
EOT

is( $utf8, <<EOT, "got utf8 TAP output");
ok 3 - pass
EOT

done_testing;