Some questions

Bill Baxter dnewsgroup at billbaxter.com
Wed Aug 1 13:19:25 PDT 2007


bearophile wrote:
> 1a/1b) I am developing a package of some modules (using the really
> useful "bud") each one of them contains some unittests. I'd like a way
> to just ignore the main() function eventually present inside imported
> modules (or maybe to allow importing and using it as modulename.main()).
> So they can be used both as main modules and imported modules (Python
> allows something similar, using a different syntax). 

Not a great solution, but can put main() inside a version block in each 
file like:

version(MAIN) {
    void main() {
       //...
    }
}

Then when you compile, pass -version=MAIN to the compiler for the one 
file you want to be main.

Related: if I have
> many modules, I don't want to unittest them all all the time, I'd like
> to unittest only the main module I am developing in that moment. Maybe
> you can suggests me ways to solve those two problems in a good way with
> no changes to the D language :-) (I currently comment out the main() of
> all the modules I am not developing at that moment, and I wait for the
> unittests of all the modules, but those aren't good solutions).


Probably 'version' is again your only hope.  Maybe you can have a 
version for each module, like TEST_{MODULENAME}, and then one that does 
everything: TEST_ALL.

I haven't used version much but you should be able to do something like 
this in each module:

----mymodule.d----
module mymodule;
version(TEST_ALL) {
    version = TEST_MYMODULE;
}

version(TEST_MYMODULE)  {
unittest {
     ...
}
}
----------

And as for main(), I find it useful to have a dummy module with nothing 
but an empty main to compile in when doing unit tests of a library.  Like:

----testmain.d----
module testmain;
void main() {}
-----------

Just throw that file into the mix when you want to do unit testing of 
some files that don't have their own main() already.

It's kinda silly to have to do that.  D should really be smart enough to 
know that if I'm asking for -unittest, then I'm going to want a main.

--bb


More information about the Digitalmars-d-learn mailing list