Skip to content

Commit

Permalink
Changed name from main-test-generator to test-generator
Browse files Browse the repository at this point in the history
  • Loading branch information
finnsson committed Oct 12, 2009
1 parent 67ae6f6 commit 8efe5c2
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 0 deletions.
87 changes: 87 additions & 0 deletions src/TestGenerator.hs
@@ -0,0 +1,87 @@
-----------------------------------------------------------------------------
--
-- Module : MainTestGenerator
-- Copyright :
-- License : BSD4
--
-- Maintainer : Oscar Finnsson
-- Stability :
-- Portability :
--
--
-----------------------------------------------------------------------------
{-# OPTIONS_GHC -fglasgow-exts -XTemplateHaskell #-}

module TestGenerator (
defaultMainGenerator,
testGroupGenerator
) where
import Language.Haskell.TH
import Language.Haskell.Exts.Parser
import Language.Haskell.Exts.Syntax
import Text.Regex.Posix
import Maybe
import Language.Haskell.Exts.Extension
import Test.Framework.Providers.HUnit
import FunctionExtractor
import Test.Framework (Test)

import Test.HUnit


import Test.Framework (defaultMain, testGroup)
import Test.Framework.Providers.HUnit

-- | Generate the usual code and extract the usual functions needed in order to run HUnit.
--
-- > {-# OPTIONS_GHC -fglasgow-exts -XTemplateHaskell #-}
-- > module MyModuleTest where
-- > import Test.HUnit
-- > import MainTestGenerator
-- >
-- > main = $(defaultMainGenerator)
-- >
-- > testFoo = do 4 @=? 4
-- >
-- > testBar = do "hej" @=? "hej"
--
-- will automagically extract testFoo and testBar and run them as well as present them as belonging to the testGroup 'MyModuleTest' such as
--
-- > me: runghc MyModuleTest.hs
-- > MyModuleTest:
-- > testFoo: [OK]
-- > testBar: [OK]
-- >
-- > Test Cases Total
-- > Passed 2 2
-- > Failed 0 0
-- > Total 2 2
--
defaultMainGenerator :: ExpQ
defaultMainGenerator =
[| defaultMain [ testGroup $(locationModule) (mapTestCases $(functionExtractor "^test") ) ] |]

-- | Generate the usual code and extract the usual functions needed for a testGroup in HUnit.
--
-- > -- file SomeModule.hs
-- > fooTestGroup = $(testGroupGenerator)
-- > main = defaultMain [fooTestGroup]
-- > test1 = do 1 @=? 1
-- > test2 = do 2 @=? 2
--
-- is the same as
--
-- > -- file SoomeModule.hs
-- > fooTestGroup = testGroup "SomeModule" [testCase "test1" test1, testCase "test2" test2]
-- > main = defaultMain [fooTestGroup]
-- > test1 = do 1 @=? 1
-- > test2 = do 2 @=? 2
--
testGroupGenerator :: ExpQ
testGroupGenerator =
[| testGroup $(locationModule) (mapTestCases $(functionExtractor "^test") ) |]


mapTestCases :: [(String, Assertion)] -> [Test.Framework.Test]
mapTestCases list =
map (uncurry testCase) list
26 changes: 26 additions & 0 deletions src/TestGeneratorTest.hs
@@ -0,0 +1,26 @@
-----------------------------------------------------------------------------
--
-- Module : TestGenerator
-- Copyright :
-- License : BSD4
--
-- Maintainer : Oscar Finnsson
-- Stability :
-- Portability :
--
-- |
--
-----------------------------------------------------------------------------
{-# OPTIONS_GHC -fglasgow-exts -XTemplateHaskell #-}
module TestGeneratorTest where
import TestGenerator

import Test.HUnit

main = $(defaultMainGenerator)

testFoo =
do 4 @=? 4

testBar =
do "hej" @=? "hej"
46 changes: 46 additions & 0 deletions test-generator.cabal
@@ -0,0 +1,46 @@
name: test-generator
version: 0.0.1
cabal-version: -any
build-type: Simple
license: BSD4
license-file: ""
copyright:
maintainer: Oscar Finnsson
build-depends: base -any, test-framework-hunit, test-framework, HUnit, function-extractor, haskell-src-exts, haskell98, regex-posix, template-haskell
stability:
homepage:
package-url:
bug-reports:
synopsis:
description: Automagically generated the HUnit-bulk-code.
category:
author: Oscar Finnsson & Emil Nordling
tested-with:
data-files:
data-dir: ""
extra-source-files:
extra-tmp-files:
exposed-modules: TestGenerator
exposed: True
buildable: True
build-tools:
cpp-options:
cc-options:
ld-options:
pkgconfig-depends:
frameworks:
c-sources:
extensions:
extra-libraries:
extra-lib-dirs:
includes:
install-includes:
include-dirs:
hs-source-dirs: src
other-modules:
ghc-prof-options:
ghc-shared-options:
ghc-options:
hugs-options:
nhc98-options:
jhc-options:

0 comments on commit 8efe5c2

Please sign in to comment.