Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Browse files

Create custom macro for templating tests with optional parameters to …

…certain functions
  • Loading branch information...
1 parent 6d46f61 commit 56ad730e829bdaacacdb39830de61251af9c629b @semperos committed
Showing with 46 additions and 2 deletions.
  1. +46 −2 test/clj_webdriver/test/util.clj
48 test/clj_webdriver/test/util.clj
@@ -1,5 +1,7 @@
(ns clj-webdriver.test.util
- (:use [ring.adapter.jetty :only [run-jetty]]
+ (:use [clojure.test :only [deftest]]
+ [clojure.template :only [apply-template]]
+ [ring.adapter.jetty :only [run-jetty]]
(:require [clj-webdriver.test.example-app.core :as web-app]))
@@ -25,4 +27,46 @@
"Ensure a number is between a min and a max, both exclusive"
[n min max]
(and (> n min)
- (< n max)))
+ (< n max)))
+(defn multirember
+ "Remove every occurrence of an item from a list, recurring into nested lists where necessary. Pulled from my exercises while working through The Little Schemer."
+ [item l]
+ (cond
+ (empty? l) '()
+ (not (list? (first l)))
+ (cond
+ (= (first l) item) (multirember item (next l))
+ :else (cons (first l) (multirember item (next l))))
+ :else (cons (multirember item (first l)) (multirember item (next l)))))
+(defmacro deftest-template-param
+ "Create two deftest's for the price of one. Every instance of `__` in the body of this macro will be (1) replaced with `optional-param` for one deftest definition and (2) will be deleted for the other deftest definition generated by this macro. If there are no replacements to be made, the macro will not produce an extra deftest.
+ Note: Though this macro looks like it fills the same role as clojure.template/apply-template, the apply-template macro can only replace templated values with other values. We do that (using the same prewalk-replace that apply-template uses), but we also produce a deftest form that completely removes the templated portion, since the intention is to template in an optional argument to functions.
+ Example:
+ (deftest-template-param test-attribute my-driver
+ (is (= (attribute __ \"div\" \"class\") \"active\")))
+ ;; Would expand to:
+ (do
+ (deftest test-attribute
+ (is (= (attribute \"div\" \"class\") \"active\")))
+ (deftest test-attribute-with-optional-param
+ (is (= (attribute my-driver \"div\" \"class\") \"active\"))))"
+ [test-name optional-param & body]
+ (let [body-no-param (multirember '__ body)
+ body-with-param (apply-template '[__] body [optional-param])
+ test-name-no-param test-name
+ test-name-with-param (symbol (str test-name "-with-optional-param"))]
+ (if (= body-no-param body-with-param)
+ `(deftest ~test-name-no-param
+ ~@body-no-param)
+ `(do
+ (deftest ~test-name-no-param
+ ~@body-no-param)
+ (deftest ~test-name-with-param
+ ~@body-with-param)))))

0 comments on commit 56ad730

Please sign in to comment.
Something went wrong with that request. Please try again.