forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request dotnet#33 from kevinwkt/TypeDiscoveryUsingSemantic
Type discovery using semantic model instead of string comparisons
- Loading branch information
Showing
6 changed files
with
220 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
...braries/System.Text.Json/System.Text.Json.SourceGeneration.UnitTests/CompilationHelper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System.Collections.Generic; | ||
using System.Collections.Immutable; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Text.Json.Serialization; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Xunit; | ||
|
||
namespace System.Text.Json.SourceGeneration.UnitTests | ||
{ | ||
public class CompilationHelper | ||
{ | ||
public static Compilation CreateCompilation(string source) | ||
{ | ||
// Bypass System.Runtime error. | ||
Assembly systemRuntimeAssembly = Assembly.Load("System.Runtime, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"); | ||
string systemRuntimeAssemblyPath = systemRuntimeAssembly.Location; | ||
|
||
MetadataReference[] references = new MetadataReference[] { | ||
MetadataReference.CreateFromFile(typeof(object).Assembly.Location), | ||
MetadataReference.CreateFromFile(typeof(Attribute).Assembly.Location), | ||
MetadataReference.CreateFromFile(typeof(JsonSerializableAttribute).Assembly.Location), | ||
MetadataReference.CreateFromFile(typeof(JsonSerializerOptions).Assembly.Location), | ||
MetadataReference.CreateFromFile(typeof(Type).Assembly.Location), | ||
MetadataReference.CreateFromFile(typeof(KeyValuePair).Assembly.Location), | ||
MetadataReference.CreateFromFile(systemRuntimeAssemblyPath), | ||
}; | ||
|
||
return CSharpCompilation.Create( | ||
"TestAssembly", | ||
syntaxTrees: new[] { CSharpSyntaxTree.ParseText(source) }, | ||
references: references, | ||
options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary) | ||
); | ||
} | ||
|
||
private static GeneratorDriver CreateDriver(Compilation compilation, params ISourceGenerator[] generators) | ||
=> new CSharpGeneratorDriver( | ||
new CSharpParseOptions(kind: SourceCodeKind.Regular, documentationMode: DocumentationMode.Parse), | ||
ImmutableArray.Create(generators), | ||
ImmutableArray<AdditionalText>.Empty); | ||
|
||
public static Compilation RunGenerators(Compilation compilation, out ImmutableArray<Diagnostic> diagnostics, params ISourceGenerator[] generators) | ||
{ | ||
CreateDriver(compilation, generators).RunFullGeneration(compilation, out Compilation outCompilation, out diagnostics); | ||
return outCompilation; | ||
} | ||
} | ||
} |
Oops, something went wrong.