Debian Codegraph @

upstream/opensuse-tumbleweed/man3/Test2::Compare.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::Compare 3pm"
61 .TH Test2::Compare 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::Compare \- Test2 extension for writing deep comparison tools.
68 .SH DESCRIPTION
69 .IX Header "DESCRIPTION"
70 This library is the driving force behind deep comparison tools such as
71 \&\f(CWTest2::Tools::Compare::is()\fR and
72 \&\f(CWTest2::Tools::ClassicCompare::is_deeply()\fR.
73 .SH SYNOPSIS
74 .IX Header "SYNOPSIS"
75 .Vb 1
76 \& package Test2::Tools::MyCheck;
77 \&
78 \& use Test2::Compare::MyCheck;
79 \& use Test2::Compare qw/compare/;
80 \&
81 \& sub MyCheck {
82 \& my ($got, $exp, $name, @diag) = @_;
83 \& my $ctx = context();
84 \&
85 \& my $delta = compare($got, $exp, \e&convert);
86 \&
87 \& if ($delta) {
88 \& $ctx\->fail($name, $delta\->diag, @diag);
89 \& }
90 \& else {
91 \& $ctx\->ok(1, $name);
92 \& }
93 \&
94 \& $ctx\->release;
95 \& return !$delta;
96 \& }
97 \&
98 \& sub convert {
99 \& my $thing = shift;
100 \& return $thing if blessed($thing) && $thing\->isa(\*(AqTest2::Compare::MyCheck\*(Aq);
101 \&
102 \& return Test2::Compare::MyCheck\->new(stuff => $thing);
103 \& }
104 .Ve
105 .PP
106 See Test2::Compare::Base for details about writing a custom check.
107 .SH EXPORTS
108 .IX Header "EXPORTS"
109 .ie n .IP "$delta = compare($got, $expect, \e&convert)" 4
110 .el .IP "\f(CW$delta\fR = compare($got, \f(CW$expect\fR, \e&convert)" 4
111 .IX Item "$delta = compare($got, $expect, &convert)"
112 This will compare the structures in \f(CW$got\fR with those in \f(CW$expect\fR, The
113 convert sub should convert vanilla structures inside \f(CW$expect\fR into checks.
114 If there are differences in the structures they will be reported back as an
115 Test2::Compare::Delta tree.
116 .ie n .IP "$build = \fBget_build()\fR" 4
117 .el .IP "\f(CW$build\fR = \fBget_build()\fR" 4
118 .IX Item "$build = get_build()"
119 Get the current global build, if any.
120 .IP push_build($build) 4
121 .IX Item "push_build($build)"
122 Set the current global build.
123 .ie n .IP "$build = pop_build($build)" 4
124 .el .IP "\f(CW$build\fR = pop_build($build)" 4
125 .IX Item "$build = pop_build($build)"
126 Unset the current global build. This will throw an exception if the build
127 passed in is different from the current global.
128 .IP "build($class, sub { ... })" 4
129 .IX Item "build($class, sub { ... })"
130 Run the provided codeblock with a new instance of \f(CW$class\fR as the current
131 build. Returns the new build.
132 .ie n .IP "$check = convert($thing)" 4
133 .el .IP "\f(CW$check\fR = convert($thing)" 4
134 .IX Item "$check = convert($thing)"
135 .PD 0
136 .ie n .IP "$check = convert($thing, $config)" 4
137 .el .IP "\f(CW$check\fR = convert($thing, \f(CW$config\fR)" 4
138 .IX Item "$check = convert($thing, $config)"
139 .PD
140 This convert function is used by \f(CWstrict_convert()\fR and \f(CWrelaxed_convert()\fR
141 under the hood. It can also be used as the basis for other convert functions.
142 .Sp
143 If you want to use it with a custom configuration you should wrap it in another
144 sub like so:
145 .Sp
146 .Vb 7
147 \& sub my_convert {
148 \& my $thing_to_convert = shift;
149 \& return convert(
150 \& $thing_to_convert,
151 \& { ... }
152 \& );
153 \& }
154 .Ve
155 .Sp
156 Or the short variant:
157 .Sp
158 .Vb 1
159 \& sub my_convert { convert($_[0], { ... }) }
160 .Ve
161 .Sp
162 There are several configuration options, here they are with the default setting
163 listed first:
164 .RS 4
165 .IP "implicit_end => 1" 4
166 .IX Item "implicit_end => 1"
167 This option toggles array/hash boundaries. If this is true then no extra hash
168 keys or array indexes will be allowed. This setting affects generated compare
169 objects as well as any passed in.
170 .IP "use_regex => 1" 4
171 .IX Item "use_regex => 1"
172 This option toggles regex matching. When true (default) regexes are converted
173 to checks such that values must match the regex. When false regexes will be
174 compared to see if they are identical regexes.
175 .IP "use_code => 0" 4
176 .IX Item "use_code => 0"
177 This option toggles code matching. When false (default) coderefs in structures
178 must be the same coderef as specified. When true coderefs will be run to verify
179 the value being checked.
180 .RE
181 .RS 4
182 .RE
183 .ie n .IP "$check = strict_convert($thing)" 4
184 .el .IP "\f(CW$check\fR = strict_convert($thing)" 4
185 .IX Item "$check = strict_convert($thing)"
186 Convert \f(CW$thing\fR to an Test2::Compare::* object. This will behave strictly
187 which means it uses these settings:
188 .RS 4
189 .IP "implicit_end => 1" 4
190 .IX Item "implicit_end => 1"
191 Array bounds will be checked when this object is used in a comparison. No
192 unexpected hash keys can be present.
193 .IP "use_code => 0" 4
194 .IX Item "use_code => 0"
195 Sub references will be compared as refs (IE are these sub refs the same ref?)
196 .IP "use_regex => 0" 4
197 .IX Item "use_regex => 0"
198 Regexes will be compared directly (IE are the regexes the same?)
199 .RE
200 .RS 4
201 .RE
202 .ie n .IP "$compare = relaxed_convert($thing)" 4
203 .el .IP "\f(CW$compare\fR = relaxed_convert($thing)" 4
204 .IX Item "$compare = relaxed_convert($thing)"
205 Convert \f(CW$thing\fR to an Test2::Compare::* object. This will be relaxed which
206 means it uses these settings:
207 .RS 4
208 .IP "implicit_end => 0" 4
209 .IX Item "implicit_end => 0"
210 Array bounds will not be checked when this object is used in a comparison.
211 Unexpected hash keys can be present.
212 .IP "use_code => 1" 4
213 .IX Item "use_code => 1"
214 Sub references will be run to verify a value.
215 .IP "use_regex => 1" 4
216 .IX Item "use_regex => 1"
217 Values will be checked against any regexes provided.
218 .RE
219 .RS 4
220 .RE
221 .SH "WRITING A VARIANT OF IS/LIKE"
222 .IX Header "WRITING A VARIANT OF IS/LIKE"
223 .Vb 1
224 \& use Test2::Compare qw/compare convert/;
225 \&
226 \& sub my_like($$;$@) {
227 \& my ($got, $exp, $name, @diag) = @_;
228 \& my $ctx = context();
229 \&
230 \& # A custom converter that does the same thing as the one used by like()
231 \& my $convert = sub {
232 \& my $thing = shift;
233 \& return convert(
234 \& $thing,
235 \& {
236 \& implicit_end => 0,
237 \& use_code => 1,
238 \& use_regex => 1,
239 \& }
240 \& );
241 \& };
242 \&
243 \& my $delta = compare($got, $exp, $convert);
244 \&
245 \& if ($delta) {
246 \& $ctx\->fail($name, $delta\->diag, @diag);
247 \& }
248 \& else {
249 \& $ctx\->ok(1, $name);
250 \& }
251 \&
252 \& $ctx\->release;
253 \& return !$delta;
254 \& }
255 .Ve
256 .PP
257 The work of a comparison tool is done by 3 entities:
258 .IP \fBcompare()\fR 4
259 .IX Item "compare()"
260 The \f(CWcompare()\fR function takes the structure you got, the specification you
261 want to check against, and a \f(CW\*(C`\e&convert\*(C'\fR sub that will convert anything that
262 is not an instance of an Test2::Compare::Base subclass into one.
263 .Sp
264 This tool will use the \f(CW\*(C`\e&convert\*(C'\fR function on the specification, and then
265 produce an Test2::Compare::Delta structure that outlines all the ways the
266 structure you got deviates from the specification.
267 .IP \e&convert 4
268 .IX Item "&convert"
269 Converts anything that is not an instance of an Test2::Compare::Base
270 subclass, and turns it into one. The objects this produces are able to check
271 that a structure matches a specification.
272 .ie n .IP $delta 4
273 .el .IP \f(CW$delta\fR 4
274 .IX Item "$delta"
275 An instance of Test2::Compare::Delta is ultimately returned. This object
276 represents all the ways in with the structure you got deviated from the
277 specification. The delta is a tree and may contain child deltas for nested
278 structures.
279 .Sp
280 The delta is capable of rendering itself as a table, use \f(CW\*(C`@lines =
281 $delta\->diag\*(C'\fR to get the table (lines in \f(CW@lines\fR will not be terminated
282 with \f(CW"\en"\fR).
283 .PP
284 The \f(CWconvert()\fR function provided by this package contains all the
285 specification behavior of \f(CWlike()\fR and \f(CWis()\fR. It is intended to be wrapped
286 in a sub that passes in a configuration hash, which allows you to control the
287 behavior.
288 .PP
289 You are free to write your own \f(CW\*(C`$check = compare($thing)\*(C'\fR function, it just
290 needs to accept a single argument, and produce a single instance of an
291 Test2::Compare::Base subclass.
292 .SH SOURCE
293 .IX Header "SOURCE"
294 The source code repository for Test2\-Suite can be found at
295 \&\fIhttps://github.com/Test\-More/test\-more/\fR.
296 .SH MAINTAINERS
297 .IX Header "MAINTAINERS"
298 .IP "Chad Granum <[email protected]>" 4
299 .IX Item "Chad Granum <[email protected]>"
300 .SH AUTHORS
301 .IX Header "AUTHORS"
302 .IP "Chad Granum <[email protected]>" 4
303 .IX Item "Chad Granum <[email protected]>"
304 .SH COPYRIGHT
305 .IX Header "COPYRIGHT"
306 Copyright Chad Granum <[email protected]>.
307 .PP
308 This program is free software; you can redistribute it and/or
309 modify it under the same terms as Perl itself.
310 .PP
311 See \fIhttp://dev.perl.org/licenses/\fR