www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.announce - dmocks version 0.1

reply Christopher Wright <dhasenan gmail.com> writes:
Hello all.

For the Test-Driven Development people out there, and all the fans of 
unittesting, I'm releasing a mock objects framework for D2, dmocks. It 
can mock classes and interfaces, even if they are templated; it cannot 
mock structs due to inheritance issues, and it can only mock virtual 
methods (no templated methods, sorry; not my fault).

Download:
http://damask-mud.googlecode.com/files/dmocks.zip

Subversion:
svn checkout http://damask-mud.googlecode.com/svn/trunk/mocks/

Note that these are temporary locations, and I hope to move the project 
to dsource shortly.

Examples:
class Bar {
    int doSomeCalculations (ubyte[] data) { ... }
}
class Foo {
    private Bar bar;
    this (Bar bar) { this.bar = bar; }

    int calculateStuff (ubyte[] data) {
       int i = bar.doSomeCalculations (data);
       ...
       return i;
    }

    unittest {
       Mocker m = new Mocker();
       auto bar = m.Mock!(Bar);
       ubyte[] data = null;

       bar.doSomeCalculations(data);
       m.LastCall().Return(5);
       // or m.Expect(bar.doSomeCalculations(data)).Return(5);

       m.Replay();
       Foo target = new Foo(bar);
       target.calculateStuff(data);

       m.Verify();
    }
}

Address any complaints you have to dhasenan -at- gmail -dot- com. If 
anyone has issues with style, I can offer both CamelCase and 
abridgedCamelCase; if enough people request it, I'll offer a free 
function interface if I can find a sane way of doing it. Complaints 
about functionality will be addressed first.


The framework in its current state represents about three days' work, 
which is more a sign of the ease of using D than any special abilities I 
have.

Dmocks is brought to you by __traits and string mixins. We therefore 
regret to inform you that it is only available for D version 2.
Nov 07 2007
parent reply Christopher Wright <dhasenan gmail.com> writes:
For anyone who doesn't know what mock objects are and wants to, there's 
Wikipedia: http://en.wikipedia.org/wiki/Mock_object

In brief, a mock object allows you to test how one class interacts with 
another, and test the functionality of each class independently of the 
rest. It does so by acting like an object that the class you are testing 
uses, but recording and checking which methods are called and returning 
values according to what the programmer explicitly told it to.

Christopher Wright wrote:
 Hello all.
 
 For the Test-Driven Development people out there, and all the fans of 
 unittesting, I'm releasing a mock objects framework for D2, dmocks. It 
 can mock classes and interfaces, even if they are templated; it cannot 
 mock structs due to inheritance issues, and it can only mock virtual 
 methods (no templated methods, sorry; not my fault).
 
 Download:
 http://damask-mud.googlecode.com/files/dmocks.zip
 
 Subversion:
 svn checkout http://damask-mud.googlecode.com/svn/trunk/mocks/
 
 Note that these are temporary locations, and I hope to move the project 
 to dsource shortly.
 
 Examples:
 class Bar {
    int doSomeCalculations (ubyte[] data) { ... }
 }
 class Foo {
    private Bar bar;
    this (Bar bar) { this.bar = bar; }
 
    int calculateStuff (ubyte[] data) {
       int i = bar.doSomeCalculations (data);
       ...
       return i;
    }
 
    unittest {
       Mocker m = new Mocker();
       auto bar = m.Mock!(Bar);
       ubyte[] data = null;
 
       bar.doSomeCalculations(data);
       m.LastCall().Return(5);
       // or m.Expect(bar.doSomeCalculations(data)).Return(5);
 
       m.Replay();
       Foo target = new Foo(bar);
       target.calculateStuff(data);
 
       m.Verify();
    }
 }
 
 Address any complaints you have to dhasenan -at- gmail -dot- com. If 
 anyone has issues with style, I can offer both CamelCase and 
 abridgedCamelCase; if enough people request it, I'll offer a free 
 function interface if I can find a sane way of doing it. Complaints 
 about functionality will be addressed first.
 
 
 The framework in its current state represents about three days' work, 
 which is more a sign of the ease of using D than any special abilities I 
 have.
 
 Dmocks is brought to you by __traits and string mixins. We therefore 
 regret to inform you that it is only available for D version 2.

Nov 07 2007
parent reply Lionello Lunesu <lio lunesu.remove.com> writes:
Christopher Wright wrote:
 For anyone who doesn't know what mock objects are and wants to, there's 
 Wikipedia: http://en.wikipedia.org/wiki/Mock_object

You should add a link to your project on that page's "External links" section! All other languages seem to be already mentioned there. L.
Nov 07 2007
parent Christopher Wright <dhasenan gmail.com> writes:
Lionello Lunesu wrote:
 Christopher Wright wrote:
 For anyone who doesn't know what mock objects are and wants to, 
 there's Wikipedia: http://en.wikipedia.org/wiki/Mock_object

You should add a link to your project on that page's "External links" section! All other languages seem to be already mentioned there. L.

I will, once Brad gets back to me and the project has a permanent home. Though it seems like advertising, if I add the link to my project.
Nov 08 2007