Skip to content

Commit

Permalink
Merge pull request #4633 from sharwell/test-update
Browse files Browse the repository at this point in the history
Update to Microsoft.CodeAnalysis.Testing 1.0.1-beta1.20623.3
  • Loading branch information
mavasani committed Jan 5, 2021
2 parents b8d5ea3 + 851ed2f commit eb54287
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 198 deletions.
2 changes: 1 addition & 1 deletion eng/Versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
<RoslynDiagnosticsAnalyzersVersion>$(DogfoodAnalyzersVersion)</RoslynDiagnosticsAnalyzersVersion>
<MicrosoftCodeAnalysisTestResourcesProprietaryVersion>2.0.0-pre-20160714</MicrosoftCodeAnalysisTestResourcesProprietaryVersion>
<!-- Roslyn Testing -->
<MicrosoftCodeAnalysisTestingVersion>1.0.1-beta1.20521.1</MicrosoftCodeAnalysisTestingVersion>
<MicrosoftCodeAnalysisTestingVersion>1.0.1-beta1.20623.3</MicrosoftCodeAnalysisTestingVersion>
<!-- Libs -->
<SystemCollectionsImmutableVersion>1.3.1</SystemCollectionsImmutableVersion>
<SystemComponentModelCompositionVersion>4.7.0</SystemComponentModelCompositionVersion>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ class SomeOtherClass : IMyOtherInterface { }";
TestState =
{
Sources = { source2 },
AdditionalProjects =
{
["DependencyProject"] =
{
Sources = { source1 },
},
},
AdditionalProjectReferences = { "DependencyProject" },
ExpectedDiagnostics =
{
// Test0.cs(2,7): error RS1009: Type SomeClass cannot implement interface IMyInterface because IMyInterface is not available for public implementation.
Expand All @@ -67,19 +75,6 @@ class SomeOtherClass : IMyOtherInterface { }";
VerifyCS.Diagnostic().WithSpan(4, 7, 4, 21).WithArguments("SomeOtherClass", "IMyInterface"),
},
},
SolutionTransforms =
{
(solution, projectId) =>
{
var dependencyProject = solution.AddProject("DependencyProject", "DependencyProject", LanguageNames.CSharp)
.WithCompilationOptions(solution.GetProject(projectId).CompilationOptions)
.WithParseOptions(solution.GetProject(projectId).ParseOptions)
.WithMetadataReferences(solution.GetProject(projectId).MetadataReferences)
.AddDocument("Test0.cs", source1, filePath: "Test0.cs").Project;
return dependencyProject.Solution.AddProjectReference(projectId, new ProjectReference(dependencyProject.Id));
},
},
}.RunAsync();
}

Expand All @@ -105,19 +100,17 @@ class SomeOtherClass : IMyOtherInterface { }";
await new VerifyCS.Test
{
ReferenceAssemblies = AdditionalMetadataReferences.DefaultWithoutRoslynSymbols,
TestState = { Sources = { source2 } },
SolutionTransforms =
TestState =
{
(solution, projectId) =>
Sources = { source2 },
AdditionalProjects =
{
var dependencyProject = solution.AddProject("DependencyProject", "DependencyProject", LanguageNames.CSharp)
.WithCompilationOptions(solution.GetProject(projectId).CompilationOptions)
.WithParseOptions(solution.GetProject(projectId).ParseOptions)
.WithMetadataReferences(solution.GetProject(projectId).MetadataReferences)
.AddDocument("Test0.cs", source1, filePath: "Test0.cs").Project;
return dependencyProject.Solution.AddProjectReference(projectId, new ProjectReference(dependencyProject.Id));
["DependencyProject"] =
{
Sources = { source1 },
},
},
AdditionalProjectReferences = { "DependencyProject" },
},
}.RunAsync();
}
Expand Down Expand Up @@ -441,6 +434,14 @@ End Class
TestState =
{
Sources = { source2 },
AdditionalProjects =
{
["DependencyProject"] =
{
Sources = { source1 },
},
},
AdditionalProjectReferences = { "DependencyProject" },
ExpectedDiagnostics =
{
// Test0.vb(2,7): error RS1009: Type SomeClass cannot implement interface IMyInterface because IMyInterface is not available for public implementation.
Expand All @@ -449,19 +450,6 @@ End Class
VerifyVB.Diagnostic().WithSpan(6, 7, 6, 21).WithArguments("SomeOtherClass", "IMyInterface"),
},
},
SolutionTransforms =
{
(solution, projectId) =>
{
var dependencyProject = solution.AddProject("DependencyProject", "DependencyProject", LanguageNames.VisualBasic)
.WithCompilationOptions(solution.GetProject(projectId).CompilationOptions)
.WithParseOptions(solution.GetProject(projectId).ParseOptions)
.WithMetadataReferences(solution.GetProject(projectId).MetadataReferences)
.AddDocument("Test0.vb", source1, filePath: "Test0.vb").Project;
return dependencyProject.Solution.AddProjectReference(projectId, new ProjectReference(dependencyProject.Id));
},
},
}.RunAsync();
}

Expand Down Expand Up @@ -495,19 +483,17 @@ End Class
await new VerifyVB.Test
{
ReferenceAssemblies = AdditionalMetadataReferences.DefaultWithoutRoslynSymbols,
TestState = { Sources = { source2 } },
SolutionTransforms =
TestState =
{
(solution, projectId) =>
Sources = { source2 },
AdditionalProjects =
{
var dependencyProject = solution.AddProject("DependencyProject", "DependencyProject", LanguageNames.VisualBasic)
.WithCompilationOptions(solution.GetProject(projectId).CompilationOptions)
.WithParseOptions(solution.GetProject(projectId).ParseOptions)
.WithMetadataReferences(solution.GetProject(projectId).MetadataReferences)
.AddDocument("Test0.vb", source1, filePath: "Test0.vb").Project;
return dependencyProject.Solution.AddProjectReference(projectId, new ProjectReference(dependencyProject.Id));
["DependencyProject"] =
{
Sources = { source1 },
},
},
AdditionalProjectReferences = { "DependencyProject" },
},
}.RunAsync();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Linq;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Testing;
using Microsoft.CodeAnalysis.VisualBasic;
using Test.Utilities;
using Xunit;

Expand Down Expand Up @@ -41,9 +38,6 @@ public class RestrictedInternalsVisibleToAnalyzerTests
private const string CSharpApiProviderFileName = "ApiProviderFileName.cs";
private const string VisualBasicApiProviderFileName = "ApiProviderFileName.vb";

private static readonly CompilationOptions s_csharpCompilationOptions = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary);
private static readonly CompilationOptions s_visualBasicCompilationOptions = new VisualBasicCompilationOptions(OutputKind.DynamicallyLinkedLibrary);

private const string CSharpRestrictedInternalsVisibleToAttribute = @"
namespace System.Runtime.CompilerServices
{
Expand Down Expand Up @@ -73,10 +67,21 @@ private async Task VerifyCSharpAsync(string apiProviderSource, string apiConsume
TestState =
{
Sources = { apiConsumerSource },
AdditionalProjects =
{
[ApiProviderProjectName] =
{
Sources =
{
(CSharpApiProviderFileName, apiProviderSource + CSharpRestrictedInternalsVisibleToAttribute),
},
},
},
AdditionalProjectReferences = { ApiProviderProjectName },
},
SolutionTransforms =
{
(solution, projectId) => ApplySolutionTransforms(solution, projectId, apiProviderSource, LanguageNames.CSharp),
(solution, projectId) => ApplySolutionTransforms(solution, projectId),
},
};

Expand All @@ -91,42 +96,31 @@ private async Task VerifyBasicAsync(string apiProviderSource, string apiConsumer
TestState =
{
Sources = { apiConsumerSource },
AdditionalProjects =
{
[ApiProviderProjectName] =
{
Sources =
{
(VisualBasicApiProviderFileName, apiProviderSource + VisualBasicRestrictedInternalsVisibleToAttribute),
},
},
},
AdditionalProjectReferences = { ApiProviderProjectName },
},
SolutionTransforms =
{
(solution, projectId) => ApplySolutionTransforms(solution, projectId, apiProviderSource, LanguageNames.VisualBasic),
(solution, projectId) => ApplySolutionTransforms(solution, projectId),
},
};

test.ExpectedDiagnostics.AddRange(expected);
await test.RunAsync();
}

private static Solution ApplySolutionTransforms(
Solution solution,
ProjectId apiConsumerProjectId,
string apiProviderSource,
string language)
{
var restrictedInternalsVisibleToAttribute = language == LanguageNames.CSharp
? CSharpRestrictedInternalsVisibleToAttribute
: VisualBasicRestrictedInternalsVisibleToAttribute;
var compilationOptions = language == LanguageNames.CSharp
? s_csharpCompilationOptions
: s_visualBasicCompilationOptions;
var fileName = language == LanguageNames.CSharp
? CSharpApiProviderFileName
: VisualBasicApiProviderFileName;

var metadataReferences = solution.Projects.Single().MetadataReferences;
solution = solution.WithProjectAssemblyName(apiConsumerProjectId, ApiConsumerProjectName);
var apiProducerProject = solution
.AddProject(ApiProviderProjectName, ApiProviderProjectName, language)
.WithCompilationOptions(compilationOptions)
.WithMetadataReferences(metadataReferences)
.AddDocument(fileName, apiProviderSource + restrictedInternalsVisibleToAttribute)
.Project;
return apiProducerProject.Solution.AddProjectReference(apiConsumerProjectId, new ProjectReference(apiProducerProject.Id));
private static Solution ApplySolutionTransforms(Solution solution, ProjectId apiConsumerProjectId)
{
return solution.WithProjectAssemblyName(apiConsumerProjectId, ApiConsumerProjectName);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Testing;
using Test.Utilities;
using Xunit;
Expand Down Expand Up @@ -464,17 +465,12 @@ public async Task CA1720_TopLevelStatements_NoDiagnostic()
{
await new VerifyCS.Test()
{
TestCode = @"int x = 0;",
LanguageVersion = CodeAnalysis.CSharp.LanguageVersion.CSharp9,
SolutionTransforms =
TestState =
{
(solution, projectId) =>
{
var project = solution.GetProject(projectId);
project = project.WithCompilationOptions(project.CompilationOptions.WithOutputKind(CodeAnalysis.OutputKind.ConsoleApplication));
return project.Solution;
},
}
Sources = { @"int x = 0;" },
OutputKind = OutputKind.ConsoleApplication,
},
LanguageVersion = CodeAnalysis.CSharp.LanguageVersion.CSharp9,
}.RunAsync();
}

Expand Down
Loading

0 comments on commit eb54287

Please sign in to comment.