File: test-helper.el

package info (click to toggle)
clojure-mode 5.20.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 25,228 kB
  • sloc: lisp: 6,718; makefile: 17
file content (103 lines) | stat: -rw-r--r-- 3,347 bytes parent folder | download | duplicates (2)
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
;;; test-helper.el --- Clojure Mode: Non-interactive unit-test setup  -*- lexical-binding: t; -*-

;; Copyright (C) 2014-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:

;; Non-interactive test suite setup.

;;; Code:

(message "Running tests on Emacs %s" emacs-version)

(defmacro with-clojure-buffer (text &rest body)
  "Create a temporary buffer, insert TEXT, switch to clojure-mode and evaluate BODY."
  (declare (indent 1))
  `(with-temp-buffer
     (erase-buffer)
     (insert ,text)
     (clojure-mode)
     ,@body))

(defmacro with-clojure-buffer-point (text &rest body)
  "Run BODY in a temporary clojure buffer with TEXT.

TEXT is a string with a | indicating where point is.  The | will be erased
and point left there."
  (declare (indent 2))
  `(progn
     (with-clojure-buffer ,text
       (goto-char (point-min))
       (re-search-forward "|")
       (delete-char -1)
       ,@body)))

(defmacro when-refactoring-it (description before after &rest body)
  "Return a buttercup spec.

Insert BEFORE into a buffer, evaluate BODY and compare the resulting buffer to
AFTER.

BODY should contain the refactoring that transforms BEFORE into AFTER.

DESCRIPTION is the description of the spec."
  (declare (indent 1))
  `(it ,description
     (with-clojure-buffer ,before
       ,@body
       (expect (buffer-string) :to-equal ,after))))

(defmacro when-refactoring-with-point-it (description before after &rest body)
  "Return a buttercup spec.

Like when-refactor-it but also checks whether point is moved to the expected
position.

BEFORE is the buffer string before refactoring, where a pipe (|) represents
point.

AFTER is the expected buffer string after refactoring, 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-buffer ,before
         (goto-char (point-min))
         (search-forward "|")
         (delete-char -1)
         ,@body
         (expect (buffer-string) :to-equal expected-state)
         (expect (point) :to-equal expected-cursor-pos)))))


;; https://emacs.stackexchange.com/a/55031
(defmacro with-temp-dir (temp-dir &rest body)
  "Create a temporary directory and bind its to TEMP-DIR while evaluating BODY.
Removes the temp directory at the end of evaluation."
  `(let ((,temp-dir (make-temp-file "" t)))
    (unwind-protect
      (progn
        ,@body)
      (delete-directory ,temp-dir t))))

(provide 'test-helper)
;;; test-helper.el ends here