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 -- breakspace backtracking is bounded by the removable indentation
175 -- (hence it can actually wrap a few words in reasonable time).
177 "Lorem ipsum dolor sit amet, Nulla nec tortor. Donec id elit quis purus\
178 \ consectetur consequat. Nam congue semper tellus. Sed erat dolor,\
179 \ dapibus sit amet, venenatis ornare, ultrices ut, nisi. Aliquam ante.\
180 \ Suspendisse scelerisque dui nec velit. Duis augue augue, gravida euismod,\
181 \ vulputate ac, facilisis id, sem. Morbi in orci. Nulla purus lacus,\
182 \ pulvinar vel, malesuada ac, mattis nec, quam. Nam molestie scelerisque\
183 \ quam. Nullam feugiat cursus lacus.orem ipsum dolor sit amet, consectetur\
184 \ adipiscing elit. Donec libero risus, commodo vitae, pharetra mollis,\
185 \ posuere eu, pede. Nulla nec tortor. Donec id elit quis purus consectetur\
186 \ consequat. Nam congue semper tellus. Sed erat dolor, dapibus sit\
187 \ amet, venenatis ornare, ultrices ut, nisi. Aliquam ante. Suspendisse\
188 \ scelerisque dui nec velit. Duis augue augue, gravida euismod, vulputate ac,\
189 \ facilisis id, sem. Morbi in orci. Nulla purus lacus, pulvinar vel,\
190 \ malesuada ac, mattis nec, quam. Nam molestie scelerisque quam.\
191 \ Nullam feugiat cursus lacus.orem ipsum dolor sit amet, consectetur\
192 \ adipiscing elit. Donec libero risus, commodo vitae, pharetra mollis,\
193 \ posuere eu, pede. Nulla nec tortor. Donec id elit quis purus consectetur\
194 \ consequat. Nam congue semper tellus. Sed erat dolor, dapibus sit amet,\
195 \ venenatis ornare, ultrices ut, nisi. Aliquam ante. Suspendisse\
196 \ scelerisque dui nec velit. Duis augue augue, gravida euismod, vulputate ac,\
197 \ facilisis id, sem. Morbi in orci. Nulla purus lacus, pulvinar vel,\
198 \ malesuada ac, mattis nec, quam. Nam molestie scelerisque quam. Nullam\
199 \ feugiat cursus lacus.orem ipsum dolor sit amet, consectetur adipiscing\
200 \ elit. Donec libero risus, commodo vitae, pharetra mollis, posuere eu, pede.\
201 \ Nulla nec tortor. Donec id elit quis purus consectetur consequat. Nam\
202 \ congue semper tellus. Sed erat dolor, dapibus sit amet, venenatis ornare,\
203 \ ultrices ut, nisi."
204 ==> "Lorem ipsum dolor sit amet, Nulla nec tortor. Donec id elit quis\
205 \ purus\nconsectetur consequat. Nam congue semper tellus. Sed erat dolor, dapibus\
206 \ sit\namet, venenatis ornare, ultrices ut, nisi. Aliquam ante. Suspendisse\
207 \ scelerisque\ndui nec velit. Duis augue augue, gravida euismod, vulputate ac,\
208 \ facilisis id,\nsem. Morbi in orci. Nulla purus lacus, pulvinar vel, malesuada\
209 \ ac, mattis nec,\nquam. Nam molestie scelerisque quam. Nullam feugiat cursus\
210 \ lacus.orem ipsum\ndolor sit amet, consectetur adipiscing elit. Donec libero\
211 \ risus, commodo vitae,\npharetra mollis, posuere eu, pede. Nulla nec tortor.\
212 \ Donec id elit quis purus\nconsectetur consequat. Nam congue semper tellus. Sed\
213 \ erat dolor, dapibus sit\namet, venenatis ornare, ultrices ut, nisi. Aliquam\
214 \ ante. Suspendisse scelerisque\ndui nec velit. Duis augue augue, gravida\
215 \ euismod, vulputate ac, facilisis id,\nsem. Morbi in orci. Nulla purus lacus,\
216 \ pulvinar vel, malesuada ac, mattis nec,\nquam. Nam molestie scelerisque quam.\
217 \ Nullam feugiat cursus lacus.orem ipsum\ndolor sit amet, consectetur adipiscing\
218 \ elit. Donec libero risus, commodo vitae,\npharetra mollis, posuere eu, pede.\
219 \ Nulla nec tortor. Donec id elit quis purus\nconsectetur consequat. Nam congue\
220 \ semper tellus. Sed erat dolor, dapibus sit\namet, venenatis ornare, ultrices\
221 \ ut, nisi. Aliquam ante. Suspendisse scelerisque\ndui nec velit. Duis augue\
222 \ augue, gravida euismod, vulputate ac, facilisis id,\nsem. Morbi in orci. Nulla\
223 \ purus lacus, pulvinar vel, malesuada ac, mattis nec,\nquam. Nam molestie\
224 \ scelerisque quam. Nullam feugiat cursus lacus.orem ipsum\ndolor sit amet,\
225 \ consectetur adipiscing elit. Donec libero risus, commodo vitae,\npharetra\
226 \ mollis, posuere eu, pede. Nulla nec tortor. Donec id elit quis\
227 \ purus\nconsectetur consequat. Nam congue semper tellus. Sed erat dolor, dapibus\
228 \ sit\namet, venenatis ornare, ultrices ut, nisi."
229 , 80 `maxWidth` justify
230 "Lorem ipsum dolor sit amet, Nulla nec tortor. Donec id elit quis purus\
231 \ consectetur consequat. Nam congue semper tellus. Sed erat dolor,\
232 \ dapibus sit amet, venenatis ornare, ultrices ut, nisi. Aliquam ante.\
233 \ Suspendisse scelerisque dui nec velit. Duis augue augue, gravida euismod,\
234 \ vulputate ac, facilisis id, sem. Morbi in orci. Nulla purus lacus,\
235 \ pulvinar vel, malesuada ac, mattis nec, quam. Nam molestie scelerisque\
236 \ quam. Nullam feugiat cursus lacus.orem ipsum dolor sit amet, consectetur\
237 \ adipiscing elit. Donec libero risus, commodo vitae, pharetra mollis,\
238 \ posuere eu, pede. Nulla nec tortor. Donec id elit quis purus consectetur\
239 \ consequat. Nam congue semper tellus. Sed erat dolor, dapibus sit\
240 \ amet, venenatis ornare, ultrices ut, nisi. Aliquam ante. Suspendisse\
241 \ scelerisque dui nec velit. Duis augue augue, gravida euismod, vulputate ac,\
242 \ facilisis id, sem. Morbi in orci. Nulla purus lacus, pulvinar vel,\
243 \ malesuada ac, mattis nec, quam. Nam molestie scelerisque quam.\
244 \ Nullam feugiat cursus lacus.orem ipsum dolor sit amet, consectetur\
245 \ adipiscing elit. Donec libero risus, commodo vitae, pharetra mollis,\
246 \ posuere eu, pede. Nulla nec tortor. Donec id elit quis purus consectetur\
247 \ consequat. Nam congue semper tellus. Sed erat dolor, dapibus sit amet,\
248 \ venenatis ornare, ultrices ut, nisi. Aliquam ante. Suspendisse\
249 \ scelerisque dui nec velit. Duis augue augue, gravida euismod, vulputate ac,\
250 \ facilisis id, sem. Morbi in orci. Nulla purus lacus, pulvinar vel,\
251 \ malesuada ac, mattis nec, quam. Nam molestie scelerisque quam. Nullam\
252 \ feugiat cursus lacus.orem ipsum dolor sit amet, consectetur adipiscing\
253 \ elit. Donec libero risus, commodo vitae, pharetra mollis, posuere eu, pede.\
254 \ Nulla nec tortor. Donec id elit quis purus consectetur consequat. Nam\
255 \ congue semper tellus. Sed erat dolor, dapibus sit amet, venenatis ornare,\
256 \ ultrices ut, nisi."
257 ==> "Lorem ipsum dolor sit amet, Nulla nec tortor. Donec id elit quis purus\n\
258 \consectetur consequat. Nam congue semper tellus. Sed erat dolor, dapibus sit\n\
259 \amet, venenatis ornare, ultrices ut, nisi. Aliquam ante. Suspendisse scelerisque\n\
260 \dui nec velit. Duis augue augue, gravida euismod, vulputate ac, facilisis id,\n\
261 \sem. Morbi in orci. Nulla purus lacus, pulvinar vel, malesuada ac, mattis nec,\n\
262 \quam. Nam molestie scelerisque quam. Nullam feugiat cursus lacus.orem ipsum\n\
263 \dolor sit amet, consectetur adipiscing elit. Donec libero risus, commodo vitae,\n\
264 \pharetra mollis, posuere eu, pede. Nulla nec tortor. Donec id elit quis purus\n\
265 \consectetur consequat. Nam congue semper tellus. Sed erat dolor, dapibus sit\n\
266 \amet, venenatis ornare, ultrices ut, nisi. Aliquam ante. Suspendisse scelerisque\n\
267 \dui nec velit. Duis augue augue, gravida euismod, vulputate ac, facilisis id,\n\
268 \sem. Morbi in orci. Nulla purus lacus, pulvinar vel, malesuada ac, mattis nec,\n\
269 \quam. Nam molestie scelerisque quam. Nullam feugiat cursus lacus.orem ipsum\n\
270 \dolor sit amet, consectetur adipiscing elit. Donec libero risus, commodo vitae,\n\
271 \pharetra mollis, posuere eu, pede. Nulla nec tortor. Donec id elit quis purus\n\
272 \consectetur consequat. Nam congue semper tellus. Sed erat dolor, dapibus sit\n\
273 \amet, venenatis ornare, ultrices ut, nisi. Aliquam ante. Suspendisse scelerisque\n\
274 \dui nec velit. Duis augue augue, gravida euismod, vulputate ac, facilisis id,\n\
275 \sem. Morbi in orci. Nulla purus lacus, pulvinar vel, malesuada ac, mattis nec,\n\
276 \quam. Nam molestie scelerisque quam. Nullam feugiat cursus lacus.orem ipsum\n\
277 \dolor sit amet, consectetur adipiscing elit. Donec libero risus, commodo vitae,\n\
278 \pharetra mollis, posuere eu, pede. Nulla nec tortor. Donec id elit quis purus\n\
279 \consectetur consequat. Nam congue semper tellus. Sed erat dolor, dapibus sit\n\
280 \amet, venenatis ornare, ultrices ut, nisi."
283 (==>) :: IsString d => d ~ String => AnsiText (Plain d) -> d -> Assertion; infix 0 ==>
284 p ==> exp = got @?= exp
285 where got = runPlain $ runAnsiText p
287 testList :: String -> [Assertion] -> TestTree
288 testList n as = testGroup n $ List.zipWith testCase (show <$> [1::Int ..]) as
290 breakpoints :: Wrappable d => Monoid d => [d] -> d
291 breakpoints = intercalate breakpoint
293 breakspaces :: Wrappable d => Monoid d => [d] -> d
294 breakspaces = intercalate breakspace
297 maxWidth :: Wrappable d => Width -> d -> d
298 maxWidth = setWidth . Just
301 From (Line String) d =>
302 Spaceable d => Indentable d => Wrappable d =>
307 from (Line (show i)) <>
308 (if n <= i then mempty
309 else align (breakspace <> go (i+1)))
311 listHorV :: IsString d => Indentable d => Wrappable d => [d] -> d
313 listHorV [d] = "["<>d<>"]"
316 ("[" <> intercalate ("," <> space) ds <> "]")
317 (align $ "[" <> space
318 <> foldr1 (\a acc -> a <> newline <> "," <> space <> acc) ds
321 fun :: IsString d => Indentable d => Wrappable d => d -> d
322 fun d = "function(" <> incrIndent 2 (breakalt d (newline<>d<>newline)) <> ")"