Debian Codegraph @

upstream/debian-unstable/man3/Test2::Manual::Tooling::Nesting.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::Nesting 3perl"
58 .TH Test2::Manual::Tooling::Nesting 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::Nesting \- Tutorial for using other tools within your
65 own.
66 .SH DESCRIPTION
67 .IX Header "DESCRIPTION"
68 Sometimes you find yourself writing the same test pattern over and over, in
69 such cases you may want to encapsulate the logic in a new test function that
70 calls several tools together. This sounds easy enough, but can cause headaches
71 if not done correctly.
72 .SH "NAIVE WAY"
73 .IX Header "NAIVE WAY"
74 Lets say you find yourself writing the same test pattern over and over for multiple objects:
75 .PP
76 .Vb 3
77 \& my $obj1 = $class1\->new;
78 \& is($obj1\->foo, \*(Aqfoo\*(Aq, "got foo");
79 \& is($obj1\->bar, \*(Aqbar\*(Aq, "got bar");
80 \&
81 \& my $obj2 = $class1\->new;
82 \& is($obj2\->foo, \*(Aqfoo\*(Aq, "got foo");
83 \& is($obj2\->bar, \*(Aqbar\*(Aq, "got bar");
84 \&
85 \& ... 10x more times for classes 2\-12
86 .Ve
87 .PP
88 The naive way to do this is to write a \f(CWcheck_class()\fR function like this:
89 .PP
90 .Vb 6
91 \& sub check_class {
92 \& my $class = shift;
93 \& my $obj = $class\->new;
94 \& is($obj\->foo, \*(Aqfoo\*(Aq, "got foo");
95 \& is($obj\->bar, \*(Aqbar\*(Aq, "got bar");
96 \& }
97 \&
98 \& check_class($class1);
99 \& check_class($class2);
100 \& check_class($class3);
101 \& ...
102 .Ve
103 .PP
104 This will appear to work fine, and you might not notice any problems,
105 \&\fIso long as the tests are passing.\fR
106 .SS "WHATS WRONG WITH IT?"
107 .IX Subsection "WHATS WRONG WITH IT?"
108 The problems with the naive approach become obvious if things start to fail.
109 The diagnostics that tell you what file and line the failure occurred on will be
110 wrong. The failure will be reported to the line \fIinside\fR \f(CW\*(C`check_class\*(C'\fR, not
111 to the line where \f(CWcheck_class()\fR was called. This is problem because it
112 leaves you with no idea which class is failing.
113 .SS "HOW TO FIX IT"
114 .IX Subsection "HOW TO FIX IT"
115 Luckily this is extremely easy to fix. You need to acquire a context object at
116 the start of your function, and release it at the end... yes it is that simple.
117 .PP
118 .Vb 1
119 \& use Test2::API qw/context/;
120 \&
121 \& sub check_class {
122 \& my $class = shift;
123 \&
124 \& my $ctx = context();
125 \&
126 \& my $obj = $class\->new;
127 \& is($obj\->foo, \*(Aqfoo\*(Aq, "got foo");
128 \& is($obj\->bar, \*(Aqbar\*(Aq, "got bar");
129 \&
130 \& $ctx\->release;
131 \& }
132 .Ve
133 .PP
134 See, that was easy. With these 2 additional lines we know have proper file+line
135 reporting. The nested tools will find the context we acquired here, and know to
136 use it's file and line numbers.
137 .PP
138 \fITHE OLD WAY (DO NOT DO THIS ANYMORE)\fR
139 .IX Subsection "THE OLD WAY (DO NOT DO THIS ANYMORE)"
140 .PP
141 With Test::Builder there was a global variables called
142 \&\f(CW$Test::Builder::Level\fR which helped solve this problem:
143 .PP
144 .Vb 2
145 \& sub check_class {
146 \& my $class = shift;
147 \&
148 \& local $Test::Builder::Level = $Test::Builder::Level + 1;
149 \&
150 \& my $obj = $class\->new;
151 \& is($obj\->foo, \*(Aqfoo\*(Aq, "got foo");
152 \& is($obj\->bar, \*(Aqbar\*(Aq, "got bar");
153 \& }
154 .Ve
155 .PP
156 This variable worked well enough (and will still work) but was not very
157 discoverable. Another problem with this variable is that it becomes cumbersome
158 if you have a more deeply nested code structure called the nested tools, you
159 might need to count stack frames, and hope they never change due to a third
160 party module. The context solution has no such caveats.
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