Unttests for nested functions

BCS BCS_member at pathlink.com
Sat May 27 23:11:12 PDT 2006


OK, after think about it for a while, I have come up with a possible solution. 
It amounts to a small change to the semantics of D, and (maybe, depending on the
implementation) a little quirk in the code generator.

First from the semantic standpoint, allow a unittest inside of a function. Just
like in a normal nested function, the unit test will have access to the
variables in the other function.
Secondly, when implementing this unittest, a fake stack frame is generated for
the function it is nested inside of. That way the unittest can have a context to
call the nested function with. This stack frame could be the first part of the
unittest's  stack frame in such a way the calling the nested function from the
unittest works just like calling it from it's enclosing function.

A purposeless example:

void fn()
{
int i;

void nf()
{
i--;
}
unittest
{
i=2;
nf();
assert(1==i);
}

i=10;
while(i) nf();
}



In article <e5b2u0$1ncl$1 at digitaldaemon.com>, BCS says...
>
>how do I unittest bar?
>
>int foo(int i)
>{
>int j=0;
>
>int bar(int k)  //////////
>{
>j+=k;
>}
>/+	cant make unit test here
>unittest
>{
>bar(0);
>}
>+/
>
>while(i);
>bar(i--);
>
>return j;
>}
>
>/+	cant access nested function here
>unittest
>{
>bar(0);
>}
>+/
>
>
>
>
>





More information about the Digitalmars-d-learn mailing list