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 hunit = testGroup "HUnit"
25 [ testPlain newline () "\n"
26 , testPlain ("hello".>"world") () "helloworld"
27 , testPlain ("hello".>newline.>"world") () "hello\nworld"
28 , testPlain ("hello\nworld") () "hello\nworld"
29 , testPlain (setWidth (Just 9) $ "hello" .> breakpoint .> "world")
32 , testPlain (setWidth (Just 10) $ intercalate_ breakpoint string)
35 , testPlain (setWidth (Just 6) $ intercalate_ breakpoint string)
38 , testPlain (setWidth (Just 6) $ intercalate_ breakpoint string)
39 ["he", "ll", "o!", "wo", "rl", "d!"]
41 , testPlain (setWidth (Just 5) $ intercalate_ breakpoint string)
44 , testPlain (setWidth (Just 5) $ intercalate_ breakpoint string)
45 ["he", "llo", "world"]
47 , testPlain (setWidth (Just 5) $ intercalate_ breakpoint string)
50 , testPlain (setWidth (Just 4) $ intercalate_ breakpoint string)
53 , testPlain (setWidth (Just 4) $ intercalate_ breakpoint string)
56 , testPlain (setWidth (Just 4) $ intercalate_ breakpoint string)
57 ["he", "llo", "world"]
59 , testPlain (setWidth (Just 4) $ intercalate_ breakpoint string)
60 ["he", "llo", "w", "orld"]
62 , testPlain (setWidth (Just 4) $ intercalate_ breakpoint string)
63 ["he", "ll", "o!", "wo", "rl", "d!"]
65 , testPlain (setWidth (Just 3) $ intercalate_ breakpoint string)
68 , testPlain (setWidth (Just 3) $ intercalate_ breakpoint string)
71 , testPlain (setWidth (Just 3) $ intercalate_ breakpoint string)
74 , testPlain (setWidth (Just 1) $ intercalate_ breakpoint string)
77 , testPlain (setWidth (Just 4) $ "__" .> align (intercalate_ breakpoint string))
78 ["he", "ll", "o!", "wo", "rl", "d!"]
79 "__he\n ll\n o!\n wo\n rl\n d!"
80 , testPlain (setWidth (Just 6) $ "__" .> align (intercalate_ breakpoint string))
81 ["he", "ll", "o!", "wo", "rl", "d!"]
82 "__hell\n o!wo\n rld!"
83 , testPlain (setWidth (Just 16) $ "__" .> bracketList string)
86 , testPlain (setWidth (Just 11) $ intercalate_ breakspace string)
89 , testPlain (setWidth (Just 10) $ intercalate_ breakspace string)
92 , testPlain (setWidth (Just 6) $ intercalate_ breakspace string)
93 ["hel", "lo", "wo", "rld"]
95 , testPlain (setWidth (Just 6) $ intercalate_ breakspace string)
96 ["hel", "lo", "wo", "rld", "HEL", "LO", "WO", "RLD"] "hel lo\nwo rld\nHEL LO\nWO RLD"
97 , testPlain (setWidth (Just 5) $ intercalate_ breakspace string)
100 , testPlain (setWidth (Just 7) $ ("hello".>breakspace.>"world"))
103 , testPlain (setWidth (Just 7) $ ("hello ".>"world"))
106 , testPlain (" ".> "hello\nworld\n!")
109 , testPlain ("__".>align "hello\nworld\n!")
111 "__hello\n world\n !"
112 , testPlain (hang 2 "hello\nworld\n!") ()
114 , testPlain (hang 2 "hello\nworld\n!".>"\nhello\n!")
116 "hello\n world\n !\nhello\n!"
117 , testPlain (setWidth (Just 10) $ unwords_ int)
119 "1 2 3 4 5\n6 7 8 9 10\n11 12 13\n14 15"
120 , testPlain (setWidth (Just 10) $ ("1234567890" .> " ") .> "1")
123 -- justify respects concatenating words
124 , testPlain (setWidth (Just 10) $ justify (setWidth (Just 11) ("1 2 3".>"4 5 6 7")))
127 -- justify flushes the buffer before
128 , testPlain (setWidth (Just 10) $ "__" .> align (justify "1 2 3 4 5"))
131 , testPlain (setWidth (Just 10) $ justify (bold ("12 34 56 78 ".> underline "90" .> " 123 456 789")))
133 "\ESC[1m12 34 56\n78 \ESC[4m90\ESC[0;1m 123\n456 789\ESC[0m"
134 -- handle escaping correctly over custom indenting
135 , testPlain (setWidth (Just 10) $ setIndent (blue "X") 1 (red ("12".>green "4\n5" .> "6")))
137 "\ESC[31m12\ESC[32m4\n\ESC[34mX\ESC[0;31;32m5\ESC[0;31m6\ESC[0m"
138 , testPlain (setWidth (Just 10) $ setIndent (blue "X") 1 (justify (red ("1 2 3 4".>green " 5 6 " .> "7 ") .> "8")))
140 "\ESC[31m1 2 3 4\ESC[32m 5\n\ESC[34mX\ESC[0;31;32m6 \ESC[0;31m7 \ESC[0m8"
141 -- unorderedList/orderedList are empty when no item
142 , testPlain (unorderedList int) [] ""
143 , testPlain (unorderedList int) [] ""
144 -- endline break spaces
145 , testPlain (setWidth (Just 10) $ ("a".>endline.>" b")) () "a\nb"
146 -- endline does no justify
147 , testPlain (setWidth (Just 10) $ justify ("a b".>endline.>" c")) () "a b\nc"
148 -- endline works overflowed
149 , testPlain (setWidth (Just 10) $ justify ("abcdefghijk".>endline.>" a")) () "abcdefghijk\na"
150 -- endline prints nothing
151 , testPlain (setWidth (Just 10) $ justify ("12345678".>endline.>"90ab".>align (" cdefghijk cdefghijk")))
156 -- newline stops overflow
157 , testPlain (setWidth (Just 10) $ breakalt "fits" "over".>"\n".>"12345678901")
161 -- breakalt triggers only if its first argument overflows,
162 -- not if what's next overflows.
163 , testPlain (setWidth (Just 10) $ spaces 2.>align(breakalt "fits" "over".>newline.>"12345678901"))
170 testList :: String -> [Assertion] -> TestTree
171 testList n as = testGroup n $ List.zipWith testCase (show <$> [1::Int ..]) as
172 testPlain :: o ~ String => Plain o a -> a -> o -> Assertion
173 testPlain fmt a exp = runPlain fmt a @?= exp