summaryrefslogtreecommitdiff
path: root/cpan/Test-Simple/t/Behavior/MonkeyPatching_done_testing.t
blob: 8c62100d071c1dfb3fb513772f09557e9d062d75 (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
use strict;
use warnings;
use B;

use Test::Stream;
use Test::MostlyLike;
use Test::More tests => 4;
use Test::Builder; # Not loaded by default in modern mode
my $orig = Test::Builder->can('done_testing');

use Test::Stream::Tester;

my $ran = 0;
no warnings 'redefine';
my $file = __FILE__;
my $line = __LINE__ + 1;
*Test::Builder::done_testing = sub { my $self = shift; $ran++; $self->$orig(@_) };
use warnings;

my @warnings;
$SIG{__WARN__} = sub { push @warnings => @_ };

events_are(
    intercept {
        ok(1, "pass");
        ok(0, "fail");

        done_testing;
    },
    check {
        event ok => { bool => 1 };
        event ok => { bool => 0 };
        event plan => { max => 2 };
        directive 'end';
    },
);

events_are(
    intercept {
        ok(1, "pass");
        ok(0, "fail");

        done_testing;
    },
    check {
        event ok => { bool => 1 };
        event ok => { bool => 0 };
        event plan => { max => 2 };
        directive 'end';
    },
);

is($ran, 2, "We ran our override both times");
mostly_like(
    \@warnings,
    [
        qr{The new sub is 'main::__ANON__' defined in \Q$file\E around line $line},
        undef,
    ],
    "Got the warning once"
);