11using System ;
22using GitHub . App . Services ;
3+ using NSubstitute ;
34using NUnit . Framework ;
45
56public class GitHubContextServiceTests
@@ -12,7 +13,7 @@ public class TheFindContextFromUrlMethod
1213 [ TestCase ( "https://github.com/github/VisualStudio/blob/master/README.md" , "github" ) ]
1314 public void Owner ( string url , string expectOwner )
1415 {
15- var target = new GitHubContextService ( ) ;
16+ var target = CreateGitHubContextService ( ) ;
1617
1718 var context = target . FindContextFromUrl ( url ) ;
1819
@@ -25,7 +26,7 @@ public void Owner(string url, string expectOwner)
2526 [ TestCase ( "https://github.com/github/VisualStudio/blob/master/README.md" , "VisualStudio" ) ]
2627 public void RepositoryName ( string url , string expectRepositoryName )
2728 {
28- var target = new GitHubContextService ( ) ;
29+ var target = CreateGitHubContextService ( ) ;
2930
3031 var context = target . FindContextFromUrl ( url ) ;
3132
@@ -38,7 +39,7 @@ public void RepositoryName(string url, string expectRepositoryName)
3839 [ TestCase ( "https://github.com/github/VisualStudio/blob/master/README.md" , "github.com" ) ]
3940 public void Host ( string url , string expectHost )
4041 {
41- var target = new GitHubContextService ( ) ;
42+ var target = CreateGitHubContextService ( ) ;
4243
4344 var context = target . FindContextFromUrl ( url ) ;
4445
@@ -55,7 +56,7 @@ public void Host(string url, string expectHost)
5556 [ TestCase ( "https://github.com/github/VisualStudio/pull/NaN" , null ) ]
5657 public void PullRequest ( string url , int ? expectPullRequest )
5758 {
58- var target = new GitHubContextService ( ) ;
59+ var target = CreateGitHubContextService ( ) ;
5960
6061 var context = target . FindContextFromUrl ( url ) ;
6162
@@ -70,7 +71,7 @@ public void PullRequest(string url, int? expectPullRequest)
7071 [ TestCase ( "https://github.com/github/VisualStudio/blob/0d264d50c57d701fa62d202f481075a6c6dbdce8/src/Code.cs#L86" , "src/Code.cs" ) ]
7172 public void Path ( string url , string expectPath )
7273 {
73- var target = new GitHubContextService ( ) ;
74+ var target = CreateGitHubContextService ( ) ;
7475
7576 var context = target . FindContextFromUrl ( url ) ;
7677
@@ -81,7 +82,7 @@ public void Path(string url, string expectPath)
8182 [ TestCase ( "https://github.com/github/VisualStudio/blob/fixes/branch/buggy.cs" , "branch/buggy.cs" ) ]
8283 public void ProblemPath ( string url , string expectPath )
8384 {
84- var target = new GitHubContextService ( ) ;
85+ var target = CreateGitHubContextService ( ) ;
8586
8687 var context = target . FindContextFromUrl ( url ) ;
8788
@@ -97,7 +98,7 @@ public void ProblemPath(string url, string expectPath)
9798 [ TestCase ( "https://github.com/github/VisualStudio/blob/0d264d50c57d701fa62d202f481075a6c6dbdce8/src/Code.cs#L86" , 86 ) ]
9899 public void Line ( string url , int ? expectLine )
99100 {
100- var target = new GitHubContextService ( ) ;
101+ var target = CreateGitHubContextService ( ) ;
101102
102103 var context = target . FindContextFromUrl ( url ) ;
103104
@@ -109,7 +110,7 @@ public void Line(string url, int? expectLine)
109110 [ TestCase ( "https://github.com/github/VisualStudio/blob/master/Code.cs#L115-L116" , 115 , 116 ) ]
110111 public void LineEnd ( string url , int ? expectLine , int ? expectLineEnd )
111112 {
112- var target = new GitHubContextService ( ) ;
113+ var target = CreateGitHubContextService ( ) ;
113114
114115 var context = target . FindContextFromUrl ( url ) ;
115116
@@ -122,7 +123,7 @@ public void LineEnd(string url, int? expectLine, int? expectLineEnd)
122123 [ TestCase ( "https://github.com/github/VisualStudio" , false ) ]
123124 public void IsNull ( string url , bool expectNull )
124125 {
125- var target = new GitHubContextService ( ) ;
126+ var target = CreateGitHubContextService ( ) ;
126127
127128 var context = target . FindContextFromUrl ( url ) ;
128129
@@ -136,7 +137,7 @@ public class TheToMethod
136137 public void DefaultGitHubDotCom ( )
137138 {
138139 var context = new GitHubContext { Host = "github.com" , Owner = "github" , RepositoryName = "VisualStudio" } ;
139- var target = new GitHubContextService ( ) ;
140+ var target = CreateGitHubContextService ( ) ;
140141
141142 var uri = target . ToRepositoryUrl ( context ) ;
142143
@@ -155,7 +156,7 @@ public class TheFindContextFromWindowTitleMethod
155156 [ TestCase ( "github/$: Description - Google Chrome" , null , Description = "Must contain only letters, numbers, `_`, `.` or `-`" ) ]
156157 public void RepositoryName ( string windowTitle , string expectRepositoryName )
157158 {
158- var target = new GitHubContextService ( ) ;
159+ var target = CreateGitHubContextService ( ) ;
159160
160161 var context = target . FindContextFromWindowTitle ( windowTitle ) ;
161162
@@ -171,7 +172,7 @@ public void RepositoryName(string windowTitle, string expectRepositoryName)
171172 [ TestCase ( "-/Repository: Description - Google Chrome" , null , Description = "Must start with letter or number" ) ]
172173 public void Owner ( string windowTitle , string expectOwner )
173174 {
174- var target = new GitHubContextService ( ) ;
175+ var target = CreateGitHubContextService ( ) ;
175176
176177 var context = target . FindContextFromWindowTitle ( windowTitle ) ;
177178
@@ -217,7 +218,7 @@ public void Owner(string windowTitle, string expectOwner)
217218 public void Branch ( string branch , string expectBranch )
218219 {
219220 var windowTitle = $ "VisualStudio/src/GitHub.VisualStudio/Resources/icons at { branch } · github/VisualStudio - Google Chrome";
220- var target = new GitHubContextService ( ) ;
221+ var target = CreateGitHubContextService ( ) ;
221222
222223 var context = target . FindContextFromWindowTitle ( windowTitle ) ;
223224
@@ -234,7 +235,7 @@ public void Branch(string branch, string expectBranch)
234235 [ TestCase ( "VisualStudio/GitHub.Exports.csproj at 89484dc25a3a475d3253afdc3bd3ddd6c6999c3b · github/VisualStudio - Google Chrome" , "github" , "VisualStudio" , "89484dc25a3a475d3253afdc3bd3ddd6c6999c3b" ) ]
235236 public void OwnerRepositoryBranch ( string windowTitle , string expectOwner , string expectRepositoryName , string expectBranch )
236237 {
237- var target = new GitHubContextService ( ) ;
238+ var target = CreateGitHubContextService ( ) ;
238239
239240 var context = target . FindContextFromWindowTitle ( windowTitle ) ;
240241
@@ -247,7 +248,7 @@ public void OwnerRepositoryBranch(string windowTitle, string expectOwner, string
247248 [ TestCase ( "GitHub - github/VisualStudio at refactor/pr-list - Mozilla Firefox" , "github" , "VisualStudio" , "refactor/pr-list" , Description = "Firefox" ) ]
248249 public void TreeBranch ( string windowTitle , string expectOwner , string expectRepositoryName , string expectBranch )
249250 {
250- var target = new GitHubContextService ( ) ;
251+ var target = CreateGitHubContextService ( ) ;
251252
252253 var context = target . FindContextFromWindowTitle ( windowTitle ) ;
253254
@@ -260,7 +261,7 @@ public void TreeBranch(string windowTitle, string expectOwner, string expectRepo
260261 [ TestCase ( "Branches · github/VisualStudio · GitHub - Mozilla Firefox" , "github" , "VisualStudio" , Description = "Firefox" ) ]
261262 public void Branches ( string windowTitle , string expectOwner , string expectRepositoryName )
262263 {
263- var target = new GitHubContextService ( ) ;
264+ var target = CreateGitHubContextService ( ) ;
264265
265266 var context = target . FindContextFromWindowTitle ( windowTitle ) ;
266267
@@ -272,7 +273,7 @@ public void Branches(string windowTitle, string expectOwner, string expectReposi
272273 [ TestCase ( "Description · Pull Request #1763 · github/VisualStudio · GitHub - Mozilla Firefox" , 1763 , Description = "Firefox" ) ]
273274 public void PullRequest ( string windowTitle , int expectPullRequest )
274275 {
275- var target = new GitHubContextService ( ) ;
276+ var target = CreateGitHubContextService ( ) ;
276277
277278 var context = target . FindContextFromWindowTitle ( windowTitle ) ;
278279
@@ -283,7 +284,7 @@ public void PullRequest(string windowTitle, int expectPullRequest)
283284 [ TestCase ( "Scrape browser titles · Issue #4 · jcansdale/VisualStudio · GitHub - Mozilla Firefox" , 4 , Description = "Firefox" ) ]
284285 public void Issue ( string windowTitle , int expectIssue )
285286 {
286- var target = new GitHubContextService ( ) ;
287+ var target = CreateGitHubContextService ( ) ;
287288
288289 var context = target . FindContextFromWindowTitle ( windowTitle ) ;
289290
@@ -295,7 +296,7 @@ public void Issue(string windowTitle, int expectIssue)
295296 [ TestCase ( "VisualStudio/README.md at master · jcansdale/VisualStudio · GitHub - Mozilla Firefox" , "README.md" , Description = "Firefox" ) ]
296297 public void Path ( string windowTitle , string expectPath )
297298 {
298- var target = new GitHubContextService ( ) ;
299+ var target = CreateGitHubContextService ( ) ;
299300
300301 var context = target . FindContextFromWindowTitle ( windowTitle ) ;
301302
@@ -308,12 +309,18 @@ public void Path(string windowTitle, string expectPath)
308309 [ TestCase ( "GitHub - jcansdale/GhostAssemblies - Mozilla Firefox" , "jcansdale" , "GhostAssemblies" , Description = "No description, Firefox" ) ]
309310 public void RepositoryHome ( string windowTitle , string expectOwner , string expectRepositoryName )
310311 {
311- var target = new GitHubContextService ( ) ;
312+ var target = CreateGitHubContextService ( ) ;
312313
313314 var context = target . FindContextFromWindowTitle ( windowTitle ) ;
314315
315316 Assert . That ( context ? . Owner , Is . EqualTo ( expectOwner ) ) ;
316317 Assert . That ( context ? . RepositoryName , Is . EqualTo ( expectRepositoryName ) ) ;
317318 }
318319 }
320+
321+ static GitHubContextService CreateGitHubContextService ( )
322+ {
323+ var sp = Substitute . For < IServiceProvider > ( ) ;
324+ return new GitHubContextService ( sp ) ;
325+ }
319326}
0 commit comments