Skip to content

Commit ee06aeb

Browse files
committed
Test PureScript indentation
1 parent ae7b21c commit ee06aeb

File tree

2 files changed

+150
-0
lines changed

2 files changed

+150
-0
lines changed

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ ELFILES = \
2525
purescript-decl-scan.el \
2626
purescript-yas.el \
2727
tests/purescript-sort-imports-tests.el \
28+
tests/purescript-indentation-tests.el \
2829
tests/purescript-str-tests.el
2930

3031
ELCFILES = $(ELFILES:.el=.elc)
@@ -46,6 +47,7 @@ compile: $(ELCFILES)
4647
test: compile
4748
@$(BATCH) -l tests/purescript-sort-imports-tests.elc \
4849
-l tests/purescript-str-tests.elc \
50+
-l tests/purescript-indentation-tests.elc \
4951
-f ert-run-tests-batch-and-exit
5052
@echo "tests passed!"
5153

tests/purescript-indentation-tests.el

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
;;; purescript-indentation-tests.el --- Unit tests for purescript indentation -*- lexical-binding: t -*-
2+
3+
;; Copyright (c) 2025 Konstantin Kharlamov. All rights reserved.
4+
5+
;; This file is free software; you can redistribute it and/or modify
6+
;; it under the terms of the GNU General Public License as published by
7+
;; the Free Software Foundation; either version 3, or (at your option)
8+
;; any later version.
9+
10+
;; This file is distributed in the hope that it will be useful,
11+
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
;; GNU General Public License for more details.
14+
15+
;; You should have received a copy of the GNU General Public License
16+
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
18+
;;; Code:
19+
20+
(require 'ert)
21+
(require 'purescript-mode)
22+
(require 'purescript-indentation)
23+
24+
(defun purescript-test-indentation (before after &optional start-line)
25+
(with-temp-buffer
26+
(insert before)
27+
(purescript-mode)
28+
(turn-on-purescript-indentation)
29+
(indent-region (if start-line start-line (point-min))
30+
(point-max))
31+
(should (string= after (buffer-string)))))
32+
33+
(ert-deftest newtype-comma-first ()
34+
(purescript-test-indentation "
35+
newtype Foo = Foo { field1 :: MyType
36+
, field2 :: Int
37+
, field3 :: HashMap ParType Foo }"
38+
39+
"
40+
newtype Foo = Foo { field1 :: MyType
41+
, field2 :: Int
42+
, field3 :: HashMap ParType Foo }"))
43+
44+
(ert-deftest newtype-comma-end ()
45+
(purescript-test-indentation "
46+
newtype Foo = Foo { field1 :: MyType,
47+
field2 :: Int,
48+
field3 :: HashMap ParType Foo }"
49+
50+
"
51+
newtype Foo = Foo { field1 :: MyType,
52+
field2 :: Int,
53+
field3 :: HashMap ParType Foo }"))
54+
55+
(ert-deftest data-bar-first ()
56+
(purescript-test-indentation "
57+
data Foo = Foo1 Bar
58+
| Foo2 Bar2
59+
| Foo3 Unit"
60+
61+
"
62+
data Foo = Foo1 Bar
63+
| Foo2 Bar2
64+
| Foo3 Unit"))
65+
66+
(ert-deftest data-bar-end ()
67+
(purescript-test-indentation "
68+
data Foo = Foo1 Bar |
69+
Foo2 Bar2 |
70+
Foo3 Unit"
71+
72+
"
73+
data Foo = Foo1 Bar |
74+
Foo2 Bar2 |
75+
Foo3 Unit"))
76+
77+
(ert-deftest imports-zero-indented ()
78+
:expected-result :failed
79+
(purescript-test-indentation "
80+
module MyModule where
81+
82+
import Prelude
83+
84+
import Data.Array (many)
85+
import Data.Array as Array
86+
import Data.Either (Either(..))"
87+
88+
"
89+
module MyModule where
90+
91+
import Prelude
92+
93+
import Data.Array (many)
94+
import Data.Array as Array
95+
import Data.Either (Either(..))"))
96+
97+
(ert-deftest imports-indented-forward ()
98+
"PureScript allows for imports to have indentation, but the
99+
indentation must be the same. In this test we skip first indented
100+
import, and test that further lines inherit indentation level."
101+
:expected-result :failed
102+
(purescript-test-indentation "
103+
module MyModule where
104+
105+
import Prelude
106+
107+
import Data.Array (many)
108+
import Data.Array as Array
109+
import Data.Either (Either(..))"
110+
111+
"
112+
module MyModule where
113+
114+
import Prelude
115+
116+
import Data.Array (many)
117+
import Data.Array as Array
118+
import Data.Either (Either(..))"
119+
6))
120+
121+
(ert-deftest do-impl ()
122+
(purescript-test-indentation "
123+
main = do
124+
pure unit"
125+
126+
"
127+
main = do
128+
pure unit"))
129+
130+
(ert-deftest let-bindings ()
131+
:expected-result :failed
132+
(purescript-test-indentation "
133+
main = do
134+
let foo = 1
135+
bar = 2
136+
let
137+
buzz = 1
138+
fuzz = 2
139+
pure unit"
140+
141+
"
142+
main = do
143+
let foo = 1
144+
bar = 2
145+
let
146+
buzz = 1
147+
fuzz = 2
148+
pure unit"))

0 commit comments

Comments
 (0)