|
3 | 3 |
|
4 | 4 | namespace StyleCop.Analyzers.Test.CSharp11.Lightup |
5 | 5 | { |
| 6 | + using System; |
6 | 7 | using System.Collections.Generic; |
7 | | - using System.Collections.Immutable; |
| 8 | + using System.Linq; |
| 9 | + using System.Threading; |
| 10 | + using System.Threading.Tasks; |
8 | 11 | using Microsoft.CodeAnalysis; |
9 | | - using Moq; |
| 12 | + using Microsoft.CodeAnalysis.CSharp; |
| 13 | + using Microsoft.CodeAnalysis.Text; |
10 | 14 | using StyleCop.Analyzers.Lightup; |
11 | 15 | using StyleCop.Analyzers.Test.CSharp10.Lightup; |
| 16 | + using StyleCop.Analyzers.Test.Verifiers; |
12 | 17 | using Xunit; |
13 | 18 |
|
14 | 19 | public partial class IImportScopeWrapperCSharp11UnitTests : IImportScopeWrapperCSharp10UnitTests |
15 | 20 | { |
16 | 21 | [Theory] |
17 | | - [InlineData(0)] |
18 | 22 | [InlineData(1)] |
19 | 23 | [InlineData(2)] |
20 | | - public void TestCompatibleInstance(int numberOfAliasSymbols) |
| 24 | + public async Task TestCompatibleInstanceAsync(int numberOfAliasSymbols) |
21 | 25 | { |
22 | | - var obj = CreateImportScope(numberOfAliasSymbols); |
| 26 | + var obj = await CreateImportScopeAsync(numberOfAliasSymbols, CancellationToken.None).ConfigureAwait(false); |
23 | 27 | Assert.True(IImportScopeWrapper.IsInstance(obj)); |
24 | 28 | var wrapper = IImportScopeWrapper.FromObject(obj); |
25 | 29 | Assert.Equal(obj.Aliases, wrapper.Aliases); |
26 | 30 | } |
27 | 31 |
|
28 | | - private static IImportScope CreateImportScope(int numberOfAliasSymbols) |
| 32 | + private static async Task<IImportScope> CreateImportScopeAsync(int numberOfAliasSymbols, CancellationToken cancellationToken) |
29 | 33 | { |
30 | | - var aliasSymbolMocks = new List<IAliasSymbol>(); |
| 34 | + var aliasDirectives = new List<string>(numberOfAliasSymbols); |
31 | 35 | for (var i = 0; i < numberOfAliasSymbols; i++) |
32 | 36 | { |
33 | | - aliasSymbolMocks.Add(Mock.Of<IAliasSymbol>()); |
| 37 | + aliasDirectives.Add($"global using Alias{i} = System.String;"); |
34 | 38 | } |
35 | 39 |
|
36 | | - var importScopeMock = new Mock<IImportScope>(); |
37 | | - importScopeMock.Setup(x => x.Aliases).Returns(aliasSymbolMocks.ToImmutableArray()); |
38 | | - return importScopeMock.Object; |
| 40 | + var source = string.Join(Environment.NewLine, aliasDirectives); |
| 41 | + if (source.Length > 0) |
| 42 | + { |
| 43 | + source += Environment.NewLine; |
| 44 | + } |
| 45 | + |
| 46 | + source += @" |
| 47 | +namespace TestNamespace |
| 48 | +{ |
| 49 | + public class TestClass |
| 50 | + { |
| 51 | + } |
| 52 | +} |
| 53 | +"; |
| 54 | + |
| 55 | + var workspace = GenericAnalyzerTest.CreateWorkspace(); |
| 56 | + var projectId = ProjectId.CreateNewId(); |
| 57 | + |
| 58 | + var references = await GenericAnalyzerTest.ReferenceAssemblies |
| 59 | + .ResolveAsync(LanguageNames.CSharp, cancellationToken).ConfigureAwait(false); |
| 60 | + |
| 61 | + var solution = workspace.CurrentSolution |
| 62 | + .AddProject(projectId, "TestProject", "TestProject", LanguageNames.CSharp) |
| 63 | + .WithProjectCompilationOptions(projectId, new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)) |
| 64 | + .WithProjectParseOptions(projectId, new CSharpParseOptions(LanguageVersionEx.CSharp11)) |
| 65 | + .AddMetadataReferences(projectId, references); |
| 66 | + |
| 67 | + var documentId = DocumentId.CreateNewId(projectId); |
| 68 | + solution = solution.AddDocument(documentId, "Test.cs", SourceText.From(source)); |
| 69 | + |
| 70 | + var project = solution.GetProject(projectId)!; |
| 71 | + var compilation = (await project.GetCompilationAsync(cancellationToken).ConfigureAwait(false))!; |
| 72 | + var syntaxTree = compilation.SyntaxTrees.Single(); |
| 73 | + var semanticModel = compilation.GetSemanticModel(syntaxTree); |
| 74 | + var importScopes = semanticModel.GetImportScopes(syntaxTree.Length, cancellationToken); |
| 75 | + |
| 76 | + var result = importScopes.FirstOrDefault(scope => scope.Aliases.Length == numberOfAliasSymbols); |
| 77 | + if (result == null) |
| 78 | + { |
| 79 | + throw new InvalidOperationException("Could not create an import scope with the expected number of alias symbols."); |
| 80 | + } |
| 81 | + |
| 82 | + return result; |
39 | 83 | } |
40 | 84 | } |
41 | 85 | } |
0 commit comments