Skip to content

Commit aa7d285

Browse files
committed
Port font lock test infrastructure and some basic tests over from clojure-mode
1 parent 2f1a02e commit aa7d285

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
;;; clojure-ts-mode-font-lock-test.el --- Clojure TS Mode: font lock 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+
28+
;; (use-package buttercup)
29+
30+
;;;; Utilities
31+
32+
(defmacro with-fontified-clojure-ts-buffer (content &rest body)
33+
"Evaluate BODY in a temporary buffer with CONTENT."
34+
(declare (debug t)
35+
(indent 1))
36+
`(with-clojure-ts-buffer ,content
37+
(font-lock-ensure)
38+
(goto-char (point-min))
39+
,@body))
40+
41+
(defun clojure-ts-get-face-at (start end content)
42+
"Get the face between START and END in CONTENT."
43+
(with-fontified-clojure-ts-buffer content
44+
(let ((start-face (get-text-property start 'face))
45+
(all-faces (cl-loop for i from start to end collect (get-text-property
46+
i 'face))))
47+
(if (cl-every (lambda (face) (eq face start-face)) all-faces)
48+
start-face
49+
'various-faces))))
50+
51+
(defun expect-face-at (content start end face)
52+
"Expect face in CONTENT between START and END to be equal to FACE."
53+
(expect (clojure-ts-get-face-at start end content) :to-equal face))
54+
55+
(defun expect-faces-at (content &rest faces)
56+
"Expect FACES in CONTENT.
57+
58+
FACES is a list of the form (content (start end expected-face)*)"
59+
(dolist (face faces)
60+
(apply (apply-partially #'expect-face-at content) face)))
61+
62+
(defmacro when-fontifying-it (description &rest tests)
63+
"Return a buttercup spec.
64+
65+
TESTS are lists of the form (content (start end expected-face)*). For each test
66+
check that each `expected-face` is found in `content` between `start` and `end`.
67+
68+
DESCRIPTION is the description of the spec."
69+
(declare (indent 1))
70+
`(it ,description
71+
(dolist (test (quote ,tests))
72+
(apply #'expect-faces-at test))))
73+
74+
;;;; Font locking
75+
76+
(describe "clojure-ts-mode-syntax-table"
77+
(when-fontifying-it "should handle any known def form"
78+
("(def a 1)" (2 4 font-lock-keyword-face))
79+
("(defonce a 1)" (2 8 font-lock-keyword-face))
80+
("(defn a [b])" (2 5 font-lock-keyword-face))
81+
("(defmacro a [b])" (2 9 font-lock-keyword-face))
82+
("(definline a [b])" (2 10 font-lock-keyword-face))
83+
("(defmulti a identity)" (2 9 font-lock-keyword-face))
84+
("(defmethod a :foo [b] (println \"bar\"))" (2 10 font-lock-keyword-face))
85+
("(defprotocol a (b [this] \"that\"))" (2 12 font-lock-keyword-face))
86+
("(definterface a (b [c]))" (2 13 font-lock-keyword-face))
87+
("(defrecord a [b c])" (2 10 font-lock-keyword-face))
88+
("(deftype a [b c])" (2 8 font-lock-keyword-face))
89+
("(defstruct a :b :c)" (2 10 font-lock-keyword-face))
90+
("(deftest a (is (= 1 1)))" (2 8 font-lock-keyword-face))
91+
92+
93+
;; TODO: copied from clojure-mode, but failing
94+
;; ("(defne [x y])" (2 6 font-lock-keyword-face))
95+
;; ("(defnm a b)" (2 6 font-lock-keyword-face))
96+
;; ("(defnu)" (2 6 font-lock-keyword-face))
97+
;; ("(defnc [a])" (2 6 font-lock-keyword-face))
98+
;; ("(defna)" (2 6 font-lock-keyword-face))
99+
;; ("(deftask a)" (2 8 font-lock-keyword-face))
100+
;; ("(defstate a :start \"b\" :stop \"c\")" (2 9 font-lock-keyword-face)))
101+
)

0 commit comments

Comments
 (0)