|
| 1 | +;;; clojure-ts-mode-indentation-test.el --- Clojure TS Mode: indentation test suite -*- lexical-binding: t; -*- |
| 2 | + |
| 3 | +;; Copyright © 2022-2024 Danny Freeman |
| 4 | + |
| 5 | +;; This file is not part of GNU Emacs. |
| 6 | + |
| 7 | +;; This program is free software; you can redistribute it and/or modify |
| 8 | +;; it under the terms of the GNU General Public License as published by |
| 9 | +;; the Free Software Foundation, either version 3 of the License, or |
| 10 | +;; (at your option) any later version. |
| 11 | + |
| 12 | +;; This program is distributed in the hope that it will be useful, |
| 13 | +;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | +;; GNU General Public License for more details. |
| 16 | + |
| 17 | +;; You should have received a copy of the GNU General Public License |
| 18 | +;; along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 19 | + |
| 20 | +;;; Commentary: |
| 21 | + |
| 22 | +;; The unit test suite of Clojure TS Mode |
| 23 | + |
| 24 | +(require 'clojure-ts-mode) |
| 25 | +(require 'cl-lib) |
| 26 | +(require 'buttercup) |
| 27 | +(require 's nil t) ;Don't burp if it's missing during compilation. |
| 28 | + |
| 29 | + |
| 30 | +(defmacro when-indenting-with-point-it (description before after) |
| 31 | + "Return a buttercup spec. |
| 32 | +
|
| 33 | +Check whether the swift indentation command will correctly change the buffer. |
| 34 | +Will also check whether point is moved to the expected position. |
| 35 | +
|
| 36 | +BEFORE is the buffer string before indenting, where a pipe (|) represents |
| 37 | +point. |
| 38 | +
|
| 39 | +AFTER is the expected buffer string after indenting, where a pipe (|) |
| 40 | +represents the expected position of point. |
| 41 | +
|
| 42 | +DESCRIPTION is a string with the description of the spec." |
| 43 | + (declare (indent 1)) |
| 44 | + `(it ,description |
| 45 | + (let* ((after ,after) |
| 46 | + (expected-cursor-pos (1+ (clojure-ts--s-index-of "|" after))) |
| 47 | + (expected-state (delete ?| after))) |
| 48 | + (with-clojure-ts-buffer ,before |
| 49 | + (goto-char (point-min)) |
| 50 | + (search-forward "|") |
| 51 | + (delete-char -1) |
| 52 | + (font-lock-ensure) |
| 53 | + (indent-according-to-mode) |
| 54 | + (expect (buffer-string) :to-equal expected-state) |
| 55 | + (expect (point) :to-equal expected-cursor-pos))))) |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | +;; Backtracking indent |
| 60 | +(defmacro when-indenting-it (description &rest forms) |
| 61 | + "Return a buttercup spec. |
| 62 | +
|
| 63 | +Check that all FORMS correspond to properly indented sexps. |
| 64 | +
|
| 65 | +DESCRIPTION is a string with the description of the spec." |
| 66 | + (declare (indent 1)) |
| 67 | + `(it ,description |
| 68 | + (progn |
| 69 | + ,@(mapcar (lambda (form) |
| 70 | + `(with-temp-buffer |
| 71 | + (clojure-ts-mode) |
| 72 | + (insert "\n" ,form);,(replace-regexp-in-string "\n +" "\n " form)) |
| 73 | + (indent-region (point-min) (point-max)) |
| 74 | + (expect (buffer-string) :to-equal ,(concat "\n" form)))) |
| 75 | + forms)))) |
| 76 | + |
| 77 | + |
| 78 | +;; Provide font locking for easier test editing. |
| 79 | + |
| 80 | +(font-lock-add-keywords |
| 81 | + 'emacs-lisp-mode |
| 82 | + `((,(rx "(" (group "when-indenting-with-point-it") eow) |
| 83 | + (1 font-lock-keyword-face)) |
| 84 | + (,(rx "(" |
| 85 | + (group "when-indenting-with-point-it") (+ space) |
| 86 | + (group bow (+ (not space)) eow) |
| 87 | + ) |
| 88 | + (1 font-lock-keyword-face) |
| 89 | + (2 font-lock-function-name-face)))) |
| 90 | + |
| 91 | + |
| 92 | +(describe "indentation" |
| 93 | + (it "should not hang on end of buffer" |
| 94 | + (with-clojure-ts-buffer "(let [a b]" |
| 95 | + (goto-char (point-max)) |
| 96 | + (expect |
| 97 | + (with-timeout (2) |
| 98 | + (newline-and-indent) |
| 99 | + t)))) |
| 100 | + |
| 101 | + (when-indenting-with-point-it "should have no indentation at top level" |
| 102 | + "|x" |
| 103 | + |
| 104 | + "|x") |
| 105 | + |
| 106 | + (when-indenting-it "should handle non-symbol at start" |
| 107 | + " |
| 108 | +{\"1\" 2 |
| 109 | + *3 4}") |
| 110 | + |
| 111 | + (when-indenting-it "should have no indentation at top level lists with metadata" |
| 112 | + " |
| 113 | +^{:foo true} |
| 114 | +(def b 2)") |
| 115 | + |
| 116 | + (when-indenting-it "should have no indentation at top level vectors with metadata" |
| 117 | + " |
| 118 | +^{:foo true} |
| 119 | +[1 2]") |
| 120 | + |
| 121 | + (when-indenting-it "should have no indentation at top level maps with metadata" |
| 122 | + " |
| 123 | +^{:foo true} |
| 124 | +{:a 1}") |
| 125 | + |
| 126 | + (when-indenting-it "should have no indentation with metadata inside comment" |
| 127 | + " |
| 128 | +(comment |
| 129 | + ^{:a 1} |
| 130 | + (def a 2))") |
| 131 | + |
| 132 | + (when-indenting-it "should have params, docstring and body correctly indented in presence of metadata" |
| 133 | + " |
| 134 | +^{:foo true} |
| 135 | +(defn c |
| 136 | + \"hello\" |
| 137 | + [_foo] |
| 138 | + (+ 1 1))")) |
0 commit comments