1 {-# LANGUAGE OverloadedStrings #-}
5 import Test.Tasty.HUnit
7 import Data.Function (($))
8 import Data.Functor ((<$>))
10 import Data.Maybe (Maybe(..))
11 import Data.String (String)
12 import Text.Show (Show(..))
13 import qualified Data.List as List
15 import Symantic.Formatter.Class
16 import Symantic.Formatter.Plain (Plain, runPlain)
20 hunit = testGroup "HUnit"
22 [ testPlain newline () "\n"
23 , testPlain ("hello".>"world") () "helloworld"
24 , testPlain ("hello".>newline.>"world") () "hello\nworld"
25 , testPlain ("hello\nworld") () "hello\nworld"
26 , testPlain (setWidth (Just 9) $ "hello" .> breakpoint .> "world")
29 , testPlain (setWidth (Just 10) $ intercalate_ breakpoint string)
32 , testPlain (setWidth (Just 6) $ intercalate_ breakpoint string)
35 , testPlain (setWidth (Just 6) $ intercalate_ breakpoint string)
36 ["he", "ll", "o!", "wo", "rl", "d!"]
38 , testPlain (setWidth (Just 5) $ intercalate_ breakpoint string)
41 , testPlain (setWidth (Just 5) $ intercalate_ breakpoint string)
42 ["he", "llo", "world"]
44 , testPlain (setWidth (Just 5) $ intercalate_ breakpoint string)
47 , testPlain (setWidth (Just 4) $ intercalate_ breakpoint string)
50 , testPlain (setWidth (Just 4) $ intercalate_ breakpoint string)
53 , testPlain (setWidth (Just 4) $ intercalate_ breakpoint string)
54 ["he", "llo", "world"]
56 , testPlain (setWidth (Just 4) $ intercalate_ breakpoint string)
57 ["he", "llo", "w", "orld"]
59 , testPlain (setWidth (Just 4) $ intercalate_ breakpoint string)
60 ["he", "ll", "o!", "wo", "rl", "d!"]
62 , testPlain (setWidth (Just 3) $ intercalate_ breakpoint string)
65 , testPlain (setWidth (Just 3) $ intercalate_ breakpoint string)
68 , testPlain (setWidth (Just 3) $ intercalate_ breakpoint string)
71 , testPlain (setWidth (Just 1) $ intercalate_ breakpoint string)
74 , testPlain (setWidth (Just 4) $ "__" .> align (intercalate_ breakpoint string))
75 ["he", "ll", "o!", "wo", "rl", "d!"]
76 "__he\n ll\n o!\n wo\n rl\n d!"
77 , testPlain (setWidth (Just 6) $ "__" .> align (intercalate_ breakpoint string))
78 ["he", "ll", "o!", "wo", "rl", "d!"]
79 "__hell\n o!wo\n rld!"
80 , testPlain (setWidth (Just 16) $ "__" .> bracketList string)
83 , testPlain (setWidth (Just 11) $ intercalate_ breakspace string)
86 , testPlain (setWidth (Just 10) $ intercalate_ breakspace string)
89 , testPlain (setWidth (Just 6) $ intercalate_ breakspace string)
90 ["hel", "lo", "wo", "rld"]
92 , testPlain (setWidth (Just 6) $ intercalate_ breakspace string)
93 ["hel", "lo", "wo", "rld", "HEL", "LO", "WO", "RLD"] "hel lo\nwo rld\nHEL LO\nWO RLD"
94 , testPlain (setWidth (Just 5) $ intercalate_ breakspace string)
97 , testPlain (setWidth (Just 7) $ ("hello".>breakspace.>"world"))
100 , testPlain (setWidth (Just 7) $ ("hello ".>"world"))
103 , testPlain (" ".> "hello\nworld\n!")
106 , testPlain ("__".>align "hello\nworld\n!")
108 "__hello\n world\n !"
109 , testPlain (hang 2 "hello\nworld\n!") ()
111 , testPlain (hang 2 "hello\nworld\n!".>"\nhello\n!")
113 "hello\n world\n !\nhello\n!"
114 , testPlain (setWidth (Just 10) $ unwords_ int)
116 "1 2 3 4 5\n6 7 8 9 10\n11 12 13\n14 15"
117 , testPlain (setWidth (Just 10) $ ("1234567890" .> " ") .> "1")
120 -- justify respects concatenating words
121 , testPlain (setWidth (Just 10) $ justify (setWidth (Just 11) ("1 2 3".>"4 5 6 7")))
124 -- justify flushes the buffer before
125 , testPlain (setWidth (Just 10) $ "__" .> align (justify "1 2 3 4 5"))
128 , testPlain (setWidth (Just 10) $ 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 -- handle escaping correctly over custom indenting
132 , testPlain (setWidth (Just 10) $ setIndent (blue "X") 1 (red ("12".>green "4\n5" .> "6")))
134 "\ESC[31m12\ESC[32m4\n\ESC[34mX\ESC[0;31;32m5\ESC[0;31m6\ESC[0m"
135 , testPlain (setWidth (Just 10) $ setIndent (blue "X") 1 (justify (red ("1 2 3 4".>green " 5 6 " .> "7 ") .> "8")))
137 "\ESC[31m1 2 3 4\ESC[32m 5\n\ESC[34mX\ESC[0;31;32m6 \ESC[0;31m7 \ESC[0m8"
138 -- unorderedList/orderedList are empty when no item
139 , testPlain (unorderedList int) [] ""
140 , testPlain (unorderedList int) [] ""
141 -- endline break spaces
142 , testPlain (setWidth (Just 10) $ ("a".>endline.>" b")) () "a\nb"
143 -- endline does no justify
144 , testPlain (setWidth (Just 10) $ justify ("a b".>endline.>" c")) () "a b\nc"
145 -- endline works overflowed
146 , testPlain (setWidth (Just 10) $ justify ("abcdefghijk".>endline.>" a")) () "abcdefghijk\na"
147 -- endline prints nothing
148 , testPlain (setWidth (Just 10) $ justify ("12345678".>endline.>"90ab".>align (" cdefghijk cdefghijk")))
153 -- newline stops overflow
154 , testPlain (setWidth (Just 10) $ breakalt "fits" "over".>"\n".>"12345678901")
158 -- breakalt triggers only if its first argument overflows,
159 -- not if what's next overflows.
160 , testPlain (setWidth (Just 10) $ spaces 2.>align(breakalt "fits" "over".>newline.>"12345678901"))
167 testList :: String -> [Assertion] -> TestTree
168 testList n as = testGroup n $ List.zipWith testCase (show <$> [1::Int ..]) as
169 testPlain :: o ~ String => Plain o a -> a -> o -> Assertion
170 testPlain fmt a exp = runPlain fmt a @?= exp