Re: Writing our own modules in Test2

[email protected] (Andy Lester) Fri, 24 Jun 2016 14:02:56 -0500
Newsgroups perl.qa
Message-ID <[email protected]>
--Apple-Mail=_26C877D2-11F5-40A0-A249-81285BE4A273
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain;
	charset=windows-1252


> On Jun 24, 2016, at 12:57 PM, Buddy Burden <[email protected]> =
wrote:
>=20
> What sort of methods did you have in mind?  I might have something to =
contribute, if you would be interested in contributions.


I=92ve accumulated a number of test functions in our work codebase for =
doing what I call =93expressive=94 tests to avoid common cut & paste.  =
I=92m using the term =93expressive=94 for now because they=92re =
expressing what it is you want, rather than making you write the code =
that explains them.


For instance, instead of doing all the=20

	is( scalar @{$foo}, 0 );  # Is this an empty array?
	ok( scalar @{$foo} );   # Is there something in the array?

I=92ve been writing

	is_empty_array( $foo );
	is_nonempty_array( $foo );

because I believe that it=92s easier to read the English =93is empty =
array=94 than to translate =93is( scalar @{$foo}, 0 )=94 into that =
meaning.  We=92ve got some 1200+ .t files of 11MB so I do a lot of =
reading of .t files all day.

Similarly:

	is_nonblank( $response );
	is_blank( $response );

instead of

	ok( defined($response) ) && like( $response, qr/./ );
	is( $response, =91=92 );

for the same reasons.  They=92re common idioms, but I=92d still rather =
read English than Perl.

Also, I see it that the fewer arguments passed around, the fewer places =
for mistakes.

I=92ve also stolen some stuff from Test::Numeric for common tests.  =
is_integer, is_positive_integer, is_nonnegative_integer, is_even.

I=92ve also got things like all_keys_exist_in( \%hash, [qw( foo bar bat =
)] ).

=85

Now, with Test2, my thinking on many of these changes.  The odious

	is( scalar @{$foo}, 0 );

now becomes simply

	is( $foo, [] );

which may not be as Englishy as=20

	is_empty_array( $foo )

but that I am probably fine with.  So, too, does something like =
all_keys_exist_in() become much easier to do with the new DSL in Test2.  =
But there will still be others like it, I suspect.

=85

I also want to have something that is an example of the Right Way To Do =
It that we can point at as an add-on distribution to Test2::Suite.  I =
think that will help future Test2::Tools writers.

So those are my high-level thoughts.

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


--Apple-Mail=_26C877D2-11F5-40A0-A249-81285BE4A273
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html;
	charset=windows-1252

<html><head><meta http-equiv=3D"Content-Type" content=3D"text/html =
charset=3Dwindows-1252"></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 Jun 24, 2016, at 12:57 PM, Buddy Burden &lt;<a =
href=3D"mailto:[email protected]" =
class=3D"">[email protected]</a>&gt; wrote:</div><br =
class=3D"Apple-interchange-newline"><div class=3D""><span =
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; float: none; display: inline =
!important;" class=3D"">What sort of methods did you have in mind? =
&nbsp;I might have something to contribute, if you would be interested =
in contributions.</span><br 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""></div></blockquote></div><div class=3D""><br =
class=3D""></div><div class=3D"">I=92ve accumulated a number of test =
functions in our work codebase for doing what I call =93expressive=94 =
tests to avoid common cut &amp; paste. &nbsp;I=92m using the term =
=93expressive=94 for now because they=92re expressing what it is you =
want, rather than making you write the code that explains =
them.</div><div class=3D""><br class=3D""></div><div class=3D""><br =
class=3D""></div><div class=3D"">For instance, instead of doing all =
the&nbsp;</div><div class=3D""><br class=3D""></div><div class=3D""><span =
class=3D"Apple-tab-span" style=3D"white-space:pre">	</span>is( =
scalar @{$foo}, 0 ); &nbsp;# Is this an empty array?</div><div =
class=3D""><span class=3D"Apple-tab-span" style=3D"white-space:pre">	=
</span>ok( scalar @{$foo} ); &nbsp; # Is there something in the =
array?</div><div class=3D""><br class=3D""></div><div class=3D"">I=92ve =
been writing</div><div class=3D""><br class=3D""></div><div =
class=3D""><span class=3D"Apple-tab-span" style=3D"white-space:pre">	=
</span>is_empty_array( $foo );</div><div class=3D""><span =
class=3D"Apple-tab-span" style=3D"white-space:pre">	=
</span>is_nonempty_array( $foo );</div><div class=3D""><br =
class=3D""></div><div class=3D"">because I believe that it=92s easier to =
read the English =93is empty array=94 than to translate =93is( scalar =
@{$foo}, 0 )=94 into that meaning. &nbsp;We=92ve got some 1200+ .t files =
of 11MB so I do a lot of reading of .t files all day.</div><div =
class=3D""><br class=3D""></div><div class=3D"">Similarly:</div><div =
class=3D""><br class=3D""></div><div class=3D""><span =
class=3D"Apple-tab-span" style=3D"white-space:pre">	=
</span>is_nonblank( $response );</div><div class=3D""><span =
class=3D"Apple-tab-span" style=3D"white-space:pre">	</span>is_blank( =
$response );</div><div class=3D""><br class=3D""></div><div =
class=3D"">instead of</div><div class=3D""><br class=3D""></div><div =
class=3D""><span class=3D"Apple-tab-span" style=3D"white-space:pre">	=
</span>ok( defined($response) ) &amp;&amp; like( $response, qr/./ =
);</div><div class=3D""><span class=3D"Apple-tab-span" =
style=3D"white-space:pre">	</span>is( $response, =91=92 =
);</div><div class=3D""><br class=3D""></div><div class=3D"">for the =
same reasons. &nbsp;They=92re common idioms, but I=92d still rather read =
English than Perl.</div><div class=3D""><br class=3D""></div><div =
class=3D"">Also, I see it that the fewer arguments passed around, the =
fewer places for mistakes.</div><div class=3D""><br class=3D""></div><div =
class=3D"">I=92ve also stolen some stuff from Test::Numeric for common =
tests. &nbsp;is_integer, is_positive_integer, is_nonnegative_integer, =
is_even.</div><div class=3D""><br class=3D""></div><div class=3D"">I=92ve =
also got things like all_keys_exist_in( \%hash, [qw( foo bar bat )] =
).</div><div class=3D""><br class=3D""></div><div class=3D"">=85</div><div=
 class=3D""><br class=3D""></div><div class=3D"">Now, with Test2, my =
thinking on many of these changes. &nbsp;The odious</div><div =
class=3D""><br class=3D""></div><div class=3D""><span =
class=3D"Apple-tab-span" style=3D"white-space:pre">	</span>is( =
scalar @{$foo}, 0 );</div><div class=3D""><br class=3D""></div><div =
class=3D"">now becomes simply</div><div class=3D""><br =
class=3D""></div><div class=3D""><span class=3D"Apple-tab-span" =
style=3D"white-space:pre">	</span>is( $foo, [] );</div><div =
class=3D""><br class=3D""></div><div class=3D"">which may not be as =
Englishy as&nbsp;</div><div class=3D""><br class=3D""></div><div =
class=3D""><span class=3D"Apple-tab-span" style=3D"white-space:pre">	=
</span>is_empty_array( $foo )</div><div class=3D""><br =
class=3D""></div><div class=3D"">but that I am probably fine with. =
&nbsp;So, too, does something like all_keys_exist_in() become much =
easier to do with the new DSL in Test2. &nbsp;But there will still be =
others like it, I suspect.</div><div class=3D""><br class=3D""></div><div =
class=3D"">=85</div><div class=3D""><br class=3D""></div><div class=3D"">I=
 also want to have something that is an example of the Right Way To Do =
It that we can point at as an add-on distribution to Test2::Suite. =
&nbsp;I think that will help future Test2::Tools writers.</div><div =
class=3D""><br class=3D""></div><div class=3D"">So those are my =
high-level thoughts.</div><div class=3D""><br class=3D""></div><div =
apple-content-edited=3D"true" class=3D"">
--<br 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=_26C877D2-11F5-40A0-A249-81285BE4A273--