1 {-# LANGUAGE OverloadedStrings #-}
5 import Test.Tasty.HUnit
7 import Data.Foldable (Foldable(..))
8 import Data.Function (($), (.))
9 import Data.Functor ((<$>))
11 import Data.Maybe (Maybe(..))
12 import Data.Monoid (Monoid(..))
13 import Data.Ord (Ord(..))
14 import Data.Semigroup (Semigroup(..))
15 import Data.String (String, IsString(..))
17 import Text.Show (Show(..))
18 import qualified Data.List as List
20 import Symantic.Document.Lang
21 import Symantic.Document.Plain
25 hunits = testGroup "HUnit" $
29 hunitPlain :: TestTree
30 hunitPlain = testList "Plain"
32 , "hello\nworld" ==> "hello\nworld"
33 , 10`maxWidth` breakpoints ["hello", "world"] ==> "helloworld"
34 , 9`maxWidth` breakpoints ["hello", "world"] ==> "hello\nworld"
35 , 6`maxWidth` breakpoints ["he", "ll", "o!"] ==> "hello!"
36 , 6`maxWidth` breakpoints ["he", "ll", "o!", "wo", "rl", "d!"] ==> "hello!\nworld!"
37 , 5`maxWidth` breakpoints ["hello", "world"] ==> "hello\nworld"
38 , 5`maxWidth` breakpoints ["he", "llo", "world"] ==> "hello\nworld"
39 , 5`maxWidth` breakpoints ["he", "ll", "o!"] ==> "hell\no!"
40 , 4`maxWidth` breakpoints ["hello", "world"] ==> "hello\nworld"
41 , 4`maxWidth` breakpoints ["he", "ll", "o!"] ==> "hell\no!"
42 , 4`maxWidth` breakpoints ["he", "llo", "world"] ==> "he\nllo\nworld"
43 , 4`maxWidth` breakpoints ["he", "llo", "w", "orld"] ==> "he\nllow\norld"
44 , 4`maxWidth` breakpoints ["he", "ll", "o!", "wo", "rl", "d!"] ==> "hell\no!wo\nrld!"
45 , 3`maxWidth` breakpoints ["hello", "world"] ==> "hello\nworld"
46 , 3`maxWidth` breakpoints ["he", "ll"] ==> "he\nll"
47 , 3`maxWidth` breakpoints ["he", "ll", "o!"] ==> "he\nll\no!"
48 , 1`maxWidth` breakpoints ["he", "ll", "o!"] ==> "he\nll\no!"
49 , 4`maxWidth` mconcat ["__", align $ breakpoints ["he", "ll", "o!", "wo", "rl", "d!"]]
50 ==> "__he\n ll\n o!\n wo\n rl\n d!"
51 , 6`maxWidth` mconcat ["__", align $ breakpoints ["he", "ll", "o!", "wo", "rl", "d!"]]
52 ==> "__hell\n o!wo\n rld!"
53 , 16`maxWidth` mconcat ["__", listHorV ["hello", "world"]] ==> "__[hello, world]"
54 , 4`maxWidth` mconcat ["__", listHorV ["hello", "world"]] ==> "__[ hello\n , world\n ]"
55 , 11`maxWidth` breakspaces ["hello", "world"] ==> "hello world"
56 , 10`maxWidth` breakspaces ["hello", "world"] ==> "hello\nworld"
57 , 6`maxWidth` breakspaces ["hel", "lo", "wo", "rld"] ==> "hel lo\nwo rld"
58 , 6`maxWidth` breakspaces ["hel", "lo", "wo", "rld", "HEL", "LO", "WO", "RLD"] ==> "hel lo\nwo rld\nHEL LO\nWO RLD"
59 , 5`maxWidth` breakspaces ["hello", "world"] ==> "hello\nworld"
60 , 19`maxWidth` fun (fun $ fun $ fun $ fun $ listHorV ["abcdefg", "abcdefg"])
61 ==> "function(function(\n function(\n function(\n function(\n [ abcdefg\n , abcdefg\n ]\n )\n )\n )\n ))"
62 , 19`maxWidth` fun (fun $ fun $ fun $ fun $ listHorV ["abcdefgh", "abcdefgh"])
63 ==> "function(\n function(\n function(\n function(\n function(\n [ abcdefgh\n , abcdefgh\n ]\n )\n )\n )\n )\n )"
64 , 7`maxWidth` ("hello"<>breakspace<>"world") ==> "hello\nworld"
65 , 7`maxWidth` ("hello "<>"world") ==> "hello\nworld"
66 , " "<> "hello\nworld\n!" ==> " hello\nworld\n!"
67 , "__"<>align "hello\nworld\n!" ==> "__hello\n world\n !"
68 , hang 2 "hello\nworld\n!" ==> "hello\n world\n !"
69 , hang 2 "hello\nworld\n!"<>"\nhello\n!" ==> "hello\n world\n !\nhello\n!"
70 , "let " <> align (catV $
71 (\(name, typ) -> fill 6 name <+> "::" <+> typ)
72 <$> [ ("abcdef","Doc")
73 , ("abcde","Int -> Doc -> Doc")
74 , ("abcdefghi","Doc") ])
75 ==> "let abcdef :: Doc\n abcde :: Int -> Doc -> Doc\n abcdefghi :: Doc"
76 , "let " <> align (catV $
77 (\(name, typ) -> fillOrBreak 6 name <> " ::" <+> typ)
78 <$> [ ("abcdef","Doc")
79 , ("abcde","Int -> Doc -> Doc")
80 , ("abcdefghi","Doc") ])
81 ==> "let abcdef :: Doc\n abcde :: Int -> Doc -> Doc\n abcdefghi\n :: Doc"
82 , "let " <> align (catV $
83 (\(name, typ) -> fillOrBreak 6 name <> " ::" <+> typ)
84 <$> [("abcdefghi","Doc ->\nDoc")])
85 ==> "let abcdefghi\n :: Doc ->\n Doc"
86 , "let " <> align (catV $
87 (\(name, typ) -> fillOrBreak 6 name <> align (" ::" <+> typ))
88 <$> [("abcdefghi","Doc ->\nDoc")])
89 ==> "let abcdefghi\n :: Doc ->\n Doc"
90 , 10 `maxWidth` "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15" ==> "1 2 3 4 5\n6 7 8 9 10\n11 12 13\n14 15"
91 , 10 `maxWidth` "a b "<>"12"<>align (" 34 5") ==> "a b 12 34\n 5"
92 , 10 `maxWidth` "a b "<>"12"<>align (" 34" <> align "") ==> "a b 12 34"
93 , 10 `maxWidth` "a b "<>"12"<>align (" 34" <> align " ") ==> "a b 12 34 "
94 , 10 `maxWidth` "a b "<>"12"<>align (" 34" <> align " 5") ==> "a b 12 34\n 5"
95 , 10 `maxWidth` "a b "<>"12"<>align (" 34" <> align " 56") ==> "a b 12\n 34\n 56"
96 , 10 `maxWidth` "a b "<>"12"<>align (" 34" <> align " 567") ==> "a b\n12 34 567"
97 , 10 `maxWidth` "a b "<>"12"<>align (" 34" <> align " 5678") ==> "a b\n12 34 5678"
98 , 10 `maxWidth` "a b "<>"12"<>align (" 34" <> align " 56789") ==> "a b\n12 34\n 56789"
99 , 10 `maxWidth` ("1234567890" <> " ") <> "1" ==> "1234567890\n1"
100 , 10 `maxWidth` nestedAlign 6 ==> "1 2 3 4 5\n 6"
101 , 10 `maxWidth` nestedAlign 7 ==> "1 2 3 4\n 5\n 6\n 7"
102 , 10 `maxWidth` nestedAlign 8 ==> "1 2 3\n 4\n 5\n 6\n 7\n 8"
103 , 10 `maxWidth` nestedAlign 9 ==> "1 2\n 3\n 4\n 5\n 6\n 7\n 8\n 9"
104 , 10 `maxWidth` nestedAlign 10 ==> "1\n 2\n 3\n 4\n 5\n 6\n 7\n 8\n 9\n 10"
106 , 10 `maxWidth` justify "1 2 3 4 5 6" ==> "1 2 3 4 5\n6"
107 -- justify compresses spaces
108 , 10 `maxWidth` justify "1 2 3 4 5 6" ==> "1 2 3 4 5\n6"
109 , 10 `maxWidth` justify " 1 2 3 4 5 6 7 8 9" ==> " 1 2 3 4 5\n6 7 8 9"
110 -- justify respects concatenating words
111 , 10 `maxWidth` justify (setWidth (Just 11) ("1 2 3"<>"4 5 6 7")) ==> "1 2 34 5 6\n7"
112 -- justify flushes the buffer before
113 , 10 `maxWidth` "__" <> align (justify "1 2 3 4 5") ==> "__1 2 3 4\n 5"
114 -- justify does not overflow the alignment
115 , 10 `maxWidth` justify (nestedAlign 6) ==> "1 2 3 4 5\n 6"
116 , 10 `maxWidth` justify ("a b c de " <> nestedAlign 2) ==> "a b c de\n1 2"
117 , 10 `maxWidth` justify (bold ("12 34 56 78 "<> underline "90" <> " 123 456 789"))
118 ==> "\ESC[1m12 34 56\n78 \ESC[4m90\ESC[0;1m 123\n456 789\ESC[0m"
119 -- justify does not justify on explicit newlines
120 , 10 `maxWidth` justify "1 2 3 4 5 6 7\n8 9 1 2 3 4 5" ==> "1 2 3 4 5\n6 7\n8 9 1 2 3\n4 5"
121 -- align flushes the buffer
122 , 10 `maxWidth` justify (ul ["1 2 3 4 5 6 7 8 9"])
126 -- ul/ol is mempty when no item
129 -- ul flushes the buffer
130 , 10 `maxWidth` justify (let i = "1 2 3 4 5 6 7 8 9" in ul [i, i])
137 , 10 `maxWidth` justify (let i = "1 2 3 4 5 6 7 8 9" in
138 ul [ul [i, i], ul [i, i]])
151 , 10 `maxWidth` justify (let i = "1 2 3 4 5 6 7 8 9" in
152 ol [ol [i, i], ol [i, i]])
173 -- endline breakspaces
174 , 10 `maxWidth` ("a"<>endline<>" b") ==> "a\nb"
175 -- endline does no justify
176 , 10 `maxWidth` justify ("a b"<>endline<>" c") ==> "a b\nc"
177 -- endline works overflowed
178 , 10 `maxWidth` justify ("abcdefghijk"<>endline<>" a") ==> "abcdefghijk\na"
179 -- endline prints no nothing
180 , 10 `maxWidth` justify ("12345678"<>endline<>"90ab"<>align (" cdefghijk cdefghijk"))
184 -- newline stops overflow
185 , 10 `maxWidth` breakalt "fits" "over"<>"\n"<>"12345678901"
188 -- breakalt triggers only if its first argument overflows,
189 -- not if what's next overflows.
190 , 10 `maxWidth` spaces 2<>align(breakalt "fits" "over"<>newline<>"12345678901")
193 -- handle escaping correctly over custom indenting
194 , 10 `maxWidth` setIndent (blue "X") 1 (red ("12"<>green "4\n5" <> "6"))
195 ==> "\ESC[31m12\ESC[32m4\n\ESC[34mX\ESC[0;31;32m5\ESC[0;31m6\ESC[0m"
196 , 10 `maxWidth` setIndent (blue "X") 1 (justify (red ("1 2 3 4"<>green " 5 6 " <> "7 ") <> "8"))
197 ==> "\ESC[31m1 2 3 4\ESC[32m 5\n\ESC[34mX\ESC[0;31;32m6 \ESC[0;31m7 \ESC[0m8"
198 -- breakspace backtracking is bounded by the removable indentation
199 -- (hence it can actually wrap a few words in reasonable time).
201 "Lorem ipsum dolor sit amet, Nulla nec tortor. Donec id elit quis purus\
202 \ consectetur consequat. Nam congue semper tellus. Sed erat dolor,\
203 \ dapibus sit amet, venenatis ornare, ultrices ut, nisi. Aliquam ante.\
204 \ Suspendisse scelerisque dui nec velit. Duis augue augue, gravida euismod,\
205 \ vulputate ac, facilisis id, sem. Morbi in orci. Nulla purus lacus,\
206 \ pulvinar vel, malesuada ac, mattis nec, quam. Nam molestie scelerisque\
207 \ quam. Nullam feugiat cursus lacus.orem ipsum dolor sit amet, consectetur\
208 \ adipiscing elit. Donec libero risus, commodo vitae, pharetra mollis,\
209 \ posuere eu, pede. Nulla nec tortor. Donec id elit quis purus consectetur\
210 \ consequat. Nam congue semper tellus. Sed erat dolor, dapibus sit\
211 \ amet, venenatis ornare, ultrices ut, nisi. Aliquam ante. Suspendisse\
212 \ scelerisque dui nec velit. Duis augue augue, gravida euismod, vulputate ac,\
213 \ facilisis id, sem. Morbi in orci. Nulla purus lacus, pulvinar vel,\
214 \ malesuada ac, mattis nec, quam. Nam molestie scelerisque quam.\
215 \ Nullam feugiat cursus lacus.orem ipsum dolor sit amet, consectetur\
216 \ adipiscing elit. Donec libero risus, commodo vitae, pharetra mollis,\
217 \ posuere eu, pede. Nulla nec tortor. Donec id elit quis purus consectetur\
218 \ consequat. Nam congue semper tellus. Sed erat dolor, dapibus sit amet,\
219 \ venenatis ornare, ultrices ut, nisi. Aliquam ante. Suspendisse\
220 \ scelerisque dui nec velit. Duis augue augue, gravida euismod, vulputate ac,\
221 \ facilisis id, sem. Morbi in orci. Nulla purus lacus, pulvinar vel,\
222 \ malesuada ac, mattis nec, quam. Nam molestie scelerisque quam. Nullam\
223 \ feugiat cursus lacus.orem ipsum dolor sit amet, consectetur adipiscing\
224 \ elit. Donec libero risus, commodo vitae, pharetra mollis, posuere eu, pede.\
225 \ Nulla nec tortor. Donec id elit quis purus consectetur consequat. Nam\
226 \ congue semper tellus. Sed erat dolor, dapibus sit amet, venenatis ornare,\
227 \ ultrices ut, nisi."
228 ==> "Lorem ipsum dolor sit amet, Nulla nec tortor. Donec id elit quis\
229 \ purus\nconsectetur consequat. Nam congue semper tellus. Sed erat dolor, dapibus\
230 \ sit\namet, venenatis ornare, ultrices ut, nisi. Aliquam ante. Suspendisse\
231 \ scelerisque\ndui nec velit. Duis augue augue, gravida euismod, vulputate ac,\
232 \ facilisis id,\nsem. Morbi in orci. Nulla purus lacus, pulvinar vel, malesuada\
233 \ ac, mattis nec,\nquam. Nam molestie scelerisque quam. Nullam feugiat cursus\
234 \ lacus.orem ipsum\ndolor sit amet, consectetur adipiscing elit. Donec libero\
235 \ risus, commodo vitae,\npharetra mollis, posuere eu, pede. Nulla nec tortor.\
236 \ Donec id elit quis purus\nconsectetur consequat. Nam congue semper tellus. Sed\
237 \ erat dolor, dapibus sit\namet, venenatis ornare, ultrices ut, nisi. Aliquam\
238 \ ante. Suspendisse scelerisque\ndui nec velit. Duis augue augue, gravida\
239 \ euismod, vulputate ac, facilisis id,\nsem. Morbi in orci. Nulla purus lacus,\
240 \ pulvinar vel, malesuada ac, mattis nec,\nquam. Nam molestie scelerisque quam.\
241 \ Nullam feugiat cursus lacus.orem ipsum\ndolor sit amet, consectetur adipiscing\
242 \ elit. Donec libero risus, commodo vitae,\npharetra mollis, posuere eu, pede.\
243 \ Nulla nec tortor. Donec id elit quis purus\nconsectetur consequat. Nam congue\
244 \ semper tellus. Sed erat dolor, dapibus sit\namet, venenatis ornare, ultrices\
245 \ ut, nisi. Aliquam ante. Suspendisse scelerisque\ndui nec velit. Duis augue\
246 \ augue, gravida euismod, vulputate ac, facilisis id,\nsem. Morbi in orci. Nulla\
247 \ purus lacus, pulvinar vel, malesuada ac, mattis nec,\nquam. Nam molestie\
248 \ scelerisque quam. Nullam feugiat cursus lacus.orem ipsum\ndolor sit amet,\
249 \ consectetur adipiscing elit. Donec libero risus, commodo vitae,\npharetra\
250 \ mollis, posuere eu, pede. Nulla nec tortor. Donec id elit quis\
251 \ purus\nconsectetur consequat. Nam congue semper tellus. Sed erat dolor, dapibus\
252 \ sit\namet, venenatis ornare, ultrices ut, nisi."
253 , 80 `maxWidth` justify
254 "Lorem ipsum dolor sit amet, Nulla nec tortor. Donec id elit quis purus\
255 \ consectetur consequat. Nam congue semper tellus. Sed erat dolor,\
256 \ dapibus sit amet, venenatis ornare, ultrices ut, nisi. Aliquam ante.\
257 \ Suspendisse scelerisque dui nec velit. Duis augue augue, gravida euismod,\
258 \ vulputate ac, facilisis id, sem. Morbi in orci. Nulla purus lacus,\
259 \ pulvinar vel, malesuada ac, mattis nec, quam. Nam molestie scelerisque\
260 \ quam. Nullam feugiat cursus lacus.orem ipsum dolor sit amet, consectetur\
261 \ adipiscing elit. Donec libero risus, commodo vitae, pharetra mollis,\
262 \ posuere eu, pede. Nulla nec tortor. Donec id elit quis purus consectetur\
263 \ consequat. Nam congue semper tellus. Sed erat dolor, dapibus sit\
264 \ amet, venenatis ornare, ultrices ut, nisi. Aliquam ante. Suspendisse\
265 \ scelerisque dui nec velit. Duis augue augue, gravida euismod, vulputate ac,\
266 \ facilisis id, sem. Morbi in orci. Nulla purus lacus, pulvinar vel,\
267 \ malesuada ac, mattis nec, quam. Nam molestie scelerisque quam.\
268 \ Nullam feugiat cursus lacus.orem ipsum dolor sit amet, consectetur\
269 \ adipiscing elit. Donec libero risus, commodo vitae, pharetra mollis,\
270 \ posuere eu, pede. Nulla nec tortor. Donec id elit quis purus consectetur\
271 \ consequat. Nam congue semper tellus. Sed erat dolor, dapibus sit amet,\
272 \ venenatis ornare, ultrices ut, nisi. Aliquam ante. Suspendisse\
273 \ scelerisque dui nec velit. Duis augue augue, gravida euismod, vulputate ac,\
274 \ facilisis id, sem. Morbi in orci. Nulla purus lacus, pulvinar vel,\
275 \ malesuada ac, mattis nec, quam. Nam molestie scelerisque quam. Nullam\
276 \ feugiat cursus lacus.orem ipsum dolor sit amet, consectetur adipiscing\
277 \ elit. Donec libero risus, commodo vitae, pharetra mollis, posuere eu, pede.\
278 \ Nulla nec tortor. Donec id elit quis purus consectetur consequat. Nam\
279 \ congue semper tellus. Sed erat dolor, dapibus sit amet, venenatis ornare,\
280 \ ultrices ut, nisi."
281 ==> "Lorem ipsum dolor sit amet, Nulla nec tortor. Donec id elit quis purus\n\
282 \consectetur consequat. Nam congue semper tellus. Sed erat dolor, dapibus sit\n\
283 \amet, venenatis ornare, ultrices ut, nisi. Aliquam ante. Suspendisse scelerisque\n\
284 \dui nec velit. Duis augue augue, gravida euismod, vulputate ac, facilisis id,\n\
285 \sem. Morbi in orci. Nulla purus lacus, pulvinar vel, malesuada ac, mattis nec,\n\
286 \quam. Nam molestie scelerisque quam. Nullam feugiat cursus lacus.orem ipsum\n\
287 \dolor sit amet, consectetur adipiscing elit. Donec libero risus, commodo vitae,\n\
288 \pharetra mollis, posuere eu, pede. Nulla nec tortor. Donec id elit quis purus\n\
289 \consectetur consequat. Nam congue semper tellus. Sed erat dolor, dapibus sit\n\
290 \amet, venenatis ornare, ultrices ut, nisi. Aliquam ante. Suspendisse scelerisque\n\
291 \dui nec velit. Duis augue augue, gravida euismod, vulputate ac, facilisis id,\n\
292 \sem. Morbi in orci. Nulla purus lacus, pulvinar vel, malesuada ac, mattis nec,\n\
293 \quam. Nam molestie scelerisque quam. Nullam feugiat cursus lacus.orem ipsum\n\
294 \dolor sit amet, consectetur adipiscing elit. Donec libero risus, commodo vitae,\n\
295 \pharetra mollis, posuere eu, pede. Nulla nec tortor. Donec id elit quis purus\n\
296 \consectetur consequat. Nam congue semper tellus. Sed erat dolor, dapibus sit\n\
297 \amet, venenatis ornare, ultrices ut, nisi. Aliquam ante. Suspendisse scelerisque\n\
298 \dui nec velit. Duis augue augue, gravida euismod, vulputate ac, facilisis id,\n\
299 \sem. Morbi in orci. Nulla purus lacus, pulvinar vel, malesuada ac, mattis nec,\n\
300 \quam. Nam molestie scelerisque quam. Nullam feugiat cursus lacus.orem ipsum\n\
301 \dolor sit amet, consectetur adipiscing elit. Donec libero risus, commodo vitae,\n\
302 \pharetra mollis, posuere eu, pede. Nulla nec tortor. Donec id elit quis purus\n\
303 \consectetur consequat. Nam congue semper tellus. Sed erat dolor, dapibus sit\n\
304 \amet, venenatis ornare, ultrices ut, nisi."
307 (==>) :: IsString d => d ~ String => Plain d -> d -> Assertion; infix 0 ==>
308 p ==> exp = got @?= exp
309 where got = runPlain p
311 testList :: String -> [Assertion] -> TestTree
312 testList n as = testGroup n $ List.zipWith testCase (show <$> [1::Int ..]) as
314 breakpoints :: Wrappable d => Monoid d => [d] -> d
315 breakpoints = intercalate breakpoint
317 breakspaces :: Wrappable d => Monoid d => [d] -> d
318 breakspaces = intercalate breakspace
321 maxWidth :: Wrappable d => Width -> d -> d
322 maxWidth = setWidth . Just
325 From (Line String) d =>
326 Spaceable d => Indentable d => Wrappable d =>
331 from (Line (show i)) <>
332 (if n <= i then mempty
333 else align (breakspace <> go (i+1)))
335 listHorV :: IsString d => Indentable d => Wrappable d => [d] -> d
337 listHorV [d] = "["<>d<>"]"
340 ("[" <> intercalate ("," <> space) ds <> "]")
341 (align $ "[" <> space
342 <> foldr1 (\a acc -> a <> newline <> "," <> space <> acc) ds
345 fun :: IsString d => Indentable d => Wrappable d => d -> d
346 fun d = "function(" <> incrIndent (spaces 2) 2 (breakalt d (newline<>d<>newline)) <> ")"