Debian Codegraph @

test/Main.hs

raw symbols SCIP
1 {-# LANGUAGE FlexibleContexts #-}
2 {-# LANGUAGE OverloadedStrings #-}
3 {-# LANGUAGE RecursiveDo #-}
4 {-# LANGUAGE ExtendedDefaultRules #-}
5
6 -- | Test suite for Lucid.
7
8 module Main where
9
10 import Lucid
11 import Lucid.Base
12 import Lucid.Bootstrap
13
14 import Control.Monad
15 import Control.Applicative
16 import Control.Monad.State.Strict
17
18 import qualified Data.Text as T
19
20 import Example1
21
22 import Test.HUnit
23 import Test.Hspec
24
25 -- | Test suite entry point, returns exit failure if any test fails.
26 main :: IO ()
27 main = hspec spec
28
29 -- | Test suite.
30 spec :: Spec
31 spec = do
32 describe "text" testText
33 describe "elements" testElements
34 describe "attributes" testAttributes
35 describe "attributes-with" testAttributesWith
36 describe "extension" testExtension
37 describe "special-elements" testSpecials
38 describe "self-closing" testSelfClosing
39 describe "commuteHtmlT" testCommuteHtmlT
40 describe "monadFix" testMonadFix
41
42 (==?*) :: (Eq a, Show a) => a -> [a] -> Assertion
43 x ==?* xs | x `elem` xs = return ()
44 | otherwise = assertFailure $ show x ++ " is not equal to any of " ++ show xs
45
46 -- | Test text/unicode.
47 testText :: Spec
48 testText =
49 do it "simple"
50 (renderText "foo" ==
51 "foo")
52 it "escaping"
53 (renderText "'<>" ==
54 "&#39;&lt;&gt;")
55 it "unicode"
56 (renderText "fo\243o\333o(\4326\728\8995\728\4326) \9835\65381*:.\65377. .\65377.:*\65381" ==
57 "fo\243o\333o(\4326\728\8995\728\4326) \9835\65381*:.\65377. .\65377.:*\65381")
58
59 -- | Test basic elements and nesting.
60 testElements :: Spec
61 testElements =
62 do it "simple"
63 (renderText (p_ "foo") ==
64 "<p>foo</p>")
65 it "escaping"
66 (renderText (p_ "'<>") ==
67 "<p>&#39;&lt;&gt;</p>")
68 it "unicode"
69 (renderText (p_ "fo\243o\333o(\4326\728\8995\728\4326) \9835\65381*:.\65377. .\65377.:*\65381") ==
70 ("<p>fo\243o\333o(\4326\728\8995\728\4326) \9835\65381*:.\65377. .\65377.:*\65381</p>"))
71 it "nesting"
72 (renderText (p_ (p_ "Hello!")) ==
73 "<p><p>Hello!</p></p>")
74 it "empty"
75 (renderText (p_ (p_ "")) ==
76 "<p><p></p></p>")
77 it "mixed"
78 (renderText (p_ (style_ "")) ==
79 "<p><style></style></p>")
80 it "no closing"
81 (renderText (p_ (input_ [])) ==
82 "<p><input></p>")
83 it "no closing"
84 (renderText (makeXmlElementNoEnd "p") ==
85 "<p/>")
86
87 -- | Test that attribute assigning works properly.
88 testAttributes :: Spec
89 testAttributes =
90 do it "simple"
91 (renderText (p_ [class_ "foo"] "foo") ==
92 "<p class=\"foo\">foo</p>")
93 it "escaping"
94 (renderText (p_ [class_ "foo"] "'<>") ==
95 "<p class=\"foo\">&#39;&lt;&gt;</p>")
96 it "unicode"
97 (renderText
98 (p_ [class_ "foo"]
99 "fo\243o\333o(\4326\728\8995\728\4326) \9835\65381*:.\65377. .\65377.:*\65381") ==
100 ("<p class=\"foo\">fo\243o\333o(\4326\728\8995\728\4326) \9835\65381*:.\65377. .\65377.:*\65381</p>"))
101 it "nesting"
102 (renderText
103 (p_ [class_ "foo"]
104 (p_ "Hello!")) ==
105 "<p class=\"foo\"><p>Hello!</p></p>")
106 it "empty"
107 (renderText
108 (p_ [class_ "foo"]
109 (p_ "")) ==
110 "<p class=\"foo\"><p></p></p>")
111 it "mixed" $
112 renderText
113 (p_ [class_ "foo",style_ "attrib"]
114 (do style_ ""
115 style_ "")) ==?*
116 [ "<p style=\"attrib\" class=\"foo\"><style></style><style></style></p>"
117 , "<p class=\"foo\" style=\"attrib\"><style></style><style></style></p>"
118 ]
119 it "no closing"
120 (renderText (p_ [class_ "foo"] (input_ [])) ==
121 "<p class=\"foo\"><input></p>")
122 it "multiple" $
123 renderText (p_ [class_ "foo",id_ "zot"] "foo") ==?*
124 [ "<p id=\"zot\" class=\"foo\">foo</p>"
125 , "<p class=\"foo\" id=\"zot\">foo</p>"
126 ]
127 it "encoded"
128 (renderText (p_ [class_ "foo<>"] "foo") ==
129 "<p class=\"foo&lt;&gt;\">foo</p>")
130 it "nesting attributes"
131 (renderText
132 (with (p_ [class_ "foo"])
133 [class_ "bar"]
134 "foo") ==
135 "<p class=\"foobar\">foo</p>")
136
137 -- | Test that the `with' combinator still works as expected.
138 testAttributesWith :: Spec
139 testAttributesWith =
140 do it "simple"
141 (renderText (with p_ [class_ "foo"] "foo") ==
142 "<p class=\"foo\">foo</p>")
143 it "escaping"
144 (renderText (with p_ [class_ "foo"] "'<>") ==
145 "<p class=\"foo\">&#39;&lt;&gt;</p>")
146 it "unicode"
147 (renderText
148 (with p_
149 [class_ "foo"]
150 "fo\243o\333o(\4326\728\8995\728\4326) \9835\65381*:.\65377. .\65377.:*\65381") ==
151 ("<p class=\"foo\">fo\243o\333o(\4326\728\8995\728\4326) \9835\65381*:.\65377. .\65377.:*\65381</p>"))
152 it "nesting"
153 (renderText
154 (with p_
155 [class_ "foo"]
156 (p_ "Hello!")) ==
157 "<p class=\"foo\"><p>Hello!</p></p>")
158 it "empty"
159 (renderText
160 (with p_
161 [class_ "foo"]
162 (p_ "")) ==
163 "<p class=\"foo\"><p></p></p>")
164 it "mixed" $
165 renderText
166 (with p_
167 [class_ "foo",style_ "attrib"]
168 (style_ "")) ==?*
169 [ "<p style=\"attrib\" class=\"foo\"><style></style></p>"
170 , "<p class=\"foo\" style=\"attrib\"><style></style></p>"
171 ]
172 it "no closing" $
173 renderText (with p_ [class_ "foo"] (with (input_ [type_ "text"]) [class_ "zot"])) ==?*
174 [ "<p class=\"foo\"><input type=\"text\" class=\"zot\"></p>"
175 , "<p class=\"foo\"><input class=\"zot\" type=\"text\"></p>"
176 ]
177 it "multiple" $
178 renderText (with p_ [class_ "foo",id_ "zot"] "foo") ==?*
179 [ "<p id=\"zot\" class=\"foo\">foo</p>"
180 , "<p class=\"foo\" id=\"zot\">foo</p>"
181 ]
182 it "encoded"
183 (renderText (with p_ [class_ "foo<>"] "foo") ==
184 "<p class=\"foo&lt;&gt;\">foo</p>")
185 it "nesting attributes"
186 (renderText (with (with p_ [class_ "foo"]) [class_ "bar"] "foo") ==
187 "<p class=\"foobar\">foo</p>")
188
189 -- | Test that one can use elements with extensible attributes.
190 testExtension :: Spec
191 testExtension =
192 do it "bootstrap"
193 (renderText (container_ "Foo!") ==
194 "<div class=\" container \">Foo!</div>")
195 it "bootstrap-attributes-extended" $
196 renderText (container_ [class_ "bar",id_ "zot"] "Foo!") ==?*
197 [ "<div class=\" container bar\" id=\"zot\">Foo!</div>"
198 , "<div class=\" container bar\" id=\"zot\">Foo!</div>"
199 ]
200
201 -- | Test special elements that do something different to normal
202 -- elements.
203 testSpecials :: Spec
204 testSpecials =
205 do it "script"
206 (renderText (script_ "alert('Hello, World!')") ==
207 "<script>alert('Hello, World!')</script>")
208 it "style"
209 (renderText (style_ "body{background:url('Hello, World!')}") ==
210 "<style>body{background:url('Hello, World!')}</style>")
211
212 -- | Elements which do not contain children.
213 testSelfClosing :: Spec
214 testSelfClosing =
215 do it "br" (renderText (br_ []) == "<br>")
216 it "hr" (renderText (hr_ []) == "<hr>")
217 it "input"
218 (renderText (input_ []) ==
219 "<input>")
220 it "input"
221 (renderText (input_ [type_ "text"]) ==
222 "<input type=\"text\">")
223
224 testCommuteHtmlT :: Spec
225 testCommuteHtmlT =
226 do it "makes using inner monads easy"
227 (example == renderText expected)
228 where
229 example = renderText $ evalState (commuteHtmlT exampleHtml) 1
230
231 exampleHtml :: (Applicative m, MonadState Int m) => HtmlT m ()
232 exampleHtml = ul_ $ replicateM_ 5 $ do
233 x <- get
234 put (x + 1)
235 li_ $ toHtml $ show x
236
237 expected = ul_ $ do
238 li_ "1"
239 li_ "2"
240 li_ "3"
241 li_ "4"
242 li_ "5"
243
244 testMonadFix :: Spec
245 testMonadFix =
246 do it "mdo" (renderText example == renderText expected)
247 where
248 toSectionId i = T.pack $ "section_" ++ show i
249
250 toSectionTitle i = T.pack $ "Section " ++ show i
251
252 example = mdo
253 forM_ sections $ \(sectionName, sectionId) ->
254 a_ [href_ sectionId] $ toHtml sectionName
255 sections <- forM [1 .. 2] $ \sectionNum -> do
256 let sectionId = toSectionId sectionNum
257 sectionTitle = toSectionTitle sectionNum
258 h1_ [id_ sectionId] $ toHtml sectionTitle
259 return (sectionTitle, sectionId)
260 return ()
261
262 expected = do
263 forM_ [1 .. 2] $ \sectionNum ->
264 a_ [href_ $ toSectionId sectionNum] $ toHtml $ toSectionTitle sectionNum
265 forM_ [1 .. 2] $ \sectionNum ->
266 h1_ [id_ $ toSectionId sectionNum] $ toHtml $ toSectionTitle sectionNum