<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package Test2::Compare;
use strict;
use warnings;

our $VERSION = '0.000162';

use Scalar::Util qw/blessed/;
use Test2::Util qw/try/;
use Test2::Util::Ref qw/rtype/;

use Carp qw/croak/;

our @EXPORT_OK = qw{
    compare
    get_build push_build pop_build build
    strict_convert relaxed_convert convert
};
use base 'Exporter';

sub compare {
    my ($got, $check, $convert) = @_;

    $check = $convert-&gt;($check);

    return $check-&gt;run(
        id      =&gt; undef,
        got     =&gt; $got,
        exists  =&gt; 1,
        convert =&gt; $convert,
        seen    =&gt; {},
    );
}

my @BUILD;

sub get_build  { @BUILD ? $BUILD[-1] : undef }
sub push_build { push @BUILD =&gt; $_[0] }

sub pop_build {
    return pop @BUILD if @BUILD &amp;&amp; $_[0] &amp;&amp; $BUILD[-1] == $_[0];
    my $have = @BUILD ? "$BUILD[-1]" : 'undef';
    my $want = $_[0]  ? "$_[0]"      : 'undef';
    croak "INTERNAL ERROR: Attempted to pop incorrect build, have $have, tried to pop $want";
}

sub build {
    my ($class, $code) = @_;

    my @caller = caller(1);

    die "'$caller[3]\()' should not be called in void context in $caller[1] line $caller[2]\n"
        unless defined(wantarray);

    my $build = $class-&gt;new(builder =&gt; $code, called =&gt; \@caller);

    push @BUILD =&gt; $build;
    my ($ok, $err) = try { $code-&gt;($build); 1 };
    pop @BUILD;
    die $err unless $ok;

    return $build;
}

sub strict_convert  { convert($_[0], { implicit_end =&gt; 1, use_regex =&gt; 0, use_code =&gt; 0 }) }
sub relaxed_convert { convert($_[0], { implicit_end =&gt; 0, use_regex =&gt; 1, use_code =&gt; 1 }) }

my $CONVERT_LOADED = 0;
my %ALLOWED_KEYS = ( implicit_end =&gt; 1, use_regex =&gt; 1, use_code =&gt; 1 );
sub convert {
    my ($thing, $config) = @_;

    unless($CONVERT_LOADED) {
        require Test2::Compare::Array;
        require Test2::Compare::Base;
        require Test2::Compare::Custom;
        require Test2::Compare::DeepRef;
        require Test2::Compare::Hash;
        require Test2::Compare::Pattern;
        require Test2::Compare::Ref;
        require Test2::Compare::Regex;
        require Test2::Compare::Scalar;
        require Test2::Compare::String;
        require Test2::Compare::Undef;
        require Test2::Compare::Wildcard;
        $CONVERT_LOADED = 1;
    }

    if (ref($config)) {
        my $bad = join ', ' =&gt; grep { !$ALLOWED_KEYS{$_} } keys %$config;
        croak "The following config options are not understood by convert(): $bad" if $bad;
        $config-&gt;{implicit_end} = 1 unless defined $config-&gt;{implicit_end};
        $config-&gt;{use_regex}    = 1 unless defined $config-&gt;{use_regex};
        $config-&gt;{use_code}     = 0 unless defined $config-&gt;{use_code};
    }
    else { # Legacy...
        if ($config) {
            $config = {
                implicit_end =&gt; 1,
                use_regex  =&gt; 0,
                use_code   =&gt; 0,
            };
        }
        else {
            $config = {
                implicit_end =&gt; 0,
                use_regex  =&gt; 1,
                use_code   =&gt; 1,
            };
        }
    }

    return _convert($thing, $config);
}

sub _convert {
    my ($thing, $config) = @_;

    return Test2::Compare::Undef-&gt;new()
        unless defined $thing;

    if (blessed($thing) &amp;&amp; $thing-&gt;isa('Test2::Compare::Base')) {
        if ($config-&gt;{implicit_end} &amp;&amp; $thing-&gt;can('set_ending') &amp;&amp; !defined $thing-&gt;ending) {
            my $clone = $thing-&gt;clone;
            $clone-&gt;set_ending('implicit');
            return $clone;
        }

        return $thing unless $thing-&gt;isa('Test2::Compare::Wildcard');
        my $newthing = _convert($thing-&gt;expect, $config);
        $newthing-&gt;set_builder($thing-&gt;builder) unless $newthing-&gt;builder;
        $newthing-&gt;set_file($thing-&gt;_file)      unless $newthing-&gt;_file;
        $newthing-&gt;set_lines($thing-&gt;_lines)    unless $newthing-&gt;_lines;
        return $newthing;
    }

    my $type = rtype($thing);

    return Test2::Compare::Array-&gt;new(inref =&gt; $thing, $config-&gt;{implicit_end} ? (ending =&gt; 1) : ())
        if $type eq 'ARRAY';

    return Test2::Compare::Hash-&gt;new(inref =&gt; $thing, $config-&gt;{implicit_end} ? (ending =&gt; 1) : ())
        if $type eq 'HASH';

    return Test2::Compare::Pattern-&gt;new(
        pattern       =&gt; $thing,
        stringify_got =&gt; 1,
    ) if $config-&gt;{use_regex} &amp;&amp; $type eq 'REGEXP';

    return Test2::Compare::Custom-&gt;new(code =&gt; $thing)
        if $config-&gt;{use_code} &amp;&amp; $type eq 'CODE';

    return Test2::Compare::Regex-&gt;new(input =&gt; $thing)
        if $type eq 'REGEXP';

    if ($type eq 'SCALAR' || $type eq 'VSTRING') {
        my $nested = _convert($$thing, $config);
        return Test2::Compare::Scalar-&gt;new(item =&gt; $nested);
    }

    return Test2::Compare::DeepRef-&gt;new(input =&gt; $thing)
        if $type eq 'REF';

    return Test2::Compare::Ref-&gt;new(input =&gt; $thing)
        if $type;

    # is() will assume string and use 'eq'
    return Test2::Compare::String-&gt;new(input =&gt; $thing);
}

1;

__END__

=pod

=encoding UTF-8

=head1 NAME

Test2::Compare - Test2 extension for writing deep comparison tools.

=head1 DESCRIPTION

This library is the driving force behind deep comparison tools such as
C&lt;Test2::Tools::Compare::is()&gt; and
C&lt;Test2::Tools::ClassicCompare::is_deeply()&gt;.

=head1 SYNOPSIS

    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, \&amp;convert);

        if ($delta) {
            $ctx-&gt;fail($name, $delta-&gt;diag, @diag);
        }
        else {
            $ctx-&gt;ok(1, $name);
        }

        $ctx-&gt;release;
        return !$delta;
    }

    sub convert {
        my $thing = shift;
        return $thing if blessed($thing) &amp;&amp; $thing-&gt;isa('Test2::Compare::MyCheck');

        return Test2::Compare::MyCheck-&gt;new(stuff =&gt; $thing);
    }

See L&lt;Test2::Compare::Base&gt; for details about writing a custom check.

=head1 EXPORTS

=over 4

=item $delta = compare($got, $expect, \&amp;convert)

This will compare the structures in C&lt;$got&gt; with those in C&lt;$expect&gt;, The
convert sub should convert vanilla structures inside C&lt;$expect&gt; into checks.
If there are differences in the structures they will be reported back as an
L&lt;Test2::Compare::Delta&gt; tree.

=item $build = get_build()

Get the current global build, if any.

=item push_build($build)

Set the current global build.

=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.

=item build($class, sub { ... })

Run the provided codeblock with a new instance of C&lt;$class&gt; as the current
build. Returns the new build.

=item $check = convert($thing)

=item $check = convert($thing, $config)

This convert function is used by C&lt;strict_convert()&gt; and C&lt;relaxed_convert()&gt;
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:

    sub my_convert {
        my $thing_to_convert = shift;
        return convert(
            $thing_to_convert,
            { ... }
        );
    }

Or the short variant:

    sub my_convert { convert($_[0], { ... }) }

There are several configuration options, here they are with the default setting
listed first:

=over 4

=item implicit_end =&gt; 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.

=item use_regex =&gt; 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.

=item use_code =&gt; 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.

=back

=item $check = strict_convert($thing)

Convert C&lt;$thing&gt; to an L&lt;Test2::Compare::*&gt; object. This will behave strictly
which means it uses these settings:

=over 4

=item implicit_end =&gt; 1

Array bounds will be checked when this object is used in a comparison. No
unexpected hash keys can be present.

=item use_code =&gt; 0

Sub references will be compared as refs (IE are these sub refs the same ref?)

=item use_regex =&gt; 0

Regexes will be compared directly (IE are the regexes the same?)

=back

=item $compare = relaxed_convert($thing)

Convert C&lt;$thing&gt; to an L&lt;Test2::Compare::*&gt; object. This will be relaxed which
means it uses these settings:

=over 4

=item implicit_end =&gt; 0

Array bounds will not be checked when this object is used in a comparison.
Unexpected hash keys can be present.

=item use_code =&gt; 1

Sub references will be run to verify a value.

=item use_regex =&gt; 1

Values will be checked against any regexes provided.

=back

=back

=head1 WRITING A VARIANT OF IS/LIKE

    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 =&gt; 0,
                    use_code     =&gt; 1,
                    use_regex    =&gt; 1,
                }
            );
        };

        my $delta = compare($got, $exp, $convert);

        if ($delta) {
            $ctx-&gt;fail($name, $delta-&gt;diag, @diag);
        }
        else {
            $ctx-&gt;ok(1, $name);
        }

        $ctx-&gt;release;
        return !$delta;
    }

The work of a comparison tool is done by 3 entities:

=over 4

=item compare()

The C&lt;compare()&gt; function takes the structure you got, the specification you
want to check against, and a C&lt;\&amp;convert&gt; sub that will convert anything that
is not an instance of an L&lt;Test2::Compare::Base&gt; subclass into one.

This tool will use the C&lt;\&amp;convert&gt; function on the specification, and then
produce an L&lt;Test2::Compare::Delta&gt; structure that outlines all the ways the
structure you got deviates from the specification.

=item \&amp;convert

Converts anything that is not an instance of an L&lt;Test2::Compare::Base&gt;
subclass, and turns it into one. The objects this produces are able to check
that a structure matches a specification.

=item $delta

An instance of L&lt;Test2::Compare::Delta&gt; 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 C&lt;&lt; @lines =
$delta-&gt;diag &gt;&gt; to get the table (lines in C&lt;@lines&gt; will not be terminated
with C&lt;"\n"&gt;).

=back

The C&lt;convert()&gt; function provided by this package contains all the
specification behavior of C&lt;like()&gt; and C&lt;is()&gt;. 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 C&lt;$check = compare($thing)&gt; function, it just
needs to accept a single argument, and produce a single instance of an
L&lt;Test2::Compare::Base&gt; subclass.

=head1 SOURCE

The source code repository for Test2-Suite can be found at
F&lt;https://github.com/Test-More/Test2-Suite/&gt;.

=head1 MAINTAINERS

=over 4

=item Chad Granum E&lt;lt&gt;exodist@cpan.orgE&lt;gt&gt;

=back

=head1 AUTHORS

=over 4

=item Chad Granum E&lt;lt&gt;exodist@cpan.orgE&lt;gt&gt;

=back

=head1 COPYRIGHT

Copyright 2018 Chad Granum E&lt;lt&gt;exodist@cpan.orgE&lt;gt&gt;.

This program is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.

See F&lt;http://dev.perl.org/licenses/&gt;

=cut
</pre></body></html>