Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added testProperty.

  • Loading branch information...
commit 83e5e7a470f6f2fdad0b1ea056371ff9274b1ae5 1 parent bf84f5a
Finnsson authored May 11, 2010
24  README.markdown
Source Rendered
@@ -17,16 +17,16 @@ Haskell-module to automagically generate repetetive code when writing HUnit-test
17 17
     -- file SomeModule.hs
18 18
     fooTestGroup = $(testGroupGenerator)
19 19
     main = defaultMain [fooTestGroup]
20  
-    test1 = do 1 @=? 1
21  
-    test2 = do 2 @=? 2
  20
+    case1 = do 1 @=? 1
  21
+    case2 = do 2 @=? 2
22 22
 
23 23
 is the same as
24 24
 
25 25
     -- file SomeModule.hs
26  
-    fooTestGroup = testGroup "SomeModule" [testCase "test1" test1, testCase "test2" test2]
  26
+    fooTestGroup = testGroup "SomeModule" [testCase "case1" case1, testCase "case2" case2]
27 27
     main = defaultMain [fooTestGroup]
28  
-    test1 = do 1 @=? 1
29  
-    test2 = do 2 @=? 2
  28
+    case1 = do 1 @=? 1
  29
+    case2 = do 2 @=? 2
30 30
 
31 31
 ## defaultMainGenerator
32 32
 
@@ -47,9 +47,9 @@ is the same as
47 47
     
48 48
     main = $(defaultMainGenerator)
49 49
    
50  
-    testFoo = do 4 @=? 4
  50
+    caseFoo = do 4 @=? 4
51 51
     
52  
-    testBar = do "hej" @=? "hej"
  52
+    caseBar = do "hej" @=? "hej"
53 53
 
54 54
 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
55 55
 
@@ -60,19 +60,19 @@ will automagically extract testFoo and testBar and run them as well as present t
60 60
     
61 61
     main =
62 62
       defaultMain [
63  
-        testGroup "MyModuleTest" [ testCase "testFoo" testFoo, testCase "testBar" testBar]
  63
+        testGroup "MyModuleTest" [ testCase "caseFoo" caseFoo, testCase "caseBar" caseBar]
64 64
         ]
65 65
     
66  
-    testFoo = do 4 @=? 4
  66
+    caseFoo = do 4 @=? 4
67 67
    
68  
-    testBar = do "hej" @=? "hej"
  68
+    caseBar = do "hej" @=? "hej"
69 69
 
70 70
 and will give the following result
71 71
 
72 72
     me: runghc MyModuleTest.hs 
73 73
     MyModuleTest:
74  
-      testFoo: [OK]
75  
-      testBar: [OK]
  74
+      caseFoo: [OK]
  75
+      caseBar: [OK]
76 76
      
77 77
              Test Cases  Total      
78 78
      Passed  2           2          
45  src/TestGenerator.hs
@@ -23,6 +23,7 @@ import Text.Regex.Posix
23 23
 import Maybe
24 24
 import Language.Haskell.Exts.Extension
25 25
 import Test.Framework.Providers.HUnit
  26
+import Test.Framework.Providers.QuickCheck2
26 27
 import TemplateHelper
27 28
 import Test.Framework (Test)
28 29
 
@@ -41,46 +42,58 @@ import Test.Framework.Providers.HUnit
41 42
 --   > 
42 43
 --   > main = $(defaultMainGenerator)
43 44
 --   >
44  
---   > testFoo = do 4 @=? 4
  45
+--   > caseFoo = do 4 @=? 4
45 46
 --   >
46  
---   > testBar = do "hej" @=? "hej"
  47
+--   > caseBar = do "hej" @=? "hej"
  48
+--   > 
  49
+--   > propReverse xs = reverse (reverse xs) == xs
  50
+--   >   where types = xs :: [Int]
47 51
 --   
48  
---   will automagically extract testFoo and testBar and run them as well as present them as belonging to the testGroup 'MyModuleTest' such as
  52
+--   will automagically extract caseFoo and caseBar and run them as well as present them as belonging to the testGroup 'MyModuleTest' such as
49 53
 --
50 54
 --   > me: runghc MyModuleTest.hs 
51 55
 --   > MyModuleTest:
52  
---   >   testFoo: [OK]
53  
---   >   testBar: [OK]
  56
+--   >   propReverse: [OK, passed 100 tests]
  57
+--   >   caseFoo: [OK]
  58
+--   >   caseBar: [OK]
54 59
 --   > 
55  
---   >          Test Cases  Total      
56  
---   >  Passed  2           2          
57  
---   >  Failed  0           0          
58  
---   >  Total   2           2 
  60
+--   >          Properties  Test Cases   Total       
  61
+--   >  Passed  1           2            3          
  62
+--   >  Failed  0           0            0           
  63
+--   >  Total   1           1            3
  64
+ 
59 65
 --   
60 66
 defaultMainGenerator :: ExpQ
61 67
 defaultMainGenerator = 
62  
-  [| defaultMain [ testGroup $(locationModule) (mapTestCases $(functionExtractor "^test") ) ] |]
  68
+  [| defaultMain [ testGroup $(locationModule) $ $(propListGenerator) ++ (mapTestCases $(functionExtractor "^case") ) ] |]
63 69
 
64 70
 -- | Generate the usual code and extract the usual functions needed for a testGroup in HUnit.
65 71
 --  
66 72
 --   > -- file SomeModule.hs
67 73
 --   > fooTestGroup = $(testGroupGenerator)
68 74
 --   > main = defaultMain [fooTestGroup]
69  
---   > test1 = do 1 @=? 1
70  
---   > test2 = do 2 @=? 2
  75
+--   > case1 = do 1 @=? 1
  76
+--   > case2 = do 2 @=? 2
  77
+--   > prop1 xs = reverse (reverse xs) == xs
  78
+--   >  where types = xs :: [Int]
71 79
 --   
72 80
 --   is the same as
73 81
 --
74 82
 --   > -- file SoomeModule.hs
75  
---   > fooTestGroup = testGroup "SomeModule" [testCase "test1" test1, testCase "test2" test2]
  83
+--   > fooTestGroup = testGroup "SomeModule" [testProperty "prop1" prop1, testCase "case1" case1, testCase "case2" case2]
76 84
 --   > main = defaultMain [fooTestGroup]
77  
---   > test1 = do 1 @=? 1
78  
---   > test2 = do 2 @=? 2
  85
+--   > case1 = do 1 @=? 1
  86
+--   > case2 = do 2 @=? 2
  87
+--   > prop1 xs = reverse (reverse xs) == xs
  88
+--   >  where types = xs :: [Int]
79 89
 --
80 90
 testGroupGenerator :: ExpQ
81 91
 testGroupGenerator =
82  
-  [| testGroup $(locationModule) (mapTestCases $(functionExtractor "^test") ) |]
  92
+  [| testGroup $(locationModule) $ $(propListGenerator) ++ (mapTestCases $(functionExtractor "^case") ) |]
83 93
 
  94
+propListGenerator :: ExpQ
  95
+propListGenerator =
  96
+  functionExtractorMap "^prop" [|testProperty|]
84 97
 
85 98
 mapTestCases :: [(String, Assertion)] -> [Test.Framework.Test]
86 99
 mapTestCases list =
13  src/TestGeneratorTest.hs
@@ -16,11 +16,20 @@ module TestGeneratorTest where
16 16
 import TestGenerator
17 17
 
18 18
 import Test.HUnit
  19
+import TemplateHelper
19 20
 
20 21
 main = $(defaultMainGenerator)
21 22
 
22  
-testFoo =
  23
+case_Foo =
23 24
   do 4 @=? 4
24 25
 
25  
-testBar =
  26
+case_Bar =
26 27
   do "hej" @=? "hej"
  28
+
  29
+prop_Reverse xs = reverse (reverse xs) == xs
  30
+  where types = xs ::[Int]
  31
+
  32
+case_numProp =
  33
+  do let expected = 1
  34
+         actual = length $ $(functionExtractor "^prop")
  35
+     expected @=? actual
18  test-generator.cabal
... ...
@@ -1,35 +1,39 @@
1 1
 name: test-generator
2  
-version: 0.1.0
  2
+version: 0.1.1
3 3
 cabal-version: -any
4 4
 build-type: Simple
5 5
 license: BSD4
6 6
 license-file: ""
7 7
 copyright:
8 8
 maintainer: Oscar Finnsson
9  
-build-depends: base -any, test-framework-hunit >= 0.2.4, test-framework, HUnit, template-helper, haskell-src-exts, haskell98, regex-posix, template-haskell
  9
+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
10 10
 stability:
11 11
 homepage: http://github.com/finnsson/test-generator
12 12
 package-url:
13 13
 bug-reports:
14  
-synopsis: Automagically generate the HUnit-bulk-code using Template Haskell.
  14
+synopsis: Automagically generate the HUnit- and Quickcheck-bulk-code using Template Haskell.
15 15
 description:
16 16
    @test-generator@ contains two interesting functions: @defaultMainGenerator@ and @testGroupGenerator@.
17 17
    .
18  
-   @defaultMainGenerator@ will extract all functions in the module and put them in a testGroup.
  18
+   @defaultMainGenerator@ will extract all functions beginning with case or prop in the module and put them in a testGroup.
19 19
    .
20 20
    > module Foo where
21 21
    > main = $(defaultMainGenerator)
22 22
    >
23  
-   > testTwo = 2 @=? 2
24  
-   > testHi = "hi" @=? "hi"
  23
+   > caseTwo = 2 @=? 2
  24
+   > caseHi = "hi" @=? "hi"
  25
+   > propReverse xs = reverse (reverse xs) == xs
  26
+   >  where types = xs :: [Int]
25 27
   .
26 28
   is the same as
27 29
   .
28 30
   > module Foo where
29  
-  > main = defaultMain [testGroup "Foo" [testCase "testTwo" testTwo, testCase "testHi" testHi]
  31
+  > main = defaultMain [testGroup "Foo" [testProperty "propReverse" propReverse, testCase "testTwo" testTwo, testCase "testHi" testHi]
30 32
   >
31 33
   > testTwo = 2 @=? 2
32 34
   > testHi = "hi" @=? "hi"
  35
+  > propReverse xs = reverse (reverse xs) == xs
  36
+  >  where types = xs :: [Int]
33 37
   .
34 38
   @testGroupGenerator@ is like @defaultMainGenerator@ but without @defaultMain@. It is useful if you need a function for the testgroup
35 39
   (e.g. if you want to be able to call the testgroup from another module).

0 notes on commit 83e5e7a

Please sign in to comment.
Something went wrong with that request. Please try again.