Writing a unit test for a singleton implementation

Idan Arye GenericNPC at gmail.com
Fri May 17 12:34:27 PDT 2013


On Friday, 17 May 2013 at 10:16:21 UTC, Dmitry Olshansky wrote:
> Can you show the code actually - bogus singleton and correct 
> one?

Here: https://gist.github.com/someboddy/5601276
I thinned down the implementation to a minimum working example.

`enum BREAK_IMPLEMENTATION` at the top of the file decides 
between using the broken implementation and the correct one. I 
put the unit test under `main()` so you can compile it without 
the -unittest flag.

The reason that `Foo` is declared at global scope is a bug in dmd 
that was fixed for the next release(can't find it at Bugzilla, 
but it's fixed in dmd master). If I declare it at the unittest's 
or in main's scope, the constructor won't be invoked and 
`sleep()` won't be called.


More information about the Digitalmars-d mailing list