overrding function from other namespace

671 views
Skip to first unread message

Martin Hauner

unread,
Mar 17, 2010, 6:12:17 PM3/17/10
to Clojure
Hi,

I trying to use clojure.contrib.mock. It says to override the function
report-problem to
integrate it into other test framework like clojure.test.

I've got this working, but the namespace switching looks a bit ugly.
Is there a better
way to handle this? Maybe something like

(ns clojure.contrib.mock
(defn... the override)
)

which avoids the extra code to switch back to the original namespace?


(ns apfloattest
(:use
apfloat
clojure.test
clojure.contrib.mock))


(ns clojure.contrib.mock
(:use
clojure.test))

; delegate mock reporting to clojure.test
(defn report-problem
([function expected actual message]
(is (= expected actual)
(str message " Function name: " function))))

(ns apfloattest)


(deftest test-sqrtf
(expect [apf (times 1 (returns (apf 5)))] (sqrtf 5)))

--
Martin

Kevin Downey

unread,
Mar 19, 2010, 7:34:10 PM3/19/10
to clo...@googlegroups.com
why are you def'ing your functions in the mock namespace? why are you
juggling namespaces at all?

> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clo...@googlegroups.com
> Note that posts from new members are moderated - please be patient with your first post.
> To unsubscribe from this group, send email to
> clojure+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
>
> To unsubscribe from this group, send email to clojure+unsubscribegooglegroups.com or reply to this email with the words "REMOVE ME" as the subject.
>

--
And what is good, Phaedrus,
And what is not good—
Need we ask anyone to tell us these things?

ataggart

unread,
Mar 19, 2010, 9:42:30 PM3/19/10
to Clojure
I'd imagine you should be using with-bindings:

user=> (ns foo)
nil
foo=> (defn bar [x] (inc x))
#'foo/bar
foo=> (ns user)
nil
user=> (foo/bar 5)
6
user=> (with-bindings {#'foo/bar #(dec %)} (foo/bar 5))
4

Martin Hauner

unread,
Mar 20, 2010, 6:50:40 AM3/20/10
to Clojure
Hi Kevin,

On 20 Mrz., 00:34, Kevin Downey <redc...@gmail.com> wrote:
> why are you def'ing your functions in the mock namespace? why are you
> juggling namespaces at all?

Because clojure.contrib.mock says so. The only way that worked for me
was switching the namespace. Without switching the namespace I get:

Name conflict, can't def report-problem because namespace: apfloattest
refers to:#'clojure.contrib.mock/report-problem

I'm not sure why you ask me why I'm using a bad solution when I'm
asking for a better one.. ;-))

--
Martin

Martin Hauner

unread,
Mar 20, 2010, 7:14:17 AM3/20/10
to Clojure
Hi,

yes, better than my solution. :) Wrapping the with-bindings around
the run-tests I can drop the namespace switch.

(with-bindings {#'clojure.contrib.mock/report-problem #'my-report-
problem}
(run-tests))

The only issue left is that when running the test with leiningen
("lein test") I don't have control of the run-tests call.

Oh, I just tried this one:

(with-bindings {#'clojure.contrib.mock/report-problem #'my-report-
problem}

(deftest test-sqrtf
(expect [apf (times 2 (returns (apf 5)))] (sqrtf 5)))

(run-tests) )

which works too. So I can simply wrap all my deftest's inside the with-
binding which also works when running the tests with "lein
test" (removing the run-tests to avoid running the test twice).

Thanks!

--
Martin

Reply all
Reply to author
Forward
0 new messages