I don't get it. version(unittest) can't seem to use local variable

Rikki Cattermole via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Jul 12 19:45:34 PDT 2014


On 13/07/2014 2:35 p.m., dysmondad wrote:
> .....
>>
>> try:
>>
>> unittest {
>>       Velocity v = new Velocity( 2.0f, 5.0f );
>>       v *= 5.0f;  // <- line 110
>>       printf( "v = %s\n", to!string(v) );
>> }
>>
>> instead. Basically version is like static if, it doesn't indicate its
>> a function. Instead it changes what code gets compiled.
>> Where as a unittest block, is essentially just a function that gets
>> called at a special time, that is only compiled as if
>> version(unittest) was also used.
>
> Thank you very much. That was quite helpful.
>
> So, what is the generally accepted way include unit testing? TDD is all
> the rage these days and I though I would try my hand at it as well as D.

Basically, have a function that does something like string manipulation 
or complex math? Use a unittest block for it. Which you can do before 
you write the function. It may not compile however.

Also do note, assert's are part of the language. And will cause an 
exception to throw should the expression return false.

assert(exp, "description");

Description is optional however.

Here's an example 
https://github.com/rikkimax/skeleton/blob/master/source/skeleton/syntax/download_mkdir.d#L175
Of course there will be better ones, just something I was doing last 
night however.


More information about the Digitalmars-d-learn mailing list