Re: Test2::Tools::Compare is vs. like

[email protected] (Chad Granum) Wed, 27 Jul 2016 08:13:16 -0700
Newsgroups perl.qa
Message-ID <CAJFr3kssvXhjABUYBgM+n8mxdKVcwNTCWajoTAyskgkVBt1PkQ@mail.gmail.com>
--001a1147886ef5d6a905389f74b8
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

I thought I had documented the differences pretty well. If you look here
https://metacpan.org/pod/Test2::Tools::Compare#COMPARISON-TOOLS and read
both the 'is()' and 'like()' sections it makes it clear.

is:

> his is the strict checker. The strict checker requires a perfect match
> between $got and $expect. All hash fields must be specified, all array
> items must be present, etc. All non-scalar/hash/array/regex references mu=
st
> be identical (same memory address). Scalar, hash and array references wil=
l
> be traversed and compared. Regex references will be compared to see if th=
ey
> have the same pattern.
>

like:

> This is the relaxed checker. This will ignore hash keys or array indexes
> that you do not actually specify in your $expect structure. In addition
> regex and sub references will be used as validators. If you provide a reg=
ex
> using qr/.../, the regex itself will be used to validate the correspondin=
g
> value in the $got structure. The same is true for coderefs, the value is
> passed in as the first argument (and in $_) and the sub should return a
> boolean value. In this tool regexes will stringify the thing they are
> checking.
>

Specifically "This will ignore hash keys or array indexes that you do not
actually specify in your $expect structure." directly documents the
behavior.


That said I have no opposition to making the docs more clear, and am open
to suggestions on how to reword or reorganize it.

-Chad

On Wed, Jul 27, 2016 at 8:03 AM, Andy Lester <[email protected]> wrote:

> I was going to mail this to Chad directly, but I think it=E2=80=99s worth=
 airing
> publicly.
>
> As a newcomer to Test2, it was never clear to me until just now when to
> use is() or like() for deep structures.  Given this code:
>
>     my $errors =3D do_something();
>     is( @{$errors}, 0. =E2=80=98No errors back from do_something()=E2=80=
=99 ); # old way
>
> And the new way to check would be:
>
>     is( $errors, [], =E2=80=98No errors back from do_something()=E2=80=99=
 );
>
> It would be very easy to use this instead:
>
>     like( $errors, [], =E2=80=98No errors back from do_something()=E2=80=
=99 );
>
> And it looks like like() works just fine, but really it will pass even if
> $errors has something in it.
>
>     is( [], [] ); # Passes
>     like( [=E2=80=98foo=E2=80=99], [] ); # passes but you wouldn=E2=80=99=
t expect it to.
>
> The docs say "This is the strict checker=E2=80=9D and =E2=80=9CThis is th=
e relaxed
> checker=E2=80=9D for each of them, respectively, but I think it would be =
worth
> having something in the docs that explains the differences between the tw=
o.
>
> Anyone else have troubles with these two functions?  Or other gotchas
> where new features aren=E2=80=99t what people switching from Test::More m=
ight
> expect?
>
> Andy
>
> --
> Andy Lester =3D> www.petdance.com
>
>

--001a1147886ef5d6a905389f74b8
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">I thought I had documented the differences pretty well. If=
 you look here=C2=A0<a href=3D"https://metacpan.org/pod/Test2::Tools::Compa=
re#COMPARISON-TOOLS">https://metacpan.org/pod/Test2::Tools::Compare#COMPARI=
SON-TOOLS</a> and read both the &#39;is()&#39; and &#39;like()&#39; section=
s it makes it clear.<div><br></div><div>is:</div><blockquote class=3D"gmail=
_quote" style=3D"margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left=
-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">his is th=
e strict checker. The strict checker requires a perfect match between $got =
and $expect. All hash fields must be specified, all array items must be pre=
sent, etc. All non-scalar/hash/array/regex references must be identical (sa=
me memory address). Scalar, hash and array references will be traversed and=
 compared. Regex references will be compared to see if they have the same p=
attern.<br></blockquote><div><br></div><div>like:</div><blockquote class=3D=
"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-left-width:1px;borde=
r-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">Thi=
s is the relaxed checker. This will ignore hash keys or array indexes that =
you do not actually specify in your $expect structure. In addition regex an=
d sub references will be used as validators. If you provide a regex using q=
r/.../, the regex itself will be used to validate the corresponding value i=
n the $got structure. The same is true for coderefs, the value is passed in=
 as the first argument (and in $_) and the sub should return a boolean valu=
e. In this tool regexes will stringify the thing they are checking.<br></bl=
ockquote><div><br></div><div>Specifically &quot;This will ignore hash keys =
or array indexes that you do not actually specify in your $expect structure=
.&quot; directly documents the behavior.</div><div><br></div><div><br></div=
><div>That said I have no opposition to making the docs more clear, and am =
open to suggestions on how to reword or reorganize it.</div><div><br></div>=
<div>-Chad</div></div><div class=3D"gmail_extra"><br><div class=3D"gmail_qu=
ote">On Wed, Jul 27, 2016 at 8:03 AM, Andy Lester <span dir=3D"ltr">&lt;<a =
href=3D"mailto:[email protected]" target=3D"_blank">[email protected]</a>&g=
t;</span> wrote:<br><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0=
 .8ex;border-left:1px #ccc solid;padding-left:1ex">I was going to mail this=
 to Chad directly, but I think it=E2=80=99s worth airing publicly.<br>
<br>
As a newcomer to Test2, it was never clear to me until just now when to use=
 is() or like() for deep structures.=C2=A0 Given this code:<br>
<br>
=C2=A0 =C2=A0 my $errors =3D do_something();<br>
=C2=A0 =C2=A0 is( @{$errors}, 0. =E2=80=98No errors back from do_something(=
)=E2=80=99 ); # old way<br>
<br>
And the new way to check would be:<br>
<br>
=C2=A0 =C2=A0 is( $errors, [], =E2=80=98No errors back from do_something()=
=E2=80=99 );<br>
<br>
It would be very easy to use this instead:<br>
<br>
=C2=A0 =C2=A0 like( $errors, [], =E2=80=98No errors back from do_something(=
)=E2=80=99 );<br>
<br>
And it looks like like() works just fine, but really it will pass even if $=
errors has something in it.<br>
<br>
=C2=A0 =C2=A0 is( [], [] ); # Passes<br>
=C2=A0 =C2=A0 like( [=E2=80=98foo=E2=80=99], [] ); # passes but you wouldn=
=E2=80=99t expect it to.<br>
<br>
The docs say &quot;This is the strict checker=E2=80=9D and =E2=80=9CThis is=
 the relaxed checker=E2=80=9D for each of them, respectively, but I think i=
t would be worth having something in the docs that explains the differences=
 between the two.<br>
<br>
Anyone else have troubles with these two functions?=C2=A0 Or other gotchas =
where new features aren=E2=80=99t what people switching from Test::More mig=
ht expect?<br>
<br>
Andy<br>
<br>
--<br>
Andy Lester =3D&gt; <a href=3D"http://www.petdance.com" rel=3D"noreferrer" =
target=3D"_blank">www.petdance.com</a><br>
<br>
</blockquote></div><br></div>

--001a1147886ef5d6a905389f74b8--