Skip to content

Commit

Permalink
[GH-157] - correct handling of CallInfo<T> usages
Browse files Browse the repository at this point in the history
  • Loading branch information
tpodolak committed Jan 30, 2021
1 parent 0d364ec commit 25757a1
Show file tree
Hide file tree
Showing 27 changed files with 397 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="NSubstitute" Version="4.0.0" />
<PackageReference Include="NSubstitute" Version="4.2.2" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="NSubstitute" Version="4.0.0" />
<PackageReference Include="NSubstitute" Version="4.2.2" />
</ItemGroup>

</Project>
Binary file added libs/nsubstitute-4.2.2/NSubstitute.dll
Binary file not shown.
Binary file added libs/nsubstitute-latest/NSubstitute.dll
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -96,24 +96,10 @@ private static SeparatedSyntaxList<ExpressionSyntax> CreateSimpleLambdaExpressio
private static ArrayTypeSyntax CreateArrayTypeNode(SyntaxGenerator syntaxGenerator, ITypeSymbol type)
{
var typeSyntax = (TypeSyntax)syntaxGenerator.TypeExpression(type);
var typeArgumentListSyntax = TypeArgumentList(
SeparatedList<TypeSyntax>(
new SyntaxNodeOrToken[]
{
QualifiedName(
QualifiedName(
IdentifierName("NSubstitute"),
IdentifierName("Core")),
IdentifierName("CallInfo")),
Token(SyntaxKind.CommaToken),
typeSyntax
}));

var qualifiedNameSyntax = QualifiedName(IdentifierName("System"), GenericName(Identifier("Func"), typeArgumentListSyntax));

var arrayRankSpecifierSyntaxes = SingletonList(ArrayRankSpecifier(SingletonSeparatedList<ExpressionSyntax>(OmittedArraySizeExpression())));

return ArrayType(qualifiedNameSyntax, arrayRankSpecifierSyntaxes).WithAdditionalAnnotations(Simplifier.Annotation);
return ArrayType(typeSyntax, arrayRankSpecifierSyntaxes).WithAdditionalAnnotations(Simplifier.Annotation);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Microsoft.CodeAnalysis.CSharp.Syntax;
using NSubstitute.Analyzers.Shared;
using NSubstitute.Analyzers.Shared.DiagnosticAnalyzers;
using NSubstitute.Analyzers.Shared.Extensions;

namespace NSubstitute.Analyzers.CSharp.DiagnosticAnalyzers
{
Expand Down Expand Up @@ -48,7 +49,7 @@ public override void VisitInvocationExpression(InvocationExpressionSyntax node)
{
var symbolInfo = _semanticModel.GetSymbolInfo(node);

if (symbolInfo.Symbol != null && symbolInfo.Symbol.ContainingType.ToString().Equals(MetadataNames.NSubstituteCallInfoFullTypeName))
if (symbolInfo.Symbol != null && symbolInfo.Symbol.ContainingType.IsCallInfoSymbol())
{
switch (symbolInfo.Symbol.Name)
{
Expand All @@ -67,7 +68,7 @@ public override void VisitInvocationExpression(InvocationExpressionSyntax node)
public override void VisitElementAccessExpression(ElementAccessExpressionSyntax node)
{
var symbolInfo = ModelExtensions.GetSymbolInfo(_semanticModel, node).Symbol ?? ModelExtensions.GetSymbolInfo(_semanticModel, node.Expression).Symbol;
if (symbolInfo != null && symbolInfo.ContainingType.ToString().Equals(MetadataNames.NSubstituteCallInfoFullTypeName))
if (symbolInfo != null && symbolInfo.ContainingType.IsCallInfoSymbol())
{
DirectIndexerAccesses.Add(node);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Microsoft.CodeAnalysis.Editing;
using Microsoft.CodeAnalysis.Operations;
using Microsoft.CodeAnalysis.Simplification;
using NSubstitute.Analyzers.Shared.Extensions;

namespace NSubstitute.Analyzers.Shared.CodeFixProviders
{
Expand Down Expand Up @@ -78,13 +79,15 @@ private async Task<Document> CreateChangedDocument(
? 1
: 0;

ITypeSymbol lambdaType = null;
foreach (var argumentSyntax in argumentSyntaxes.Skip(skip))
{
if (IsArrayParamsArgument(semanticModel, argumentSyntax))
{
lambdaType = lambdaType ?? ConstructCallInfoLambdaType(methodSymbol, semanticModel);
var updatedParamsArgumentSyntaxNode = CreateUpdatedParamsArgumentSyntaxNode(
SyntaxGenerator.GetGenerator(context.Document),
methodSymbol.TypeArguments.FirstOrDefault() ?? methodSymbol.ReceiverType,
lambdaType,
argumentSyntax);

documentEditor.ReplaceNode(argumentSyntax, updatedParamsArgumentSyntaxNode);
Expand All @@ -100,6 +103,21 @@ private async Task<Document> CreateChangedDocument(
return await Simplifier.ReduceAsync(documentEditor.GetChangedDocument(), cancellationToken: ct);
}

private static ITypeSymbol ConstructCallInfoLambdaType(IMethodSymbol methodSymbol, SemanticModel semanticModel)
{
var callInfoOverloadMethodSymbol = methodSymbol.ContainingType.GetMembers(methodSymbol.Name)
.Where(symbol => !symbol.Equals(methodSymbol.ConstructedFrom))
.OfType<IMethodSymbol>()
.First(method => method.Parameters.Any(param => param.Type.IsCallInfoDelegate(semanticModel)));

var typeArgument = methodSymbol.TypeArguments.FirstOrDefault() ?? methodSymbol.ReceiverType;
var constructedOverloadSymbol = callInfoOverloadMethodSymbol.Construct(typeArgument);
var lambdaType = constructedOverloadSymbol.Parameters
.First(param => param.Type.IsCallInfoDelegate(semanticModel)).Type;

return lambdaType;
}

private bool IsFixSupported(SemanticModel semanticModel, IEnumerable<TArgumentSyntax> arguments)
{
return arguments.All(arg =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ typeSymbol is INamedTypeSymbol namedTypeSymbol &&
}

public static bool IsCallInfoSymbol(this ITypeSymbol symbol)
{
return IsCallInfoSymbolInternal(symbol) || IsCallInfoSymbolInternal(symbol.BaseType);
}

private static bool IsCallInfoSymbolInternal(ISymbol symbol)
{
return symbol != null &&
symbol.ContainingAssembly?.Name.Equals(MetadataNames.NSubstituteAssemblyName, StringComparison.OrdinalIgnoreCase) == true &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,25 +116,10 @@ private static SingleLineLambdaExpressionSyntax CreateSingleLineLambdaExpression
return lambdaExpression;
}

private static QualifiedNameSyntax CreateTypeNode(SyntaxGenerator syntaxGenerator, ITypeSymbol type)
private static TypeSyntax CreateTypeNode(SyntaxGenerator syntaxGenerator, ITypeSymbol type)
{
var typeSyntax = (TypeSyntax)syntaxGenerator.TypeExpression(type);
var typeArgumentListSyntax = TypeArgumentList(
SeparatedList<TypeSyntax>(
new SyntaxNodeOrToken[]
{
QualifiedName(
QualifiedName(
IdentifierName("NSubstitute"),
IdentifierName("Core")),
IdentifierName("CallInfo")),
Token(SyntaxKind.CommaToken),
typeSyntax
}));

var qualifiedNameSyntax = QualifiedName(IdentifierName("System"), GenericName(Identifier("Func"), typeArgumentListSyntax));

return qualifiedNameSyntax.WithAdditionalAnnotations(Simplifier.Annotation);
return typeSyntax.WithAdditionalAnnotations(Simplifier.Annotation);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Microsoft.CodeAnalysis.VisualBasic.Syntax;
using NSubstitute.Analyzers.Shared;
using NSubstitute.Analyzers.Shared.DiagnosticAnalyzers;
using NSubstitute.Analyzers.Shared.Extensions;

namespace NSubstitute.Analyzers.VisualBasic.DiagnosticAnalyzers
{
Expand Down Expand Up @@ -48,7 +49,7 @@ public override void VisitInvocationExpression(InvocationExpressionSyntax node)
{
var symbol = _semanticModel.GetSymbolInfo(node).Symbol;

if (symbol != null && symbol.ContainingType.ToString().Equals(MetadataNames.NSubstituteCallInfoFullTypeName))
if (symbol != null && symbol.ContainingType.IsCallInfoSymbol())
{
switch (symbol.Name)
{
Expand All @@ -68,7 +69,7 @@ public override void VisitInvocationExpression(InvocationExpressionSyntax node)
{
var expressionSymbol = _semanticModel.GetSymbolInfo(node.Expression).Symbol;

if (expressionSymbol != null && expressionSymbol.ContainingType.ToString().Equals(MetadataNames.NSubstituteCallInfoFullTypeName))
if (expressionSymbol != null && expressionSymbol.ContainingType.IsCallInfoSymbol())
{
DirectIndexerAccesses.Add(node);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
<PackageReference Include="coverlet.msbuild" Version="2.6.0" />
<PackageReference Include="FluentAssertions" Version="5.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0" />
<PackageReference Include="NSubstitute" Version="4.0.0" />
<PackageReference Include="System.Threading.Tasks.Extensions" Version="4.5.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
<PackageReference Include="xunit" Version="2.4.1" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,17 @@ protected ReEntrantSetupCodeFixVerifier()

[Theory]
[InlineData("CreateReEntrantSubstitute(), CreateDefaultValue(), 1", "_ => CreateReEntrantSubstitute(), _ => CreateDefaultValue(), _ => 1")]
[InlineData("CreateReEntrantSubstitute(), new [] { CreateDefaultValue(), 1 }", "_ => CreateReEntrantSubstitute(), new System.Func<NSubstitute.Core.CallInfo, int>[] { _ => CreateDefaultValue(), _ => 1 }")]
[InlineData("CreateReEntrantSubstitute(), new int[] { CreateDefaultValue(), 1 }", "_ => CreateReEntrantSubstitute(), new System.Func<NSubstitute.Core.CallInfo, int>[] { _ => CreateDefaultValue(), _ => 1 }")]
[InlineData("CreateReEntrantSubstitute(), new [] { CreateDefaultValue(), 1 }", "_ => CreateReEntrantSubstitute(), new System.Func<NSubstitute.Core.CallInfo<int>, int>[] { _ => CreateDefaultValue(), _ => 1 }")]
[InlineData("CreateReEntrantSubstitute(), new int[] { CreateDefaultValue(), 1 }", "_ => CreateReEntrantSubstitute(), new System.Func<NSubstitute.Core.CallInfo<int>, int>[] { _ => CreateDefaultValue(), _ => 1 }")]
[InlineData("returnThis: CreateReEntrantSubstitute()", "returnThis: _ => CreateReEntrantSubstitute()")]
[InlineData("returnThis: CreateReEntrantSubstitute(), returnThese: new [] { CreateDefaultValue(), 1 }", "returnThis: _ => CreateReEntrantSubstitute(), returnThese: new System.Func<NSubstitute.Core.CallInfo, int>[] { _ => CreateDefaultValue(), _ => 1 }")]
[InlineData("returnThis: CreateReEntrantSubstitute(), returnThese: new int[] { CreateDefaultValue(), 1 }", "returnThis: _ => CreateReEntrantSubstitute(), returnThese: new System.Func<NSubstitute.Core.CallInfo, int>[] { _ => CreateDefaultValue(), _ => 1 }")]
[InlineData("returnThis: CreateReEntrantSubstitute(), returnThese: new [] { CreateDefaultValue(), 1 }", "returnThis: _ => CreateReEntrantSubstitute(), returnThese: new System.Func<NSubstitute.Core.CallInfo<int>, int>[] { _ => CreateDefaultValue(), _ => 1 }")]
[InlineData("returnThis: CreateReEntrantSubstitute(), returnThese: new int[] { CreateDefaultValue(), 1 }", "returnThis: _ => CreateReEntrantSubstitute(), returnThese: new System.Func<NSubstitute.Core.CallInfo<int>, int>[] { _ => CreateDefaultValue(), _ => 1 }")]
public abstract Task ReplacesArgumentExpression_WithLambda(string arguments, string rewrittenArguments);

[Fact]
public abstract Task ReplacesArgumentExpression_WithLambdaWithReducedTypes_WhenGeneratingArrayParamsArgument();

[Fact]
public abstract Task ReplacesArgumentExpression_WithLambdaWithNonGenericCallInfo_WhenGeneratingArrayParamsArgument();
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Threading.Tasks;
using NSubstitute.Analyzers.Tests.Shared;

namespace NSubstitute.Analyzers.Tests.CSharp.CodeFixProviderTests.ReEntrantSetupCodeFixProviderTests
{
Expand Down Expand Up @@ -148,7 +150,7 @@ public interface IFoo
public void Test()
{
var secondSubstitute = Substitute.For<IFoo>();
secondSubstitute.Id.Returns(_ => CreateReEntrantSubstitute(), new Func<CallInfo, int>[] { _ => MyNamespace.FooTests.Value });
secondSubstitute.Id.Returns(_ => CreateReEntrantSubstitute(), new Func<CallInfo<int>, int>[] { _ => MyNamespace.FooTests.Value });
}
private int CreateReEntrantSubstitute()
Expand All @@ -161,5 +163,83 @@ private int CreateReEntrantSubstitute()
}";
await VerifyFix(oldSource, newSource);
}

public override async Task ReplacesArgumentExpression_WithLambdaWithNonGenericCallInfo_WhenGeneratingArrayParamsArgument()
{
var oldSource = @"using NSubstitute;
using NSubstitute.Core;
using System;
namespace MyNamespace
{
public class FooTests
{
private IFoo firstSubstitute = Substitute.For<IFoo>();
public static int Value { get; set; }
public FooTests()
{
firstSubstitute.Id.Returns(45);
}
public interface IFoo
{
int Id { get; }
}
public void Test()
{
var secondSubstitute = Substitute.For<IFoo>();
secondSubstitute.Id.Returns(CreateReEntrantSubstitute(), new[] { MyNamespace.FooTests.Value });
}
private int CreateReEntrantSubstitute()
{
var substitute = Substitute.For<IFoo>();
substitute.Id.Returns(1);
return 1;
}
}
}";

var newSource = @"using NSubstitute;
using NSubstitute.Core;
using System;
namespace MyNamespace
{
public class FooTests
{
private IFoo firstSubstitute = Substitute.For<IFoo>();
public static int Value { get; set; }
public FooTests()
{
firstSubstitute.Id.Returns(45);
}
public interface IFoo
{
int Id { get; }
}
public void Test()
{
var secondSubstitute = Substitute.For<IFoo>();
secondSubstitute.Id.Returns(_ => CreateReEntrantSubstitute(), new Func<CallInfo, int>[] { _ => MyNamespace.FooTests.Value });
}
private int CreateReEntrantSubstitute()
{
var substitute = Substitute.For<IFoo>();
substitute.Id.Returns(1);
return 1;
}
}
}";
await VerifyFix(oldSource, newSource, version: NSubstituteVersion.NSubstitute4_2_2);
}
}
}
Loading

0 comments on commit 25757a1

Please sign in to comment.