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

Data Structures design help to represent function args for testing

by goibhniu (Hermit)
on Sep 20, 2007 at 21:22 UTC ( [id://640231]=perlquestion: print w/replies, xml ) Need Help??

goibhniu has asked for the wisdom of the Perl Monks concerning the following question:

I have some perl modules that were left behind by some consultants. They didn't use strict (and when they set stuff up I didn't know enough to make them). I'm trying to extend their work and reuse their code, but it's kludgey, doesn't use strict and some of it just doesn't work.

So I've set about to refactor. I'm trying to build in some safety nets and I'm trying to do some things right. Here's what I've done:

  • all the original code (from them) is versioned
  • where they had package Foo::Bar, I've created a folder under and copied their work to Foo::Strict::Bar
  • I've decided to write a test harness and baseline the as-is results. My goal will be to reproduce these results with the fixed up code. This way I won't break their interface.

I'm getting a little stuck on the baselining and test harnessing. I haven't used Test::Anything before and am reading those. I started with:
perl -lne "print $1 if /^\W*sub (.*)/i" harcommon.pm
to get a list of subs and then wrote this (with help from chatterboxers) to test them:

#!/usr/bin/perl use harcommon; my @funclist = qw(udpinifile colsep remove_duplicates runsql runhsql get_ini_setting create_min_ini delete_min_ini get_ini_file get_os get_tempdir get_s +lash file_exists display_text debug trim rtrim ltrim date_string extension_in_list string_in_list unlock_user exiterror capture_file + transfer_file delete_file execute_cmd find_user_home version get_cl +ear_pass get_pass get_vp get_full_item get_item get_version_num get_ext get_item_minus_ext); my %dispatch; #$dispatch{$_}=*{$_}{'CODE'} foreach @funclist; #also works $dispatch{$_}=\&{"harcommon::$_"} foreach @funclist; { local $\="\n"; foreach my $func (@funclist) { print '==========================='; print $func; #print $dispatch{$func}; print '==========================='; print harcommon->$func() } }

(I'm not using strict because they're not using strict and it just blows up totally; I intend to use strict after I've got the baseline of what they do now).

Some of their functions run without args, some work without args but give screwy results, some kill the script and block the rest of the tests. I want to go function by function and make a set of test args so at least I'm calling them in good faith for the baseline. What data structure should I use to represent the function args?


I humbly seek wisdom.

Replies are listed 'Best First'.
Re: Data Structures design help to represent function args for testing
by Joost (Canon) on Sep 20, 2007 at 21:38 UTC

      Wonderful - that's the kind of data structure I was looking for. Each arg list as an array. Each @funclist as the key to a hash pointing to the arg list.

      Any suggestions for a good data structure if I want more than one test list of args per function? (not a requirement in the original post - I'm just exploring the design space).


      I humbly seek wisdom.

        You're taking it up one level, so extrapolate the argument solution up one level - use a hash of arrays or an array of arrays:

        my %functions = ( function1 => {arglist1 => ["arg1", { arg2 => val2 }], arglist2 => [...], ... }, function2 => {...}, ..., ); while (my ($func, $argsList) = each %functions) { for my $key (keys %$argsList) { print "Test $func against args $key\n" &{"harcommon::$func"}(@{$argsList->{$key}}); } }

        or:

        my %functions = ( function1 => [["arg1", { arg2 => val2 }], [...], ... ], function2 => [...], ..., ); while (my ($func, $argsList) = each %functions) { for my $args (@$argsList) { print "Test $func against args $key\n" &{"harcommon::$func"}(@$args); } }

        Perl is environmentally friendly - it saves trees
Re: Data Structures design help to represent function args for testing
by rvosa (Curate) on Sep 21, 2007 at 07:03 UTC
    At the risk of answering questions you haven't actually asked, I've had some success maintaining other people's code using: You're certainly right to start writing tests so you won't break anything as you refactor, though. Good luck!

      I take no offense. I like people of experience teaching me the questions I should be asking as well as answering the ones I do ask. This is valuable advice I'll be looking into. Thanks


      I humbly seek wisdom.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://640231]
Approved by Joost
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (5)
As of 2024-04-25 14:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found