Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: No plan "weakens" my Test scripts?

by xdg (Monsignor)
on Feb 08, 2005 at 13:41 UTC ( #429056=note: print w/ replies, xml ) Need Help??


in reply to No plan "weakens" my Test scripts?

'no_plan' is good when you're willy-nilly writing tests and don't want to keep jumping to the top of your file to update the number of tests. (Alternative frameworks like Test::Class actually help keep count for you and it plays nicely with Test::More and friends).

If you have a knowable number of test cases, you can always count the number and pass that to the plan (either on the "use" line or with the "plan" function. For example:

use Test::More; open my($fh), "<datafile"; my $count = do { my $i; $i++ while <$fh>; $i }; seek ( $fh, 0, 0 ); plan tests => $count; load_database("datafile"); # or whatever while (<$fh>) { ok( in_database( $_ ) ); }

-xdg

Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.


Comment on Re: No plan "weakens" my Test scripts?
Download Code
Re^2: No plan "weakens" my Test scripts?
by Aristotle (Chancellor) on Feb 19, 2005 at 03:29 UTC
    my $count = do { my $i; $i++ while <$fh>; $i };
    my $count = do { () = <$fh>; $. };

    Makeshifts last the longest.

      I'm pretty sure that () = <$fh> loads the entire file contents into memory at once (on the stack), not throwing any of it away until the entire file has been read. So you might as well just use:

      my $count= ()= <$fh>;

      If you want to avoid memory use:

      my $count= do { 0 while <$fh>; $. };

      - tye        

        I was willing to 'waste' the memory on the $i variable for clarity in my example since the discussion was about testing and not about file line counting. (Why make someone who doesn't know it look up $. just to understand an example?) :-)

        -xdg

        Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://429056]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (7)
As of 2014-07-30 10:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (230 votes), past polls