4 use Test::More 'modern';
7 can_ok( __PACKAGE__, 'intercept', 'results_are' );
9 my $results = intercept {
14 isa_ok($results->[0], 'Test::Builder::Result::Ok');
15 is($results->[0]->bool, 1, "Got one success");
16 is($results->[0]->name, "Woo!", "Got test name");
18 isa_ok($results->[1], 'Test::Builder::Result::Ok');
19 is($results->[1]->bool, 0, "Got one fail");
20 is($results->[1]->name, "Boo!", "Got test name");
22 $results = intercept {
25 ok(0, "Should not see this");
27 is(@$results, 2, "Only got 2");
28 isa_ok($results->[0], 'Test::Builder::Result::Ok');
29 isa_ok($results->[1], 'Test::Builder::Result::Bail');
31 $results = intercept {
32 plan skip_all => 'All tests are skipped';
36 ok(0, "Should not see this");
38 is(@$results, 1, "Only got 1");
39 isa_ok($results->[0], 'Test::Builder::Result::Plan');
44 intercept { ok(1, "foo") },
45 ok => {id => 'blah', bool => 0},
46 end => 'Lets name this test!',
50 ok => {id => 'first', bool => 0},
52 diag => {message => qr{Failed test 'Lets name this test!'.*at (\./)?t/Modern/Tester2\.t line}s},
53 diag => {message => q{(ok blah) Wanted bool => '0', but got bool => '1'}},
54 diag => {message => <<" EOT"},
55 Full result found was: ok => {
61 file: t/Modern/Tester2.t
65 source: t/Modern/Tester2.t
67 tool_package: Test::More
71 end => 'Failure diag checking',
77 intercept { ok(1, "foo"); ok(1, "bar") },
78 ok => {id => 'blah', bool => 1},
83 ok => {id => 'first', bool => 0},
86 diag => {message => q{Expected end of results, but more results remain}},
88 end => 'skipping a diag',
93 local $SIG{__WARN__} = sub { push @warn => @_ };
95 local $Test::Builder::Level = $Test::Builder::Level + 1;
99 # The results generated here are to be ignored. We are just checking on warnings.
100 intercept { $doit->(); $doit->(); $doit->() };
102 is(@warn, 1, "got a warning, but only once");
103 like($warn[0], qr/\$Test::Builder::Level was used to trace a test! \$Test::Builder::Level is deprecated!/, "Expected warning");
107 # Intercept all the Test::Builder::Result objects produced in the block.
108 my $results = intercept {
115 is($results->[0]->{bool}, 1, "First result passed");
120 ok => { id => 'a', bool => 1, name => 'pass' },
121 ok => { id => 'b', bool => 0, name => 'fail' },
122 diag => { message => qr/Failed test 'fail'/ },
123 diag => { message => qr/xxx/ },
129 require Test::Simple;
130 my $results = intercept {
131 Test::More::ok(1, "foo");
132 Test::More::ok(1, "bar");
133 Test::More::ok(1, "baz");
134 Test::Simple::ok(1, "bat");
139 ok => { name => "foo" },
140 ok => { name => "bar" },
142 # From this point on, only more 'Test::Simple' results will be checked.
143 filter_provider => 'Test::Simple',
145 # So it goes right to the Test::Simple result.
146 ok => { name => "bat" },
153 my $results = intercept {
166 ok => { name => "foo" },
167 diag => { message => 'XXX' },
168 ok => { name => "bar" },
169 diag => { message => 'YYY' },
171 # From this point on, only 'diag' types will be seen
172 filter_type => 'diag',
174 # So it goes right to the next diag.
175 diag => { message => 'ZZZ' },
182 my $results = intercept {
195 ok => { name => "foo" },
197 skip => 1, # Skips the diag
199 ok => { name => "bar" },
201 skip => 2, # Skips a diag and an ok
203 diag => { message => 'ZZZ' },
210 my $results = intercept {
222 ok => { name => "foo" },
224 skip => '*', # Skip until the next 'ok' is found since that is our next check.
226 ok => { name => "bar" },
233 my $results = intercept {
250 name => 'docs 6 inner',
253 ok => { name => "foo" },
254 # The diags are ignored,
255 ok => { name => "bar" },
259 # This will fail because the diag is not ignored anymore.
260 ok => { name => "baz" },
265 diag => { message => qr/Failed test 'docs 6 inner'/ },
266 diag => { message => q{(ok 3) Wanted result type 'ok', But got: 'diag'} },
267 diag => { message => qr/Full result found was:/ },