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