-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLightupHelpersUnitTests.cs
More file actions
111 lines (94 loc) · 7.06 KB
/
Copy pathLightupHelpersUnitTests.cs
File metadata and controls
111 lines (94 loc) · 7.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright (c) Contributors to the New StyleCop Analyzers project.
// Licensed under the MIT License. See LICENSE in the project root for license information.
#nullable disable
namespace StyleCop.Analyzers.Test.Lightup
{
using System;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using StyleCop.Analyzers.Lightup;
using Xunit;
public class LightupHelpersUnitTests
{
[Fact]
public void TestCanWrapNullNode()
{
Assert.True(LightupHelpers.CanWrapNode(null, typeof(PatternSyntaxWrapper)));
}
[Fact]
public void TestCanAccessNonExistentProperty()
{
var propertyAccessor = LightupHelpers.CreateSyntaxPropertyAccessor<SyntaxNode, object>(typeof(SyntaxNode), "NonExistentProperty");
Assert.NotNull(propertyAccessor);
Assert.Null(propertyAccessor(SyntaxFactory.AccessorList()));
Assert.Throws<NullReferenceException>(() => propertyAccessor(null));
var withPropertyAccessor = LightupHelpers.CreateSyntaxWithPropertyAccessor<SyntaxNode, object>(typeof(SyntaxNode), "NonExistentProperty");
Assert.NotNull(withPropertyAccessor);
Assert.NotNull(withPropertyAccessor(SyntaxFactory.AccessorList(), null));
Assert.ThrowsAny<NotSupportedException>(() => withPropertyAccessor(SyntaxFactory.AccessorList(), new object()));
Assert.Throws<NullReferenceException>(() => withPropertyAccessor(null, new object()));
var separatedListPropertyAccessor = LightupHelpers.CreateSeparatedSyntaxListPropertyAccessor<SyntaxNode, SyntaxNode>(typeof(SyntaxNode), "NonExistentProperty");
Assert.NotNull(separatedListPropertyAccessor);
Assert.NotNull(separatedListPropertyAccessor(SyntaxFactory.AccessorList()));
Assert.Throws<NullReferenceException>(() => separatedListPropertyAccessor(null));
var separatedListWithPropertyAccessor = LightupHelpers.CreateSeparatedSyntaxListWithPropertyAccessor<SyntaxNode, SyntaxNode>(typeof(SyntaxNode), "NonExistentProperty");
Assert.NotNull(separatedListWithPropertyAccessor);
Assert.NotNull(separatedListWithPropertyAccessor(SyntaxFactory.AccessorList(), null));
Assert.ThrowsAny<NotSupportedException>(() => separatedListWithPropertyAccessor(SyntaxFactory.AccessorList(), new SeparatedSyntaxListWrapper<SyntaxNode>.AutoWrapSeparatedSyntaxList<LiteralExpressionSyntax>(SyntaxFactory.SingletonSeparatedList(SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression)))));
Assert.Throws<NullReferenceException>(() => separatedListWithPropertyAccessor(null, SeparatedSyntaxListWrapper<SyntaxNode>.UnsupportedEmpty));
}
[Fact]
public void TestCreateSyntaxPropertyAccessor()
{
// The call *should* have been made with the first generic argument set to `BaseMethodDeclarationSyntax`
// instead of `MethodDeclarationSyntax`.
Assert.ThrowsAny<InvalidOperationException>(() => LightupHelpers.CreateSyntaxPropertyAccessor<MethodDeclarationSyntax, BlockSyntax>(typeof(BaseMethodDeclarationSyntax), nameof(BaseMethodDeclarationSyntax.Body)));
// The call *should* have been made with the second generic argument set to `ArrowExpressionClauseSyntax`
// instead of `BlockSyntax`.
Assert.ThrowsAny<InvalidOperationException>(() => LightupHelpers.CreateSyntaxPropertyAccessor<MethodDeclarationSyntax, BlockSyntax>(typeof(MethodDeclarationSyntax), nameof(MethodDeclarationSyntax.ExpressionBody)));
}
[Fact]
public void TestCreateSeparatedSyntaxListPropertyAccessor()
{
// The call works for `SeparatedSyntaxList<T>`, not work for `SyntaxList<T>`.
Assert.ThrowsAny<InvalidOperationException>(() => LightupHelpers.CreateSeparatedSyntaxListPropertyAccessor<BlockSyntax, StatementSyntax>(typeof(BlockSyntax), nameof(BlockSyntax.Statements)));
// The call *should* have been made with the first generic argument set to `BaseParameterListSyntax`
// instead of `ParameterListSyntax`.
Assert.ThrowsAny<InvalidOperationException>(() => LightupHelpers.CreateSeparatedSyntaxListPropertyAccessor<ParameterListSyntax, ParameterSyntax>(typeof(BaseParameterListSyntax), nameof(BaseParameterListSyntax.Parameters)));
}
[Fact]
public void TestCreateSeparatedSyntaxListPropertyAccessorValidateElementType()
{
// The call *should* have been made with the second generic argument set to `ParameterSyntax`
// instead of `ArgumentSyntax`.
Assert.ThrowsAny<InvalidOperationException>(() => LightupHelpers.CreateSeparatedSyntaxListPropertyAccessor<BaseParameterListSyntax, ArgumentSyntax>(typeof(BaseParameterListSyntax), nameof(BaseParameterListSyntax.Parameters)));
}
[Fact]
public void TestCreateSyntaxWithPropertyAccessor()
{
// The call *should* have been made with the first generic argument set to `BaseMethodDeclarationSyntax`
// instead of `MethodDeclarationSyntax`.
Assert.ThrowsAny<InvalidOperationException>(() => LightupHelpers.CreateSyntaxWithPropertyAccessor<MethodDeclarationSyntax, BlockSyntax>(typeof(BaseMethodDeclarationSyntax), nameof(BaseMethodDeclarationSyntax.Body)));
// The call *should* have been made with the second generic argument set to `ArrowExpressionClauseSyntax`
// instead of `BlockSyntax`.
Assert.ThrowsAny<InvalidOperationException>(() => LightupHelpers.CreateSyntaxWithPropertyAccessor<MethodDeclarationSyntax, BlockSyntax>(typeof(MethodDeclarationSyntax), nameof(MethodDeclarationSyntax.ExpressionBody)));
}
[Fact]
public void TestCreateSeparatedSyntaxListWithPropertyAccessor()
{
// The call works for `SeparatedSyntaxList<T>`, not work for `SyntaxList<T>`.
Assert.ThrowsAny<InvalidOperationException>(() => LightupHelpers.CreateSeparatedSyntaxListWithPropertyAccessor<BlockSyntax, StatementSyntax>(typeof(BlockSyntax), nameof(BlockSyntax.Statements)));
// The call *should* have been made with the first generic argument set to `BaseParameterListSyntax`
// instead of `ParameterListSyntax`.
Assert.ThrowsAny<InvalidOperationException>(() => LightupHelpers.CreateSeparatedSyntaxListWithPropertyAccessor<ParameterListSyntax, ParameterSyntax>(typeof(BaseParameterListSyntax), nameof(BaseParameterListSyntax.Parameters)));
}
[Fact]
public void TestCreateSeparatedSyntaxListWithPropertyAccessorValidateElementType()
{
// The call *should* have been made with the second generic argument set to `ParameterSyntax`
// instead of `ArgumentSyntax`.
Assert.ThrowsAny<InvalidOperationException>(() => LightupHelpers.CreateSeparatedSyntaxListWithPropertyAccessor<BaseParameterListSyntax, ArgumentSyntax>(typeof(BaseParameterListSyntax), nameof(BaseParameterListSyntax.Parameters)));
}
}
}