Skip to content

Commit f72431d

Browse files
committed
add codegen_test
1 parent 1032d44 commit f72431d

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
(ns libpython-clj2.codegen-test
2+
(:require [clojure.test :refer [deftest is testing]]
3+
[clojure.string :as s]
4+
[clojure.java.io :as io]
5+
[libpython-clj2.codegen :as codegen]))
6+
7+
(defn- ns-symbol-for
8+
[py-mod-or-cls ns-prefix]
9+
(symbol (str (when-not (s/blank? ns-prefix)
10+
(str ns-prefix "."))
11+
py-mod-or-cls)))
12+
13+
(deftest ns-prefix-nil-test
14+
(testing "ns-prefix nil should not produce leading dot"
15+
(is (= 'numpy (ns-symbol-for "numpy" nil)))
16+
(is (= 'builtins (ns-symbol-for "builtins" nil))))
17+
18+
(testing "ns-prefix empty string should not produce leading dot"
19+
(is (= 'numpy (ns-symbol-for "numpy" "")))
20+
(is (= 'builtins (ns-symbol-for "builtins" ""))))
21+
22+
(testing "ns-prefix with value should produce prefixed namespace"
23+
(is (= 'python.numpy (ns-symbol-for "numpy" "python")))
24+
(is (= 'my.prefix.builtins (ns-symbol-for "builtins" "my.prefix")))))
25+
26+
(deftest write-namespace-nil-prefix-test
27+
(testing "write-namespace! with nil ns-prefix"
28+
(let [tmp-dir (str (System/getProperty "java.io.tmpdir") "/libpython-clj-test-" (System/currentTimeMillis))]
29+
(try
30+
(codegen/write-namespace! "builtins" {:output-dir tmp-dir
31+
:ns-prefix nil})
32+
(is (.exists (io/file tmp-dir "builtins.clj")))
33+
(let [content (slurp (io/file tmp-dir "builtins.clj"))]
34+
(is (re-find #"\(ns builtins" content)))
35+
(finally
36+
(doseq [f (reverse (file-seq (io/file tmp-dir)))]
37+
(.delete f)))))))

0 commit comments

Comments
 (0)