Liskov principle and unittest

Philippe Sigaud philippe.sigaud at gmail.com
Sun Sep 25 12:30:19 PDT 2011


On Sun, Sep 25, 2011 at 20:02, deadalnix <deadalnix at gmail.com> wrote:
>> I think what he wants is to have automatically generated unittests for
>> derived classes that check if the unittests of the parent still pass for
>> them. I don't know of a nice way to do that without requiring some
>> minimal effort by the implementor of the derived class.

IIUC, I might have a partial solution:

1- put your unit test in a CTE function template that, given a type,
will generate the unit test you want.
2- given a class type, find all classes derived from it in a module.
(That's doable. I think I could even code something that determine the
inheritance tree in a module. Dunno for multi-module hierarchy,
though).
3- iterate on the found classes, generating the
unitests-as-strings-to-be-mixed-in
4- mix them in.

Client code would look like this:

class Origin {}
class Derived1 : Origin {}
...
class Derived1000 : Origin /* or any other Derived */ {}

string generateUnitTest(Type)()
{
    return "auto t = new(" ~ Type.stringof ~ ")(...);
               /* some more unit test code*/
               ";
}

mixin(testDerived!(Origin, generateUnitTest);


where testDerived is :

string testDerived(Type, alias generator)()
{
    alias GetDerived!Origin Children;
    string result;
    foreach(i, Child; Children)
        result ~= "unittest { " ~ generator!Child ~ " }\n";
    return result;
}

So you write the generator once, and mix testDerived in. And you should be done.
Is that what you want? (Still have to code GetDerived, of course)

The limitation is I'm not sure that would work for class templates hierarchies.


More information about the Digitalmars-d mailing list