]> code.delx.au - offlineimap/blob - testsrc/TestParser.hs
More respText test.
[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 TestParser 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.Parser
28 import Network.IMAP.Connection
29 import Network.IMAP.Types
30
31 import TestInfrastructure
32 import TestConnection(expectedString, noCR)
33 import TestParserPrim(isValidText, isValidAtom)
34
35 prop_getFullLine_basic :: [String] -> Property
36 prop_getFullLine_basic s =
37 (null s || not ("}" `isSuffixOf` (head s))) && noCR s ==>
38 runLinesConnection s (getFullLine []) @?=
39 if null s
40 then Left "EOF in input in readLine"
41 else Right (head s, (expectedString (tail s), []))
42
43 prop_getFullLine_count :: [String] -> Property
44 prop_getFullLine_count s =
45 length s >= 2 && noCR s && (length s < 3 || not ("}" `isSuffixOf` (s !! 2)))
46 ==>
47 runLinesConnection lenS (getFullLine []) @?=
48 Right (expectedResult, (expectedRemain, []))
49 where lenS = [braceString] ++ [(head . tail $ s)] ++ drop 2 s
50 braceString = head s ++ "{" ++ show (length (s !! 1)) ++ "}"
51 expectedResult = braceString ++ "\r\n" ++ (s !! 1)
52 expectedRemain = expectedString (drop 2 s)
53
54 prop_rfr_basic :: [String] -> Property
55 prop_rfr_basic s =
56 let testlist =
57 case length s of
58 0 -> []
59 1 -> ["TAG " ++ head s]
60 _ -> map ("* " ++) (init s) ++
61 ["TAG " ++ last s]
62 resultstr = expectedString testlist
63 in noCR s && noBrace s ==>
64 runLinesConnection testlist readFullResponse @?=
65 if null s
66 then Left "EOF in input in readLine"
67 else Right (resultstr, ([], []))
68
69 noBrace s = and (map (not . isSuffixOf "}") s)
70
71 prop_respTextSimple :: String -> Result
72 prop_respTextSimple s =
73 p respText s @?=
74 if isValidText s && (head s /= '[')
75 then Just (RespText Nothing s)
76 else Nothing
77
78 prop_respTextAtom :: String -> Property
79 prop_respTextAtom s2 =
80 isValidAtom s2 && isValidText s1 && ']' `notElem` s1 ==>
81 p respText ("[" ++ s2 ++ "] " ++ s1) @?=
82 Just (RespText (Just s2) s1)
83 where s1 = reverse s2 -- Gen manually to avoid test exhaustion
84
85 prop_respTextAtomOpt :: String -> String -> String -> Property
86 prop_respTextAtomOpt codeatom codedesc text =
87 isValidAtom codeatom && isValidText codedesc && isValidText text &&
88 ']' `notElem` (codeatom ++ codedesc) &&
89 (head text) /= '[' ==>
90 p respText ("[" ++ codeatom ++ " " ++ codedesc ++ "] " ++ text) @?=
91 Just (RespText (Just (codeatom ++ " " ++ codedesc)) text)
92
93 allt = [q "getFullLine_basic" prop_getFullLine_basic,
94 q "getFullLine_count" prop_getFullLine_count,
95 q "readFullResponse_basic" prop_rfr_basic,
96 q "respText simple" prop_respTextSimple,
97 q "respText atom" prop_respTextAtom,
98 q "respTextAtomOpt" prop_respTextAtomOpt
99 ]