2 Copyright (C) 2002-2008 John Goerzen <jgoerzen@complete.org>
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.
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.
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
18 module TestParserPrim where
20 import qualified Test.HUnit as HU
21 import Test.HUnit.Utils
23 import Test.QuickCheck
24 import TestInfrastructure
26 import Network.IMAP.Parser.Prim
27 import Network.IMAP.Types
29 import TestInfrastructure
30 import Text.ParserCombinators.Parsec
31 import Text.ParserCombinators.Parsec.Error
33 prop_quoted :: String -> Result
35 p quoted (gen_quoted s) @?= Just s
37 gen_quoted :: String -> String
38 gen_quoted s = '"' : concatMap quoteChar s ++ "\""
39 where quoteChar '\\' = "\\\\"
40 quoteChar '"' = "\\\""
43 prop_literal :: String -> Result
45 p literal (gen_literal s) @?= Just s
47 gen_literal :: String -> String
49 "{" ++ show (length s) ++ "}\r\n" ++ s
51 gen_string3501 :: String -> Bool -> String
52 gen_string3501 s True = gen_quoted s
53 gen_string3501 s False = gen_literal s
55 prop_string3501 :: String -> Bool -> Result
56 prop_string3501 s useQuoted = p string3501 (gen_string3501 s useQuoted) @?= Just s
58 prop_atom :: String -> Result
60 p atom s @?= if isvalid
63 where isvalid = not (null s) && all (`notElem` atomSpecials) s
65 prop_astring_basic :: String -> Result
66 prop_astring_basic s =
67 p astring s @?= if isValidAtom s
71 prop_astring_basic_thorough :: String -> Property
72 prop_astring_basic_thorough s =
73 isValidAtom s ==> p astring s @?= Just s
75 isValidAtom s = not (null s) && all isValidChar s
77 c `notElem` atomSpecials ||
80 prop_astring :: String -> Bool -> Result
81 prop_astring s useQuoted =
83 then p astring s @=? Just s
84 else p astring (gen_string3501 s useQuoted) @=? Just s
86 prop_text :: String -> Result
88 p text s @=? if isValidText s
92 isValidText s = not (null s) && all (`notElem` crlf) s
94 prop_tag :: String -> Result
96 p tag s @=? if isValid
99 where isValid = not (null s) && all isValidChar s
101 c `notElem` ('+' : atomSpecials) ||
102 c `elem` respSpecials
104 allt = [q "quoted" prop_quoted,
105 q "literal" prop_literal,
106 q "string3501" prop_string3501,
108 q "astring basic" prop_astring_basic,
109 q "astring basic thorough" prop_astring_basic_thorough,
110 q "astring full" prop_astring,