.\" -*- mode: troff; coding: utf-8 -*-
.\" Automatically generated by Pod::Man v6.0.2 (Pod::Simple 3.47)
.\"
.\" Standard preamble:
.\" ========================================================================
.de Sp \" Vertical space (when we can't use .PP)
.if t .sp .5v
.if n .sp
..
.de Vb \" Begin verbatim text
.ft CW
.nf
.ne \\$1
..
.de Ve \" End verbatim text
.ft R
.fi
..
.\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>.
.ie n \{\
.    ds C` ""
.    ds C' ""
'br\}
.el\{\
.    ds C`
.    ds C'
'br\}
.\"
.\" Escape single quotes in literal strings from groff's Unicode transform.
.ie \n(.g .ds Aq \(aq
.el       .ds Aq '
.\"
.\" If the F register is >0, we'll generate index entries on stderr for
.\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
.\" entries marked with X<> in POD.  Of course, you'll have to process the
.\" output yourself in some meaningful fashion.
.\"
.\" Avoid warning from groff about undefined register 'F'.
.de IX
..
.nr rF 0
.if \n(.g .if rF .nr rF 1
.if (\n(rF:(\n(.g==0)) \{\
.    if \nF \{\
.        de IX
.        tm Index:\\$1\t\\n%\t"\\$2"
..
.        if !\nF==2 \{\
.            nr % 0
.            nr F 2
.        \}
.    \}
.\}
.rr rF
.\"
.\" Required to disable full justification in groff 1.23.0.
.if n .ds AD l
.\" ========================================================================
.\"
.IX Title "Test2::Manual::Tooling::FirstTool 3"
.TH Test2::Manual::Tooling::FirstTool 3 2025-07-25 "perl v5.42.0" "User Contributed Perl Documentation"
.\" For nroff, turn off justification.  Always turn off hyphenation; it makes
.\" way too many mistakes in technical documents.
.if n .ad l
.nh
.SH NAME
Test2::Manual::Tooling::FirstTool \- Write your first tool with Test2.
.SH DESCRIPTION
.IX Header "DESCRIPTION"
This tutorial will help you write your very first tool by cloning the \f(CWok()\fR
tool.
.SH "COMPLETE CODE UP FRONT"
.IX Header "COMPLETE CODE UP FRONT"
.Vb 3
\&    package Test2::Tools::MyOk;
\&    use strict;
\&    use warnings;
\&
\&    use Test2::API qw/context/;
\&
\&    use base \*(AqExporter\*(Aq;
\&    our @EXPORT = qw/ok/;
\&
\&    sub ok($;$@) {
\&        my ($bool, $name, @diag) = @_;
\&
\&        my $ctx = context();
\&
\&        return $ctx\->pass_and_release($name) if $bool;
\&        return $ctx\->fail_and_release($name, @diag);
\&    }
\&
\&    1;
.Ve
.SH "LINE BY LINE"
.IX Header "LINE BY LINE"
.IP "sub ok($;$@) {" 4
.IX Item "sub ok($;$@) {"
In this case we are emulating the \f(CWok()\fR function exported by
Test2::Tools::Basic.
.Sp
\&\f(CWok()\fR 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.
.Sp
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.
.ie n .IP "my ($bool, $name, @diag) = @_;" 4
.el .IP "my ($bool, \f(CW$name\fR, \f(CW@diag\fR) = \f(CW@_\fR;" 4
.IX Item "my ($bool, $name, @diag) = @_;"
This line does not need much explanation, we are simply grabbing the args.
.ie n .IP "my $ctx = \fBcontext()\fR;" 4
.el .IP "my \f(CW$ctx\fR = \fBcontext()\fR;" 4
.IX Item "my $ctx = context();"
This is a vital line in \fBALL\fR tools. The context object is the primary API for
test tools. You \fBMUST\fR 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.
.Sp
\&\fBNote:\fR A test function \fBMUST\fR 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.
.Sp
\&\fBNote:\fR You \fBMUST NOT\fR ever store or pass around a \fIreal\fR context object. If
you wish to hold on to a context for any reason you must use clone to make a
copy \f(CW\*(C`my $copy = $ctx\->clone\*(C'\fR. The copy may be passed around or stored,
but the original \fBMUST\fR be released when you are done with it.
.ie n .IP "return $ctx\->pass_and_release($name) if $bool;" 4
.el .IP "return \f(CW$ctx\fR\->pass_and_release($name) if \f(CW$bool\fR;" 4
.IX Item "return $ctx->pass_and_release($name) if $bool;"
When \f(CW$bool\fR is true, this line uses the context object to issue a
Test2::Event::Pass event. Along with issuing the event this will also
release the context object and return true.
.Sp
This is short form for:
.Sp
.Vb 5
\&    if($bool) {
\&        $ctx\->pass($name);
\&        $ctx\->release;
\&        return 1;
\&    }
.Ve
.ie n .IP "return $ctx\->fail_and_release($name, @diag);" 4
.el .IP "return \f(CW$ctx\fR\->fail_and_release($name, \f(CW@diag\fR);" 4
.IX Item "return $ctx->fail_and_release($name, @diag);"
This line issues a Test2::Event::Fail event, releases the context object,
and returns false. The fail event will include any diagnostics messages from
the \f(CW@diag\fR array.
.Sp
This is short form for:
.Sp
.Vb 3
\&    $ctx\->fail($name, @diag);
\&    $ctx\->release;
\&    return 0;
.Ve
.SH "CONTEXT OBJECT DOCUMENTATION"
.IX Header "CONTEXT OBJECT DOCUMENTATION"
Test2::API::Context is the place to read up on what methods the context
provides.
.SH "SEE ALSO"
.IX Header "SEE ALSO"
Test2::Manual \- Primary index of the manual.
.SH SOURCE
.IX Header "SOURCE"
The source code repository for Test2\-Manual can be found at
\&\fIhttps://github.com/Test\-More/test\-more/\fR.
.SH MAINTAINERS
.IX Header "MAINTAINERS"
.IP "Chad Granum <exodist@cpan.org>" 4
.IX Item "Chad Granum <exodist@cpan.org>"
.SH AUTHORS
.IX Header "AUTHORS"
.IP "Chad Granum <exodist@cpan.org>" 4
.IX Item "Chad Granum <exodist@cpan.org>"
.SH COPYRIGHT
.IX Header "COPYRIGHT"
Copyright Chad Granum <exodist@cpan.org>.
.PP
This program is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.
.PP
See \fIhttp://dev.perl.org/licenses/\fR
