unittesting generic functions

Timon Gehr via Digitalmars-d digitalmars-d at puremagic.com
Thu Aug 14 11:39:08 PDT 2014


On 08/14/2014 08:26 PM, bearophile wrote:
> Andrei Alexandrescu:
>> Destroy https://issues.dlang.org/show_bug.cgi?id=13291?
>
> I'd like a way to test nested functions:
>
>
> void foo() {
>      int bar() { return 0; }
>
>      unittest {
>          assert(bar() == 1);
>      }
> }
>
> void main() {}
>
>
> Bye,
> bearophile

It is already possible, but I doubt that this is intended (where would 
the unit test get the enclosing stack frame of 'foo' from?):

void foo(){
     int bar(){ return 0; }
     mixin barTest;
}
template barTest(){
     unittest {
         assert(bar()==1);
     }
}
void main() {}

You should be safe for static nested functions though, which the 
compiler will enforce are the only ones called if you make the unit test 
'static'. :o)


More information about the Digitalmars-d mailing list