From 85d47d67c34f608c369cabb81f20b57f86e3697b Mon Sep 17 00:00:00 2001 From: Mohammadreza Taikandi Date: Sun, 3 Jan 2021 09:26:07 +0000 Subject: [PATCH] Add option to generate xml docs. --- .../Configuration/MapToConfigurations.cs | 26 ---- src/MapTo/Diagnostics.cs | 17 ++- src/MapTo/Extensions/EnumExtensions.cs | 9 ++ .../GeneratorExecutionContextExtensions.cs | 35 +++++ src/MapTo/MapToGenerator.cs | 23 ++- .../AccessModifier.cs | 2 +- src/MapTo/Models/MapModel.cs | 49 ++---- src/MapTo/Models/SourceGenerationOptions.cs | 17 +++ src/MapTo/SourceBuilder.cs | 143 +++++++++++------- .../{ => Infrastructure}/CSharpGenerator.cs | 15 +- .../TestAnalyzerConfigOptions.cs | 19 +++ .../TestAnalyzerConfigOptionsProvider.cs | 24 +++ test/MapTo.Tests/Tests.cs | 31 ++-- 13 files changed, 244 insertions(+), 166 deletions(-) delete mode 100644 src/MapTo/Configuration/MapToConfigurations.cs create mode 100644 src/MapTo/Extensions/EnumExtensions.cs create mode 100644 src/MapTo/Extensions/GeneratorExecutionContextExtensions.cs rename src/MapTo/{Configuration => Models}/AccessModifier.cs (74%) create mode 100644 src/MapTo/Models/SourceGenerationOptions.cs rename test/MapTo.Tests/{ => Infrastructure}/CSharpGenerator.cs (82%) create mode 100644 test/MapTo.Tests/Infrastructure/TestAnalyzerConfigOptions.cs create mode 100644 test/MapTo.Tests/Infrastructure/TestAnalyzerConfigOptionsProvider.cs diff --git a/src/MapTo/Configuration/MapToConfigurations.cs b/src/MapTo/Configuration/MapToConfigurations.cs deleted file mode 100644 index 3639386..0000000 --- a/src/MapTo/Configuration/MapToConfigurations.cs +++ /dev/null @@ -1,26 +0,0 @@ -using System; -using Microsoft.CodeAnalysis; - -namespace MapTo.Configuration -{ - internal sealed class MapToConfigurations - { - private MapToConfigurations(AccessModifier constructorAccessModifier) - { - ConstructorAccessModifier = constructorAccessModifier; - } - - internal AccessModifier ConstructorAccessModifier { get; } - - internal static MapToConfigurations From(GeneratorExecutionContext context) - { - var constructorAccessModifier = - context.AnalyzerConfigOptions.GlobalOptions.TryGetValue("build_property.MapTo_ConstructorAccessModifier", out var ctorModifierValue) && - Enum.TryParse(ctorModifierValue, out var ctorModifier) ? ctorModifier : AccessModifier.Public; - - return new MapToConfigurations( - constructorAccessModifier - ); - } - } -} \ No newline at end of file diff --git a/src/MapTo/Diagnostics.cs b/src/MapTo/Diagnostics.cs index f796d1a..41be782 100644 --- a/src/MapTo/Diagnostics.cs +++ b/src/MapTo/Diagnostics.cs @@ -5,18 +5,21 @@ namespace MapTo internal static class Diagnostics { private const string UsageCategory = "Usage"; - + private const string ErrorId = "MT0"; + private const string InfoId = "MT1"; + private const string WarningId = "MT2"; + internal static Diagnostic SymbolNotFoundError(Location location, string syntaxName) => - Create("MT0001", "Symbol not found.", $"Unable to find any symbols for {syntaxName}", location); + Create($"{ErrorId}001", "Symbol not found.", $"Unable to find any symbols for {syntaxName}", location); internal static Diagnostic MapFromAttributeNotFoundError(Location location) => - Create("MT0002", "Attribute Not Available", $"Unable to find {SourceBuilder.MapFromAttributeName} type.", location); - - internal static Diagnostic ClassMappingsGenerated(Location location, string typeName) => - Create("MT1001", "Mapped Type", $"Generated mappings for {typeName}", location, DiagnosticSeverity.Info); + Create($"{ErrorId}002", "Attribute Not Available", $"Unable to find {SourceBuilder.MapFromAttributeName} type.", location); internal static Diagnostic NoMatchingPropertyFoundError(Location location, string className, string sourceTypeName) => - Create("MT2001", "Property Not Found", $"No matching properties found between '{className}' and '{sourceTypeName}' types.", location); + Create($"{ErrorId}003", "Property Not Found", $"No matching properties found between '{className}' and '{sourceTypeName}' types.", location); + + internal static Diagnostic ConfigurationParseError(string error) => + Create($"{ErrorId}004", "Incorrect Configuration", error, Location.None); private static Diagnostic Create(string id, string title, string message, Location location, DiagnosticSeverity severity = DiagnosticSeverity.Error) => Diagnostic.Create(new DiagnosticDescriptor(id, title, message, UsageCategory, severity, true), location); diff --git a/src/MapTo/Extensions/EnumExtensions.cs b/src/MapTo/Extensions/EnumExtensions.cs new file mode 100644 index 0000000..f170a05 --- /dev/null +++ b/src/MapTo/Extensions/EnumExtensions.cs @@ -0,0 +1,9 @@ +using System; + +namespace MapTo.Extensions +{ + internal static class EnumExtensions + { + internal static string ToLowercaseString(this Enum member) => member.ToString().ToLower(); + } +} \ No newline at end of file diff --git a/src/MapTo/Extensions/GeneratorExecutionContextExtensions.cs b/src/MapTo/Extensions/GeneratorExecutionContextExtensions.cs new file mode 100644 index 0000000..2d29e91 --- /dev/null +++ b/src/MapTo/Extensions/GeneratorExecutionContextExtensions.cs @@ -0,0 +1,35 @@ +using System; +using Microsoft.CodeAnalysis; + +namespace MapTo.Extensions +{ + internal static class GeneratorExecutionContextExtensions + { + private const string PropertyNameSuffix = "MapTo_"; + + internal static T GetBuildGlobalOption(this GeneratorExecutionContext context, string propertyName, T defaultValue = default!) where T: notnull + { + if (!context.AnalyzerConfigOptions.GlobalOptions.TryGetValue($"build_property.{PropertyNameSuffix}{propertyName}", out var optionValue)) + { + return defaultValue; + } + + var type = typeof(T); + + if (!type.IsEnum) + { + return (T)Convert.ChangeType(optionValue, type); + } + + try + { + return (T)Enum.Parse(type, optionValue, true); + } + catch (Exception) + { + context.ReportDiagnostic(Diagnostics.ConfigurationParseError($"'{optionValue}' is not a valid value for {PropertyNameSuffix}{propertyName} property.")); + return defaultValue; + } + } + } +} \ No newline at end of file diff --git a/src/MapTo/MapToGenerator.cs b/src/MapTo/MapToGenerator.cs index 5d55bf3..8730d5f 100644 --- a/src/MapTo/MapToGenerator.cs +++ b/src/MapTo/MapToGenerator.cs @@ -1,7 +1,6 @@ using System.Collections.Generic; using System.Collections.Immutable; using System.Linq; -using MapTo.Configuration; using MapTo.Extensions; using MapTo.Models; using Microsoft.CodeAnalysis; @@ -21,23 +20,22 @@ namespace MapTo /// public void Execute(GeneratorExecutionContext context) { - AddAttribute(context, SourceBuilder.GenerateMapFromAttribute()); - AddAttribute(context, SourceBuilder.GenerateIgnorePropertyAttribute()); + var options = SourceGenerationOptions.From(context); + + AddAttribute(context, SourceBuilder.GenerateMapFromAttribute(options)); + AddAttribute(context, SourceBuilder.GenerateIgnorePropertyAttribute(options)); if (context.SyntaxReceiver is MapToSyntaxReceiver receiver && receiver.CandidateClasses.Any()) { - AddGeneratedMappingsClasses(context, receiver.CandidateClasses); + AddGeneratedMappingsClasses(context, receiver.CandidateClasses, options); } } - private static void AddGeneratedMappingsClasses(GeneratorExecutionContext context, IEnumerable candidateClasses) + private static void AddGeneratedMappingsClasses(GeneratorExecutionContext context, IEnumerable candidateClasses, SourceGenerationOptions options) { - var configs = MapToConfigurations.From(context); - foreach (var classSyntax in candidateClasses) { - - var model = CreateModel(context, classSyntax, configs); + var model = CreateModel(context, classSyntax, options); if (model is null) { continue; @@ -46,7 +44,6 @@ namespace MapTo var (source, hintName) = SourceBuilder.GenerateSource(model); context.AddSource(hintName, source); - context.ReportDiagnostic(Diagnostics.ClassMappingsGenerated(classSyntax.GetLocation(), model.ClassName)); } } @@ -64,7 +61,7 @@ namespace MapTo return sourceTypeExpressionSyntax is not null ? model.GetTypeInfo(sourceTypeExpressionSyntax.Type).Type as INamedTypeSymbol : null; } - private static MapModel? CreateModel(GeneratorExecutionContext context, ClassDeclarationSyntax classSyntax, MapToConfigurations configs) + private static MapModel? CreateModel(GeneratorExecutionContext context, ClassDeclarationSyntax classSyntax, SourceGenerationOptions sourceGenerationOptions) { var root = classSyntax.GetCompilationUnit(); var classSemanticModel = context.Compilation.GetSemanticModel(classSyntax.SyntaxTree); @@ -93,14 +90,14 @@ namespace MapTo } return new MapModel( + sourceGenerationOptions, root.GetNamespace(), classSyntax.Modifiers, className, sourceTypeSymbol.ContainingNamespace.ToString(), sourceClassName, sourceTypeSymbol.ToString(), - mappedProperties, - configs.ConstructorAccessModifier); + mappedProperties); } private static ImmutableArray GetMappedProperties(ITypeSymbol classSymbol, ITypeSymbol sourceTypeSymbol) diff --git a/src/MapTo/Configuration/AccessModifier.cs b/src/MapTo/Models/AccessModifier.cs similarity index 74% rename from src/MapTo/Configuration/AccessModifier.cs rename to src/MapTo/Models/AccessModifier.cs index 5e321a0..8788916 100644 --- a/src/MapTo/Configuration/AccessModifier.cs +++ b/src/MapTo/Models/AccessModifier.cs @@ -1,4 +1,4 @@ -namespace MapTo.Configuration +namespace MapTo.Models { internal enum AccessModifier { diff --git a/src/MapTo/Models/MapModel.cs b/src/MapTo/Models/MapModel.cs index 226e5d0..5d4190e 100644 --- a/src/MapTo/Models/MapModel.cs +++ b/src/MapTo/Models/MapModel.cs @@ -1,45 +1,16 @@ using System.Collections.Immutable; -using MapTo.Configuration; using Microsoft.CodeAnalysis; namespace MapTo.Models { - internal class MapModel - { - internal MapModel( - string? ns, - SyntaxTokenList classModifiers, - string className, - string sourceNamespace, - string sourceClassName, - string sourceClassFullName, - ImmutableArray mappedProperties, - AccessModifier constructorAccessModifier) - { - Namespace = ns; - ClassModifiers = classModifiers; - ClassName = className; - SourceNamespace = sourceNamespace; - SourceClassName = sourceClassName; - SourceClassFullName = sourceClassFullName; - MappedProperties = mappedProperties; - ConstructorAccessModifier = constructorAccessModifier; - } - - public string? Namespace { get; } - - public SyntaxTokenList ClassModifiers { get; } - - public string ClassName { get; } - - public string SourceNamespace { get; } - - public string SourceClassName { get; } - - public string SourceClassFullName { get; } - - public ImmutableArray MappedProperties { get; } - - public AccessModifier ConstructorAccessModifier { get; } - } + internal record MapModel ( + SourceGenerationOptions Options, + string? Namespace, + SyntaxTokenList ClassModifiers, + string ClassName, + string SourceNamespace, + string SourceClassName, + string SourceClassFullName, + ImmutableArray MappedProperties + ); } \ No newline at end of file diff --git a/src/MapTo/Models/SourceGenerationOptions.cs b/src/MapTo/Models/SourceGenerationOptions.cs new file mode 100644 index 0000000..3ba0cb7 --- /dev/null +++ b/src/MapTo/Models/SourceGenerationOptions.cs @@ -0,0 +1,17 @@ +using MapTo.Extensions; +using Microsoft.CodeAnalysis; + +namespace MapTo.Models +{ + internal record SourceGenerationOptions( + AccessModifier ConstructorAccessModifier, + AccessModifier MappingsAccessModifier, + bool GenerateXmlDocument) + { + internal static SourceGenerationOptions From(GeneratorExecutionContext context) => new( + context.GetBuildGlobalOption(nameof(ConstructorAccessModifier)), + context.GetBuildGlobalOption(nameof(MappingsAccessModifier)), + context.GetBuildGlobalOption(nameof(GenerateXmlDocument), defaultValue: true) + ); + } +} \ No newline at end of file diff --git a/src/MapTo/SourceBuilder.cs b/src/MapTo/SourceBuilder.cs index bbbc2c5..68e95ac 100644 --- a/src/MapTo/SourceBuilder.cs +++ b/src/MapTo/SourceBuilder.cs @@ -17,53 +17,85 @@ namespace MapTo private const int Indent2 = Indent1 * 2; private const int Indent3 = Indent1 * 3; - internal static (string source, string hintName) GenerateMapFromAttribute() + internal static (string source, string hintName) GenerateMapFromAttribute(SourceGenerationOptions options) { - var source = $@"{GeneratedFilesHeader} -using System; + var builder = new StringBuilder(); + builder + .AppendFileHeader() + .AppendLine("using System;") + .AppendLine() + .AppendFormat("namespace {0}", NamespaceName) + .AppendOpeningBracket(); + + if (options.GenerateXmlDocument) + { + builder + .PadLeft(Indent1).AppendLine("/// ") + .PadLeft(Indent1).AppendLine("/// Specifies that the annotated class can be mapped from the provided .") + .PadLeft(Indent1).AppendLine("/// "); + } -namespace MapTo -{{ - /// - /// Specifies that the annotated class can be mapped from the provided . - /// - [AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)] - public sealed class {MapFromAttributeName}Attribute : Attribute - {{ - /// - /// Initializes a new instance of the class - /// with the specified . - /// - public {MapFromAttributeName}Attribute(Type sourceType) - {{ - SourceType = sourceType; - }} + builder + .PadLeft(Indent1).AppendLine("[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]") + .PadLeft(Indent1).AppendFormat("public sealed class {0}Attribute : Attribute", MapFromAttributeName) + .AppendOpeningBracket(Indent1); - /// - /// Gets the type of the class that the annotated class should be able to map from. - /// - public Type SourceType {{ get; }} - }} -}}"; + if (options.GenerateXmlDocument) + { + builder + .PadLeft(Indent2).AppendLine("/// ") + .PadLeft(Indent2).AppendFormat("/// Initializes a new instance of the class with the specified .", MapFromAttributeName).AppendLine() + .PadLeft(Indent2).AppendLine("/// "); + } - return (source, $"{MapFromAttributeName}Attribute.g.cs"); + builder + .PadLeft(Indent2).AppendFormat("public {0}Attribute(Type sourceType)", MapFromAttributeName) + .AppendOpeningBracket(Indent2) + .PadLeft(Indent3).AppendLine("SourceType = sourceType;") + .AppendClosingBracket(Indent2, padNewLine: false) + .AppendLine() + .AppendLine(); + + if (options.GenerateXmlDocument) + { + builder + .PadLeft(Indent2).AppendLine("/// ") + .PadLeft(Indent2).AppendLine("/// Gets the type of the class that the annotated class should be able to map from.") + .PadLeft(Indent2).AppendLine("/// "); + } + + builder + .PadLeft(Indent2).AppendLine("public Type SourceType { get; }") + .AppendClosingBracket(Indent1, padNewLine: false) + .AppendClosingBracket(); + + return (builder.ToString(), $"{MapFromAttributeName}Attribute.g.cs"); } - internal static (string source, string hintName) GenerateIgnorePropertyAttribute() + internal static (string source, string hintName) GenerateIgnorePropertyAttribute(SourceGenerationOptions options) { - var source = $@"{GeneratedFilesHeader} -using System; + var builder = new StringBuilder(); + builder + .AppendFileHeader() + .AppendLine("using System;") + .AppendLine() + .AppendFormat("namespace {0}", NamespaceName) + .AppendOpeningBracket(); -namespace MapTo -{{ - /// - /// Specified that the annotated property should not be included in the generated mappings. - /// - [AttributeUsage(AttributeTargets.Property, Inherited = false, AllowMultiple = false)] - public sealed class {IgnorePropertyAttributeName}Attribute : Attribute {{ }} -}}"; + if (options.GenerateXmlDocument) + { + builder + .PadLeft(Indent1).AppendLine("/// ") + .PadLeft(Indent1).AppendLine("/// Specified that the annotated property should not be included in the generated mappings.") + .PadLeft(Indent1).AppendLine("/// "); + } - return (source, $"{IgnorePropertyAttributeName}Attribute.g.cs"); + builder + .PadLeft(Indent1).AppendLine("[AttributeUsage(AttributeTargets.Property, Inherited = false, AllowMultiple = false)]") + .PadLeft(Indent1).AppendFormat("public sealed class {0}Attribute : Attribute {{ }}", IgnorePropertyAttributeName) + .AppendClosingBracket(); + + return (builder.ToString(), $"{IgnorePropertyAttributeName}Attribute.g.cs"); } internal static (string source, string hintName) GenerateSource(MapModel model) @@ -116,23 +148,22 @@ namespace MapTo return builder.AppendLine(); } - /// - /// - /// - /// - /// - /// private static StringBuilder GenerateConstructor(this StringBuilder builder, MapModel model) { var sourceClassParameterName = model.SourceClassName.ToCamelCase(); + if (model.Options.GenerateXmlDocument) + { + builder + .PadLeft(Indent2).AppendLine("/// ") + .PadLeft(Indent2).AppendFormat("/// Initializes a new instance of the class", model.ClassName).AppendLine() + .PadLeft(Indent2).AppendFormat("/// using the property values from the specified .", sourceClassParameterName).AppendLine() + .PadLeft(Indent2).AppendLine("/// ") + .PadLeft(Indent2).AppendFormat("/// {0} is null", sourceClassParameterName).AppendLine(); + } + builder - .PadLeft(Indent2).AppendLine("/// ") - .PadLeft(Indent2).AppendFormat("/// Initializes a new instance of the class", model.ClassName).AppendLine() - .PadLeft(Indent2).AppendFormat("/// using the property values from the specified .", sourceClassParameterName).AppendLine() - .PadLeft(Indent2).AppendLine("/// ") - .PadLeft(Indent2).AppendFormat("/// {0} is null", sourceClassParameterName).AppendLine() - .PadLeft(Indent2).AppendFormat("{0} {1}({2} {3})", model.ConstructorAccessModifier.ToString().ToLower(), model.ClassName, model.SourceClassFullName, sourceClassParameterName) + .PadLeft(Indent2).AppendFormat("{0} {1}({2} {3})", model.Options.ConstructorAccessModifier.ToLowercaseString(), model.ClassName, model.SourceClassFullName, sourceClassParameterName) .AppendOpeningBracket(Indent2) .PadLeft(Indent3).AppendFormat("if ({0} == null) throw new ArgumentNullException(nameof({0}));", sourceClassParameterName).AppendLine() .AppendLine(); @@ -149,13 +180,6 @@ namespace MapTo return builder.AppendClosingBracket(Indent2, false); } - /// - /// - /// - /// - /// - /// - /// private static StringBuilder GenerateFactoryMethod(this StringBuilder builder, MapModel model) { var sourceClassParameterName = model.SourceClassName.ToCamelCase(); @@ -186,6 +210,11 @@ namespace MapTo private static StringBuilder AppendConvertorMethodsXmlDocs(this StringBuilder builder, MapModel model, string sourceClassParameterName) { + if (!model.Options.GenerateXmlDocument) + { + return builder; + } + return builder .PadLeft(Indent2).AppendLine("/// ") .PadLeft(Indent2).AppendFormat("/// Creates a new instance of and sets its participating properties", model.ClassName).AppendLine() diff --git a/test/MapTo.Tests/CSharpGenerator.cs b/test/MapTo.Tests/Infrastructure/CSharpGenerator.cs similarity index 82% rename from test/MapTo.Tests/CSharpGenerator.cs rename to test/MapTo.Tests/Infrastructure/CSharpGenerator.cs index 288595f..12bd416 100644 --- a/test/MapTo.Tests/CSharpGenerator.cs +++ b/test/MapTo.Tests/Infrastructure/CSharpGenerator.cs @@ -1,13 +1,12 @@ using System; +using System.Collections.Generic; using System.Collections.Immutable; using System.Linq; -using MapTo; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Xunit; -using Xunit.Abstractions; -namespace MapToTests +namespace MapTo.Tests.Infrastructure { internal static class CSharpGenerator { @@ -16,7 +15,7 @@ namespace MapToTests Assert.False(diagnostics.Any(d => d.Severity >= DiagnosticSeverity.Warning), $"Failed: {Environment.NewLine}{string.Join($"{Environment.NewLine}- ", diagnostics.Select(c => c.GetMessage()))}"); } - internal static (Compilation compilation, ImmutableArray diagnostics) GetOutputCompilation(string source, bool assertCompilation = false) + internal static (Compilation compilation, ImmutableArray diagnostics) GetOutputCompilation(string source, bool assertCompilation = false, IDictionary analyzerConfigOptions = null) { var syntaxTree = CSharpSyntaxTree.ParseText(source); var references = AppDomain.CurrentDomain.GetAssemblies() @@ -33,10 +32,12 @@ namespace MapToTests Assert.False(compileDiagnostics.Any(d => d.Severity == DiagnosticSeverity.Error), $"Failed: {Environment.NewLine}{string.Join($"{Environment.NewLine}- ", compileDiagnostics.Select(c => c.GetMessage()))}"); } - ISourceGenerator generator = new MapToGenerator(); - var driver = CSharpGeneratorDriver.Create(generator); - driver.RunGeneratorsAndUpdateCompilation(compilation, out var outputCompilation, out var generateDiagnostics); + var driver = CSharpGeneratorDriver.Create( + new[] { new MapToGenerator() }, + optionsProvider: new TestAnalyzerConfigOptionsProvider(analyzerConfigOptions) + ); + driver.RunGeneratorsAndUpdateCompilation(compilation, out var outputCompilation, out var generateDiagnostics); return (outputCompilation, generateDiagnostics); } } diff --git a/test/MapTo.Tests/Infrastructure/TestAnalyzerConfigOptions.cs b/test/MapTo.Tests/Infrastructure/TestAnalyzerConfigOptions.cs new file mode 100644 index 0000000..092e58b --- /dev/null +++ b/test/MapTo.Tests/Infrastructure/TestAnalyzerConfigOptions.cs @@ -0,0 +1,19 @@ +using System.Collections.Generic; +using System.Collections.Immutable; +using System.Diagnostics.CodeAnalysis; +using Microsoft.CodeAnalysis.Diagnostics; + +namespace MapTo.Tests.Infrastructure +{ + internal sealed class TestAnalyzerConfigOptions : AnalyzerConfigOptions + { + private readonly ImmutableDictionary _backing; + + public TestAnalyzerConfigOptions(IDictionary properties) + { + _backing = properties?.ToImmutableDictionary(KeyComparer) ?? ImmutableDictionary.Create(KeyComparer); + } + + public override bool TryGetValue(string key, [NotNullWhen(true)] out string value) => _backing.TryGetValue(key, out value); + } +} \ No newline at end of file diff --git a/test/MapTo.Tests/Infrastructure/TestAnalyzerConfigOptionsProvider.cs b/test/MapTo.Tests/Infrastructure/TestAnalyzerConfigOptionsProvider.cs new file mode 100644 index 0000000..91e2ae3 --- /dev/null +++ b/test/MapTo.Tests/Infrastructure/TestAnalyzerConfigOptionsProvider.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.Diagnostics; + +namespace MapTo.Tests.Infrastructure +{ + internal sealed class TestAnalyzerConfigOptionsProvider : AnalyzerConfigOptionsProvider + { + public TestAnalyzerConfigOptionsProvider(IDictionary options) + { + GlobalOptions = new TestAnalyzerConfigOptions(options); + } + + /// + public override AnalyzerConfigOptions GlobalOptions { get; } + + /// + public override AnalyzerConfigOptions GetOptions(SyntaxTree tree) => throw new NotImplementedException(); + + /// + public override AnalyzerConfigOptions GetOptions(AdditionalText textFile) => throw new NotImplementedException(); + } +} \ No newline at end of file diff --git a/test/MapTo.Tests/Tests.cs b/test/MapTo.Tests/Tests.cs index 037c093..a1ad52e 100644 --- a/test/MapTo.Tests/Tests.cs +++ b/test/MapTo.Tests/Tests.cs @@ -1,8 +1,9 @@ using System; +using System.Collections.Generic; using System.Linq; using System.Text; using MapTo.Extensions; -using MapToTests; +using MapTo.Tests.Infrastructure; using Microsoft.CodeAnalysis; using Shouldly; using Xunit; @@ -15,14 +16,12 @@ namespace MapTo.Tests private const int Indent1 = 4; private const int Indent2 = Indent1 * 2; private const int Indent3 = Indent1 * 3; - - public Tests(ITestOutputHelper output) + + private static readonly Dictionary DefaultAnalyzerOptions = new() { - _output = output; - } - - private readonly ITestOutputHelper _output; - + ["build_property.MapTo_GenerateXmlDocument"] = "false" + }; + private static readonly string ExpectedAttribute = $@"{SourceBuilder.GeneratedFilesHeader} using System; @@ -128,7 +127,7 @@ namespace MapTo const string source = ""; // Act - var (compilation, diagnostics) = CSharpGenerator.GetOutputCompilation(source); + var (compilation, diagnostics) = CSharpGenerator.GetOutputCompilation(source, analyzerConfigOptions: DefaultAnalyzerOptions); // Assert diagnostics.ShouldBeSuccessful(); @@ -174,7 +173,7 @@ namespace Test "; // Act - var (compilation, diagnostics) = CSharpGenerator.GetOutputCompilation(source); + var (compilation, diagnostics) = CSharpGenerator.GetOutputCompilation(source, analyzerConfigOptions: DefaultAnalyzerOptions); // Assert diagnostics.ShouldBeSuccessful(); @@ -237,7 +236,7 @@ namespace Test "; // Act - var (compilation, diagnostics) = CSharpGenerator.GetOutputCompilation(source); + var (compilation, diagnostics) = CSharpGenerator.GetOutputCompilation(source, analyzerConfigOptions: DefaultAnalyzerOptions); // Assert diagnostics.ShouldBeSuccessful(); @@ -303,7 +302,7 @@ namespace Test "; // Act - var (compilation, diagnostics) = CSharpGenerator.GetOutputCompilation(source); + var (compilation, diagnostics) = CSharpGenerator.GetOutputCompilation(source, analyzerConfigOptions: DefaultAnalyzerOptions); // Assert diagnostics.ShouldBeSuccessful(); @@ -348,7 +347,7 @@ namespace Test "; // Act - var (compilation, diagnostics) = CSharpGenerator.GetOutputCompilation(source); + var (compilation, diagnostics) = CSharpGenerator.GetOutputCompilation(source, analyzerConfigOptions: DefaultAnalyzerOptions); // Assert diagnostics.ShouldBeSuccessful(); @@ -372,7 +371,7 @@ namespace MapTo ".Trim(); // Act - var (compilation, diagnostics) = CSharpGenerator.GetOutputCompilation(source); + var (compilation, diagnostics) = CSharpGenerator.GetOutputCompilation(source, analyzerConfigOptions: DefaultAnalyzerOptions); // Assert diagnostics.ShouldBeSuccessful(); @@ -407,7 +406,7 @@ namespace MapTo ".Trim(); // Act - var (compilation, diagnostics) = CSharpGenerator.GetOutputCompilation(source); + var (compilation, diagnostics) = CSharpGenerator.GetOutputCompilation(source, analyzerConfigOptions: DefaultAnalyzerOptions); // Assert diagnostics.ShouldBeSuccessful(); @@ -441,7 +440,7 @@ namespace MapTo ".Trim(); // Act - var (compilation, diagnostics) = CSharpGenerator.GetOutputCompilation(source); + var (compilation, diagnostics) = CSharpGenerator.GetOutputCompilation(source, analyzerConfigOptions: DefaultAnalyzerOptions); // Assert diagnostics.ShouldBeSuccessful();