main modules in GHC, deriving differences between GHC and Hugs

Matthew Donadio m.p.donadio@ieee.org
Fri, 13 Jun 2003 15:47:09 -0400


Graham Klyne wrote:
> GHC seems to require a 'main' module in a file in order to generate an exe
> file.   This makes it awkward to create unit test programs, because I
> generally create one (to run stand-alone) for each major module.  Now I
> want to create a "master test" module that runs all the individual module
> tests.  But if the module tests are all "main" modules it seems I cannot
> bring them all together into a larger program.  Have I overlooked any way
> to create an executable program from any module containing a main function
> of the appropriate type?

The easiest way to handle this is to run all the source through the C
preprocessor, and put #ifdef's around main and the module name. 
Something like

#ifdef UNIT_TEST
module Main where
#else
module Foo where
#endif

foo x y = x + y

tests = [ foo 2 3 == 5, 
          foo 3 4 /= 6
        ]

#ifdef UNIT_TEST
main = print $ and tests
#else
foo_test = and tests
#endif

I haven't done this with Haskell, but I have done it with a lot of my C
libraries.

-- 
Matthew Donadio (m.p.donadio@ieee.org)