OCaml supports static linking and dynamic module loading; what you usually do (and what a safe type is) is static binding. I would recommend only dynamic loading if you need some kind of plugin architecture.
In any case, a library is nothing more than a module (possibly with submodules). If you statically link a module, all the "main" routines will be executed in the order of the modules in your executable.
So, if you are not doing anything, the module does not know which executable file it is connected in any “magic” way; what you should do is:
- move tests from modules, perhaps using ounit OR
- at least rewrite your test functions as real functions, for example. "let test () = ..."; then write a test interface that will call all the "test" functions from all of your modules.
Application:
If you do this in other languages, then there seems to be no free cake:
In Java, if there are several network components in the code, you must explicitly choose the one you want to run the executable file.
In C, you can use the C preprocessor to do something like
#ifdef TEST_1 int main() { ... } #endif
OCaml has its own camlp4 preprocessor ( camlp4 wikipedia article ), with which you could do something like this. I personally think that such a test implementation is bad programming. You better check your module / class / .. from the interface side and mark your internal invariants with statements (which exist in Java, C and OCaml).
lambdapower
source share