|
1 | 1 | using System.IO;
|
| 2 | +using System.Linq; |
2 | 3 | using System.Text;
|
3 | 4 | using LibGit2Sharp.Tests.TestHelpers;
|
4 | 5 | using Xunit;
|
@@ -126,5 +127,85 @@ public void ComparingTwoNullBlobsReturnsAnEmptyContentChanges()
|
126 | 127 | Assert.Equal(0, changes.LinesDeleted);
|
127 | 128 | }
|
128 | 129 | }
|
| 130 | + |
| 131 | + [Fact] |
| 132 | + public void ComparingBlobsWithNoSpacesAndIndentHeuristicOptionMakesADifference() |
| 133 | + { |
| 134 | + var path = SandboxStandardTestRepoGitDir(); |
| 135 | + using (var repo = new Repository(path)) |
| 136 | + { |
| 137 | + // Based on test diff indent heuristic from: |
| 138 | + // https://github.com/git/git/blob/433860f3d0beb0c6f205290bd16cda413148f098/t/t4061-diff-indent.sh#L17 |
| 139 | + var oldContent = |
| 140 | +@" 1 |
| 141 | + 2 |
| 142 | + a |
| 143 | +
|
| 144 | + b |
| 145 | + 3 |
| 146 | + 4"; |
| 147 | + var newContent = |
| 148 | +@" 1 |
| 149 | + 2 |
| 150 | + a |
| 151 | +
|
| 152 | + b |
| 153 | + a |
| 154 | +
|
| 155 | + b |
| 156 | + 3 |
| 157 | + 4"; |
| 158 | + var oldBlob = repo.ObjectDatabase.CreateBlob(new MemoryStream(Encoding.UTF8.GetBytes(oldContent))); |
| 159 | + var newBlob = repo.ObjectDatabase.CreateBlob(new MemoryStream(Encoding.UTF8.GetBytes(newContent))); |
| 160 | + var noIndentHeuristicOption = new CompareOptions { IndentHeuristic = false }; |
| 161 | + var indentHeuristicOption = new CompareOptions { IndentHeuristic = true }; |
| 162 | + |
| 163 | + ContentChanges changes0 = repo.Diff.Compare(oldBlob, newBlob, noIndentHeuristicOption); |
| 164 | + ContentChanges changes1 = repo.Diff.Compare(oldBlob, newBlob, indentHeuristicOption); |
| 165 | + |
| 166 | + Assert.NotEqual(changes0.Patch, changes1.Patch); |
| 167 | + Assert.Equal(CanonicalChangedLines(changes0), CanonicalChangedLines(changes1)); |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + [Fact] |
| 172 | + public void ComparingBlobsWithNoSpacesIndentHeuristicOptionMakesNoDifference() |
| 173 | + { |
| 174 | + var path = SandboxStandardTestRepoGitDir(); |
| 175 | + using (var repo = new Repository(path)) |
| 176 | + { |
| 177 | + var oldContent = |
| 178 | +@" 1 |
| 179 | + 2 |
| 180 | + a |
| 181 | + b |
| 182 | + 3 |
| 183 | + 4"; |
| 184 | + var newContent = |
| 185 | +@" 1 |
| 186 | + 2 |
| 187 | + a |
| 188 | + b |
| 189 | + a |
| 190 | + b |
| 191 | + 3 |
| 192 | + 4"; |
| 193 | + var oldBlob = repo.ObjectDatabase.CreateBlob(new MemoryStream(Encoding.UTF8.GetBytes(oldContent))); |
| 194 | + var newBlob = repo.ObjectDatabase.CreateBlob(new MemoryStream(Encoding.UTF8.GetBytes(newContent))); |
| 195 | + var noIndentHeuristicOption = new CompareOptions { IndentHeuristic = false }; |
| 196 | + var indentHeuristicOption = new CompareOptions { IndentHeuristic = true }; |
| 197 | + |
| 198 | + ContentChanges changes0 = repo.Diff.Compare(oldBlob, newBlob, noIndentHeuristicOption); |
| 199 | + ContentChanges changes1 = repo.Diff.Compare(oldBlob, newBlob, indentHeuristicOption); |
| 200 | + |
| 201 | + Assert.Equal(changes0.Patch, changes1.Patch); |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + static string CanonicalChangedLines(ContentChanges changes) |
| 206 | + { |
| 207 | + // Create an ordered representation of lines that have been added or removed |
| 208 | + return string.Join("\n", changes.Patch.Split('\n').Where(l => l.StartsWith("+") || l.StartsWith("-")).OrderBy(l => l)); |
| 209 | + } |
129 | 210 | }
|
130 | 211 | }
|
0 commit comments