better assertions and __FILE__ (char[] vs. string)

Bruce Adams ifyoudontknowmebynow at searchdaweb.com
Thu Aug 16 16:47:28 PDT 2007


Okay. So having realised my first idea is not possible with current D (prove me wrong please!). What about the char array versus string
malarcky. Can anyone help with that?

How do I get "foo" to be implicitly interpreted as char[] rather than
char[3]. I though the WYSIWYG thing might do it i.e. r"foo" but it
doesn't. In fact I'm not sure what it does do.

Bruce Adams Wrote:

> 
> 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