|
| 1 | +#include <gtest/gtest.h> |
| 2 | + |
| 3 | +#include <string> |
| 4 | + |
| 5 | +#include "JsonHandler.h" |
| 6 | + |
| 7 | +// TODO: Adjust test case when numeric values are properly supported in SortJsonByKey |
| 8 | + |
| 9 | +namespace JsonSortByKey |
| 10 | +{ |
| 11 | + class JsonSortByKeyTest : public ::testing::Test |
| 12 | + { |
| 13 | + protected: |
| 14 | + JsonHandler m_jsonHandler {{}}; |
| 15 | + |
| 16 | + protected: |
| 17 | + void SetUp() override {} |
| 18 | + void TearDown() override {} |
| 19 | + }; |
| 20 | + |
| 21 | + |
| 22 | + // Test SortJsonByKey |
| 23 | + TEST_F(JsonSortByKeyTest, TestSortJsonByKey_Success) |
| 24 | + { |
| 25 | + std::string inputJson = R"({"b": "valueB", "a": "valueA"})"; |
| 26 | + auto result = m_jsonHandler.SortJsonByKey(inputJson, {}, {}, ' ', 4); |
| 27 | + |
| 28 | + ASSERT_TRUE(result.success); |
| 29 | + ASSERT_EQ(result.response, "{\n \"a\": \"valueA\",\n \"b\": \"valueB\"\n}"); |
| 30 | + } |
| 31 | + |
| 32 | + TEST_F(JsonSortByKeyTest, TestSortJsonByKey_InvalidJson) |
| 33 | + { |
| 34 | + std::string inputJson = R"({"b": "valueB", "a": "valueA")"; // Invalid JSON |
| 35 | + auto result = m_jsonHandler.SortJsonByKey(inputJson, {}, {}, ' ', 4); |
| 36 | + |
| 37 | + ASSERT_FALSE(result.success); |
| 38 | + } |
| 39 | + |
| 40 | + TEST_F(JsonSortByKeyTest, TestSortJsonByKey_NumberValue_Success) |
| 41 | + { |
| 42 | + std::string inputJson = R"({"b": "valueB", "a": 30})"; |
| 43 | + auto result = m_jsonHandler.SortJsonByKey(inputJson, {}, {}, ' ', 4); |
| 44 | + |
| 45 | + ASSERT_TRUE(result.success); |
| 46 | + ASSERT_EQ(result.response, "{\n \"a\": \"30\",\n \"b\": \"valueB\"\n}"); |
| 47 | + |
| 48 | + std::string inputJson2 = R"({ "name": "My name?", "age": 27, "is_student": false })"; |
| 49 | + auto result2 = m_jsonHandler.SortJsonByKey(inputJson2, {}, {}, ' ', 2); |
| 50 | + |
| 51 | + ASSERT_TRUE(result2.success); |
| 52 | + ASSERT_EQ(result2.response, "{\n \"age\": \"27\",\n \"is_student\": false,\n \"name\": \"My name?\"\n}"); |
| 53 | + } |
| 54 | + |
| 55 | + TEST_F(JsonSortByKeyTest, TestSortJsonByKey_NaN_Value_Success) |
| 56 | + { |
| 57 | + std::string inputJson = R"({"Nan": NaN, "Hello": "World"})"; |
| 58 | + auto result = m_jsonHandler.SortJsonByKey(inputJson, {}, {}, ' ', 4); |
| 59 | + |
| 60 | + ASSERT_TRUE(result.success); |
| 61 | + ASSERT_EQ(result.response, "{\n \"Hello\": \"World\",\n \"Nan\": \"NaN\"\n}"); |
| 62 | + } |
| 63 | + |
| 64 | + TEST_F(JsonSortByKeyTest, TestSortJsonByKey_ArrayValue_Success) |
| 65 | + { |
| 66 | + std::string inputJson = R"({"text": ["Hello", "World"], "Inf": [-Infinity, Infinity, -Infinity, Infinity]})"; |
| 67 | + auto result = m_jsonHandler.SortJsonByKey(inputJson, {}, LF::kFormatSingleLineArray, ' ', 2); |
| 68 | + |
| 69 | + ASSERT_TRUE(result.success); |
| 70 | + ASSERT_EQ(result.response, "{\n \"Inf\": [\"-Infinity\", \"Infinity\", \"-Infinity\", \"Infinity\"],\n \"text\": [\"Hello\", \"World\"]\n}"); |
| 71 | + } |
| 72 | + |
| 73 | + TEST_F(JsonSortByKeyTest, TestSortJsonByKey_EmptyObject) |
| 74 | + { |
| 75 | + std::string inputJson = R"({})"; |
| 76 | + auto result = m_jsonHandler.SortJsonByKey(inputJson, {}, {}, ' ', 2); |
| 77 | + |
| 78 | + ASSERT_TRUE(result.success); |
| 79 | + ASSERT_EQ(result.response, "{}"); |
| 80 | + } |
| 81 | + |
| 82 | + TEST_F(JsonSortByKeyTest, TestSortJsonByKey_NestedObject) |
| 83 | + { |
| 84 | + std::string inputJson = R"({"z": {"b": 2, "a": 1}, "a": {"c": 3}})"; |
| 85 | + auto result = m_jsonHandler.SortJsonByKey(inputJson, {}, {}, ' ', 2); |
| 86 | + |
| 87 | + ASSERT_TRUE(result.success); |
| 88 | + ASSERT_EQ(result.response, "{\n \"a\": {\n \"c\": \"3\"\n },\n \"z\": {\n \"a\": \"1\",\n \"b\": \"2\"\n }\n}"); |
| 89 | + } |
| 90 | + |
| 91 | + TEST_F(JsonSortByKeyTest, TestSortJsonByKey_SpecialCharacters) |
| 92 | + { |
| 93 | + std::string inputJson = R"({"!": 1, "#": 2, "A": 3})"; |
| 94 | + auto result = m_jsonHandler.SortJsonByKey(inputJson, {}, {}, ' ', 2); |
| 95 | + |
| 96 | + ASSERT_TRUE(result.success); |
| 97 | + ASSERT_EQ(result.response, "{\n \"!\": \"1\",\n \"#\": \"2\",\n \"A\": \"3\"\n}"); |
| 98 | + } |
| 99 | + |
| 100 | + TEST_F(JsonSortByKeyTest, TestSortJsonByKey_UnicodeKeys) |
| 101 | + { |
| 102 | + std::string inputJson = R"({"α": 1, "β": 2, "γ": 3})"; |
| 103 | + auto result = m_jsonHandler.SortJsonByKey(inputJson, {}, {}, ' ', 2); |
| 104 | + |
| 105 | + ASSERT_TRUE(result.success); |
| 106 | + ASSERT_EQ(result.response, "{\n \"α\": \"1\",\n \"β\": \"2\",\n \"γ\": \"3\"\n}"); |
| 107 | + } |
| 108 | + |
| 109 | + TEST_F(JsonSortByKeyTest, TestSortJsonByKey_NullValue) |
| 110 | + { |
| 111 | + std::string inputJson = R"({"b": null, "a": "valueA"})"; |
| 112 | + auto result = m_jsonHandler.SortJsonByKey(inputJson, {}, {}, ' ', 2); |
| 113 | + |
| 114 | + ASSERT_TRUE(result.success); |
| 115 | + ASSERT_EQ(result.response, "{\n \"a\": \"valueA\",\n \"b\": null\n}"); |
| 116 | + } |
| 117 | + |
| 118 | + TEST_F(JsonSortByKeyTest, TestSortJsonByKey_IgnoreComment) |
| 119 | + { |
| 120 | + ParseOptions options; |
| 121 | + options.bIgnoreComment = true; |
| 122 | + std::string inputJson = R"({"b": 2, /* comment */ "a": 1})"; |
| 123 | + auto result = JsonHandler {options}.SortJsonByKey(inputJson, {}, {}, ' ', 2); |
| 124 | + |
| 125 | + ASSERT_TRUE(result.success); |
| 126 | + ASSERT_EQ(result.response, "{\n \"a\": \"1\",\n \"b\": \"2\"\n}"); |
| 127 | + } |
| 128 | + |
| 129 | + TEST_F(JsonSortByKeyTest, TestSortJsonByKey_IgnoreTrailingComma) |
| 130 | + { |
| 131 | + ParseOptions options; |
| 132 | + options.bIgnoreTrailingComma = false; |
| 133 | + std::string inputJson = R"({"a": 1, "b": 2,})"; |
| 134 | + auto result = JsonHandler {options}.SortJsonByKey(inputJson, {}, {}, ' ', 2); |
| 135 | + |
| 136 | + ASSERT_FALSE(result.success); |
| 137 | + } |
| 138 | +} // namespace JsonSortByKey |
0 commit comments