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

Commit d8b2d16

Browse files
committed
Add tests for url generation
1 parent b36c02c commit d8b2d16

File tree

3 files changed

+88
-2
lines changed

3 files changed

+88
-2
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using System;
2+
using GitHub.Extensions;
3+
using GitHub.Models;
4+
using GitHub.VisualStudio;
5+
using LibGit2Sharp;
6+
using NSubstitute;
7+
using UnitTests;
8+
using Xunit;
9+
using GitHub.Primitives;
10+
using System.Linq;
11+
12+
public class SimpleRepositoryModelExtensionTests : TempFileBaseClass
13+
{
14+
void SetupRepository(string sha)
15+
{
16+
var provider = Substitutes.ServiceProvider;
17+
Services.PackageServiceProvider = provider;
18+
var gitservice = provider.GetGitService();
19+
var repo = Substitute.For<IRepository>();
20+
gitservice.GetRepo(Args.String).Returns(repo);
21+
if (!String.IsNullOrEmpty(sha))
22+
{
23+
var commit = Substitute.For<Commit>();
24+
commit.Sha.Returns(sha);
25+
repo.Commits.Returns(new FakeCommitLog { commit });
26+
}
27+
}
28+
29+
[Theory]
30+
[InlineData(false, "https://github.com/foo/bar", "123123", @"src\dir\file1.cs", -1, -1, "https://github.com/foo/bar/blob/123123/src/dir/file1.cs")]
31+
[InlineData(false, "https://github.com/foo/bar", "123123", @"src\dir\file1.cs", 1, -1, "https://github.com/foo/bar/blob/123123/src/dir/file1.cs#L1")]
32+
[InlineData(false, "https://github.com/foo/bar", "123123", @"src\dir\file1.cs", 1, 2, "https://github.com/foo/bar/blob/123123/src/dir/file1.cs#L1-L2")]
33+
[InlineData(false, "https://github.com/foo/bar", "123123", @"src\dir\file1.cs", -1, 2, "https://github.com/foo/bar/blob/123123/src/dir/file1.cs")]
34+
[InlineData(false, "https://github.com/foo/bar", "123123", "", 1, 2, "https://github.com/foo/bar/commit/123123")]
35+
[InlineData(false, "https://github.com/foo/bar", "", @"src\dir\file1.cs", -1, 2, "https://github.com/foo/bar")]
36+
[InlineData(false, "https://github.com/foo/bar", null, null, -1, -1, "https://github.com/foo/bar")]
37+
[InlineData(false, null, "123123", @"src\dir\file1.cs", 1, 2, null)]
38+
[InlineData(true, "https://github.com/foo/bar", "123123", @"src\dir\file1.cs", -1, -1, "https://github.com/foo/bar/blob/123123/src/dir/file1.cs")]
39+
[InlineData(true, "https://github.com/foo/bar", "123123", @"src\dir\file1.cs", 1, -1, "https://github.com/foo/bar/blob/123123/src/dir/file1.cs#L1")]
40+
[InlineData(true, "https://github.com/foo/bar", "123123", @"src\dir\file1.cs", 1, 2, "https://github.com/foo/bar/blob/123123/src/dir/file1.cs#L1-L2")]
41+
[InlineData(true, "https://github.com/foo/bar", "123123", @"src\dir\file1.cs", -1, 2, "https://github.com/foo/bar/blob/123123/src/dir/file1.cs")]
42+
[InlineData(true, "https://github.com/foo/bar", "", @"src\dir\file1.cs", -1, 2, "https://github.com/foo/bar")]
43+
[InlineData(true, null, "123123", @"src\dir\file1.cs", 1, 2, null)]
44+
public void GenerateUrl(bool createRootedPath, string baseUrl, string sha, string path, int startLine, int endLine, string expected)
45+
{
46+
SetupRepository(sha);
47+
48+
var basePath = Directory.CreateSubdirectory("generate-url-test1");
49+
if (createRootedPath && path != null)
50+
path = System.IO.Path.Combine(basePath.FullName, path);
51+
ISimpleRepositoryModel model = null;
52+
if (!String.IsNullOrEmpty(baseUrl))
53+
model = new SimpleRepositoryModel("bar", new UriString(baseUrl), basePath.FullName);
54+
else
55+
model = new SimpleRepositoryModel(basePath.FullName);
56+
var result = model.GenerateUrl(path, startLine, endLine);
57+
Assert.Equal(expected, result?.ToString());
58+
}
59+
}
Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using LibGit2Sharp;
34

4-
public class FakeCommitLog : List<Commit>, ICommitLog
5+
public class FakeCommitLog : List<Commit>, IQueryableCommitLog
56
{
67
public CommitSortStrategies SortedBy
78
{
@@ -10,4 +11,29 @@ public CommitSortStrategies SortedBy
1011
return CommitSortStrategies.Topological;
1112
}
1213
}
14+
15+
public Commit FindMergeBase(IEnumerable<Commit> commits, MergeBaseFindingStrategy strategy)
16+
{
17+
throw new NotImplementedException();
18+
}
19+
20+
public Commit FindMergeBase(Commit first, Commit second)
21+
{
22+
throw new NotImplementedException();
23+
}
24+
25+
public IEnumerable<LogEntry> QueryBy(string path)
26+
{
27+
throw new NotImplementedException();
28+
}
29+
30+
public ICommitLog QueryBy(CommitFilter filter)
31+
{
32+
throw new NotImplementedException();
33+
}
34+
35+
public IEnumerable<LogEntry> QueryBy(string path, FollowFilter filter)
36+
{
37+
throw new NotImplementedException();
38+
}
1339
}

src/UnitTests/UnitTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@
155155
<Compile Include="GitHub.Exports.Reactive\Caches\AccountCacheItemTests.cs" />
156156
<Compile Include="GitHub.Exports\GitServiceTests.cs" />
157157
<Compile Include="GitHub.Exports\VSServicesTests.cs" />
158+
<Compile Include="GitHub.Extensions\SimpleRepositoryModelExtensionTests.cs" />
158159
<Compile Include="GitHub.Extensions\UriExtensionTests.cs" />
159160
<Compile Include="GitHub.Primitives\UriStringTests.cs" />
160161
<Compile Include="GitHub.UI\TwoFactorInputTests.cs" />

0 commit comments

Comments
 (0)