Skip to content

Commit

Permalink
Resolve issue scgilardi#20, properly implement test for selector form
Browse files Browse the repository at this point in the history
  • Loading branch information
scgilardi committed Feb 25, 2012
1 parent a2f4df5 commit 3647564
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/slingshot/support.clj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@
[smap coll]
(clojure.walk/postwalk-replace smap coll))

(defn appears-within?
"Returns true if x appears within coll at any nesting depth"
[x coll]
(let [result (atom false)]
(clojure.walk/postwalk
(fn [t]
(when (= x t)
(reset! result true)))
coll)
@result))

(defn throw-arg
"Throws an IllegalArgumentException with a message given arguments
for clojure.core/format"
Expand Down Expand Up @@ -155,7 +166,8 @@
(throw-arg "key-value selector: %s does not match: %s"
(pr-str selector) "[key val]"))))
(selector-form []
(and (seq? selector) selector))
(and (seq? selector) (appears-within? '% selector)
selector))
(predicate []
`(~selector ~'%))]
(->> (or (class-name) (key-value) (selector-form) (predicate))
Expand Down
8 changes: 8 additions & 0 deletions test/slingshot/test/slingshot.clj
Original file line number Diff line number Diff line change
Expand Up @@ -316,3 +316,11 @@
&throw-context))]
(is (= "wrapper-0" (.getMessage ^Throwable (:wrapper context))))
(is (= "wrapper-1" (.getMessage ^Throwable (:throwable context))))))

(deftest test-inline-predicate
(is (= :not-caught (try+
(throw+ {:foo true})
(catch #(-> % :foo (= false)) data
:caught)
(catch Object _
:not-caught)))))

0 comments on commit 3647564

Please sign in to comment.