Debian Codegraph @

upstream/mageia-cauldron/man3pm/Test2::Manual::Tooling::FirstTool.3pm

raw
1 .\" -*- mode: troff; coding: utf-8 -*-
2 .\" Automatically generated by Pod::Man v6.0.2 (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 .\" Required to disable full justification in groff 1.23.0.
57 .if n .ds AD l
58 .\" ========================================================================
59 .\"
60 .IX Title "Test2::Manual::Tooling::FirstTool 3pm"
61 .TH Test2::Manual::Tooling::FirstTool 3pm 2025-06-24 "perl v5.42.0" "Perl Programmers Reference Guide"
62 .\" For nroff, turn off justification. Always turn off hyphenation; it makes
63 .\" way too many mistakes in technical documents.
64 .if n .ad l
65 .nh
66 .SH NAME
67 Test2::Manual::Tooling::FirstTool \- Write your first tool with Test2.
68 .SH DESCRIPTION
69 .IX Header "DESCRIPTION"
70 This tutorial will help you write your very first tool by cloning the \f(CWok()\fR
71 tool.
72 .SH "COMPLETE CODE UP FRONT"
73 .IX Header "COMPLETE CODE UP FRONT"
74 .Vb 3
75 \& package Test2::Tools::MyOk;
76 \& use strict;
77 \& use warnings;
78 \&
79 \& use Test2::API qw/context/;
80 \&
81 \& use base \*(AqExporter\*(Aq;
82 \& our @EXPORT = qw/ok/;
83 \&
84 \& sub ok($;$@) {
85 \& my ($bool, $name, @diag) = @_;
86 \&
87 \& my $ctx = context();
88 \&
89 \& return $ctx\->pass_and_release($name) if $bool;
90 \& return $ctx\->fail_and_release($name, @diag);
91 \& }
92 \&
93 \& 1;
94 .Ve
95 .SH "LINE BY LINE"
96 .IX Header "LINE BY LINE"
97 .IP "sub ok($;$@) {" 4
98 .IX Item "sub ok($;$@) {"
99 In this case we are emulating the \f(CWok()\fR function exported by
100 Test2::Tools::Basic.
101 .Sp
102 \&\f(CWok()\fR and similar test tools use prototypes to enforce argument parsing. Your
103 test tools do not necessarily need prototypes, like any perl function you need
104 to make the decision based on how it is used.
105 .Sp
106 The prototype requires at least 1 argument, which will
107 be forced into a scalar context. The second argument is optional, and is also
108 forced to be scalar, it is the name of the test. Any remaining arguments are
109 treated as diagnostics messages that will only be used if the test failed.
110 .ie n .IP "my ($bool, $name, @diag) = @_;" 4
111 .el .IP "my ($bool, \f(CW$name\fR, \f(CW@diag\fR) = \f(CW@_\fR;" 4
112 .IX Item "my ($bool, $name, @diag) = @_;"
113 This line does not need much explanation, we are simply grabbing the args.
114 .ie n .IP "my $ctx = \fBcontext()\fR;" 4
115 .el .IP "my \f(CW$ctx\fR = \fBcontext()\fR;" 4
116 .IX Item "my $ctx = context();"
117 This is a vital line in \fBALL\fR tools. The context object is the primary API for
118 test tools. You \fBMUST\fR get a context if you want to issue any events, such as
119 making assertions. Further, the context is responsible for making sure failures
120 are attributed to the correct file and line number.
121 .Sp
122 \&\fBNote:\fR A test function \fBMUST\fR always release the context when it is done,
123 you cannot simply let it fall out of scope and be garbage collected. Test2 does
124 a pretty good job of yelling at you if you make this mistake.
125 .Sp
126 \&\fBNote:\fR You \fBMUST NOT\fR ever store or pass around a \fIreal\fR context object. If
127 you wish to hold on to a context for any reason you must use clone to make a
128 copy \f(CW\*(C`my $copy = $ctx\->clone\*(C'\fR. The copy may be passed around or stored,
129 but the original \fBMUST\fR be released when you are done with it.
130 .ie n .IP "return $ctx\->pass_and_release($name) if $bool;" 4
131 .el .IP "return \f(CW$ctx\fR\->pass_and_release($name) if \f(CW$bool\fR;" 4
132 .IX Item "return $ctx->pass_and_release($name) if $bool;"
133 When \f(CW$bool\fR is true, this line uses the context object to issue a
134 Test2::Event::Pass event. Along with issuing the event this will also
135 release the context object and return true.
136 .Sp
137 This is short form for:
138 .Sp
139 .Vb 5
140 \& if($bool) {
141 \& $ctx\->pass($name);
142 \& $ctx\->release;
143 \& return 1;
144 \& }
145 .Ve
146 .ie n .IP "return $ctx\->fail_and_release($name, @diag);" 4
147 .el .IP "return \f(CW$ctx\fR\->fail_and_release($name, \f(CW@diag\fR);" 4
148 .IX Item "return $ctx->fail_and_release($name, @diag);"
149 This line issues a Test2::Event::Fail event, releases the context object,
150 and returns false. The fail event will include any diagnostics messages from
151 the \f(CW@diag\fR array.
152 .Sp
153 This is short form for:
154 .Sp
155 .Vb 3
156 \& $ctx\->fail($name, @diag);
157 \& $ctx\->release;
158 \& return 0;
159 .Ve
160 .SH "CONTEXT OBJECT DOCUMENTATION"
161 .IX Header "CONTEXT OBJECT DOCUMENTATION"
162 Test2::API::Context is the place to read up on what methods the context
163 provides.
164 .SH "SEE ALSO"
165 .IX Header "SEE ALSO"
166 Test2::Manual \- Primary index of the manual.
167 .SH SOURCE
168 .IX Header "SOURCE"
169 The source code repository for Test2\-Manual can be found at
170 \&\fIhttps://github.com/Test\-More/test\-more/\fR.
171 .SH MAINTAINERS
172 .IX Header "MAINTAINERS"
173 .IP "Chad Granum <[email protected]>" 4
174 .IX Item "Chad Granum <[email protected]>"
175 .SH AUTHORS
176 .IX Header "AUTHORS"
177 .IP "Chad Granum <[email protected]>" 4
178 .IX Item "Chad Granum <[email protected]>"
179 .SH COPYRIGHT
180 .IX Header "COPYRIGHT"
181 Copyright Chad Granum <[email protected]>.
182 .PP
183 This program is free software; you can redistribute it and/or
184 modify it under the same terms as Perl itself.
185 .PP
186 See \fIhttp://dev.perl.org/licenses/\fR