Skip to content

Commit

Permalink
Check position of the :Var and :var* keywords
Browse files Browse the repository at this point in the history
Signal an error if the :Var or :var* keyword is found in any other
position than the first argument of a describe macro.  Fixes jorgenschaefer#223.

There are several issues with the :var(*) keywords, the most
important being it's inability to handle dynamic variables (jorgenschaefer#127).
But there is also the question of what the correct behaviour would be
if you use multiple :var(*) or when they are put anywhere but at the
beginning of the describe macro.
  • Loading branch information
snogge committed Aug 14, 2022
1 parent 9022546 commit fa966dc
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
5 changes: 4 additions & 1 deletion buttercup.el
Original file line number Diff line number Diff line change
Expand Up @@ -863,7 +863,10 @@ mainly calls to `describe', `it' and `before-each'."
`((let* ,(elt body 1)
,@(cddr body))))
(t body))))
`(buttercup-describe ,description (lambda () ,@new-body))))
(if (or (memq :var new-body)
(memq :var* new-body))
`(error "buttercup: :var(*) found in invalid position of describe form \"%s\"" ,description)
`(buttercup-describe ,description (lambda () ,@new-body)))))

(defun buttercup-describe (description body-function)
"Function to handle a `describe' form.
Expand Down
4 changes: 3 additions & 1 deletion docs/writing-tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,9 @@ as full sentences in traditional

The `describe` macro supports the optional `:var` and `:var*` args.
These bind variables for the suite by passing them as a varlist to the
`let` and `let*` form respectively.
`let` and `let*` form respectively. Only one instance of `:var` or
`:var*` is allowed, and it must come first in the `describe` form. It
can not be interspersed between `it` statements.

```Emacs-Lisp
(describe "A spec using :VAR"
Expand Down
11 changes: 10 additions & 1 deletion tests/test-buttercup.el
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,16 @@ text properties using `ansi-color-apply'."
(expect (macroexpand '(describe "description" :var* (foo bar) (+ foo bar)))
:to-equal
'(buttercup-describe "description"
(lambda () (let* (foo bar) (+ foo bar)))))))
(lambda () (let* (foo bar) (+ foo bar))))))
(describe "should error when "
(it ":var is not first"
(expect (macroexpand '(describe "description" (it "foo") :var (x)))
:to-equal
'(error "buttercup: :var(*) found in invalid position of describe form \"%s\"" "description")))
(it ":var* is not first"
(expect (macroexpand '(describe "description" (it "foo") :var* (x)))
:to-equal
'(error "buttercup: :var(*) found in invalid position of describe form \"%s\"" "description")))))

(describe "The `buttercup-describe' function"
(it "should run the enclosing body"
Expand Down

0 comments on commit fa966dc

Please sign in to comment.