Skip to content

Commit d87b921

Browse files
committed
Add tests for SyntaxWrapper<T> and LightupHelpers
1 parent ed8974f commit d87b921

6 files changed

Lines changed: 186 additions & 0 deletions

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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 StyleCop.Analyzers.Lightup;
7+
using StyleCop.Analyzers.Test.Lightup;
8+
9+
/// <summary>
10+
/// This class tests edge case behavior of <see cref="LightupHelpers"/> in Roslyn 2+. It extends
11+
/// <see cref="LightupHelpersTests"/> since the tests defined there are valid in both environments without
12+
/// alteration.
13+
/// </summary>
14+
public class LightupHelpersTestsCSharp7 : LightupHelpersTests
15+
{
16+
}
17+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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;
7+
using Microsoft.CodeAnalysis.CSharp;
8+
using Microsoft.CodeAnalysis.CSharp.Syntax;
9+
using StyleCop.Analyzers.Lightup;
10+
using Xunit;
11+
12+
public class SyntaxWrapperTests
13+
{
14+
[Fact]
15+
public void TestWrapSyntaxNode()
16+
{
17+
SyntaxNode syntaxNode = SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression);
18+
19+
Assert.Same(syntaxNode, SyntaxWrapper<LiteralExpressionSyntax>.Default.Wrap(syntaxNode));
20+
Assert.Same(syntaxNode, SyntaxWrapper<LiteralExpressionSyntax>.Default.Unwrap((LiteralExpressionSyntax)syntaxNode));
21+
}
22+
23+
[Fact]
24+
public void TestWrapSyntaxWrapperNode()
25+
{
26+
var syntaxNode = SyntaxFactory.DiscardDesignation();
27+
var syntaxWrapper = SyntaxWrapper<DiscardDesignationSyntaxWrapper>.Default;
28+
29+
Assert.Same(syntaxNode, syntaxWrapper.Wrap(syntaxNode).SyntaxNode);
30+
Assert.Same(syntaxNode, syntaxWrapper.Unwrap((DiscardDesignationSyntaxWrapper)syntaxNode));
31+
}
32+
}
33+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,11 @@
201201
<Compile Include="Lightup\DeclarationPatternSyntaxWrapperTests.cs" />
202202
<Compile Include="Lightup\DestructorDeclarationSyntaxExtensionsTests.cs" />
203203
<Compile Include="Lightup\DiscardDesignationSyntaxWrapperTests.cs" />
204+
<Compile Include="Lightup\LightupHelpersTestsCSharp7.cs" />
204205
<Compile Include="Lightup\ParenthesizedVariableDesignationSyntaxWrapperTests.cs" />
205206
<Compile Include="Lightup\PatternSyntaxWrapperTests.cs" />
206207
<Compile Include="Lightup\SingleVariableDesignationSyntaxWrapperTests.cs" />
208+
<Compile Include="Lightup\SyntaxWrapperTests.cs" />
207209
<Compile Include="Lightup\VariableDesignationSyntaxWrapperTests.cs" />
208210
<Compile Include="Properties\AssemblyInfo.cs" />
209211
</ItemGroup>
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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 LightupHelpersTests
14+
{
15+
[Fact]
16+
public void TestCanWrapNullNode()
17+
{
18+
Assert.True(LightupHelpers.CanWrapNode(null, typeof(PatternSyntaxWrapper)));
19+
}
20+
21+
[Fact]
22+
public void TestCanAccessNonExistentProperty()
23+
{
24+
var propertyAccessor = LightupHelpers.CreateSyntaxPropertyAccessor<SyntaxNode, object>(typeof(SyntaxNode), "NonExistentProperty");
25+
Assert.NotNull(propertyAccessor);
26+
Assert.Null(propertyAccessor(SyntaxFactory.AccessorList()));
27+
Assert.Throws<NullReferenceException>(() => propertyAccessor(null));
28+
29+
var withPropertyAccessor = LightupHelpers.CreateSyntaxWithPropertyAccessor<SyntaxNode, object>(typeof(SyntaxNode), "NonExistentProperty");
30+
Assert.NotNull(withPropertyAccessor);
31+
Assert.NotNull(withPropertyAccessor(SyntaxFactory.AccessorList(), null));
32+
Assert.ThrowsAny<NotSupportedException>(() => withPropertyAccessor(SyntaxFactory.AccessorList(), new object()));
33+
Assert.Throws<NullReferenceException>(() => withPropertyAccessor(null, new object()));
34+
35+
var separatedListPropertyAccessor = LightupHelpers.CreateSeparatedSyntaxListPropertyAccessor<SyntaxNode, SyntaxNode>(typeof(SyntaxNode), "NonExistentProperty");
36+
Assert.NotNull(separatedListPropertyAccessor);
37+
Assert.NotNull(separatedListPropertyAccessor(SyntaxFactory.AccessorList()));
38+
Assert.Throws<NullReferenceException>(() => separatedListPropertyAccessor(null));
39+
40+
var separatedListWithPropertyAccessor = LightupHelpers.CreateSeparatedSyntaxListWithPropertyAccessor<SyntaxNode, SyntaxNode>(typeof(SyntaxNode), "NonExistentProperty");
41+
Assert.NotNull(separatedListWithPropertyAccessor);
42+
Assert.NotNull(separatedListWithPropertyAccessor(SyntaxFactory.AccessorList(), null));
43+
Assert.ThrowsAny<NotSupportedException>(() => separatedListWithPropertyAccessor(SyntaxFactory.AccessorList(), new SeparatedSyntaxListWrapper<SyntaxNode>.AutoWrapSeparatedSyntaxList<LiteralExpressionSyntax>(SyntaxFactory.SingletonSeparatedList(SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression)))));
44+
Assert.Throws<NullReferenceException>(() => separatedListWithPropertyAccessor(null, new SeparatedSyntaxListWrapper<SyntaxNode>.UnsupportedSyntaxList()));
45+
}
46+
47+
[Fact]
48+
public void TestCreateSyntaxPropertyAccessor()
49+
{
50+
// The call *should* have been made with the first generic argument set to `BaseMethodDeclarationSyntax`
51+
// instead of `MethodDeclarationSyntax`.
52+
Assert.ThrowsAny<InvalidOperationException>(() => LightupHelpers.CreateSyntaxPropertyAccessor<MethodDeclarationSyntax, BlockSyntax>(typeof(BaseMethodDeclarationSyntax), nameof(BaseMethodDeclarationSyntax.Body)));
53+
54+
// The call *should* have been made with the second generic argument set to `ArrowExpressionClauseSyntax`
55+
// instead of `BlockSyntax`.
56+
Assert.ThrowsAny<InvalidOperationException>(() => LightupHelpers.CreateSyntaxPropertyAccessor<MethodDeclarationSyntax, BlockSyntax>(typeof(MethodDeclarationSyntax), nameof(MethodDeclarationSyntax.ExpressionBody)));
57+
}
58+
59+
[Fact]
60+
public void TestCreateSeparatedSyntaxListPropertyAccessor()
61+
{
62+
// The call works for `SeparatedSyntaxList<T>`, not work for `SyntaxList<T>`.
63+
Assert.ThrowsAny<InvalidOperationException>(() => LightupHelpers.CreateSeparatedSyntaxListPropertyAccessor<BlockSyntax, StatementSyntax>(typeof(BlockSyntax), nameof(BlockSyntax.Statements)));
64+
65+
// The call *should* have been made with the first generic argument set to `BaseParameterListSyntax`
66+
// instead of `ParameterListSyntax`.
67+
Assert.ThrowsAny<InvalidOperationException>(() => LightupHelpers.CreateSeparatedSyntaxListPropertyAccessor<ParameterListSyntax, ParameterSyntax>(typeof(BaseParameterListSyntax), nameof(BaseParameterListSyntax.Parameters)));
68+
}
69+
70+
[Fact(Skip = "Validation is not currently performed for the second argument.")]
71+
public void TestCreateSeparatedSyntaxListPropertyAccessorValidateElementType()
72+
{
73+
// The call *should* have been made with the second generic argument set to `ParameterSyntax`
74+
// instead of `ArgumentSyntax`.
75+
Assert.ThrowsAny<InvalidOperationException>(() => LightupHelpers.CreateSeparatedSyntaxListPropertyAccessor<BaseParameterListSyntax, ArgumentSyntax>(typeof(BaseParameterListSyntax), nameof(BaseParameterListSyntax.Parameters)));
76+
}
77+
78+
[Fact]
79+
public void TestCreateSyntaxWithPropertyAccessor()
80+
{
81+
// The call *should* have been made with the first generic argument set to `BaseMethodDeclarationSyntax`
82+
// instead of `MethodDeclarationSyntax`.
83+
Assert.ThrowsAny<InvalidOperationException>(() => LightupHelpers.CreateSyntaxWithPropertyAccessor<MethodDeclarationSyntax, BlockSyntax>(typeof(BaseMethodDeclarationSyntax), nameof(BaseMethodDeclarationSyntax.Body)));
84+
85+
// The call *should* have been made with the second generic argument set to `ArrowExpressionClauseSyntax`
86+
// instead of `BlockSyntax`.
87+
Assert.ThrowsAny<InvalidOperationException>(() => LightupHelpers.CreateSyntaxWithPropertyAccessor<MethodDeclarationSyntax, BlockSyntax>(typeof(MethodDeclarationSyntax), nameof(MethodDeclarationSyntax.ExpressionBody)));
88+
}
89+
90+
[Fact]
91+
public void TestCreateSeparatedSyntaxListWithPropertyAccessor()
92+
{
93+
// The call works for `SeparatedSyntaxList<T>`, not work for `SyntaxList<T>`.
94+
Assert.ThrowsAny<InvalidOperationException>(() => LightupHelpers.CreateSeparatedSyntaxListWithPropertyAccessor<BlockSyntax, StatementSyntax>(typeof(BlockSyntax), nameof(BlockSyntax.Statements)));
95+
96+
// The call *should* have been made with the first generic argument set to `BaseParameterListSyntax`
97+
// instead of `ParameterListSyntax`.
98+
Assert.ThrowsAny<InvalidOperationException>(() => LightupHelpers.CreateSeparatedSyntaxListWithPropertyAccessor<ParameterListSyntax, ParameterSyntax>(typeof(BaseParameterListSyntax), nameof(BaseParameterListSyntax.Parameters)));
99+
}
100+
101+
[Fact(Skip = "Validation is not currently performed for the second argument.")]
102+
public void TestCreateSeparatedSyntaxListWithPropertyAccessorValidateElementType()
103+
{
104+
// The call *should* have been made with the second generic argument set to `ParameterSyntax`
105+
// instead of `ArgumentSyntax`.
106+
Assert.ThrowsAny<InvalidOperationException>(() => LightupHelpers.CreateSeparatedSyntaxListWithPropertyAccessor<BaseParameterListSyntax, ArgumentSyntax>(typeof(BaseParameterListSyntax), nameof(BaseParameterListSyntax.Parameters)));
107+
}
108+
}
109+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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 Microsoft.CodeAnalysis;
7+
using Microsoft.CodeAnalysis.CSharp;
8+
using Microsoft.CodeAnalysis.CSharp.Syntax;
9+
using StyleCop.Analyzers.Lightup;
10+
using Xunit;
11+
12+
public class SyntaxWrapperTests
13+
{
14+
[Fact]
15+
public void TestWrapSyntaxNode()
16+
{
17+
SyntaxNode syntaxNode = SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression);
18+
19+
Assert.Same(syntaxNode, SyntaxWrapper<LiteralExpressionSyntax>.Default.Wrap(syntaxNode));
20+
Assert.Same(syntaxNode, SyntaxWrapper<LiteralExpressionSyntax>.Default.Unwrap((LiteralExpressionSyntax)syntaxNode));
21+
}
22+
}
23+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,9 +242,11 @@
242242
<Compile Include="Lightup\DeclarationPatternSyntaxWrapperTests.cs" />
243243
<Compile Include="Lightup\DestructorDeclarationSyntaxExtensionsTests.cs" />
244244
<Compile Include="Lightup\DiscardDesignationSyntaxWrapperTests.cs" />
245+
<Compile Include="Lightup\LightupHelpersTests.cs" />
245246
<Compile Include="Lightup\ParenthesizedVariableDesignationSyntaxWrapperTests.cs" />
246247
<Compile Include="Lightup\PatternSyntaxWrapperTests.cs" />
247248
<Compile Include="Lightup\SingleVariableDesignationSyntaxWrapperTests.cs" />
249+
<Compile Include="Lightup\SyntaxWrapperTests.cs" />
248250
<Compile Include="Lightup\VariableDesignationSyntaxWrapperTests.cs" />
249251
<Compile Include="LinqHelpers\SyntaxTriviaListEnumerableTests.cs" />
250252
<Compile Include="MaintainabilityRules\DebugMessagesUnitTestsBase.cs" />

0 commit comments

Comments
 (0)