<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package Test2::Manual::Testing::Migrating;
use strict;
use warnings;

our $VERSION = '0.000162';

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&lt;Test::More&gt;.

=head1 LEGACY TEST

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

C&lt;t/example.t&gt;:

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

    use strict;
    use warnings;

    use Test::More tests =&gt; 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 =&gt; 1}, {a =&gt; 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 =&gt; 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&lt;Test2::V0&gt; is the recommended bundle. In a full migration you
will want to replace L&lt;Test::More&gt; with the L&lt;Test2::V0&gt; bundle.

B&lt;Note:&gt; You should always double check the latest L&lt;Test2&gt; 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&lt;File::Temp&gt;
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&lt;Test2::V0&gt;:

    use Test2::V0 -no_srand =&gt; 1;

=item Stop using use_ok()

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

If you I&lt;REALLY&gt; want/need to assert that the file loaded you can use the L&lt;ok&gt;
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&lt;require_ok&gt; has been removed just like C&lt;use_ok&gt;. There is no L&lt;ok&gt; module
equivalent here. Just use C&lt;require&gt;.

=item Remove strict/warnings (optional)

The L&lt;Test2::V0&gt; 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&lt;&lt; tests =&gt; 11 &gt;&gt; as an import argument. In L&lt;Test2&gt; you either need to
use the C&lt;plan()&gt; function to set the plan, or use C&lt;done_testing()&gt; at the end
of the test.

If your test already uses C&lt;done_testing()&gt; you can keep that and no plan
changes are necessary.

B&lt;Note:&gt; We are also changing the plan from 14 to 11, that is because we
dropped C&lt;use_ok&gt;, C&lt;require_ok&gt;, and we will be dropping one more later on.
This is why C&lt;done_testing()&gt; 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&lt;$TODO&gt; package variable is gone. You now have a C&lt;todo()&gt; function.

There are 2 ways this can be used:

=over 4

=item todo $reason =&gt; sub { ... }

    todo "These are todo" =&gt; 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&lt;$TODO&gt; variable you create a todo object with the C&lt;todo()&gt; 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&lt;is_deeply()&gt; with C&lt;is()&gt;.

    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&lt;is()&gt; function provided by L&lt;Test::More&gt; forces both arguments into
strings, which makes this a comparison of the reference addresses. L&lt;Test2&gt;'s
C&lt;is()&gt; 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&lt;ref_is()&gt; 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 =&gt; 1}, {a =&gt; 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&lt;eq_array&gt;, C&lt;eq_hash&gt; and C&lt;eq_set&gt; have been considered deprecated for a
very long time, L&lt;Test2&gt; does not provide them at all. Instead you can just use
C&lt;is()&gt;:

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

C&lt;eq_set&gt; is a tad more complicated, see L&lt;Test2::Tools::Compare&gt; for an
explanation:

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

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

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

C&lt;new_ok()&gt; is gone. The implementation was complicated, and did not add much
value:

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

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

The complete section after the conversion is:

    is([1], [1], "array comparison");
    is({a =&gt; 1}, {a =&gt; 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-&gt;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&lt;Test::More&gt; these functions are very confusing, and most people use them
wrong!

C&lt;isa_ok()&gt; from L&lt;Test::More&gt; 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&lt;can_ok()&gt; from L&lt;Test::More&gt; is not consistent with C&lt;isa_ok&gt; as all
arguments after the first are subroutine names.

L&lt;Test2&gt; 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" =&gt; 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 =&gt; 1}, {a =&gt; 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-&gt;new;

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

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

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

=head1 SEE ALSO

L&lt;Test2::Manual&gt; - Primary index of the manual.

=head1 SOURCE

The source code repository for Test2-Manual can be found at
F&lt;https://github.com/Test-More/Test2-Suite/&gt;.

=head1 MAINTAINERS

=over 4

=item Chad Granum E&lt;lt&gt;exodist@cpan.orgE&lt;gt&gt;

=back

=head1 AUTHORS

=over 4

=item Chad Granum E&lt;lt&gt;exodist@cpan.orgE&lt;gt&gt;

=back

=head1 COPYRIGHT

Copyright 2018 Chad Granum E&lt;lt&gt;exodist@cpan.orgE&lt;gt&gt;.

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

See F&lt;http://dev.perl.org/licenses/&gt;

=cut



</pre></body></html>