Skip to content

Commit

Permalink
Add more unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
SinghRajenM committed Sep 9, 2024
1 parent dfaba04 commit 719b7d6
Show file tree
Hide file tree
Showing 5 changed files with 237 additions and 30 deletions.
42 changes: 21 additions & 21 deletions src/NppJsonViewer/Profile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,43 +74,43 @@ bool ProfileSetting::GetSettings(Setting &info) const
bool bRetVal = true;

int nVal = 0;
bRetVal &= ReadValue(STR_INI_FORMATTING_SEC, STR_INI_FORMATTING_EOL, nVal, static_cast<int>(info.lineEnding));
bRetVal = bRetVal && ReadValue(STR_INI_FORMATTING_SEC, STR_INI_FORMATTING_EOL, nVal, static_cast<int>(info.lineEnding));
if (bRetVal)
info.lineEnding = static_cast<LineEnding>(nVal);

bRetVal &= ReadValue(STR_INI_FORMATTING_SEC, STR_INI_FORMATTING_LINE, nVal, static_cast<int>(info.lineFormat));
bRetVal = bRetVal && ReadValue(STR_INI_FORMATTING_SEC, STR_INI_FORMATTING_LINE, nVal, static_cast<int>(info.lineFormat));
if (bRetVal)
info.lineFormat = static_cast<LineFormat>(nVal);

bRetVal &= ReadValue(STR_INI_FORMATTING_SEC, STR_INI_FORMATTING_INDENT, nVal, static_cast<int>(info.indent.style));
bRetVal = bRetVal && ReadValue(STR_INI_FORMATTING_SEC, STR_INI_FORMATTING_INDENT, nVal, static_cast<int>(info.indent.style));
if (bRetVal)
info.indent.style = static_cast<IndentStyle>(nVal);

bRetVal &= ReadValue(STR_INI_FORMATTING_SEC, STR_INI_FORMATTING_INDENTCOUNT, nVal, info.indent.len);
bRetVal = bRetVal && ReadValue(STR_INI_FORMATTING_SEC, STR_INI_FORMATTING_INDENTCOUNT, nVal, info.indent.len);
if (bRetVal)
info.indent.len = nVal;

bRetVal &= ReadValue(STR_INI_OTHER_SEC, STR_INI_OTHER_FOLLOW_TAB, nVal, info.bFollowCurrentTab);
bRetVal = bRetVal && ReadValue(STR_INI_OTHER_SEC, STR_INI_OTHER_FOLLOW_TAB, nVal, info.bFollowCurrentTab);
if (bRetVal)
info.bFollowCurrentTab = static_cast<bool>(nVal);

bRetVal &= ReadValue(STR_INI_OTHER_SEC, STR_INI_OTHER_AUTO_FORMAT, nVal, info.bAutoFormat);
bRetVal = bRetVal && ReadValue(STR_INI_OTHER_SEC, STR_INI_OTHER_AUTO_FORMAT, nVal, info.bAutoFormat);
if (bRetVal)
info.bAutoFormat = static_cast<bool>(nVal);

bRetVal &= ReadValue(STR_INI_OTHER_SEC, STR_INI_OTHER_USE_HIGHLIGHT, nVal, info.bUseJsonHighlight);
bRetVal = bRetVal && ReadValue(STR_INI_OTHER_SEC, STR_INI_OTHER_USE_HIGHLIGHT, nVal, info.bUseJsonHighlight);
if (bRetVal)
info.bUseJsonHighlight = static_cast<bool>(nVal);

bRetVal &= ReadValue(STR_INI_OTHER_SEC, STR_INI_OTHER_IGNORE_COMMENT, nVal, info.parseOptions.bIgnoreComment);
bRetVal = bRetVal && ReadValue(STR_INI_OTHER_SEC, STR_INI_OTHER_IGNORE_COMMENT, nVal, info.parseOptions.bIgnoreComment);
if (bRetVal)
info.parseOptions.bIgnoreComment = static_cast<bool>(nVal);

bRetVal &= ReadValue(STR_INI_OTHER_SEC, STR_INI_OTHER_IGNORE_COMMA, nVal, info.parseOptions.bIgnoreTrailingComma);
bRetVal = bRetVal && ReadValue(STR_INI_OTHER_SEC, STR_INI_OTHER_IGNORE_COMMA, nVal, info.parseOptions.bIgnoreTrailingComma);
if (bRetVal)
info.parseOptions.bIgnoreTrailingComma = static_cast<bool>(nVal);

bRetVal &= ReadValue(STR_INI_OTHER_SEC, STR_INI_OTHER_REPLACE_UNDEFINED, nVal, info.parseOptions.bReplaceUndefined);
bRetVal = bRetVal && ReadValue(STR_INI_OTHER_SEC, STR_INI_OTHER_REPLACE_UNDEFINED, nVal, info.parseOptions.bReplaceUndefined);
if (bRetVal)
info.parseOptions.bReplaceUndefined = static_cast<bool>(nVal);

Expand All @@ -121,17 +121,17 @@ bool ProfileSetting::SetSettings(const Setting &info) const
{
bool bRetVal = true;

bRetVal &= WriteValue(STR_INI_FORMATTING_SEC, STR_INI_FORMATTING_EOL, static_cast<int>(info.lineEnding));
bRetVal &= WriteValue(STR_INI_FORMATTING_SEC, STR_INI_FORMATTING_LINE, static_cast<int>(info.lineFormat));
bRetVal &= WriteValue(STR_INI_FORMATTING_SEC, STR_INI_FORMATTING_INDENT, static_cast<int>(info.indent.style));
bRetVal &= WriteValue(STR_INI_FORMATTING_SEC, STR_INI_FORMATTING_INDENTCOUNT, info.indent.len);

bRetVal &= WriteValue(STR_INI_OTHER_SEC, STR_INI_OTHER_FOLLOW_TAB, info.bFollowCurrentTab);
bRetVal &= WriteValue(STR_INI_OTHER_SEC, STR_INI_OTHER_AUTO_FORMAT, info.bAutoFormat);
bRetVal &= WriteValue(STR_INI_OTHER_SEC, STR_INI_OTHER_USE_HIGHLIGHT, info.bUseJsonHighlight);
bRetVal &= WriteValue(STR_INI_OTHER_SEC, STR_INI_OTHER_IGNORE_COMMENT, info.parseOptions.bIgnoreComment);
bRetVal &= WriteValue(STR_INI_OTHER_SEC, STR_INI_OTHER_IGNORE_COMMA, info.parseOptions.bIgnoreTrailingComma);
bRetVal &= WriteValue(STR_INI_OTHER_SEC, STR_INI_OTHER_REPLACE_UNDEFINED, info.parseOptions.bReplaceUndefined);
bRetVal = bRetVal && WriteValue(STR_INI_FORMATTING_SEC, STR_INI_FORMATTING_EOL, static_cast<int>(info.lineEnding));
bRetVal = bRetVal && WriteValue(STR_INI_FORMATTING_SEC, STR_INI_FORMATTING_LINE, static_cast<int>(info.lineFormat));
bRetVal = bRetVal && WriteValue(STR_INI_FORMATTING_SEC, STR_INI_FORMATTING_INDENT, static_cast<int>(info.indent.style));
bRetVal = bRetVal && WriteValue(STR_INI_FORMATTING_SEC, STR_INI_FORMATTING_INDENTCOUNT, info.indent.len);

bRetVal = bRetVal && WriteValue(STR_INI_OTHER_SEC, STR_INI_OTHER_FOLLOW_TAB, info.bFollowCurrentTab);
bRetVal = bRetVal && WriteValue(STR_INI_OTHER_SEC, STR_INI_OTHER_AUTO_FORMAT, info.bAutoFormat);
bRetVal = bRetVal && WriteValue(STR_INI_OTHER_SEC, STR_INI_OTHER_USE_HIGHLIGHT, info.bUseJsonHighlight);
bRetVal = bRetVal && WriteValue(STR_INI_OTHER_SEC, STR_INI_OTHER_IGNORE_COMMENT, info.parseOptions.bIgnoreComment);
bRetVal = bRetVal && WriteValue(STR_INI_OTHER_SEC, STR_INI_OTHER_IGNORE_COMMA, info.parseOptions.bIgnoreTrailingComma);
bRetVal = bRetVal && WriteValue(STR_INI_OTHER_SEC, STR_INI_OTHER_REPLACE_UNDEFINED, info.parseOptions.bReplaceUndefined);

return bRetVal;
}
1 change: 1 addition & 0 deletions src/NppJsonViewer/Profile.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

class Profile
{
protected:
std::wstring m_ProfileFilePath;

public:
Expand Down
181 changes: 181 additions & 0 deletions tests/UnitTest/ProfileTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
#include <gtest/gtest.h>

#include <memory>
#include <filesystem>

#include "Profile.h"

namespace ProfileSettingTests
{
class ProfileSettingEx : public ProfileSetting
{
public:
ProfileSettingEx(const std::wstring &path)
: ProfileSetting(path)
{
// any left over from previous run
RemoveProfileFile();
}

~ProfileSettingEx()
{
RemoveProfileFile();
}

bool ReadValue(const std::wstring &section, const std::wstring &key, int &retVal, int defaultVal = 0) const
{
return Profile::ReadValue(section, key, retVal, defaultVal);
}

bool ReadValue(const std::wstring &section, const std::wstring &key, std::wstring &retVal, const std::wstring &defaultVal = {}) const
{
return Profile::ReadValue(section, key, retVal, defaultVal);
}

bool WriteValue(const std::wstring &section, const std::wstring &key, int value) const
{
return Profile::WriteValue(section, key, value);
}

bool WriteValue(const std::wstring &section, const std::wstring &key, const std::wstring &value) const
{
return Profile::WriteValue(section, key, value);
}

private:
void RemoveProfileFile()
{
std::error_code ec {};
std::filesystem::remove(m_ProfileFilePath, ec);
}
};

class ProfileTest : public ::testing::Test
{
protected:
std::unique_ptr<ProfileSettingEx> m_pProfile;

void SetUp() override
{
std::wstring exePath;
wchar_t path[MAX_PATH];
DWORD size = GetModuleFileName(nullptr, path, MAX_PATH);
if (size)
{
exePath = path;
size_t pos = exePath.find_last_of(L"\\/");
if (pos != std::wstring::npos)
{
// Remove the executable name, leave only the directory
exePath = exePath.substr(0, pos + 1);
}
}
exePath += L"test_profile.ini";

m_pProfile = std::make_unique<ProfileSettingEx>(exePath);
}

void TearDown() override {}
};

TEST_F(ProfileTest, ReadValueInt_Default)
{
int result = 0;
EXPECT_TRUE(m_pProfile->ReadValue(L"Settings", L"SomeIntegerKey", result, 42));
EXPECT_EQ(result, 42);
}

TEST_F(ProfileTest, ReadValueInt_Positive)
{
int result = 0;
EXPECT_TRUE(m_pProfile->WriteValue(L"Settings", L"SomeIntegerKey", 100));
EXPECT_TRUE(m_pProfile->ReadValue(L"Settings", L"SomeIntegerKey", result));
EXPECT_EQ(result, 100);
}

TEST_F(ProfileTest, ReadValueInt_Missing)
{
int result = 0;
EXPECT_TRUE(m_pProfile->ReadValue(L"Settings", L"SomeIntegerKey", result));
EXPECT_EQ(result, 0);
}

TEST_F(ProfileTest, ReadValueString_Default)
{
std::wstring result;
EXPECT_TRUE(m_pProfile->ReadValue(L"Settings", L"SomeStringKey", result, L"default"));
EXPECT_EQ(result, L"default");
}

TEST_F(ProfileTest, ReadValueString_Positive)
{
std::wstring result;
EXPECT_TRUE(m_pProfile->WriteValue(L"Settings", L"SomeStringKey", L"It will be written there."));
EXPECT_TRUE(m_pProfile->ReadValue(L"Settings", L"SomeStringKey", result));
EXPECT_EQ(result, L"It will be written there.");
}

TEST_F(ProfileTest, ReadValueString_Missing)
{
std::wstring result;
EXPECT_TRUE(m_pProfile->ReadValue(L"Settings", L"SomeStringKey", result));
EXPECT_EQ(result, L"");
}

TEST_F(ProfileTest, GetSettings_Defualt)
{
Setting setting {};
EXPECT_TRUE(m_pProfile->GetSettings(setting));

EXPECT_EQ(setting.lineEnding, LineEnding::AUTO);

EXPECT_EQ(setting.lineFormat, LineFormat::DEFAULT);

EXPECT_EQ(setting.indent.len, 4u);
EXPECT_EQ(setting.indent.style, IndentStyle::AUTO);

EXPECT_EQ(setting.bFollowCurrentTab, false);
EXPECT_EQ(setting.bAutoFormat, false);
EXPECT_EQ(setting.bUseJsonHighlight, true);

EXPECT_EQ(setting.parseOptions.bIgnoreComment, true);
EXPECT_EQ(setting.parseOptions.bIgnoreTrailingComma, true);
EXPECT_EQ(setting.parseOptions.bReplaceUndefined, false);
}

TEST_F(ProfileTest, SetSettings_Positive)
{
Setting expected, actual;
expected.lineEnding = LineEnding::WINDOWS;
expected.lineFormat, LineFormat::SINGLELINE;
expected.indent.len = 2u;
expected.indent.style = IndentStyle::TAB;

expected.bAutoFormat = true;
expected.bFollowCurrentTab = true;
expected.bAutoFormat = true;
expected.bUseJsonHighlight = false;

expected.parseOptions.bIgnoreComment = false;
expected.parseOptions.bIgnoreTrailingComma = false;
expected.parseOptions.bReplaceUndefined = true;

EXPECT_TRUE(m_pProfile->SetSettings(expected));
EXPECT_TRUE(m_pProfile->GetSettings(actual));

EXPECT_EQ(actual.lineEnding, expected.lineEnding);

EXPECT_EQ(actual.lineFormat, expected.lineFormat);

EXPECT_EQ(actual.indent.len, expected.indent.len);
EXPECT_EQ(actual.indent.style, expected.indent.style);

EXPECT_EQ(actual.bFollowCurrentTab, expected.bFollowCurrentTab);
EXPECT_EQ(actual.bAutoFormat, expected.bAutoFormat);
EXPECT_EQ(actual.bUseJsonHighlight, expected.bUseJsonHighlight);

EXPECT_EQ(actual.parseOptions.bIgnoreComment, expected.parseOptions.bIgnoreComment);
EXPECT_EQ(actual.parseOptions.bIgnoreTrailingComma, expected.parseOptions.bIgnoreTrailingComma);
EXPECT_EQ(actual.parseOptions.bReplaceUndefined, expected.parseOptions.bReplaceUndefined);
}
} // namespace ProfileSettingTests
11 changes: 11 additions & 0 deletions tests/UnitTest/UnitTest.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -60,22 +60,28 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ExternalIncludePath>$(SolutionDir)..\external\googletest\googletest;$(SolutionDir)..\external\googletest\googletest\include;$(SolutionDir)..\external\googletest\googlemock;$(SolutionDir)..\external\googletest\googlemock\include;$(ExternalIncludePath)</ExternalIncludePath>
<LinkIncremental>true</LinkIncremental>
<IncludePath>$(SolutionDir)NppJsonViewer;$(IncludePath)</IncludePath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ExternalIncludePath>$(SolutionDir)..\external\googletest\googletest;$(SolutionDir)..\external\googletest\googletest\include;$(SolutionDir)..\external\googletest\googlemock;$(SolutionDir)..\external\googletest\googlemock\include;$(ExternalIncludePath)</ExternalIncludePath>
<IncludePath>$(SolutionDir)NppJsonViewer;$(IncludePath)</IncludePath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ExternalIncludePath>$(SolutionDir)..\external\googletest\googletest;$(SolutionDir)..\external\googletest\googletest\include;$(SolutionDir)..\external\googletest\googlemock;$(SolutionDir)..\external\googletest\googlemock\include;$(ExternalIncludePath)</ExternalIncludePath>
<LinkIncremental>true</LinkIncremental>
<IncludePath>$(SolutionDir)NppJsonViewer;$(IncludePath)</IncludePath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|ARM64'">
<ExternalIncludePath>$(SolutionDir)..\external\googletest\googletest;$(SolutionDir)..\external\googletest\googletest\include;$(SolutionDir)..\external\googletest\googlemock;$(SolutionDir)..\external\googletest\googlemock\include;$(ExternalIncludePath)</ExternalIncludePath>
<IncludePath>$(SolutionDir)NppJsonViewer;$(IncludePath)</IncludePath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ExternalIncludePath>$(SolutionDir)..\external\googletest\googletest;$(SolutionDir)..\external\googletest\googletest\include;$(SolutionDir)..\external\googletest\googlemock;$(SolutionDir)..\external\googletest\googlemock\include;$(ExternalIncludePath)</ExternalIncludePath>
<IncludePath>$(SolutionDir)NppJsonViewer;$(IncludePath)</IncludePath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|ARM64'">
<ExternalIncludePath>$(SolutionDir)..\external\googletest\googletest;$(SolutionDir)..\external\googletest\googletest\include;$(SolutionDir)..\external\googletest\googlemock;$(SolutionDir)..\external\googletest\googlemock\include;$(ExternalIncludePath)</ExternalIncludePath>
<IncludePath>$(SolutionDir)NppJsonViewer;$(IncludePath)</IncludePath>
</PropertyGroup>
<ItemDefinitionGroup />
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
Expand Down Expand Up @@ -146,7 +152,9 @@
<ItemGroup>
<ClCompile Include="..\..\external\googletest\googlemock\src\gmock-all.cc" />
<ClCompile Include="..\..\external\googletest\googletest\src\gtest-all.cc" />
<ClCompile Include="..\..\src\NppJsonViewer\Profile.cpp" />
<ClCompile Include="main.cpp" />
<ClCompile Include="ProfileTest.cpp" />
<ClCompile Include="StringHelperTest.cpp" />
<ClCompile Include="UtilityTest.cpp" />
</ItemGroup>
Expand All @@ -155,6 +163,9 @@
<Project>{171cafc6-e679-4b81-bf5b-049ac0fab4f8}</Project>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\src\NppJsonViewer\Profile.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
Expand Down
32 changes: 23 additions & 9 deletions tests/UnitTest/UnitTest.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,50 @@
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Source Files">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<UniqueIdentifier>{85c7eb1f-2b68-43cb-8405-a71e55e79fd5}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Header Files">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<UniqueIdentifier>{80e1a248-3f71-4247-836e-d9c0755d3a45}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
</Filter>
<Filter Include="Resource Files">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<UniqueIdentifier>{e63ed3dd-a0ad-4b70-b440-e132f2aec749}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
<Filter Include="Source Files\googletest">
<UniqueIdentifier>{6566c3ee-c762-478e-89d9-b411a9670e1a}</UniqueIdentifier>
<UniqueIdentifier>{43a04267-7051-4c70-836c-2691b50401ed}</UniqueIdentifier>
</Filter>
<Filter Include="Source Files\NppJsonViewer">
<UniqueIdentifier>{be698e04-b1a9-4f55-9035-986c6e58c21a}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="main.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\..\external\googletest\googletest\src\gtest-all.cc">
<Filter>Source Files\googletest</Filter>
</ClCompile>
<ClCompile Include="..\..\external\googletest\googlemock\src\gmock-all.cc">
<Filter>Source Files\googletest</Filter>
</ClCompile>
<ClCompile Include="StringHelperTest.cpp">
<Filter>Source Files</Filter>
<ClCompile Include="..\..\src\NppJsonViewer\Profile.cpp">
<Filter>Source Files\NppJsonViewer</Filter>
</ClCompile>
<ClCompile Include="UtilityTest.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="main.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="ProfileTest.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="StringHelperTest.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\src\NppJsonViewer\Profile.h">
<Filter>Source Files\NppJsonViewer</Filter>
</ClInclude>
</ItemGroup>
</Project>

0 comments on commit 719b7d6

Please sign in to comment.