www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Unittest helper module?

reply Chad J <gamerChad _spamIsBad_gmail.com> writes:
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?
Aug 29 2007
next sibling parent reply Nathan Reed <nathaniel.reed gmail.com> writes:
Chad J wrote:
 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.
Couldn't you just use string mixins for this? Thanks, Nathan Reed
Aug 29 2007
parent Chad J <gamerChad _spamIsBad_gmail.com> writes:
Nathan Reed wrote:
 Chad J wrote:
 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.
Couldn't you just use string mixins for this? Thanks, Nathan Reed
Maybe, but I wasn't able to figure out how. If I wasn't interested in the intermediate values, it'd be easy, but I am interested in the intermediate values. This is the problem I run into: consider ut.test("foo( foo(a) & foo(b) ) == 0"); Now I'd like to know what foo(a) and foo(b) evaluate to, as well as what foo(a) & foo(b) evaluates to. So I can just mix that substring in and evaluate it and grab the value. Then I mixin and evaluate the entire contents of foo(foo(a)&foo(b)). This is where the problem starts: foo(a) and foo(b) were just evaluated 2 times, when they should have only been evaluated once. For pure functions this is fine, but functions with side effects could break the unittest if this is done. Using mixins with no analysis causes this foo(...) to be called 10 times, when it should only be called 3 times.
Aug 29 2007
prev sibling parent reply Robert Fraser <fraserofthenight gmail.com> writes:
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); }
Aug 31 2007
parent Chad J <gamerChad _spamIsBad_gmail.com> writes:
Robert Fraser wrote:

 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);
 }
Yep. Just gotta wait for D 3.0 :) (or whatever version it will be)
Aug 31 2007