diff --git a/src/GitHub.Exports/Models/DiffUtilities.cs b/src/GitHub.Exports/Models/DiffUtilities.cs index d1e123bcff..a6914e4d78 100644 --- a/src/GitHub.Exports/Models/DiffUtilities.cs +++ b/src/GitHub.Exports/Models/DiffUtilities.cs @@ -96,7 +96,10 @@ public static DiffLine Match(IEnumerable diff, IList target { if (source.Lines[i].Content == target[j].Content) { - if (++j == target.Count) return source.Lines[i + j - 1]; + if (++j == target.Count || i == 0) + { + return source.Lines[i + j - 1]; + } } else { diff --git a/test/GitHub.InlineReviews.UnitTests/Models/DiffUtilitiesTests.cs b/test/GitHub.InlineReviews.UnitTests/Models/DiffUtilitiesTests.cs index 78cd796831..d1328baf6b 100644 --- a/test/GitHub.InlineReviews.UnitTests/Models/DiffUtilitiesTests.cs +++ b/test/GitHub.InlineReviews.UnitTests/Models/DiffUtilitiesTests.cs @@ -173,14 +173,20 @@ public class TheMatchMethod { [Theory] [InlineData(" 1", " 1", 0)] - [InlineData(" 1\n 2", " 2", 1)] - [InlineData(" 1\n 1", " 1", 1)] // match the later line + [InlineData(" 1. 2", " 2", 1)] + [InlineData(" 1. 1", " 1", 1)] // match the later line [InlineData("+x", "-x", -1)] [InlineData("", " x", -1)] [InlineData(" x", "", -1)] + + [InlineData(" 1. 2.", " 2. 1.", 1)] // matched full context + [InlineData(" 1. 2.", " 2. 3.", -1)] // didn't match full context + [InlineData(" 2.", " 2. 1.", 0)] // match if we run out of context lines public void MatchLine(string lines1, string lines2, int skip /* -1 for no match */) { var header = "@@ -1 +1 @@"; + lines1 = lines1.Replace(".", "\r\n"); + lines2 = lines2.Replace(".", "\r\n"); var chunks1 = DiffUtilities.ParseFragment(header + "\n" + lines1).ToList(); var chunks2 = DiffUtilities.ParseFragment(header + "\n" + lines2).ToList(); var expectLine = (skip != -1) ? chunks1.First().Lines.Skip(skip).First() : null;