From f48aa9607b7511ddec3135615cfd03ba946ddd95 Mon Sep 17 00:00:00 2001 From: Rob Levy Date: Mon, 19 Mar 2012 10:30:22 -0700 Subject: [PATCH] make syntax nicer (rewritten for new backend changes) --- clojure/src/main/clj/cucumber/runtime/clj.clj | 19 +++++----- .../cucumber/runtime/clojure/stepdefs.clj | 36 ++++++++----------- 2 files changed, 24 insertions(+), 31 deletions(-) diff --git a/clojure/src/main/clj/cucumber/runtime/clj.clj b/clojure/src/main/clj/cucumber/runtime/clj.clj index edafb03542..2fd1ed937a 100644 --- a/clojure/src/main/clj/cucumber/runtime/clj.clj +++ b/clojure/src/main/clj/cucumber/runtime/clj.clj @@ -119,19 +119,18 @@ (defmacro step-macros [& names] (cons 'do (for [name names] - `(defmacro ~name [pattern# fun#] - `(add-step-definition ~pattern# ~fun# + `(defmacro ~name [pattern# binding-form# & body#] + `(add-step-definition ~pattern# + (fn ~binding-form# ~@body#) '~{:file *file* :line (:line (meta ~'&form))}))))) (step-macros Given When Then And But) -(defmacro Before [& fun] - (when (not (empty? fun)) - (let [fun (first fun)] - `(add-hook-definition :before [] ~fun)))) -(defmacro After [& fun] - (when (not (empty? fun)) - (let [fun (first fun)] - `(add-hook-definition :after [] ~fun)))) +(defmacro Before [binding-form & body] + `(add-hook-definition :before [] (fn ~binding-form ~@body))) + +(defmacro After [binding-form & body] + `(add-hook-definition :after [] (fn ~binding-form ~@body))) + diff --git a/clojure/src/test/resources/cucumber/runtime/clojure/stepdefs.clj b/clojure/src/test/resources/cucumber/runtime/clojure/stepdefs.clj index c1a8cf7069..294e59261b 100644 --- a/clojure/src/test/resources/cucumber/runtime/clojure/stepdefs.clj +++ b/clojure/src/test/resources/cucumber/runtime/clojure/stepdefs.clj @@ -2,30 +2,24 @@ (def some-state (atom "'Before' hasn't run.")) -(Before - (fn [] - (do - (reset! some-state "'Before' has run.") - (println "Executing 'Before'.")))) +(Before [] + (reset! some-state "'Before' has run.") + (println "Executing 'Before'.")) -(After - (fn [] - (println (str "Executing 'After' " @some-state)))) +(After [] + (println (str "Executing 'After' " @some-state))) -(Given #"^I have (\d+) cukes in my belly$" - #(eat (Float. %1))) +(Given #"^I have (\d+) cukes in my belly$" [cuke-count] + (eat (Float. cuke-count))) -(Given #"^I have this many cukes in my belly:$" - #(doseq [x (.raw %1)] (eat (Float. (first x))))) +(Given #"^I have this many cukes in my belly:$" [cuke-table] + (doseq [x (.raw cuke-table)] (eat (Float. (first x))))) -(When #"^there are (\d+) cukes in my belly$" - (fn [expected] - (assert (= (last-meal) (Float. expected))))) +(When #"^there are (\d+) cukes in my belly$" [expected] + (assert (= (last-meal) (Float. expected)))) -(Then #"^the (.*) contains (.*)$" - (fn [container, ingredient] - (assert (= "glass" container)))) +(Then #"^the (.*) contains (.*)$" [container ingredient] + (assert (= "glass" container))) -(When #"^I add (.*)$" - (fn [liquid] - (assert (= "milk" liquid)))) +(When #"^I add (.*)$" [liquid] + (assert (= "milk" liquid)))