|
| 1 | +// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. |
| 2 | +// Licensed under the MIT License. See LICENSE in the project root for license information. |
| 3 | + |
| 4 | +namespace StyleCop.Analyzers.Test.HelperTests |
| 5 | +{ |
| 6 | + using System.Linq; |
| 7 | + using System.Threading; |
| 8 | + using System.Threading.Tasks; |
| 9 | + using Microsoft.CodeAnalysis; |
| 10 | + using Microsoft.CodeAnalysis.CSharp; |
| 11 | + using Microsoft.CodeAnalysis.CSharp.Syntax; |
| 12 | + using Microsoft.CodeAnalysis.Text; |
| 13 | + using StyleCop.Analyzers.Helpers; |
| 14 | + using StyleCop.Analyzers.Test.Verifiers; |
| 15 | + using Xunit; |
| 16 | + |
| 17 | + /// <summary> |
| 18 | + /// Unit tests for the <see cref="SymbolNameHelpers"/> class. |
| 19 | + /// </summary> |
| 20 | + public class SymbolNameHelpersTests : IAsyncLifetime |
| 21 | + { |
| 22 | + private const string TestProjectName = "TestProject"; |
| 23 | + private const string TestFilename = "Test.cs"; |
| 24 | + |
| 25 | + private Solution testSolution; |
| 26 | + |
| 27 | + public async Task InitializeAsync() |
| 28 | + { |
| 29 | + var compilationOptions = this.GetCompilationOptions(); |
| 30 | + this.testSolution = await CreateTestSolutionAsync(compilationOptions).ConfigureAwait(false); |
| 31 | + } |
| 32 | + |
| 33 | + public Task DisposeAsync() |
| 34 | + { |
| 35 | + return Task.FromResult(true); |
| 36 | + } |
| 37 | + |
| 38 | + /// <summary> |
| 39 | + /// Verify the workings of <see cref="SymbolNameHelpers.ToQualifiedString(ISymbol, NameSyntax)"/> |
| 40 | + /// for standard use cases. |
| 41 | + /// </summary> |
| 42 | + /// <param name="inputString">A string representation of a type or namespace to process.</param> |
| 43 | + /// <param name="isNamespace"><see langword="true"/> if <paramref name="inputString"/> is a namespace; |
| 44 | + /// <see langword="false"/> if <paramref name="inputString"/> is a type to be used as an alias target.</param> |
| 45 | + /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> |
| 46 | + [Theory] |
| 47 | + [InlineData("System.ValueTuple<int, object>")] |
| 48 | + [InlineData("System.ValueTuple<System.Int32, System.Object>")] |
| 49 | + [InlineData("System.ValueTuple<System.Int32?, System.Object>")] |
| 50 | + [InlineData("System.ValueTuple<int, object[]>")] |
| 51 | + [InlineData("System.ValueTuple<int?, object>")] |
| 52 | + [InlineData("System.ValueTuple<int[], object>")] |
| 53 | + [InlineData("System.ValueTuple<int?[], object>")] |
| 54 | + [InlineData("System.Collections.Generic.KeyValuePair<int, object>")] |
| 55 | + [InlineData("System.Collections.Generic.KeyValuePair<int?, object>")] |
| 56 | + [InlineData("System.Collections.Generic.KeyValuePair<int, object[]>")] |
| 57 | + [InlineData("System.Nullable<int>")] |
| 58 | + [InlineData("System", true)] |
| 59 | + [InlineData("System.Collections.Generic", true)] |
| 60 | + [WorkItem(3149, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3149")] |
| 61 | + public Task VerifyToQualifiedStringAsync(string inputString, bool isNamespace = false) |
| 62 | + { |
| 63 | + return this.PerformTestAsync(inputString, isNamespace); |
| 64 | + } |
| 65 | + |
| 66 | + /// <summary> |
| 67 | + /// Performs the actual testing work. |
| 68 | + /// </summary> |
| 69 | + /// <param name="inputString">A string representation of a type or namespace to process.</param> |
| 70 | + /// <param name="isNamespace"><see langword="true"/> if <paramref name="inputString"/> is a namespace; |
| 71 | + /// <see langword="false"/> if <paramref name="inputString"/> is a type to be used as an alias target.</param> |
| 72 | + /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> |
| 73 | + protected async Task PerformTestAsync(string inputString, bool isNamespace) |
| 74 | + { |
| 75 | + var fileContent = isNamespace ? "using " + inputString : "using AliasType = " + inputString; |
| 76 | + fileContent += ";"; |
| 77 | + |
| 78 | + var document = GetDocument(this.testSolution, fileContent); |
| 79 | + var syntaxRoot = await document.GetSyntaxRootAsync().ConfigureAwait(false); |
| 80 | + var semanticModel = await document.GetSemanticModelAsync().ConfigureAwait(false); |
| 81 | + |
| 82 | + var usingDirectiveSyntax = syntaxRoot.DescendantNodes().OfType<UsingDirectiveSyntax>().First(); |
| 83 | + var typeSymbol = semanticModel.GetSymbolInfo(usingDirectiveSyntax.Name).Symbol; |
| 84 | + |
| 85 | + var resultString = typeSymbol.ToQualifiedString(usingDirectiveSyntax.Name); |
| 86 | + Assert.Equal(inputString, resultString); |
| 87 | + } |
| 88 | + |
| 89 | + /// <summary> |
| 90 | + /// When overridden in derived classes, provides a <see cref="CSharpCompilationOptions"/> instance |
| 91 | + /// to be used for the test project in the workspace. |
| 92 | + /// </summary> |
| 93 | + /// <returns>An instance of <see cref="CSharpCompilationOptions"/> describing the project compilation options.</returns> |
| 94 | + protected virtual CSharpCompilationOptions GetCompilationOptions() |
| 95 | + { |
| 96 | + return new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary); |
| 97 | + } |
| 98 | + |
| 99 | + private static Document GetDocument(Solution solution, string sourceCode) |
| 100 | + { |
| 101 | + var projectId = solution.ProjectIds[0]; |
| 102 | + var documentId = DocumentId.CreateNewId(projectId); |
| 103 | + solution = solution.AddDocument(documentId, TestFilename, SourceText.From(sourceCode)); |
| 104 | + return solution.GetDocument(documentId); |
| 105 | + } |
| 106 | + |
| 107 | + private static async Task<Solution> CreateTestSolutionAsync(CSharpCompilationOptions compilationOptions) |
| 108 | + { |
| 109 | + var workspace = GenericAnalyzerTest.CreateWorkspace(); |
| 110 | + var projectId = ProjectId.CreateNewId(); |
| 111 | + |
| 112 | + var references = await GenericAnalyzerTest.ReferenceAssemblies |
| 113 | + .ResolveAsync(LanguageNames.CSharp, CancellationToken.None).ConfigureAwait(false); |
| 114 | + |
| 115 | + var solution = workspace.CurrentSolution |
| 116 | + .AddProject(projectId, TestProjectName, TestProjectName, LanguageNames.CSharp) |
| 117 | + .WithProjectCompilationOptions(projectId, compilationOptions) |
| 118 | + .AddMetadataReferences(projectId, references); |
| 119 | + |
| 120 | + return solution; |
| 121 | + } |
| 122 | + } |
| 123 | +} |
0 commit comments