Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit aac9adb

Browse files
committed
Make tests for for DiffUtilities.Match easier to understand
1 parent aa210b8 commit aac9adb

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed

test/GitHub.InlineReviews.UnitTests/Models/DiffUtilitiesTests.cs

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -185,38 +185,41 @@ public void InvalidDiffLineChangeChar(string line, string expectMessage)
185185

186186
public class TheMatchMethod
187187
{
188+
/// <param name="diffLines">Target diff chunk with header (with '.' as line separator)</param>
189+
/// <param name="matchLines">Diff lines to match (with '.' as line separator)</param>
190+
/// <param name="expectedDiffLineNumber">The DiffLineNumber that the last line of matchLines falls on</param>
188191
[Theory]
189-
[InlineData(" 1", " 1", 0)]
190-
[InlineData(" 1. 2", " 2", 1)]
191-
[InlineData(" 1. 1", " 1", 1)] // match the later line
192+
[InlineData(" 1", " 1", 1)]
193+
[InlineData(" 1. 2", " 2", 2)]
194+
[InlineData(" 1. 1", " 1", 2)] // match the later line
192195
[InlineData("+x", "-x", -1)]
193196
[InlineData("", " x", -1)]
194197
[InlineData(" x", "", -1)]
195198

196-
[InlineData(" 1. 2.", " 1. 2.", 1)] // matched full context
199+
[InlineData(" 1. 2.", " 1. 2.", 2)] // matched full context
197200
[InlineData(" 1. 2.", " 3. 2.", -1)] // didn't match full context
198-
[InlineData(" 2.", " 1. 2.", 0)] // match if we run out of context lines
201+
[InlineData(" 2.", " 1. 2.", 1)] // match if we run out of context lines
199202

200203
// Tests for https://github.com/github/VisualStudio/issues/1149
201204
// Matching algorithm got confused when there was a partial match.
202-
[InlineData("+a.+x.+x.", "+a.+x.", 1)]
203-
[InlineData("+a.+x.+x.", "+a.+x.+x.", 2)]
204-
[InlineData("+a.+x.+x.+b.+x.+x.", "+a.+x.", 1)]
205-
[InlineData("+a.+x.+x.+b.+x.+x.", "+b.+x.", 4)]
205+
[InlineData("+a.+x.+x.", "+a.+x.", 2)]
206+
[InlineData("+a.+x.+x.", "+a.+x.+x.", 3)]
207+
[InlineData("+a.+x.+x.+b.+x.+x.", "+a.+x.", 2)]
208+
[InlineData("+a.+x.+x.+b.+x.+x.", "+b.+x.", 5)]
206209
[InlineData("+a.+b.+x", "+a.+x.", -1)] // backtrack when there is a failed match
207-
public void MatchLine(string lines1, string lines2, int skip /* -1 for no match */)
210+
public void MatchLine(string diffLines, string matchLines, int expectedDiffLineNumber /* -1 for no match */)
208211
{
209212
var header = "@@ -1 +1 @@";
210-
lines1 = lines1.Replace(".", "\r\n");
211-
lines2 = lines2.Replace(".", "\r\n");
212-
var chunks1 = DiffUtilities.ParseFragment(header + "\n" + lines1).ToList();
213-
var chunks2 = DiffUtilities.ParseFragment(header + "\n" + lines2).ToList();
214-
var expectLine = (skip != -1) ? chunks1.First().Lines[skip] : null;
213+
diffLines = diffLines.Replace(".", "\r\n");
214+
matchLines = matchLines.Replace(".", "\r\n");
215+
var chunks1 = DiffUtilities.ParseFragment(header + "\n" + diffLines).ToList();
216+
var chunks2 = DiffUtilities.ParseFragment(header + "\n" + matchLines).ToList();
215217
var targetLines = chunks2.First().Lines.Reverse().ToList();
216218

217219
var line = DiffUtilities.Match(chunks1, targetLines);
218220

219-
Assert.Equal(expectLine, line);
221+
var diffLineNumber = (line != null) ? line.DiffLineNumber : -1;
222+
Assert.Equal(expectedDiffLineNumber, diffLineNumber);
220223
}
221224

222225
[Fact]

0 commit comments

Comments
 (0)