Andrej Mitrovic wrote: > I'd like to turn some attention to unittests, which don't seem to work > with static libraries. Consider: > > .\main.d: > import mylib.test; > void main() > { > auto x = foo(); > } > > .\mylib\test.d > module mylib.test; > int foo() { return 1; } > unittest { assert(0); } > > $ dmd -unittest main.d mylib\test.d && main.exe > core.exception.asserter...@mylib.test(5): unittest failure > > So far so good. It even works if I don't reference foo() from within > main(), even though the "unittest.d" file in Phobos has this comment > about it: > "// Bring in unit test for module by referencing function in it" > > I think that comment might be outdated. > > But now try it with a static library: > > $ cd mylib > $ dmd -unittest -lib test.d > $ cd.. > $ dmd -unittest main.d mylib\test.lib && main.exe > > The unittests from the library don't run. > > There's a bug about this in bugzilla somewhere (I just can't seem to > find it now), but it doesn't seem to be getting much attention. Since > unittests are first-class citizens in D, I really hope we can get this > working soon. Cheers.
I think that it is not a bug, it is a feature (sort of). Could you look at the assembly generated for your main.d file? My guess is that it does not reference foo at all, either because the call to foo was inlined or because it was discarded (since you do not use x after initializing it). The reason it works with the first form is that all object files that are specifically put on the command line are included in the executable when linking even if they are not used. The reason it does not work with the library is that library objects are only included if they are referenced (to save executable size). Of course, that is an issue with unit tests. Not sure how to fix it though, since it is as much a linker issue as a compiler issue, unless there is a way to trick the linker into thinking that any module with unittest is referenced from any other module importing it (when compiling with -unittest) even when there is no other reference (in which case, it will stop being necessary to call a function from the tested module to include the unit tests). Jerome -- mailto:jeber...@free.fr http://jeberger.free.fr Jabber: jeber...@jabber.fr
signature.asc
Description: OpenPGP digital signature