-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from MrLuje/feature/nsubstitute
Feature/nsubstitute
- Loading branch information
Showing
12 changed files
with
520 additions
and
396 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{ "path": "cz-conventional-changelog" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Microsoft.VSSDK.BuildTools |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
Mocking.Helpers/Mocking.Helpers/Core/BaseMockingProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace Mocking.Helpers.Interfaces | ||
{ | ||
public abstract class BaseMockingProvider | ||
{ | ||
public abstract string AssemblyName { get; } | ||
public abstract string MockingMethodName { get; } | ||
public abstract string MockingWildcardMethod { get; } | ||
|
||
protected string FormatMockingWildcardMethod(string parameterTypeName) | ||
{ | ||
return string.Format(this.MockingWildcardMethod, parameterTypeName); | ||
} | ||
|
||
public virtual string GenerateSuggestionForParameterWildcard(SemanticModel model, IMethodSymbol methodSymbol, ArgumentListSyntax arguments) | ||
{ | ||
var suggestionParameter = methodSymbol.Parameters.Aggregate(String.Empty, (acc, p) => $"{acc}, {this.FormatMockingWildcardMethod(p.Type.ToMinimalDisplayString(model, arguments.SpanStart))}").TrimStart(',').Trim(); | ||
return suggestionParameter; | ||
} | ||
|
||
public virtual string GenerateSuggestionForParameterWildcardOfType(SemanticModel model, ITypeSymbol typeSymbol, ArgumentListSyntax arguments) | ||
{ | ||
var suggestionParameter = this.FormatMockingWildcardMethod(typeSymbol.ToMinimalDisplayString(model, arguments.SpanStart)); | ||
return suggestionParameter; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,17 @@ | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Mocking.Helpers.Interfaces; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace Mocking.Helpers.Moq | ||
{ | ||
public class MoqProvider | ||
public class MoqProvider : BaseMockingProvider | ||
{ | ||
public string MockingMethodName { get; } = "Setup"; | ||
public string AssemblyName { get; } = "Moq"; | ||
public string MockingWildcardMethod { get; } = "It.IsAny<{0}>()"; | ||
|
||
string FormatMockingWildcardMethod(string parameterTypeName) | ||
{ | ||
return string.Format(this.MockingWildcardMethod, parameterTypeName); | ||
} | ||
|
||
public string GenerateSuggestionForParameterWildcard(SemanticModel model, IMethodSymbol methodSymbol, ArgumentListSyntax arguments) | ||
{ | ||
var suggestionParameter = methodSymbol.Parameters.Aggregate(String.Empty, (acc, p) => $"{acc}, {this.FormatMockingWildcardMethod(p.Type.ToMinimalDisplayString(model, arguments.SpanStart))}").TrimStart(',').Trim(); | ||
return suggestionParameter; | ||
} | ||
|
||
public string GenerateSuggestionForParameterWildcardOfType(SemanticModel model, ITypeSymbol typeSymbol, ArgumentListSyntax arguments) | ||
{ | ||
var suggestionParameter = this.FormatMockingWildcardMethod(typeSymbol.ToMinimalDisplayString(model, arguments.SpanStart)); | ||
return suggestionParameter; | ||
} | ||
public override string MockingMethodName => "Setup"; | ||
public override string AssemblyName => "Moq"; | ||
public override string MockingWildcardMethod => "It.IsAny<{0}>()"; | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
Mocking.Helpers/Mocking.Helpers/NSubstitute/NSubstituteArgAnyCompletion.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.Completion; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Mocking.Helpers.NSubstitute | ||
{ | ||
[ExportCompletionProvider(nameof(NSubstituteArgAnyCompletion), LanguageNames.CSharp)] | ||
public class NSubstituteArgAnyCompletion : CompletionProvider | ||
{ | ||
private NSubstituteProvider _provider; | ||
|
||
public NSubstituteArgAnyCompletion() | ||
{ | ||
this._provider = new NSubstituteProvider(); | ||
} | ||
|
||
internal bool IsSubstituteForMethod(InvocationExpressionSyntax invocation) | ||
{ | ||
return SyntaxHelpers.IsMethodNamed(invocation, this._provider.MockingMethodName); | ||
} | ||
|
||
public override async Task ProvideCompletionsAsync(CompletionContext context) | ||
{ | ||
try | ||
{ | ||
if (!context.Document.SupportsSemanticModel || !context.Document.SupportsSyntaxTree) return; | ||
|
||
var hasNSubstituteReferenced = context.Document.Project.MetadataReferences.Any(r => r.Display.Contains(this._provider.AssemblyName)); | ||
if (!hasNSubstituteReferenced) return; | ||
|
||
var syntaxRoot = await context.Document.GetSyntaxRootAsync(); | ||
var token = SyntaxHelpers.GetSelectedTokens(syntaxRoot, context.Position); | ||
|
||
// Not in an opened method | ||
if (token.Parent == null) return; | ||
|
||
var mockedMethodArgumentList = token.Parent as ArgumentListSyntax; | ||
var mockedMethodInvocation = mockedMethodArgumentList.Ancestors() | ||
.OfType<InvocationExpressionSyntax>() | ||
.FirstOrDefault(); | ||
|
||
if (mockedMethodInvocation == null) return; | ||
|
||
var semanticModel = await context.Document.GetSemanticModelAsync(); | ||
var matchingMockedMethods = SyntaxHelpers.GetCandidatesMockedMethodSignatures(semanticModel, mockedMethodInvocation); | ||
|
||
var completionService = new CompletionService(context, token, semanticModel, this._provider); | ||
|
||
foreach (IMethodSymbol matchingMockedMethodSymbol in matchingMockedMethods) | ||
{ | ||
completionService.AddSuggestionsForMethod(matchingMockedMethodSymbol, mockedMethodArgumentList); | ||
} | ||
} | ||
catch | ||
{ | ||
} | ||
} | ||
} | ||
} | ||
|
16 changes: 16 additions & 0 deletions
16
Mocking.Helpers/Mocking.Helpers/NSubstitute/NSubstituteProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Mocking.Helpers.Interfaces; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace Mocking.Helpers.NSubstitute | ||
{ | ||
public class NSubstituteProvider : BaseMockingProvider | ||
{ | ||
public override string AssemblyName => "NSubstitute"; | ||
public override string MockingMethodName => "For"; | ||
public override string MockingWildcardMethod => "Arg.Any<{0}>()"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
version 5.137.2 | ||
storage: none | ||
framework: netstandard20 | ||
framework: auto-detect | ||
source https://www.nuget.org/api/v2 | ||
|
||
nuget Microsoft.CodeAnalysis.CSharp 2.6.1 | ||
nuget Microsoft.CodeAnalysis.EditorFeatures 2.6.1 | ||
nuget Microsoft.CodeAnalysis.EditorFeatures.Text 2.6.1 | ||
nuget Microsoft.CodeAnalysis.Features 2.6.1 | ||
nuget Microsoft.CodeAnalysis.Features 2.6.1 | ||
nuget Microsoft.VSSDK.BuildTools 15.5.100 |
Oops, something went wrong.