Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting list of object names in module with template haskell?

Tags:

haskell

I'd like to be able to take a file with declarations such as:

test_1 = assert $ 1 == 1
test_2 = assert $ 2 == 1

and generate a basic run function like

main = runTests [test_1, test2]

The goal is to get something like Python's nosetest.

Can I do this with template Haskell? I cannot find a lot of documentation on it (there are many broken links in the Wiki).

like image 933
luispedro Avatar asked Dec 05 '10 21:12

luispedro


2 Answers

You might want to look into the test-framework family of packages. In particular, the test-framework-th package provides the Template Haskell function defaultMainGenerator which does exactly what you want for both QuickCheck and HUnit tests, as long as you follow the convention of prefixing HUnit test cases with case_ and QuickCheck properties with prop_.

{-# LANGUAGE TemplateHaskell #-}

import Test.Framework.Providers.HUnit
import Test.Framework.Providers.QuickCheck2
import Test.Framework.TH

import Test.HUnit
import Test.QuickCheck

main = $(defaultMainGenerator)

case_checkThatHUnitWorks =
    assert $ 1 == 1

prop_checkThatQuickCheckWorks =
    (1 == 1)
like image 171
hammar Avatar answered Nov 10 '22 13:11

hammar


There is another way, you don't have to use template haskell. haskell-src-exts can parse Haskell, and you could extract from that.

Or if your purpose is practical, you can make like quickcheck and do a simple-minded parse, i.e. looking for identifiers that start with prop_ in column 0. This is a perfectly adequate solution for real work, though it may be theoretically unsatisfying.

like image 44
luqui Avatar answered Nov 10 '22 13:11

luqui