Debian Codegraph @

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