Debian Codegraph @

tests/Tests.hs

raw
1 {-# LANGUAGE FlexibleInstances, OverloadedStrings, ViewPatterns #-}
2 {-# OPTIONS_GHC -fno-warn-orphans #-}
3 module Main where
4
5 import Control.Applicative
6
7 import Data.Monoid (Monoid(..),(<>))
8 import Data.ByteString (ByteString)
9 import qualified Data.ByteString.Char8 as S
10 import Data.Text (Text)
11 import qualified Data.Text as T
12 import qualified Data.Text.Encoding as T
13 import qualified Data.Conduit as C
14 import qualified Control.Monad.Trans.Resource as C
15 import qualified Data.Conduit.List as CL
16
17 import Test.Hspec
18 import Test.Hspec.QuickCheck (prop)
19 import Test.HUnit hiding (Test)
20 import Test.QuickCheck
21
22 import Text.HTML.TagStream
23 import qualified Text.HTML.TagStream.ByteString as S
24 import qualified Text.HTML.TagStream.Text as T
25
26 main :: IO ()
27 main = hspec $ do
28 describe "[ByteString] Property" $ do
29 prop "Text nodes can't be empty" propTextNotEmpty
30 prop "Parse results can't empty" propResultNotEmpty
31 describe "[Text] Property" $ do
32 prop "Text nodes can't be empty" propTextNotEmptyText
33 prop "Parse results can't empty" propResultNotEmptyText
34 describe "[ByteString]One pass parse" onePassTests
35 describe "[ByteString]Streamline parse" streamlineTests
36 describe "[Text]One pass parse" onePassTestsText
37 describe "[Text]Streamline parse" streamlineTestsText
38
39 propTextNotEmpty :: ByteString -> Bool
40 propTextNotEmpty = either (const False) text_not_empty . S.decode
41 where text_not_empty = all not_empty
42 not_empty (Text s) = S.length s > 0
43 not_empty _ = True
44
45 propResultNotEmpty :: ByteString -> Bool
46 propResultNotEmpty s = either (const False) not_empty . S.decode $ s
47 where not_empty tokens = (S.null s && null tokens)
48 || (not (S.null s) && not (null tokens))
49
50 propTextNotEmptyText :: Text -> Bool
51 propTextNotEmptyText = either (const False) text_not_empty . T.decode
52 where text_not_empty = all not_empty
53 not_empty (Text s) = not (T.null s)
54 not_empty _ = True
55
56 propResultNotEmptyText :: Text -> Bool
57 propResultNotEmptyText s = either (const False) not_empty . T.decode $ s
58 where not_empty tokens = (T.null s && null tokens)
59 || (not (T.null s) && not (null tokens))
60
61 encodeTokenUtf8 :: Token' Text -> Token' ByteString
62 encodeTokenUtf8 = fmap T.encodeUtf8
63
64 onePassTests :: Spec
65 onePassTests = mapM_ one testcases
66 where
67 one (T.encodeUtf8 -> str, map encodeTokenUtf8 -> tokens) =
68 it (S.unpack str) $ do
69 result <- combineText <$> assertDecodeBS str
70 assertEqual "one-pass parse result incorrect" tokens result
71
72 onePassTestsText :: Spec
73 onePassTestsText = mapM_ one testcases
74 where
75 one (str, tokens) = it (T.unpack str) $ do
76 result <- combineText <$> assertDecodeText str
77 assertEqual "one-pass parse result incorrect" tokens result
78
79 streamlineTests :: Spec
80 streamlineTests = mapM_ one testcases
81 where
82 isIncomplete (Incomplete _) = True
83 isIncomplete _ = False
84 one (T.encodeUtf8 -> str, map encodeTokenUtf8 -> tokens) =
85 it (S.unpack str) $ do
86 -- streamline parse result don't contain the trailing Incomplete token.
87 let tokens' = reverse . dropWhile isIncomplete . reverse $ tokens
88 result <- combineText <$> C.runResourceT (
89 CL.sourceList (map S.singleton (S.unpack str))
90 C.$= S.tokenStream
91 C.$$ CL.consume )
92 assertEqual "streamline parse result incorrect" tokens' result
93
94 streamlineTestsText :: Spec
95 streamlineTestsText = mapM_ one testcases
96 where
97 isIncomplete (Incomplete _) = True
98 isIncomplete _ = False
99 one (T.encodeUtf8 -> str, tokens) =
100 it (S.unpack str) $ do
101 -- streamline parse result don't contain the trailing Incomplete token.
102 let tokens' = reverse . dropWhile isIncomplete . reverse $ tokens
103 result <- combineText <$> C.runResourceT (
104 CL.sourceList (map S.singleton (S.unpack str))
105 C.$= T.tokenStreamBS
106 C.$$ CL.consume )
107 assertEqual "streamline parse result incorrect" tokens' result
108
109 testcases :: [(Text, [Token' Text])]
110 testcases =
111 -- attributes {{{
112 [ ( "<span readonly title=foo class=\"foo bar\" style='display:none;'>"
113 , [TagOpen "span" [("readonly", ""), ("title", "foo"), ("class", "foo bar"), ("style", "display:none;")] False]
114 )
115 , ( "<span a = b = c = d>"
116 , [TagOpen "span" [("a", "b"), ("=", ""), ("c", "d")] False]
117 )
118 , ( "<span a = b = c>"
119 , [TagOpen "span" [("a", "b"), ("=", ""), ("c", "")] False]
120 )
121 , ( "<span /foo=bar>"
122 , [TagOpen "span" [("/foo", "bar")] False]
123 )
124 -- }}}
125 -- quoted string and escaping {{{
126 , ( "<span \"<p>xx \\\"'\\\\</p>\"=\"<p>xx \\\"'\\\\</p>\">"
127 , [TagOpen "span" [("<p>xx \"'\\</p>", "<p>xx \"'\\</p>")] False]
128 )
129 , ( "<span '<p>xx \\\"\\'\\\\</p>'='<p>xx \\\"\\'\\\\</p>'>"
130 , [TagOpen "span" [("<p>xx \"'\\</p>", "<p>xx \"'\\</p>")] False]
131 )
132 -- }}}
133 -- attribute and tag end {{{
134 , ( "<br/>"
135 , [TagOpen "br" [] True]
136 )
137 , ( "<img src=http://foo.bar.com/foo.jpg />"
138 , [TagOpen "img" [("src", "http://foo.bar.com/foo.jpg")] True]
139 )
140 , ( "<span foo>"
141 , [TagOpen "span" [("foo", "")] False]
142 )
143 , ( "<span foo/>"
144 , [TagOpen "span" [("foo", "")] True]
145 )
146 , ( "<span foo=/>"
147 , [TagOpen "span" [("foo", "/")] False]
148 )
149 -- }}}
150 -- normal tag {{{
151 , ( "<p>text</p>"
152 , [TagOpen "p" [] False, Text "text", TagClose "p"]
153 )
154 , ( "<>"
155 , [TagOpen "" [] False]
156 )
157 , ( "<a\ttitle\n=\r\"foo bar\" alt=\n/\r\t>"
158 , [TagOpen "a" [("title", "foo bar"), ("alt", "/")] False]
159 )
160 -- }}}
161 -- comment tag {{{
162 , ( "<!--foo-->"
163 , [Comment "foo"] )
164 , ( "<!--f--oo->-->"
165 , [Comment "f--oo->"] )
166 , ( "<!--foo-->bar-->"
167 , [Comment "foo", Text "bar-->"]
168 )
169 -- }}}
170 -- special tag {{{
171 , ( "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\">"
172 , [Special "DOCTYPE" "html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\""]
173 )
174 , ( "<!DOCTYPE html>"
175 , [Special "DOCTYPE" "html"]
176 )
177 -- }}}
178 -- close tag {{{
179 , ( "</\r\t\nbr>"
180 , [TagClose "\r\t\nbr"]
181 )
182 , ( "</br/>"
183 , [TagClose "br/"]
184 )
185 , ( "</>"
186 , [TagClose ""]
187 )
188 -- }}}
189 -- incomplete test {{{
190 -- }}}
191 -- script tag TODO{{{
192 , ( "<script></script>"
193 , [TagOpen "script" [] False, TagClose "script"]
194 )
195 , ( "<script>var x=\"</script>"
196 , [TagOpen "script" [] False, Text "var x=\"", TagClose "script"]
197 )
198 --, ( "<script>var x=\"</script>\";</script>"
199 -- , [TagOpen "script" [] False, Text "var x=\"</script>\";", TagClose "script"]
200 -- )
201 , ( "<script>// '\r\n</script>"
202 , [TagOpen "script" [] False, Text "// '\r\n", TagClose "script"]
203 )
204 -- }}}
205 -- issue 10 https://github.com/yihuang/tagstream-conduit/issues/10
206 , ( "<foo> hello</foo>"
207 , [TagOpen "foo" [] False, Text " hello", TagClose "foo"]
208 )
209
210 -- Text entity decoding
211 , text "" ""
212 , text "&" "&"
213 , text "& hello" "& hello"
214 , text "&amp" "&amp"
215 , text "&amp;" "&"
216 , text "&quot;" "\""
217 , text "&unknown;" "&unknown;"
218 , text "a &unknown b" "a &unknown b"
219 , text "\"&unknown\"" "\"&unknown\""
220 , text "foo &bar; mu" "foo &bar; mu"
221 , text "&foo; &bar &quot;mu&lt; zot &hello;" "&foo; &bar \"mu< zot &hello;"
222 , text "&lt;p&gt;" "<p>"
223 , text "&#60;" "<"
224 , text "a&#97;a" "aaa"
225 , text "foo &" "foo &"
226 , text "foo &amp" "foo &amp"
227 , text "foo &amp;" "foo &"
228
229 -- Attribute entity decoding
230 , attr "" ""
231 , attr "&" "&"
232 , attr "& hello" "& hello"
233 , attr "&amp" "&amp"
234 , attr "&amp;" "&"
235 , attr "&quot;" "\""
236 , attr "&unknown;" "&unknown;"
237 , attr "a &unknown b" "a &unknown b"
238 , attr "\"&unknown\"" "\"&unknown\""
239 , attr "foo &bar; mu" "foo &bar; mu"
240 , attr "&foo; &bar &quot;mu&lt; zot &hello;" "&foo; &bar \"mu< zot &hello;"
241 , attr "&lt;p&gt;" "<p>"
242 , attr "&#60;" "<"
243 , attr "a&#97;a" "aaa"
244 , attr "foo &" "foo &"
245 , attr "foo &amp" "foo &amp"
246 , attr "foo &amp;" "foo &"
247 ]
248 where text b a = ("<p>" <> b <> "</p>"
249 ,concat [[TagOpen "p" [] False],[Text a | not (T.null a)],[TagClose "p"]])
250 attr b a = ("<a title='" <> b <> "'></a><a title='" <> b <> "'></a>"
251 ,concat [[TagOpen "a" [("title",a)] False],[TagClose "a"]
252 ,[TagOpen "a" [("title",a)] False],[TagClose "a"]])
253
254 testChar :: Gen Char
255 testChar = growingElements "<>/=\"' \t\r\nabcde\\"
256 testString :: Gen String
257 testString = listOf testChar
258 testBS :: Gen ByteString
259 testBS = S.pack <$> testString
260 testText :: Gen Text
261 testText = T.pack <$> testString
262
263 instance Arbitrary ByteString where
264 arbitrary = testBS
265 instance Arbitrary Text where
266 arbitrary = testText
267
268 assertEither :: Either String a -> Assertion
269 assertEither = either (assertFailure . ("Left:"++)) (const $ return ())
270
271 assertDecodeBS :: ByteString -> IO [S.Token]
272 assertDecodeBS s = do
273 let result = S.decode s
274 assertEither result
275 let (Right tokens) = result
276 return tokens
277
278 assertDecodeText :: Text -> IO [T.Token]
279 assertDecodeText s = do
280 let result = T.decode s
281 assertEither result
282 let (Right tokens) = result
283 return tokens
284
285 combineText :: Monoid s => [Token' s] -> [Token' s]
286 combineText = go
287 where go [] = []
288 go (Text t1 : Text t2 : xs) = go $ Text (mappend t1 t2) : xs
289 go (x:xs) = x : go xs