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

Commit ad08473

Browse files
committed
Add unit tests for FindNearestMatchingLine
1 parent ea9b355 commit ad08473

File tree

3 files changed

+55
-26
lines changed

3 files changed

+55
-26
lines changed

src/GitHub.App/Services/NavigationService.cs

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -54,32 +54,7 @@ public IVsTextView FindActiveView()
5454
return hresult == VSConstants.S_OK ? view : null;
5555
}
5656

57-
string GetText(IVsTextView textView)
58-
{
59-
IVsTextLines buffer;
60-
ErrorHandler.ThrowOnFailure(textView.GetBuffer(out buffer));
61-
62-
int line;
63-
int index;
64-
ErrorHandler.ThrowOnFailure(buffer.GetLastLineIndex(out line, out index));
65-
66-
string text;
67-
ErrorHandler.ThrowOnFailure(buffer.GetLineText(0, 0, line, index, out text));
68-
return text;
69-
}
70-
71-
IVsTextView OpenDocument(string fullPath)
72-
{
73-
var logicalView = VSConstants.LOGVIEWID.TextView_guid;
74-
IVsUIHierarchy hierarchy;
75-
uint itemID;
76-
IVsWindowFrame windowFrame;
77-
IVsTextView view;
78-
VsShellUtilities.OpenDocument(serviceProvider, fullPath, logicalView, out hierarchy, out itemID, out windowFrame, out view);
79-
return view;
80-
}
81-
82-
static int FindNearestMatchingLine(IList<string> fromLines, IList<string> toLines, int line)
57+
public int FindNearestMatchingLine(IList<string> fromLines, IList<string> toLines, int line)
8358
{
8459
line = line < fromLines.Count ? line : fromLines.Count - 1; // VS shows one extra line at end
8560
var fromLine = fromLines[line];
@@ -107,6 +82,31 @@ static int FindNearestMatchingLine(IList<string> fromLines, IList<string> toLine
10782
}
10883
}
10984

85+
string GetText(IVsTextView textView)
86+
{
87+
IVsTextLines buffer;
88+
ErrorHandler.ThrowOnFailure(textView.GetBuffer(out buffer));
89+
90+
int line;
91+
int index;
92+
ErrorHandler.ThrowOnFailure(buffer.GetLastLineIndex(out line, out index));
93+
94+
string text;
95+
ErrorHandler.ThrowOnFailure(buffer.GetLineText(0, 0, line, index, out text));
96+
return text;
97+
}
98+
99+
IVsTextView OpenDocument(string fullPath)
100+
{
101+
var logicalView = VSConstants.LOGVIEWID.TextView_guid;
102+
IVsUIHierarchy hierarchy;
103+
uint itemID;
104+
IVsWindowFrame windowFrame;
105+
IVsTextView view;
106+
VsShellUtilities.OpenDocument(serviceProvider, fullPath, logicalView, out hierarchy, out itemID, out windowFrame, out view);
107+
return view;
108+
}
109+
110110
static IList<string> ReadLines(string text)
111111
{
112112
var lines = new List<string>();
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using GitHub.Services;
4+
using NUnit.Framework;
5+
using NSubstitute;
6+
7+
public class NavigationServiceTests
8+
{
9+
public class TheFindNearestMatchingLineMethod
10+
{
11+
[TestCase(new[] { "line" }, new[] { "line" }, 0, 0, Description = "Match same line")]
12+
[TestCase(new[] { "line" }, new[] { "line_no_match" }, 0, -1, Description = "No matching line")]
13+
[TestCase(new[] { "line" }, new[] { "", "line" }, 0, 1, Description = "Match line moved up")]
14+
[TestCase(new[] { "", "line" }, new[] { "line" }, 1, 0, Description = "Match line moved down")]
15+
[TestCase(new[] { "line", "line" }, new[] { "line", "line" }, 0, 0, Description = "Match nearest line")]
16+
[TestCase(new[] { "line", "line" }, new[] { "line", "line" }, 1, 1, Description = "Match nearest line")]
17+
[TestCase(new[] { "line" }, new[] { "line" }, 1, 0, Description = "Treat after last line the same as last line")]
18+
public void FindNearestMatching(IList<string> fromLines, IList<string> toLines, int line, int expectLine)
19+
{
20+
var sp = Substitute.For<IServiceProvider>();
21+
var target = new NavigationService(sp);
22+
23+
var matchingLine = target.FindNearestMatchingLine(fromLines, toLines, line);
24+
25+
Assert.That(matchingLine, Is.EqualTo(expectLine));
26+
}
27+
}
28+
}

test/UnitTests/UnitTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@
234234
<Compile Include="GitHub.App\Models\RepositoryModelTests.cs" />
235235
<Compile Include="GitHub.App\Services\AvatarProviderTests.cs" />
236236
<Compile Include="GitHub.App\Services\GitClientTests.cs" />
237+
<Compile Include="GitHub.App\Services\NavigationServiceTests.cs" />
237238
<Compile Include="GitHub.App\Services\OAuthCallbackListenerTests.cs" />
238239
<Compile Include="GitHub.App\Services\PullRequestServiceTests.cs" />
239240
<Compile Include="GitHub.App\Services\RepositoryCloneServiceTests.cs" />

0 commit comments

Comments
 (0)