Skip to content

Commit

Permalink
Don't require the trace function to be passed in until construction time
Browse files Browse the repository at this point in the history
  • Loading branch information
amalloy committed Jul 29, 2011
1 parent f62b65b commit 6ba585a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 19 deletions.
2 changes: 1 addition & 1 deletion project.clj
@@ -1,4 +1,4 @@
(defproject useful "0.5.3"
(defproject useful "0.5.4"
:description "A collection of generally-useful Clojure utility functions"
:dependencies [[clojure "1.2.0"]
[org.clojure/tools.macro "0.1.1"]]
Expand Down
16 changes: 8 additions & 8 deletions src/useful/experimental.clj
Expand Up @@ -104,9 +104,10 @@
(concat coll [item])))]

(defmacro protocol-stub
[name trace-fn proto-specs]
(let [[trace impl-field ret] (map gensym '(trace impl ret))
impl-kw (keyword impl-field)
[name proto-specs]
(let [[trace-field impl-field ret] (map gensym '(trace impl ret))
[impl-kw trace-kw] (map keyword [impl-field trace-field])
trace-fn (fn [this] `(~trace-kw ~this))

proto-fns
(mapify
Expand All @@ -126,11 +127,10 @@
`([~@argvec]
(let [~ret ~(when forward?
`(~fn-name (~impl-kw ~this) ~@args))]
~(->> `(~trace '~short-name (list ~@argvec))
~(->> `(~(trace-fn this) '~short-name (list ~@argvec))
(append-if forward? ret))
~ret))))}))}))]
`(do
(defrecord ~name [~impl-field])
(let [~trace ~trace-fn]
(extend ~name
~@(apply concat proto-fns)))))))
(defrecord ~name [~impl-field ~trace-field])
(extend ~name
~@(apply concat proto-fns))))))
19 changes: 9 additions & 10 deletions test/useful/experimental_test.clj
Expand Up @@ -33,22 +33,21 @@
(define [this k v] false)
(lookup [this k] :not-found))

(def call-log (atom []))

(protocol-stub StubImpl
(fn
([f [this & args]]
(reset! call-log (keyed [f args])))
([f [this & args] ret]
(reset! call-log (keyed [f args ret]))))

{Sample {:default :forward}
Define {:default :stub,
:exceptions [lookup]}})

(deftest stub-test
(let [real-impl (Implementor.)
stub-impl (StubImpl. real-impl)]
(reset! call-log []) ;; in case of stale test state
(let [call-log (atom [])
real-impl (Implementor.)
stub-impl (StubImpl. real-impl
(fn
([f [this & args]]
(reset! call-log (keyed [f args])))
([f [this & args] ret]
(reset! call-log (keyed [f args ret])))))]
(testing "default action works without exceptions"
(is (= [] @call-log))
(is (= 10 (sample real-impl 'whatever)))
Expand Down

0 comments on commit 6ba585a

Please sign in to comment.