Skip to content

Commit 3d8984d

Browse files
committed
Merge pull request #562 from gracjan/pr-test-cases
Better test cases for haskell-indentation.
2 parents 64cb818 + 4175441 commit 3d8984d

File tree

1 file changed

+180
-20
lines changed

1 file changed

+180
-20
lines changed

tests/haskell-indentation-tests.el

Lines changed: 180 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,196 @@
44
(require 'cl-lib)
55

66

7-
(defun haskell-indentation-check-on-last-line (lines expected)
7+
(defun haskell-indentation-check (&rest lines)
8+
"Check if `haskell-indentation-find-indentations` returns expected list of positions.
9+
10+
LINES is the lines to insert to a temp buffer. Last line is not
11+
inserted, it is treated as a pattern of indentation points marked
12+
by '^' characters in positions. Point will be set on the *last*
13+
line inserted in the buffer so if you need to test indentation
14+
*past* source code on empty line then an empty line must be
15+
specified.
16+
17+
Example of lines:
18+
19+
\"func = do\"
20+
\" x\"
21+
\" ^\"
22+
"
823
(with-temp-buffer
924
(haskell-mode)
1025
(haskell-indentation-mode)
11-
;; insert lines
12-
(dolist (line lines)
13-
(insert line)
14-
(insert "\n"))
15-
(forward-line -1)
16-
(let ((indentations (haskell-indentation-find-indentations))
17-
(result "") )
26+
27+
(let (expected
28+
indentations
29+
(result ""))
30+
(dolist (line lines)
31+
(when expected
32+
(insert expected)
33+
(insert "\n"))
34+
(setq expected line))
35+
36+
(forward-line -1)
37+
(setq indentations (haskell-indentation-find-indentations))
1838

1939
(dotimes (i (1+ (apply #'max indentations)))
2040
(setq result (concat result (if (memq i indentations)
2141
"^" " "))))
2242

23-
(should (equal (car expected) result)))))
43+
(should (equal expected result)))))
2444

2545

2646
(ert-deftest haskell-indentation-check-1 ()
27-
"Check if '{' on its own line gets properly indented"
28-
(haskell-indentation-check-on-last-line
29-
'("function = Record"
30-
" { field = 123 }")
31-
'("^ ^")))
47+
"Check if '{' on its own line gets properly indented"
48+
(haskell-indentation-check
49+
"function = Record"
50+
" { field = 123 }"
51+
"^ ^"))
3252

3353
(ert-deftest haskell-indentation-check-2 ()
34-
"Handle underscore in identifiers"
35-
(haskell-indentation-check-on-last-line
36-
'("function = do"
37-
" (_x) <- return ()"
38-
" z")
39-
'("^ ^ ^")))
54+
"Handle underscore in identifiers"
55+
(haskell-indentation-check
56+
"function = do"
57+
" (_x) <- return ()"
58+
" z"
59+
"^ ^ ^"))
60+
61+
(ert-deftest haskell-indentation-check-2a ()
62+
"Handle apostrophe in identifiers"
63+
:expected-result :failed
64+
(haskell-indentation-check
65+
"function = do"
66+
" (_'x') <- return ()"
67+
" z"
68+
"^ ^ ^"))
69+
70+
71+
(ert-deftest haskell-indentation-check-3 ()
72+
"Import statememnt symbol list 1"
73+
(haskell-indentation-check
74+
"import Control.Concurrent"
75+
" ( forkIO,"
76+
" killThread)"
77+
" ^"))
78+
79+
(ert-deftest haskell-indentation-check-4 ()
80+
"Import statememnt symbol list 2"
81+
:expected-result :failed
82+
(haskell-indentation-check
83+
"import Control.Concurrent"
84+
" ( forkIO"
85+
" , killThread)"
86+
" ^"))
87+
88+
(ert-deftest haskell-indentation-check-5 ()
89+
"List comprehension"
90+
(haskell-indentation-check
91+
"fun = [ x | y"
92+
" , z ]"
93+
" ^"))
94+
95+
(ert-deftest haskell-indentation-check-5a ()
96+
"List comprehension"
97+
:expected-result :failed
98+
(haskell-indentation-check
99+
"fun = [ x | y,"
100+
" z ]"
101+
" ^"))
102+
103+
(ert-deftest haskell-indentation-check-6 ()
104+
"let in list comprehension"
105+
:expected-result :failed
106+
(haskell-indentation-check
107+
"fun = [ f | x <- xs"
108+
" , y <- ys"
109+
" , let c = 123"
110+
" , f <- fx x y c ]"
111+
" ^"))
112+
113+
(ert-deftest haskell-indentation-check-7 ()
114+
"import after import"
115+
:expected-result :failed
116+
(haskell-indentation-check
117+
"import ABC"
118+
"import DEF"
119+
"^"))
120+
121+
(ert-deftest haskell-indentation-check-8 ()
122+
"Guards in function definition"
123+
(haskell-indentation-check
124+
"resolve (amount, max) number"
125+
" | number > max = (1, number)"
126+
" | number == max = (amount + 1, number)"
127+
" ^"))
128+
129+
(ert-deftest haskell-indentation-check-9 ()
130+
"Operator last on line"
131+
:expected-result :failed
132+
(haskell-indentation-check
133+
"fun = x ++"
134+
" ^"))
135+
136+
(ert-deftest haskell-indentation-check-10 ()
137+
"Operator first on line"
138+
:expected-result :failed
139+
(haskell-indentation-check
140+
"fun = x"
141+
" ++ z"
142+
" ^"))
143+
144+
(ert-deftest haskell-indentation-check-11 ()
145+
"Guards with commas"
146+
(haskell-indentation-check
147+
"clunky env var1 var2"
148+
" | Just val1 <- lookup env var1"
149+
" , Just val2 <- lookup env var2"
150+
" ^"))
151+
152+
(ert-deftest haskell-indentation-check-12 ()
153+
"Guards with commas"
154+
:expected-result :failed
155+
(haskell-indentation-check
156+
"fun = do { putStrLn \"X\";"
157+
" }"
158+
" ^"))
159+
160+
(ert-deftest haskell-indentation-check-13 ()
161+
"Don't indent after deriving"
162+
:expected-result :failed
163+
(haskell-indentation-check
164+
"data X = X"
165+
" deriving (Eq, Ord, Show)"
166+
"^"))
167+
168+
(ert-deftest haskell-indentation-check-14 ()
169+
"Line starting with operator inside a 'do' needs to be indented"
170+
:expected-result :failed
171+
(haskell-indentation-check
172+
"fun = do"
173+
" pure X"
174+
" <*> marg"
175+
" ^"))
176+
177+
(ert-deftest haskell-indentation-check-15 ()
178+
"An if..then inside a do block"
179+
:expected-result :failed
180+
(haskell-indentation-check
181+
"fun = do"
182+
" if x"
183+
" then do"
184+
" putStrLn \"True\""
185+
" ^"))
186+
187+
(ert-deftest haskell-indentation-check-16 ()
188+
"Lambda and a do block"
189+
:expected-result :failed
190+
(haskell-indentation-check
191+
"fun = \x -> do"
192+
" ^"))
193+
194+
(ert-deftest haskell-indentation-check-16a ()
195+
"A lambda"
196+
:expected-result :failed
197+
(haskell-indentation-check
198+
"fun = \x ->"
199+
" ^"))

0 commit comments

Comments
 (0)