This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
2d56440a55a8131b006337db1d7d5d7bf6ac2cac
[perl5.git] / cpan / Test-Simple / t / Modern / Tester2.t
1 use strict;
2 use warnings;
3
4 use Test::More 'modern';
5 use Test::Tester2;
6
7 can_ok( __PACKAGE__, 'intercept', 'results_are' );
8
9 my $results = intercept {
10     ok(1, "Woo!");
11     ok(0, "Boo!");
12 };
13
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");
17
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");
21
22 $results = intercept {
23     ok(1, "Woo!");
24     BAIL_OUT("Ooops");
25     ok(0, "Should not see this");
26 };
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');
30
31 $results = intercept {
32     plan skip_all => 'All tests are skipped';
33
34     ok(1, "Woo!");
35     BAIL_OUT("Ooops");
36     ok(0, "Should not see this");
37 };
38 is(@$results, 1, "Only got 1");
39 isa_ok($results->[0], 'Test::Builder::Result::Plan');
40
41 results_are(
42     intercept {
43         results_are(
44             intercept { ok(1, "foo") },
45             ok => {id => 'blah', bool => 0},
46             end => 'Lets name this test!',
47         );
48     },
49
50     ok => {id => 'first', bool => 0},
51
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 => {
56   name: foo
57   bool: 1
58   real_bool: 1
59   in_todo: 0
60   package: main
61   file: t/Modern/Tester2.t
62   line: 44
63   pid: $$
64   depth: 0
65   source: t/Modern/Tester2.t
66   tool_name: ok
67   tool_package: Test::More
68   tap: ok - foo
69 }
70     EOT
71     end => 'Failure diag checking',
72 );
73
74 results_are(
75     intercept {
76         results_are(
77             intercept { ok(1, "foo"); ok(1, "bar") },
78             ok => {id => 'blah', bool => 1},
79             'end'
80         );
81     },
82
83     ok => {id => 'first', bool => 0},
84
85     diag => {},
86     diag => {message => q{Expected end of results, but more results remain}},
87
88     end => 'skipping a diag',
89 );
90
91 {
92     my @warn;
93     local $SIG{__WARN__} = sub { push @warn => @_ };
94     my $doit = sub {
95         local $Test::Builder::Level = $Test::Builder::Level + 1;
96         ok(1, "example");
97     };
98
99     # The results generated here are to be ignored. We are just checking on warnings.
100     intercept { $doit->(); $doit->(); $doit->() };
101
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");
104 }
105
106 DOCS_1: {
107     # Intercept all the Test::Builder::Result objects produced in the block.
108     my $results = intercept {
109         ok(1, "pass");
110         ok(0, "fail");
111         diag("xxx");
112     };
113
114     # By Hand
115     is($results->[0]->{bool}, 1, "First result passed");
116
117     # With help
118     results_are(
119         $results,
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/ },
124         end => 'docs 1',
125     );
126 }
127
128 DOCS_2: {
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");
135     };
136
137     results_are(
138         $results,
139         ok => { name => "foo" },
140         ok => { name => "bar" },
141
142         # From this point on, only more 'Test::Simple' results will be checked.
143         filter_provider => 'Test::Simple',
144
145         # So it goes right to the Test::Simple result.
146         ok => { name => "bat" },
147
148         end => 'docs 2',
149     );
150 }
151
152 DOCS_3: {
153     my $results = intercept {
154         ok(1, "foo");
155         diag("XXX");
156
157         ok(1, "bar");
158         diag("YYY");
159
160         ok(1, "baz");
161         diag("ZZZ");
162     };
163
164     results_are(
165         $results,
166         ok => { name => "foo" },
167         diag => { message => 'XXX' },
168         ok => { name => "bar" },
169         diag => { message => 'YYY' },
170
171         # From this point on, only 'diag' types will be seen
172         filter_type => 'diag',
173
174         # So it goes right to the next diag.
175         diag => { message => 'ZZZ' },
176
177         end => 'docs 3',
178     );
179 }
180
181 DOCS_4: {
182     my $results = intercept {
183         ok(1, "foo");
184         diag("XXX");
185
186         ok(1, "bar");
187         diag("YYY");
188
189         ok(1, "baz");
190         diag("ZZZ");
191     };
192
193     results_are(
194         $results,
195         ok => { name => "foo" },
196
197         skip => 1, # Skips the diag
198
199         ok => { name => "bar" },
200
201         skip => 2, # Skips a diag and an ok
202
203         diag => { message => 'ZZZ' },
204
205         end => 'docs 4'
206     );
207 }
208
209 DOCS_5: {
210     my $results = intercept {
211         ok(1, "foo");
212
213         diag("XXX");
214         diag("YYY");
215         diag("ZZZ");
216
217         ok(1, "bar");
218     };
219
220     results_are(
221         $results,
222         ok => { name => "foo" },
223
224         skip => '*', # Skip until the next 'ok' is found since that is our next check.
225
226         ok => { name => "bar" },
227
228         end => 'docs 5',
229     );
230 }
231
232 DOCS_6: {
233     my $results = intercept {
234         ok(1, "foo");
235
236         diag("XXX");
237         diag("YYY");
238
239         ok(1, "bar");
240         diag("ZZZ");
241
242         ok(1, "baz");
243     };
244
245     results_are(
246         intercept {
247             results_are(
248                 $results,
249
250                 name => 'docs 6 inner',
251
252                 seek => 1,
253                 ok => { name => "foo" },
254                 # The diags are ignored,
255                 ok => { name => "bar" },
256
257                 seek => 0,
258
259                 # This will fail because the diag is not ignored anymore.
260                 ok => { name => "baz" },
261             );
262         },
263
264         ok => { bool => 0 },
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:/ },
268
269         end => 'docs 6',
270     );
271 }
272
273 done_testing;