|
| 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 | +} |
0 commit comments