Debian Codegraph @

upstream/debian-unstable/man3/Test2::Manual::Tooling::FirstTool.3perl

raw
1 .\" -*- mode: troff; coding: utf-8 -*-
2 .\" Automatically generated by Pod::Man 5.0102 (Pod::Simple 3.45)
3 .\"
4 .\" Standard preamble:
5 .\" ========================================================================
6 .de Sp \" Vertical space (when we can't use .PP)
7 .if t .sp .5v
8 .if n .sp
9 ..
10 .de Vb \" Begin verbatim text
11 .ft CW
12 .nf
13 .ne \\$1
14 ..
15 .de Ve \" End verbatim text
16 .ft R
17 .fi
18 ..
19 .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>.
20 .ie n \{\
21 . ds C` ""
22 . ds C' ""
23 'br\}
24 .el\{\
25 . ds C`
26 . ds C'
27 'br\}
28 .\"
29 .\" Escape single quotes in literal strings from groff's Unicode transform.
30 .ie \n(.g .ds Aq \(aq
31 .el .ds Aq '
32 .\"
33 .\" If the F register is >0, we'll generate index entries on stderr for
34 .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
35 .\" entries marked with X<> in POD. Of course, you'll have to process the
36 .\" output yourself in some meaningful fashion.
37 .\"
38 .\" Avoid warning from groff about undefined register 'F'.
39 .de IX
40 ..
41 .nr rF 0
42 .if \n(.g .if rF .nr rF 1
43 .if (\n(rF:(\n(.g==0)) \{\
44 . if \nF \{\
45 . de IX
46 . tm Index:\\$1\t\\n%\t"\\$2"
47 ..
48 . if !\nF==2 \{\
49 . nr % 0
50 . nr F 2
51 . \}
52 . \}
53 .\}
54 .rr rF
55 .\" ========================================================================
56 .\"
57 .IX Title "Test2::Manual::Tooling::FirstTool 3perl"
58 .TH Test2::Manual::Tooling::FirstTool 3perl 2025-11-16 "perl v5.40.1" "Perl Programmers Reference Guide"
59 .\" For nroff, turn off justification. Always turn off hyphenation; it makes
60 .\" way too many mistakes in technical documents.
61 .if n .ad l
62 .nh
63 .SH NAME
64 Test2::Manual::Tooling::FirstTool \- Write your first tool with Test2.
65 .SH DESCRIPTION
66 .IX Header "DESCRIPTION"
67 This tutorial will help you write your very first tool by cloning the \f(CWok()\fR
68 tool.
69 .SH "COMPLETE CODE UP FRONT"
70 .IX Header "COMPLETE CODE UP FRONT"
71 .Vb 3
72 \& package Test2::Tools::MyOk;
73 \& use strict;
74 \& use warnings;
75 \&
76 \& use Test2::API qw/context/;
77 \&
78 \& use base \*(AqExporter\*(Aq;
79 \& our @EXPORT = qw/ok/;
80 \&
81 \& sub ok($;$@) {
82 \& my ($bool, $name, @diag) = @_;
83 \&
84 \& my $ctx = context();
85 \&
86 \& return $ctx\->pass_and_release($name) if $bool;
87 \& return $ctx\->fail_and_release($name, @diag);
88 \& }
89 \&
90 \& 1;
91 .Ve
92 .SH "LINE BY LINE"
93 .IX Header "LINE BY LINE"
94 .IP "sub ok($;$@) {" 4
95 .IX Item "sub ok($;$@) {"
96 In this case we are emulating the \f(CWok()\fR function exported by
97 Test2::Tools::Basic.
98 .Sp
99 \&\f(CWok()\fR and similar test tools use prototypes to enforce argument parsing. Your
100 test tools do not necessarily need prototypes, like any perl function you need
101 to make the decision based on how it is used.
102 .Sp
103 The prototype requires at least 1 argument, which will
104 be forced into a scalar context. The second argument is optional, and is also
105 forced to be scalar, it is the name of the test. Any remaining arguments are
106 treated as diagnostics messages that will only be used if the test failed.
107 .ie n .IP "my ($bool, $name, @diag) = @_;" 4
108 .el .IP "my ($bool, \f(CW$name\fR, \f(CW@diag\fR) = \f(CW@_\fR;" 4
109 .IX Item "my ($bool, $name, @diag) = @_;"
110 This line does not need much explanation, we are simply grabbing the args.
111 .ie n .IP "my $ctx = \fBcontext()\fR;" 4
112 .el .IP "my \f(CW$ctx\fR = \fBcontext()\fR;" 4
113 .IX Item "my $ctx = context();"
114 This is a vital line in \fBALL\fR tools. The context object is the primary API for
115 test tools. You \fBMUST\fR get a context if you want to issue any events, such as
116 making assertions. Further, the context is responsible for making sure failures
117 are attributed to the correct file and line number.
118 .Sp
119 \&\fBNote:\fR A test function \fBMUST\fR always release the context when it is done,
120 you cannot simply let it fall out of scope and be garbage collected. Test2 does
121 a pretty good job of yelling at you if you make this mistake.
122 .Sp
123 \&\fBNote:\fR You \fBMUST NOT\fR ever store or pass around a \fIreal\fR context object. If
124 you wish to hold on to a context for any reason you must use clone to make a
125 copy \f(CW\*(C`my $copy = $ctx\->clone\*(C'\fR. The copy may be passed around or stored,
126 but the original \fBMUST\fR be released when you are done with it.
127 .ie n .IP "return $ctx\->pass_and_release($name) if $bool;" 4
128 .el .IP "return \f(CW$ctx\fR\->pass_and_release($name) if \f(CW$bool\fR;" 4
129 .IX Item "return $ctx->pass_and_release($name) if $bool;"
130 When \f(CW$bool\fR is true, this line uses the context object to issue a
131 Test2::Event::Pass event. Along with issuing the event this will also
132 release the context object and return true.
133 .Sp
134 This is short form for:
135 .Sp
136 .Vb 5
137 \& if($bool) {
138 \& $ctx\->pass($name);
139 \& $ctx\->release;
140 \& return 1;
141 \& }
142 .Ve
143 .ie n .IP "return $ctx\->fail_and_release($name, @diag);" 4
144 .el .IP "return \f(CW$ctx\fR\->fail_and_release($name, \f(CW@diag\fR);" 4
145 .IX Item "return $ctx->fail_and_release($name, @diag);"
146 This line issues a Test2::Event::Fail event, releases the context object,
147 and returns false. The fail event will include any diagnostics messages from
148 the \f(CW@diag\fR array.
149 .Sp
150 This is short form for:
151 .Sp
152 .Vb 3
153 \& $ctx\->fail($name, @diag);
154 \& $ctx\->release;
155 \& return 0;
156 .Ve
157 .SH "CONTEXT OBJECT DOCUMENTATION"
158 .IX Header "CONTEXT OBJECT DOCUMENTATION"
159 Test2::API::Context is the place to read up on what methods the context
160 provides.
161 .SH "SEE ALSO"
162 .IX Header "SEE ALSO"
163 Test2::Manual \- Primary index of the manual.
164 .SH SOURCE
165 .IX Header "SOURCE"
166 The source code repository for Test2\-Manual can be found at
167 \&\fIhttps://github.com/Test\-More/Test2\-Suite/\fR.
168 .SH MAINTAINERS
169 .IX Header "MAINTAINERS"
170 .IP "Chad Granum <[email protected]>" 4
171 .IX Item "Chad Granum <[email protected]>"
172 .SH AUTHORS
173 .IX Header "AUTHORS"
174 .PD 0
175 .IP "Chad Granum <[email protected]>" 4
176 .IX Item "Chad Granum <[email protected]>"
177 .PD
178 .SH COPYRIGHT
179 .IX Header "COPYRIGHT"
180 Copyright 2018 Chad Granum <[email protected]>.
181 .PP
182 This program is free software; you can redistribute it and/or
183 modify it under the same terms as Perl itself.
184 .PP
185 See \fIhttp://dev.perl.org/licenses/\fR