开发者

Getting list of object names in module with template 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).


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)


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜