On Apr 26, 5:25 pm, David McNeil <mcneil.da...@gmail.com> wrote:
> I am experimenting with clojure.test and I encountered the following
> situation which I cannot explain.
>
> This code:
>
> (println (do
>            (ns ns01
>              (:use clojure.test))
>            (deftest test1 nil)
>            (run-tests)))
>
> Produces the expected result (note: it runs one test):
This is actually NOT what I'm seeing.  At least when I run it, it
appears to run exactly zero tests.

> [snip]
>
> (println (let []
>            (do
>              (ns ns02
>                (:use clojure.test))
>              (deftest test1 nil)
>              (run-tests))))
>
> Then I get the unexpected result that no tests are executed:
The let here is, I believe a red herring.  Again, with what I'm
seeing, this form executes the same way as the above and runs exactly
zero tests.

> Seems there is something going on with namespaces that I do not
> understand and I hope that somewhere here can explain it.
I think I can explain the above behavior and suggest a way to obtain
the behavior you want.
I don't think it has as much to do with namespaces as it does with the
evaluation rule and it's relationship to def.

You've defined a do, which will execute each of the forms in turn, but
the deftest (a macro which expands to a def form) creates it's test
function in the namespace that the do was executed in, not the
namespace created and switched to just previously.

In fact, if you check your namespace, you should see that test1 is
defined not in ns01/ns02, but in user (or whichever namespace you
executed the do from).

What I can't sufficiently explain is why def doesn't see the binding
of *ns* but run-tests obviously does, as it tries to find all tests
defined in ns01/ns02 (of which there are none).
(println (do
           (ns ns03
             (:use clojure.test))
           (binding [*ns* (find-ns 'ns03)]
             (deftest test3 nil))
           (run-tests)))

Now, when the do evaluates the second form, it uses the namespace that
was created by the first form.

Hope this helps.

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@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+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to