How to unittest nested functions
Jonathan M Davis
jmdavisProg at gmx.com
Wed May 9 13:37:57 PDT 2012
On Wednesday, May 09, 2012 22:32:09 Tobias Pankrath wrote:
> How can i write a unittest for bar?
>
> --
>
> int foo()
> {
> int bar()
> {
> return 42;
> }
>
> unittest { // not allowed
> assert(bar() == 42);
> }
>
> return bar();
> }
>
> unittest { // bar not visible
> assert(bar() == 42);
> }
>
> void main() {}
You can't. Either just unit test the outer function or extract the nested
function as a private one outside of the outer one. And if you can't do that
because it's a delegate, then it wouldn't make sense to unit test it anyway,
because it's state relies on the outer function. If the function is major
enough that it needs unit testing, it should probably be external anyway, but
I can see why it might be a bit annoying to have to extract a function just to
unit test it.
- Jonathan M Davis
More information about the Digitalmars-d-learn
mailing list