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

[email protected] (Andy Lester) Wed, 27 Jul 2016 10:34:50 -0500
Newsgroups perl.qa
Message-ID <[email protected]>
--Apple-Mail=_CF76F06E-75B1-4EC9-9A32-04F5952DEF63
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain;
	charset=utf-8


> On Jul 27, 2016, at 10:13 AM, Chad Granum <[email protected]> wrote:
>=20
> Specifically "This will ignore hash keys or array indexes that you do =
not actually specify in your $expect structure." directly documents the =
behavior.
>=20


Right.  That is a fact that is clearly spelled out.  I think it would be =
helpful to have something that is higher level, that explains when you =
use which, and examples for each.

For instance, it might be something like (if my understanding is =
correct).

    my $employee =3D get_employee();
    my $expected_employee =3D { name =3D> =E2=80=98Bob=E2=80=99, dept =3D>=
 =E2=80=98IT=E2=80=99 };

    # If you want to check the API, and ensure that get_employee() =
returns two and only two fields:
    is( get_employee(), $expected_employee );

    # If you want to check that you got the right record, but don=E2=80=99=
t care if it comes back with, say, a phone_number field:
    like( get_employee(), $expected_employee );

Also, when do you have to use the hash/field construction system?

    is( $employee, { name =3D> 'Bob', dept =3D> 'IT' } );
    like( $employee, { name =3D> 'Bob', dept =3D> 'IT' } );
    is( $employee, hash {
            field name =3D> 'Bob';
            field dept =3D> 'IT';
        }  =20
    );

When is it appropriate to use each of these?  Is it an error to mix =
like() and hash()/field()?

An example of calling like() on coderefs ("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.=E2=80=9D) would be good, too.

I=E2=80=99d be glad to write the docs if I knew the answers to the =
questions and the zen of what to use when.

As a newbie to Test2, I=E2=80=99d really like to start using it as much =
as possible, but I=E2=80=99m also afraid of screwing up existing tests =
because I use a new tool incorrectly.

Andy


--
Andy Lester =3D> www.petdance.com


--Apple-Mail=_CF76F06E-75B1-4EC9-9A32-04F5952DEF63
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html;
	charset=utf-8

<html><head><meta http-equiv=3D"Content-Type" content=3D"text/html =
charset=3Dutf-8"></head><body style=3D"word-wrap: break-word; =
-webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" =
class=3D""><br class=3D""><div><blockquote type=3D"cite" class=3D""><div =
class=3D"">On Jul 27, 2016, at 10:13 AM, Chad Granum &lt;<a =
href=3D"mailto:[email protected]" class=3D"">[email protected]</a>&gt; =
wrote:</div><br class=3D"Apple-interchange-newline"><div class=3D""><div =
style=3D"font-family: Georgia; font-size: 14px; font-style: normal; =
font-variant: normal; font-weight: normal; letter-spacing: normal; =
line-height: normal; orphans: auto; text-align: start; text-indent: 0px; =
text-transform: none; white-space: normal; widows: auto; word-spacing: =
0px; -webkit-text-stroke-width: 0px;" class=3D"">Specifically "This will =
ignore hash keys or array indexes that you do not actually specify in =
your $expect structure." directly documents the behavior.</div><br =
class=3D"Apple-interchange-newline"></div></blockquote></div><div =
class=3D""><br class=3D""></div><div class=3D"">Right. &nbsp;That is a =
fact that is clearly spelled out. &nbsp;I think it would be helpful to =
have something that is higher level, that explains when you use which, =
and examples for each.</div><div class=3D""><br class=3D""></div><div =
class=3D"">For instance, it might be something like (if my understanding =
is correct).</div><div class=3D""><br class=3D""></div><div =
class=3D""><font face=3D"Courier New" class=3D"">&nbsp; &nbsp; my =
$employee =3D get_employee();</font></div><div class=3D""><font =
face=3D"Courier New" class=3D"">&nbsp; &nbsp; my $expected_employee =3D =
{ name =3D&gt; =E2=80=98Bob=E2=80=99, dept =3D&gt; =E2=80=98IT=E2=80=99 =
};</font></div><div class=3D""><font face=3D"Courier New" class=3D""><br =
class=3D""></font></div><div class=3D""><font face=3D"Courier New" =
class=3D"">&nbsp; &nbsp; # If you want to check the API, and ensure that =
get_employee() returns two and only two fields:</font></div><div =
class=3D""><font face=3D"Courier New" class=3D"">&nbsp; &nbsp; is( =
get_employee(), $expected_employee );</font></div><div class=3D""><font =
face=3D"Courier New" class=3D""><br class=3D""></font></div><div =
class=3D""><font face=3D"Courier New" class=3D"">&nbsp; &nbsp; # If you =
want to check that you got the right record, but don=E2=80=99t care if =
it comes back with, say, a phone_number field:</font></div><div =
class=3D""><font face=3D"Courier New" class=3D"">&nbsp; &nbsp; like( =
get_employee(), $expected_employee );</font></div><div class=3D""><br =
class=3D""></div><div class=3D"">Also, when do you have to use the =
hash/field construction system?</div><div class=3D""><br =
class=3D""></div><div class=3D""><font face=3D"Courier New" =
class=3D"">&nbsp; &nbsp; is(&nbsp;$employee, =
{&nbsp;name&nbsp;=3D&gt;&nbsp;'Bob',&nbsp;dept&nbsp;=3D&gt;&nbsp;'IT'&nbsp=
;} );<br class=3D"">&nbsp; &nbsp; like(&nbsp;$employee, =
{&nbsp;name&nbsp;=3D&gt;&nbsp;'Bob',&nbsp;dept&nbsp;=3D&gt;&nbsp;'IT'&nbsp=
;} );<br class=3D"">&nbsp; &nbsp; is(&nbsp;$employee, hash {<br =
class=3D"">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp;&nbsp;field&nbsp;name&nbsp;=3D&gt;&nbsp;'Bob';<br class=3D"">&nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp;&nbsp;field&nbsp;dept&nbsp;=3D&gt;&nbsp;'IT';<br class=3D"">&nbsp; =
&nbsp; &nbsp; &nbsp;&nbsp;}&nbsp; &nbsp;<br class=3D"">&nbsp; &nbsp; =
);</font></div><div class=3D""><br class=3D""></div><div class=3D"">When =
is it appropriate to use each of these? &nbsp;Is it an error to mix =
like() and hash()/field()?</div><div class=3D""><br class=3D""></div><div =
class=3D"">An example of calling like() on coderefs ("The same is true =
for coderefs, the value is passed in as the first argument (and in <code =
class=3D"">$_</code>) and the sub should return a boolean value.=E2=80=9D)=
 would be good, too.</div><div class=3D""><br class=3D""></div><div =
class=3D"">I=E2=80=99d be glad to write the docs if I knew the answers =
to the questions and the zen of what to use when.</div><div class=3D""><br=
 class=3D""></div><div class=3D"">As a newbie to Test2, I=E2=80=99d =
really like to start using it as much as possible, but I=E2=80=99m also =
afraid of screwing up existing tests because I use a new tool =
incorrectly.</div><div class=3D""><br class=3D""></div><div =
class=3D"">Andy</div><div class=3D""><br class=3D""></div><div =
class=3D""><br class=3D""></div><div class=3D"">--</div><div =
apple-content-edited=3D"true" class=3D"">Andy Lester =3D&gt;&nbsp;<a =
href=3D"http://www.petdance.com" class=3D"">www.petdance.com</a>

</div>
<br class=3D""></body></html>=

--Apple-Mail=_CF76F06E-75B1-4EC9-9A32-04F5952DEF63--