Skip to content

Commit d0dbf0c

Browse files
committed
feat!: net10 supports
1 parent fb2e69e commit d0dbf0c

4 files changed

Lines changed: 138 additions & 3 deletions

File tree

ProjectR.Sample/ProjectR.Sample.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
<ImplicitUsings>enable</ImplicitUsings>
77
<GenerateDocumentationFile>true</GenerateDocumentationFile>
88
<IsPackable>false</IsPackable>
9+
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
10+
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)Generated</CompilerGeneratedFilesOutputPath>
911
</PropertyGroup>
1012

1113
<ItemGroup>

ProjectR.Tests/GeneratorTests.cs

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
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+
}

ProjectR.Tests/ProjectR.Tests.csproj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net8.0</TargetFramework>
4+
<TargetFramework>net10.0</TargetFramework>
55
<ImplicitUsings>enable</ImplicitUsings>
66
<Nullable>enable</Nullable>
77

@@ -24,6 +24,7 @@
2424
<PackageReference Include="FluentAssertions" Version="6.12.0" />
2525
<PackageReference Include="Moq" Version="4.20.69" />
2626
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
27+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.SourceGenerators.Testing.XUnit" Version="1.1.2-beta1.23306.2" />
2728
</ItemGroup>
2829

2930
<ItemGroup>

ProjectR/Mapping/MapperGenerator.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ private static void Execute(Compilation compilation,
101101
public partial class {mapperName} : global::ProjectR.Mapper<{entityFullName}, {dtoFullName}> {{ }}
102102
}}";
103103
context.AddSource($"{mapperName}.ph.g.cs", sourceText);
104-
var syntaxTree = CSharpSyntaxTree.ParseText(sourceText, path: $"{mapperName}.ph.g.cs");
104+
var parseOptions = (compilation.SyntaxTrees.FirstOrDefault()?.Options as CSharpParseOptions) ?? CSharpParseOptions.Default;
105+
var syntaxTree = CSharpSyntaxTree.ParseText(sourceText, parseOptions, path: $"{mapperName}.ph.g.cs");
105106
placeholderSyntaxTrees.Add(syntaxTree);
106107

107108
var placeholderClassNode = syntaxTree.GetRoot().DescendantNodes().OfType<ClassDeclarationSyntax>().First();

0 commit comments

Comments
 (0)