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 compress spaces
109 , 10 `maxWidth` justify "1 2 3 4 5 6" ==> "1 2 3 4 5\n6"
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\n" <> nestedAlign 2) ==> "a b\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 -- breakspace backtracking is bounded by the removable indentation
120 -- (hence it can actually wrap a few words in reasonable time).
122 "Lorem ipsum dolor sit amet, Nulla nec tortor. Donec id elit quis purus\
123 \ consectetur consequat. Nam congue semper tellus. Sed erat dolor,\
124 \ dapibus sit amet, venenatis ornare, ultrices ut, nisi. Aliquam ante.\
125 \ Suspendisse scelerisque dui nec velit. Duis augue augue, gravida euismod,\
126 \ vulputate ac, facilisis id, sem. Morbi in orci. Nulla purus lacus,\
127 \ pulvinar vel, malesuada ac, mattis nec, quam. Nam molestie scelerisque\
128 \ quam. Nullam feugiat cursus lacus.orem ipsum dolor sit amet, consectetur\
129 \ adipiscing elit. Donec libero risus, commodo vitae, pharetra mollis,\
130 \ posuere eu, pede. Nulla nec tortor. Donec id elit quis purus consectetur\
131 \ consequat. Nam congue semper tellus. Sed erat dolor, dapibus sit\
132 \ amet, venenatis ornare, ultrices ut, nisi. Aliquam ante. Suspendisse\
133 \ scelerisque dui nec velit. Duis augue augue, gravida euismod, vulputate ac,\
134 \ facilisis id, sem. Morbi in orci. Nulla purus lacus, pulvinar vel,\
135 \ malesuada ac, mattis nec, quam. Nam molestie scelerisque quam.\
136 \ Nullam feugiat cursus lacus.orem ipsum dolor sit amet, consectetur\
137 \ adipiscing elit. Donec libero risus, commodo vitae, pharetra mollis,\
138 \ posuere eu, pede. Nulla nec tortor. Donec id elit quis purus consectetur\
139 \ consequat. Nam congue semper tellus. Sed erat dolor, dapibus sit amet,\
140 \ venenatis ornare, ultrices ut, nisi. Aliquam ante. Suspendisse\
141 \ scelerisque dui nec velit. Duis augue augue, gravida euismod, vulputate ac,\
142 \ facilisis id, sem. Morbi in orci. Nulla purus lacus, pulvinar vel,\
143 \ malesuada ac, mattis nec, quam. Nam molestie scelerisque quam. Nullam\
144 \ feugiat cursus lacus.orem ipsum dolor sit amet, consectetur adipiscing\
145 \ elit. Donec libero risus, commodo vitae, pharetra mollis, posuere eu, pede.\
146 \ Nulla nec tortor. Donec id elit quis purus consectetur consequat. Nam\
147 \ congue semper tellus. Sed erat dolor, dapibus sit amet, venenatis ornare,\
148 \ ultrices ut, nisi."
149 ==> "Lorem ipsum dolor sit amet, Nulla nec tortor. Donec id elit quis\
150 \ purus\nconsectetur consequat. Nam congue semper tellus. Sed erat dolor, dapibus\
151 \ sit\namet, venenatis ornare, ultrices ut, nisi. Aliquam ante. Suspendisse\
152 \ scelerisque\ndui nec velit. Duis augue augue, gravida euismod, vulputate ac,\
153 \ facilisis id,\nsem. Morbi in orci. Nulla purus lacus, pulvinar vel, malesuada\
154 \ ac, mattis nec,\nquam. Nam molestie scelerisque quam. Nullam feugiat cursus\
155 \ lacus.orem ipsum\ndolor sit amet, consectetur adipiscing elit. Donec libero\
156 \ risus, commodo vitae,\npharetra mollis, posuere eu, pede. Nulla nec tortor.\
157 \ Donec id elit quis purus\nconsectetur consequat. Nam congue semper tellus. Sed\
158 \ erat dolor, dapibus sit\namet, venenatis ornare, ultrices ut, nisi. Aliquam\
159 \ ante. Suspendisse scelerisque\ndui nec velit. Duis augue augue, gravida\
160 \ euismod, vulputate ac, facilisis id,\nsem. Morbi in orci. Nulla purus lacus,\
161 \ pulvinar vel, malesuada ac, mattis nec,\nquam. Nam molestie scelerisque quam.\
162 \ Nullam feugiat cursus lacus.orem ipsum\ndolor sit amet, consectetur adipiscing\
163 \ elit. Donec libero risus, commodo vitae,\npharetra mollis, posuere eu, pede.\
164 \ Nulla nec tortor. Donec id elit quis purus\nconsectetur consequat. Nam congue\
165 \ semper tellus. Sed erat dolor, dapibus sit\namet, venenatis ornare, ultrices\
166 \ ut, nisi. Aliquam ante. Suspendisse scelerisque\ndui nec velit. Duis augue\
167 \ augue, gravida euismod, vulputate ac, facilisis id,\nsem. Morbi in orci. Nulla\
168 \ purus lacus, pulvinar vel, malesuada ac, mattis nec,\nquam. Nam molestie\
169 \ scelerisque quam. Nullam feugiat cursus lacus.orem ipsum\ndolor sit amet,\
170 \ consectetur adipiscing elit. Donec libero risus, commodo vitae,\npharetra\
171 \ mollis, posuere eu, pede. Nulla nec tortor. Donec id elit quis\
172 \ purus\nconsectetur consequat. Nam congue semper tellus. Sed erat dolor, dapibus\
173 \ sit\namet, venenatis ornare, ultrices ut, nisi."
174 , 80 `maxWidth` justify
175 "Lorem ipsum dolor sit amet, Nulla nec tortor. Donec id elit quis purus\
176 \ consectetur consequat. Nam congue semper tellus. Sed erat dolor,\
177 \ dapibus sit amet, venenatis ornare, ultrices ut, nisi. Aliquam ante.\
178 \ Suspendisse scelerisque dui nec velit. Duis augue augue, gravida euismod,\
179 \ vulputate ac, facilisis id, sem. Morbi in orci. Nulla purus lacus,\
180 \ pulvinar vel, malesuada ac, mattis nec, quam. Nam molestie scelerisque\
181 \ quam. Nullam feugiat cursus lacus.orem ipsum dolor sit amet, consectetur\
182 \ adipiscing elit. Donec libero risus, commodo vitae, pharetra mollis,\
183 \ posuere eu, pede. Nulla nec tortor. Donec id elit quis purus consectetur\
184 \ consequat. Nam congue semper tellus. Sed erat dolor, dapibus sit\
185 \ amet, venenatis ornare, ultrices ut, nisi. Aliquam ante. Suspendisse\
186 \ scelerisque dui nec velit. Duis augue augue, gravida euismod, vulputate ac,\
187 \ facilisis id, sem. Morbi in orci. Nulla purus lacus, pulvinar vel,\
188 \ malesuada ac, mattis nec, quam. Nam molestie scelerisque quam.\
189 \ Nullam feugiat cursus lacus.orem ipsum dolor sit amet, consectetur\
190 \ adipiscing elit. Donec libero risus, commodo vitae, pharetra mollis,\
191 \ posuere eu, pede. Nulla nec tortor. Donec id elit quis purus consectetur\
192 \ consequat. Nam congue semper tellus. Sed erat dolor, dapibus sit amet,\
193 \ venenatis ornare, ultrices ut, nisi. Aliquam ante. Suspendisse\
194 \ scelerisque dui nec velit. Duis augue augue, gravida euismod, vulputate ac,\
195 \ facilisis id, sem. Morbi in orci. Nulla purus lacus, pulvinar vel,\
196 \ malesuada ac, mattis nec, quam. Nam molestie scelerisque quam. Nullam\
197 \ feugiat cursus lacus.orem ipsum dolor sit amet, consectetur adipiscing\
198 \ elit. Donec libero risus, commodo vitae, pharetra mollis, posuere eu, pede.\
199 \ Nulla nec tortor. Donec id elit quis purus consectetur consequat. Nam\
200 \ congue semper tellus. Sed erat dolor, dapibus sit amet, venenatis ornare,\
201 \ ultrices ut, nisi."
202 ==> "Lorem ipsum dolor sit amet, Nulla nec tortor. Donec id elit quis purus\n\
203 \consectetur consequat. Nam congue semper tellus. Sed erat dolor, dapibus sit\n\
204 \amet, venenatis ornare, ultrices ut, nisi. Aliquam ante. Suspendisse scelerisque\n\
205 \dui nec velit. Duis augue augue, gravida euismod, vulputate ac, facilisis id,\n\
206 \sem. Morbi in orci. Nulla purus lacus, pulvinar vel, malesuada ac, mattis nec,\n\
207 \quam. Nam molestie scelerisque quam. Nullam feugiat cursus lacus.orem ipsum\n\
208 \dolor sit amet, consectetur adipiscing elit. Donec libero risus, commodo vitae,\n\
209 \pharetra mollis, posuere eu, pede. Nulla nec tortor. Donec id elit quis purus\n\
210 \consectetur consequat. Nam congue semper tellus. Sed erat dolor, dapibus sit\n\
211 \amet, venenatis ornare, ultrices ut, nisi. Aliquam ante. Suspendisse scelerisque\n\
212 \dui nec velit. Duis augue augue, gravida euismod, vulputate ac, facilisis id,\n\
213 \sem. Morbi in orci. Nulla purus lacus, pulvinar vel, malesuada ac, mattis nec,\n\
214 \quam. Nam molestie scelerisque quam. Nullam feugiat cursus lacus.orem ipsum\n\
215 \dolor sit amet, consectetur adipiscing elit. Donec libero risus, commodo vitae,\n\
216 \pharetra mollis, posuere eu, pede. Nulla nec tortor. Donec id elit quis purus\n\
217 \consectetur consequat. Nam congue semper tellus. Sed erat dolor, dapibus sit\n\
218 \amet, venenatis ornare, ultrices ut, nisi. Aliquam ante. Suspendisse scelerisque\n\
219 \dui nec velit. Duis augue augue, gravida euismod, vulputate ac, facilisis id,\n\
220 \sem. Morbi in orci. Nulla purus lacus, pulvinar vel, malesuada ac, mattis nec,\n\
221 \quam. Nam molestie scelerisque quam. Nullam feugiat cursus lacus.orem ipsum\n\
222 \dolor sit amet, consectetur adipiscing elit. Donec libero risus, commodo vitae,\n\
223 \pharetra mollis, posuere eu, pede. Nulla nec tortor. Donec id elit quis purus\n\
224 \consectetur consequat. Nam congue semper tellus. Sed erat dolor, dapibus sit\n\
225 \amet, venenatis ornare, ultrices ut, nisi."
228 (==>) :: IsString d => d ~ String => AnsiText (Plain d) -> d -> Assertion; infix 0 ==>
229 p ==> exp = got @?= exp
230 where got = runPlain $ runAnsiText p
232 testList :: String -> [Assertion] -> TestTree
233 testList n as = testGroup n $ List.zipWith testCase (show <$> [1::Int ..]) as
235 breakpoints :: Wrappable d => Monoid d => [d] -> d
236 breakpoints = intercalate breakpoint
238 breakspaces :: Wrappable d => Monoid d => [d] -> d
239 breakspaces = intercalate breakspace
242 maxWidth :: Wrappable d => Width -> d -> d
243 maxWidth = setWidth . Just
246 From (Line String) d =>
247 Spaceable d => Indentable d => Wrappable d =>
252 from (Line (show i)) <>
253 (if n <= i then mempty
254 else align (breakspace <> go (i+1)))
256 listHorV :: IsString d => Indentable d => Wrappable d => [d] -> d
258 listHorV [d] = "["<>d<>"]"
261 ("[" <> intercalate ("," <> space) ds <> "]")
262 (align $ "[" <> space
263 <> foldr1 (\a acc -> a <> newline <> "," <> space <> acc) ds
266 fun :: IsString d => Indentable d => Wrappable d => d -> d
267 fun d = "function(" <> incrIndent 2 (breakalt d (newline<>d<>newline)) <> ")"