]> code.delx.au - offlineimap/blob - testsrc/TestConnection.hs
Changed Connection to use StateT instead of State, with Either
[offlineimap] / testsrc / TestConnection.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 (not (null s)) && (and (map (notElem '\r') s)) ==>
51 runLinesConnection s readLine @?=
52 Right (head s, (expectedString (tail s), []))
53
54 prop_readBytes :: String -> Int -> Property
55 prop_readBytes s l =
56 l <= length s && l >= 0 ==>
57 runStringConnection s (\c -> readBytes c (fromIntegral l)) ==
58 Right (take l s, (drop l s, []))
59
60 q :: Testable a => String -> a -> HU.Test
61 q = qccheck (defaultConfig {configMaxTest = 250, configMaxFail = 5000})
62
63 allt = [q "Identity" prop_identity,
64 q "Lines identity" prop_linesidentity,
65 q "Lines list identity" prop_lineslistidentity,
66 q "readline" prop_readLine,
67 q "readBytes" prop_readBytes
68 ]