Skip to content

Commit 205e18e

Browse files
Warn when :var is used with a special variable
Buttercup's ":var" and ":var*" forms only support lexical variables, so using a special variable in these forms will almost certainly not do what the test author expects. See #127.
1 parent d9b22e6 commit 205e18e

File tree

1 file changed

+26
-12
lines changed

1 file changed

+26
-12
lines changed

buttercup.el

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -951,19 +951,33 @@ mainly calls to `describe', `it' and `before-each'."
951951
(unless lexical-binding
952952
(signal 'buttercup-dynamic-binding-error
953953
"buttercup requires `lexical-binding' to be t"))
954-
(let ((new-body
955-
(cond
956-
((eq (elt body 0) :var)
957-
`((let ,(elt body 1)
958-
,@(cddr body))))
959-
((eq (elt body 0) :var*)
960-
`((let* ,(elt body 1)
961-
,@(cddr body))))
962-
(t body))))
963-
(if (or (memq :var new-body)
964-
(memq :var* new-body))
954+
;; Convert `:var' or `:var*' to a lexical let form
955+
(when
956+
(memq (elt body 0) '(:var :var*))
957+
(let ((let-form-to-use (if (eq (elt body 0) :var) 'let 'let*))
958+
(let-bindings (elt body 1))
959+
(let-body (cddr body)))
960+
;; Verify that all the specified variables are lexical
961+
(cl-loop
962+
for binding in let-bindings
963+
for var = (if (symbolp binding) binding (car binding))
964+
when (special-variable-p var)
965+
do (display-warning
966+
'buttercup-describe
967+
(concat "Possible erroneous use of special variable `"
968+
(symbol-name var)
969+
"' in :var(*) form")))
970+
;; Wrap new body in the appropriate let form
971+
(setq body
972+
`((,let-form-to-use
973+
;; Let bindings
974+
,let-bindings
975+
;; Let body
976+
,@let-body)))))
977+
(if (or (memq :var body)
978+
(memq :var* body))
965979
`(error "buttercup: :var(*) found in invalid position of describe form \"%s\"" ,description)
966-
`(buttercup-describe ,description (lambda () ,@new-body)))))
980+
`(buttercup-describe ,description (lambda () ,@body))))
967981

968982
(defun buttercup-describe (description body-function)
969983
"Function to handle a `describe' form.

0 commit comments

Comments
 (0)