Debian Codegraph @

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