| 1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
use Test::More tests => 17; |
| 6 |
use TAP::Harness; |
| 7 |
use IO::Scalar; |
| 8 |
use File::Slurp qw(write_file); |
| 9 |
|
| 10 |
############################################################################### |
| 11 |
# Ensure timing correctness, when test has a plan |
| 12 |
# |
| 13 |
# Test once with merged output off, then once with it on; want to make sure that |
| 14 |
# merging diagnostic output into the TAP doesn't monkey up the timings. |
| 15 |
correct_timing_test_has_plan: { |
| 16 |
my $test = qq| |
| 17 |
BEGIN { sleep 3 }; |
| 18 |
END { sleep 2 }; |
| 19 |
use Test::More tests => 3; |
| 20 |
sleep 0; pass "one"; |
| 21 |
sleep 2; pass "two"; |
| 22 |
|
| 23 |
sleep 1; diag "foo"; |
| 24 |
sleep 1; diag "bar"; |
| 25 |
sleep 3; diag "foobar"; |
| 26 |
pass "three"; |
| 27 |
|; |
| 28 |
my $expect = { |
| 29 |
'(init)' => 3, |
| 30 |
'1 - one' => 0, |
| 31 |
'2 - two' => 2, |
| 32 |
'3 - three' => 5, |
| 33 |
'(teardown)' => 2, |
| 34 |
}; |
| 35 |
|
| 36 |
unmerged: { |
| 37 |
my $results = run_test($test, { |
| 38 |
timer => 1, |
| 39 |
merge => 0, |
| 40 |
} ); |
| 41 |
ok $results, 'got JUnit - timing correctness w/test plan (unmerged)'; |
| 42 |
verify_timings($results, $expect); |
| 43 |
} |
| 44 |
|
| 45 |
merged: { |
| 46 |
my $results = run_test($test, { |
| 47 |
timer => 1, |
| 48 |
merge => 1, |
| 49 |
} ); |
| 50 |
ok $results, 'got JUnit - timing correctness w/test plan (merged)'; |
| 51 |
verify_timings($results, $expect); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
############################################################################### |
| 56 |
# Ensure timing correctness, when test has no plan |
| 57 |
# |
| 58 |
# The *first* test isn't going to be predictable/accurate w.r.t. the calculated |
| 59 |
# timing, as it'll also involve the startup overhead. As such, its skipped (by |
| 60 |
# denoting it as "skip" in its test name). |
| 61 |
correct_timing_test_unplanned: { |
| 62 |
my $test = qq| |
| 63 |
BEGIN { sleep 3 }; |
| 64 |
END { sleep 2 }; |
| 65 |
use Test::More qw(no_plan); |
| 66 |
sleep 0; pass "one"; |
| 67 |
sleep 2; pass "two"; |
| 68 |
|
| 69 |
sleep 1; diag "foo"; |
| 70 |
sleep 1; diag "bar"; |
| 71 |
sleep 3; diag "foobar"; |
| 72 |
pass "three"; |
| 73 |
|; |
| 74 |
my $expect = { |
| 75 |
'1 - one' => 3, # init time is *hidden* in initial test |
| 76 |
'2 - two' => 2, |
| 77 |
'3 - three' => 5, |
| 78 |
'(teardown)' => 2, |
| 79 |
}; |
| 80 |
|
| 81 |
my $results = run_test($test, { |
| 82 |
timer => 1, |
| 83 |
merge => 1, |
| 84 |
} ); |
| 85 |
ok $results, 'got JUnit - timing correctness w/o test plan'; |
| 86 |
verify_timings($results, $expect); |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
sub run_test { |
| 91 |
my $code = shift; |
| 92 |
my $opts = shift; |
| 93 |
my $file = "test-$$.t"; |
| 94 |
|
| 95 |
my $junit = undef; |
| 96 |
my $fh = IO::Scalar->new(\$junit); |
| 97 |
my $harness = TAP::Harness->new( { |
| 98 |
formatter_class => 'TAP::Formatter::JUnit', |
| 99 |
stdout => $fh, |
| 100 |
%{$opts}, |
| 101 |
} ); |
| 102 |
|
| 103 |
write_file($file, $code); |
| 104 |
$harness->runtests($file); |
| 105 |
unlink $file; |
| 106 |
|
| 107 |
return $junit; |
| 108 |
} |
| 109 |
|
| 110 |
sub verify_timings { |
| 111 |
my $junit = shift; |
| 112 |
my $expect = shift; |
| 113 |
|
| 114 |
my @lines = split /^/, $junit; |
| 115 |
my @tests = grep { /<testcase/ } @lines; |
| 116 |
|
| 117 |
foreach my $test (@tests) { |
| 118 |
my ($time) = ($test =~ /time="([^"]+)"/); |
| 119 |
my ($name) = ($test =~ /name="([^"]+)"/); |
| 120 |
if ((!defined $time) || (!defined $name)) { |
| 121 |
fail "... unexpected test line: $test"; |
| 122 |
next; |
| 123 |
} |
| 124 |
|
| 125 |
if (exists $expect->{$name}) { |
| 126 |
rounds_to($time, $expect->{$name}, "... test timing: $name"); |
| 127 |
} |
| 128 |
else { |
| 129 |
fail "... unexpected test name: $name"; |
| 130 |
diag $test; |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
sub rounds_to { |
| 136 |
my ($got, $expected, $message) = @_; |
| 137 |
my $r_got = sprintf('%1.0f', $got); |
| 138 |
my $r_expected = sprintf('%1.0f', $expected); |
| 139 |
local $Test::Builder::Level = $Test::Builder::Level + 1; |
| 140 |
is $r_got, $r_expected, $message; |
| 141 |
} |