The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Test2::Tools::ClassicCompare - Classic (Test::More style) comparison tools.

DESCRIPTION

This provides comparison functions that behave like they did in Test::More, unlike the Test2::Tools::Compare plugin which has modified them.

SYNOPSIS

use Test2::Tools::ClassicCompare qw/is is_deeply isnt like unlike cmp_ok/;

is($got, $expect, "These are the same when stringified");
isnt($got, $unexpected, "These are not the same when stringified");

like($got, qr/.../, "'got' matches the pattern");
unlike($got, qr/.../, "'got' does not match the pattern");

is_deeply($got, $expect, "These structures are same when checked deeply");

cmp_ok($GOT, $OP, $WANT, 'Compare these items using the specified operatr');

EXPORTS

$bool = is($got, $expect)
$bool = is($got, $expect, $name)
$bool = is($got, $expect, $name, @diag)

This does a string comparison of the two arguments. If the two arguments are the same after stringification the test passes. The test will also pass if both arguments are undef.

The test $name is optional.

The test @diag is optional, it is extra diagnostics messages that will be displayed if the test fails. The diagnostics are ignored if the test passes.

It is important to note that this tool considers "1" and "1.0" to not be equal as it uses a string comparison.

See Test2::Tools::Compare if you want an is() function that tries to be smarter for you.

$bool = isnt($got, $dont_expect)
$bool = isnt($got, $dont_expect, $name)
$bool = isnt($got, $dont_expect, $name, @diag)

This is the inverse of is(), it passes when the strings are not the same.

$bool = like($got, $pattern)
$bool = like($got, $pattern, $name)
$bool = like($got, $pattern, $name, @diag)

Check if $got matches the specified pattern. Will fail if it does not match.

The test $name is optional.

The test @diag is optional. It contains extra diagnostics messages that will be displayed if the test fails. The diagnostics are ignored if the test passes.

$bool = unlike($got, $pattern)
$bool = unlike($got, $pattern, $name)
$bool = unlike($got, $pattern, $name, @diag)

This is the inverse of like(). This will fail if $got matches $pattern.

$bool = is_deeply($got, $expect)
$bool = is_deeply($got, $expect, $name)
$bool = is_deeply($got, $expect, $name, @diag)

This does a deep check, comparing the structures in $got with those in $expect. It will recurse into hashrefs, arrayrefs, and scalar refs. All other values will be stringified and compared as strings. It is important to note that this tool considers "1" and "1.0" to not be equal as it uses a string comparison.

This is the same as Test2::Tools::Compare::is().

cmp_ok($got, $op, $expect)
cmp_ok($got, $op, $expect, $name)
cmp_ok($got, $op, $expect, $name, @diag)

Compare $got to $expect using the operator specified in $op. This is effectively an eval "\$got $op \$expect" with some other stuff to make it more sane. This is useful for comparing numbers, overloaded objects, etc.

Overloading Note: Your input is passed as-is to the comparison. If the comparison fails between two overloaded objects, the diagnostics will try to show you the overload form that was used in comparisons. It is possible that the diagnostics will be wrong, though attempts have been made to improve them since Test::More.

Exceptions: If the comparison results in an exception then the test will fail and the exception will be shown.

cmp_ok() has an internal list of operators it supports. If you provide an unsupported operator it will issue a warning. You can add operators to the %Test2::Tools::ClassicCompare::OPS hash, the key should be the operator, and the value should either be 'str' for string comparison operators, 'num' for numeric operators, or any other true value for other operators.

Supported operators:

== (num)
!= (num)
>= (num)
<= (num)
> (num)
< (num)
<=> (num)
eq (str)
ne (str)
gt (str)
lt (str)
ge (str)
le (str)
cmp (str)
!~ (str)
=~ (str)
&&
||
xor
or
and
//
&
|
~~

SOURCE

The source code repository for Test2-Suite can be found at https://github.com/Test-More/test-more/.

MAINTAINERS

Chad Granum <exodist@cpan.org>

AUTHORS

Chad Granum <exodist@cpan.org>

COPYRIGHT

Copyright Chad Granum <exodist@cpan.org>.

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/