File: Migrating.pm

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 (431 lines) | stat: -rw-r--r-- 10,073 bytes parent folder | download | duplicates (2)
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
package Test2::Manual::Testing::Migrating;
use strict;
use warnings;

our $VERSION = '1.302210';

1;

=head1 NAME

Test2::Manual::Testing::Migrating - How to migrate existing tests from
Test::More to Test2.

=head1 DESCRIPTION

This tutorial covers the conversion of an existing test. This tutorial assumes
you have a test written using L<Test::More>.

=head1 LEGACY TEST

This tutorial will be converting this example test one section at a time:

C<t/example.t>:

    #####################
    # Boilerplate

    use strict;
    use warnings;

    use Test::More tests => 14;

    use_ok 'Scalar::Util';
    require_ok 'Exporter';

    #####################
    # Simple assertions (no changes)

    ok(1, "pass");

    is("apple", "apple", "Simple string compare");

    like("foo bar baz", qr/bar/, "Regex match");

    #####################
    # Todo

    {
        local $TODO = "These are todo";

        ok(0, "oops");
    }

    #####################
    # Deep comparisons

    is_deeply([1, 2, 3], [1, 2, 3], "Deep comparison");

    #####################
    # Comparing references

    my $ref = [1];
    is($ref, $ref, "Check that we have the same ref both times");

    #####################
    # Things that are gone

    ok(eq_array([1], [1]), "array comparison");
    ok(eq_hash({a => 1}, {a => 1}), "hash comparison");
    ok(eq_set([1, 3, 2], [1, 2, 3]), "set comparison");

    note explain([1, 2, 3]);

    {
        package THING;
        sub new { bless({}, shift) }
    }

    my $thing = new_ok('THING');

    #####################
    # Tools that changed

    isa_ok($thing, 'THING', '$thing');

    can_ok(__PACKAGE__, qw/ok is/);

=head1 BOILERPLATE

BEFORE:

    use strict;
    use warnings;

    use Test::More tests => 14;

    use_ok 'Scalar::Util';
    require_ok 'Exporter';

AFTER:

    use Test2::V0;
    plan(11);

    use Scalar::Util;
    require Exporter;

=over 4

=item Replace Test::More with Test2::V0

L<Test2::V0> is the recommended bundle. In a full migration you
will want to replace L<Test::More> with the L<Test2::V0> bundle.

B<Note:> You should always double check the latest L<Test2> to see if there is
a new recommended bundle. When writing a new test you should always use the
newest Test::V# module. Higher numbers are newer version.

=item NOTE: srand

When srand is on (default) it can cause problems with things like L<File::Temp>
which will end up attempting the same "random" filenames for every test process
started on a given day (or sharing the same seed).

If this is a problem for you then please disable srand when loading
L<Test2::V0>:

    use Test2::V0 -no_srand => 1;

=item Stop using use_ok()

C<use_ok()> has been removed. a C<use MODULE> statement will throw an exception
on failure anyway preventing the test from passing.

If you I<REALLY> want/need to assert that the file loaded you can use the L<ok>
module:

    use ok 'Scalar::Util';

The main difference here is that there is a space instead of an underscore.

=item Stop using require_ok()

C<require_ok> has been removed just like C<use_ok>. There is no L<ok> module
equivalent here. Just use C<require>.

=item Remove strict/warnings (optional)

The L<Test2::V0> bundle turns strict and warnings on for you.

=item Change where the plan is set

Test2 does not allow you to set the plan at import. In the old code you would
pass C<< tests => 11 >> as an import argument. In L<Test2> you either need to
use the C<plan()> function to set the plan, or use C<done_testing()> at the end
of the test.

If your test already uses C<done_testing()> you can keep that and no plan
changes are necessary.

B<Note:> We are also changing the plan from 14 to 11, that is because we
dropped C<use_ok>, C<require_ok>, and we will be dropping one more later on.
This is why C<done_testing()> is recommended over a set plan.

=back

=head1 SIMPLE ASSERTIONS

The vast majority of assertions will not need any changes:

    #####################
    # Simple assertions (no changes)

    ok(1, "pass");

    is("apple", "apple", "Simple string compare");

    like("foo bar baz", qr/bar/, "Regex match");

=head1 TODO

    {
        local $TODO = "These are todo";

        ok(0, "oops");
    }

The C<$TODO> package variable is gone. You now have a C<todo()> function.

There are 2 ways this can be used:

=over 4

=item todo $reason => sub { ... }

    todo "These are todo" => sub {
        ok(0, "oops");
    };

This is the cleanest way to do a todo. This will make all assertions inside the
codeblock into TODO assertions.

=item { my $TODO = todo $reason; ... }

    {
        my $TODO = todo "These are todo";

        ok(0, "oops");
    }

This is a system that emulates the old way. Instead of modifying a global
C<$TODO> variable you create a todo object with the C<todo()> function and
assign it to a lexical variable. Once the todo object falls out of scope the
TODO ends.

=back

=head1 DEEP COMPARISONS

    is_deeply([1, 2, 3], [1, 2, 3], "Deep comparison");

Deep comparisons are easy, simply replace C<is_deeply()> with C<is()>.

    is([1, 2, 3], [1, 2, 3], "Deep comparison");

=head1 COMPARING REFERENCES

    my $ref = [1];
    is($ref, $ref, "Check that we have the same ref both times");

The C<is()> function provided by L<Test::More> forces both arguments into
strings, which makes this a comparison of the reference addresses. L<Test2>'s
C<is()> function is a deep comparison, so this will still pass, but fails to
actually test what we want (that both references are the same exact ref, not
just identical structures.)

We now have the C<ref_is()> function that does what we really want, it ensures
both references are the same reference. This function does the job better than
the original, which could be thrown off by string overloading.

    my $ref = [1];
    ref_is($ref, $ref, "Check that we have the same ref both times");

=head1 TOOLS THAT ARE GONE

    ok(eq_array([1], [1]), "array comparison");
    ok(eq_hash({a => 1}, {a => 1}), "hash comparison");
    ok(eq_set([1, 3, 2], [1, 2, 3]), "set comparison");

    note explain([1, 2, 3]);

    {
        package THING;
        sub new { bless({}, shift) }
    }

    my $thing = new_ok('THING');

C<eq_array>, C<eq_hash> and C<eq_set> have been considered deprecated for a
very long time, L<Test2> does not provide them at all. Instead you can just use
C<is()>:

    is([1], [1], "array comparison");
    is({a => 1}, {a => 1}, "hash comparison");

C<eq_set> is a tad more complicated, see L<Test2::Tools::Compare> for an
explanation:

    is([1, 3, 2], bag { item 1; item 2; item 3; end }, "set comparison");

C<explain()> has a rocky history. There have been arguments about how it should
work. L<Test2> decided to simply not include C<explain()> to avoid the
arguments. You can instead directly use Data::Dumper:

    use Data::Dumper;
    note Dumper([1, 2, 3]);

C<new_ok()> is gone. The implementation was complicated, and did not add much
value:

    {
        package THING;
        sub new { bless({}, shift) }
    }

    my $thing = THING->new;
    ok($thing, "made a new thing");

The complete section after the conversion is:

    is([1], [1], "array comparison");
    is({a => 1}, {a => 1}, "hash comparison");
    is([1, 3, 2], bag { item 1; item 2; item 3; end }, "set comparison");

    use Data::Dumper;
    note Dumper([1, 2, 3]);

    {
        package THING;
        sub new { bless({}, shift) }
    }

    my $thing = THING->new;
    ok($thing, "made a new thing");

=head1 TOOLS THAT HAVE CHANGED

    isa_ok($thing, 'THING', '$thing');

    can_ok(__PACKAGE__, qw/ok is/);

In L<Test::More> these functions are very confusing, and most people use them
wrong!

C<isa_ok()> from L<Test::More> takes a thing, a class/reftype to check, and
then uses the third argument as an alternative display name for the first
argument (NOT a test name!).

C<can_ok()> from L<Test::More> is not consistent with C<isa_ok> as all
arguments after the first are subroutine names.

L<Test2> fixes this by making both functions consistent and obvious:

    isa_ok($thing, ['THING'], 'got a THING');

    can_ok(__PACKAGE__, [qw/ok is/], "have expected subs");

You will note that both functions take a thing, an arrayref as the second
argument, then a test name as the third argument.

=head1 FINAL VERSION

    #####################
    # Boilerplate

    use Test2::V0;
    plan(11);

    use Scalar::Util;
    require Exporter;

    #####################
    # Simple assertions (no changes)

    ok(1, "pass");

    is("apple", "apple", "Simple string compare");

    like("foo bar baz", qr/bar/, "Regex match");

    #####################
    # Todo

    todo "These are todo" => sub {
        ok(0, "oops");
    };

    #####################
    # Deep comparisons

    is([1, 2, 3], [1, 2, 3], "Deep comparison");

    #####################
    # Comparing references

    my $ref = [1];
    ref_is($ref, $ref, "Check that we have the same ref both times");

    #####################
    # Things that are gone

    is([1], [1], "array comparison");
    is({a => 1}, {a => 1}, "hash comparison");

    is([1, 3, 2], bag { item 1; item 2; item 3; end }, "set comparison");

    use Data::Dumper;
    note Dumper([1, 2, 3]);

    {
        package THING;
        sub new { bless({}, shift) }
    }

    my $thing = THING->new;

    #####################
    # Tools that changed

    isa_ok($thing, ['THING'], 'got a THING');

    can_ok(__PACKAGE__, [qw/ok is/], "have expected subs");

=head1 SEE ALSO

L<Test2::Manual> - Primary index of the manual.

=head1 SOURCE

The source code repository for Test2-Manual can be found at
F<https://github.com/Test-More/test-more/>.

=head1 MAINTAINERS

=over 4

=item Chad Granum E<lt>exodist@cpan.orgE<gt>

=back

=head1 AUTHORS

=over 4

=item Chad Granum E<lt>exodist@cpan.orgE<gt>

=back

=head1 COPYRIGHT

Copyright Chad Granum E<lt>exodist@cpan.orgE<gt>.

This program is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.

See F<http://dev.perl.org/licenses/>

=cut