Skip to content

Commit

Permalink
Merge pull request dotnet#33 from kevinwkt/TypeDiscoveryUsingSemantic
Browse files Browse the repository at this point in the history
Type discovery using semantic model instead of string comparisons
  • Loading branch information
kevinwkt committed Aug 7, 2020
2 parents 816409c + 91abe6e commit 7d31d9c
Show file tree
Hide file tree
Showing 6 changed files with 220 additions and 140 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.

using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Serialization;
using Xunit;

Expand Down Expand Up @@ -67,11 +68,11 @@ public void TestGeneratedCode()
Assert.Equal(expectedCtorParamsExternal, externalTypeTest.CtorParams);

// Public and private methods are visible.
List<string> expectedMethodsInternal = new List<string> { "get_PublicIntPropertyPublic", "set_PublicIntPropertyPublic", "get_PublicIntPropertyPrivateSet", "set_PublicIntPropertyPrivateSet", "get_PublicIntPropertyPrivateGet", "set_PublicIntPropertyPrivateGet", "UseFields" };
Assert.Equal(expectedMethodsInternal, internalTypeTest.Methods);
List<string> expectedMethodsInternal = new List<string> { "get_PublicIntPropertyPrivateGet", "get_PublicIntPropertyPrivateSet", "get_PublicIntPropertyPublic", "set_PublicIntPropertyPrivateGet", "set_PublicIntPropertyPrivateSet", "set_PublicIntPropertyPublic", "UseFields" };
Assert.Equal(expectedMethodsInternal, internalTypeTest.Methods.OrderBy(s => s).ToList());

List<string> expectedMethodsExternal = new List<string> { "get_ConverterType", "CreateConverter" };
Assert.Equal(expectedMethodsExternal, externalTypeTest.Methods);
List<string> expectedMethodsExternal = new List<string> { "CreateConverter", "get_ConverterType" };
Assert.Equal(expectedMethodsExternal, externalTypeTest.Methods.OrderBy(s => s).ToList());

// Public and private fields are visible.
Dictionary<string, string> expectedFieldsInternal = new Dictionary<string, string> { { "PublicCharField", "Char" }, { "PrivateStringField", "String" } };
Expand Down
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;
}
}
}
Loading

0 comments on commit 7d31d9c

Please sign in to comment.