From e91f432ccac56c2f24ff9a569715c653706b1521 Mon Sep 17 00:00:00 2001 From: Takashi Sawanaka Date: Sun, 8 Oct 2023 19:54:40 +0900 Subject: [PATCH] DiffWrapper_test.cpp: Add RunFileDiff_LineFilters and RunFileDiff_SubstitutionFilters --- .../DiffWrapper/DiffWrapper_test.cpp | 132 ++++++++++++++++++ .../GoogleTest/UnitTests/UnitTests.vcxproj | 6 +- .../UnitTests/UnitTests.vcxproj.filters | 14 +- 3 files changed, 150 insertions(+), 2 deletions(-) diff --git a/Testing/GoogleTest/DiffWrapper/DiffWrapper_test.cpp b/Testing/GoogleTest/DiffWrapper/DiffWrapper_test.cpp index a2dd5b08869..ca3844e2496 100644 --- a/Testing/GoogleTest/DiffWrapper/DiffWrapper_test.cpp +++ b/Testing/GoogleTest/DiffWrapper/DiffWrapper_test.cpp @@ -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) { @@ -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]); + } + } +} diff --git a/Testing/GoogleTest/UnitTests/UnitTests.vcxproj b/Testing/GoogleTest/UnitTests/UnitTests.vcxproj index 474a354394f..1f6b9c8ade0 100644 --- a/Testing/GoogleTest/UnitTests/UnitTests.vcxproj +++ b/Testing/GoogleTest/UnitTests/UnitTests.vcxproj @@ -527,6 +527,7 @@ $(IntDir)$(TargetName)2.pch + Use pch.h @@ -551,6 +552,7 @@ + @@ -823,6 +825,7 @@ + @@ -844,6 +847,7 @@ + @@ -851,4 +855,4 @@ - + \ No newline at end of file diff --git a/Testing/GoogleTest/UnitTests/UnitTests.vcxproj.filters b/Testing/GoogleTest/UnitTests/UnitTests.vcxproj.filters index a0cbcdcb14e..140483ed3c0 100644 --- a/Testing/GoogleTest/UnitTests/UnitTests.vcxproj.filters +++ b/Testing/GoogleTest/UnitTests/UnitTests.vcxproj.filters @@ -291,6 +291,12 @@ Source Files + + Source Files + + + Source Files + @@ -440,5 +446,11 @@ Header Files + + Header Files + + + Header Files + - + \ No newline at end of file