Skip to content

Commit 3db5ad0

Browse files
authored
Merge pull request #2439 from bjornhellander/Issue2384_AddTestsForLocalFunctionStatementSyntaxWrapper
Added tests for LocalFunctionStatementSyntaxWrapper
2 parents 9abfa77 + 422b331 commit 3db5ad0

4 files changed

Lines changed: 284 additions & 0 deletions

File tree

Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
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 System;
7+
using System.Collections.Immutable;
8+
using Microsoft.CodeAnalysis;
9+
using Microsoft.CodeAnalysis.CSharp;
10+
using Microsoft.CodeAnalysis.CSharp.Syntax;
11+
using StyleCop.Analyzers.Lightup;
12+
using Xunit;
13+
14+
public class LocalFunctionStatementSyntaxWrapperTests
15+
{
16+
[Fact]
17+
public void TestNull()
18+
{
19+
var syntaxNode = default(SyntaxNode);
20+
var wrapper = (LocalFunctionStatementSyntaxWrapper)syntaxNode;
21+
Assert.Null(wrapper.SyntaxNode);
22+
Assert.Throws<NullReferenceException>(() => wrapper.Modifiers);
23+
Assert.Throws<NullReferenceException>(() => wrapper.ReturnType);
24+
Assert.Throws<NullReferenceException>(() => wrapper.Identifier);
25+
Assert.Throws<NullReferenceException>(() => wrapper.TypeParameterList);
26+
Assert.Throws<NullReferenceException>(() => wrapper.ParameterList);
27+
Assert.Throws<NullReferenceException>(() => wrapper.ConstraintClauses);
28+
Assert.Throws<NullReferenceException>(() => wrapper.Body);
29+
Assert.Throws<NullReferenceException>(() => wrapper.ExpressionBody);
30+
Assert.Throws<NullReferenceException>(() => wrapper.SemicolonToken);
31+
Assert.Throws<NullReferenceException>(() => wrapper.WithModifiers(SyntaxFactory.TokenList()));
32+
Assert.Throws<NullReferenceException>(() => wrapper.WithReturnType(SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.IntKeyword))));
33+
Assert.Throws<NullReferenceException>(() => wrapper.WithIdentifier(SyntaxFactory.Identifier("Identifier")));
34+
Assert.Throws<NullReferenceException>(() => wrapper.WithTypeParameterList(SyntaxFactory.TypeParameterList()));
35+
Assert.Throws<NullReferenceException>(() => wrapper.WithParameterList(SyntaxFactory.ParameterList()));
36+
Assert.Throws<NullReferenceException>(() => wrapper.WithConstraintClauses(SyntaxFactory.List<TypeParameterConstraintClauseSyntax>()));
37+
Assert.Throws<NullReferenceException>(() => wrapper.WithBody(SyntaxFactory.Block()));
38+
Assert.Throws<NullReferenceException>(() => wrapper.WithExpressionBody(SyntaxFactory.ArrowExpressionClause(SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(0)))));
39+
Assert.Throws<NullReferenceException>(() => wrapper.WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken)));
40+
Assert.Throws<NullReferenceException>(() => wrapper.AddModifiers());
41+
Assert.Throws<NullReferenceException>(() => wrapper.AddTypeParameterListParameters());
42+
Assert.Throws<NullReferenceException>(() => wrapper.AddParameterListParameters());
43+
Assert.Throws<NullReferenceException>(() => wrapper.AddConstraintClauses());
44+
Assert.Throws<NullReferenceException>(() => wrapper.AddBodyStatements());
45+
}
46+
47+
[Fact]
48+
public void TestProperties()
49+
{
50+
var syntaxNode = this.CreateLocalFunctionStatement();
51+
Assert.True(syntaxNode.IsKind(SyntaxKind.LocalFunctionStatement));
52+
Assert.True(syntaxNode.IsKind(SyntaxKindEx.LocalFunctionStatement));
53+
54+
var wrapper = (LocalFunctionStatementSyntaxWrapper)syntaxNode;
55+
Assert.Same(syntaxNode, wrapper.SyntaxNode);
56+
Assert.Equal(syntaxNode.Modifiers, wrapper.Modifiers); // This is a struct, so we can't use Same()
57+
Assert.Same(syntaxNode.ReturnType, wrapper.ReturnType);
58+
Assert.Equal(syntaxNode.Identifier, wrapper.Identifier); // This is a struct, so we can't use Same()
59+
Assert.Same(syntaxNode.TypeParameterList, wrapper.TypeParameterList);
60+
Assert.Same(syntaxNode.ParameterList, wrapper.ParameterList);
61+
Assert.Equal(syntaxNode.ConstraintClauses, wrapper.ConstraintClauses); // This is a struct, so we can't use Same()
62+
Assert.Same(syntaxNode.Body, wrapper.Body);
63+
Assert.Same(syntaxNode.ExpressionBody, wrapper.ExpressionBody);
64+
Assert.True(syntaxNode.SemicolonToken.IsEquivalentTo(wrapper.SemicolonToken));
65+
66+
var newModifiers = SyntaxFactory.TokenList();
67+
var wrapperWithModifiedModifiers = wrapper.WithModifiers(newModifiers);
68+
Assert.NotNull(wrapperWithModifiedModifiers.SyntaxNode);
69+
Assert.NotSame(syntaxNode.Modifiers, wrapperWithModifiedModifiers.Modifiers);
70+
Assert.Equal(0, wrapperWithModifiedModifiers.Modifiers.Count);
71+
72+
var newReturnType = SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.BoolKeyword));
73+
var wrapperWithModifiedReturnType = wrapper.WithReturnType(newReturnType);
74+
Assert.NotNull(wrapperWithModifiedReturnType.SyntaxNode);
75+
Assert.NotSame(syntaxNode.ReturnType, wrapperWithModifiedReturnType.ReturnType);
76+
Assert.Equal(SyntaxKind.PredefinedType, wrapperWithModifiedReturnType.ReturnType.Kind());
77+
78+
var newIdentifier = SyntaxFactory.Identifier("NewIdentifier");
79+
var wrapperWithModifiedIdentifier = wrapper.WithIdentifier(newIdentifier);
80+
Assert.NotNull(wrapperWithModifiedIdentifier.SyntaxNode);
81+
Assert.NotSame(syntaxNode.Identifier, wrapperWithModifiedIdentifier.Identifier);
82+
Assert.Equal(SyntaxKind.IdentifierToken, wrapperWithModifiedIdentifier.Identifier.Kind());
83+
Assert.Equal("NewIdentifier", wrapperWithModifiedIdentifier.Identifier.Text);
84+
85+
var newTypeParameterList = SyntaxFactory.TypeParameterList();
86+
var wrapperWithModifiedTypeParameterList = wrapper.WithTypeParameterList(newTypeParameterList);
87+
Assert.NotNull(wrapperWithModifiedTypeParameterList.SyntaxNode);
88+
Assert.NotSame(syntaxNode.TypeParameterList, wrapperWithModifiedTypeParameterList.TypeParameterList);
89+
Assert.Equal(0, wrapperWithModifiedTypeParameterList.TypeParameterList.Parameters.Count);
90+
91+
var newParameterList = SyntaxFactory.ParameterList();
92+
var wrapperWithModifiedParameterList = wrapper.WithParameterList(newParameterList);
93+
Assert.NotNull(wrapperWithModifiedParameterList.SyntaxNode);
94+
Assert.NotSame(syntaxNode.ParameterList, wrapperWithModifiedParameterList.ParameterList);
95+
Assert.Equal(0, wrapperWithModifiedParameterList.ParameterList.Parameters.Count);
96+
97+
var newConstraintClauses = SyntaxFactory.List<TypeParameterConstraintClauseSyntax>();
98+
var wrapperWithModifiedConstraintClauses = wrapper.WithConstraintClauses(newConstraintClauses);
99+
Assert.NotNull(wrapperWithModifiedConstraintClauses.SyntaxNode);
100+
Assert.NotSame(syntaxNode.ConstraintClauses, wrapperWithModifiedConstraintClauses.ConstraintClauses);
101+
Assert.Equal(0, wrapperWithModifiedConstraintClauses.ConstraintClauses.Count);
102+
103+
var newBody = SyntaxFactory.Block();
104+
var wrapperWithModifiedBody = wrapper.WithBody(newBody);
105+
Assert.NotNull(wrapperWithModifiedBody.SyntaxNode);
106+
Assert.Equal(SyntaxKind.Block, wrapperWithModifiedBody.Body.Kind());
107+
Assert.Equal(0, wrapperWithModifiedBody.Body.Statements.Count);
108+
109+
var newExpressionBody = SyntaxFactory.ArrowExpressionClause(SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression));
110+
var wrapperWithModifiedExpressionBody = wrapper.WithExpressionBody(newExpressionBody);
111+
Assert.NotNull(wrapperWithModifiedExpressionBody.SyntaxNode);
112+
Assert.Equal(SyntaxKind.ArrowExpressionClause, wrapperWithModifiedExpressionBody.ExpressionBody.Kind());
113+
Assert.Equal(SyntaxKind.NullLiteralExpression, wrapperWithModifiedExpressionBody.ExpressionBody.Expression.Kind());
114+
115+
var newSemicolonToken = SyntaxFactory.Token(SyntaxKind.SemicolonToken).WithLeadingTrivia(SyntaxFactory.Space);
116+
var wrapperWithModifiedSemicolonToken = wrapper.WithSemicolonToken(newSemicolonToken);
117+
Assert.NotNull(wrapperWithModifiedSemicolonToken.SyntaxNode);
118+
Assert.Equal(1, wrapperWithModifiedSemicolonToken.SemicolonToken.LeadingTrivia.Count);
119+
Assert.Equal(" ", wrapperWithModifiedSemicolonToken.SemicolonToken.LeadingTrivia.ToString());
120+
121+
var addedModifiers = new SyntaxToken[] { SyntaxFactory.Token(SyntaxKind.AsyncKeyword) };
122+
var wrapperWithAddedModifiers = wrapper.AddModifiers(addedModifiers);
123+
Assert.NotNull(wrapperWithAddedModifiers.SyntaxNode);
124+
Assert.NotSame(syntaxNode.Modifiers, wrapperWithAddedModifiers.Modifiers);
125+
Assert.Equal(2, wrapperWithAddedModifiers.Modifiers.Count);
126+
Assert.Equal(SyntaxKind.PrivateKeyword, wrapperWithAddedModifiers.Modifiers[0].Kind());
127+
Assert.Equal(SyntaxKind.AsyncKeyword, wrapperWithAddedModifiers.Modifiers[1].Kind());
128+
129+
var addedTypeParameterList = new TypeParameterSyntax[] { SyntaxFactory.TypeParameter("T2") };
130+
var wrapperWithAddedTypeParameterList = wrapper.AddTypeParameterListParameters(addedTypeParameterList);
131+
Assert.NotNull(wrapperWithAddedTypeParameterList.SyntaxNode);
132+
Assert.NotSame(syntaxNode.TypeParameterList, wrapperWithAddedTypeParameterList.TypeParameterList);
133+
Assert.Equal(2, wrapperWithAddedTypeParameterList.TypeParameterList.Parameters.Count);
134+
Assert.Equal("T1", wrapperWithAddedTypeParameterList.TypeParameterList.Parameters[0].Identifier.Text);
135+
Assert.Equal("T2", wrapperWithAddedTypeParameterList.TypeParameterList.Parameters[1].Identifier.Text);
136+
137+
var addedParameterList = new ParameterSyntax[] { SyntaxFactory.Parameter(SyntaxFactory.Identifier("param2")) };
138+
var wrapperWithAddedParameterList = wrapper.AddParameterListParameters(addedParameterList);
139+
Assert.NotNull(wrapperWithAddedParameterList.SyntaxNode);
140+
Assert.NotSame(syntaxNode.ParameterList, wrapperWithAddedParameterList.ParameterList);
141+
Assert.Equal(2, wrapperWithAddedParameterList.ParameterList.Parameters.Count);
142+
Assert.Equal("param1", wrapperWithAddedParameterList.ParameterList.Parameters[0].Identifier.Text);
143+
Assert.Equal("param2", wrapperWithAddedParameterList.ParameterList.Parameters[1].Identifier.Text);
144+
145+
var addedConstraintClauses = new TypeParameterConstraintClauseSyntax[] { SyntaxFactory.TypeParameterConstraintClause(SyntaxFactory.IdentifierName("constraint2")) };
146+
var wrapperWithAddedConstraintClauses = wrapper.AddConstraintClauses(addedConstraintClauses);
147+
Assert.NotNull(wrapperWithAddedConstraintClauses.SyntaxNode);
148+
Assert.NotSame(syntaxNode.ConstraintClauses, wrapperWithAddedConstraintClauses.ConstraintClauses);
149+
Assert.Equal(2, wrapperWithAddedConstraintClauses.ConstraintClauses.Count);
150+
Assert.Equal("constraint1", wrapperWithAddedConstraintClauses.ConstraintClauses[0].Name.Identifier.Text);
151+
Assert.Equal("constraint2", wrapperWithAddedConstraintClauses.ConstraintClauses[1].Name.Identifier.Text);
152+
153+
var addedBodyStatements = new StatementSyntax[] { SyntaxFactory.ReturnStatement() };
154+
var wrapperWithAddedBodyStatements = wrapper.AddBodyStatements(addedBodyStatements);
155+
Assert.NotNull(wrapperWithAddedBodyStatements.SyntaxNode);
156+
Assert.Equal(SyntaxKind.Block, wrapperWithAddedBodyStatements.Body.Kind());
157+
Assert.Equal(2, wrapperWithAddedBodyStatements.Body.Statements.Count);
158+
Assert.Equal(SyntaxKind.BreakStatement, wrapperWithAddedBodyStatements.Body.Statements[0].Kind());
159+
Assert.Equal(SyntaxKind.ReturnStatement, wrapperWithAddedBodyStatements.Body.Statements[1].Kind());
160+
}
161+
162+
[Fact]
163+
public void TestIsInstance()
164+
{
165+
Assert.False(LocalFunctionStatementSyntaxWrapper.IsInstance(null));
166+
Assert.False(LocalFunctionStatementSyntaxWrapper.IsInstance(SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression)));
167+
168+
var syntaxNode = this.CreateLocalFunctionStatement();
169+
Assert.True(LocalFunctionStatementSyntaxWrapper.IsInstance(syntaxNode));
170+
}
171+
172+
[Fact]
173+
public void TestConversionsNull()
174+
{
175+
var syntaxNode = default(SyntaxNode);
176+
var wrapper = (LocalFunctionStatementSyntaxWrapper)syntaxNode;
177+
178+
StatementSyntax syntax = wrapper;
179+
Assert.Null(syntax);
180+
}
181+
182+
[Fact]
183+
public void TestConversions()
184+
{
185+
var syntaxNode = this.CreateLocalFunctionStatement();
186+
var wrapper = (LocalFunctionStatementSyntaxWrapper)syntaxNode;
187+
188+
StatementSyntax syntax = wrapper;
189+
Assert.Same(syntaxNode, syntax);
190+
}
191+
192+
[Fact]
193+
public void TestInvalidConversion()
194+
{
195+
var syntaxNode = SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression);
196+
Assert.Throws<InvalidCastException>(() => (LocalFunctionStatementSyntaxWrapper)syntaxNode);
197+
}
198+
199+
private LocalFunctionStatementSyntax CreateLocalFunctionStatement()
200+
{
201+
return SyntaxFactory.LocalFunctionStatement(
202+
modifiers: SyntaxFactory.TokenList(SyntaxFactory.TokenList(SyntaxFactory.Token(SyntaxKind.PrivateKeyword))),
203+
returnType: SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.IntKeyword)),
204+
identifier: SyntaxFactory.Identifier("Identifier"),
205+
typeParameterList: SyntaxFactory.TypeParameterList(SyntaxFactory.SeparatedList(ImmutableArray.Create(SyntaxFactory.TypeParameter("T1")))),
206+
parameterList: SyntaxFactory.ParameterList(SyntaxFactory.SeparatedList(ImmutableArray.Create(SyntaxFactory.Parameter(SyntaxFactory.Identifier("param1"))))),
207+
constraintClauses: SyntaxFactory.List<TypeParameterConstraintClauseSyntax>(ImmutableArray.Create(SyntaxFactory.TypeParameterConstraintClause(SyntaxFactory.IdentifierName("constraint1")))),
208+
body: SyntaxFactory.Block(SyntaxFactory.BreakStatement()),
209+
expressionBody: SyntaxFactory.ArrowExpressionClause(SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(0))));
210+
}
211+
}
212+
}

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
@@ -284,6 +284,7 @@
284284
<Compile Include="Lightup\RefTypeSyntaxWrapperTests.cs" />
285285
<Compile Include="Lightup\SingleVariableDesignationSyntaxWrapperTests.cs" />
286286
<Compile Include="Lightup\SyntaxWrapperTests.cs" />
287+
<Compile Include="Lightup\LocalFunctionStatementSyntaxWrapperTests.cs" />
287288
<Compile Include="Lightup\ThrowExpressionSyntaxWrapperTests.cs" />
288289
<Compile Include="Lightup\TupleElementSyntaxWrapperTests.cs" />
289290
<Compile Include="Lightup\TupleExpressionSyntaxWrapperTests.cs" />
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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;
8+
using Microsoft.CodeAnalysis.CSharp;
9+
using Microsoft.CodeAnalysis.CSharp.Syntax;
10+
using StyleCop.Analyzers.Lightup;
11+
using Xunit;
12+
13+
public class LocalFunctionStatementSyntaxWrapperTests
14+
{
15+
[Fact]
16+
public void TestNull()
17+
{
18+
var syntaxNode = default(SyntaxNode);
19+
var wrapper = (LocalFunctionStatementSyntaxWrapper)syntaxNode;
20+
Assert.Null(wrapper.SyntaxNode);
21+
Assert.Throws<NullReferenceException>(() => wrapper.Modifiers);
22+
Assert.Throws<NullReferenceException>(() => wrapper.ReturnType);
23+
Assert.Throws<NullReferenceException>(() => wrapper.Identifier);
24+
Assert.Throws<NullReferenceException>(() => wrapper.TypeParameterList);
25+
Assert.Throws<NullReferenceException>(() => wrapper.ParameterList);
26+
Assert.Throws<NullReferenceException>(() => wrapper.ConstraintClauses);
27+
Assert.Throws<NullReferenceException>(() => wrapper.Body);
28+
Assert.Throws<NullReferenceException>(() => wrapper.ExpressionBody);
29+
Assert.Throws<NullReferenceException>(() => wrapper.SemicolonToken);
30+
Assert.Throws<NullReferenceException>(() => wrapper.WithModifiers(SyntaxFactory.TokenList()));
31+
Assert.Throws<NullReferenceException>(() => wrapper.WithReturnType(SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.IntKeyword))));
32+
Assert.Throws<NullReferenceException>(() => wrapper.WithIdentifier(SyntaxFactory.Identifier("Identifier")));
33+
Assert.Throws<NullReferenceException>(() => wrapper.WithTypeParameterList(SyntaxFactory.TypeParameterList()));
34+
Assert.Throws<NullReferenceException>(() => wrapper.WithParameterList(SyntaxFactory.ParameterList()));
35+
Assert.Throws<NullReferenceException>(() => wrapper.WithConstraintClauses(SyntaxFactory.List<TypeParameterConstraintClauseSyntax>()));
36+
Assert.Throws<NullReferenceException>(() => wrapper.WithBody(SyntaxFactory.Block()));
37+
Assert.Throws<NullReferenceException>(() => wrapper.WithExpressionBody(SyntaxFactory.ArrowExpressionClause(SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(0)))));
38+
Assert.Throws<NullReferenceException>(() => wrapper.WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken)));
39+
Assert.Throws<NullReferenceException>(() => wrapper.AddModifiers());
40+
Assert.Throws<NullReferenceException>(() => wrapper.AddTypeParameterListParameters());
41+
Assert.Throws<NullReferenceException>(() => wrapper.AddParameterListParameters());
42+
Assert.Throws<NullReferenceException>(() => wrapper.AddConstraintClauses());
43+
Assert.Throws<NullReferenceException>(() => wrapper.AddBodyStatements());
44+
}
45+
46+
[Fact]
47+
public void TestIsInstance()
48+
{
49+
Assert.False(LocalFunctionStatementSyntaxWrapper.IsInstance(null));
50+
Assert.False(LocalFunctionStatementSyntaxWrapper.IsInstance(SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression)));
51+
}
52+
53+
[Fact]
54+
public void TestConversionsNull()
55+
{
56+
var syntaxNode = default(SyntaxNode);
57+
var wrapper = (LocalFunctionStatementSyntaxWrapper)syntaxNode;
58+
59+
StatementSyntax syntax = wrapper;
60+
Assert.Null(syntax);
61+
}
62+
63+
[Fact]
64+
public void TestInvalidConversion()
65+
{
66+
var syntaxNode = SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression);
67+
Assert.Throws<InvalidCastException>(() => (LocalFunctionStatementSyntaxWrapper)syntaxNode);
68+
}
69+
}
70+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@
267267
<Compile Include="Lightup\SeparatedSyntaxListWrapperTestBase.cs" />
268268
<Compile Include="Lightup\SingleVariableDesignationSyntaxWrapperTests.cs" />
269269
<Compile Include="Lightup\SyntaxWrapperTests.cs" />
270+
<Compile Include="Lightup\LocalFunctionStatementSyntaxWrapperTests.cs" />
270271
<Compile Include="Lightup\ThrowExpressionSyntaxWrapperTests.cs" />
271272
<Compile Include="Lightup\TupleElementSyntaxWrapperTests.cs" />
272273
<Compile Include="Lightup\TupleExpressionSyntaxWrapperTests.cs" />

0 commit comments

Comments
 (0)