-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
executable file
·142 lines (125 loc) · 4.07 KB
/
tests.py
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/env python
import py4j
import unittest, logging, sys, os
from zensols.clojure import Clojure
logger = logging.getLogger('py4j.clojure.test')
class TestClojure(unittest.TestCase):
def test_call(self):
self.assertEqual('test||one||2234', Clojure.call('clojure.string', 'join', '||', ['test', 'one', 2234]))
def test_nippy(self):
cw = Clojure('taoensso.nippy')
try:
cw.add_depenedency('com.taoensso', 'nippy', '2.13.0')
dat = cw.invoke('freeze', [123, 'strarg', 1.2])
thawed = cw.invoke('thaw', dat)
self.assertEqual(123, thawed[0])
self.assertEqual('strarg', thawed[1])
self.assertEqual(1.2, thawed[2])
finally:
cw.close()
def test_entrypoint(self):
cw = Clojure()
try:
ep = cw.entry_point
logger.info('entry point: %s' % ep)
eps = ep.toString()
self.assertGreater(len(eps), 0)
finally:
cw.close()
def test_eval(self):
cw = Clojure()
try:
ret = cw.eval('(+ 1 1 a b)', {'a': 5.5, 'b': 8})
self.assertEqual(ret, 15.5)
finally:
cw.close()
def test_other_namespace(self):
cw = Clojure('clojure.string')
try:
ret = cw.eval("""
(join "," ["one" "two" "three"])
""")
self.assertEqual(ret, "one,two,three")
finally:
cw.close()
def test_namespace(self):
cw = Clojure()
try:
ret = cw.eval("""
(with-ns 'clojure.string
(join "," ["one" "two" "three"]))
""")
self.assertEqual(ret, "one,two,three")
finally:
cw.close()
def test_temp_namespace(self):
cw = Clojure()
try:
ret = cw.eval("""
(with-temp-ns 'some-new-ns
(def never-seen 2)
(require '[clojure.string :as s])
(s/join "," ["one" "two" "three" never-seen]))
""")
self.assertEqual(ret, "one,two,three,2")
finally:
cw.close()
def test_entrypoint_ns(self):
cw = Clojure()
try:
ep = cw.entry_point
self.assertEqual(ep.eval("""
(with-ns 'clojure.string
(capitalize "stuff"))
"""), "Stuff")
finally:
cw.close()
def log_val(self, v):
logger.info('aval: %s (%s)' % (v, type(v)))
def test_python_object(self):
cw = Clojure()
try:
o = cw.eval("""{:a 1 :b 2 :arr [{:animal "dog"} 99.2 :kw]}""")
o = cw.python_object(o)
self.assertTrue(isinstance(o, dict))
aval = o['a']
self.log_val(aval)
self.assertTrue(isinstance(aval, int) or isinstance(aval, long))
self.assertEqual(aval, 1)
arr = o['arr']
self.assertTrue(isinstance(arr, list))
arrmap = arr[0]
self.assertTrue(isinstance(arrmap, dict))
mval = arrmap['animal']
self.log_val(mval)
self.assertTrue(isinstance(mval, str) or isinstance(mval, unicode))
self.assertEqual('dog', mval)
self.log_val(arr[1])
self.assertEqual(arr[1], 99.2)
self.log_val(arr[2])
self.assertTrue(isinstance(arr[2], str) or isinstance(arr[2], unicode))
self.assertEqual(arr[2], ':kw')
finally:
cw.close()
def test_embedded_python_object(self):
cw = Clojure()
try:
o = cw.eval("""{:last :map}""")
o = cw.eval("""{:a 1 :b 2 :arr [{:animal "dog"} 99.2 :kw lm]}""", {'lm': o})
o = cw.python_object(o)
self.assertEqual(o['arr'][3]['last'], ':map')
finally:
cw.close()
def enable_debug():
logging.basicConfig(level=logging.WARN)
if 'TEST_DEBUG' in os.environ and os.environ['TEST_DEBUG']:
logger.setLevel(logging.DEBUG)
def main(args=sys.argv[1:]):
enable_debug()
if len(args) > 0 and args[0] == 'kill':
print('shutting down...')
Clojure.kill_server()
else:
unittest.main()
if __name__ == '__main__':
main()