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.Plaintext.Classes
16 import Symantic.Plaintext.Writer (Writer, runWriter)
20 hunit = testGroup "HUnit"
22 [ testWriter newline () "\n"
23 , testWriter ("hello".>"world") () "helloworld"
24 , testWriter ("hello".>newline.>"world") () "hello\nworld"
25 , testWriter ("hello\nworld") () "hello\nworld"
26 , testWriter (setWidth (Just 9) $ "hello" .> breakpoint .> "world")
29 , testWriter (setWidth (Just 10) $ intercalate_ breakpoint string)
32 , testWriter (setWidth (Just 6) $ intercalate_ breakpoint string)
35 , testWriter (setWidth (Just 6) $ intercalate_ breakpoint string)
36 ["he", "ll", "o!", "wo", "rl", "d!"]
38 , testWriter (setWidth (Just 5) $ intercalate_ breakpoint string)
41 , testWriter (setWidth (Just 5) $ intercalate_ breakpoint string)
42 ["he", "llo", "world"]
44 , testWriter (setWidth (Just 5) $ intercalate_ breakpoint string)
47 , testWriter (setWidth (Just 4) $ intercalate_ breakpoint string)
50 , testWriter (setWidth (Just 4) $ intercalate_ breakpoint string)
53 , testWriter (setWidth (Just 4) $ intercalate_ breakpoint string)
54 ["he", "llo", "world"]
56 , testWriter (setWidth (Just 4) $ intercalate_ breakpoint string)
57 ["he", "llo", "w", "orld"]
59 , testWriter (setWidth (Just 4) $ intercalate_ breakpoint string)
60 ["he", "ll", "o!", "wo", "rl", "d!"]
62 , testWriter (setWidth (Just 3) $ intercalate_ breakpoint string)
65 , testWriter (setWidth (Just 3) $ intercalate_ breakpoint string)
68 , testWriter (setWidth (Just 3) $ intercalate_ breakpoint string)
71 , testWriter (setWidth (Just 1) $ intercalate_ breakpoint string)
74 , testWriter (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 , testWriter (setWidth (Just 6) $ "__" .> align (intercalate_ breakpoint string))
78 ["he", "ll", "o!", "wo", "rl", "d!"]
79 "__hell\n o!wo\n rld!"
80 , testWriter (setWidth (Just 16) $ "__" .> bracketList string)
83 , testWriter (setWidth (Just 11) $ intercalate_ breakspace string)
86 , testWriter (setWidth (Just 10) $ intercalate_ breakspace string)
89 , testWriter (setWidth (Just 6) $ intercalate_ breakspace string)
90 ["hel", "lo", "wo", "rld"]
92 , testWriter (setWidth (Just 6) $ intercalate_ breakspace string)
93 ["hel", "lo", "wo", "rld", "HEL", "LO", "WO", "RLD"] "hel lo\nwo rld\nHEL LO\nWO RLD"
94 , testWriter (setWidth (Just 5) $ intercalate_ breakspace string)
97 , testWriter (setWidth (Just 7) $ ("hello".>breakspace.>"world"))
100 , testWriter (setWidth (Just 7) $ ("hello ".>"world"))
103 , testWriter (" ".> "hello\nworld\n!")
106 , testWriter ("__".>align "hello\nworld\n!")
108 "__hello\n world\n !"
109 , testWriter (hang 2 "hello\nworld\n!") ()
111 , testWriter (hang 2 "hello\nworld\n!".>"\nhello\n!")
113 "hello\n world\n !\nhello\n!"
114 , testWriter (setWidth (Just 10) $ unwords_ int)
116 "1 2 3 4 5\n6 7 8 9 10\n11 12 13\n14 15"
117 , testWriter (setWidth (Just 10) $ ("1234567890" .> " ") .> "1")
120 -- justify respects concatenating words
121 , testWriter (setWidth (Just 10) $ justify (setWidth (Just 11) ("1 2 3".>"4 5 6 7")))
124 -- justify flushes the buffer before
125 , testWriter (setWidth (Just 10) $ "__" .> align (justify "1 2 3 4 5"))
128 , testWriter (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 , testWriter (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 , testWriter (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 , testWriter (unorderedList int) [] ""
140 , testWriter (unorderedList int) [] ""
141 -- endline break spaces
142 , testWriter (setWidth (Just 10) $ ("a".>endline.>" b")) () "a\nb"
143 -- endline does no justify
144 , testWriter (setWidth (Just 10) $ justify ("a b".>endline.>" c")) () "a b\nc"
145 -- endline works overflowed
146 , testWriter (setWidth (Just 10) $ justify ("abcdefghijk".>endline.>" a")) () "abcdefghijk\na"
147 -- endline prints nothing
148 , testWriter (setWidth (Just 10) $ justify ("12345678".>endline.>"90ab".>align (" cdefghijk cdefghijk")))
153 -- newline stops overflow
154 , testWriter (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 , testWriter (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 testWriter :: o ~ String => Writer o a -> a -> o -> Assertion
170 testWriter fmt a exp = runWriter fmt a @?= exp