Skip to content

Commit

Permalink
Merge pull request #8 from dag/testGroup
Browse files Browse the repository at this point in the history
Support custom test groups, closes #7

Thanks for the pull request. I'll upload a new version to HackageDB.
  • Loading branch information
finnsson committed Dec 2, 2012
2 parents f56f90e + bb35bad commit 4439f31
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
27 changes: 19 additions & 8 deletions src/Test/Framework/TH.hs
Expand Up @@ -28,7 +28,7 @@ import Language.Haskell.Extract
import Test.Framework (defaultMain, testGroup)

-- | Generate the usual code and extract the usual functions needed in order to run HUnit/Quickcheck/Quickcheck2.
-- All functions beginning with case_ or prop_ will be extracted.
-- All functions beginning with case_, prop_ or test_ will be extracted.
--
-- > {-# OPTIONS_GHC -fglasgow-exts -XTemplateHaskell #-}
-- > module MyModuleTest where
Expand All @@ -43,31 +43,39 @@ import Test.Framework (defaultMain, testGroup)
-- >
-- > prop_Reverse xs = reverse (reverse xs) == xs
-- > where types = xs :: [Int]
-- >
-- > test_Group =
-- > [ testCase "1" case_Foo
-- > , testProperty "2" prop_Reverse
-- > ]
--
-- will automagically extract prop_Reverse, case_Foo and case_Bar and run them as well as present them as belonging to the testGroup 'MyModuleTest' such as
-- will automagically extract prop_Reverse, case_Foo, case_Bar and test_Group and run them as well as present them as belonging to the testGroup 'MyModuleTest' such as
--
-- > me: runghc MyModuleTest.hs
-- > MyModuleTest:
-- > Reverse: [OK, passed 100 tests]
-- > Foo: [OK]
-- > Bar: [OK]
-- > Group:
-- > 1: [OK]
-- > 2: [OK, passed 100 tests]
-- >
-- > Properties Test Cases Total
-- > Passed 1 2 3
-- > Passed 2 3 5
-- > Failed 0 0 0
-- > Total 1 1 3
-- > Total 2 3 5

--
defaultMainGenerator :: ExpQ
defaultMainGenerator =
[| defaultMain [ testGroup $(locationModule) $ $(propListGenerator) ++ $(caseListGenerator) ] |]
[| defaultMain [ testGroup $(locationModule) $ $(propListGenerator) ++ $(caseListGenerator) ++ $(testListGenerator) ] |]

defaultMainGenerator2 :: ExpQ
defaultMainGenerator2 =
[| defaultMain [ testGroup $(locationModule) $ $(caseListGenerator) ++ $(propListGenerator) ] |]
[| defaultMain [ testGroup $(locationModule) $ $(caseListGenerator) ++ $(propListGenerator) ++ $(testListGenerator) ] |]

-- | Generate the usual code and extract the usual functions needed for a testGroup in HUnit/Quickcheck/Quickcheck2.
-- All functions beginning with case_ or prop_ will be extracted.
-- All functions beginning with case_, prop_ or test_ will be extracted.
--
-- > -- file SomeModule.hs
-- > fooTestGroup = $(testGroupGenerator)
Expand All @@ -89,7 +97,7 @@ defaultMainGenerator2 =
--
testGroupGenerator :: ExpQ
testGroupGenerator =
[| testGroup $(locationModule) $ $(propListGenerator) ++ $(caseListGenerator) |]
[| testGroup $(locationModule) $ $(propListGenerator) ++ $(caseListGenerator) ++ $(testListGenerator) |]

listGenerator :: String -> String -> ExpQ
listGenerator beginning funcName =
Expand All @@ -101,6 +109,9 @@ propListGenerator = listGenerator "^prop_" "testProperty"
caseListGenerator :: ExpQ
caseListGenerator = listGenerator "^case_" "testCase"

testListGenerator :: ExpQ
testListGenerator = listGenerator "^test_" "testGroup"

-- | The same as
-- e.g. \n f -> testProperty (fixName n) f
applyNameFix :: String -> ExpQ
Expand Down
6 changes: 6 additions & 0 deletions src/Test/Framework/TestTH.hs
@@ -1,5 +1,6 @@
{-# OPTIONS_GHC -XTemplateHaskell #-}
module Test.Framework.TestTH where
import Test.Framework
import Test.Framework.TH

import Test.HUnit
Expand All @@ -22,3 +23,8 @@ case_num_Prop =
do let expected = 1
actual = length $ $(functionExtractor "^prop")
expected @=? actual

test_Group =
[ testCase "1" case_Foo
, testProperty "2" prop_Reverse
]
2 changes: 1 addition & 1 deletion test-framework-th.cabal
Expand Up @@ -10,7 +10,7 @@ synopsis: Automagically generate the HUnit- and Quickcheck-bulk-code using Templ
description:
@test-framework-th@ contains two interesting functions: @defaultMainGenerator@ and @testGroupGenerator@.
.
@defaultMainGenerator@ will extract all functions beginning with case_ or prop_ in the module and put them in a testGroup.
@defaultMainGenerator@ will extract all functions beginning with case_, prop_ or test_in the module and put them in a testGroup.
.
> -- file SomeModule.hs
> ( -# LANGUAGE TemplateHaskell #- )
Expand Down

0 comments on commit 4439f31

Please sign in to comment.