diff --git a/buttercup.el b/buttercup.el index f6b3f42..250f5fb 100644 --- a/buttercup.el +++ b/buttercup.el @@ -803,7 +803,9 @@ mainly calls to `describe', `it' and `before-each'." `((let ,var ,@body)) body))) - `(buttercup-describe ,description (lambda () ,@new-body) ',env))) + (if env + `(buttercup-describe ,description (lambda () ,@new-body) ',env) + `(buttercup-describe ,description (lambda () ,@new-body))))) (defun buttercup-describe (description body-function &optional env) "Function to handle a `describe' form. diff --git a/tests/test-buttercup.el b/tests/test-buttercup.el index 54df118..f016ceb 100644 --- a/tests/test-buttercup.el +++ b/tests/test-buttercup.el @@ -315,7 +315,50 @@ (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)))))) + + (it "should support the :var argument after :env" + (expect (macroexpand '(describe "description" :env nil :var (foo bar) (+ foo bar))) + :to-equal + '(buttercup-describe "description" + (lambda () (let (foo bar) (+ foo bar)))))) + + (it "should support the :env" + (expect (macroexpand '(describe "description" :env ((kill-ring kill-ring)) (+ foo bar))) + :to-equal + '(buttercup-describe "description" + (lambda () (+ foo bar)) + '((kill-ring kill-ring))))) + + (defvar buttercup-test--special-var "global") + + (describe "Dynamic environment" + + (describe "preserving global value" + :env ((kill-ring kill-ring)) + + (it "should update environment locally in a test" + (with-temp-buffer + (insert "killed line") + (goto-char (point-min)) + (kill-line)) + (expect kill-ring :to-equal (list "killed line"))) + + (it "should restore environment between tests" + (expect kill-ring :to-be nil))) + + (describe "binding new value" + :env ((buttercup-test--special-var "local")) + + (it "should setup new dynamic environment for special vars" + (expect (bound-and-true-p buttercup-test--special-var) :to-be-truthy) + (expect buttercup-test--special-var :to-equal "local"))) + + (describe "keeping global value" + + (it "should use the global value for variable which is not locally updated" + (expect (bound-and-true-p buttercup-test--special-var) :to-be-truthy) + (expect buttercup-test--special-var :to-equal "global"))))) (describe "The `buttercup-describe' function" (it "should run the enclosing body" @@ -716,44 +759,44 @@ 23)) (it "works with strings" - (spy-on 'test-function :and-return-value "return value") - (expect (test-function 2 3) - :to-equal - "return value")) + (spy-on 'test-function :and-return-value "return value") + (expect (test-function 2 3) + :to-equal + "return value")) (it "works with vectors" - (spy-on 'test-function :and-return-value [1 2 3 4]) - (expect (test-function 2 3) - :to-equal - [1 2 3 4])) + (spy-on 'test-function :and-return-value [1 2 3 4]) + (expect (test-function 2 3) + :to-equal + [1 2 3 4])) (it "works with symbols" - (spy-on 'test-function :and-return-value 'symbol) - (expect (test-function 2 3) - :to-equal - 'symbol)) + (spy-on 'test-function :and-return-value 'symbol) + (expect (test-function 2 3) + :to-equal + 'symbol)) (it "works with conses" - (spy-on 'test-function :and-return-value '(1 . 2)) - (expect (test-function 2 3) - :to-equal - (cons 1 2))) + (spy-on 'test-function :and-return-value '(1 . 2)) + (expect (test-function 2 3) + :to-equal + (cons 1 2))) (it "works with lists" - (spy-on 'test-function :and-return-value '(1 2 3)) - (expect (test-function 2 3) - :to-equal - '(1 2 3))) + (spy-on 'test-function :and-return-value '(1 2 3)) + (expect (test-function 2 3) + :to-equal + '(1 2 3))) (it "works with alists" - (spy-on 'test-function :and-return-value '((first . 1) - (second . 2) - (third . 3))) - (expect (test-function 2 3) - :to-equal - '((first . 1) - (second . 2) - (third . 3))))) + (spy-on 'test-function :and-return-value '((first . 1) + (second . 2) + (third . 3))) + (expect (test-function 2 3) + :to-equal + '((first . 1) + (second . 2) + (third . 3))))) (describe ":and-call-fake keyword functionality" (before-each