Skip to content

Commit

Permalink
DiffWrapper_test.cpp: Add RunFileDiff_LineFilters and RunFileDiff_Sub…
Browse files Browse the repository at this point in the history
…stitutionFilters
  • Loading branch information
sdottaka committed Oct 8, 2023
1 parent 0141d65 commit e91f432
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 2 deletions.
132 changes: 132 additions & 0 deletions Testing/GoogleTest/DiffWrapper/DiffWrapper_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include "paths.h"
#include "TempFile.h"
#include "UniFile.h"
#include "LineFiltersList.h"
#include "SubstitutionFiltersList.h"

const TempFile WriteToTempFile(const String& text)
{
Expand Down Expand Up @@ -165,3 +167,133 @@ TEST(DiffWrapper, RunFileDiff_IgnoreComments)
}
}
}

TEST(DiffWrapper, RunFileDiff_LineFilters)
{
CDiffWrapper dw;
DIFFOPTIONS options{};
DIFFRANGE dr;

for (auto algo : { DIFF_ALGORITHM_DEFAULT, DIFF_ALGORITHM_MINIMAL, DIFF_ALGORITHM_PATIENCE, DIFF_ALGORITHM_HISTOGRAM })
{
options.nDiffAlgorithm = algo;
LineFiltersList lineFilterList;
lineFilterList.AddFilter(_T("\\d{4}-\\d{2}-\\d{2}"), true);

{
DiffList diffList;
TempFile left = WriteToTempFile(_T("a\n# 2023-10-09\nc"));
TempFile right = WriteToTempFile(_T("a\n# 2023-10-08\nc"));
dw.SetFilterList(lineFilterList.MakeFilterList());
dw.SetCreateDiffList(&diffList);
dw.SetPaths({ left.GetPath(), right.GetPath() }, false);
dw.SetOptions(&options);
dw.RunFileDiff();
EXPECT_EQ(1, diffList.GetSize());
diffList.GetDiff(0, dr);
EXPECT_EQ(OP_TRIVIAL, dr.op);
EXPECT_EQ(1, dr.begin[0]);
EXPECT_EQ(1, dr.begin[1]);
EXPECT_EQ(1, dr.end[0]);
EXPECT_EQ(1, dr.end[1]);
}

{
DiffList diffList;
TempFile left = WriteToTempFile(_T("a\n# 2023-10-09\n# 2023-10-09\nc"));
TempFile right = WriteToTempFile(_T("a\n# 2023-10-08\nc"));
dw.SetFilterList(lineFilterList.MakeFilterList());
dw.SetCreateDiffList(&diffList);
dw.SetPaths({ left.GetPath(), right.GetPath() }, false);
dw.SetOptions(&options);
dw.RunFileDiff();
EXPECT_EQ(1, diffList.GetSize());
diffList.GetDiff(0, dr);
EXPECT_EQ(OP_TRIVIAL, dr.op);
EXPECT_EQ(1, dr.begin[0]);
EXPECT_EQ(1, dr.begin[1]);
EXPECT_EQ(2, dr.end[0]);
EXPECT_EQ(1, dr.end[1]);
}

{
DiffList diffList;
TempFile left = WriteToTempFile(_T("a\n# 2023-10-09\nb1\nc"));
TempFile right = WriteToTempFile(_T("a\n# 2023-10-08\nb2\nc"));
dw.SetFilterList(lineFilterList.MakeFilterList());
dw.SetCreateDiffList(&diffList);
dw.SetPaths({ left.GetPath(), right.GetPath() }, false);
dw.SetOptions(&options);
dw.RunFileDiff();
EXPECT_EQ(2, diffList.GetSize());
diffList.GetDiff(0, dr);
EXPECT_EQ(OP_TRIVIAL, dr.op);
EXPECT_EQ(1, dr.begin[0]);
EXPECT_EQ(1, dr.begin[1]);
EXPECT_EQ(1, dr.end[0]);
EXPECT_EQ(1, dr.end[1]);
diffList.GetDiff(1, dr);
EXPECT_EQ(OP_DIFF, dr.op);
EXPECT_EQ(2, dr.begin[0]);
EXPECT_EQ(2, dr.begin[1]);
EXPECT_EQ(2, dr.end[0]);
EXPECT_EQ(2, dr.end[1]);
}
}
}

TEST(DiffWrapper, RunFileDiff_SubstitutionFilters)
{
CDiffWrapper dw;
DIFFOPTIONS options{};
DIFFRANGE dr;

for (auto algo : { DIFF_ALGORITHM_DEFAULT, DIFF_ALGORITHM_MINIMAL, DIFF_ALGORITHM_PATIENCE, DIFF_ALGORITHM_HISTOGRAM })
{
options.nDiffAlgorithm = algo;
SubstitutionFiltersList substitutionFilterList;
substitutionFilterList.Add(_T("\\d{4}-\\d{2}-\\d{2}"), _T("XXXX-XX-XX"), true, false, false, true);

{
DiffList diffList;
TempFile left = WriteToTempFile(_T("a\n# 2023-10-09\nc"));
TempFile right = WriteToTempFile(_T("a\n# 2023-10-08\nc"));
dw.SetSubstitutionList(substitutionFilterList.MakeSubstitutionList());
dw.SetCreateDiffList(&diffList);
dw.SetPaths({ left.GetPath(), right.GetPath() }, false);
dw.SetOptions(&options);
dw.RunFileDiff();
EXPECT_EQ(1, diffList.GetSize());
diffList.GetDiff(0, dr);
EXPECT_EQ(OP_TRIVIAL, dr.op);
EXPECT_EQ(1, dr.begin[0]);
EXPECT_EQ(1, dr.begin[1]);
EXPECT_EQ(1, dr.end[0]);
EXPECT_EQ(1, dr.end[1]);
}

{
DiffList diffList;
TempFile left = WriteToTempFile(_T("a\n# 2023-10-09\nb1\nc"));
TempFile right = WriteToTempFile(_T("a\n# 2023-10-08\nb2\nc"));
dw.SetSubstitutionList(substitutionFilterList.MakeSubstitutionList());
dw.SetCreateDiffList(&diffList);
dw.SetPaths({ left.GetPath(), right.GetPath() }, false);
dw.SetOptions(&options);
dw.RunFileDiff();
EXPECT_EQ(2, diffList.GetSize());
diffList.GetDiff(0, dr);
EXPECT_EQ(OP_TRIVIAL, dr.op);
EXPECT_EQ(1, dr.begin[0]);
EXPECT_EQ(1, dr.begin[1]);
EXPECT_EQ(1, dr.end[0]);
EXPECT_EQ(1, dr.end[1]);
diffList.GetDiff(1, dr);
EXPECT_EQ(OP_DIFF, dr.op);
EXPECT_EQ(2, dr.begin[0]);
EXPECT_EQ(2, dr.begin[1]);
EXPECT_EQ(2, dr.end[0]);
EXPECT_EQ(2, dr.end[1]);
}
}
}
6 changes: 5 additions & 1 deletion Testing/GoogleTest/UnitTests/UnitTests.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,7 @@
<PrecompiledHeaderOutputFile>$(IntDir)$(TargetName)2.pch</PrecompiledHeaderOutputFile>
</ClCompile>
<ClCompile Include="..\..\..\Src\Common\lwdisp.c" />
<ClCompile Include="..\..\..\Src\LineFiltersList.cpp" />
<ClCompile Include="..\..\..\Src\markdown.cpp">
<PrecompiledHeader>Use</PrecompiledHeader>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
Expand All @@ -551,6 +552,7 @@
</ClCompile>
<ClCompile Include="..\..\..\Src\HashCalc.cpp" />
<ClCompile Include="..\..\..\Src\PropertySystem.cpp" />
<ClCompile Include="..\..\..\Src\SubstitutionFiltersList.cpp" />
<ClCompile Include="..\..\..\Src\SubstitutionList.cpp" />
<ClCompile Include="..\..\..\Src\TempFile.cpp" />
<ClCompile Include="..\..\..\Src\xdiff_gnudiff_compat.cpp" />
Expand Down Expand Up @@ -823,6 +825,7 @@
<ClInclude Include="..\..\..\Src\FilterList.h" />
<ClInclude Include="..\..\..\Src\Common\LogFile.h" />
<ClInclude Include="..\..\..\Src\Common\lwdisp.h" />
<ClInclude Include="..\..\..\Src\LineFiltersList.h" />
<ClInclude Include="..\..\..\Src\markdown.h" />
<ClInclude Include="..\..\..\Src\MergeCmdLineInfo.h" />
<ClInclude Include="..\..\..\Src\Common\multiformatText.h" />
Expand All @@ -844,11 +847,12 @@
<ClInclude Include="..\..\..\Src\Common\unicoder.h" />
<ClInclude Include="..\..\..\Src\Common\UnicodeString.h" />
<ClInclude Include="..\..\..\Src\Common\varprop.h" />
<ClInclude Include="..\..\..\Src\SubstitutionFiltersList.h" />
<ClInclude Include="..\..\..\Src\SubstitutionList.h" />
<ClInclude Include="..\..\..\Src\TempFile.h" />
<ClInclude Include="..\..\..\Src\xdiff_gnudiff_compat.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
</Project>
14 changes: 13 additions & 1 deletion Testing/GoogleTest/UnitTests/UnitTests.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,12 @@
<ClCompile Include="..\..\..\Src\TempFile.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\..\..\Src\LineFiltersList.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\..\..\Src\SubstitutionFiltersList.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\..\Src\charsets.h">
Expand Down Expand Up @@ -440,5 +446,11 @@
<ClInclude Include="..\..\..\Src\TempFile.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\..\..\Src\LineFiltersList.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\..\..\Src\SubstitutionFiltersList.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>
</Project>

0 comments on commit e91f432

Please sign in to comment.