-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbin_tests.py
executable file
·250 lines (174 loc) · 7.4 KB
/
bin_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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#!/usr/bin/env python
import subprocess
import unittest
from bin.lib import jsonops
def exec_cmd(cmd):
return subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE,
stderr=subprocess.PIPE).stdout.read()
def retcode_cmd(cmd):
return subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE,
stderr=subprocess.PIPE).wait()
class json_array_enum_tests(unittest.TestCase):
def test_empty(self):
self.assertNotEqual(
retcode_cmd('echo "" | ./bin/json-array-enum'), 0)
def test_array(self):
self.assertEqual(
jsonops.load_json(exec_cmd('echo "[1,2,3]" | ./bin/json-array-enum')),
{"0": 1, "1": 2, "2": 3})
self.assertEqual(
jsonops.load_json(exec_cmd('echo "[\\"foo\\",1,2,3]" | ./bin/json-array-enum')),
{"0": "foo", "1": 1, "2": 2, "3": 3})
def test_with_cnt(self):
self.assertEqual(
jsonops.load_json(exec_cmd('echo "[1,2,3]" | ./bin/json-array-enum -c 1')),
{"1": 1, "2": 2, "3": 3})
self.assertEqual(
jsonops.load_json(exec_cmd('echo "[\\"foo\\",1,2,3]" | ./bin/json-array-enum -c 2')),
{"2": "foo", "3": 1, "4": 2, "5": 3})
class json_array_get_tests(unittest.TestCase):
def test_empty(self):
self.assertNotEqual(
retcode_cmd('echo "" | ./bin/json-array-get 0'), 0)
self.assertNotEqual(
retcode_cmd('echo "[1,2,3]" | ./bin/json-array-get'), 0)
def test_array(self):
self.assertEqual(
exec_cmd('echo "[1,2,3]" | ./bin/json-array-get 1'),
'2\n')
self.assertEqual(
exec_cmd('echo "[\\"bar\\",2,3]" | ./bin/json-array-get 0'),
'"bar"\n')
class json_array_slice_tests(unittest.TestCase):
def test_empty(self):
self.assertNotEqual(
retcode_cmd('echo "" | ./bin/json-array-slice'), 0)
self.assertNotEqual(
retcode_cmd('echo "[1,2,3]" | ./bin/json-array-slice'), 0)
self.assertNotEqual(
retcode_cmd('echo "[1,2,3]" | ./bin/json-array-slice 1'), 0)
def test_array(self):
self.assertEqual(
jsonops.load_json(exec_cmd('echo "[1,2,3]" | ./bin/json-array-slice 1 1')),
[])
self.assertEqual(
jsonops.load_json(exec_cmd('echo "[1,2,3]" | ./bin/json-array-slice 1 2')),
[2])
class json_array_head_tests(unittest.TestCase):
def test_empty(self):
self.assertNotEqual(
retcode_cmd('echo "" | ./bin/json-array-head'), 0)
self.assertNotEqual(
retcode_cmd('echo "" | ./bin/json-array-head 1'), 0)
self.assertNotEqual(
retcode_cmd('echo "[1,2,3]" | ./bin/json-array-head'), 0)
def test_array(self):
self.assertEqual(
jsonops.load_json(exec_cmd('echo "[1,2,3]" | ./bin/json-array-head 0')),
[])
self.assertEqual(
jsonops.load_json(exec_cmd('echo "[1,2,3]" | ./bin/json-array-head 2')),
[1,2])
class json_array_tail_tests(unittest.TestCase):
def test_empty(self):
self.assertNotEqual(
retcode_cmd('echo "" | ./bin/json-array-tail'), 0)
self.assertNotEqual(
retcode_cmd('echo "" | ./bin/json-array-tail 1'), 0)
self.assertNotEqual(
retcode_cmd('echo "[1,2,3]" | ./bin/json-array-tail'), 0)
def test_array(self):
self.assertEqual(
jsonops.load_json(exec_cmd('echo "[1,2,3]" | ./bin/json-array-tail 3')),
[])
self.assertEqual(
jsonops.load_json(exec_cmd('echo "[1,2,3]" | ./bin/json-array-tail 1')),
[2,3])
class json_array_shuffle_tests(unittest.TestCase):
def test_empty(self):
self.assertNotEqual(
retcode_cmd('echo "" | ./bin/json-array-shuffle'), 0)
def test_array(self):
self.assertEqual(
len(jsonops.load_json(exec_cmd('echo "[1,2,3]" | ./bin/json-array-shuffle'))),
3)
class json_array_sort_tests(unittest.TestCase):
def test_empty(self):
self.assertNotEqual(
retcode_cmd('echo "" | ./bin/json-array-sort'), 0)
def test_array(self):
self.assertEqual(
jsonops.load_json(exec_cmd('echo "[1,2,3]" | ./bin/json-array-sort')),
[3,2,1])
self.assertEqual(
jsonops.load_json(exec_cmd('echo "[1,2,3]" | ./bin/json-array-sort -r')),
[1,2,3])
class json_array_uniq_tests(unittest.TestCase):
def test_empty(self):
self.assertNotEqual(
retcode_cmd('echo "" | ./bin/json-array-uniq'), 0)
def test_array(self):
self.assertEqual(
jsonops.load_json(exec_cmd('echo "[1,2,2,2,3,3,3]" | ./bin/json-array-uniq')),
[1,2,3])
class json_object_get_tests(unittest.TestCase):
def test_empty(self):
self.assertNotEqual(
retcode_cmd('echo "" | ./bin/json-object-get'), 0)
self.assertNotEqual(
retcode_cmd('echo "{\\"foo\\":\\"bar\\"}" | ./bin/json-object-get'), 0)
self.assertNotEqual(
retcode_cmd('echo "" | ./bin/json-object-get foo'), 0)
def test_array(self):
self.assertEqual(
exec_cmd('echo "{\\"foo\\":\\"bar\\"}" | ./bin/json-object-get foo'),
'"bar"\n')
self.assertEqual(
exec_cmd('echo "{\\"foo\\":1}" | ./bin/json-object-get foo'),
'1\n')
class json_object_keys_tests(unittest.TestCase):
def test_empty(self):
self.assertNotEqual(
retcode_cmd('echo "" | ./bin/json-object-keys'), 0)
def test_array(self):
self.assertEqual(
jsonops.load_json(exec_cmd('echo "{\\"foo\\":\\"bar\\"}" | ./bin/json-object-keys')),
["foo"])
self.assertEqual(
len(jsonops.load_json(exec_cmd('echo "{\\"0\\":\\"1\\", \\"1\\":\\"2\\"}" | ./bin/json-object-keys'))),
2)
class json_object_values_tests(unittest.TestCase):
def test_empty(self):
self.assertNotEqual(
retcode_cmd('echo "" | ./bin/json-object-values'), 0)
def test_array(self):
self.assertEqual(
jsonops.load_json(exec_cmd('echo "{\\"foo\\":\\"bar\\"}" | ./bin/json-object-values')),
["bar"])
self.assertEqual(
len(jsonops.load_json(exec_cmd('echo "{\\"0\\":\\"1\\", \\"1\\":\\"2\\"}" | ./bin/json-object-values'))),
2)
class json_print_tests(unittest.TestCase):
def test_empty(self):
self.assertNotEqual(
retcode_cmd('echo "" | ./bin/json-print'), 0)
self.assertNotEqual(
retcode_cmd('echo "foo" | ./bin/json-print'), 0)
self.assertNotEqual(
retcode_cmd('echo "[1" | ./bin/json-print'), 0)
self.assertNotEqual(
retcode_cmd('echo "{\\"foo\\":\\"bar\\"" | ./bin/json-print'), 0)
def test_array(self):
self.assertEqual(
jsonops.load_json(exec_cmd('echo "{\\"foo\\":\\"bar\\"}" | ./bin/json-print')),
{"foo":"bar"})
self.assertEqual(
jsonops.load_json(exec_cmd('echo "{\\"0\\":\\"1\\", \\"1\\":\\"2\\"}" | ./bin/json-print')),
{"0":"1", "1":"2"})
self.assertEqual(
jsonops.load_json(exec_cmd('echo "[0,\\"1\\",\\"foo\\",\\"3\\", null]" | ./bin/json-print')),
[0, "1", "foo", "3", None])
def main():
unittest.main()
if __name__ == '__main__':
main()