Skip to content

Commit 28cf330

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

File tree

6 files changed

+355
-2
lines changed

6 files changed

+355
-2
lines changed

.github/workflows/ci_build.yml

Lines changed: 17 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,43 @@ 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 x86 unit tests
50+
- name: Run tests x86
51+
run: |
52+
cd src\Build\Bin\Release\Win32
53+
./UnitTest.exe
54+
55+
# Step 7: Run x64 unit tests
56+
- name: Run tests x86
57+
run: |
58+
cd src\Build\Bin\Release\x64
59+
./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|x64
47+
{5A15FD53-E7C1-4F10-85FA-B7C3BB5D4D64}.Debug|ARM64.Build.0 = Debug|x64
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|x64
53+
{5A15FD53-E7C1-4F10-85FA-B7C3BB5D4D64}.Release|ARM64.Build.0 = Release|x64
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

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

tests/UnitTest/UnitTest.vcxproj

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ItemGroup Label="ProjectConfigurations">
4+
<ProjectConfiguration Include="Debug|Win32">
5+
<Configuration>Debug</Configuration>
6+
<Platform>Win32</Platform>
7+
</ProjectConfiguration>
8+
<ProjectConfiguration Include="Release|Win32">
9+
<Configuration>Release</Configuration>
10+
<Platform>Win32</Platform>
11+
</ProjectConfiguration>
12+
<ProjectConfiguration Include="Debug|x64">
13+
<Configuration>Debug</Configuration>
14+
<Platform>x64</Platform>
15+
</ProjectConfiguration>
16+
<ProjectConfiguration Include="Release|x64">
17+
<Configuration>Release</Configuration>
18+
<Platform>x64</Platform>
19+
</ProjectConfiguration>
20+
</ItemGroup>
21+
<PropertyGroup Label="Globals">
22+
<ProjectGuid>{5a15fd53-e7c1-4f10-85fa-b7c3bb5d4d64}</ProjectGuid>
23+
<Keyword>Win32Proj</Keyword>
24+
<WindowsTargetPlatformVersion>10.0.22621.0</WindowsTargetPlatformVersion>
25+
<ConfigurationType>Application</ConfigurationType>
26+
<PlatformToolset>v143</PlatformToolset>
27+
<CharacterSet>Unicode</CharacterSet>
28+
</PropertyGroup>
29+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
30+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
31+
<ImportGroup Label="ExtensionSettings" />
32+
<ImportGroup Label="Shared" />
33+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
34+
<Import Project="..\..\src\NppJsonViewerCommon.props" />
35+
</ImportGroup>
36+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
37+
<Import Project="..\..\src\NppJsonViewerCommon.props" />
38+
</ImportGroup>
39+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
40+
<Import Project="..\..\src\NppJsonViewerCommon.props" />
41+
</ImportGroup>
42+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
43+
<Import Project="..\..\src\NppJsonViewerCommon.props" />
44+
</ImportGroup>
45+
<PropertyGroup Label="UserMacros" />
46+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
47+
<ExternalIncludePath>$(SolutionDir)..\external\googletest\googletest;$(SolutionDir)..\external\googletest\googletest\include;$(SolutionDir)..\external\googletest\googlemock;$(SolutionDir)..\external\googletest\googlemock\include;$(ExternalIncludePath)</ExternalIncludePath>
48+
</PropertyGroup>
49+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
50+
<ExternalIncludePath>$(SolutionDir)..\external\googletest\googletest;$(SolutionDir)..\external\googletest\googletest\include;$(SolutionDir)..\external\googletest\googlemock;$(SolutionDir)..\external\googletest\googlemock\include;$(ExternalIncludePath)</ExternalIncludePath>
51+
</PropertyGroup>
52+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
53+
<ExternalIncludePath>$(SolutionDir)..\external\googletest\googletest;$(SolutionDir)..\external\googletest\googletest\include;$(SolutionDir)..\external\googletest\googlemock;$(SolutionDir)..\external\googletest\googlemock\include;$(ExternalIncludePath)</ExternalIncludePath>
54+
</PropertyGroup>
55+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
56+
<ExternalIncludePath>$(SolutionDir)..\external\googletest\googletest;$(SolutionDir)..\external\googletest\googletest\include;$(SolutionDir)..\external\googletest\googlemock;$(SolutionDir)..\external\googletest\googlemock\include;$(ExternalIncludePath)</ExternalIncludePath>
57+
</PropertyGroup>
58+
<ItemDefinitionGroup />
59+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
60+
<ClCompile>
61+
<Optimization>Disabled</Optimization>
62+
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
63+
<BasicRuntimeChecks>EnableFastChecks</BasicRuntimeChecks>
64+
</ClCompile>
65+
<Link>
66+
<GenerateDebugInformation>true</GenerateDebugInformation>
67+
<SubSystem>Console</SubSystem>
68+
</Link>
69+
</ItemDefinitionGroup>
70+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
71+
<ClCompile>
72+
<Optimization>Disabled</Optimization>
73+
<PreprocessorDefinitions>X64;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
74+
<BasicRuntimeChecks>EnableFastChecks</BasicRuntimeChecks>
75+
</ClCompile>
76+
<Link>
77+
<GenerateDebugInformation>true</GenerateDebugInformation>
78+
<SubSystem>Console</SubSystem>
79+
</Link>
80+
</ItemDefinitionGroup>
81+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
82+
<ClCompile>
83+
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
84+
<DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
85+
</ClCompile>
86+
<Link>
87+
<GenerateDebugInformation>true</GenerateDebugInformation>
88+
<SubSystem>Console</SubSystem>
89+
</Link>
90+
</ItemDefinitionGroup>
91+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
92+
<ClCompile>
93+
<PreprocessorDefinitions>X64;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
94+
<DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
95+
</ClCompile>
96+
<Link>
97+
<GenerateDebugInformation>true</GenerateDebugInformation>
98+
<SubSystem>Console</SubSystem>
99+
</Link>
100+
</ItemDefinitionGroup>
101+
<ItemGroup>
102+
<ClCompile Include="..\..\external\googletest\googlemock\src\gmock-all.cc" />
103+
<ClCompile Include="..\..\external\googletest\googletest\src\gtest-all.cc" />
104+
<ClCompile Include="main.cpp" />
105+
<ClCompile Include="StringHelperTest.cpp" />
106+
</ItemGroup>
107+
<ItemGroup>
108+
<ProjectReference Include="..\..\src\UtilityLib\UtilityLib.vcxproj">
109+
<Project>{171cafc6-e679-4b81-bf5b-049ac0fab4f8}</Project>
110+
</ProjectReference>
111+
</ItemGroup>
112+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
113+
<ImportGroup Label="ExtensionTargets">
114+
</ImportGroup>
115+
</Project>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ItemGroup>
4+
<Filter Include="Source Files">
5+
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
6+
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
7+
</Filter>
8+
<Filter Include="Header Files">
9+
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
10+
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
11+
</Filter>
12+
<Filter Include="Resource Files">
13+
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
14+
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
15+
</Filter>
16+
<Filter Include="Source Files\googletest">
17+
<UniqueIdentifier>{6566c3ee-c762-478e-89d9-b411a9670e1a}</UniqueIdentifier>
18+
</Filter>
19+
</ItemGroup>
20+
<ItemGroup>
21+
<ClCompile Include="main.cpp">
22+
<Filter>Source Files</Filter>
23+
</ClCompile>
24+
<ClCompile Include="..\..\external\googletest\googletest\src\gtest-all.cc">
25+
<Filter>Source Files\googletest</Filter>
26+
</ClCompile>
27+
<ClCompile Include="..\..\external\googletest\googlemock\src\gmock-all.cc">
28+
<Filter>Source Files\googletest</Filter>
29+
</ClCompile>
30+
<ClCompile Include="StringHelperTest.cpp">
31+
<Filter>Source Files</Filter>
32+
</ClCompile>
33+
</ItemGroup>
34+
</Project>

0 commit comments

Comments
 (0)