summaryrefslogtreecommitdiff
path: root/cpan/Test-Simple/t/subtest/line_numbers.t
blob: c1736d07d3cb10f6f9c76ecc573cc7fa449e354c (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
#!/usr/bin/perl -w

# Test Test::More::subtest(), focusing on correct line numbers in
# failed test diagnostics.

BEGIN {
    if( $ENV{PERL_CORE} ) {
        chdir 't';
        @INC = ( '../lib', 'lib' );
    }
    else {
        unshift @INC, 't/lib';
    }
}

use strict;
use warnings;

use Test::More tests => 5;
use Test::Builder;
use Test::Builder::Tester;

# Formatting may change if we're running under Test::Harness.
$ENV{HARNESS_ACTIVE} = 0;

our %line;

{
    test_out("# Subtest: namehere");
    test_out("    1..3");
    test_out("    ok 1");
    test_out("    not ok 2");
    test_err("    #   Failed test at $0 line $line{innerfail1}.");
    test_out("    ok 3");
    test_err("    # Looks like you failed 1 test of 3.");
    test_out("not ok 1 - namehere");
    test_err("#   Failed test 'namehere'");
    test_err("#   at $0 line $line{outerfail1}.");

    subtest namehere => sub {
        plan tests => 3;
        ok 1;
        ok 0; BEGIN{ $line{innerfail1} = __LINE__ }
        ok 1;
    }; BEGIN{ $line{outerfail1} = __LINE__ }

    test_test("un-named inner tests");
}
{
    test_out("# Subtest: namehere");
    test_out("    1..3");
    test_out("    ok 1 - first is good");
    test_out("    not ok 2 - second is bad");
    test_err("    #   Failed test 'second is bad'");
    test_err("    #   at $0 line $line{innerfail2}.");
    test_out("    ok 3 - third is good");
    test_err("    # Looks like you failed 1 test of 3.");
    test_out("not ok 1 - namehere");
    test_err("#   Failed test 'namehere'");
    test_err("#   at $0 line $line{outerfail2}.");

    subtest namehere => sub {
        plan tests => 3;
        ok 1, "first is good";
        ok 0, "second is bad"; BEGIN{ $line{innerfail2} = __LINE__ }
        ok 1, "third is good";
    }; BEGIN{ $line{outerfail2} = __LINE__ }

    test_test("named inner tests");
}

sub run_the_subtest {
    subtest namehere => sub {
        plan tests => 3;
        ok 1, "first is good";
        ok 0, "second is bad"; BEGIN{ $line{innerfail3} = __LINE__ }
        ok 1, "third is good";
    }; BEGIN{ $line{outerfail3} = __LINE__ }
}
{
    test_out("# Subtest: namehere");
    test_out("    1..3");
    test_out("    ok 1 - first is good");
    test_out("    not ok 2 - second is bad");
    test_err("    #   Failed test 'second is bad'");
    test_err("    #   at $0 line $line{innerfail3}.");
    test_out("    ok 3 - third is good");
    test_err("    # Looks like you failed 1 test of 3.");
    test_out("not ok 1 - namehere");
    test_err("#   Failed test 'namehere'");
    test_err("#   at $0 line $line{outerfail3}.");

    run_the_subtest();

    test_test("subtest() called from a sub");
}
{
    test_out( "# Subtest: namehere");
    test_out( "    1..0");
    test_err( "    # No tests run!");
    test_out( 'not ok 1 - No tests run for subtest "namehere"');
    test_err(q{#   Failed test 'No tests run for subtest "namehere"'});
    test_err( "#   at $0 line $line{outerfail4}.");

    subtest namehere => sub {
        done_testing;
    }; BEGIN{ $line{outerfail4} = __LINE__ }

    test_test("lineno in 'No tests run' diagnostic");
}
{
    test_out("# Subtest: namehere");
    test_out("    1..1");
    test_out("    not ok 1 - foo is bar");
    test_err("    #   Failed test 'foo is bar'");
    test_err("    #   at $0 line $line{is_fail}.");
    test_err("    #          got: 'foo'");
    test_err("    #     expected: 'bar'");
    test_err("    # Looks like you failed 1 test of 1.");
    test_out('not ok 1 - namehere');
    test_err("#   Failed test 'namehere'");
    test_err("#   at $0 line $line{is_outer_fail}.");

    subtest namehere => sub {
        plan tests => 1;
        is 'foo', 'bar', 'foo is bar'; BEGIN{ $line{is_fail} = __LINE__ }
    }; BEGIN{ $line{is_outer_fail} = __LINE__ }

    test_test("diag indent for is() in subtest");
}