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

our $VERSION = '0.000162';

1;

__END__

=head1 NAME

Test2::Manual::Tooling::FirstTool - Write your first tool with Test2.

=head1 DESCRIPTION

This tutorial will help you write your very first tool by cloning the C&lt;ok()&gt;
tool.

=head1 COMPLETE CODE UP FRONT

    package Test2::Tools::MyOk;
    use strict;
    use warnings;

    use Test2::API qw/context/;

    use base 'Exporter';
    our @EXPORT = qw/ok/;

    sub ok($;$@) {
        my ($bool, $name, @diag) = @_;

        my $ctx = context();

        return $ctx-&gt;pass_and_release($name) if $bool;
        return $ctx-&gt;fail_and_release($name, @diag);
    }

    1;

=head1 LINE BY LINE

=over 4

=item sub ok($;$@) {

In this case we are emulating the C&lt;ok()&gt; function exported by
L&lt;Test2::Tools::Basic&gt;.

C&lt;ok()&gt; and similar test tools use prototypes to enforce argument parsing. Your
test tools do not necessarily need prototypes, like any perl function you need
to make the decision based on how it is used.

The prototype requires at least 1 argument, which will
be forced into a scalar context. The second argument is optional, and is also
forced to be scalar, it is the name of the test. Any remaining arguments are
treated as diagnostics messages that will only be used if the test failed.

=item my ($bool, $name, @diag) = @_;

This line does not need much explanation, we are simply grabbing the args.

=item my $ctx = context();

This is a vital line in B&lt;ALL&gt; tools. The context object is the primary API for
test tools. You B&lt;MUST&gt; get a context if you want to issue any events, such as
making assertions. Further, the context is responsible for making sure failures
are attributed to the correct file and line number.

B&lt;Note:&gt; A test function B&lt;MUST&gt; always release the context when it is done,
you cannot simply let it fall out of scope and be garbage collected. Test2 does
a pretty good job of yelling at you if you make this mistake.

B&lt;Note:&gt; You B&lt;MUST NOT&gt; ever store or pass around a I&lt;real&gt; context object. If
you wish to hold on to a context for any reason you must use clone to make a
copy C&lt;&lt; my $copy = $ctx-&gt;clone &gt;&gt;. The copy may be passed around or stored,
but the original B&lt;MUST&gt; be released when you are done with it.

=item return $ctx-&gt;pass_and_release($name) if $bool;

When C&lt;$bool&gt; is true, this line uses the context object to issue a
L&lt;Test2::Event::Pass&gt; event. Along with issuing the event this will also
release the context object and return true.

This is short form for:

    if($bool) {
        $ctx-&gt;pass($name);
        $ctx-&gt;release;
        return 1;
    }

=item return $ctx-&gt;fail_and_release($name, @diag);

This line issues a L&lt;Test2::Event::Fail&gt; event, releases the context object,
and returns false. The fail event will include any diagnostics messages from
the C&lt;@diag&gt; array.

This is short form for:

    $ctx-&gt;fail($name, @diag);
    $ctx-&gt;release;
    return 0;

=back

=head1 CONTEXT OBJECT DOCUMENTATION

L&lt;Test2::API::Context&gt; is the place to read up on what methods the context
provides.

=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>