Skip to content

Commit d7c06d8

Browse files
committed
Added unit test project
1 parent 2b61408 commit d7c06d8

File tree

8 files changed

+547
-3
lines changed

8 files changed

+547
-3
lines changed

.github/workflows/ci_build.yml

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: CI Build
1+
name: Build and test
22

33
on:
44
push:
@@ -17,28 +17,45 @@ jobs:
1717
build_platform: [Win32, x64, ARM64]
1818

1919
steps:
20+
# Step 1: Check out the code from the repo
2021
- name: Checkout repo
2122
uses: actions/checkout@v4
2223
with:
2324
submodules: recursive
2425

26+
# Step 2: Prepare for build
2527
- name: Pre Build
2628
uses: microsoft/setup-msbuild@v2
2729

30+
# Step 3: Build projects and unit test
2831
- name: Build
2932
working-directory: src
3033
run: msbuild NppJSONViewer.sln /m /p:configuration="${{ matrix.build_configuration }}" /p:platform="${{ matrix.build_platform }}" /p:PlatformToolset="v143"
3134

35+
# Step 4: Upload build binary artifacts
3236
- name: Archive binaries artifacts
3337
uses: actions/upload-artifact@v3
3438
with:
3539
name: ${{ matrix.build_platform}}_${{ matrix.build_configuration}}
3640
path: src\Build\Bin\${{ matrix.build_configuration}}\${{ matrix.build_platform}}\NPPJSONViewer.dll
3741

42+
# Step 5: Upload build pdb artifacts
3843
- name: Archive symbols artifacts
3944
uses: actions/upload-artifact@v3
4045
with:
4146
name: ${{ matrix.build_platform}}_${{ matrix.build_configuration}}_pdb
4247
path: src\Build\Bin\${{ matrix.build_configuration}}\${{ matrix.build_platform}}\NPPJSONViewer.pdb
4348

44-
49+
# Step 6: Run unit tests for x86 | Release
50+
- name: Run tests x86 | Release
51+
if: matrix.build_platform == 'Win32' && matrix.build_configuration == 'Release'
52+
run: |
53+
cd src\Build\Bin\Release\Win32
54+
./UnitTest.exe
55+
56+
# Step 7: Run unit tests for x64 | Release
57+
- name: Run tests x64 | Release
58+
if: matrix.build_platform == 'x64' && matrix.build_configuration == 'Release'
59+
run: |
60+
cd src\Build\Bin\Release\x64
61+
./UnitTest.exe

src/NPPJSONViewer.sln

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "NPPJSONViewer", "NppJsonVie
77
EndProject
88
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "UtilityLib", "UtilityLib\UtilityLib.vcxproj", "{171CAFC6-E679-4B81-BF5B-049AC0FAB4F8}"
99
EndProject
10+
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "UnitTest", "..\tests\UnitTest\UnitTest.vcxproj", "{5A15FD53-E7C1-4F10-85FA-B7C3BB5D4D64}"
11+
EndProject
1012
Global
1113
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1214
Debug|ARM64 = Debug|ARM64
@@ -41,6 +43,18 @@ Global
4143
{171CAFC6-E679-4B81-BF5B-049AC0FAB4F8}.Release|Win32.Build.0 = Release|Win32
4244
{171CAFC6-E679-4B81-BF5B-049AC0FAB4F8}.Release|x64.ActiveCfg = Release|x64
4345
{171CAFC6-E679-4B81-BF5B-049AC0FAB4F8}.Release|x64.Build.0 = Release|x64
46+
{5A15FD53-E7C1-4F10-85FA-B7C3BB5D4D64}.Debug|ARM64.ActiveCfg = Debug|ARM64
47+
{5A15FD53-E7C1-4F10-85FA-B7C3BB5D4D64}.Debug|ARM64.Build.0 = Debug|ARM64
48+
{5A15FD53-E7C1-4F10-85FA-B7C3BB5D4D64}.Debug|Win32.ActiveCfg = Debug|Win32
49+
{5A15FD53-E7C1-4F10-85FA-B7C3BB5D4D64}.Debug|Win32.Build.0 = Debug|Win32
50+
{5A15FD53-E7C1-4F10-85FA-B7C3BB5D4D64}.Debug|x64.ActiveCfg = Debug|x64
51+
{5A15FD53-E7C1-4F10-85FA-B7C3BB5D4D64}.Debug|x64.Build.0 = Debug|x64
52+
{5A15FD53-E7C1-4F10-85FA-B7C3BB5D4D64}.Release|ARM64.ActiveCfg = Release|ARM64
53+
{5A15FD53-E7C1-4F10-85FA-B7C3BB5D4D64}.Release|ARM64.Build.0 = Release|ARM64
54+
{5A15FD53-E7C1-4F10-85FA-B7C3BB5D4D64}.Release|Win32.ActiveCfg = Release|Win32
55+
{5A15FD53-E7C1-4F10-85FA-B7C3BB5D4D64}.Release|Win32.Build.0 = Release|Win32
56+
{5A15FD53-E7C1-4F10-85FA-B7C3BB5D4D64}.Release|x64.ActiveCfg = Release|x64
57+
{5A15FD53-E7C1-4F10-85FA-B7C3BB5D4D64}.Release|x64.Build.0 = Release|x64
4458
EndGlobalSection
4559
GlobalSection(SolutionProperties) = preSolution
4660
HideSolutionNode = FALSE

src/UtilityLib/Utility.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ auto CUtility::GetFileExtension(const std::wstring &fileName) -> std::wstring
185185

186186
auto CUtility::GetTempFilePath() -> std::wstring
187187
{
188-
TCHAR tmpDir[1024];
188+
TCHAR tmpDir[1024] {};
189189
GetTempPath(1024, tmpDir);
190190
return tmpDir;
191191
}

tests/UnitTest/StringHelperTest.cpp

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
#include <gtest/gtest.h>
2+
3+
#include <vector>
4+
#include <string>
5+
6+
#include "StringHelper.h"
7+
8+
namespace Utility
9+
{
10+
class StringHelperTest : public ::testing::Test
11+
{
12+
protected:
13+
void SetUp() override {}
14+
void TearDown() override {}
15+
};
16+
17+
// Test ReplaceAll with simple strings
18+
TEST_F(StringHelperTest, ReplaceAll_ShouldReplaceSubstring)
19+
{
20+
std::string result = StringHelper::ReplaceAll("hello world", "world", "C++");
21+
EXPECT_EQ(result, "hello C++");
22+
}
23+
24+
TEST_F(StringHelperTest, ReplaceAll_ShouldReplaceMultipleOccurrences)
25+
{
26+
std::string result = StringHelper::ReplaceAll("a quick brown fox jumps over the lazy dog", "o", "0");
27+
EXPECT_EQ(result, "a quick br0wn f0x jumps 0ver the lazy d0g");
28+
}
29+
30+
TEST_F(StringHelperTest, ReplaceAll_NoOccurrences)
31+
{
32+
std::string result = StringHelper::ReplaceAll("hello world", "foo", "bar");
33+
EXPECT_EQ(result, "hello world");
34+
}
35+
36+
// Test ReplaceAll for wide strings
37+
TEST_F(StringHelperTest, ReplaceAll_WideString_ShouldReplaceSubstring)
38+
{
39+
std::wstring result = StringHelper::ReplaceAll(L"hello world", L"world", L"C++");
40+
EXPECT_EQ(result, L"hello C++");
41+
}
42+
43+
TEST_F(StringHelperTest, ReplaceAll_WideString_NoOccurrences)
44+
{
45+
std::wstring result = StringHelper::ReplaceAll(L"hello world", L"foo", L"bar");
46+
EXPECT_EQ(result, L"hello world");
47+
}
48+
49+
// Test ToWstring with various encodings
50+
TEST_F(StringHelperTest, ToWstring_ShouldConvertString)
51+
{
52+
std::wstring result = StringHelper::ToWstring("hello", CP_UTF8);
53+
EXPECT_EQ(result, L"hello");
54+
}
55+
56+
TEST_F(StringHelperTest, ToWstring_ShouldHandleEmptyString)
57+
{
58+
std::wstring result = StringHelper::ToWstring("", CP_UTF8);
59+
EXPECT_EQ(result, L"");
60+
}
61+
62+
TEST_F(StringHelperTest, ToWstring_InvalidEncoding_ShouldReturnEmpty)
63+
{
64+
std::wstring result = StringHelper::ToWstring("invalid", 9999); // Invalid codepage
65+
EXPECT_EQ(result, L"");
66+
}
67+
68+
// Test ToString with various encodings
69+
TEST_F(StringHelperTest, ToString_ShouldConvertWstring)
70+
{
71+
std::string result = StringHelper::ToString(L"hello", CP_UTF8);
72+
EXPECT_EQ(result, "hello");
73+
}
74+
75+
TEST_F(StringHelperTest, ToString_ShouldHandleEmptyWstring)
76+
{
77+
std::string result = StringHelper::ToString(L"", CP_UTF8);
78+
EXPECT_EQ(result, "");
79+
}
80+
81+
TEST_F(StringHelperTest, ToString_InvalidEncoding_ShouldReturnEmpty)
82+
{
83+
std::string result = StringHelper::ToString(L"invalid", 9999); // Invalid codepage
84+
EXPECT_EQ(result, "");
85+
}
86+
87+
// Test Split for standard strings
88+
TEST_F(StringHelperTest, Split_ShouldSplitStringByDelimiter)
89+
{
90+
std::vector<std::string> result = StringHelper::Split("a,b,c", ",");
91+
std::vector<std::string> expected = {"a", "b", "c"};
92+
EXPECT_EQ(result.size(), expected.size());
93+
EXPECT_EQ(result, expected);
94+
}
95+
96+
TEST_F(StringHelperTest, Split_ShouldHandleEmptyString)
97+
{
98+
std::vector<std::string> result = StringHelper::Split("", ",");
99+
EXPECT_TRUE(result.empty());
100+
}
101+
102+
TEST_F(StringHelperTest, Split_ShouldHandleNoDelimiters)
103+
{
104+
std::vector<std::string> result = StringHelper::Split("abc", ",");
105+
std::vector<std::string> expected = {"abc"};
106+
EXPECT_EQ(result.size(), expected.size());
107+
EXPECT_EQ(result, expected);
108+
}
109+
110+
// Test Split for wide strings
111+
TEST_F(StringHelperTest, Split_WideString_ShouldSplitStringByDelimiter)
112+
{
113+
std::vector<std::wstring> result = StringHelper::Split(L"a,b,c", L",");
114+
std::vector<std::wstring> expected = {L"a", L"b", L"c"};
115+
EXPECT_EQ(result.size(), expected.size());
116+
EXPECT_EQ(result, expected);
117+
}
118+
119+
TEST_F(StringHelperTest, Split_WideString_ShouldHandleEmptyString)
120+
{
121+
std::vector<std::wstring> result = StringHelper::Split(L"", L",");
122+
EXPECT_TRUE(result.empty());
123+
}
124+
125+
TEST_F(StringHelperTest, Split_WideString_ShouldHandleNoDelimiters)
126+
{
127+
std::vector<std::wstring> result = StringHelper::Split(L"abc", L",");
128+
std::vector<std::wstring> expected = {L"abc"};
129+
EXPECT_EQ(result.size(), expected.size());
130+
EXPECT_EQ(result, expected);
131+
}
132+
133+
// Test Contains method with case sensitivity
134+
TEST_F(StringHelperTest, Contains_ShouldFindSubstring)
135+
{
136+
bool result = StringHelper::Contains("hello world", "world", false);
137+
EXPECT_TRUE(result);
138+
}
139+
140+
TEST_F(StringHelperTest, Contains_ShouldReturnFalseForNonExistingSubstring)
141+
{
142+
bool result = StringHelper::Contains("hello world", "foo", false);
143+
EXPECT_FALSE(result);
144+
}
145+
146+
TEST_F(StringHelperTest, Contains_IgnoreCase_ShouldFindSubstring)
147+
{
148+
bool result = StringHelper::Contains("Hello World", "world", true);
149+
EXPECT_TRUE(result);
150+
}
151+
152+
// Test ToLower
153+
TEST_F(StringHelperTest, ToLower_ShouldConvertStringToLowercase)
154+
{
155+
std::string input = "HeLLo WoRLD";
156+
StringHelper::ToLower(input);
157+
EXPECT_EQ(input, "hello world");
158+
}
159+
160+
TEST_F(StringHelperTest, ToLower_WideString_ShouldConvertToLowercase)
161+
{
162+
std::wstring input = L"HeLLo WoRLD";
163+
StringHelper::ToLower(input);
164+
EXPECT_EQ(input, L"hello world");
165+
}
166+
} // namespace Utility

0 commit comments

Comments
 (0)