Liskov principle and unittest

Timon Gehr timon.gehr at gmx.ch
Fri Sep 23 14:36:26 PDT 2011


On 09/23/2011 10:17 PM, Max Klyga wrote:
> On 2011-09-23 12:55:25 +0300, deadalnix said:
>
>> I recently faced a problem in java. I have looooooooots of classes
>> inheriting from the same superclass. Thoses classes has to conform to
>> some behaviour expected when manipulation the superclass as Liskov
>> substitution principle says.
>>
>> This is definitvelly hard to achieve. Solution promoted on the web are
>> frankly not satisfying. Most of them suggest to inherit jUnit tests
>> too, bit this is unable to handle classes implementing multiple
>> interfaces, and require to write a test class for each subclasses. So
>> if somebody don't do thoses tests, the subclass isn't checked (which
>> is predictible) but the superclass tests are not ran either, which
>> make things even worse. This innability to check that easily makes a
>> lazy dev even more dangerous, which isn't what we want.
>>
>> Back to D, which has integrated unittest, I wonder how I could
>> implement such a check. I think the D unittest system suffer the same
>> flaw as jUint (previously exposed). What do you think ?
>>
>> deadalnix
>
> D has a wonderful feature that is suitable for such use cases - Design
> By Contract.

It's called Contract Programming actually, afaik because Design By 
Contract is trademarked by Eiffel Software. :o)

> You specify invariants and in/out contracts in your superclass and they
> will be automatically checked for subclasses.
>
> Read more here - http://www.d-p-l.org/dbc.html
>

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. Contracts 
quickly detect and report otherwise potentially unnoticed bugs during 
test runs, but they don't provide the robustness of running actual unit 
tests. The two features can be combined very well though.



More information about the Digitalmars-d mailing list