.\" Automatically generated by Pod::Man 4.07 (Pod::Simple 3.32) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .if !\nF .nr F 0 .if \nF>0 \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} .\} .\" ======================================================================== .\" .IX Title "Test2::Compare 3pm" .TH Test2::Compare 3pm "2019-05-06" "perl v5.24.1" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Test2::Compare \- Test2 extension for writing deep comparison tools. .SH "DESCRIPTION" .IX Header "DESCRIPTION" This library is the driving force behind deep comparison tools such as \&\f(CW\*(C`Test2::Tools::Compare::is()\*(C'\fR and \&\f(CW\*(C`Test2::Tools::ClassicCompare::is_deeply()\*(C'\fR. .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 1 \& package Test2::Tools::MyCheck; \& \& use Test2::Compare::MyCheck; \& use Test2::Compare qw/compare/; \& \& sub MyCheck { \& my ($got, $exp, $name, @diag) = @_; \& my $ctx = context(); \& \& my $delta = compare($got, $exp, \e&convert); \& \& if ($delta) { \& $ctx\->ok(0, $name, [$delta\->table, @diag]); \& } \& else { \& $ctx\->ok(1, $name); \& } \& \& $ctx\->release; \& return !$delta; \& } \& \& sub convert { \& my $thing = shift; \& return $thing if blessed($thing) && $thing\->isa(\*(AqTest2::Compare::MyCheck\*(Aq); \& \& return Test2::Compare::MyCheck\->new(stuff => $thing); \& } .Ve .PP See Test2::Compare::Base for details about writing a custom check. .SH "EXPORTS" .IX Header "EXPORTS" .ie n .IP "$delta = compare($got, $expect, \e&convert)" 4 .el .IP "\f(CW$delta\fR = compare($got, \f(CW$expect\fR, \e&convert)" 4 .IX Item "$delta = compare($got, $expect, &convert)" This will compare the structures in \f(CW$got\fR with those in \f(CW$expect\fR, The convert sub should convert vanilla structures inside \f(CW$expect\fR into checks. If there are differences in the structures they will be reported back as an Test2::Compare::Delta tree. .ie n .IP "$build = \fIget_build()\fR" 4 .el .IP "\f(CW$build\fR = \fIget_build()\fR" 4 .IX Item "$build = get_build()" Get the current global build, if any. .IP "push_build($build)" 4 .IX Item "push_build($build)" Set the current global build. .ie n .IP "$build = pop_build($build)" 4 .el .IP "\f(CW$build\fR = pop_build($build)" 4 .IX Item "$build = pop_build($build)" Unset the current global build. This will throw an exception if the build passed in is different from the current global. .IP "build($class, sub { ... })" 4 .IX Item "build($class, sub { ... })" Run the provided codeblock with a new instance of \f(CW$class\fR as the current build. Returns the new build. .ie n .IP "$check = convert($thing)" 4 .el .IP "\f(CW$check\fR = convert($thing)" 4 .IX Item "$check = convert($thing)" .PD 0 .ie n .IP "$check = convert($thing, $config)" 4 .el .IP "\f(CW$check\fR = convert($thing, \f(CW$config\fR)" 4 .IX Item "$check = convert($thing, $config)" .PD This convert function is used by \f(CW\*(C`strict_convert()\*(C'\fR and \f(CW\*(C`relaxed_convert()\*(C'\fR under the hood. It can also be used as the basis for other convert functions. .Sp If you want to use it with a custom configuration you should wrap it in another sub like so: .Sp .Vb 7 \& sub my_convert { \& my $thing_to_convert = shift; \& return convert( \& $thing_to_convert, \& { ... } \& ); \& } .Ve .Sp Or the short variant: .Sp .Vb 1 \& sub my_convert { convert($_[0], { ... }) } .Ve .Sp There are several configuration options, here they are with the default setting listed first: .RS 4 .IP "implicit_end => 1" 4 .IX Item "implicit_end => 1" This option toggles array/hash boundaries. If this is true then no extra hash keys or array indexes will be allowed. This setting effects generated compare objects as well as any passed in. .IP "use_regex => 1" 4 .IX Item "use_regex => 1" This option toggles regex matching. When true (default) regexes are converted to checks such that values must match the regex. When false regexes will be compared to see if they are identical regexes. .IP "use_code => 0" 4 .IX Item "use_code => 0" This option toggles code matching. When false (default) coderefs in structures must be the same coderef as specified. When true coderefs will be run to verify the value being checked. .RE .RS 4 .RE .ie n .IP "$check = strict_convert($thing)" 4 .el .IP "\f(CW$check\fR = strict_convert($thing)" 4 .IX Item "$check = strict_convert($thing)" Convert \f(CW$thing\fR to an Test2::Compare::* object. This will behave strictly which means it uses these settings: .RS 4 .IP "implicit_end => 1" 4 .IX Item "implicit_end => 1" Array bounds will be checked when this object is used in a comparison. No unexpected hash keys can be present. .IP "use_code => 0" 4 .IX Item "use_code => 0" Sub references will be compared as refs (\s-1IE\s0 are these sub refs the same ref?) .IP "use_regex => 0" 4 .IX Item "use_regex => 0" Regexes will be compared directly (\s-1IE\s0 are the regexes the same?) .RE .RS 4 .RE .ie n .IP "$compare = relaxed_convert($thing)" 4 .el .IP "\f(CW$compare\fR = relaxed_convert($thing)" 4 .IX Item "$compare = relaxed_convert($thing)" Convert \f(CW$thing\fR to an Test2::Compare::* object. This will be relaxed which means it uses these settings: .RS 4 .IP "implicit_end => 0" 4 .IX Item "implicit_end => 0" Array bounds will not be checked when this object is used in a comparison. Unexpected hash keys can be present. .IP "use_code => 1" 4 .IX Item "use_code => 1" Sub references will be run to verify a value. .IP "use_regex => 1" 4 .IX Item "use_regex => 1" Values will be checked against any regexes provided. .RE .RS 4 .RE .SH "WRITING A VARIANT OF IS/LIKE" .IX Header "WRITING A VARIANT OF IS/LIKE" .Vb 1 \& use Test2::Compare qw/compare convert/; \& \& sub my_like($$;$@) { \& my ($got, $exp, $name, @diag) = @_; \& my $ctx = context(); \& \& # A custom converter that does the same thing as the one used by like() \& my $convert = sub { \& my $thing = shift; \& return convert( \& $thing, \& { \& implicit_end => 0, \& use_code => 1, \& use_regex => 1, \& } \& ); \& }; \& \& my $delta = compare($got, $exp, $convert); \& \& if ($delta) { \& $ctx\->ok(0, $name, [$delta\->diag, @diag]); \& } \& else { \& $ctx\->ok(1, $name); \& } \& \& $ctx\->release; \& return !$delta; \& } .Ve .PP The work of a comparison tool is done by 3 entities: .IP "\fIcompare()\fR" 4 .IX Item "compare()" The \f(CW\*(C`compare()\*(C'\fR function takes the structure you got, the specification you want to check against, and a \f(CW\*(C`\e&convert\*(C'\fR sub that will convert anything that is not an instance of an Test2::Compare::Base subclass into one. .Sp This tool will use the \f(CW\*(C`\e&convert\*(C'\fR function on the specification, and then produce an Test2::Compare::Delta structure that outlines all the ways the structure you got deviates from the specification. .IP "\e&convert" 4 .IX Item "&convert" Converts anything that is not an instance of an Test2::Compare::Base subclass, and turns it into one. The objects this produces are able to check that a structure matches a specification. .ie n .IP "$delta" 4 .el .IP "\f(CW$delta\fR" 4 .IX Item "$delta" An instance of Test2::Compare::Delta is ultimately returned. This object represents all the ways in with the structure you got deviated from the specification. The delta is a tree and may contain child deltas for nested structures. .Sp The delta is capable of rendering itself as a table, use \f(CW\*(C`@lines = $delta\->diag\*(C'\fR to get the table (lines in \f(CW@lines\fR will not be terminated with \f(CW"\en"\fR). .PP The \f(CW\*(C`convert()\*(C'\fR function provided by this package contains all the specification behavior of \f(CW\*(C`like()\*(C'\fR and \f(CW\*(C`is()\*(C'\fR. It is intended to be wrapped in a sub that passes in a configuration hash, which allows you to control the behavior. .PP You are free to write your own \f(CW\*(C`$check = compare($thing)\*(C'\fR function, it just needs to accept a single argument, and produce a single instance of an Test2::Compare::Base subclass. .SH "SOURCE" .IX Header "SOURCE" The source code repository for Test2\-Suite can be found at \&\fIhttps://github.com/Test\-More/Test2\-Suite/\fR. .SH "MAINTAINERS" .IX Header "MAINTAINERS" .IP "Chad Granum " 4 .IX Item "Chad Granum " .SH "AUTHORS" .IX Header "AUTHORS" .PD 0 .IP "Chad Granum " 4 .IX Item "Chad Granum " .PD .SH "COPYRIGHT" .IX Header "COPYRIGHT" Copyright 2018 Chad Granum . .PP This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. .PP See \fIhttp://dev.perl.org/licenses/\fR