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.Ord (Ord(..))
13 import Data.String (String, IsString(..))
15 import Text.Show (Show(..))
16 import qualified Data.List as List
18 import Symantic.Formatter.Class
19 import Symantic.Formatter.Plain (Plain, runPlain)
23 hunits = testGroup "HUnit" $
27 hunitPlain :: TestTree
28 hunitPlain = testList "Plain"
30 , "hello".>"world" ==> "helloworld"
31 , "hello".>newline.>"world" ==> "hello\nworld"
32 , "hello\nworld" ==> "hello\nworld"
33 , 10`maxWidth` breakpoints ["hello", "world"] ==> "helloworld"
34 , 9`maxWidth` "hello" .> breakpoint .> "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` concat ["__", align $ breakpoints ["he", "ll", "o!", "wo", "rl", "d!"]]
50 ==> "__he\n ll\n o!\n wo\n rl\n d!"
52 (4`maxWidth` "__" .> align (intercalate_ breakpoint string))
53 ["he", "ll", "o!", "wo", "rl", "d!"]
54 "__he\n ll\n o!\n wo\n rl\n d!"
55 , 6`maxWidth` concat ["__", align $ breakpoints ["he", "ll", "o!", "wo", "rl", "d!"]]
56 ==> "__hell\n o!wo\n rld!"
58 (6`maxWidth` "__" .> align (intercalate_ breakpoint string))
59 ["he", "ll", "o!", "wo", "rl", "d!"]
60 "__hell\n o!wo\n rld!"
61 , 16`maxWidth` concat ["__", listHorV ["hello", "world"]] ==> "__[hello, world]"
63 (16`maxWidth` "__" .> bracketList string)
66 , 4`maxWidth` concat ["__", listHorV ["hello", "world"]] ==> "__[ hello\n , world\n ]"
67 , 11`maxWidth` breakspaces ["hello", "world"] ==> "hello world"
68 , 10`maxWidth` breakspaces ["hello", "world"] ==> "hello\nworld"
69 , 6`maxWidth` breakspaces ["hel", "lo", "wo", "rld"] ==> "hel lo\nwo rld"
70 , 6`maxWidth` breakspaces ["hel", "lo", "wo", "rld", "HEL", "LO", "WO", "RLD"] ==> "hel lo\nwo rld\nHEL LO\nWO RLD"
71 , 5`maxWidth` breakspaces ["hello", "world"] ==> "hello\nworld"
72 , 19`maxWidth` fun (fun $ fun $ fun $ fun $ listHorV ["abcdefg", "abcdefg"])
73 ==> "function(function(\n function(\n function(\n function(\n [ abcdefg\n , abcdefg\n ]\n )\n )\n )\n ))"
74 , 19`maxWidth` fun (fun $ fun $ fun $ fun $ listHorV ["abcdefgh", "abcdefgh"])
75 ==> "function(\n function(\n function(\n function(\n function(\n [ abcdefgh\n , abcdefgh\n ]\n )\n )\n )\n )\n )"
76 , 7`maxWidth` ("hello".>breakspace.>"world") ==> "hello\nworld"
77 , 7`maxWidth` ("hello ".>"world") ==> "hello\nworld"
78 , " ".> "hello\nworld\n!" ==> " hello\nworld\n!"
79 , "__".>align "hello\nworld\n!" ==> "__hello\n world\n !"
80 , hang 2 "hello\nworld\n!" ==> "hello\n world\n !"
81 , hang 2 "hello\nworld\n!".>"\nhello\n!" ==> "hello\n world\n !\nhello\n!"
82 , "let " .> align (catV $
83 (\(name, typ) -> fill 6 name <+ "::" +> typ)
84 <$> [ ("abcdef","Doc")
85 , ("abcde","Int -> Doc -> Doc")
86 , ("abcdefghi","Doc") ])
87 ==> "let abcdef :: Doc\n abcde :: Int -> Doc -> Doc\n abcdefghi :: Doc"
88 , "let " .> align (catV $
89 (\(name, typ) -> fillOrBreak 6 name <. " ::" +> typ)
90 <$> [ ("abcdef","Doc")
91 , ("abcde","Int -> Doc -> Doc")
92 , ("abcdefghi","Doc") ])
93 ==> "let abcdef :: Doc\n abcde :: Int -> Doc -> Doc\n abcdefghi\n :: Doc"
94 , "let " .> align (catV $
95 (\(name, typ) -> fillOrBreak 6 name <. " ::" +> typ)
96 <$> [("abcdefghi","Doc ->\nDoc")])
97 ==> "let abcdefghi\n :: Doc ->\n Doc"
98 , "let " .> align (catV $
99 (\(name, typ) -> fillOrBreak 6 name <. align (" ::" +> typ))
100 <$> [("abcdefghi","Doc ->\nDoc")])
101 ==> "let abcdefghi\n :: Doc ->\n Doc"
102 , 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"
103 , 10 `maxWidth` "a b ".>"12".>align (" 34 5") ==> "a b 12 34\n 5"
104 , 10 `maxWidth` "a b ".>"12".>align (" 34" .> align "") ==> "a b 12 34"
105 , 10 `maxWidth` "a b ".>"12".>align (" 34" .> align " ") ==> "a b 12 34 "
106 , 10 `maxWidth` "a b ".>"12".>align (" 34" .> align " 5") ==> "a b 12 34\n 5"
107 , 10 `maxWidth` "a b ".>"12".>align (" 34" .> align " 56") ==> "a b 12\n 34\n 56"
108 , 10 `maxWidth` "a b ".>"12".>align (" 34" .> align " 567") ==> "a b\n12 34 567"
109 , 10 `maxWidth` "a b ".>"12".>align (" 34" .> align " 5678") ==> "a b\n12 34 5678"
110 , 10 `maxWidth` "a b ".>"12".>align (" 34" .> align " 56789") ==> "a b\n12 34\n 56789"
111 , 10 `maxWidth` ("1234567890" .> " ") .> "1" ==> "1234567890\n1"
112 , 10 `maxWidth` nestedAlign 6 ==> "1 2 3 4 5\n 6"
113 , 10 `maxWidth` nestedAlign 7 ==> "1 2 3 4\n 5\n 6\n 7"
114 , 10 `maxWidth` nestedAlign 8 ==> "1 2 3\n 4\n 5\n 6\n 7\n 8"
115 , 10 `maxWidth` nestedAlign 9 ==> "1 2\n 3\n 4\n 5\n 6\n 7\n 8\n 9"
116 , 10 `maxWidth` nestedAlign 10 ==> "1\n 2\n 3\n 4\n 5\n 6\n 7\n 8\n 9\n 10"
118 , 10 `maxWidth` justify "1 2 3 4 5 6" ==> "1 2 3 4 5\n6"
119 -- justify compresses spaces
120 , 10 `maxWidth` justify "1 2 3 4 5 6" ==> "1 2 3 4 5\n6"
121 , 10 `maxWidth` justify " 1 2 3 4 5 6 7 8 9" ==> " 1 2 3 4 5\n6 7 8 9"
122 -- justify respects concatenating words
123 , 10 `maxWidth` justify (setWidth (Just 11) ("1 2 3".>"4 5 6 7")) ==> "1 2 34 5 6\n7"
124 -- justify flushes the buffer before
125 , 10 `maxWidth` "__" .> align (justify "1 2 3 4 5") ==> "__1 2 3 4\n 5"
126 -- justify does not overflow the alignment
127 , 10 `maxWidth` justify (nestedAlign 6) ==> "1 2 3 4 5\n 6"
128 , 10 `maxWidth` justify ("a b c de " .> nestedAlign 2) ==> "a b c de\n1 2"
129 , 10 `maxWidth` justify (bold ("12 34 56 78 ".> underline "90" .> " 123 456 789"))
130 ==> "\ESC[1m12 34 56\n78 \ESC[4m90\ESC[0;1m 123\n456 789\ESC[0m"
131 -- justify does not justify on explicit newlines
132 , 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"
133 -- align flushes the buffer
134 , 10 `maxWidth` justify (ul ["1 2 3 4 5 6 7 8 9"])
138 -- ul/ol is empty when no item
141 -- ul flushes the buffer
142 , 10 `maxWidth` justify (let i = "1 2 3 4 5 6 7 8 9" in ul [i, i])
149 , testPlain (10 `maxWidth` justify (unorderedList (unwords_ int))) (let i = [1..9] in [i, i])
156 , testPlain (11 `maxWidth` justify (orderedList (unwords_ int))) (let i = [1..9] in [i, i])
163 , 10 `maxWidth` justify (let i = "1 2 3 4 5 6 7 8 9" in
164 ul [ul [i, i], ul [i, i]])
177 , 10 `maxWidth` justify (let i = "1 2 3 4 5 6 7 8 9" in
178 ol [ol [i, i], ol [i, i]])
199 -- endline breakspaces
200 , 10 `maxWidth` ("a".>endline.>" b") ==> "a\nb"
201 -- endline does no justify
202 , 10 `maxWidth` justify ("a b".>endline.>" c") ==> "a b\nc"
203 -- endline works overflowed
204 , 10 `maxWidth` justify ("abcdefghijk".>endline.>" a") ==> "abcdefghijk\na"
205 -- endline prints no nothing
206 , 10 `maxWidth` justify ("12345678".>endline.>"90ab".>align (" cdefghijk cdefghijk"))
210 -- newline stops overflow
211 , 10 `maxWidth` breakalt "fits" "over".>"\n".>"12345678901"
214 -- breakalt triggers only if its first argument overflows,
215 -- not if what's next overflows.
216 , 10 `maxWidth` spaces 2.>align(breakalt "fits" "over".>newline.>"12345678901")
219 -- handle escaping correctly over custom indenting
220 , 10 `maxWidth` setIndent (blue "X") 1 (red ("12".>green "4\n5" .> "6"))
221 ==> "\ESC[31m12\ESC[32m4\n\ESC[34mX\ESC[0;31;32m5\ESC[0;31m6\ESC[0m"
222 , 10 `maxWidth` setIndent (blue "X") 1 (justify (red ("1 2 3 4".>green " 5 6 " .> "7 ") .> "8"))
223 ==> "\ESC[31m1 2 3 4\ESC[32m 5\n\ESC[34mX\ESC[0;31;32m6 \ESC[0;31m7 \ESC[0m8"
224 -- breakspace backtracking is bounded by the removable indentation
225 -- (hence it can actually wrap a few words in reasonable time).
227 "Lorem ipsum dolor sit amet, Nulla nec tortor. Donec id elit quis purus\
228 \ consectetur consequat. Nam congue semper tellus. Sed erat dolor,\
229 \ dapibus sit amet, venenatis ornare, ultrices ut, nisi. Aliquam ante.\
230 \ Suspendisse scelerisque dui nec velit. Duis augue augue, gravida euismod,\
231 \ vulputate ac, facilisis id, sem. Morbi in orci. Nulla purus lacus,\
232 \ pulvinar vel, malesuada ac, mattis nec, quam. Nam molestie scelerisque\
233 \ quam. Nullam feugiat cursus lacus.orem ipsum dolor sit amet, consectetur\
234 \ adipiscing elit. Donec libero risus, commodo vitae, pharetra mollis,\
235 \ posuere eu, pede. Nulla nec tortor. Donec id elit quis purus consectetur\
236 \ consequat. Nam congue semper tellus. Sed erat dolor, dapibus sit\
237 \ amet, venenatis ornare, ultrices ut, nisi. Aliquam ante. Suspendisse\
238 \ scelerisque dui nec velit. Duis augue augue, gravida euismod, vulputate ac,\
239 \ facilisis id, sem. Morbi in orci. Nulla purus lacus, pulvinar vel,\
240 \ malesuada ac, mattis nec, quam. Nam molestie scelerisque quam.\
241 \ Nullam feugiat cursus lacus.orem ipsum dolor sit amet, consectetur\
242 \ adipiscing elit. Donec libero risus, commodo vitae, pharetra mollis,\
243 \ posuere eu, pede. Nulla nec tortor. Donec id elit quis purus consectetur\
244 \ consequat. Nam congue semper tellus. Sed erat dolor, dapibus sit amet,\
245 \ venenatis ornare, ultrices ut, nisi. Aliquam ante. Suspendisse\
246 \ scelerisque dui nec velit. Duis augue augue, gravida euismod, vulputate ac,\
247 \ facilisis id, sem. Morbi in orci. Nulla purus lacus, pulvinar vel,\
248 \ malesuada ac, mattis nec, quam. Nam molestie scelerisque quam. Nullam\
249 \ feugiat cursus lacus.orem ipsum dolor sit amet, consectetur adipiscing\
250 \ elit. Donec libero risus, commodo vitae, pharetra mollis, posuere eu, pede.\
251 \ Nulla nec tortor. Donec id elit quis purus consectetur consequat. Nam\
252 \ congue semper tellus. Sed erat dolor, dapibus sit amet, venenatis ornare,\
253 \ ultrices ut, nisi."
254 ==> "Lorem ipsum dolor sit amet, Nulla nec tortor. Donec id elit quis\
255 \ purus\nconsectetur consequat. Nam congue semper tellus. Sed erat dolor, dapibus\
256 \ sit\namet, venenatis ornare, ultrices ut, nisi. Aliquam ante. Suspendisse\
257 \ scelerisque\ndui nec velit. Duis augue augue, gravida euismod, vulputate ac,\
258 \ facilisis id,\nsem. Morbi in orci. Nulla purus lacus, pulvinar vel, malesuada\
259 \ ac, mattis nec,\nquam. Nam molestie scelerisque quam. Nullam feugiat cursus\
260 \ lacus.orem ipsum\ndolor sit amet, consectetur adipiscing elit. Donec libero\
261 \ risus, commodo vitae,\npharetra mollis, posuere eu, pede. Nulla nec tortor.\
262 \ Donec id elit quis purus\nconsectetur consequat. Nam congue semper tellus. Sed\
263 \ erat dolor, dapibus sit\namet, venenatis ornare, ultrices ut, nisi. Aliquam\
264 \ ante. Suspendisse scelerisque\ndui nec velit. Duis augue augue, gravida\
265 \ euismod, vulputate ac, facilisis id,\nsem. Morbi in orci. Nulla purus lacus,\
266 \ pulvinar vel, malesuada ac, mattis nec,\nquam. Nam molestie scelerisque quam.\
267 \ Nullam feugiat cursus lacus.orem ipsum\ndolor sit amet, consectetur adipiscing\
268 \ elit. Donec libero risus, commodo vitae,\npharetra mollis, posuere eu, pede.\
269 \ Nulla nec tortor. Donec id elit quis purus\nconsectetur consequat. Nam congue\
270 \ semper tellus. Sed erat dolor, dapibus sit\namet, venenatis ornare, ultrices\
271 \ ut, nisi. Aliquam ante. Suspendisse scelerisque\ndui nec velit. Duis augue\
272 \ augue, gravida euismod, vulputate ac, facilisis id,\nsem. Morbi in orci. Nulla\
273 \ purus lacus, pulvinar vel, malesuada ac, mattis nec,\nquam. Nam molestie\
274 \ scelerisque quam. Nullam feugiat cursus lacus.orem ipsum\ndolor sit amet,\
275 \ consectetur adipiscing elit. Donec libero risus, commodo vitae,\npharetra\
276 \ mollis, posuere eu, pede. Nulla nec tortor. Donec id elit quis\
277 \ purus\nconsectetur consequat. Nam congue semper tellus. Sed erat dolor, dapibus\
278 \ sit\namet, venenatis ornare, ultrices ut, nisi."
279 , 80 `maxWidth` justify
280 "Lorem ipsum dolor sit amet, Nulla nec tortor. Donec id elit quis purus\
281 \ consectetur consequat. Nam congue semper tellus. Sed erat dolor,\
282 \ dapibus sit amet, venenatis ornare, ultrices ut, nisi. Aliquam ante.\
283 \ Suspendisse scelerisque dui nec velit. Duis augue augue, gravida euismod,\
284 \ vulputate ac, facilisis id, sem. Morbi in orci. Nulla purus lacus,\
285 \ pulvinar vel, malesuada ac, mattis nec, quam. Nam molestie scelerisque\
286 \ quam. Nullam feugiat cursus lacus.orem ipsum dolor sit amet, consectetur\
287 \ adipiscing elit. Donec libero risus, commodo vitae, pharetra mollis,\
288 \ posuere eu, pede. Nulla nec tortor. Donec id elit quis purus consectetur\
289 \ consequat. Nam congue semper tellus. Sed erat dolor, dapibus sit\
290 \ amet, venenatis ornare, ultrices ut, nisi. Aliquam ante. Suspendisse\
291 \ scelerisque dui nec velit. Duis augue augue, gravida euismod, vulputate ac,\
292 \ facilisis id, sem. Morbi in orci. Nulla purus lacus, pulvinar vel,\
293 \ malesuada ac, mattis nec, quam. Nam molestie scelerisque quam.\
294 \ Nullam feugiat cursus lacus.orem ipsum dolor sit amet, consectetur\
295 \ adipiscing elit. Donec libero risus, commodo vitae, pharetra mollis,\
296 \ posuere eu, pede. Nulla nec tortor. Donec id elit quis purus consectetur\
297 \ consequat. Nam congue semper tellus. Sed erat dolor, dapibus sit amet,\
298 \ venenatis ornare, ultrices ut, nisi. Aliquam ante. Suspendisse\
299 \ scelerisque dui nec velit. Duis augue augue, gravida euismod, vulputate ac,\
300 \ facilisis id, sem. Morbi in orci. Nulla purus lacus, pulvinar vel,\
301 \ malesuada ac, mattis nec, quam. Nam molestie scelerisque quam. Nullam\
302 \ feugiat cursus lacus.orem ipsum dolor sit amet, consectetur adipiscing\
303 \ elit. Donec libero risus, commodo vitae, pharetra mollis, posuere eu, pede.\
304 \ Nulla nec tortor. Donec id elit quis purus consectetur consequat. Nam\
305 \ congue semper tellus. Sed erat dolor, dapibus sit amet, venenatis ornare,\
306 \ ultrices ut, nisi."
307 ==> "Lorem ipsum dolor sit amet, Nulla nec tortor. Donec id elit quis purus\n\
308 \consectetur consequat. Nam congue semper tellus. Sed erat dolor, dapibus sit\n\
309 \amet, venenatis ornare, ultrices ut, nisi. Aliquam ante. Suspendisse scelerisque\n\
310 \dui nec velit. Duis augue augue, gravida euismod, vulputate ac, facilisis id,\n\
311 \sem. Morbi in orci. Nulla purus lacus, pulvinar vel, malesuada ac, mattis nec,\n\
312 \quam. Nam molestie scelerisque quam. Nullam feugiat cursus lacus.orem ipsum\n\
313 \dolor sit amet, consectetur adipiscing elit. Donec libero risus, commodo vitae,\n\
314 \pharetra mollis, posuere eu, pede. Nulla nec tortor. Donec id elit quis purus\n\
315 \consectetur consequat. Nam congue semper tellus. Sed erat dolor, dapibus sit\n\
316 \amet, venenatis ornare, ultrices ut, nisi. Aliquam ante. Suspendisse scelerisque\n\
317 \dui nec velit. Duis augue augue, gravida euismod, vulputate ac, facilisis id,\n\
318 \sem. Morbi in orci. Nulla purus lacus, pulvinar vel, malesuada ac, mattis nec,\n\
319 \quam. Nam molestie scelerisque quam. Nullam feugiat cursus lacus.orem ipsum\n\
320 \dolor sit amet, consectetur adipiscing elit. Donec libero risus, commodo vitae,\n\
321 \pharetra mollis, posuere eu, pede. Nulla nec tortor. Donec id elit quis purus\n\
322 \consectetur consequat. Nam congue semper tellus. Sed erat dolor, dapibus sit\n\
323 \amet, venenatis ornare, ultrices ut, nisi. Aliquam ante. Suspendisse scelerisque\n\
324 \dui nec velit. Duis augue augue, gravida euismod, vulputate ac, facilisis id,\n\
325 \sem. Morbi in orci. Nulla purus lacus, pulvinar vel, malesuada ac, mattis nec,\n\
326 \quam. Nam molestie scelerisque quam. Nullam feugiat cursus lacus.orem ipsum\n\
327 \dolor sit amet, consectetur adipiscing elit. Donec libero risus, commodo vitae,\n\
328 \pharetra mollis, posuere eu, pede. Nulla nec tortor. Donec id elit quis purus\n\
329 \consectetur consequat. Nam congue semper tellus. Sed erat dolor, dapibus sit\n\
330 \amet, venenatis ornare, ultrices ut, nisi."
333 (==>) :: IsString o => o ~ String => Plain o () -> o -> Assertion; infix 0 ==>
334 fmt ==> exp = got @?= exp
335 where got = runPlain fmt ()
336 testPlain :: IsString o => o ~ String => Plain o a -> a -> o -> Assertion
337 testPlain fmt a exp = got @?= exp
338 where got = runPlain fmt a
340 testList :: String -> [Assertion] -> TestTree
341 testList n as = testGroup n $ List.zipWith testCase (show <$> [1::Int ..]) as
345 ProductFunctor repr =>
348 breakpoints = intercalate breakpoint
352 ProductFunctor repr =>
355 breakspaces = intercalate breakspace
358 maxWidth :: Wrappable repr => Width -> repr a -> repr a
359 maxWidth = setWidth . Just
362 IsString (repr ()) =>
365 ProductFunctor repr =>
371 fromString (show i) .>
372 (if n <= i then empty
373 else align (breakspace .> go (i+1)))
376 IsString (repr ()) =>
379 ProductFunctor repr =>
384 listHorV [t] = "[".>t<."]"
387 ("[" .> intercalate ("," .> space) ts <. "]")
388 (align $ "[" .> space
389 .> foldr1 (\a acc -> a <. newline <. "," <. space <. acc) ts
394 ProductFunctor repr =>
397 IsString (repr ()) =>
400 .> incrIndent (spaces 2) 2 (breakalt t (newline.>t<.newline))