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

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

{
    package MyModernTester;
    use Test::More;
    use Test::Stream;
    use Test::MostlyLike;

    no warnings 'redefine';
    local *Test::Builder::note = sub {
        my $self = shift;
        return $self->$orig(__PACKAGE__ . ": ", @_);
    };
    use warnings;

    my $file = __FILE__;
    # Line number is tricky, just use what B says The sub may not actually think it
    # is on the line it is may be off by 1.
    my $line = B::svref_2object(\&Test::Builder::note)->START->line;

    my @warnings;
    {
        local $SIG{__WARN__} = sub { push @warnings => @_ };
        note('first');
        note('seconds');
    }
    mostly_like(
        \@warnings,
        [
            qr{The new sub is 'MyModernTester::__ANON__' defined in \Q$file\E around line $line},
            undef, #Only 1 warning
        ],
        "Found expected warning, just the one"
    );
}

{
    package MyModernTester2;
    use Test::More;
    use Test::Stream;
    use Test::MostlyLike;

    no warnings 'redefine';
    local *Test::Builder::note = sub {
        my $self = shift;
        return $self->$orig(__PACKAGE__ . ": ", @_);
    };
    use warnings;

    my $file = __FILE__;
    # Line number is tricky, just use what B says The sub may not actually think it
    # is on the line it is may be off by 1.
    my $line = B::svref_2object(\&Test::Builder::note)->START->line;

    my @warnings;
    {
        local $SIG{__WARN__} = sub { push @warnings => @_ };
        note('first');
        note('seconds');
    }
    mostly_like(
        \@warnings,
        [
            qr{The new sub is 'MyModernTester2::__ANON__' defined in \Q$file\E around line $line},
            undef, #Only 1 warning
        ],
        "new override, new warning"
    );
}

{
    package MyLegacyTester;
    use Test::More;

    no warnings 'redefine';
    local *Test::Builder::note = sub {
        my $self = shift;
        return $self->$orig(__PACKAGE__ . ": ", @_);
    };
    use warnings;

    my @warnings;
    {
        local $SIG{__WARN__} = sub { push @warnings => @_ };
        note('first');
        note('seconds');
    }
    is(@warnings, 0, "no warnings for a legacy tester");
}