forked from rplevy/clojure-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patht_core.clj
More file actions
70 lines (60 loc) · 1.84 KB
/
t_core.clj
File metadata and controls
70 lines (60 loc) · 1.84 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
(ns clojure-python.t-core
(:require [midje.sweet :refer :all]
[clojure-python.core :as base]))
(fact "append-paths adds the path to system path"
(binding [base/*interp* (org.python.util.PythonInterpreter.)]
(-> (#'base/append-paths ["test/clojure_python/"])
.getLocals
(.__getitem__ "sys")
.path
set
(get "test/clojure_python/")))
=>
"test/clojure_python/")
(fact "init sets *interp* root binding (but only once)"
(do
(base/init {:libpaths ["test/clojure_python/"]})
(class base/*interp*))
=>
org.python.util.PythonInterpreter
(do
(base/init {:libpaths ["test/clojure_python/"]})
(class base/*interp*))
=>
(do
(base/init {:libpaths ["test/clojure_python/"]})
(class base/*interp*)))
(defmacro with-test-interp [& body]
`(base/with-interpreter
{:libpaths ["test/clojure_python/"]}
~@body))
(fact "with-interpreter dynamically binds a new interpreter environment"
(with-test-interp base/*interp*)
=not=>
(with-test-interp base/*interp*))
(fact "importing python modules works"
(with-test-interp
(base/py-import-lib example)
(class example))
=>
org.python.core.PyStringMap)
(fact "importing python functions works"
(with-test-interp
(base/py-import-lib example)
(base/import-fn example hello)
(fn? hello))
=>
true)
(fact "calling python functions works"
(with-test-interp
(base/py-import-lib example)
(base/import-fn example hello)
(hello "world"))
=>
"hello, world how are you."
(with-test-interp
(base/py-import-lib example)
((base/py-fn example hello)
"person"))
=>
"hello, person how are you.")