Skip to content

Commit 553cdfb

Browse files
committed
Add keyword config for etest
1 parent 8a6df8e commit 553cdfb

File tree

3 files changed

+70
-11
lines changed

3 files changed

+70
-11
lines changed

test/etest/README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,34 @@ The `:cleanup` takes lisp expressions evaluated in LIFO order with unwind protec
137137
```
138138

139139

140+
## Reusing mode-specific configuration
141+
142+
Mode-specific configuration can be stored in a variable or created with a function. This typically includes an `:init` keyword that sets up the major-mode for the test buffer:
143+
144+
```elisp
145+
(defvar my-config '(:init ((mode . text))))
146+
```
147+
148+
The configuration may contain any valid etest code and is registered with the `:config` keyword. This keyword must appear before any other keywords:
149+
150+
```elisp
151+
(etest-deftest my-test ()
152+
:config my-config))
153+
```
154+
155+
You can also set it with the buffer-local variable `etest-local-config`. It is convenient to set it as a file-local variable, this way `etest-update` is automatically aware of the configuration relevant to the test file.
156+
157+
```elisp
158+
(etest-deftest my-test ())
159+
160+
;; Local Variables:
161+
;; etest-local-config: my-config
162+
;; End:
163+
```
164+
165+
If supplied, the `:config` keyword has precedence over the configuration stored in `etest-local-config` and completely replaces it.
166+
167+
140168
## Checking the contents of an inferior buffer
141169

142170
In addition to checking the side effects in the test buffer, it is often useful to check the side effects in an auxiliary buffer. For instance, checking the output in an inferior process buffer.

test/etest/etest.el

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ up a particular mode.")
4747
(lambda (actual expected)
4848
(should (string= actual expected)))))))
4949

50+
(defmacro etest--setup-body (place)
51+
`(progn
52+
(when (eq (car ,place) :config)
53+
(pop ,place)
54+
(setq ,place (append (eval (pop ,place)) ,place)))
55+
(etest--pop-init ,place)))
56+
5057
(defmacro etest--pop-init (place)
5158
`(let (local)
5259
(while (eq (car ,place) :init)
@@ -56,14 +63,15 @@ up a particular mode.")
5663

5764
;; Evaluate symbols to make it easier to set local variables
5865
(defmacro etest--push-local-config (place)
59-
`(let ((etest--config (cond ((not etest-local-config)
60-
nil)
61-
((symbolp etest-local-config)
62-
(eval etest-local-config))
63-
(t
64-
etest-local-config))))
65-
(when etest--config
66-
(setq ,place (append etest--config ,place)))))
66+
`(unless (eq (car ,place) :config)
67+
(let ((etest--config (cond ((not etest-local-config)
68+
nil)
69+
((symbolp etest-local-config)
70+
(eval etest-local-config))
71+
(t
72+
etest-local-config))))
73+
(when etest--config
74+
(setq ,place (append etest--config ,place))))))
6775

6876
(defmacro etest--with-test-buffer (init &rest body)
6977
(declare (indent 1)
@@ -104,7 +112,7 @@ buffer-local variable `etest-local-inferior-buffer'.
104112
105113
`:messages' keywords check the contents of the messages buffers
106114
and are processed with DO-RESULT."
107-
(etest--with-test-buffer (etest--pop-init body)
115+
(etest--with-test-buffer (etest--setup-body body)
108116
(let ((etest--msg-sentinel (etest--make-message-sentinel))
109117
etest--cleanup)
110118
(unwind-protect

test/etest/test-etest.el

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,14 +236,37 @@ bar"
236236
:eval "<right>"
237237
:result "fo¶o×")
238238

239+
240+
(defun etest-make-config ()
241+
'(:init ((mode . text))))
242+
243+
(etest-deftest etest-config-fun-test ()
244+
"Configuration is picked up from function."
245+
:config (etest-make-config)
246+
:eval (should (eq major-mode 'text-mode)))
247+
248+
249+
(defvar etest-some-config '(:init ((mode . text))))
250+
251+
(etest-deftest etest-config-var-test ()
252+
"Configuration is picked up from variable."
253+
:config etest-some-config
254+
:eval (should (eq major-mode 'text-mode)))
255+
256+
239257
;; `let' doesn't seem to work here, perhaps an interaction between
240258
;; scoping in macros and file-local variables
241259
(setq etest-local-config '(:init ((mode . text))))
242260

243-
(etest-deftest etest-local-config-test ()
244-
"Local configuration is picked up"
261+
(etest-deftest etest-config-local-test ()
262+
"Local configuration is picked up."
245263
:eval (should (eq major-mode 'text-mode)))
246264

265+
(etest-deftest etest-config-keyword-test ()
266+
"Keyword config has precedence over local config."
267+
:config nil
268+
:eval (should (eq major-mode 'fundamental-mode)))
269+
247270
(setq etest-local-config nil)
248271

249272
(etest-deftest etest-default-mode ()

0 commit comments

Comments
 (0)