|
| 1 | +using System.Threading.Tasks; |
| 2 | +using System.Collections.Immutable; |
| 3 | +using Microsoft.CodeAnalysis; |
| 4 | +using Microsoft.CodeAnalysis.CSharp; |
| 5 | +using Microsoft.CodeAnalysis.CSharp.Testing; |
| 6 | +using Microsoft.CodeAnalysis.Testing; |
| 7 | +using Microsoft.CodeAnalysis.Testing.Verifiers; |
| 8 | +using Xunit; |
| 9 | +using ProjectR; |
| 10 | + |
| 11 | +namespace ProjectR.Tests |
| 12 | +{ |
| 13 | + public static class CSharpSourceGeneratorVerifier<TSourceGenerator> |
| 14 | + where TSourceGenerator : IIncrementalGenerator, new() |
| 15 | + { |
| 16 | + public class Test : CSharpSourceGeneratorTest<TSourceGenerator, XUnitVerifier> |
| 17 | + { |
| 18 | + public Test() |
| 19 | + { |
| 20 | + ReferenceAssemblies = ReferenceAssemblies.Net.Net80; |
| 21 | + } |
| 22 | + |
| 23 | + protected override CompilationOptions CreateCompilationOptions() |
| 24 | + { |
| 25 | + var options = base.CreateCompilationOptions(); |
| 26 | + return options.WithSpecificDiagnosticOptions( |
| 27 | + options.SpecificDiagnosticOptions.SetItems(CSharpVerifierHelper.NullableWarnings)); |
| 28 | + } |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + internal static class CSharpVerifierHelper |
| 33 | + { |
| 34 | + /// <summary> |
| 35 | + /// By default, the compiler reports diagnostics for nullable reference types at |
| 36 | + /// <see cref="DiagnosticSeverity.Warning"/>, and the analyzer test framework defaults to only validating |
| 37 | + /// diagnostics at <see cref="DiagnosticSeverity.Error"/>. This map contains all compiler diagnostic IDs |
| 38 | + /// related to nullability wrapped with <see cref="ReportDiagnostic.Error"/>, which is then used to enable all |
| 39 | + /// of these warnings for default validation during analyzer and code fix tests. |
| 40 | + /// </summary> |
| 41 | + internal static ImmutableDictionary<string, ReportDiagnostic> NullableWarnings { get; } = GetNullableWarningsFromCompiler(); |
| 42 | + |
| 43 | + private static ImmutableDictionary<string, ReportDiagnostic> GetNullableWarningsFromCompiler() |
| 44 | + { |
| 45 | + string[] args = { "/warnaserror:nullable" }; |
| 46 | + var commandLineArguments = CSharpCommandLineParser.Default.Parse(args, baseDirectory: null, sdkDirectory: null); |
| 47 | + return commandLineArguments.CompilationOptions.SpecificDiagnosticOptions; |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + public class GeneratorTests |
| 52 | + { |
| 53 | + [Fact] |
| 54 | + public async Task SimpleMapping_GeneratesCorrectCode() |
| 55 | + { |
| 56 | + var source = @" |
| 57 | +using ProjectR; |
| 58 | +
|
| 59 | +namespace TestNamespace |
| 60 | +{ |
| 61 | + public class Source |
| 62 | + { |
| 63 | + public int Id { get; set; } |
| 64 | + public string Name { get; set; } |
| 65 | + } |
| 66 | +
|
| 67 | + public class Destination |
| 68 | + { |
| 69 | + public int Id { get; set; } |
| 70 | + public string Name { get; set; } |
| 71 | + } |
| 72 | +
|
| 73 | + public partial class TestMapper : Mapper<Source, Destination> |
| 74 | + { |
| 75 | + } |
| 76 | +}"; |
| 77 | + |
| 78 | + var expected = @"// <auto-generated/> |
| 79 | +using System; |
| 80 | +using System.Linq; |
| 81 | +using ProjectR; |
| 82 | +
|
| 83 | +namespace TestNamespace |
| 84 | +{ |
| 85 | + public partial class TestMapper |
| 86 | + { |
| 87 | + public override TestNamespace.Destination ProjectGenerated(TestNamespace.Source source) |
| 88 | + { |
| 89 | + if (source is null) return default(TestNamespace.Destination); |
| 90 | +
|
| 91 | + return new TestNamespace.Destination |
| 92 | + { |
| 93 | + Id = source.Id, |
| 94 | + Name = source.Name, |
| 95 | + }; |
| 96 | +
|
| 97 | + } |
| 98 | +
|
| 99 | + public override TestNamespace.Source BuildGenerated(TestNamespace.Destination source) |
| 100 | + { |
| 101 | + if (source is null) return default(TestNamespace.Source); |
| 102 | +
|
| 103 | + return new TestNamespace.Source |
| 104 | + { |
| 105 | + Id = source.Id, |
| 106 | + Name = source.Name, |
| 107 | + }; |
| 108 | +
|
| 109 | + } |
| 110 | +
|
| 111 | + } |
| 112 | +} |
| 113 | +"; |
| 114 | + |
| 115 | + var test = new CSharpSourceGeneratorVerifier<MapperGenerator>.Test |
| 116 | + { |
| 117 | + TestState = |
| 118 | + { |
| 119 | + Sources = { source }, |
| 120 | + GeneratedSources = |
| 121 | + { |
| 122 | + (typeof(MapperGenerator), "TestMapper.g.cs", expected) |
| 123 | + }, |
| 124 | + AdditionalReferences = { MetadataReference.CreateFromFile(typeof(ProjectR.Mapper<,>).Assembly.Location) } |
| 125 | + }, |
| 126 | + }; |
| 127 | + |
| 128 | + await test.RunAsync(); |
| 129 | + } |
| 130 | + } |
| 131 | +} |
0 commit comments