Liskov principle and unittest

deadalnix deadalnix at gmail.com
Mon Sep 26 05:05:53 PDT 2011


Le 25/09/2011 21:30, Philippe Sigaud a écrit :
> 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.

Sounds nice ! I'm pretty sure this has some limitations, but that 
definitively a step forward.

I'll do some tests about that.

If the getDerived isn't devellopped, then the whole stuff will fail at 
compile time, so this ok. We can be sure that tests are performed for 
every derived classes, and so, even if the devellopper of the derived 
class forgot to do unittest or don't want to.


More information about the Digitalmars-d mailing list