Skip to content

Commit 57bc0cc

Browse files
committed
Use real symbols instead of Moq
1 parent 3f04950 commit 57bc0cc

File tree

2 files changed

+55
-12
lines changed

2 files changed

+55
-12
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp11/Lightup/IImportScopeWrapperCSharp11UnitTests.cs

Lines changed: 55 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,83 @@
33

44
namespace StyleCop.Analyzers.Test.CSharp11.Lightup
55
{
6+
using System;
67
using System.Collections.Generic;
7-
using System.Collections.Immutable;
8+
using System.Linq;
9+
using System.Threading;
10+
using System.Threading.Tasks;
811
using Microsoft.CodeAnalysis;
9-
using Moq;
12+
using Microsoft.CodeAnalysis.CSharp;
13+
using Microsoft.CodeAnalysis.Text;
1014
using StyleCop.Analyzers.Lightup;
1115
using StyleCop.Analyzers.Test.CSharp10.Lightup;
16+
using StyleCop.Analyzers.Test.Verifiers;
1217
using Xunit;
1318

1419
public partial class IImportScopeWrapperCSharp11UnitTests : IImportScopeWrapperCSharp10UnitTests
1520
{
1621
[Theory]
17-
[InlineData(0)]
1822
[InlineData(1)]
1923
[InlineData(2)]
20-
public void TestCompatibleInstance(int numberOfAliasSymbols)
24+
public async Task TestCompatibleInstanceAsync(int numberOfAliasSymbols)
2125
{
22-
var obj = CreateImportScope(numberOfAliasSymbols);
26+
var obj = await CreateImportScopeAsync(numberOfAliasSymbols, CancellationToken.None).ConfigureAwait(false);
2327
Assert.True(IImportScopeWrapper.IsInstance(obj));
2428
var wrapper = IImportScopeWrapper.FromObject(obj);
2529
Assert.Equal(obj.Aliases, wrapper.Aliases);
2630
}
2731

28-
private static IImportScope CreateImportScope(int numberOfAliasSymbols)
32+
private static async Task<IImportScope> CreateImportScopeAsync(int numberOfAliasSymbols, CancellationToken cancellationToken)
2933
{
30-
var aliasSymbolMocks = new List<IAliasSymbol>();
34+
var aliasDirectives = new List<string>(numberOfAliasSymbols);
3135
for (var i = 0; i < numberOfAliasSymbols; i++)
3236
{
33-
aliasSymbolMocks.Add(Mock.Of<IAliasSymbol>());
37+
aliasDirectives.Add($"global using Alias{i} = System.String;");
3438
}
3539

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;
3983
}
4084
}
4185
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
<ItemGroup>
2121
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.4.0" />
22-
<PackageReference Include="Moq" Version="4.20.70" />
2322
<PackageReference Include="xunit" Version="2.4.1" />
2423
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" PrivateAssets="all" />
2524
</ItemGroup>

0 commit comments

Comments
 (0)