```{index} single: Test2::Compare ``` # Test2::Compare Test2 extension for writing deep comparison tools. ## DESCRIPTION This library is the driving force behind deep comparison tools such as `Test2::Tools::Compare::is()` and `Test2::Tools::ClassicCompare::is_deeply()`. ## SYNOPSIS ```perl 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, \&convert); if ($delta) { $ctx->fail($name, $delta->diag, @diag); } else { $ctx->ok(1, $name); } $ctx->release; return !$delta; } sub convert { my $thing = shift; return $thing if blessed($thing) && $thing->isa('Test2::Compare::MyCheck'); return Test2::Compare::MyCheck->new(stuff => $thing); } ``` See [Test2::Compare::Base](Compare/Base.md) for details about writing a custom check. ## EXPORTS - $delta = compare($got, $expect, \\&convert) This will compare the structures in `$got` with those in `$expect`, The convert sub should convert vanilla structures inside `$expect` into checks. If there are differences in the structures they will be reported back as an [Test2::Compare::Delta](Compare/Delta.md) tree. - $build = get\_build() Get the current global build, if any. - push\_build($build) Set the current global build. - $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. - build($class, sub { ... }) Run the provided codeblock with a new instance of `$class` as the current build. Returns the new build. - $check = convert($thing) - $check = convert($thing, $config) This convert function is used by `strict_convert()` and `relaxed_convert()` under the hood. It can also be used as the basis for other convert functions. If you want to use it with a custom configuration you should wrap it in another sub like so: ```perl sub my_convert { my $thing_to_convert = shift; return convert( $thing_to_convert, { ... } ); } ``` Or the short variant: ```perl sub my_convert { convert($_[0], { ... }) } ``` There are several configuration options, here they are with the default setting listed first: - 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 affects generated compare objects as well as any passed in. - 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. - 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. - $check = strict\_convert($thing) Convert `$thing` to an [Test2::Compare::\*](Compare/Base.md) object. This will behave strictly which means it uses these settings: - implicit\_end => 1 Array bounds will be checked when this object is used in a comparison. No unexpected hash keys can be present. - use\_code => 0 Sub references will be compared as refs (IE are these sub refs the same ref?) - use\_regex => 0 Regexes will be compared directly (IE are the regexes the same?) - $compare = relaxed\_convert($thing) Convert `$thing` to an [Test2::Compare::\*](Compare/Base.md) object. This will be relaxed which means it uses these settings: - implicit\_end => 0 Array bounds will not be checked when this object is used in a comparison. Unexpected hash keys can be present. - use\_code => 1 Sub references will be run to verify a value. - use\_regex => 1 Values will be checked against any regexes provided. ## WRITING A VARIANT OF IS/LIKE ```perl 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->fail($name, $delta->diag, @diag); } else { $ctx->ok(1, $name); } $ctx->release; return !$delta; } ``` The work of a comparison tool is done by 3 entities: - compare() The `compare()` function takes the structure you got, the specification you want to check against, and a `\&convert` sub that will convert anything that is not an instance of an [Test2::Compare::Base](Compare/Base.md) subclass into one. This tool will use the `\&convert` function on the specification, and then produce an [Test2::Compare::Delta](Compare/Delta.md) structure that outlines all the ways the structure you got deviates from the specification. - \\&convert Converts anything that is not an instance of an [Test2::Compare::Base](Compare/Base.md) subclass, and turns it into one. The objects this produces are able to check that a structure matches a specification. - $delta An instance of [Test2::Compare::Delta](Compare/Delta.md) 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. The delta is capable of rendering itself as a table, use `@lines = $delta->diag` to get the table (lines in `@lines` will not be terminated with `"\n"`). The `convert()` function provided by this package contains all the specification behavior of `like()` and `is()`. It is intended to be wrapped in a sub that passes in a configuration hash, which allows you to control the behavior. You are free to write your own `$check = compare($thing)` function, it just needs to accept a single argument, and produce a single instance of an [Test2::Compare::Base](Compare/Base.md) subclass. ## SOURCE The source code repository for Test2-Suite can be found at `https://github.com/Test-More/test-more/`. ## MAINTAINERS - Chad Granum ## AUTHORS - Chad Granum ## COPYRIGHT Copyright Chad Granum . This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See `http://dev.perl.org/licenses/` ## Modules - [`Test2::Compare::Array`](Compare/Array.md) - Internal representation of an array comparison. - [`Test2::Compare::Bag`](Compare/Bag.md) - Internal representation of a bag comparison. - [`Test2::Compare::Base`](Compare/Base.md) - Base class for comparison classes. - [`Test2::Compare::Bool`](Compare/Bool.md) - Compare two values as booleans - [`Test2::Compare::Custom`](Compare/Custom.md) - Custom field check for comparisons. - [`Test2::Compare::DeepRef`](Compare/DeepRef.md) - Ref comparison - [`Test2::Compare::Delta`](Compare/Delta.md) - Representation of differences between nested data structures. - [`Test2::Compare::Event`](Compare/Event.md) - Event specific Object subclass. - [`Test2::Compare::EventMeta`](Compare/EventMeta.md) - Meta class for events in deep comparisons - [`Test2::Compare::Float`](Compare/Float.md) - Compare two values as numbers with tolerance. - [`Test2::Compare::Hash`](Compare/Hash.md) - Representation of a hash in a deep comparison. - [`Test2::Compare::Isa`](Compare/Isa.md) - Check if the value is an instance of the class. - [`Test2::Compare::Meta`](Compare/Meta.md) - Check library for meta-checks - [`Test2::Compare::Negatable`](Compare/Negatable.md) - Poor mans 'role' for compare objects that can be negated. - [`Test2::Compare::Number`](Compare/Number.md) - Compare two values as numbers - [`Test2::Compare::Object`](Compare/Object.md) - Representation of an object during deep comparison. - [`Test2::Compare::OrderedSubset`](Compare/OrderedSubset.md) - Internal representation of an ordered subset. - [`Test2::Compare::Pattern`](Compare/Pattern.md) - Use a pattern to validate values in a deep comparison. - [`Test2::Compare::Ref`](Compare/Ref.md) - Ref comparison - [`Test2::Compare::Regex`](Compare/Regex.md) - Regex direct comparison - [`Test2::Compare::Scalar`](Compare/Scalar.md) - Representation of a Scalar Ref in deep comparisons - [`Test2::Compare::Set`](Compare/Set.md) - Allows a field to be matched against a set of checks. - [`Test2::Compare::String`](Compare/String.md) - Compare two values as strings - [`Test2::Compare::Undef`](Compare/Undef.md) - Check that something is undefined - [`Test2::Compare::Wildcard`](Compare/Wildcard.md) - Placeholder check. ```{toctree} :hidden: :maxdepth: 1 Compare/Array Compare/Bag Compare/Base Compare/Bool Compare/Custom Compare/DeepRef Compare/Delta Compare/Event Compare/EventMeta Compare/Float Compare/Hash Compare/Isa Compare/Meta Compare/Negatable Compare/Number Compare/Object Compare/OrderedSubset Compare/Pattern Compare/Ref Compare/Regex Compare/Scalar Compare/Set Compare/String Compare/Undef Compare/Wildcard ```