From 3a24be7f240e725980fbf40240ef4acf2c1e9fbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20H=C3=BCbscher?= Date: Sat, 4 Apr 2020 21:22:21 +0200 Subject: [PATCH] Create a test for PatchWhitespaceMode. --- LibGit2Sharp.Tests/DiffTreeToTreeFixture.cs | 67 ++++++++++++++++++++- 1 file changed, 66 insertions(+), 1 deletion(-) diff --git a/LibGit2Sharp.Tests/DiffTreeToTreeFixture.cs b/LibGit2Sharp.Tests/DiffTreeToTreeFixture.cs index dba762bfe..a5bb8c1ad 100644 --- a/LibGit2Sharp.Tests/DiffTreeToTreeFixture.cs +++ b/LibGit2Sharp.Tests/DiffTreeToTreeFixture.cs @@ -4,7 +4,6 @@ using System.Text; using LibGit2Sharp.Tests.TestHelpers; using Xunit; -using Xunit.Extensions; namespace LibGit2Sharp.Tests { @@ -1219,5 +1218,71 @@ public void UsingPatienceAlgorithmCompareOptionProducesPatienceDiff() Assert.Equal(diffPatience, changes); } } + + [Fact] + public void PatchWhitespaceModeIsObeyed() + { + string repoPath = InitNewRepository(); + + var compareOptions = Enum + .GetValues(typeof(PatchWhitespaceMode)) + .Cast() + .Select(whitespaceOption => new CompareOptions() { PatchWhitespaceMode = whitespaceOption }); + + using (var repo = new Repository(repoPath)) + { + const string fileName = "file.txt"; + var commits = new System.Collections.Generic.List(); + + Touch(repo.Info.WorkingDirectory, fileName, "White space is not wastedspace."); + Commands.Stage(repo, fileName); + commits.Add(repo.Commit("Initial", Constants.Signature, Constants.Signature)); + + Touch(repo.Info.WorkingDirectory, fileName, "White space is not wastedspace. \t"); + Commands.Stage(repo, fileName); + commits.Add(repo.Commit("Add trailing whitespace.", Constants.Signature, Constants.Signature)); + + Touch(repo.Info.WorkingDirectory, fileName, "White space\tis not wastedspace. "); + Commands.Stage(repo, fileName); + commits.Add(repo.Commit("Change whitespace.", Constants.Signature, Constants.Signature)); + + Touch(repo.Info.WorkingDirectory, fileName, " Whitespace is not wasted space."); + Commands.Stage(repo, fileName); + commits.Add(repo.Commit("Insert whitespace.", Constants.Signature, Constants.Signature)); + + Touch(repo.Info.WorkingDirectory, fileName, "Completely different content."); + Commands.Stage(repo, fileName); + commits.Add(repo.Commit("Completely different.", Constants.Signature, Constants.Signature)); + + var commitPairs = commits + .Select((oldCommit, index) => new { oldCommit, index }) + .Zip(commits.Skip(1), (old, newCommit) => new { old.oldCommit, newCommit, old.index }); + + foreach (var commitPair in commitPairs) + foreach (var compareOption in compareOptions) + using (var patch = repo.Diff.Compare(commitPair.oldCommit.Tree, commitPair.newCommit.Tree, compareOption)) + { + int expectedDiffLines; + switch (compareOption.PatchWhitespaceMode) + { + case PatchWhitespaceMode.DontIgnoreWhitespace: + expectedDiffLines = 1; + break; + case PatchWhitespaceMode.IgnoreAllWhitespace: + expectedDiffLines = commitPair.index > 2 ? 1 : 0; + break; + case PatchWhitespaceMode.IgnoreWhitespaceChange: + expectedDiffLines = commitPair.index > 1 ? 1 : 0; + break; + case PatchWhitespaceMode.IgnoreWhitespaceEol: + expectedDiffLines = commitPair.index > 0 ? 1 : 0; + break; + default: + throw new Exception("Unexpected " + nameof(PatchWhitespaceMode)); + } + Assert.Equal(expectedDiffLines, patch.LinesAdded); + } + } + } } }