Skip to content
Browse files

Changed name from main-test-generator to test-generator

  • Loading branch information...
1 parent 67ae6f6 commit 8efe5c2023eaefb7abc85b01c343619aebba570e @finnsson committed Oct 12, 2009
Showing with 159 additions and 0 deletions.
  1. +87 −0 src/TestGenerator.hs
  2. +26 −0 src/TestGeneratorTest.hs
  3. +46 −0 test-generator.cabal
View
87 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
View
26 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"
View
46 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.
Something went wrong with that request. Please try again.