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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
|
# coding=utf8
from __future__ import unicode_literals
import jq
from .tools import assert_equal, assert_is
def test_output_of_dot_operator_is_input():
assert_equal(
"42",
jq.compile(".").input("42").first()
)
def test_can_add_one_to_each_element_of_an_array():
assert_equal(
[2, 3, 4],
jq.compile("[.[]+1]").input([1, 2, 3]).first()
)
def test_nan_is_outputted_as_null():
assert_equal(
None,
jq.compile("0/0").input(0).first(),
)
def test_positive_infinity_is_outputted_as_positive_max_double():
assert_equal(
1.7976931348623157e+308,
jq.compile(". * 2").input(1.7976931348623157e+308).first(),
)
def test_negative_infinity_is_outputted_as_negative_max_double():
assert_equal(
-1.7976931348623157e+308,
jq.compile(". * 2").input(-1.7976931348623157e+308).first(),
)
def test_can_use_regexes():
assert_equal(
True,
jq.compile('test(".*")').input("42").first()
)
assert_equal(
True,
jq.compile('test("^[0-9]+$")').input("42").first()
)
assert_equal(
False,
jq.compile('test("^[0-9]+$")').input("42a").first()
)
def test_when_text_argument_is_used_then_input_is_treated_as_json_text():
assert_equal(
42,
jq.compile(".").input(text="42").first()
)
def test_when_text_method_is_used_on_result_then_output_is_serialised_to_json_string():
assert_equal(
'"42"',
jq.compile(".").input("42").text()
)
def test_elements_in_text_output_are_separated_by_newlines():
assert_equal(
"1\n2\n3",
jq.compile(".[]").input([1, 2, 3]).text()
)
def test_when_first_method_is_used_on_result_then_first_element_of_result_is_returned():
assert_equal(
2,
jq.compile(".[]+1").input([1, 2, 3]).first()
)
def test_when_all_method_is_used_on_result_then_all_elements_are_returned_in_list():
assert_equal(
[2, 3, 4],
jq.compile(".[]+1").input([1, 2, 3]).all()
)
def test_can_treat_execute_result_as_iterable():
iterator = iter(jq.compile(".[]+1").input([1, 2, 3]))
assert_equal(2, next(iterator))
assert_equal(3, next(iterator))
assert_equal(4, next(iterator))
assert_equal("end", next(iterator, "end"))
def test_can_execute_same_program_again_before_consuming_output_of_first_execution():
program = jq.compile(".[]+1")
first = iter(program.input([1, 2, 3]))
assert_equal(2, next(first))
second = iter(program.input([11, 12, 13]))
assert_equal(12, next(second))
assert_equal(3, next(first))
assert_equal(4, next(first))
assert_equal(13, next(second))
assert_equal(14, next(second))
def test_iterators_from_same_program_and_input_are_independent():
program_with_input = jq.compile(".[]+1").input([1, 2, 3])
first = iter(program_with_input)
assert_equal(2, next(first))
second = iter(program_with_input)
assert_equal(2, next(second))
assert_equal(3, next(first))
assert_equal(4, next(first))
assert_equal(3, next(second))
assert_equal(4, next(second))
def test_multiple_inputs_in_text_input_are_separated_by_newlines():
assert_equal(
[2, 3, 4],
jq.compile(".+1").input(text="1\n2\n3").all()
)
def test_value_error_is_raised_if_program_is_invalid():
try:
jq.compile("!")
assert False, "Expected error"
except ValueError as error:
expected_error_str = "jq: error: syntax error, unexpected INVALID_CHARACTER, expecting $end (Unix shell quoting issues?) at <top-level>, line 1:\n!\njq: 1 compile error"
assert_equal(str(error), expected_error_str)
def test_value_error_is_raised_if_input_cannot_be_processed_by_program():
program = jq.compile(".x")
try:
program.input(1).all()
assert False, "Expected error"
except ValueError as error:
expected_error_str = "Cannot index number with string \"x\""
assert_equal(str(error), expected_error_str)
def test_errors_do_not_leak_between_transformations():
program = jq.compile(".x")
try:
program.input(1).all()
assert False, "Expected error"
except ValueError as error:
pass
assert_equal(1, program.input({"x": 1}).first())
def test_value_error_is_raised_if_input_is_not_valid_json():
program = jq.compile(".x")
try:
program.input(text="!!").first()
assert False, "Expected error"
except ValueError as error:
expected_error_str = "parse error: Invalid numeric literal at EOF at line 1, column 2"
assert_equal(str(error), expected_error_str)
def test_unicode_strings_can_be_used_as_input():
assert_equal(
"‽",
jq.compile(".").input(text='"‽"').first()
)
def test_unicode_strings_can_be_used_as_programs():
assert_equal(
"Dragon‽",
jq.compile('.+"‽"').input(text='"Dragon"').first()
)
def test_returned_integers_larger_than_32_bits_have_int_type():
program = jq.compile("pow(.; 33)")
result = program.input(2).first()
assert_equal(2 ** 33, result)
assert_equal(int, type(result))
def test_compiling_with_args_should_set_predefined_variables():
assert_equal(
123,
jq.compile("$a + $b + .", args={"a": 100, "b": 20}).input(3).first()
)
def test_repr_of_compile_result_is_compilation_string():
program = jq.compile(".")
repr_string = repr(program)
assert_equal("jq.compile({!r})".format("."), repr_string)
def test_program_string_can_be_retrieved_from_program():
program = jq.compile(".")
assert_equal(".", program.program_string)
class TestJvToPython(object):
def test_program_preserves_null(self):
program = jq.compile(".")
assert_is(None, program.input(text='null').first())
def test_program_preserves_bools(self):
program = jq.compile(".")
assert_is(False, program.input(text="false").first())
assert_is(True, program.input(text="true").first())
def test_program_preserves_ints(self):
program = jq.compile(".")
assert_equal(int, type(program.input(text="0").first()))
assert_equal(0, program.input(text="0").first())
assert_equal(1, program.input(text="1").first())
assert_equal(-1, program.input(text="-1").first())
assert_equal(12345, program.input(text="12345").first())
assert_equal(-12345, program.input(text="-12345").first())
def test_program_preserves_floats(self):
program = jq.compile(".")
assert_equal(float, type(program.input(text="1.1").first()))
assert_equal(3.14159, program.input(text="3.14159").first())
assert_equal(-3.14159, program.input(text="-3.14159").first())
assert_equal(42E100, program.input(text="42E100").first())
assert_equal(-42E100, program.input(text="-42E100").first())
def test_program_preserves_strings(self):
program = jq.compile(".")
assert_equal(type(""), type(program.input(text='""').first()))
assert_equal("", program.input(text='""').first())
assert_equal("x", program.input(text='"x"').first())
assert_equal("xyz", program.input(text='"xyz"').first())
def test_program_preserves_arrays(self):
program = jq.compile(".")
assert_equal(list, type(program.input(text='[]').first()))
assert_equal([], program.input(text='[]').first())
assert_equal([1], program.input(text='[1]').first())
assert_equal([1, 2], program.input(text='[1, 2]').first())
assert_equal([3.14159], program.input(text='[3.14159]').first())
assert_equal([3.14159, 95141.3], program.input(text='[3.14159, 95141.3]').first())
assert_equal([False], program.input(text='[false]').first())
assert_equal([False, True], program.input(text='[false, true]').first())
assert_equal([[]], program.input(text='[[]]').first())
assert_equal([[[]]], program.input(text='[[[]]]').first())
assert_equal([[], []], program.input(text='[[], []]').first())
assert_equal([[[], []], [[], []]], program.input(text='[[[], []], [[], []]]').first())
assert_equal([{}], program.input(text='[{}]').first())
assert_equal([{"": []}], program.input(text='[{"": []}]').first())
assert_equal([{"1": [], "2": []}], program.input(text='[{"1": [], "2": []}]').first())
def test_program_preserves_objects(self):
program = jq.compile(".")
assert_equal(dict, type(program.input(text='{}').first()))
assert_equal({}, program.input(text='{}').first())
assert_equal({"": True}, program.input(text='{"": true}').first())
assert_equal({"": False}, program.input(text='{"": false}').first())
assert_equal({"": 0}, program.input(text='{"": 0}').first())
assert_equal({"": 1}, program.input(text='{"": 1}').first())
assert_equal({"a": 0, "b": 1}, program.input(text='{"a": 0, "b": 1}').first())
assert_equal({"": 3.14159}, program.input(text='{"": 3.14159}').first())
assert_equal({"a": 3.14159, "b": 95141.3},
program.input(text='{"a": 3.14159, "b": 95141.3}').first())
assert_equal({"": ""}, program.input(text='{"": ""}').first())
assert_equal({"": "x"}, program.input(text='{"": "x"}').first())
assert_equal({"": []}, program.input(text='{"": []}').first())
assert_equal({"": [{}]}, program.input(text='{"": [{}]}').first())
assert_equal({"a": [1], "b": [2]},
program.input(text='{"a": [1], "b": [2]}').first())
assert_equal({"": {}}, program.input(text='{"": {}}').first())
assert_equal({"a": {}, "b": {}}, program.input(text='{"a": {}, "b": {}}').first())
assert_equal({"": {"": {}}}, program.input(text='{"": {"": {}}}').first())
class TestConvenienceFunctions(object):
def test_first_function_with_json_value_input_returns_first_output_element(self):
output = jq.first(".[] + 1", [1, 2, 3])
assert_equal(2, output)
def test_first_function_with_json_text_input_returns_first_output_element(self):
output = jq.first(".[] + 1", text="[1, 2, 3]")
assert_equal(2, output)
def test_text_function_with_json_value_input_returns_all_output_elements_as_string(self):
output = jq.text(".[] + 1", [1, 2, 3])
assert_equal("2\n3\n4", output)
def test_text_function_with_json_text_input_returns_all_output_elements_as_string(self):
output = jq.text(".[] + 1", text="[1, 2, 3]")
assert_equal("2\n3\n4", output)
def test_all_function_with_json_value_input_returns_all_output_elements_in_list(self):
output = jq.all(".[] + 1", [1, 2, 3])
assert_equal([2, 3, 4], output)
def test_all_function_with_json_text_input_returns_all_output_element_in_list(self):
output = jq.all(".[] + 1", text="[1, 2, 3]")
assert_equal([2, 3, 4], output)
def test_iter_function_with_json_value_input_returns_all_output_elements_in_iterator(self):
iterator = jq.iter(".[] + 1", [1, 2, 3])
assert_equal(2, next(iterator))
assert_equal(3, next(iterator))
assert_equal(4, next(iterator))
assert_equal("end", next(iterator, "end"))
def test_iter_function_with_json_text_input_returns_all_output_element_in_iterator(self):
iterator = jq.iter(".[] + 1", text="[1, 2, 3]")
assert_equal(2, next(iterator))
assert_equal(3, next(iterator))
assert_equal(4, next(iterator))
assert_equal("end", next(iterator, "end"))
|