Skip to content

Commit b1fa37e

Browse files
committed
Add unit test for jsonparsing
1 parent 719b7d6 commit b1fa37e

File tree

5 files changed

+109
-2
lines changed

5 files changed

+109
-2
lines changed

tests/UnitTest/JsonHandlerTest.cpp

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#include <gtest/gtest.h>
2+
3+
#include <vector>
4+
#include <string>
5+
#include <memory>
6+
7+
#include "JsonHandler.h"
8+
9+
namespace JsonParsing
10+
{
11+
class JsonHandlerTest : public ::testing::Test
12+
{
13+
protected:
14+
JsonHandler handler {{}};
15+
16+
protected:
17+
void SetUp() override {}
18+
void TearDown() override {}
19+
20+
void setParseOptions(const ParseOptions& opt)
21+
{
22+
handler = JsonHandler(opt);
23+
}
24+
};
25+
26+
TEST_F(JsonHandlerTest, TestGetCompressedJson_Success)
27+
{
28+
std::string inputJson = R"({"key": "value"})";
29+
auto result = handler.GetCompressedJson(inputJson);
30+
31+
ASSERT_TRUE(result.success);
32+
ASSERT_EQ(result.response, R"({"key":"value"})");
33+
}
34+
35+
TEST_F(JsonHandlerTest, TestGetCompressedJson_InvalidJson)
36+
{
37+
std::string inputJson = R"({"key": "value")"; // Missing closing brace
38+
auto result = handler.GetCompressedJson(inputJson);
39+
40+
ASSERT_FALSE(result.success);
41+
ASSERT_TRUE(result.response.empty());
42+
}
43+
44+
TEST_F(JsonHandlerTest, TestFormatJson_Success)
45+
{
46+
std::string inputJson = R"({"key": "value"})";
47+
auto result = handler.FormatJson(inputJson, {}, {}, ' ', 4);
48+
49+
ASSERT_TRUE(result.success);
50+
ASSERT_EQ(result.response, "{\n \"key\": \"value\"\n}");
51+
}
52+
53+
TEST_F(JsonHandlerTest, TestFormatJson_InvalidJson)
54+
{
55+
std::string inputJson = R"({"key": "value")"; // Invalid JSON
56+
auto result = handler.FormatJson(inputJson, {}, {}, ' ', 4);
57+
58+
ASSERT_FALSE(result.success);
59+
}
60+
61+
// Test ValidateJson
62+
TEST_F(JsonHandlerTest, TestValidateJson_Success)
63+
{
64+
std::string inputJson = R"({"key": "value"})";
65+
auto result = handler.ValidateJson(inputJson);
66+
67+
ASSERT_TRUE(result.success);
68+
}
69+
70+
TEST_F(JsonHandlerTest, TestValidateJson_InvalidJson)
71+
{
72+
std::string inputJson = R"({"key": "value")"; // Invalid JSON
73+
auto result = handler.ValidateJson(inputJson);
74+
75+
ASSERT_FALSE(result.success);
76+
}
77+
78+
// Test SortJsonByKey
79+
TEST_F(JsonHandlerTest, TestSortJsonByKey_Success)
80+
{
81+
std::string inputJson = R"({"b": "valueB", "a": "valueA"})";
82+
auto result = handler.SortJsonByKey(inputJson, {}, {}, ' ', 4);
83+
84+
ASSERT_TRUE(result.success);
85+
ASSERT_EQ(result.response, "{\n \"a\": \"valueA\",\n \"b\": \"valueB\"\n}");
86+
}
87+
88+
TEST_F(JsonHandlerTest, TestSortJsonByKey_InvalidJson)
89+
{
90+
std::string inputJson = R"({"b": "valueB", "a": "valueA")"; // Invalid JSON
91+
auto result = handler.SortJsonByKey(inputJson, {}, {}, ' ', 4);
92+
93+
ASSERT_FALSE(result.success);
94+
}
95+
} // namespace JsonParsing

tests/UnitTest/StringHelperTest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
#include "StringHelper.h"
77

8-
namespace Utility
8+
namespace UtilityTest
99
{
1010
class StringHelperTest : public ::testing::Test
1111
{

tests/UnitTest/UnitTest.vcxproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,9 @@
152152
<ItemGroup>
153153
<ClCompile Include="..\..\external\googletest\googlemock\src\gmock-all.cc" />
154154
<ClCompile Include="..\..\external\googletest\googletest\src\gtest-all.cc" />
155+
<ClCompile Include="..\..\src\NppJsonViewer\JsonHandler.cpp" />
155156
<ClCompile Include="..\..\src\NppJsonViewer\Profile.cpp" />
157+
<ClCompile Include="JsonHandlerTest.cpp" />
156158
<ClCompile Include="main.cpp" />
157159
<ClCompile Include="ProfileTest.cpp" />
158160
<ClCompile Include="StringHelperTest.cpp" />
@@ -164,6 +166,7 @@
164166
</ProjectReference>
165167
</ItemGroup>
166168
<ItemGroup>
169+
<ClInclude Include="..\..\src\NppJsonViewer\JsonHandler.h" />
167170
<ClInclude Include="..\..\src\NppJsonViewer\Profile.h" />
168171
</ItemGroup>
169172
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />

tests/UnitTest/UnitTest.vcxproj.filters

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,19 @@
4242
<ClCompile Include="StringHelperTest.cpp">
4343
<Filter>Source Files</Filter>
4444
</ClCompile>
45+
<ClCompile Include="JsonHandlerTest.cpp">
46+
<Filter>Source Files</Filter>
47+
</ClCompile>
48+
<ClCompile Include="..\..\src\NppJsonViewer\JsonHandler.cpp">
49+
<Filter>Source Files\NppJsonViewer</Filter>
50+
</ClCompile>
4551
</ItemGroup>
4652
<ItemGroup>
4753
<ClInclude Include="..\..\src\NppJsonViewer\Profile.h">
4854
<Filter>Source Files\NppJsonViewer</Filter>
4955
</ClInclude>
56+
<ClInclude Include="..\..\src\NppJsonViewer\JsonHandler.h">
57+
<Filter>Source Files\NppJsonViewer</Filter>
58+
</ClInclude>
5059
</ItemGroup>
5160
</Project>

tests/UnitTest/UtilityTest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
#include "Utility.h"
88

9-
namespace Utility
9+
namespace UtilityTest
1010
{
1111
// Test GetXFromLPARAM with sample LPARAM
1212
TEST(CUtilityTest, GetXFromLPARAM)

0 commit comments

Comments
 (0)