1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
|
;;; clojure-mode-syntax-test.el --- Clojure Mode: syntax related tests -*- lexical-binding: t; -*-
;; Copyright (C) 2015-2021 Bozhidar Batsov <bozhidar@batsov.dev>
;; 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 Mode
;;; Code:
(require 'clojure-mode)
(require 'buttercup)
(require 'test-helper "test/utils/test-helper")
(defun non-func (form-a form-b)
(with-clojure-buffer form-a
(save-excursion (insert form-b))
(clojure--not-function-form-p)))
(describe "clojure--not-function-form-p"
(it "should handle forms that are not funcions"
(dolist (form '(("#?@ " "(c d)")
("#?@" "(c d)")
("#? " "(c d)")
("#?" "(c d)")
("" "[asda]")
("" "{a b}")
("#" "{a b}")
("" "(~)")))
(expect (apply #'non-func form))))
(it "should handle forms that are funcions"
(dolist (form '("(c d)"
"(.c d)"
"(:c d)"
"(c/a d)"
"(.c/a d)"
"(:c/a d)"
"(c/a)"
"(:c/a)"
"(.c/a)"))
(expect (non-func "" form) :to-be nil)
(expect (non-func "^hint" form) :to-be nil)
(expect (non-func "#macro" form) :to-be nil)
(expect (non-func "^hint " form) :to-be nil)
(expect (non-func "#macro " form) :to-be nil))))
(describe "clojure-match-next-def"
(let ((some-sexp "\n(list [1 2 3])"))
(it "handles vars with metadata"
(dolist (form '("(def ^Integer a 1)"
"(def ^:a a 1)"
"(def ^::a a 1)"
"(def ^::a/b a 1)"
"(def ^{:macro true} a 1)"))
(with-clojure-buffer (concat form some-sexp)
(end-of-buffer)
(clojure-match-next-def)
(expect (looking-at "(def")))))
(it "handles vars without metadata"
(with-clojure-buffer (concat "(def a 1)" some-sexp)
(end-of-buffer)
(clojure-match-next-def)
(expect (looking-at "(def"))))
(it "handles invalid def forms"
(dolist (form '("(def ^Integer)"
"(def)"
"(def ^{:macro})"
"(def ^{:macro true})"
"(def ^{:macro true} foo)"
"(def ^{:macro} foo)"))
(with-clojure-buffer (concat form some-sexp)
(end-of-buffer)
(clojure-match-next-def)
(expect (looking-at "(def"))))))
(it "captures var name"
(dolist (form '("(def some-name 1)"
"(def some-name)"
"(def ^:private some-name 2)"
"(def ^{:private true} some-name 3)"))
(with-clojure-buffer form
(end-of-buffer)
(clojure-match-next-def)
(cl-destructuring-bind (name-beg name-end) (match-data)
(expect (string= "some-name" (buffer-substring name-beg name-end)))))))
(it "captures var name with dispatch value for defmethod"
(dolist (form '("(defmethod some-name :key [a])"
"(defmethod ^:meta some-name :key [a])"
"(defmethod ^{:meta true} some-name :key [a])"
"(defmethod some-name :key)"))
(with-clojure-buffer form
(end-of-buffer)
(clojure-match-next-def)
(cl-destructuring-bind (name-beg name-end) (match-data)
(expect (string= "some-name :key" (buffer-substring name-beg name-end))))))))
(describe "clojure syntax"
(it "handles prefixed symbols"
(dolist (form '(("#?@aaa" . "aaa")
("#?aaa" . "?aaa")
("#aaa" . "aaa")
("'aaa" . "aaa")))
(with-clojure-buffer (car form)
;; FIXME: Shouldn't there be an `expect' here?
(equal (symbol-name (symbol-at-point)) (cdr form)))))
(it "skips prefixes"
(dolist (form '("#?@aaa" "#?aaa" "#aaa" "'aaa"))
(with-clojure-buffer form
(backward-word)
(backward-prefix-chars)
(expect (bobp))))))
(describe "fill-paragraph"
(it "should work within comments"
(with-clojure-buffer "
;; Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt
;; ut labore et dolore magna aliqua."
(goto-char (point-min))
(let ((fill-column 80))
(fill-paragraph))
(expect (buffer-string) :to-equal "
;; Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
;; tempor incididunt ut labore et dolore magna aliqua.")))
(it "should work within inner comments"
(with-clojure-buffer "
(let [a 1]
;; Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt
;; ut labore et dolore
;; magna aliqua.
)"
(goto-char (point-min))
(forward-line 2)
(let ((fill-column 80))
(fill-paragraph))
(expect (buffer-string) :to-equal "
(let [a 1]
;; Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
;; tempor incididunt ut labore et dolore magna aliqua.
)")))
(when (fboundp 'font-lock-ensure)
(it "should not alter surrounding code"
(with-clojure-buffer "(def my-example-variable
\"It has a very long docstring. So long, in fact, that it wraps onto multiple lines! This is to demonstrate what happens when the docstring wraps over three lines.\"
nil)"
(font-lock-ensure)
(goto-char 40)
(let ((clojure-docstring-fill-column 80)
(fill-column 80))
(fill-paragraph))
(expect (buffer-string) :to-equal "(def my-example-variable
\"It has a very long docstring. So long, in fact, that it wraps onto multiple
lines! This is to demonstrate what happens when the docstring wraps over three
lines.\"
nil)")))))
(when (fboundp 'font-lock-ensure)
(describe "clojure-in-docstring-p"
(it "should handle def with docstring"
(with-clojure-buffer "(def my-example-variable
\"Doc here and `doc-here`\"
nil)"
(font-lock-ensure)
(goto-char 32)
(expect (clojure-in-docstring-p))
(goto-char 46)
(expect (clojure-in-docstring-p))))))
(provide 'clojure-mode-syntax-test)
;;; clojure-mode-syntax-test.el ends here
|