]> code.delx.au - offlineimap/blob - testsrc/TestParser.hs
Added TestParser from TestConnection
[offlineimap] / testsrc / TestParser.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 module TestConnection where
19 import qualified Data.Map as Map
20 import Data.List
21 import qualified Test.HUnit as HU
22 import Test.HUnit.Utils
23 import Data.Word
24 import Test.QuickCheck
25 import TestInfrastructure
26
27 import Network.IMAP.Connection
28 import Network.IMAP.Types
29
30 prop_identity :: String -> Bool
31 prop_identity f = runStringConnection f (\_ -> return ()) == Right ((), (f, []))
32
33 prop_linesidentity :: String -> Bool
34 prop_linesidentity f =
35 runLinesConnection [f] (\_ -> return ()) == Right ((), (f ++ "\r\n", []))
36
37 prop_lineslistidentity :: [String] -> Property
38 prop_lineslistidentity f =
39 and (map (notElem '\r') f) ==>
40 runLinesConnection f (\_ -> return ()) @?= Right ((), (expected, []))
41 where expected = expectedString f
42
43 expectedString f =
44 case f of
45 [] -> []
46 _ -> (intercalate "\r\n" f) ++ "\r\n"
47
48 prop_readLine :: [String] -> Property
49 prop_readLine s =
50 (and (map (notElem '\r') s)) ==>
51 runLinesConnection s readLine @?=
52 if null s
53 then Left "EOF in input in readLine"
54 else Right (head s, (expectedString (tail s), []))
55
56 prop_readBytes :: String -> Int -> Result
57 prop_readBytes s l =
58 runStringConnection s (\c -> readBytes c (fromIntegral l)) @?=
59 if l < 0
60 then Left "readBytes: negative count"
61 else case compare l (length s) of
62 EQ -> Right (take l s, (drop l s, []))
63 LT -> Right (take l s, (drop l s, []))
64 GT -> Left "EOF in input in readBytes"
65
66 q :: Testable a => String -> a -> HU.Test
67 q = qccheck (defaultConfig {configMaxTest = 250, configMaxFail = 5000})
68
69 allt = [q "Identity" prop_identity,
70 q "Lines identity" prop_linesidentity,
71 q "Lines list identity" prop_lineslistidentity,
72 q "readline" prop_readLine,
73 q "readBytes" prop_readBytes
74 ]