Blame examples/Example.hs
|
Packit |
bc3140 |
-- Example.hs -- Examples from HUnit user's guide
|
|
Packit |
bc3140 |
--
|
|
Packit |
bc3140 |
-- For more examples, check out the tests directory. It contains unit tests
|
|
Packit |
bc3140 |
-- for HUnit.
|
|
Packit |
bc3140 |
|
|
Packit |
bc3140 |
module Example where
|
|
Packit |
bc3140 |
|
|
Packit |
bc3140 |
import Test.HUnit
|
|
Packit |
bc3140 |
|
|
Packit |
bc3140 |
|
|
Packit |
bc3140 |
foo :: Int -> (Int, Int)
|
|
Packit |
bc3140 |
foo x = (1, x)
|
|
Packit |
bc3140 |
|
|
Packit |
bc3140 |
partA :: Int -> IO (Int, Int)
|
|
Packit |
bc3140 |
partA v = return (v+2, v+3)
|
|
Packit |
bc3140 |
|
|
Packit |
bc3140 |
partB :: Int -> IO Bool
|
|
Packit |
bc3140 |
partB v = return (v > 5)
|
|
Packit |
bc3140 |
|
|
Packit |
bc3140 |
test1 :: Test
|
|
Packit |
bc3140 |
test1 = TestCase (assertEqual "for (foo 3)," (1,2) (foo 3))
|
|
Packit |
bc3140 |
|
|
Packit |
bc3140 |
test2 :: Test
|
|
Packit |
bc3140 |
test2 = TestCase (do (x,y) <- partA 3
|
|
Packit |
bc3140 |
assertEqual "for the first result of partA," 5 x
|
|
Packit |
bc3140 |
b <- partB y
|
|
Packit |
bc3140 |
assertBool ("(partB " ++ show y ++ ") failed") b)
|
|
Packit |
bc3140 |
|
|
Packit |
bc3140 |
tests :: Test
|
|
Packit |
bc3140 |
tests = TestList [TestLabel "test1" test1, TestLabel "test2" test2]
|
|
Packit |
bc3140 |
|
|
Packit |
bc3140 |
tests' :: Test
|
|
Packit |
bc3140 |
tests' = test [ "test1" ~: "(foo 3)" ~: (1,2) ~=? (foo 3),
|
|
Packit |
bc3140 |
"test2" ~: do (x, y) <- partA 3
|
|
Packit |
bc3140 |
assertEqual "for the first result of partA," 5 x
|
|
Packit |
bc3140 |
partB y @? "(partB " ++ show y ++ ") failed" ]
|
|
Packit |
bc3140 |
|
|
Packit |
bc3140 |
main :: IO Counts
|
|
Packit |
bc3140 |
main = do _ <- runTestTT tests
|
|
Packit |
bc3140 |
runTestTT tests'
|