Unittest helper module?

Robert Fraser fraserofthenight at gmail.com
Fri Aug 31 14:50:24 PDT 2007


Chad J Wrote:

> So I was wondering if anyone has written or is writing something that I 
> could use like so:
> 
> unittest
> {
>    int foo = 42;
>    int bar = 5;
>    char[] baz = "moo";
> 
>    UTester ut; // Unit Tester struct
>    ut.verbose = true;
>    ut.test("foo == bar");
>    ut.test("foo > bar");
>    ut.test("baz.length is 3");
> 
>    assert( ut.allPassed ); // assertion exception
> }
> 
> prints:
> "foo == bar": false.
>    foo == 42
>    bar == 5
> "foo > bar": true.
>    foo == 42
>    bar == 5
> "baz.length is 3": true.
>    baz == "moo"
>    baz.length == 3
> Error: AssertError Failure ...
> 
> 
> 
> At some point I realized I was writing all of my unittest code 2 times. 
>   Once in the form of using the tested code and dumping the output to 
> the console, and then again in the form of using the tested code and 
> comparing the output against expected values.  I initially dump the 
> output to console so that if it fails, I know how so, and sometimes I 
> don't know precisely what the expected value is.  Perhaps 
> ut.show("..."); would be useful too.
> 
> I also gave a short shot at writing this, and realized that it would 
> need a D expression evaluator, which I don't feel like writing right now.
> 
> I could have sworn I talked to someone at D con that was doing unittest 
> stuff.
> 
> Well, anyone have this sorta thing laying around?


Sounds like a great application of macros... Something like this:

macro test(e : A == B)
{
    writefln("%s: %s", e.stringof, e ? "true", "false");
    writefln("   %s = %s", A.stringof, A);
    writefln("   %s = %s", B.stringof, B);
}


More information about the Digitalmars-d-learn mailing list