]> code.delx.au - offlineimap/blob - testsrc/TestInfrastructure.hs
feb058e186473f2cf4fb34d7a594f48994b9abe6
[offlineimap] / testsrc / TestInfrastructure.hs
1 {-
2 Copyright (C) 2002-2008 John Goerzen <jgoerzen@complete.org>
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 -}
18
19 module TestInfrastructure where
20 import Test.QuickCheck
21 import Test.QuickCheck.Batch
22 import qualified Test.HUnit as HU
23 import qualified Data.Map as Map
24 import System.IO
25 import Text.Printf
26 import System.Random
27 import Data.Word
28 import Test.HUnit.Utils
29 import Text.ParserCombinators.Parsec
30
31 (@=?) :: (Eq a, Show a) => a -> a -> Result
32 expected @=? actual =
33 Result {ok = Just (expected == actual),
34 arguments = ["Result: expected " ++ show expected ++ ", got " ++ show actual],
35 stamp = []}
36
37 (@?=) :: (Eq a, Show a) => a -> a -> Result
38 (@?=) = flip (@=?)
39
40 keysToMap :: Ord k => [k] -> Map.Map k ()
41 keysToMap = foldl (\map k -> Map.insert k () map) Map.empty
42
43 emptymap :: (Eq k, Ord k, Show v) => Map.Map k v
44 emptymap = Map.empty
45
46 instance (Arbitrary k, Arbitrary v, Eq k, Ord k) => Arbitrary (Map.Map k v) where
47 arbitrary =
48 do items <- arbitrary
49 return $ Map.fromList items
50 coarbitrary = coarbitrary . Map.keys
51
52 instance Arbitrary Word8 where
53 arbitrary = sized $ \n -> choose (0, min (fromIntegral n) maxBound)
54 coarbitrary n = variant (if n >= 0 then 2 * x else 2 * x + 1)
55 where x = abs . fromIntegral $ n
56
57 instance Random Word8 where
58 randomR (a, b) g = (\(x, y) -> (fromInteger x, y)) $
59 randomR (toInteger a, toInteger b) g
60 random g = randomR (minBound, maxBound) g
61
62 instance Arbitrary Char where
63 arbitrary = sized $ \n -> choose (toEnum 0, min (toEnum n) maxBound)
64 coarbitrary n = variant (if (fromEnum n) >= 0 then toEnum (2 * x) else toEnum (2 * x + 1))
65 where x = (abs . fromEnum $ n)::Int
66
67 -- Modified from HUnit
68 runVerbTestText :: HU.PutText st -> HU.Test -> IO (HU.Counts, st)
69 runVerbTestText (HU.PutText put us) t = do
70 (counts, us') <- HU.performTest reportStart reportError reportFailure us t
71 us'' <- put (HU.showCounts counts) True us'
72 return (counts, us'')
73 where
74 reportStart ss us = do hPrintf stdout "\rTesting %-70s\n"
75 (HU.showPath (HU.path ss))
76 put (HU.showCounts (HU.counts ss)) False us
77 reportError = reportProblem "Error:" "Error in: "
78 reportFailure = reportProblem "Failure:" "Failure in: "
79 reportProblem p0 p1 msg ss us = put line True us
80 where line = "### " ++ kind ++ path' ++ '\n' : msg
81 kind = if null path' then p0 else p1
82 path' = HU.showPath (HU.path ss)
83
84 q :: Testable a => String -> a -> HU.Test
85 q = qccheck (defaultConfig {configMaxTest = 250, configMaxFail = 10000,
86 configEvery = \_ _ -> ""})
87 -- configEvery = testCount for displaying a running test counter
88 where testCountBase n = " (test " ++ show n ++ "/250)"
89 testCount n _ = testCountBase n ++
90 replicate (length (testCountBase n)) '\b'
91
92 {- | Test a parser, forcing it to apply to all input. -}
93 p parser input =
94 case parse parseTest "(none)" input of
95 Left _ -> Nothing
96 Right y -> Just y
97 where parseTest = do r <- parser
98 eof
99 return r
100