Skip to content

Commit 45e0f02

Browse files
committed
Add initial tests for SeparatedSyntaxListWrapper<T>
1 parent d87b921 commit 45e0f02

7 files changed

Lines changed: 109 additions & 3 deletions
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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 StyleCop.Analyzers.Lightup;
9+
10+
public class AutoWrapSeparatedSyntaxListTests : SeparatedSyntaxListWrapperTestBase
11+
{
12+
internal override SeparatedSyntaxListWrapper<SyntaxNode> CreateList()
13+
{
14+
return new SeparatedSyntaxListWrapper<SyntaxNode>.AutoWrapSeparatedSyntaxList<SyntaxNode>(default(SeparatedSyntaxList<SyntaxNode>));
15+
}
16+
17+
internal override bool TryCreateNonEmptyList(out SeparatedSyntaxListWrapper<SyntaxNode> list)
18+
{
19+
list = new SeparatedSyntaxListWrapper<SyntaxNode>.AutoWrapSeparatedSyntaxList<SyntaxNode>(
20+
SyntaxFactory.SingletonSeparatedList<SyntaxNode>(SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression)));
21+
return true;
22+
}
23+
}
24+
}

StyleCop.Analyzers/StyleCop.Analyzers.Test/Lightup/LightupHelpersTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public void TestCanAccessNonExistentProperty()
4141
Assert.NotNull(separatedListWithPropertyAccessor);
4242
Assert.NotNull(separatedListWithPropertyAccessor(SyntaxFactory.AccessorList(), null));
4343
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()));
44+
Assert.Throws<NullReferenceException>(() => separatedListWithPropertyAccessor(null, SeparatedSyntaxListWrapper<SyntaxNode>.UnsupportedEmpty));
4545
}
4646

4747
[Fact]

StyleCop.Analyzers/StyleCop.Analyzers.Test/Lightup/ParenthesizedVariableDesignationSyntaxWrapperTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public void TestNull()
2121
Assert.Throws<NullReferenceException>(() => parenthesizedVariableDesignationSyntax.Variables);
2222
Assert.Throws<NullReferenceException>(() => parenthesizedVariableDesignationSyntax.CloseParenToken);
2323
Assert.Throws<NullReferenceException>(() => parenthesizedVariableDesignationSyntax.WithOpenParenToken(SyntaxFactory.Token(SyntaxKind.OpenParenToken)));
24-
Assert.Throws<NullReferenceException>(() => parenthesizedVariableDesignationSyntax.WithVariables(new SeparatedSyntaxListWrapper<VariableDesignationSyntaxWrapper>.UnsupportedSyntaxList()));
24+
Assert.Throws<NullReferenceException>(() => parenthesizedVariableDesignationSyntax.WithVariables(SeparatedSyntaxListWrapper<VariableDesignationSyntaxWrapper>.UnsupportedEmpty));
2525
Assert.Throws<NullReferenceException>(() => parenthesizedVariableDesignationSyntax.WithCloseParenToken(SyntaxFactory.Token(SyntaxKind.CloseParenToken)));
2626
}
2727

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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 StyleCop.Analyzers.Lightup;
9+
using Xunit;
10+
11+
public abstract class SeparatedSyntaxListWrapperTestBase
12+
{
13+
[Fact]
14+
public void TestBasicProperties()
15+
{
16+
var list = this.CreateList();
17+
Assert.Equal(0, list.Count);
18+
Assert.Equal(0, list.SeparatorCount);
19+
Assert.Equal(default(SeparatedSyntaxList<SyntaxNode>).FullSpan, list.FullSpan);
20+
Assert.Equal(default(SeparatedSyntaxList<SyntaxNode>).Span, list.Span);
21+
Assert.Equal(default(SeparatedSyntaxList<SyntaxNode>).ToString(), list.ToString());
22+
Assert.Equal(default(SeparatedSyntaxList<SyntaxNode>).ToFullString(), list.ToFullString());
23+
Assert.ThrowsAny<ArgumentException>(() => list[0]);
24+
25+
if (list.UnderlyingList != null)
26+
{
27+
Assert.IsAssignableFrom(typeof(SeparatedSyntaxList<SyntaxNode>), list.UnderlyingList);
28+
var underlyingList = (SeparatedSyntaxList<SyntaxNode>)list.UnderlyingList;
29+
Assert.Equal(0, list.Count);
30+
}
31+
}
32+
33+
[Fact]
34+
public void TestElements()
35+
{
36+
var list = this.CreateList();
37+
Assert.False(list.Any());
38+
Assert.Null(list.FirstOrDefault());
39+
Assert.Null(list.LastOrDefault());
40+
Assert.ThrowsAny<ArgumentOutOfRangeException>(() => list.First());
41+
Assert.ThrowsAny<ArgumentOutOfRangeException>(() => list.Last());
42+
43+
if (this.TryCreateNonEmptyList(out list))
44+
{
45+
Assert.True(list.Any());
46+
Assert.NotNull(list.First());
47+
Assert.NotNull(list.FirstOrDefault());
48+
Assert.NotNull(list.Last());
49+
Assert.NotNull(list.LastOrDefault());
50+
}
51+
}
52+
53+
internal abstract SeparatedSyntaxListWrapper<SyntaxNode> CreateList();
54+
55+
internal abstract bool TryCreateNonEmptyList(out SeparatedSyntaxListWrapper<SyntaxNode> list);
56+
}
57+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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 StyleCop.Analyzers.Lightup;
8+
9+
public class UnsupportedSyntaxListTests : SeparatedSyntaxListWrapperTestBase
10+
{
11+
internal override SeparatedSyntaxListWrapper<SyntaxNode> CreateList()
12+
{
13+
return SeparatedSyntaxListWrapper<SyntaxNode>.UnsupportedEmpty;
14+
}
15+
16+
internal override bool TryCreateNonEmptyList(out SeparatedSyntaxListWrapper<SyntaxNode> list)
17+
{
18+
list = null;
19+
return false;
20+
}
21+
}
22+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@
236236
<Compile Include="LayoutRules\SA1519UnitTests.cs" />
237237
<Compile Include="LayoutRules\SA1520UnitTests.cs" />
238238
<Compile Include="Lightup\AccessorDeclarationSyntaxExtensionsTests.cs" />
239+
<Compile Include="Lightup\AutoWrapSeparatedSyntaxListTests.cs" />
239240
<Compile Include="Lightup\BaseMethodDeclarationSyntaxExtensionsTests.cs" />
240241
<Compile Include="Lightup\ConstantPatternSyntaxWrapperTests.cs" />
241242
<Compile Include="Lightup\ConstructorDeclarationSyntaxExtensionsTests.cs" />
@@ -245,8 +246,10 @@
245246
<Compile Include="Lightup\LightupHelpersTests.cs" />
246247
<Compile Include="Lightup\ParenthesizedVariableDesignationSyntaxWrapperTests.cs" />
247248
<Compile Include="Lightup\PatternSyntaxWrapperTests.cs" />
249+
<Compile Include="Lightup\SeparatedSyntaxListWrapperTestBase.cs" />
248250
<Compile Include="Lightup\SingleVariableDesignationSyntaxWrapperTests.cs" />
249251
<Compile Include="Lightup\SyntaxWrapperTests.cs" />
252+
<Compile Include="Lightup\UnsupportedSyntaxListTests.cs" />
250253
<Compile Include="Lightup\VariableDesignationSyntaxWrapperTests.cs" />
251254
<Compile Include="LinqHelpers\SyntaxTriviaListEnumerableTests.cs" />
252255
<Compile Include="MaintainabilityRules\DebugMessagesUnitTestsBase.cs" />

StyleCop.Analyzers/StyleCop.Analyzers/Lightup/SeparatedSyntaxListWrapper`1.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ public override string ToString()
281281
=> this.syntaxList.ToString();
282282
}
283283

284-
internal sealed class UnsupportedSyntaxList : SeparatedSyntaxListWrapper<TNode>
284+
private sealed class UnsupportedSyntaxList : SeparatedSyntaxListWrapper<TNode>
285285
{
286286
private static readonly SeparatedSyntaxList<SyntaxNode> SyntaxList = default(SeparatedSyntaxList<SyntaxNode>);
287287

0 commit comments

Comments
 (0)