Skip to content

Commit

Permalink
Added test for invalid construction and local function
Browse files Browse the repository at this point in the history
  • Loading branch information
vweijsters committed Mar 1, 2019
1 parent 77ee36f commit ffe11e1
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ private static SyntaxNode ReplaceWithLambda(SemanticModel semanticModel, Anonymo
case SyntaxKind.ArrowExpressionClause:
case SyntaxKind.ReturnStatement:
argumentList = GetMemberReturnTypeArgumentList(semanticModel, anonymousMethod);
if (argumentList.IsEmpty)
{
return null;
}

break;
}

Expand Down Expand Up @@ -204,9 +209,8 @@ private static ImmutableArray<string> GetEqualsArgumentList(SemanticModel semant
private static ImmutableArray<string> GetMemberReturnTypeArgumentList(SemanticModel semanticModel, AnonymousMethodExpressionSyntax anonymousMethod)
{
var enclosingSymbol = semanticModel.GetEnclosingSymbol(anonymousMethod.Parent.SpanStart);
var returnType = (INamedTypeSymbol)((IMethodSymbol)enclosingSymbol).ReturnType;

return returnType.DelegateInvokeMethod.Parameters.Select(ps => ps.Name).ToImmutableArray();
var returnType = ((IMethodSymbol)enclosingSymbol).ReturnType as INamedTypeSymbol;
return (returnType == null) ? ImmutableArray<string>.Empty : returnType.DelegateInvokeMethod.Parameters.Select(ps => ps.Name).ToImmutableArray();
}

private static List<ParameterSyntax> GenerateUniqueParameterNames(SemanticModel semanticModel, AnonymousMethodExpressionSyntax anonymousMethod, ImmutableArray<string> argumentNames)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,47 @@

namespace StyleCop.Analyzers.Test.CSharp7.ReadabilityRules
{
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Testing;
using StyleCop.Analyzers.Test.ReadabilityRules;
using Xunit;
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
StyleCop.Analyzers.ReadabilityRules.SA1130UseLambdaSyntax,
StyleCop.Analyzers.ReadabilityRules.SA1130CodeFixProvider>;

public class SA1130CSharp7UnitTests : SA1130UnitTests
{
[Fact]
[WorkItem(2902, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2902")]
public async Task VerifyLocalFunctionAsync()
{
var testCode = @"using System;
public class TestClass
{
public void TestMethod()
{
EventHandler LocalTestFunction() => delegate { };
}
}
";

var fixedCode = @"using System;
public class TestClass
{
public void TestMethod()
{
EventHandler LocalTestFunction() => (sender, e) => { };
}
}
";

DiagnosticResult[] expected =
{
Diagnostic().WithLocation(6, 45),
};

await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -812,5 +812,33 @@ public class TestClass

await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
}

[Fact]
[WorkItem(2902, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2902")]
public async Task VerifyInvalidCodeConstructionsAsync()
{
var testCode = @"using System;
public class TestClass
{
public static EventHandler[] TestMethod() => delegate { };
}
";

DiagnosticResult[] expected =
{
Diagnostic().WithSpan(4, 50, 4, 58),
DiagnosticResult.CompilerError("CS1660").WithMessage("Cannot convert anonymous method to type 'EventHandler[]' because it is not a delegate type").WithSpan(4, 50, 4, 62),
};

var test = new CSharpTest
{
TestCode = testCode,
FixedCode = testCode,
};

test.ExpectedDiagnostics.AddRange(expected);
test.RemainingDiagnostics.AddRange(expected);
await test.RunAsync(CancellationToken.None).ConfigureAwait(false);
}
}
}

0 comments on commit ffe11e1

Please sign in to comment.