Skip to content

Commit 3a24be7

Browse files
committed
Create a test for PatchWhitespaceMode.
1 parent 16f17b0 commit 3a24be7

File tree

1 file changed

+66
-1
lines changed

1 file changed

+66
-1
lines changed

LibGit2Sharp.Tests/DiffTreeToTreeFixture.cs

+66-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System.Text;
55
using LibGit2Sharp.Tests.TestHelpers;
66
using Xunit;
7-
using Xunit.Extensions;
87

98
namespace LibGit2Sharp.Tests
109
{
@@ -1219,5 +1218,71 @@ public void UsingPatienceAlgorithmCompareOptionProducesPatienceDiff()
12191218
Assert.Equal(diffPatience, changes);
12201219
}
12211220
}
1221+
1222+
[Fact]
1223+
public void PatchWhitespaceModeIsObeyed()
1224+
{
1225+
string repoPath = InitNewRepository();
1226+
1227+
var compareOptions = Enum
1228+
.GetValues(typeof(PatchWhitespaceMode))
1229+
.Cast<PatchWhitespaceMode>()
1230+
.Select(whitespaceOption => new CompareOptions() { PatchWhitespaceMode = whitespaceOption });
1231+
1232+
using (var repo = new Repository(repoPath))
1233+
{
1234+
const string fileName = "file.txt";
1235+
var commits = new System.Collections.Generic.List<Commit>();
1236+
1237+
Touch(repo.Info.WorkingDirectory, fileName, "White space is not wastedspace.");
1238+
Commands.Stage(repo, fileName);
1239+
commits.Add(repo.Commit("Initial", Constants.Signature, Constants.Signature));
1240+
1241+
Touch(repo.Info.WorkingDirectory, fileName, "White space is not wastedspace. \t");
1242+
Commands.Stage(repo, fileName);
1243+
commits.Add(repo.Commit("Add trailing whitespace.", Constants.Signature, Constants.Signature));
1244+
1245+
Touch(repo.Info.WorkingDirectory, fileName, "White space\tis not wastedspace. ");
1246+
Commands.Stage(repo, fileName);
1247+
commits.Add(repo.Commit("Change whitespace.", Constants.Signature, Constants.Signature));
1248+
1249+
Touch(repo.Info.WorkingDirectory, fileName, " Whitespace is not wasted space.");
1250+
Commands.Stage(repo, fileName);
1251+
commits.Add(repo.Commit("Insert whitespace.", Constants.Signature, Constants.Signature));
1252+
1253+
Touch(repo.Info.WorkingDirectory, fileName, "Completely different content.");
1254+
Commands.Stage(repo, fileName);
1255+
commits.Add(repo.Commit("Completely different.", Constants.Signature, Constants.Signature));
1256+
1257+
var commitPairs = commits
1258+
.Select((oldCommit, index) => new { oldCommit, index })
1259+
.Zip(commits.Skip(1), (old, newCommit) => new { old.oldCommit, newCommit, old.index });
1260+
1261+
foreach (var commitPair in commitPairs)
1262+
foreach (var compareOption in compareOptions)
1263+
using (var patch = repo.Diff.Compare<Patch>(commitPair.oldCommit.Tree, commitPair.newCommit.Tree, compareOption))
1264+
{
1265+
int expectedDiffLines;
1266+
switch (compareOption.PatchWhitespaceMode)
1267+
{
1268+
case PatchWhitespaceMode.DontIgnoreWhitespace:
1269+
expectedDiffLines = 1;
1270+
break;
1271+
case PatchWhitespaceMode.IgnoreAllWhitespace:
1272+
expectedDiffLines = commitPair.index > 2 ? 1 : 0;
1273+
break;
1274+
case PatchWhitespaceMode.IgnoreWhitespaceChange:
1275+
expectedDiffLines = commitPair.index > 1 ? 1 : 0;
1276+
break;
1277+
case PatchWhitespaceMode.IgnoreWhitespaceEol:
1278+
expectedDiffLines = commitPair.index > 0 ? 1 : 0;
1279+
break;
1280+
default:
1281+
throw new Exception("Unexpected " + nameof(PatchWhitespaceMode));
1282+
}
1283+
Assert.Equal(expectedDiffLines, patch.LinesAdded);
1284+
}
1285+
}
1286+
}
12221287
}
12231288
}

0 commit comments

Comments
 (0)