|
| 1 | +;;; clojure-mode-indentation-test.el --- Clojure Mode: indentation tests -*- lexical-binding: t; -*- |
| 2 | + |
| 3 | +;; Copyright (C) 2015 Bozhidar Batsov <[email protected]> |
| 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 <http://www.gnu.org/licenses/>. |
| 19 | + |
| 20 | +;;; Commentary: |
| 21 | + |
| 22 | +;; The unit test suite of Clojure Mode |
| 23 | + |
| 24 | +;;; Code: |
| 25 | + |
| 26 | +(require 'clojure-mode) |
| 27 | +(require 'cl-lib) |
| 28 | +(require 'ert) |
| 29 | +(require 's) |
| 30 | + |
| 31 | +(defmacro check-indentation (description before after &optional var-bindings) |
| 32 | + "Declare an ert test for indentation behaviour. |
| 33 | +The test will check that the swift indentation command changes the buffer |
| 34 | +from one state to another. It will also test that point is moved to an |
| 35 | +expected position. |
| 36 | +
|
| 37 | +DESCRIPTION is a symbol describing the test. |
| 38 | +
|
| 39 | +BEFORE is the buffer string before indenting, where a pipe (|) represents |
| 40 | +point. |
| 41 | +
|
| 42 | +AFTER is the expected buffer string after indenting, where a pipe (|) |
| 43 | +represents the expected position of point. |
| 44 | +
|
| 45 | +VAR-BINDINGS is an optional let-bindings list. It can be used to set the |
| 46 | +values of customisable variables." |
| 47 | + (declare (indent 1)) |
| 48 | + (let ((fname (intern (format "indentation/%s" description)))) |
| 49 | + `(ert-deftest ,fname () |
| 50 | + (let* ((after ,after) |
| 51 | + (expected-cursor-pos (1+ (s-index-of "|" after))) |
| 52 | + (expected-state (delete ?| after)) |
| 53 | + ,@var-bindings) |
| 54 | + (with-temp-buffer |
| 55 | + (insert ,before) |
| 56 | + (goto-char (point-min)) |
| 57 | + (search-forward "|") |
| 58 | + (delete-char -1) |
| 59 | + (clojure-mode) |
| 60 | + (indent-according-to-mode) |
| 61 | + |
| 62 | + (should (equal expected-state (buffer-string))) |
| 63 | + (should (equal expected-cursor-pos (point)))))))) |
| 64 | + |
| 65 | +;; Provide font locking for easier test editing. |
| 66 | + |
| 67 | +(font-lock-add-keywords |
| 68 | + 'emacs-lisp-mode |
| 69 | + `((,(rx "(" (group "check-indentation") eow) |
| 70 | + (1 font-lock-keyword-face)) |
| 71 | + (,(rx "(" |
| 72 | + (group "check-indentation") (+ space) |
| 73 | + (group bow (+ (not space)) eow) |
| 74 | + ) |
| 75 | + (1 font-lock-keyword-face) |
| 76 | + (2 font-lock-function-name-face)))) |
| 77 | + |
| 78 | + |
| 79 | +;;; Tests |
| 80 | + |
| 81 | + |
| 82 | +(check-indentation no-indentation-at-top-level |
| 83 | + "|x" |
| 84 | + "|x") |
| 85 | + |
| 86 | +(check-indentation cond-indentation |
| 87 | + " |
| 88 | +(cond |
| 89 | +|x)" |
| 90 | + " |
| 91 | +(cond |
| 92 | + |x)") |
| 93 | + |
| 94 | +(check-indentation threading-with-expression-on-first-line |
| 95 | + " |
| 96 | +(->> expr |
| 97 | + |ala)" |
| 98 | + " |
| 99 | +(->> expr |
| 100 | + |ala)") |
| 101 | + |
| 102 | +(check-indentation threading-with-expression-on-second-line |
| 103 | + " |
| 104 | +(->> |
| 105 | +|expr)" |
| 106 | + " |
| 107 | +(->> |
| 108 | + |expr)") |
| 109 | + |
| 110 | +(provide 'clojure-mode-indentation-test) |
| 111 | + |
| 112 | +;; Local Variables: |
| 113 | +;; indent-tabs-mode: nil |
| 114 | +;; End: |
| 115 | + |
| 116 | +;;; clojure-mode-indentation-test.el ends here |
0 commit comments