forked from clj-python/libpython-clj
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequire_python_test.clj
More file actions
77 lines (65 loc) · 2.87 KB
/
Copy pathrequire_python_test.clj
File metadata and controls
77 lines (65 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
(ns libpython-clj.require-python-test
(:require [libpython-clj.require :as req :refer [require-python]
:reload true]
[libpython-clj.python :as py]
[clojure.test :refer :all]))
;; Since this test mutates the global environment we have to accept that
;; it may not always work.
(require-python '[math
:refer :*
:exclude [sin cos]
:as pymath])
(deftest base-require-test
(let [publics (ns-publics (find-ns 'libpython-clj.require-python-test))]
(is (not (contains? publics 'sin)))
(is (= 10.0 (double (floor 10.1))))
(is (thrown? Throwable (require-python '[math :refer [blah]])))))
(deftest parse-flags-test
;; sanity check
(is (= #{:reload :foo}
#{:foo :reload}))
(let [parse-flags #'req/parse-flags]
(is (= #{:reload} (parse-flags
#{:reload}
'[:reload foo])))
(is (= #{:reload} (parse-flags #{:reload} '[:reload true])))
(is (= #{:reload} (parse-flags #{:reload} '[:reload :as foo])))
(is (= #{:reload} (parse-flags #{:reload} '[:reload foo :as])))
(is (= #{:reload} (parse-flags #{:reload} '[foo :reload :as bar])))
(is (= #{} (parse-flags #{:reload} '[:reload false])))
(is (= #{:reload} (parse-flags #{:reload} '[:reload false :reload])))
(is (= #{:reload} (parse-flags #{:reload} '[:reload false :reload true])))
(is (= #{} (parse-flags #{:reload} '[:reload :reload false])))
(is (= #{} (parse-flags #{:reload} '[:reload true :reload false])))
(is (= #{:a :b} (parse-flags #{:a :b} '[:a true :b])))
(is (= #{:a :b} (parse-flags #{:a :b} '[:a :b])))
(is (= #{:a :b} (parse-flags #{:a :b} '[:a true :b])))
(is (= #{:a :b} (parse-flags #{:a :b} '[:a :b true])))
(is (= #{:a :b} (parse-flags #{:a :b} '[:a true :b true])))
(is (= #{:b} (parse-flags #{:a :b} '[:a false :b true])))
(is (= #{:b} (parse-flags #{:a :b} '[:b true :a false])))
(is (= #{:b} (parse-flags #{:a :b} '[:b :a false])))
(is (= #{:b :a} (parse-flags #{:a :b} '[:b :a false :a true])))))
(require-python '([builtins :as python]
[builtins.list :as python.pylist]))
;; NOTE -- even though builtins.list has been aliased to
;; to python.pylist, you are still required to require
;; "builtins as python" in order to construct a list
(deftest require-python-classes-with-alias-test
(let [l (python/list)]
(is (= (vec l) []))
(python.pylist/append l 1)
(is (= (vec l) [1]))
(python.pylist/append l 3)
(is (= (vec l) [1 3]))
(python.pylist/append l 5)
(is (= (vec l) [1 3 5]))
(python.pylist/clear l)
(is (= (python/list) (vec l)))))
(require-python 'csv.DictWriter)
(deftest require-python-classes
;; simple creation/recall test
(is csv.DictWriter/__init__)
(is csv.DictWriter/writeheader)
(is csv.DictWriter/writerow)
(is csv.DictWriter/writerows))