|
| 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 imports-qualified-newline-comma-first () |
| 122 | + :expected-result :failed |
| 123 | + (purescript-test-indentation " |
| 124 | +import Data.List.NonEmpty (NonEmptyList) |
| 125 | +import Data.Maybe (Maybe(..) |
| 126 | +, fromMaybe)" |
| 127 | + |
| 128 | +" |
| 129 | +import Data.List.NonEmpty (NonEmptyList) |
| 130 | +import Data.Maybe ( Maybe(..) |
| 131 | + , fromMaybe)")) |
| 132 | + |
| 133 | +(ert-deftest imports-qualified-newline-comma-end () |
| 134 | + :expected-result :failed |
| 135 | + (purescript-test-indentation " |
| 136 | +import Data.List.NonEmpty (NonEmptyList) |
| 137 | +import Data.Maybe (Maybe(..), |
| 138 | +fromMaybe)" |
| 139 | + |
| 140 | +" |
| 141 | +import Data.List.NonEmpty (NonEmptyList) |
| 142 | +import Data.Maybe (Maybe(..), |
| 143 | + fromMaybe)")) |
| 144 | + |
| 145 | +(ert-deftest do-impl () |
| 146 | + (purescript-test-indentation " |
| 147 | +main = do |
| 148 | +pure unit" |
| 149 | + |
| 150 | +" |
| 151 | +main = do |
| 152 | + pure unit")) |
| 153 | + |
| 154 | +(ert-deftest let-bindings () |
| 155 | + :expected-result :failed |
| 156 | + (purescript-test-indentation " |
| 157 | +main = do |
| 158 | + let foo = 1 |
| 159 | + bar = 2 |
| 160 | + let |
| 161 | + buzz = 1 |
| 162 | + fuzz = 2 |
| 163 | +pure unit" |
| 164 | + |
| 165 | +" |
| 166 | +main = do |
| 167 | + let foo = 1 |
| 168 | + bar = 2 |
| 169 | + let |
| 170 | + buzz = 1 |
| 171 | + fuzz = 2 |
| 172 | + pure unit")) |
| 173 | + |
| 174 | +(ert-deftest module-exports-list () |
| 175 | + :expected-result :failed |
| 176 | + (purescript-test-indentation " |
| 177 | +module MyModule ( class A |
| 178 | +, b, c |
| 179 | +, d) where" |
| 180 | + |
| 181 | +" |
| 182 | +module MyModule ( class A |
| 183 | + , b, c |
| 184 | + , d) where")) |
| 185 | + |
| 186 | +(ert-deftest module-exports-next-line () |
| 187 | + "Parentheses should get indented to the mode indentation size" |
| 188 | + :expected-result :failed |
| 189 | + (purescript-test-indentation " |
| 190 | +module MyModule |
| 191 | + (class A, b, c, d) where" |
| 192 | + |
| 193 | +" |
| 194 | +module MyModule |
| 195 | + (class A, b, c, d) where")) |
| 196 | + |
| 197 | +(ert-deftest keyword-record-values () |
| 198 | + "PureScript allows keywords to be part of a Record declaration" |
| 199 | + :expected-result :failed |
| 200 | + (purescript-test-indentation " |
| 201 | +type MyRec = { data :: Number |
| 202 | +, where :: Number |
| 203 | +, instance :: Number |
| 204 | +}" |
| 205 | + |
| 206 | +" |
| 207 | +type MyRec = { data :: Number |
| 208 | + , where :: Number |
| 209 | + , instance :: Number |
| 210 | + }")) |
| 211 | + |
| 212 | +(ert-deftest func-with-do () |
| 213 | + :expected-result :failed |
| 214 | + (purescript-test-indentation " |
| 215 | +foo :: Foo |
| 216 | +foo = do |
| 217 | + pure unit" |
| 218 | + |
| 219 | +" |
| 220 | +foo :: Foo |
| 221 | +foo = do |
| 222 | + pure unit")) |
| 223 | + |
| 224 | +(ert-deftest do-bindings () |
| 225 | + :expected-result :failed |
| 226 | + (purescript-test-indentation " |
| 227 | +foo :: Foo |
| 228 | +foo = do |
| 229 | +_ <- something |
| 230 | +identifier :: Array String <- function call |
| 231 | +_ <- another call |
| 232 | +pure unit" |
| 233 | + |
| 234 | +" |
| 235 | +foo :: Foo |
| 236 | +foo = do |
| 237 | + _ <- something |
| 238 | + identifier :: Array String <- function call |
| 239 | + _ <- another call |
| 240 | + pure unit")) |
0 commit comments