1#!/usr/bin/perl -w
2
3# Test Test::More::subtest(), focusing on correct line numbers in
4# failed test diagnostics.
5
6BEGIN {
7    if( $ENV{PERL_CORE} ) {
8        chdir 't';
9        @INC = ( '../lib', 'lib' );
10    }
11    else {
12        unshift @INC, 't/lib';
13    }
14}
15
16use strict;
17use warnings;
18
19use Test::More tests => 5;
20use Test::Builder;
21use Test::Builder::Tester;
22
23# Formatting may change if we're running under Test::Harness.
24$ENV{HARNESS_ACTIVE} = 0;
25
26our %line;
27
28{
29    test_out("# Subtest: namehere");
30    test_out("    1..3");
31    test_out("    ok 1");
32    test_out("    not ok 2");
33    test_err("    #   Failed test at $0 line $line{innerfail1}.");
34    test_out("    ok 3");
35    test_err("    # Looks like you failed 1 test of 3.");
36    test_out("not ok 1 - namehere");
37    test_err("#   Failed test 'namehere'");
38    test_err("#   at $0 line $line{outerfail1}.");
39
40    subtest namehere => sub {
41        plan tests => 3;
42        ok 1;
43        ok 0; BEGIN{ $line{innerfail1} = __LINE__ }
44        ok 1;
45    }; BEGIN{ $line{outerfail1} = __LINE__ }
46
47    test_test("un-named inner tests");
48}
49{
50    test_out("# Subtest: namehere");
51    test_out("    1..3");
52    test_out("    ok 1 - first is good");
53    test_out("    not ok 2 - second is bad");
54    test_err("    #   Failed test 'second is bad'");
55    test_err("    #   at $0 line $line{innerfail2}.");
56    test_out("    ok 3 - third is good");
57    test_err("    # Looks like you failed 1 test of 3.");
58    test_out("not ok 1 - namehere");
59    test_err("#   Failed test 'namehere'");
60    test_err("#   at $0 line $line{outerfail2}.");
61
62    subtest namehere => sub {
63        plan tests => 3;
64        ok 1, "first is good";
65        ok 0, "second is bad"; BEGIN{ $line{innerfail2} = __LINE__ }
66        ok 1, "third is good";
67    }; BEGIN{ $line{outerfail2} = __LINE__ }
68
69    test_test("named inner tests");
70}
71
72sub run_the_subtest {
73    subtest namehere => sub {
74        plan tests => 3;
75        ok 1, "first is good";
76        ok 0, "second is bad"; BEGIN{ $line{innerfail3} = __LINE__ }
77        ok 1, "third is good";
78    }; BEGIN{ $line{outerfail3} = __LINE__ }
79}
80{
81    test_out("# Subtest: namehere");
82    test_out("    1..3");
83    test_out("    ok 1 - first is good");
84    test_out("    not ok 2 - second is bad");
85    test_err("    #   Failed test 'second is bad'");
86    test_err("    #   at $0 line $line{innerfail3}.");
87    test_out("    ok 3 - third is good");
88    test_err("    # Looks like you failed 1 test of 3.");
89    test_out("not ok 1 - namehere");
90    test_err("#   Failed test 'namehere'");
91    test_err("#   at $0 line $line{outerfail3}.");
92
93    run_the_subtest();
94
95    test_test("subtest() called from a sub");
96}
97{
98    test_out( "# Subtest: namehere");
99    test_out( "    1..0");
100    test_err( "    # No tests run!");
101    test_out( 'not ok 1 - No tests run for subtest "namehere"');
102    test_err(q{#   Failed test 'No tests run for subtest "namehere"'});
103    test_err( "#   at $0 line $line{outerfail4}.");
104
105    subtest namehere => sub {
106        done_testing;
107    }; BEGIN{ $line{outerfail4} = __LINE__ }
108
109    test_test("lineno in 'No tests run' diagnostic");
110}
111{
112    test_out("# Subtest: namehere");
113    test_out("    1..1");
114    test_out("    not ok 1 - foo is bar");
115    test_err("    #   Failed test 'foo is bar'");
116    test_err("    #   at $0 line $line{is_fail}.");
117    test_err("    #          got: 'foo'");
118    test_err("    #     expected: 'bar'");
119    test_err("    # Looks like you failed 1 test of 1.");
120    test_out('not ok 1 - namehere');
121    test_err("#   Failed test 'namehere'");
122    test_err("#   at $0 line $line{is_outer_fail}.");
123
124    subtest namehere => sub {
125        plan tests => 1;
126        is 'foo', 'bar', 'foo is bar'; BEGIN{ $line{is_fail} = __LINE__ }
127    }; BEGIN{ $line{is_outer_fail} = __LINE__ }
128
129    test_test("diag indent for is() in subtest");
130}
131