Skip to content

Commit f48250c

Browse files
committed
Add initial tests for AccessorDeclarationSyntaxExtensions
1 parent a043f15 commit f48250c

6 files changed

Lines changed: 70 additions & 38 deletions

File tree

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
2+
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
3+
4+
namespace StyleCop.Analyzers.Test.CSharp7.Lightup
5+
{
6+
using Microsoft.CodeAnalysis.CSharp;
7+
using StyleCop.Analyzers.Lightup;
8+
using Xunit;
9+
10+
public class AccessorDeclarationSyntaxExtensionsTests
11+
{
12+
[Fact]
13+
public void TestExpressionBody()
14+
{
15+
var accessorDeclarationSyntax = SyntaxFactory.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration)
16+
.WithExpressionBody(SyntaxFactory.ArrowExpressionClause(SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression)));
17+
Assert.Same(accessorDeclarationSyntax.ExpressionBody, AccessorDeclarationSyntaxExtensions.ExpressionBody(accessorDeclarationSyntax));
18+
}
19+
20+
[Fact]
21+
public void TestWithExpressionBody()
22+
{
23+
var accessorDeclarationSyntax = SyntaxFactory.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration);
24+
var expressionBody = SyntaxFactory.ArrowExpressionClause(SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression));
25+
var accessorWithBody = AccessorDeclarationSyntaxExtensions.WithExpressionBody(accessorDeclarationSyntax, expressionBody);
26+
Assert.Null(accessorDeclarationSyntax.ExpressionBody);
27+
Assert.NotNull(accessorWithBody.ExpressionBody);
28+
Assert.Equal(SyntaxKind.NullLiteralExpression, accessorWithBody.ExpressionBody.Expression.Kind());
29+
}
30+
}
31+
}

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/StyleCop.Analyzers.Test.CSharp7.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@
194194
</Reference>
195195
</ItemGroup>
196196
<ItemGroup>
197+
<Compile Include="Lightup\AccessorDeclarationSyntaxExtensionsTests.cs" />
197198
<Compile Include="Properties\AssemblyInfo.cs" />
198199
</ItemGroup>
199200
<ItemGroup>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
2+
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
3+
4+
namespace StyleCop.Analyzers.Test.Lightup
5+
{
6+
using System;
7+
using Microsoft.CodeAnalysis.CSharp;
8+
using StyleCop.Analyzers.Lightup;
9+
using Xunit;
10+
11+
public class AccessorDeclarationSyntaxExtensionsTests
12+
{
13+
[Fact]
14+
public void TestExpressionBody()
15+
{
16+
var accessorDeclarationSyntax = SyntaxFactory.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration);
17+
Assert.Null(AccessorDeclarationSyntaxExtensions.ExpressionBody(accessorDeclarationSyntax));
18+
}
19+
20+
[Fact]
21+
public void TestWithExpressionBody()
22+
{
23+
var accessorDeclarationSyntax = SyntaxFactory.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration);
24+
25+
// With default value is allowed
26+
var accessorWithDefaultBody = AccessorDeclarationSyntaxExtensions.WithExpressionBody(accessorDeclarationSyntax, null);
27+
Assert.Null(AccessorDeclarationSyntaxExtensions.ExpressionBody(accessorWithDefaultBody));
28+
29+
// Non-default throws an exception
30+
var expressionBody = SyntaxFactory.ArrowExpressionClause(SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression));
31+
Assert.Throws<NotSupportedException>(() => AccessorDeclarationSyntaxExtensions.WithExpressionBody(accessorDeclarationSyntax, expressionBody));
32+
}
33+
}
34+
}

StyleCop.Analyzers/StyleCop.Analyzers.Test/StyleCop.Analyzers.Test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@
235235
<Compile Include="LayoutRules\SA1518UnitTests.cs" />
236236
<Compile Include="LayoutRules\SA1519UnitTests.cs" />
237237
<Compile Include="LayoutRules\SA1520UnitTests.cs" />
238+
<Compile Include="Lightup\AccessorDeclarationSyntaxExtensionsTests.cs" />
238239
<Compile Include="LinqHelpers\SyntaxTriviaListEnumerableTests.cs" />
239240
<Compile Include="MaintainabilityRules\DebugMessagesUnitTestsBase.cs" />
240241
<Compile Include="MaintainabilityRules\FileMayOnlyContainTestBase.cs" />

StyleCop.Analyzers/StyleCop.Analyzers/Lightup/AccessorDeclarationSyntaxExtensions.cs

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44
namespace StyleCop.Analyzers.Lightup
55
{
66
using System;
7-
using System.Linq;
8-
using System.Linq.Expressions;
9-
using System.Reflection;
107
using Microsoft.CodeAnalysis.CSharp.Syntax;
118

129
internal static class AccessorDeclarationSyntaxExtensions
@@ -16,40 +13,8 @@ internal static class AccessorDeclarationSyntaxExtensions
1613

1714
static AccessorDeclarationSyntaxExtensions()
1815
{
19-
if (LightupHelpers.SupportsCSharp7)
20-
{
21-
{
22-
var expressionBodyProperty = typeof(AccessorDeclarationSyntax).GetTypeInfo().GetDeclaredProperty(nameof(ExpressionBody));
23-
var syntaxParameter = Expression.Parameter(typeof(AccessorDeclarationSyntax), "syntax");
24-
Expression<Func<AccessorDeclarationSyntax, ArrowExpressionClauseSyntax>> expression =
25-
Expression.Lambda<Func<AccessorDeclarationSyntax, ArrowExpressionClauseSyntax>>(
26-
Expression.Call(syntaxParameter, expressionBodyProperty.GetMethod),
27-
syntaxParameter);
28-
ExpressionBodyAccessor = expression.Compile();
29-
}
30-
31-
{
32-
var withExpressionBodyMethod = typeof(AccessorDeclarationSyntax).GetTypeInfo().GetDeclaredMethods(nameof(WithExpressionBody))
33-
.Single(method => method.GetParameters().Length == 1 && method.GetParameters()[0].ParameterType == typeof(ArrowExpressionClauseSyntax));
34-
var syntaxParameter = Expression.Parameter(typeof(AccessorDeclarationSyntax), "syntax");
35-
var expressionBodyParameter = Expression.Parameter(typeof(ArrowExpressionClauseSyntax), "expressionBody");
36-
Expression<Func<AccessorDeclarationSyntax, ArrowExpressionClauseSyntax, AccessorDeclarationSyntax>> expression =
37-
Expression.Lambda<Func<AccessorDeclarationSyntax, ArrowExpressionClauseSyntax, AccessorDeclarationSyntax>>(
38-
Expression.Call(syntaxParameter, withExpressionBodyMethod, expressionBodyParameter),
39-
syntaxParameter,
40-
expressionBodyParameter);
41-
WithExpressionBodyAccessor = expression.Compile();
42-
}
43-
}
44-
else
45-
{
46-
ExpressionBodyAccessor = syntax => null;
47-
WithExpressionBodyAccessor =
48-
(syntax, expressionBody) =>
49-
{
50-
throw new NotSupportedException("Expression-bodied accessors are only available in C# 7+.");
51-
};
52-
}
16+
ExpressionBodyAccessor = LightupHelpers.CreateSyntaxPropertyAccessor<AccessorDeclarationSyntax, ArrowExpressionClauseSyntax>(typeof(AccessorDeclarationSyntax), nameof(ExpressionBody));
17+
WithExpressionBodyAccessor = LightupHelpers.CreateSyntaxWithPropertyAccessor<AccessorDeclarationSyntax, ArrowExpressionClauseSyntax>(typeof(AccessorDeclarationSyntax), nameof(ExpressionBody));
5318
}
5419

5520
public static ArrowExpressionClauseSyntax ExpressionBody(this AccessorDeclarationSyntax syntax)

StyleCop.Analyzers/StyleCop.Analyzers/Lightup/LightupHelpers.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ internal static Func<TSyntax, TProperty, TSyntax> CreateSyntaxWithPropertyAccess
123123
}
124124

125125
var methodInfo = type.GetTypeInfo().GetDeclaredMethods("With" + propertyName)
126-
.Single(m => !m.IsStatic && m.GetParameters().Length == 1 && m.GetParameters()[0].GetType().Equals(property.PropertyType));
126+
.Single(m => !m.IsStatic && m.GetParameters().Length == 1 && m.GetParameters()[0].ParameterType.Equals(property.PropertyType));
127127

128128
var syntaxParameter = Expression.Parameter(typeof(TSyntax), "syntax");
129129
var valueParameter = Expression.Parameter(typeof(TProperty), methodInfo.GetParameters()[0].Name);

0 commit comments

Comments
 (0)