Skip to content

Commit

Permalink
Port indentation testing infrastructure over from clojure-mode
Browse files Browse the repository at this point in the history
  • Loading branch information
kommen committed Oct 28, 2024
1 parent adf2a7e commit f450591
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Eldev
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

(eldev-use-plugin 'autoloads)

(eldev-add-extra-dependencies 'test 'buttercup)
(eldev-add-extra-dependencies 'test 'buttercup 's)

(setq byte-compile-docstring-max-column 240)
(setq checkdoc-force-docstrings-flag nil)
Expand Down
110 changes: 110 additions & 0 deletions test/clojure-ts-mode-indentation-test.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
;;; clojure-ts-mode-indentation-test.el --- Clojure TS Mode: indentation test suite -*- lexical-binding: t; -*-

;; Copyright © 2022-2024 Danny Freeman

;; This file is not part of GNU Emacs.

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:

;; The unit test suite of Clojure TS Mode

(require 'clojure-ts-mode)
(require 'cl-lib)
(require 'buttercup)
(require 's nil t) ;Don't burp if it's missing during compilation.


(defmacro when-indenting-with-point-it (description before after)
"Return a buttercup spec.
Check whether the swift indentation command will correctly change the buffer.
Will also check whether point is moved to the expected position.
BEFORE is the buffer string before indenting, where a pipe (|) represents
point.
AFTER is the expected buffer string after indenting, where a pipe (|)
represents the expected position of point.
DESCRIPTION is a string with the description of the spec."
(declare (indent 1))
`(it ,description
(let* ((after ,after)
(expected-cursor-pos (1+ (s-index-of "|" after)))
(expected-state (delete ?| after)))
(with-clojure-ts-buffer ,before
(goto-char (point-min))
(search-forward "|")
(delete-char -1)
(font-lock-ensure)
(indent-according-to-mode)
(expect (buffer-string) :to-equal expected-state)
(expect (point) :to-equal expected-cursor-pos)))))



;; Backtracking indent
(defmacro when-indenting-it (description &rest forms)
"Return a buttercup spec.
Check that all FORMS correspond to properly indented sexps.
DESCRIPTION is a string with the description of the spec."
(declare (indent 1))
`(it ,description
(progn
,@(mapcar (lambda (form)
`(with-temp-buffer
(clojure-ts-mode)
(insert "\n" ,form);,(replace-regexp-in-string "\n +" "\n " form))
(indent-region (point-min) (point-max))
(expect (buffer-string) :to-equal ,(concat "\n" form))))
forms))))


;; Provide font locking for easier test editing.

(font-lock-add-keywords
'emacs-lisp-mode
`((,(rx "(" (group "when-indenting-with-point-it") eow)
(1 font-lock-keyword-face))
(,(rx "("
(group "when-indenting-with-point-it") (+ space)
(group bow (+ (not space)) eow)
)
(1 font-lock-keyword-face)
(2 font-lock-function-name-face))))


(describe "indentation"
(it "should not hang on end of buffer"
(with-clojure-ts-buffer "(let [a b]"
(goto-char (point-max))
(expect
(with-timeout (2)
(newline-and-indent)
t))))

(when-indenting-with-point-it "should have no indentation at top level"
"|x"

"|x")

(when-indenting-it "should handle non-symbol at start"
"
{\"1\" 2
*3 4}")
)

0 comments on commit f450591

Please sign in to comment.