]> code.delx.au - offlineimap/blob - testsrc/TestParserPrim.hs
Added first respText 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 isValidAtom s = not (null s) && all isValidChar s
72 where isValidChar c =
73 c `notElem` atomSpecials ||
74 c `elem` respSpecials
75
76 prop_astring :: String -> Bool -> Result
77 prop_astring s useQuoted =
78 if isValidAtom s
79 then p astring s @=? Just s
80 else p astring (gen_string3501 s useQuoted) @=? Just s
81
82 prop_text :: String -> Result
83 prop_text s =
84 p text s @=? if isValidText s
85 then Just s
86 else Nothing
87
88 isValidText s = not (null s) && all (`notElem` crlf) s
89
90 prop_tag :: String -> Result
91 prop_tag s =
92 p tag s @=? if isValid
93 then Just s
94 else Nothing
95 where isValid = not (null s) && all isValidChar s
96 isValidChar c =
97 c `notElem` ('+' : atomSpecials) ||
98 c `elem` respSpecials
99
100 allt = [q "quoted" prop_quoted,
101 q "literal" prop_literal,
102 q "string3501" prop_string3501,
103 q "atom" prop_atom,
104 q "astring basic" prop_astring_basic,
105 q "astring full" prop_astring,
106 q "text" prop_text,
107 q "tag" prop_tag
108 ]
109