Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
139 lines (110 loc) · 4.74 KB

File metadata and controls

139 lines (110 loc) · 4.74 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
import unittest
import json
import re
from os import path
from jsonquerylang import parse, JsonQueryParseOptions
class ParseTestCase(unittest.TestCase):
def test_suite(self):
"""Run the official parse test-suite"""
test_suite_file = (
path.dirname(path.realpath(__file__)) + "/test-suite/parse.test.json"
)
with open(test_suite_file, "r") as read_file:
suite = json.load(read_file)
for group in suite["groups"]:
for test in group["tests"]:
message = f"[{group['category']}] {group['description']} (input: {test['input']})"
if "output" in test:
with self.subTest(message=message):
self.assertEqual(parse(test["input"]), test["output"])
else:
with self.subTest(message=message):
self.assertRaisesRegex(
SyntaxError,
re.escape(test["throws"]),
lambda: parse(test["input"]),
)
def test_options1(self):
"""should parse a custom function"""
options: JsonQueryParseOptions = {"functions": {"customFn": lambda: lambda: 42}}
self.assertEqual(
parse('customFn(.age, "desc")', options),
["customFn", ["get", "age"], "desc"],
)
# built-in functions should still be available
self.assertEqual(parse("add(2, 3)", options), ["add", 2, 3])
def test_options2(self):
"""should parse a custom operator without vararg"""
options: JsonQueryParseOptions = {
"operators": [{"name": "aboutEq", "op": "~=", "at": "=="}]
}
self.assertEqual(
parse(".score ~= 8", options), ["aboutEq", ["get", "score"], 8]
)
# built-in operators should still be available
self.assertEqual(parse(".score == 8", options), ["eq", ["get", "score"], 8])
self.assertRaisesRegex(
SyntaxError,
re.escape("Unexpected part '~= 4'"),
lambda: parse("2 ~= 3 ~= 4", options),
)
def test_options3(self):
"""should parse a custom operator with vararg without left_associative"""
options: JsonQueryParseOptions = {
"operators": [{"name": "aboutEq", "op": "~=", "at": "==", "vararg": True}]
}
self.assertEqual(parse("2 and 3 and 4", options), ["and", 2, 3, 4])
self.assertEqual(parse("2 ~= 3", options), ["aboutEq", 2, 3])
self.assertEqual(parse("2 ~= 3 and 4", options), ["and", ["aboutEq", 2, 3], 4])
self.assertEqual(parse("2 and 3 ~= 4", options), ["and", 2, ["aboutEq", 3, 4]])
self.assertEqual(parse("2 == 3 ~= 4", options), ["aboutEq", ["eq", 2, 3], 4])
self.assertEqual(parse("2 ~= 3 == 4", options), ["eq", ["aboutEq", 2, 3], 4])
self.assertRaisesRegex(
SyntaxError,
re.escape("Unexpected part '~= 4'"),
lambda: parse("2 ~= 3 ~= 4", options),
)
self.assertRaisesRegex(
SyntaxError,
re.escape("Unexpected part '== 4'"),
lambda: parse("2 == 3 == 4", options),
)
def test_options4(self):
"""should parse a custom operator with vararg without left_associative"""
options: JsonQueryParseOptions = {
"operators": [
{
"name": "aboutEq",
"op": "~=",
"at": "==",
"vararg": True,
"left_associative": True,
}
]
}
self.assertEqual(parse("2 and 3 and 4", options), ["and", 2, 3, 4])
self.assertEqual(parse("2 ~= 3", options), ["aboutEq", 2, 3])
self.assertEqual(parse("2 ~= 3 ~= 4", options), ["aboutEq", 2, 3, 4])
self.assertRaisesRegex(
SyntaxError,
re.escape("Unexpected part '== 4'"),
lambda: parse("2 == 3 == 4", options),
)
def test_options5(self):
"""should throw an error in case of an invalid custom operator"""
options: JsonQueryParseOptions = {"operators": [dict()]}
self.assertRaisesRegex(
RuntimeError,
re.escape("Invalid custom operator"),
lambda: parse(".score > 8", options),
)
def test_options6(self):
"""should throw an error in case of an invalid custom operator (2)"""
options: JsonQueryParseOptions = {"operators": dict()}
self.assertRaisesRegex(
RuntimeError,
re.escape("Invalid custom operators"),
lambda: parse(".score > 8", options),
)
if __name__ == "__main__":
unittest.main()
Morty Proxy This is a proxified and sanitized view of the page, visit original site.