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

Once again, I am creating a zoo of OO Perl modules from scratch, and given the opportunity I am using test Driven Development (TDD), which I have to say is a great way to code.

Having built three OO modules so far, I am running into a problem. I want to test creating objects with as many different combinations as possible, and my general test plan is

Ideally this leaves things exactly the way they started, which means I can run the tests over and over, without stopping to clean up afterwards.

I'd like to try lots of different choices when I create the objects, and later test that all of the choics I made were correctly stored in the instantied object. The two methods I can think of for doing that are 1. nested loops, and 2. a single loop that increments a number, along with a series of tests inside the loop that checks for a particular bit and, if set, enables that particular option.

Aside: Hmm, maybe the way I should be doing it is create a named object in one scope, and then immediately test the named object in another scope, all inside one loop ..

Neither of these seem very elegant .. I did find Math::Combinatorics on CPAN but that's not really want I'm looking for. Has anyone else come across this particular challenge?

Alex / talexb / Toronto

"Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

Replies are listed 'Best First'.
Re: Testing and combinations
by chromatic (Archbishop) on Oct 30, 2007 at 22:02 UTC

    For some reason I think of Test::LectroTest. I don't have a good example, but it might help you.

Re: Testing and combinations
by GrandFather (Saint) on Oct 30, 2007 at 19:52 UTC

    How are you setting options? By passing parameters to a new or init method, by calling specific setters on the object, some other fashion?

    In any case I'd be inclined to build a table with a list of option and value pairs for each object class. You can then use the table entries to drive generating and configuring object instances as well as validating the result. We really need to see something more of the way the API for your objects is put together though to be able to sketch some practical code.


    Perl is environmentally friendly - it saves trees

      I'm setting options by adding things to an arg hash, and I am currently using a table to select my two test cases, one without a file, and the other one with. I also have an option that includes specifing a list, and I want to be able to specify a null list, a one element list or a three element list.

      I think I'll use your suggestion of basing it on tables, and pre-populate my tables so that the first entry is 'nothing'; then I just use a serious of next ifs to try all of the different combinations.

      Alex / talexb / Toronto

      "Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

        A series of next if ...s sounds like hard work for maintenance if there are more than a few of them. Sketch out your code and post it here if it looks like becoming unwieldy.

        The main thing to consider is that you should be able to handle option lists of arbitrary length without needing to alter the test code. Also you should be able to specify the domain of valid option combinations in the table and have the test code generate the permutations without having to author each case in the test code.


        Perl is environmentally friendly - it saves trees
Re: Testing and combinations
by jdporter (Paladin) on Oct 30, 2007 at 23:02 UTC