better assertions and __FILE__

Bruce Adams tortoise_74 at nospam.ya.hoo.co.mapson.uk
Wed Aug 15 16:03:27 PDT 2007


Hi,
    Another FAQ item I couldn't find. I'm looking to implement some wrappers around assert as assert is too primative and doesn't give me enough information. I want to go down the xUnit path E.g.

in the code:
   assertEqual(foo,bar);

leading to output:
   Equality assertion failed in file.cpp at line 10
   Expected Value: 10
   Actual Value: 5

I got as far as:

void assertEqual(Type)(Type actual_, 
                                Type expected_, 
		            char[] file_, 
		            uint line_) {
   if (actual_ != expected_) {
      writefln("Equality assertion failed");
      writefln("actual:   '", actual_, "'");
      writefln("expected: '", expected_, "'");
      _d_assert(file_,line_);
   }
}

This lets me use:

   assertEqual!(string)("foo","bar",cast(char[])(__FILE__),__LINE__);

Where I'm having trouble is getting __FILE__ and __LINE__ added (presumably using a mixin) so I can avoid the cruft.

As a side issue how do I get string constants implicitly converted to
char[] or better yet string.
In my example above

   assertEqual("foo","bar",cast(char[])(__FILE__),__LINE__);

also works but only because "foo".length == "bar".length

   // broken!
   assertEqual("foo","bar2",cast(char[])(__FILE__),__LINE__);

Final question to prove I'm a noob. In std.asserterror shouldn't 

 _d_assert(char[] file_,uint line_);

actually be:

 _d_assert(string file_,uint line_);

Regards,

Bruce.



More information about the Digitalmars-d-learn mailing list