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
|
# coding=utf8
from nose.tools import istest, assert_equal, assert_raises
from jq import jq
@istest
def output_of_dot_operator_is_input():
assert_equal(
"42",
jq(".").transform("42")
)
@istest
def can_add_one_to_each_element_of_an_array():
assert_equal(
[2, 3, 4],
jq("[.[]+1]").transform([1, 2, 3])
)
@istest
def input_string_is_parsed_to_json_if_raw_input_is_true():
assert_equal(
42,
jq(".").transform(text="42")
)
@istest
def output_is_serialised_to_json_string_if_text_output_is_true():
assert_equal(
'"42"',
jq(".").transform("42", text_output=True)
)
@istest
def elements_in_text_output_are_separated_by_newlines():
assert_equal(
"1\n2\n3",
jq(".[]").transform([1, 2, 3], text_output=True)
)
@istest
def first_output_element_is_returned_if_multiple_output_is_false_but_there_are_multiple_output_elements():
assert_equal(
2,
jq(".[]+1").transform([1, 2, 3])
)
@istest
def multiple_output_elements_are_returned_if_multiple_output_is_true():
assert_equal(
[2, 3, 4],
jq(".[]+1").transform([1, 2, 3], multiple_output=True)
)
@istest
def multiple_inputs_in_raw_input_are_separated_by_newlines():
assert_equal(
[2, 3, 4],
jq(".+1").transform(text="1\n2\n3", multiple_output=True)
)
@istest
def value_error_is_raised_if_program_is_invalid():
try:
jq("!")
assert False, "Expected error"
except ValueError as error:
expected_error_str = """error: syntax error, unexpected INVALID_CHARACTER, expecting $end
!
1 compile error"""
assert_equal(str(error), expected_error_str)
@istest
def value_error_is_raised_if_input_cannot_be_processed_by_program():
program = jq(".x")
try:
program.transform(1)
assert False, "Expected error"
except ValueError as error:
expected_error_str = "jq: error: Cannot index number with string"
assert_equal(str(error), expected_error_str)
@istest
def errors_do_not_leak_between_transformations():
program = jq(".x")
try:
program.transform(1)
assert False, "Expected error"
except ValueError as error:
pass
assert_equal(1, program.transform({"x": 1}))
@istest
def value_error_is_raised_if_input_is_not_valid_json():
program = jq(".x")
try:
program.transform(text="!!")
assert False, "Expected error"
except ValueError as error:
expected_error_str = "parse error: Invalid numeric literal\n"
assert_equal(str(error), expected_error_str)
@istest
def unicode_strings_can_be_used_as_input():
assert_equal(
u"‽",
jq(".").transform(text=u'"‽"')
)
@istest
def unicode_strings_can_be_used_as_programs():
assert_equal(
u"Dragon‽",
jq(u'.+"‽"').transform(text=u'"Dragon"')
)
|