File: Basic.t

package info (click to toggle)
perl 5.42.0-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 128,392 kB
  • sloc: perl: 534,963; ansic: 240,563; sh: 72,042; pascal: 6,934; xml: 2,428; yacc: 1,360; makefile: 1,197; cpp: 208; lisp: 1
file content (304 lines) | stat: -rw-r--r-- 6,868 bytes parent folder | download | duplicates (9)
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
use Test2::Bundle::Extended -target => 'Test2::Tools::Basic';

{
    package Temp;
    use Test2::Tools::Basic;

    main::imported_ok(qw{
        ok pass fail diag note todo skip
        plan skip_all done_testing bail_out
    });
}

pass('Testing Pass');

my @lines;
like(
    intercept {
        pass('pass');               push @lines => __LINE__;
        fail('fail');               push @lines => __LINE__;
        fail('fail', 'added diag'); push @lines => __LINE__;
    },
    array {
        event Ok => sub {
            call pass => 1;
            call name => 'pass';

            prop file    => __FILE__;
            prop package => __PACKAGE__;
            prop line    => $lines[0];
            prop subname => 'Test2::Tools::Basic::pass';
        };

        event Ok => sub {
            call pass => 0;
            call name => 'fail';

            prop file    => __FILE__;
            prop package => __PACKAGE__;
            prop line    => $lines[1];
            prop subname => 'Test2::Tools::Basic::fail';
        };
        event Diag => sub {
            call message => qr/Failed test 'fail'.*line $lines[1]/s;

            prop file    => __FILE__;
            prop package => __PACKAGE__;
            prop line    => $lines[1];
            prop subname => 'Test2::Tools::Basic::fail';
        };

        event Ok => sub {
            call pass => 0;
            call name => 'fail';

            prop file    => __FILE__;
            prop package => __PACKAGE__;
            prop line    => $lines[2];
            prop subname => 'Test2::Tools::Basic::fail';
        };
        event Diag => sub {
            call message => qr/Failed test 'fail'.*line $lines[2]/s;

            prop file    => __FILE__;
            prop package => __PACKAGE__;
            prop line    => $lines[2];
            prop subname => 'Test2::Tools::Basic::fail';
        };
        event Diag => sub {
            call message => 'added diag';

            prop file    => __FILE__;
            prop package => __PACKAGE__;
            prop line    => $lines[2];
            prop subname => 'Test2::Tools::Basic::fail';
        };

        end;
    },
    "Got expected events for 'pass' and 'fail'"
);

ok(1, 'Testing ok');

@lines = ();
like(
    intercept {
        ok(1, 'pass', 'invisible diag'); push @lines => __LINE__;
        ok(0, 'fail');                   push @lines => __LINE__;
        ok(0, 'fail', 'added diag');     push @lines => __LINE__;
    },
    array {
        event Ok => sub {
            call pass => 1;
            call name => 'pass';
            prop line => $lines[0];
        };

        event Ok => sub {
            call pass => 0;
            call name => 'fail';
            prop debug => 'at ' . __FILE__ . " line $lines[1]";
        };
        event Diag => sub {
            call message => qr/Failed test 'fail'.*line $lines[1]/s;
            prop debug => 'at ' . __FILE__ . " line $lines[1]";
        };

        event Ok => sub {
            call pass => 0;
            call name => 'fail';
            prop debug => 'at ' . __FILE__ . " line $lines[2]";
        };
        event Diag => sub {
            call message => qr/Failed test 'fail'.*line $lines[2]/s;
            prop debug => 'at ' . __FILE__ . " line $lines[2]";
        };
        event Diag => sub {
            call message => 'added diag';
            prop debug => 'at ' . __FILE__ . " line $lines[2]";
        };

        end;
    },
    "Got expected events for 'ok'"
);

diag "Testing Diag (AUTHOR_TESTING ONLY)" if $ENV{AUTHOR_TESTING};

like(
    intercept {
        diag "foo";
        diag "foo", ' ', "bar";
    },
    array {
        event Diag => { message => 'foo' };
        event Diag => { message => 'foo bar' };
    },
    "Got expected events for diag"
);

note "Testing Note";

like(
    intercept {
        note "foo";
        note "foo", ' ', "bar";
    },
    array {
        event Note => { message => 'foo' };
        event Note => { message => 'foo bar' };
    },
    "Got expected events for note"
);

like(
    intercept {
        bail_out 'oops';
        # Should not get here
        print STDERR "Something is wrong, did not bail out!\n";
        exit 255;
    },
    array {
        event Bail => { reason => 'oops' };
        end;
    },
    "Got bail event"
);

like(
    intercept {
        skip_all 'oops';
        # Should not get here
        print STDERR "Something is wrong, did not skip!\n";
        exit 255;
    },
    array {
        event Plan => { max => 0, directive => 'SKIP', reason => 'oops' };
        end;
    },
    "Got plan (skip_all) event"
);

like(
    intercept {
        plan skip_all => 'oops';
        # Should not get here
        print STDERR "Something is wrong, did not skip!\n";
        exit 255;
    },
    array {
        event Plan => { max => 0, directive => 'SKIP', reason => 'oops' };
        end;
    },
    "Got plan 'skip_all' prefix"
);


like(
    intercept {
        plan(5);
    },
    array {
        event Plan => { max => 5 };
        end;
    },
    "Got plan"
);

like(
    intercept {
        plan(tests => 5);
    },
    array {
        event Plan => { max => 5 };
        end;
    },
    "Got plan 'tests' prefix"
);


like(
    intercept {
        ok(1);
        ok(2);
        done_testing;
    },
    array {
        event Ok => { pass => 1 };
        event Ok => { pass => 1 };
        event Plan => { max => 2 };
        end;
    },
    "Done Testing works"
);

like(
    intercept {
        ok(0, "not todo");

        {
            my $todo = todo('todo 1');
            ok(0, 'todo fail');
        }

        ok(0, "not todo");

        my $todo = todo('todo 2');
        ok(0, 'todo fail');
        $todo = undef;

        ok(0, "not todo");

        todo 'todo 3' => sub {
            ok(0, 'todo fail');
        };

        ok(0, "not todo");
    },
    array {
        for my $id (1 .. 3) {
            event Ok => sub {
                call pass => 0;
                call effective_pass => 0;
                call todo => undef;
            };
            event Diag => { message => qr/Failed/ };

            event Ok => sub {
                call pass => 0;
                call effective_pass => 1;
                call todo => "todo $id";
            };
            event Note => { message => qr/Failed/ };
        }
        event Ok => { pass => 0, effective_pass => 0 };
        event Diag => { message => qr/Failed/ };
        end;
    },
    "Got todo events"
);

like(
    intercept {
        ok(1, 'pass');
        SKIP: {
            skip 'oops' => 5;

            ok(1, "Should not see this");
        }
    },
    array {
        event Ok => { pass => 1 };

        event Skip => sub {
            call pass => 1;
            call reason => 'oops';
        } for 1 .. 5;

        end;
    },
    "got skip events"
);

done_testing;