]> code.delx.au - offlineimap/blob - testsrc/TestParserPrim.hs
Added greeting test
[offlineimap] / testsrc / TestParserPrim.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 TestParserPrim where
19 import Data.List
20 import qualified Test.HUnit as HU
21 import Test.HUnit.Utils
22 import Data.Word
23 import Test.QuickCheck
24 import TestInfrastructure
25
26 import Network.IMAP.Parser.Prim
27 import Network.IMAP.Types
28
29 import TestInfrastructure
30 import Text.ParserCombinators.Parsec
31 import Text.ParserCombinators.Parsec.Error
32
33 prop_quoted :: String -> Result
34 prop_quoted s =
35 p quoted (gen_quoted s) @?= Just s
36
37 gen_quoted :: String -> String
38 gen_quoted s = '"' : concatMap quoteChar s ++ "\""
39 where quoteChar '\\' = "\\\\"
40 quoteChar '"' = "\\\""
41 quoteChar x = [x]
42
43 prop_literal :: String -> Result
44 prop_literal s =
45 p literal (gen_literal s) @?= Just s
46
47 gen_literal :: String -> String
48 gen_literal s =
49 "{" ++ show (length s) ++ "}\r\n" ++ s
50
51 gen_string3501 :: String -> Bool -> String
52 gen_string3501 s True = gen_quoted s
53 gen_string3501 s False = gen_literal s
54
55 prop_string3501 :: String -> Bool -> Result
56 prop_string3501 s useQuoted = p string3501 (gen_string3501 s useQuoted) @?= Just s
57
58 prop_atom :: String -> Result
59 prop_atom s =
60 p atom s @?= if isvalid
61 then Just s
62 else Nothing
63 where isvalid = not (null s) && all (`notElem` atomSpecials) s
64
65 prop_astring_basic :: String -> Result
66 prop_astring_basic s =
67 p astring s @?= if isValidAtom s
68 then Just s
69 else Nothing
70
71 prop_astring_basic_thorough :: String -> Property
72 prop_astring_basic_thorough s =
73 isValidAtom s ==> p astring s @?= Just s
74
75 isValidAtom s = not (null s) && all isValidChar s
76 where isValidChar c =
77 c `notElem` atomSpecials ||
78 c `elem` respSpecials
79
80 prop_astring :: String -> Bool -> Result
81 prop_astring s useQuoted =
82 if isValidAtom s
83 then p astring s @=? Just s
84 else p astring (gen_string3501 s useQuoted) @=? Just s
85
86 prop_text :: String -> Result
87 prop_text s =
88 p text s @=? if isValidText s
89 then Just s
90 else Nothing
91
92 isValidText s = not (null s) && all (`notElem` crlf) s
93
94 prop_tag :: String -> Result
95 prop_tag s =
96 p tag s @=? if isValid
97 then Just s
98 else Nothing
99 where isValid = not (null s) && all isValidChar s
100 isValidChar c =
101 c `notElem` ('+' : atomSpecials) ||
102 c `elem` respSpecials
103
104 allt = [q "quoted" prop_quoted,
105 q "literal" prop_literal,
106 q "string3501" prop_string3501,
107 q "atom" prop_atom,
108 q "astring basic" prop_astring_basic,
109 q "astring basic thorough" prop_astring_basic_thorough,
110 q "astring full" prop_astring,
111 q "text" prop_text,
112 q "tag" prop_tag
113 ]
114