Skip to content

Commit

Permalink
[Issue #82] introduce fn pop-opts-map
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexBaranosky committed Mar 18, 2012
1 parent 74effe7 commit df51fa7
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
6 changes: 2 additions & 4 deletions src/midje/checkers/defining.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

(ns ^{:doc "Various ways to define checkers."}
midje.checkers.defining
(:use [midje.util.form-utils :only [pop-docstring]]))
(:use [midje.util.form-utils :only [pop-docstring pop-opts-map]]))

(defn as-checker
"Turns an already existing function into a checker. Checkers can be used
Expand Down Expand Up @@ -36,9 +36,7 @@
{:arglists '([name docstring? attr-map? bindings+bodies])}
[name & args]
(let [[docstring more-args] (pop-docstring args)
[attr-map bindings+bodies] (if (map? (first more-args))
[(first more-args) (rest more-args)]
[{} more-args])]
[attr-map bindings+bodies] (pop-opts-map more-args)]
(working-with-arglists+bodies name docstring attr-map bindings+bodies))))

(defmacro checker
Expand Down
6 changes: 2 additions & 4 deletions src/midje/ideas/formulas.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(ns ^{:doc "Midje's special blend of generative-style testing."}
midje.ideas.formulas
(:use [midje.util.form-utils :only [first-named? named? pop-docstring]]
(:use [midje.util.form-utils :only [first-named? named? pop-docstring pop-opts-map]]
[midje.error-handling.validation-errors :only [simple-report-validation-error
validate valid-let]]
[midje.ideas.prerequisites :only [is-head-of-form-providing-prerequisites?]]
Expand Down Expand Up @@ -36,9 +36,7 @@

(defn- deconstruct-formula-args [args]
(let [[docstring? more-args] (pop-docstring args)
[opts-map bindings body] (if (map? (first more-args))
[(first more-args) (second more-args) (rest (rest more-args))]
[{} (first more-args) (rest more-args)])]
[opts-map [bindings & body]] (pop-opts-map more-args)]
[docstring? opts-map bindings body]))

(defmacro formula
Expand Down
17 changes: 12 additions & 5 deletions src/midje/util/form_utils.clj
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,15 @@ metadata (as provided by def) merged into the metadata of the original."
`(fn [] ~x))))

(defn pop-docstring
"Used to extract optional docstring args"
[forms]
(if (string? (first forms))
[(first forms) (rest forms)]
[nil forms]))
"Used to extract optional docstring from head of args"
[args]
(if (string? (first args))
[(first args) (rest args)]
[nil args]))

(defn pop-opts-map
"Used to extract optional docstring from head of args"
[args]
(if (map? (first args))
[(first args) (rest args)]
[{} args]))

0 comments on commit df51fa7

Please sign in to comment.