Unit testing with asserts: Why is assertHandler required to throw?

Trip Volpe mraccident at gmail.com
Sun Jan 31 16:05:11 PST 2010


Lutger Wrote:
> 
> You can use line and file info with default arguments, this works 
> (special case). I just hack around the default unittest system, 
> something like this:
> 
> void test(string testName)(void delegate () testClosure,
>                             int line = __LINE__,
>                             string file = __FILE__)
> {
>      // register test start, run testClosure, end test
> }
> 
> void expectEquals(A, B)(A a, B b,
>                          int line = __LINE__,
>                          string file = __FILE__)
> {
>      // record outcome of assertion
> }

Ah ha! I din't know that D had __LINE__ and __FILE__. I thought those had been done away with along with the preprocessor. It looks like they're a lot smarter than in C as well, since this works just fine:

test.d:
1   void main() {
2      printlocation();    // prints "test.d:2" (as opposed to "test.d:5")
3   }
4   
5   void printlocation( string file = __FILE__, int line = __LINE__ ) {
6      writefln( "%s:%d", file, line );
7   }

This still feels a little more hacky than I'd like, but it should do. Thanks! :-)




More information about the Digitalmars-d mailing list