.\" Automatically generated by Pod::Man 4.07 (Pod::Simple 3.32) .\" .\" 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 .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . 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 .. .if !\nF .nr F 0 .if \nF>0 \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} .\} .\" ======================================================================== .\" .IX Title "Test2::Manual::Tooling::FirstTool 3pm" .TH Test2::Manual::Tooling::FirstTool 3pm "2019-05-06" "perl v5.24.1" "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(CW\*(C`ok()\*(C'\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(CW\*(C`ok()\*(C'\fR function exported by Test2::Tools::Basic. .Sp \&\f(CW\*(C`ok()\*(C'\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 = \fIcontext()\fR;" 4 .el .IP "my \f(CW$ctx\fR = \fIcontext()\fR;" 4 .IX Item "my $ctx = context();" This is a vital line in \fB\s-1ALL\s0\fR tools. The context object is the primary \s-1API\s0 for test tools. You \fB\s-1MUST\s0\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 \fB\s-1MUST\s0\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 \fB\s-1MUST NOT\s0\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 \fB\s-1MUST\s0\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/Test2\-Suite/\fR. .SH "MAINTAINERS" .IX Header "MAINTAINERS" .IP "Chad Granum " 4 .IX Item "Chad Granum " .SH "AUTHORS" .IX Header "AUTHORS" .PD 0 .IP "Chad Granum " 4 .IX Item "Chad Granum " .PD .SH "COPYRIGHT" .IX Header "COPYRIGHT" Copyright 2018 Chad Granum . .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