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

[email protected] (Chad Granum) Wed, 27 Jul 2016 08:46:37 -0700
Newsgroups perl.qa
Message-ID <CAJFr3ku3rLb=WM9_t+me2drW=hoM3fX3_aDV49VH+v+qGgLEdQ@mail.gmail.com>
--94eb2c0954e83b809605389fec80
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

The hash/field/etc builder system is always optional. You can always mix
them, it is perfectly fine to use the builders for both 'is' and 'like',
though for the builders it really does not matter which of the 2 you use
since strict/relaxed only applies to conversions of
hashrefs/arrays/scalars,  checks you build using the builders don't get
converted (though embedded hashes and arrays will).

is(
    $foo,
    { # This hash is converted to checks using the strict conversion
        foo =3D> hash { # Not converted
            # 'strict' conversion to string check
            field a =3D> 'xxx';

            # This hash is converted using strict (cause we are inside an
is())
            field b =3D> { foo =3D> 'bar' };

            # This being here makes the foo =3D> hash { ... } check strict.=
 If
            # end() was not specified here it would be a relaxed check,
is/like
            # do not control/alter it.
            end();
        }
    },
    "description"
);

Here is a sub example for you:

like(
    {foo =3D> 'foo' },
    {foo =3D> sub { $_ eq 'foo' ? 1 : 0 } },
    "Useless and trivial example of using a sub check in like"
);



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

>
> On Jul 27, 2016, at 10:13 AM, Chad Granum <[email protected]> wrote:
>
> Specifically "This will ignore hash keys or array indexes that you do not
> actually specify in your $expect structure." directly documents the
> behavior.
>
>
> 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 us=
e
> 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() return=
s
> 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=
=99t 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';
>         }
>     );
>
> 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 questi=
ons 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 a=
s
> possible, but I=E2=80=99m also afraid of screwing up existing tests becau=
se I use a
> new tool incorrectly.
>
> Andy
>
>
> --
> Andy Lester =3D> www.petdance.com
>
>

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

<div dir=3D"ltr"><div><div>The hash/field/etc builder system is always opti=
onal. You can always mix them, it is perfectly fine to use the builders for=
 both &#39;is&#39; and &#39;like&#39;, though for the builders it really do=
es not matter which of the 2 you use since strict/relaxed only applies to c=
onversions of hashrefs/arrays/scalars, =C2=A0checks you build using the bui=
lders don&#39;t get converted (though embedded hashes and arrays will).</di=
v><div><br></div><div>is(</div><div>=C2=A0 =C2=A0 $foo,</div><div>=C2=A0 =
=C2=A0 { # This hash is converted to checks using the strict conversion</di=
v><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 foo =3D&gt; hash { # Not converted</div>=
<div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 # &#39;strict&#39; conversio=
n to string check</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 field=
 a =3D&gt; &#39;xxx&#39;;</div><div><br></div><div>=C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0 =C2=A0 # This hash is converted using strict (cause we are insid=
e an is())</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 field b =3D&=
gt; { foo =3D&gt; &#39;bar&#39; };</div><div><br></div><div>=C2=A0 =C2=A0 =
=C2=A0 =C2=A0 =C2=A0 =C2=A0 # This being here makes the foo =3D&gt; hash { =
... } check strict. If</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =
# end() was not specified here it would be a relaxed check, is/like</div><d=
iv>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 # do not control/alter it.</di=
v><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 end();</div><div>=C2=A0 =
=C2=A0 =C2=A0 =C2=A0 } =C2=A0=C2=A0</div><div>=C2=A0 =C2=A0 }, =C2=A0</div>=
<div>=C2=A0 =C2=A0 &quot;description&quot;</div><div>);</div></div><div><br=
></div><div>Here is a sub example for you:<br></div><div><br></div><div>lik=
e(</div><div>=C2=A0 =C2=A0 {foo =3D&gt; &#39;foo&#39; },</div><div>=C2=A0 =
=C2=A0 {foo =3D&gt; sub { $_ eq &#39;foo&#39; ? 1 : 0 } },</div><div>=C2=A0=
 =C2=A0 &quot;Useless and trivial example of using a sub check in like&quot=
;</div><div>);</div><div><br></div><div><br></div></div><div class=3D"gmail=
_extra"><br><div class=3D"gmail_quote">On Wed, Jul 27, 2016 at 8:34 AM, And=
y Lester <span dir=3D"ltr">&lt;<a href=3D"mailto:[email protected]" target=
=3D"_blank">[email protected]</a>&gt;</span> wrote:<br><blockquote class=3D=
"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding=
-left:1ex"><div style=3D"word-wrap:break-word"><span class=3D""><br><div><b=
lockquote type=3D"cite"><div>On Jul 27, 2016, at 10:13 AM, Chad Granum &lt;=
<a href=3D"mailto:[email protected]" target=3D"_blank">[email protected]<=
/a>&gt; wrote:</div><br><div><div style=3D"font-family:Georgia;font-size:14=
px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:=
normal;line-height:normal;text-align:start;text-indent:0px;text-transform:n=
one;white-space:normal;word-spacing:0px">Specifically &quot;This will ignor=
e hash keys or array indexes that you do not actually specify in your $expe=
ct structure.&quot; directly documents the behavior.</div><br></div></block=
quote></div><div><br></div></span><div>Right.=C2=A0 That is a fact that is =
clearly spelled out.=C2=A0 I think it would be helpful to have something th=
at is higher level, that explains when you use which, and examples for each=
.</div><div><br></div><div>For instance, it might be something like (if my =
understanding is correct).</div><div><br></div><div><font face=3D"Courier N=
ew">=C2=A0 =C2=A0 my $employee =3D get_employee();</font></div><div><font f=
ace=3D"Courier New">=C2=A0 =C2=A0 my $expected_employee =3D { name =3D&gt; =
=E2=80=98Bob=E2=80=99, dept =3D&gt; =E2=80=98IT=E2=80=99 };</font></div><di=
v><font face=3D"Courier New"><br></font></div><div><font face=3D"Courier Ne=
w">=C2=A0 =C2=A0 # If you want to check the API, and ensure that get_employ=
ee() returns two and only two fields:</font></div><div><font face=3D"Courie=
r New">=C2=A0 =C2=A0 is( get_employee(), $expected_employee );</font></div>=
<div><font face=3D"Courier New"><br></font></div><div><font face=3D"Courier=
 New">=C2=A0 =C2=A0 # If you want to check that you got the right record, b=
ut don=E2=80=99t care if it comes back with, say, a phone_number field:</fo=
nt></div><div><font face=3D"Courier New">=C2=A0 =C2=A0 like( get_employee()=
, $expected_employee );</font></div><div><br></div><div>Also, when do you h=
ave to use the hash/field construction system?</div><div><br></div><div><fo=
nt face=3D"Courier New">=C2=A0 =C2=A0 is(=C2=A0$employee, {=C2=A0name=C2=A0=
=3D&gt;=C2=A0&#39;Bob&#39;,=C2=A0dept=C2=A0=3D&gt;=C2=A0&#39;IT&#39;=C2=A0}=
 );<br>=C2=A0 =C2=A0 like(=C2=A0$employee, {=C2=A0name=C2=A0=3D&gt;=C2=A0&#=
39;Bob&#39;,=C2=A0dept=C2=A0=3D&gt;=C2=A0&#39;IT&#39;=C2=A0} );<br>=C2=A0 =
=C2=A0 is(=C2=A0$employee, hash {<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0=C2=A0field=C2=A0name=C2=A0=3D&gt;=C2=A0&#39;Bob&#39;;<br>=C2=A0 =C2=A0 =
=C2=A0 =C2=A0 =C2=A0 =C2=A0=C2=A0field=C2=A0dept=C2=A0=3D&gt;=C2=A0&#39;IT&=
#39;;<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0=C2=A0}=C2=A0 =C2=A0<br>=C2=A0 =C2=A0 )=
;</font></div><div><br></div><div>When is it appropriate to use each of the=
se?=C2=A0 Is it an error to mix like() and hash()/field()?</div><div><br></=
div><div>An example of calling like() on coderefs (&quot;The same is true f=
or coderefs, the value is passed in as the first argument (and in <code>$_<=
/code>) and the sub should return a boolean value.=E2=80=9D) would be good,=
 too.</div><div><br></div><div>I=E2=80=99d be glad to write the docs if I k=
new the answers to the questions and the zen of what to use when.</div><div=
><br></div><div>As a newbie to Test2, I=E2=80=99d really like to start usin=
g it as much as possible, but I=E2=80=99m also afraid of screwing up existi=
ng tests because I use a new tool incorrectly.</div><span class=3D""><div><=
br></div><div>Andy</div><div><br></div><div><br></div><div>--</div><div>And=
y Lester =3D&gt;=C2=A0<a href=3D"http://www.petdance.com" target=3D"_blank"=
>www.petdance.com</a>

</div>
<br></span></div></blockquote></div><br></div>

--94eb2c0954e83b809605389fec80--