deftest is a macro.  Macros are expanded at compile time.  So, in this
case, at compile time, a function called namespace2 is def'd with meta
data :test set to the body of your deftest.

All of that body is namespace resolved in macro expansion, before
in-ns is ever executed (which happens when you actually call the
namespace2 function created by the macro).  Put another way, (def
anything 10) is namespace resolved to (def
learn.clojure.test.core/anything 10) at macro expansion time (compile
time), before the test function is ever called, and thereby before
in-ns is ever executed.

Hope this helps.

 - Mark



On Mon, Aug 15, 2011 at 5:07 PM, Richard  Rattigan <ratti...@gmail.com> wrote:
> I'm finding that namespaces don't seem to behave as I expect
> intuitively, or according to the reference. It's quite possible I'm in
> the wrong here though, as I'm just kicking clojure's tires at this
> point.
>
> Here is the relevant doc:
>
> http://clojure.org/special_forms
> (def symbol init?)
> Creates and interns or locates a global var with the name of symbol
> and a namespace of the value of the current namespace (*ns*).
>
> In the test below, which succeeds, the var does not appear to end up
> in the "current namespace" per this definition. Am I misinterpreting
> something, or is this a deviation from the spec/reference?
>
> (ns learn.clojure.test.core
>  (:use [clojure.test]))
> (deftest namespace2
>  (in-ns 'my.new.namespace)
>  ;confirm the current namespace
>  (is (= "my.new.namespace" (str *ns*)))
>  ;attempt to def a var in the current namespace
>  (def anything 10)
>  ;the var is not defined in the current namespace
>  (is (nil? (ns-resolve *ns* 'anything)))
>  ;the var is however definined in the orginal namespace
>  (is (not (nil? (ns-resolve (find-ns 'learn.clojure.test.core)
> 'anything))))
>  (is (= 10 learn.clojure.test.core/anything)))
>
> On Aug 15, 1:42 pm, Alan Malloy <a...@malloys.org> wrote:
>> On Aug 15, 6:16 am, Richard  Rattigan <ratti...@gmail.com> wrote:
>>
>>
>>
>>
>>
>>
>>
>>
>>
>> > I don't understand why this test would fail - can anyone explain why?
>>
>> > (ns learn.clojure.test.core
>> >   (:use [clojure.test]))
>> > (deftest namespaces
>> >   (in-ns 'my.new.namespace)
>> >   (is (= "my.new.namespace" (str *ns*)))
>> >   (is (= `anything 'my.new.namespace/anything)))
>>
>> > expected: (= (quote learn.clojure.test.core/anything) (quote
>> > my.new.namespace/anything))
>> >   actual: (not (= learn.clojure.test.core/anything my.new.namespace/
>> > anything))
>>
>> > When I try this on the REPL, it seems to work as expected:
>>
>> > user=> (in-ns 'my.new.namespace)
>> > #<Namespace my.new.namespace>
>> > my.new.namespace=> `anything
>> > my.new.namespace/anything
>>
>> The syntax quoting happens at read time, before in-ns is executed. You
>> can do something similar at the repl:
>>
>> user=> (do (in-ns 'tmp) `foo)
>> user/foo
>
> --
> 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

-- 
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