1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
|
HUnitTestBase.lhs -- test support and basic tests (Haskell 98 compliant)
> module HUnitTestBase where
> import Test.HUnit
> import Test.HUnit.Terminal (terminalAppearance)
> import System.IO (IOMode(..), openFile, hClose)
> data Report = Start State
> | Error String State
> | UnspecifiedError State
> | Failure String State
> deriving (Show, Read)
> instance Eq Report where
> Start s1 == Start s2 = s1 == s2
> Error m1 s1 == Error m2 s2 = m1 == m2 && s1 == s2
> Error m1 s1 == UnspecifiedError s2 = s1 == s2
> UnspecifiedError s1 == Error m2 s2 = s1 == s2
> UnspecifiedError s1 == UnspecifiedError s2 = s1 == s2
> Failure m1 s1 == Failure m2 s2 = m1 == m2 && s1 == s2
> _ == _ = False
> expectReports :: [Report] -> Counts -> Test -> Test
> expectReports reports counts test = TestCase $ do
> (counts', reports') <- performTest (\ ss us -> return (Start ss : us))
> (\m ss us -> return (Error m ss : us))
> (\m ss us -> return (Failure m ss : us))
> [] test
> assertEqual "for the reports from a test," reports (reverse reports')
> assertEqual "for the counts from a test," counts counts'
> simpleStart = Start (State [] (Counts 1 0 0 0))
> expectSuccess :: Test -> Test
> expectSuccess = expectReports [simpleStart] (Counts 1 1 0 0)
> expectProblem :: (String -> State -> Report) -> Int -> String -> Test -> Test
> expectProblem kind err msg =
> expectReports [simpleStart, kind msg (State [] counts)] counts
> where counts = Counts 1 1 err (1-err)
> expectError, expectFailure :: String -> Test -> Test
> expectError = expectProblem Error 1
> expectFailure = expectProblem Failure 0
> expectUnspecifiedError :: Test -> Test
> expectUnspecifiedError = expectProblem (\ msg st -> UnspecifiedError st) 1 undefined
> data Expect = Succ | Err String | UErr | Fail String
> expect :: Expect -> Test -> Test
> expect Succ test = expectSuccess test
> expect (Err m) test = expectError m test
> expect UErr test = expectUnspecifiedError test
> expect (Fail m) test = expectFailure m test
> baseTests = test [ assertTests,
> testCaseCountTests,
> testCasePathsTests,
> reportTests,
> textTests,
> showPathTests,
> showCountsTests,
> assertableTests,
> predicableTests,
> compareTests,
> extendedTestTests ]
> ok = test (assert ())
> bad m = test (assertFailure m)
> assertTests = test [
> "null" ~: expectSuccess ok,
> "userError" ~:
> expectError "error" (TestCase (ioError (userError "error"))),
> "IO error (file missing)" ~:
> expectUnspecifiedError
> (test (do openFile "3g9djs" ReadMode; return ())),
"error" ~:
expectError "error" (TestCase (error "error")),
"tail []" ~:
expectUnspecifiedError (TestCase (tail [] `seq` return ())),
-- GHC doesn't currently catch arithmetic exceptions.
"div by 0" ~:
expectUnspecifiedError (TestCase ((3 `div` 0) `seq` return ())),
> "assertFailure" ~:
> let msg = "simple assertFailure"
> in expectFailure msg (test (assertFailure msg)),
> "assertString null" ~: expectSuccess (TestCase (assertString "")),
> "assertString nonnull" ~:
> let msg = "assertString nonnull"
> in expectFailure msg (TestCase (assertString msg)),
> let exp v non =
> show v ++ " with " ++ non ++ "null message" ~:
> expect (if v then Succ else Fail non) $ test $ assertBool non v
> in "assertBool" ~: [ exp v non | v <- [True, False], non <- ["non", ""] ],
> let msg = "assertBool True"
> in msg ~: expectSuccess (test (assertBool msg True)),
> let msg = "assertBool False"
> in msg ~: expectFailure msg (test (assertBool msg False)),
> "assertEqual equal" ~:
> expectSuccess (test (assertEqual "" 3 3)),
> "assertEqual unequal no msg" ~:
> expectFailure "expected: 3\n but got: 4"
> (test (assertEqual "" 3 4)),
> "assertEqual unequal with msg" ~:
> expectFailure "for x,\nexpected: 3\n but got: 4"
> (test (assertEqual "for x," 3 4))
> ]
> emptyTest0 = TestList []
> emptyTest1 = TestLabel "empty" emptyTest0
> emptyTest2 = TestList [ emptyTest0, emptyTest1, emptyTest0 ]
> emptyTests = [emptyTest0, emptyTest1, emptyTest2]
> testCountEmpty test = TestCase (assertEqual "" 0 (testCaseCount test))
> suite0 = (0, ok)
> suite1 = (1, TestList [])
> suite2 = (2, TestLabel "3" ok)
> suite3 = (3, suite)
> suite =
> TestLabel "0"
> (TestList [ TestLabel "1" (bad "1"),
> TestLabel "2" (TestList [ TestLabel "2.1" ok,
> ok,
> TestLabel "2.3" (bad "2") ]),
> TestLabel "3" (TestLabel "4" (TestLabel "5" (bad "3"))),
> TestList [ TestList [ TestLabel "6" (bad "4") ] ] ])
> suiteCount = (6 :: Int)
> suitePaths = [
> [Label "0", ListItem 0, Label "1"],
> [Label "0", ListItem 1, Label "2", ListItem 0, Label "2.1"],
> [Label "0", ListItem 1, Label "2", ListItem 1],
> [Label "0", ListItem 1, Label "2", ListItem 2, Label "2.3"],
> [Label "0", ListItem 2, Label "3", Label "4", Label "5"],
> [Label "0", ListItem 3, ListItem 0, ListItem 0, Label "6"]]
> suiteReports = [ Start (State (p 0) (Counts 6 0 0 0)),
> Failure "1" (State (p 0) (Counts 6 1 0 1)),
> Start (State (p 1) (Counts 6 1 0 1)),
> Start (State (p 2) (Counts 6 2 0 1)),
> Start (State (p 3) (Counts 6 3 0 1)),
> Failure "2" (State (p 3) (Counts 6 4 0 2)),
> Start (State (p 4) (Counts 6 4 0 2)),
> Failure "3" (State (p 4) (Counts 6 5 0 3)),
> Start (State (p 5) (Counts 6 5 0 3)),
> Failure "4" (State (p 5) (Counts 6 6 0 4))]
> where p n = reverse (suitePaths !! n)
> suiteCounts = Counts 6 6 0 4
> suiteOutput = "### Failure in: 0:0:1\n\
> \1\n\
> \### Failure in: 0:1:2:2:2.3\n\
> \2\n\
> \### Failure in: 0:2:3:4:5\n\
> \3\n\
> \### Failure in: 0:3:0:0:6\n\
> \4\n\
> \Cases: 6 Tried: 6 Errors: 0 Failures: 4\n"
> suites = [suite0, suite1, suite2, suite3]
> testCount (num, test) count =
> "testCaseCount suite" ++ show num ~:
> TestCase $ assertEqual "for test count," count (testCaseCount test)
> testCaseCountTests = TestList [
> "testCaseCount empty" ~: test (map testCountEmpty emptyTests),
> testCount suite0 1,
> testCount suite1 0,
> testCount suite2 1,
> testCount suite3 suiteCount
> ]
> testPaths (num, test) paths =
> "testCasePaths suite" ++ show num ~:
> TestCase $ assertEqual "for test paths,"
> (map reverse paths) (testCasePaths test)
> testPathsEmpty test = TestCase $ assertEqual "" [] (testCasePaths test)
> testCasePathsTests = TestList [
> "testCasePaths empty" ~: test (map testPathsEmpty emptyTests),
> testPaths suite0 [[]],
> testPaths suite1 [],
> testPaths suite2 [[Label "3"]],
> testPaths suite3 suitePaths
> ]
> reportTests = "reports" ~: expectReports suiteReports suiteCounts suite
> expectText counts text test = TestCase $ do
> (counts', text') <- runTestText putTextToShowS test
> assertEqual "for the final counts," counts counts'
> assertEqual "for the failure text output," text (text' "")
> textTests = test [
> "lone error" ~:
> expectText (Counts 1 1 1 0)
> "### Error:\nxyz\nCases: 1 Tried: 1 Errors: 1 Failures: 0\n"
> (test (do ioError (userError "xyz"); return ())),
> "lone failure" ~:
> expectText (Counts 1 1 0 1)
> "### Failure:\nxyz\nCases: 1 Tried: 1 Errors: 0 Failures: 1\n"
> (test (assert "xyz")),
> "putTextToShowS" ~:
> expectText suiteCounts suiteOutput suite,
> "putTextToHandle (file)" ~:
> let filename = "HUnitTest.tmp"
> trim = unlines . map (reverse . dropWhile (== ' ') . reverse) . lines
> in map test
> [ "show progress = " ++ show flag ~: do
> handle <- openFile filename WriteMode
> (counts, _) <- runTestText (putTextToHandle handle flag) suite
> hClose handle
> assertEqual "for the final counts," suiteCounts counts
> text <- readFile filename
> let text' = if flag then trim (terminalAppearance text) else text
> assertEqual "for the failure text output," suiteOutput text'
> | flag <- [False, True] ]
> ]
> showPathTests = "showPath" ~: [
> "empty" ~: showPath [] ~?= "",
> ":" ~: showPath [Label ":", Label "::"] ~?= "\"::\":\":\"",
> "\"\\\n" ~: showPath [Label "\"\\n\n\""] ~?= "\"\\\"\\\\n\\n\\\"\"",
> "misc" ~: showPath [Label "b", ListItem 2, ListItem 3, Label "foo"] ~?=
> "foo:3:2:b"
> ]
> showCountsTests = "showCounts" ~: showCounts (Counts 4 3 2 1) ~?=
> "Cases: 4 Tried: 3 Errors: 2 Failures: 1"
> lift :: a -> IO a
> lift a = return a
> assertableTests =
> let assertables x = [
> ( "", assert x , test (lift x)) ,
> ( "IO ", assert (lift x) , test (lift (lift x))) ,
> ( "IO IO ", assert (lift (lift x)), test (lift (lift (lift x))))]
> assertabled l e x =
> test [ test [ "assert" ~: pre ++ l ~: expect e $ test $ a,
> "test" ~: pre ++ "IO " ++ l ~: expect e $ t ]
> | (pre, a, t) <- assertables x ]
> in "assertable" ~: [
> assertabled "()" Succ (),
> assertabled "True" Succ True,
> assertabled "False" (Fail "") False,
> assertabled "\"\"" Succ "",
> assertabled "\"x\"" (Fail "x") "x"
> ]
> predicableTests =
> let predicables x m = [
> ( "", assertionPredicate x , x @? m, x ~? m ),
> ( "IO ", assertionPredicate (l x) , l x @? m, l x ~? m ),
> ( "IO IO ", assertionPredicate (l(l x)), l(l x) @? m, l(l x) ~? m )]
> l x = lift x
> predicabled l e m x =
> test [ test [ "pred" ~: pre ++ l ~: m ~: expect e $ test $ tst p,
> "(@?)" ~: pre ++ l ~: m ~: expect e $ test $ a,
> "(~?)" ~: pre ++ l ~: m ~: expect e $ t ]
> | (pre, p, a, t) <- predicables x m ]
> where tst p = p >>= assertBool m
> in "predicable" ~: [
> predicabled "True" Succ "error" True,
> predicabled "False" (Fail "error") "error" False,
> predicabled "True" Succ "" True,
> predicabled "False" (Fail "" ) "" False
> ]
> compareTests = test [
> let succ = const Succ
> compare f exp act = test [ "(@=?)" ~: expect e $ test (exp @=? act),
> "(@?=)" ~: expect e $ test (act @?= exp),
> "(~=?)" ~: expect e $ exp ~=? act,
> "(~?=)" ~: expect e $ act ~?= exp ]
> where e = f $ "expected: " ++ show exp ++ "\n but got: " ++ show act
> in test [
> compare succ 1 1,
> compare Fail 1 2,
> compare succ (1,'b',3.0) (1,'b',3.0),
> compare Fail (1,'b',3.0) (1,'b',3.1)
> ]
> ]
> expectList1 :: Int -> Test -> Test
> expectList1 c =
> expectReports
> [ Start (State [ListItem n] (Counts c n 0 0)) | n <- [0..c-1] ]
> (Counts c c 0 0)
> expectList2 :: [Int] -> Test -> Test
> expectList2 cs test =
> expectReports
> [ Start (State [ListItem j, ListItem i] (Counts c n 0 0))
> | ((i,j),n) <- zip coords [0..] ]
> (Counts c c 0 0)
> test
> where coords = [ (i,j) | i <- [0 .. length cs - 1], j <- [0 .. cs!!i - 1] ]
> c = testCaseCount test
> extendedTestTests = test [
> "test idempotent" ~: expect Succ $ test $ test $ test $ ok,
> "test list 1" ~: expectList1 3 $ test [assert (), assert "", assert True],
> "test list 2" ~: expectList2 [0, 1, 2] $ test [[], [ok], [ok, ok]]
> ]
|