@@ -10,36 +10,29 @@ module Main where
10
10
11
11
import Basics
12
12
13
- import Text.Printf ( printf )
13
+ import Test.Hspec
14
14
import Test.QuickCheck
15
- import Test.QuickCheck.Test (isSuccess )
16
- import System.Exit (exitSuccess , exitFailure )
17
15
\end {code}
18
16
19
- The tuples in the following list consist of a name for the test and a
20
- quickCheckResult applied to a property. For example, we check that the output
21
- of funnyfunction is one of three possibilities. The second test checks that
22
- reversing twice gives the same string back.
23
-
24
- % TODO: add tests using thenumbers, somenumbers, randomnumbers
17
+ The following uses the HSpec library to define different tests.
18
+ Note that the first test is a specific test with fixed inputs.
19
+ The second and third test use QuickCheck.
25
20
26
21
\begin {code}
27
- tests :: [(String , IO Result )]
28
- tests =
29
- [ (" funnytest1" , quickCheckResult (\ n -> funnyfunction n `elem` [42 , n* 100 , (n- 1 )* 100 ]) )
30
- , (" reversetest" , quickCheckResult (\ str -> myreverse (myreverse str) == (str:: String )) )
31
- ]
32
-
33
22
main :: IO ()
34
- main = do
35
- results <- mapM (\ (s,a) -> printf " \n %-20s: " s >> a) tests
36
- if all isSuccess results
37
- then exitSuccess
38
- else exitFailure
23
+ main = hspec $ do
24
+ describe " Basics" $ do
25
+ it " somenumbers should be the same as [1..10]" $
26
+ somenumbers `shouldBe` [1 .. 10 ]
27
+ it " funnyfunction: result is within [1..100]" $
28
+ property (\ n -> funnyfunction n `elem` [1 .. 100 ])
29
+ it " myreverse: using it twice gives back the same list" $
30
+ property $ \ str -> myreverse (myreverse str) == (str:: String )
39
31
\end {code}
40
32
41
- To run this, use \verb |stack clean && stack test --coverage |. In particular
42
- this will generate a nice report using hpc. Look for `` The coverage report
43
- for ... is available at ... .html'' and open this html file in your browser.
44
- See \url {https://wiki.haskell.org/Haskell_program_coverage} for more examples
45
- and information how to read the report.
33
+ To run the tests, use \verb |stack test |.
34
+
35
+ To also find out which part of your program is actually used for these tests,
36
+ run \verb |stack clean && stack test --coverage |. Then look for `` The coverage
37
+ report for ... is available at ... .html'' and open this file in your browser.
38
+ See also: \url {https://wiki.haskell.org/Haskell_program_coverage}.
0 commit comments