forked from NPP-JSONViewer/JSON-Viewer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonSortByKeyTest.cpp
More file actions
149 lines (116 loc) · 5.94 KB
/
JsonSortByKeyTest.cpp
File metadata and controls
149 lines (116 loc) · 5.94 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
144
145
146
147
148
149
#include <gtest/gtest.h>
#include <string>
#include "JsonHandler.h"
namespace JsonSortByKey
{
class JsonSortByKeyTest : public ::testing::Test
{
protected:
JsonHandler m_jsonHandler {{}};
protected:
void SetUp() override {}
void TearDown() override {}
};
// Test SortJsonByKey
TEST_F(JsonSortByKeyTest, TestSortJsonByKey_Success)
{
std::string inputJson = R"({"b": "valueB", "a": "valueA"})";
auto result = m_jsonHandler.SortJsonByKey(inputJson, {}, {}, ' ', 4);
ASSERT_TRUE(result.success);
ASSERT_EQ(result.response, "{\n \"a\": \"valueA\",\n \"b\": \"valueB\"\n}");
}
TEST_F(JsonSortByKeyTest, TestSortJsonByKey_InvalidJson)
{
std::string inputJson = R"({"b": "valueB", "a": "valueA")"; // Invalid JSON
auto result = m_jsonHandler.SortJsonByKey(inputJson, {}, {}, ' ', 4);
ASSERT_FALSE(result.success);
}
TEST_F(JsonSortByKeyTest, TestSortJsonByKey_NumberValue_Success)
{
std::string inputJson = R"({"b": "valueB", "a": 30})";
auto result = m_jsonHandler.SortJsonByKey(inputJson, {}, {}, ' ', 4);
ASSERT_TRUE(result.success);
ASSERT_EQ(result.response, "{\n \"a\": 30,\n \"b\": \"valueB\"\n}");
std::string inputJson2 = R"({ "name": "My name?", "age": 27, "is_student": false })";
auto result2 = m_jsonHandler.SortJsonByKey(inputJson2, {}, {}, ' ', 2);
ASSERT_TRUE(result2.success);
ASSERT_EQ(result2.response, "{\n \"age\": 27,\n \"is_student\": false,\n \"name\": \"My name?\"\n}");
}
TEST_F(JsonSortByKeyTest, TestSortJsonByKey_NaN_Value_Success)
{
std::string inputJson = R"({"Nan": NaN, "Hello": "World"})";
auto result = m_jsonHandler.SortJsonByKey(inputJson, {}, {}, ' ', 4);
ASSERT_TRUE(result.success);
ASSERT_EQ(result.response, "{\n \"Hello\": \"World\",\n \"Nan\": NaN\n}");
}
TEST_F(JsonSortByKeyTest, TestSortJsonByKey_ArrayValue_Success)
{
std::string inputJson = R"({"text": ["Hello", "World"], "Inf": [-Infinity, Infinity, -Infinity, Infinity]})";
auto result = m_jsonHandler.SortJsonByKey(inputJson, {}, LF::kFormatSingleLineArray, ' ', 2);
ASSERT_TRUE(result.success);
ASSERT_EQ(result.response, "{\n \"Inf\": [-Infinity, Infinity, -Infinity, Infinity],\n \"text\": [\"Hello\", \"World\"]\n}");
}
TEST_F(JsonSortByKeyTest, TestSortJsonByKey_EmptyObject)
{
std::string inputJson = R"({})";
auto result = m_jsonHandler.SortJsonByKey(inputJson, {}, {}, ' ', 2);
ASSERT_TRUE(result.success);
ASSERT_EQ(result.response, "{}");
}
TEST_F(JsonSortByKeyTest, TestSortJsonByKey_NestedObject)
{
std::string inputJson = R"({"z": {"b": 2, "a": 1}, "a": {"c": 3}})";
auto result = m_jsonHandler.SortJsonByKey(inputJson, {}, {}, ' ', 2);
ASSERT_TRUE(result.success);
ASSERT_EQ(result.response, "{\n \"a\": {\n \"c\": 3\n },\n \"z\": {\n \"a\": 1,\n \"b\": 2\n }\n}");
}
TEST_F(JsonSortByKeyTest, TestSortJsonByKey_SpecialCharacters)
{
std::string inputJson = R"({"!": 1, "#": 2, "A": 3})";
auto result = m_jsonHandler.SortJsonByKey(inputJson, {}, {}, ' ', 2);
ASSERT_TRUE(result.success);
ASSERT_EQ(result.response, "{\n \"!\": 1,\n \"#\": 2,\n \"A\": 3\n}");
}
TEST_F(JsonSortByKeyTest, TestSortJsonByKey_UnicodeKeys)
{
std::string inputJson = R"({"α": 1, "β": 2, "γ": 3})";
auto result = m_jsonHandler.SortJsonByKey(inputJson, {}, {}, ' ', 2);
ASSERT_TRUE(result.success);
ASSERT_EQ(result.response, "{\n \"α\": 1,\n \"β\": 2,\n \"γ\": 3\n}");
}
TEST_F(JsonSortByKeyTest, TestSortJsonByKey_NullValue)
{
std::string inputJson = R"({"b": null, "a": "valueA"})";
auto result = m_jsonHandler.SortJsonByKey(inputJson, {}, {}, ' ', 2);
ASSERT_TRUE(result.success);
ASSERT_EQ(result.response, "{\n \"a\": \"valueA\",\n \"b\": null\n}");
}
TEST_F(JsonSortByKeyTest, TestSortJsonByKey_IgnoreComment)
{
ParseOptions options;
options.bIgnoreComment = true;
std::string inputJson = R"({"b": 2, /* comment */ "a": 1})";
auto result = JsonHandler {options}.SortJsonByKey(inputJson, {}, {}, ' ', 2);
ASSERT_TRUE(result.success);
ASSERT_EQ(result.response, "{\n \"a\": 1,\n \"b\": 2\n}");
}
TEST_F(JsonSortByKeyTest, TestSortJsonByKey_IgnoreTrailingComma)
{
ParseOptions options;
options.bIgnoreTrailingComma = false;
std::string inputJson = R"({"a": 1, "b": 2,})";
auto result = JsonHandler {options}.SortJsonByKey(inputJson, {}, {}, ' ', 2);
ASSERT_FALSE(result.success);
}
TEST_F(JsonSortByKeyTest, TestSortJsonByKey_NumberPrecision_Success)
{
// Verify numbers survive the sort-by-key roundtrip without gaining quotes.
// Note: SortJsonByKey re-parses via FormatJson (which uses kParseFullPrecisionFlag),
// so very high precision may be normalized (e.g. 0.00000000000001 -> 1e-14).
// But they must NOT become strings (e.g. "3.14" or "12345678901234567890").
std::string inputJson = R"({"z": "last", "pi": 3.141592653589793, "tiny": 1e-14, "tiny2": 0.00000000000001, "big": 12345678901234567890})";
auto result = m_jsonHandler.SortJsonByKey(inputJson, {}, {}, ' ', 2);
ASSERT_TRUE(result.success);
ASSERT_EQ(result.response, "{\n \"big\": 12345678901234567890,\n \"pi\": 3.141592653589793,\n \"tiny\": 1e-14,\n \"tiny2\": 1e-14,\n \"z\": \"last\"\n}");
}
} // namespace JsonSortByKey