Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

another benefit of one-liners

by geektron (Curate)
on Feb 02, 2001 at 12:05 UTC ( #55964=perlmeditation: print w/replies, xml ) Need Help??

seems this is the third or fourth post i've seen on one-liners recently, but i've found another of my favorite uses for them - testing object methods. it's a pretty simple thing, really:
perl -MMyModule -MData::Dumper -e '$foo = MyModule->new(); print Dumpe +r $foo;'
and that's it.

but this way i can verify that objects are instantiated correctly, and add whatever quicky method calls ( like one set of objects that uses another method to fetch DB records ).

quick, easy, and also good to use with the Devel::DProf to scan for any intial problem areas without creating extra test scripts.

Replies are listed 'Best First'.
Re: another benefit of one-liners
by Falkkin (Chaplain) on Feb 02, 2001 at 20:16 UTC
    Of course, if you really want to test your objects, I'd recommend Test::Unit...

    package Foo; ## BEGIN UNIT TESTING use Test::Unit; print "Testing Foo: "; create_suite(); run_suite(); print "\n\n"; sub test_new { my $foo = Foo->new(name => 'Bar', baz => "Quux"); # Test to make sure it was constructed correctly. assert($foo->get_name() eq 'Bar'); assert($foo->get_baz() eq 'Quux'); # My constructor should die if called with no args, check for that w +ith this line: eval { my $foo = Foo->new() }; assert($@); } sub test_method_1 { my $foo = Foo->new(name => 'Bar', baz => 'foon'); assert($foo->gurglify(15)); assert($foo->gurgled_amount() == 15); }
    ... and so on. I typically use one single test for each method, along with a couple moe complicated tests that make sure the methods interact correctly. Run them all with perl every time you change anything within the file, and you can be assured that it all still works (as long as your tests are comprehensive...)
      Unit Tests are definately a programmers friend. I wish I could give you more then one ++ for bringing this up. Lets just pretend that I gave you 2 ++ votes, and one vindictive -- vote for beating me to it. :-)

      Seriously though, the only thing better then a unit test which covers all the methods and exercises all the code including error cases.... is a Perl script to run all those tests for you! After I make changes to a chunk of code, I run my tests with a single script, which tells me where all my blatant bugs are. Very useful indeed. (And gives you that nice warm feeling when the test passes)

      quite true. i haven't used the Unit::Test - i end up rolling my own.

      i'll have to look into it. . . :)

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://55964]
Approved by root
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2021-09-20 14:57 GMT
Find Nodes?
    Voting Booth?

    No recent polls found