-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathtest_JSON.py
More file actions
143 lines (112 loc) · 5.32 KB
/
test_JSON.py
File metadata and controls
143 lines (112 loc) · 5.32 KB
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
import pytest
from utilities import get_runtime_data_file
import yup
#==================================================================================================
def test_parse_with_valid_json_returns_correct_result():
text = '{"key": "value"}'
expected = {"key": "value"}
result = yup.JSON.parse(text)
assert result == expected
#==================================================================================================
def test_parse_with_invalid_json_returns_empty_var():
text = '{"key": "value"'
result = yup.JSON.parse(text)
assert not result
#==================================================================================================
def test_parse_with_file_returns_correct_result():
file_path = get_runtime_data_file("valid_json_file.json")
file_path.replaceWithText('{"key": "value"}')
file = yup.File(file_path)
expected = {"key": "value"}
result = yup.JSON.parse(file)
assert result == expected
#==================================================================================================
def test_parse_with_stream_returns_correct_result():
stream = yup.MemoryInputStream(b'{"key": "value"}', True)
expected = {"key": "value"}
result = yup.JSON.parse(stream)
assert result == expected
#==================================================================================================
def test_to_string_with_default_options():
obj_to_format = {"key": "value"}
expected = '{"key": "value"}'
result = yup.JSON.toString(obj_to_format, allOnOneLine=True)
assert result == expected
#==================================================================================================
def test_format_options():
format_options = yup.JSON.FormatOptions().withSpacing(yup.JSON.Spacing.none)
assert format_options.getSpacing() == yup.JSON.Spacing.none
format_options = yup.JSON.FormatOptions().withIndentLevel(10)
assert format_options.getIndentLevel() == 10
format_options = yup.JSON.FormatOptions().withMaxDecimalPlaces(4)
assert format_options.getMaxDecimalPlaces() == 4
#==================================================================================================
def test_to_string_with_format_options_no_spacing():
obj_to_format = {"key1": "value", "key2": 1, "key3": 2}
format_options = yup.JSON.FormatOptions().withSpacing(yup.JSON.Spacing.none)
expected = '{"key1":"value","key2":1,"key3":2}'
result = yup.JSON.toString(obj_to_format, format_options)
assert result == expected
#==================================================================================================
def test_to_string_with_format_options_single_line():
obj_to_format = {"key1": "value", "key2": 1, "key3": 2}
format_options = yup.JSON.FormatOptions().withSpacing(yup.JSON.Spacing.singleLine)
expected = '{"key1": "value", "key2": 1, "key3": 2}'
result = yup.JSON.toString(obj_to_format, format_options)
assert result == expected
#==================================================================================================
def test_to_string_with_format_options_multi_line():
obj_to_format = {"key1": "value", "key2": 1, "key3": 2, "obj": { "obj1": 1, "obj2": [1, 2, 3, 4] }}
format_options = yup.JSON.FormatOptions().withSpacing(yup.JSON.Spacing.multiLine).withIndentLevel(2)
expected = """ {
"key1": "value",
"key2": 1,
"key3": 2,
"obj": {
"obj1": 1,
"obj2": [
1,
2,
3,
4
]
}
}"""
result = yup.JSON.toString(obj_to_format, format_options)
assert result.replace("\r", "").strip() == expected.replace("\r", "").strip()
#==================================================================================================
@pytest.mark.skip(reason="Bug in YUP")
def test_to_string_with_format_options_max_decimal_places():
obj_to_format = {"key": 0.123456789}
format_options = yup.JSON.FormatOptions().withMaxDecimalPlaces(2)
expected = '{"key": 0.12}'
result = yup.JSON.toString(obj_to_format, format_options)
assert result == expected
#==================================================================================================
def test_write_to_stream():
stream = yup.MemoryOutputStream()
expected = {"key": "value"}
yup.JSON.writeToStream(stream, expected, allOnOneLine=True)
assert stream.toString() == '{"key": "value"}'
#==================================================================================================
def test_from_string_with_valid_json():
json_string = '{"key": "value"}'
expected = {"key": "value"}
result = yup.JSON.fromString(json_string)
assert result == expected
#==================================================================================================
def test_escape_string_escapes_special_characters():
original_string = '"Hello, \nWorld!"'
expected = '\\"Hello, \\nWorld!\\"'
result = yup.JSON.escapeString(original_string)
assert result == expected
#==================================================================================================
"""
def test_parse_quoted_string_with_valid_input():
text = '"Hello, World!"'
expected = "Hello, World!"
char_pointer = text
result_var = None
result = yup.JSON.parseQuotedString(char_pointer, result_var)
assert result.wasOk() and result_var == expected
"""