Finding vars from dynamically created namespaces in clojure

The following test failed:

(ns clojure_refactoring.rename-fn-test (:use clojure.test)) (deftest test-fn-location (in-ns 'refactoring-test-fn-rename) (clojure.core/refer-clojure) (defn a [b] (inc b)) (in-ns 'clojure_refactoring.rename-fn-test) (is (not= (find-var 'refactoring-test-fn-rename/a) nil)) (remove-ns 'refactoring-test-fn-rename)) 

That is, find-var (from the just created var in the namespace that I just created) returns nil. This behavior does not occur in repl, where typing test steps works fine.

Am I doing something wrong, or is it just something that is not working in clojure right now?

+4
source share
1 answer

Updated to version that apparently does the intended thing, unlike my original answer ...

This version works:

 (ns clojure-refactoring.rename-fn-test (:use clojure.test [clojure.contrib.with-ns :only [with-ns]])) (deftest test-fn-location (create-ns 'refactoring-test-fn-rename) (with-ns 'refactoring-test-fn-rename (clojure.core/refer-clojure) (defn a [b] (inc b))) (is (not= (find-var 'refactoring-test-fn-rename/a) nil)) (remove-ns 'refactoring-test-fn-rename)) 

In addition, you really need to change all occurrences of _ in the namespace names to - , and vice versa - to file names.

With these changes in place, the test works fine for me. ( I didn’t even try to start it . Apparently, it still works without making changes _ / - , but in fact you need to do it! That the accepted agreement and everything is not so guaranteed to work if you do not follow his.)

For some reason, the code from the question seems to create Var a in the namespace in which the test was defined, so (find-var 'clojure-refactoring.rename-fn-test/a) returned Var, while the test failed . With the above value (find-var 'clojure-refactoring.rename-fn-test/a) returns nil as expected.

+1
source

Source: https://habr.com/ru/post/1310441/


All Articles