This repository was archived by the owner on Jun 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathSubstitutes.cs
More file actions
206 lines (178 loc) · 9.72 KB
/
Substitutes.cs
File metadata and controls
206 lines (178 loc) · 9.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
using GitHub.Authentication;
using GitHub.Models;
using GitHub.Services;
using GitHub.VisualStudio;
using Microsoft.VisualStudio.ComponentModelHost;
using NSubstitute;
using Rothko;
using System;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using GitHub.Factories;
using GitHub.Api;
using Microsoft.VisualStudio.Threading;
namespace UnitTests
{
internal static class Substitutes
{
public static T1 For<T1, T2, T3, T4>(params object[] constructorArguments)
where T1 : class
where T2 : class
where T3 : class
where T4 : class
{
return (T1)Substitute.For(new Type[4]
{
typeof (T1),
typeof (T2),
typeof (T3),
typeof (T4)
}, constructorArguments);
}
public static IGitService IGitService { get { return Substitute.For<IGitService>(); } }
public static IViewViewModelFactory ViewViewModelFactory { get { return Substitute.For<IViewViewModelFactory>(); } }
public static IRepositoryCreationService RepositoryCreationService { get { return Substitute.For<IRepositoryCreationService>(); } }
public static IRepositoryCloneService RepositoryCloneService { get { return Substitute.For<IRepositoryCloneService>(); } }
public static IConnection Connection { get { return Substitute.For<IConnection>(); } }
public static IConnectionManager ConnectionManager { get { return Substitute.For<IConnectionManager>(); } }
public static IDelegatingTwoFactorChallengeHandler TwoFactorChallengeHandler { get { return Substitute.For<IDelegatingTwoFactorChallengeHandler>(); } }
public static IGistPublishService GistPublishService { get { return Substitute.For<IGistPublishService>(); } }
public static IPullRequestService PullRequestService { get { return Substitute.For<IPullRequestService>(); } }
/// <summary>
/// This returns a service provider with everything mocked except for
/// RepositoryCloneService and RepositoryCreationService, which are real
/// instances.
/// </summary>
public static IGitHubServiceProvider ServiceProvider { get { return GetServiceProvider(); } }
/// <summary>
/// This returns a service provider with mocked IRepositoryCreationService and
/// IRepositoryCloneService as well as all other services mocked. The regular
/// GetServiceProvider method (and ServiceProvider property return a IServiceProvider
/// with real RepositoryCloneService and RepositoryCreationService instances.
/// </summary>
/// <returns></returns>
public static IServiceProvider GetFullyMockedServiceProvider()
{
return GetServiceProvider(RepositoryCloneService, RepositoryCreationService);
}
/// <summary>
/// This returns a service provider with everything mocked except for
/// RepositoryCloneService and RepositoryCreationService, which are real
/// instances.
/// </summary>
/// <param name="cloneService"></param>
/// <param name="creationService"></param>
/// <returns></returns>
public static IGitHubServiceProvider GetServiceProvider(
IRepositoryCloneService cloneService = null,
IRepositoryCreationService creationService = null,
IAvatarProvider avatarProvider = null)
{
var ret = Substitute.For<IGitHubServiceProvider, IServiceProvider>();
var gitservice = IGitService;
var cm = Substitute.For<SComponentModel, IComponentModel>();
var cc = new CompositionContainer(CompositionOptions.IsThreadSafe | CompositionOptions.DisableSilentRejection);
cc.ComposeExportedValue(gitservice);
((IComponentModel)cm).DefaultExportProvider.Returns(cc);
ret.GetService(typeof(SComponentModel)).Returns(cm);
Services.UnitTestServiceProvider = ret;
var clone = cloneService ?? new RepositoryCloneService(Substitute.For<IOperatingSystem>(),
Substitute.For<IVSGitServices>(), Substitute.For<ITeamExplorerServices>(),
Substitute.For<IGraphQLClientFactory>(), Substitute.For<IGitHubContextService>(),
Substitute.For<IUsageTracker>(), ret, new JoinableTaskContext());
var create = creationService ?? new RepositoryCreationService(clone);
avatarProvider = avatarProvider ?? Substitute.For<IAvatarProvider>();
ret.GetService(typeof(IGitService)).Returns(gitservice);
ret.GetService(typeof(IVSServices)).Returns(Substitute.For<IVSServices>());
ret.GetService(typeof(ITeamExplorerServices)).Returns(Substitute.For<ITeamExplorerServices>());
ret.GetService(typeof(IGraphQLClientFactory)).Returns(Substitute.For<IGraphQLClientFactory>());
ret.GetService(typeof(IGitHubContextService)).Returns(Substitute.For<IGitHubContextService>());
ret.GetService(typeof(IVSGitExt)).Returns(Substitute.For<IVSGitExt>());
ret.GetService(typeof(IUsageTracker)).Returns(Substitute.For<IUsageTracker>());
ret.GetService(typeof(IVSGitServices)).Returns(Substitute.For<IVSGitServices>());
ret.GetService(typeof(IOperatingSystem)).Returns(Substitute.For<IOperatingSystem>());
ret.GetService(typeof(IRepositoryCloneService)).Returns(clone);
ret.GetService(typeof(IRepositoryCreationService)).Returns(create);
ret.GetService(typeof(IViewViewModelFactory)).Returns(ViewViewModelFactory);
ret.GetService(typeof(IConnection)).Returns(Connection);
ret.GetService(typeof(IConnectionManager)).Returns(ConnectionManager);
ret.GetService(typeof(IAvatarProvider)).Returns(avatarProvider);
ret.GetService(typeof(IDelegatingTwoFactorChallengeHandler)).Returns(TwoFactorChallengeHandler);
ret.GetService(typeof(IGistPublishService)).Returns(GistPublishService);
ret.GetService(typeof(IPullRequestService)).Returns(PullRequestService);
return ret;
}
public static IVSServices GetVSServices(this IServiceProvider provider)
{
return provider.GetService(typeof(IVSServices)) as IVSServices;
}
public static ITeamExplorerServices GetTeamExplorerServices(this IServiceProvider provider)
{
return provider.GetService(typeof(ITeamExplorerServices)) as ITeamExplorerServices;
}
public static IGraphQLClientFactory GetGraphQLClientFactory(this IServiceProvider provider)
{
return provider.GetService(typeof(IGraphQLClientFactory)) as IGraphQLClientFactory;
}
public static IGitHubContextService GetGitHubContextService(this IServiceProvider provider)
{
return provider.GetService(typeof(IGitHubContextService)) as IGitHubContextService;
}
public static IVSGitExt GetVSGitExt(this IServiceProvider provider)
{
return provider.GetService(typeof(IVSGitExt)) as IVSGitExt;
}
public static IUsageTracker GetUsageTracker(this IServiceProvider provider)
{
return provider.GetService(typeof(IUsageTracker)) as IUsageTracker;
}
public static IVSGitServices GetVSGitServices(this IServiceProvider provider)
{
return provider.GetService(typeof(IVSGitServices)) as IVSGitServices;
}
public static IGitService GetGitService(this IServiceProvider provider)
{
return provider.GetService(typeof(IGitService)) as IGitService;
}
public static IOperatingSystem GetOperatingSystem(this IServiceProvider provider)
{
return provider.GetService(typeof(IOperatingSystem)) as IOperatingSystem;
}
public static IRepositoryCloneService GetRepositoryCloneService(this IServiceProvider provider)
{
return provider.GetService(typeof(IRepositoryCloneService)) as IRepositoryCloneService;
}
public static IRepositoryCreationService GetRepositoryCreationService(this IServiceProvider provider)
{
return provider.GetService(typeof(IRepositoryCreationService)) as IRepositoryCreationService;
}
public static IViewViewModelFactory GetExportFactoryProvider(this IServiceProvider provider)
{
return provider.GetService(typeof(IViewViewModelFactory)) as IViewViewModelFactory;
}
public static IConnection GetConnection(this IServiceProvider provider)
{
return provider.GetService(typeof(IConnection)) as IConnection;
}
public static IConnectionManager GetConnectionManager(this IServiceProvider provider)
{
return provider.GetService(typeof(IConnectionManager)) as IConnectionManager;
}
public static IAvatarProvider GetAvatarProvider(this IServiceProvider provider)
{
return provider.GetService(typeof(IAvatarProvider)) as IAvatarProvider;
}
public static IDelegatingTwoFactorChallengeHandler GetTwoFactorChallengeHandler(this IServiceProvider provider)
{
return provider.GetService(typeof(IDelegatingTwoFactorChallengeHandler)) as IDelegatingTwoFactorChallengeHandler;
}
public static IGistPublishService GetGistPublishService(this IServiceProvider provider)
{
return provider.GetService(typeof(IGistPublishService)) as IGistPublishService;
}
public static IPullRequestService GetPullRequestsService(this IServiceProvider provider)
{
return provider.GetService(typeof(IPullRequestService)) as IPullRequestService;
}
}
}