Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

syntactic sugar tweak for clojure dsl #244

Closed
rplevy-draker opened this Issue · 6 comments

3 participants

@rplevy-draker

In my cucumber step definitions I find it convenient to wrap the Given/When/Then macros in what I consider to be slightly better syntactic sugar.

For example:

(defmacro Given'
  [re binding-form & body]
  `(Given ~re (fn ~binding-form ~@body)))

It occurred to me that there's no reason to expose the (fn [] ... ) in step definitions, for any use case I can imagine. So why not make this the default syntax?

Looking at the way the dsl is implemented I think the best way to do this is not to change the implementation of stepdef at all, but just replace for example (def Given stepdef) with a defmacro expressions similar the one shown above.

If you agree with this feature, I'll submit a pull request for a modified clojure/src/main/resources/cucumber/runtime/clojure/dsl.clj

@rplevy-draker

After looking at the cucumber-jvm Clojure DSL code, this is how I now do it in my step definition code:

(defmacro stepdef-sugar [re binding-form & body] 
  `(stepdef ~re (fn ~binding-form ~@body)))

(defmacro ^:private clone-macro [existing-macro new-macros]
  (cons 'do (mapcat
             (fn [m#]
               `((def ~m# #'~existing-macro)
                 (. #'~m# (setMacro))))
             new-macros)))

(clone-macro stepdef-sugar [Given When Then And But])

This would be pretty much the exact code I would add to https://github.com/cucumber/cucumber-jvm/blob/master/clojure/src/main/resources/cucumber/runtime/clojure/dsl.clj

@aslakhellesoy

I don't know enough Clojure to have an opinion on this, so if you say this is better I'm sure it is! Please send along the pullreq.

@nilswloka
Collaborator

Just noticed that this will probably be obsolete if the clojure-native pull request is merged (#265).

@aslakhellesoy

Yes, obsolete now

@rplevy-draker

So by obsolete do you mean it is incompatible? I agree, that is the case. However, I would still like to add this feature to cucumber-jvm. It will just need to be written differently.

@aslakhellesoy

See comment in #248

@aslakhellesoy aslakhellesoy reopened this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.