Permalink
Browse files

Added testProperty.

  • Loading branch information...
1 parent bf84f5a commit 83e5e7a470f6f2fdad0b1ea056371ff9274b1ae5 @finnsson committed May 11, 2010
Showing with 63 additions and 37 deletions.
  1. +12 −12 README.markdown
  2. +29 −16 src/TestGenerator.hs
  3. +11 −2 src/TestGeneratorTest.hs
  4. +11 −7 test-generator.cabal
View
24 README.markdown
@@ -17,16 +17,16 @@ Haskell-module to automagically generate repetetive code when writing HUnit-test
-- file SomeModule.hs
fooTestGroup = $(testGroupGenerator)
main = defaultMain [fooTestGroup]
- test1 = do 1 @=? 1
- test2 = do 2 @=? 2
+ case1 = do 1 @=? 1
+ case2 = do 2 @=? 2
is the same as
-- file SomeModule.hs
- fooTestGroup = testGroup "SomeModule" [testCase "test1" test1, testCase "test2" test2]
+ fooTestGroup = testGroup "SomeModule" [testCase "case1" case1, testCase "case2" case2]
main = defaultMain [fooTestGroup]
- test1 = do 1 @=? 1
- test2 = do 2 @=? 2
+ case1 = do 1 @=? 1
+ case2 = do 2 @=? 2
## defaultMainGenerator
@@ -47,9 +47,9 @@ is the same as
main = $(defaultMainGenerator)
- testFoo = do 4 @=? 4
+ caseFoo = do 4 @=? 4
- testBar = do "hej" @=? "hej"
+ caseBar = do "hej" @=? "hej"
will automagically extract testFoo and testBar and run them as well as present them as belonging to the testGroup 'MyModuleTest'. The above code is the same as
@@ -60,19 +60,19 @@ will automagically extract testFoo and testBar and run them as well as present t
main =
defaultMain [
- testGroup "MyModuleTest" [ testCase "testFoo" testFoo, testCase "testBar" testBar]
+ testGroup "MyModuleTest" [ testCase "caseFoo" caseFoo, testCase "caseBar" caseBar]
]
- testFoo = do 4 @=? 4
+ caseFoo = do 4 @=? 4
- testBar = do "hej" @=? "hej"
+ caseBar = do "hej" @=? "hej"
and will give the following result
me: runghc MyModuleTest.hs
MyModuleTest:
- testFoo: [OK]
- testBar: [OK]
+ caseFoo: [OK]
+ caseBar: [OK]
Test Cases Total
Passed 2 2
View
45 src/TestGenerator.hs
@@ -23,6 +23,7 @@ import Text.Regex.Posix
import Maybe
import Language.Haskell.Exts.Extension
import Test.Framework.Providers.HUnit
+import Test.Framework.Providers.QuickCheck2
import TemplateHelper
import Test.Framework (Test)
@@ -41,46 +42,58 @@ import Test.Framework.Providers.HUnit
-- >
-- > main = $(defaultMainGenerator)
-- >
--- > testFoo = do 4 @=? 4
+-- > caseFoo = do 4 @=? 4
-- >
--- > testBar = do "hej" @=? "hej"
+-- > caseBar = do "hej" @=? "hej"
+-- >
+-- > propReverse xs = reverse (reverse xs) == xs
+-- > where types = xs :: [Int]
--
--- will automagically extract testFoo and testBar and run them as well as present them as belonging to the testGroup 'MyModuleTest' such as
+-- will automagically extract caseFoo and caseBar 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]
+-- > propReverse: [OK, passed 100 tests]
+-- > caseFoo: [OK]
+-- > caseBar: [OK]
-- >
--- > Test Cases Total
--- > Passed 2 2
--- > Failed 0 0
--- > Total 2 2
+-- > Properties Test Cases Total
+-- > Passed 1 2 3
+-- > Failed 0 0 0
+-- > Total 1 1 3
+
--
defaultMainGenerator :: ExpQ
defaultMainGenerator =
- [| defaultMain [ testGroup $(locationModule) (mapTestCases $(functionExtractor "^test") ) ] |]
+ [| defaultMain [ testGroup $(locationModule) $ $(propListGenerator) ++ (mapTestCases $(functionExtractor "^case") ) ] |]
-- | 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
+-- > case1 = do 1 @=? 1
+-- > case2 = do 2 @=? 2
+-- > prop1 xs = reverse (reverse xs) == xs
+-- > where types = xs :: [Int]
--
-- is the same as
--
-- > -- file SoomeModule.hs
--- > fooTestGroup = testGroup "SomeModule" [testCase "test1" test1, testCase "test2" test2]
+-- > fooTestGroup = testGroup "SomeModule" [testProperty "prop1" prop1, testCase "case1" case1, testCase "case2" case2]
-- > main = defaultMain [fooTestGroup]
--- > test1 = do 1 @=? 1
--- > test2 = do 2 @=? 2
+-- > case1 = do 1 @=? 1
+-- > case2 = do 2 @=? 2
+-- > prop1 xs = reverse (reverse xs) == xs
+-- > where types = xs :: [Int]
--
testGroupGenerator :: ExpQ
testGroupGenerator =
- [| testGroup $(locationModule) (mapTestCases $(functionExtractor "^test") ) |]
+ [| testGroup $(locationModule) $ $(propListGenerator) ++ (mapTestCases $(functionExtractor "^case") ) |]
+propListGenerator :: ExpQ
+propListGenerator =
+ functionExtractorMap "^prop" [|testProperty|]
mapTestCases :: [(String, Assertion)] -> [Test.Framework.Test]
mapTestCases list =
View
13 src/TestGeneratorTest.hs
@@ -16,11 +16,20 @@ module TestGeneratorTest where
import TestGenerator
import Test.HUnit
+import TemplateHelper
main = $(defaultMainGenerator)
-testFoo =
+case_Foo =
do 4 @=? 4
-testBar =
+case_Bar =
do "hej" @=? "hej"
+
+prop_Reverse xs = reverse (reverse xs) == xs
+ where types = xs ::[Int]
+
+case_numProp =
+ do let expected = 1
+ actual = length $ $(functionExtractor "^prop")
+ expected @=? actual
View
18 test-generator.cabal
@@ -1,35 +1,39 @@
name: test-generator
-version: 0.1.0
+version: 0.1.1
cabal-version: -any
build-type: Simple
license: BSD4
license-file: ""
copyright:
maintainer: Oscar Finnsson
-build-depends: base -any, test-framework-hunit >= 0.2.4, test-framework, HUnit, template-helper, haskell-src-exts, haskell98, regex-posix, template-haskell
+build-depends: base >= 4, test-framework-hunit >= 0.2.4, test-framework-quickcheck2 >= 0.2.4, test-framework, HUnit, template-helper, haskell-src-exts, haskell98, regex-posix, template-haskell
stability:
homepage: http://github.com/finnsson/test-generator
package-url:
bug-reports:
-synopsis: Automagically generate the HUnit-bulk-code using Template Haskell.
+synopsis: Automagically generate the HUnit- and Quickcheck-bulk-code using Template Haskell.
description:
@test-generator@ contains two interesting functions: @defaultMainGenerator@ and @testGroupGenerator@.
.
- @defaultMainGenerator@ will extract all functions in the module and put them in a testGroup.
+ @defaultMainGenerator@ will extract all functions beginning with case or prop in the module and put them in a testGroup.
.
> module Foo where
> main = $(defaultMainGenerator)
>
- > testTwo = 2 @=? 2
- > testHi = "hi" @=? "hi"
+ > caseTwo = 2 @=? 2
+ > caseHi = "hi" @=? "hi"
+ > propReverse xs = reverse (reverse xs) == xs
+ > where types = xs :: [Int]
.
is the same as
.
> module Foo where
- > main = defaultMain [testGroup "Foo" [testCase "testTwo" testTwo, testCase "testHi" testHi]
+ > main = defaultMain [testGroup "Foo" [testProperty "propReverse" propReverse, testCase "testTwo" testTwo, testCase "testHi" testHi]
>
> testTwo = 2 @=? 2
> testHi = "hi" @=? "hi"
+ > propReverse xs = reverse (reverse xs) == xs
+ > where types = xs :: [Int]
.
@testGroupGenerator@ is like @defaultMainGenerator@ but without @defaultMain@. It is useful if you need a function for the testgroup
(e.g. if you want to be able to call the testgroup from another module).

0 comments on commit 83e5e7a

Please sign in to comment.