Enabled nullable reference type check.
This commit is contained in:
parent
c3bdc8f008
commit
98c899e078
|
@ -6,6 +6,7 @@ using MapTo.Sources;
|
||||||
using Microsoft.CodeAnalysis;
|
using Microsoft.CodeAnalysis;
|
||||||
using Microsoft.CodeAnalysis.CSharp;
|
using Microsoft.CodeAnalysis.CSharp;
|
||||||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||||||
|
using Shouldly;
|
||||||
|
|
||||||
namespace MapTo.Tests
|
namespace MapTo.Tests
|
||||||
{
|
{
|
||||||
|
@ -21,7 +22,7 @@ namespace MapTo.Tests
|
||||||
[GeneratorExecutionContextExtensions.GetBuildPropertyName(nameof(SourceGenerationOptions.GenerateXmlDocument))] = "false"
|
[GeneratorExecutionContextExtensions.GetBuildPropertyName(nameof(SourceGenerationOptions.GenerateXmlDocument))] = "false"
|
||||||
};
|
};
|
||||||
|
|
||||||
internal static string GetSourceText(SourceGeneratorOptions options = null)
|
internal static string GetSourceText(SourceGeneratorOptions? options = null)
|
||||||
{
|
{
|
||||||
const string ns = "Test";
|
const string ns = "Test";
|
||||||
options ??= new SourceGeneratorOptions();
|
options ??= new SourceGeneratorOptions();
|
||||||
|
@ -95,7 +96,12 @@ namespace MapTo.Tests
|
||||||
return builder.ToString();
|
return builder.ToString();
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static string[] GetEmployeeManagerSourceText(Func<string> employeeClassSource = null, Func<string> managerClassSource = null, Func<string> employeeViewModelSource = null, Func<string> managerViewModelSource = null, bool useDifferentViewModelNamespace = false)
|
internal static string[] GetEmployeeManagerSourceText(
|
||||||
|
Func<string>? employeeClassSource = null,
|
||||||
|
Func<string>? managerClassSource = null,
|
||||||
|
Func<string>? employeeViewModelSource = null,
|
||||||
|
Func<string>? managerViewModelSource = null,
|
||||||
|
bool useDifferentViewModelNamespace = false)
|
||||||
{
|
{
|
||||||
return new[]
|
return new[]
|
||||||
{
|
{
|
||||||
|
@ -231,7 +237,7 @@ namespace Test.ViewModels
|
||||||
var propSyntax = GetPropertyDeclarationSyntax(syntaxTree, propertyName, targetClass);
|
var propSyntax = GetPropertyDeclarationSyntax(syntaxTree, propertyName, targetClass);
|
||||||
|
|
||||||
var semanticModel = compilation.GetSemanticModel(syntaxTree);
|
var semanticModel = compilation.GetSemanticModel(syntaxTree);
|
||||||
return semanticModel.GetDeclaredSymbol(propSyntax);
|
return semanticModel.GetDeclaredSymbol(propSyntax).ShouldNotBeNull();
|
||||||
}
|
}
|
||||||
|
|
||||||
internal record SourceGeneratorOptions(
|
internal record SourceGeneratorOptions(
|
||||||
|
@ -239,8 +245,8 @@ namespace Test.ViewModels
|
||||||
string SourceClassNamespace = "Test.Models",
|
string SourceClassNamespace = "Test.Models",
|
||||||
int ClassPropertiesCount = 3,
|
int ClassPropertiesCount = 3,
|
||||||
int SourceClassPropertiesCount = 3,
|
int SourceClassPropertiesCount = 3,
|
||||||
Action<SourceBuilder> PropertyBuilder = null,
|
Action<SourceBuilder>? PropertyBuilder = null,
|
||||||
Action<SourceBuilder> SourcePropertyBuilder = null,
|
Action<SourceBuilder>? SourcePropertyBuilder = null,
|
||||||
IEnumerable<string> Usings = null);
|
IEnumerable<string>? Usings = null);
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -7,7 +7,7 @@ namespace MapTo.Tests.Extensions
|
||||||
{
|
{
|
||||||
internal static class RoslynExtensions
|
internal static class RoslynExtensions
|
||||||
{
|
{
|
||||||
internal static SyntaxTree GetGeneratedSyntaxTree(this Compilation compilation, string className) =>
|
internal static SyntaxTree? GetGeneratedSyntaxTree(this Compilation compilation, string className) =>
|
||||||
compilation.SyntaxTrees.SingleOrDefault(s => s.FilePath.EndsWith($"{className}.g.cs"));
|
compilation.SyntaxTrees.SingleOrDefault(s => s.FilePath.EndsWith($"{className}.g.cs"));
|
||||||
|
|
||||||
internal static string PrintSyntaxTree(this Compilation compilation)
|
internal static string PrintSyntaxTree(this Compilation compilation)
|
||||||
|
|
|
@ -11,7 +11,7 @@ namespace MapTo.Tests.Extensions
|
||||||
{
|
{
|
||||||
internal static class ShouldlyExtensions
|
internal static class ShouldlyExtensions
|
||||||
{
|
{
|
||||||
internal static void ShouldContainSource(this IEnumerable<SyntaxTree> syntaxTree, string typeName, string expectedSource, string customMessage = null)
|
internal static void ShouldContainSource(this IEnumerable<SyntaxTree> syntaxTree, string typeName, string expectedSource, string? customMessage = null)
|
||||||
{
|
{
|
||||||
var syntax = syntaxTree
|
var syntax = syntaxTree
|
||||||
.Select(s => s.ToString().Trim())
|
.Select(s => s.ToString().Trim())
|
||||||
|
@ -21,7 +21,7 @@ namespace MapTo.Tests.Extensions
|
||||||
syntax.ShouldBe(expectedSource, customMessage);
|
syntax.ShouldBe(expectedSource, customMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static void ShouldContainPartialSource(this IEnumerable<SyntaxTree> syntaxTree, string typeName, string expectedSource, string customMessage = null)
|
internal static void ShouldContainPartialSource(this IEnumerable<SyntaxTree> syntaxTree, string typeName, string expectedSource, string? customMessage = null)
|
||||||
{
|
{
|
||||||
var syntax = syntaxTree
|
var syntax = syntaxTree
|
||||||
.Select(s => s.ToString().Trim())
|
.Select(s => s.ToString().Trim())
|
||||||
|
@ -31,14 +31,14 @@ namespace MapTo.Tests.Extensions
|
||||||
syntax.ShouldContainWithoutWhitespace(expectedSource, customMessage);
|
syntax.ShouldContainWithoutWhitespace(expectedSource, customMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static void ShouldContainPartialSource(this SyntaxTree syntaxTree, string expectedSource, string customMessage = null)
|
internal static void ShouldContainPartialSource(this SyntaxTree syntaxTree, string expectedSource, string? customMessage = null)
|
||||||
{
|
{
|
||||||
var syntax = syntaxTree.ToString();
|
var syntax = syntaxTree.ToString();
|
||||||
syntax.ShouldNotBeNullOrWhiteSpace();
|
syntax.ShouldNotBeNullOrWhiteSpace();
|
||||||
syntax.ShouldContainWithoutWhitespace(expectedSource, customMessage);
|
syntax.ShouldContainWithoutWhitespace(expectedSource, customMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static void ShouldBeSuccessful(this IEnumerable<Diagnostic> diagnostics, Compilation compilation = null, IEnumerable<string> ignoreDiagnosticsIds = null)
|
internal static void ShouldBeSuccessful(this IEnumerable<Diagnostic> diagnostics, Compilation? compilation = null, IEnumerable<string>? ignoreDiagnosticsIds = null)
|
||||||
{
|
{
|
||||||
var actual = diagnostics
|
var actual = diagnostics
|
||||||
.Where(d => (ignoreDiagnosticsIds is null || ignoreDiagnosticsIds.All(i => !d.Id.StartsWith(i) )) && (d.Severity == DiagnosticSeverity.Warning || d.Severity == DiagnosticSeverity.Error))
|
.Where(d => (ignoreDiagnosticsIds is null || ignoreDiagnosticsIds.All(i => !d.Id.StartsWith(i) )) && (d.Severity == DiagnosticSeverity.Warning || d.Severity == DiagnosticSeverity.Error))
|
||||||
|
@ -74,14 +74,14 @@ namespace MapTo.Tests.Extensions
|
||||||
compilationDiagnostics.ShouldBeSuccessful();
|
compilationDiagnostics.ShouldBeSuccessful();
|
||||||
|
|
||||||
Assert.NotNull(actualDiagnostics);
|
Assert.NotNull(actualDiagnostics);
|
||||||
Assert.Equal(expectedError.Id, actualDiagnostics.Id);
|
Assert.Equal(expectedError.Id, actualDiagnostics?.Id);
|
||||||
Assert.Equal(expectedError.Descriptor.Id, actualDiagnostics.Descriptor.Id);
|
Assert.Equal(expectedError.Descriptor.Id, actualDiagnostics?.Descriptor.Id);
|
||||||
Assert.Equal(expectedError.Descriptor.Description, actualDiagnostics.Descriptor.Description);
|
Assert.Equal(expectedError.Descriptor.Description, actualDiagnostics?.Descriptor.Description);
|
||||||
Assert.Equal(expectedError.Descriptor.Title, actualDiagnostics.Descriptor.Title);
|
Assert.Equal(expectedError.Descriptor.Title, actualDiagnostics?.Descriptor.Title);
|
||||||
|
|
||||||
if (expectedError.Location != Location.None)
|
if (expectedError.Location != Location.None)
|
||||||
{
|
{
|
||||||
Assert.Equal(expectedError.Location, actualDiagnostics.Location);
|
Assert.Equal(expectedError.Location, actualDiagnostics?.Location);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,6 @@ using System.Linq;
|
||||||
using MapTo.Tests.Extensions;
|
using MapTo.Tests.Extensions;
|
||||||
using Microsoft.CodeAnalysis;
|
using Microsoft.CodeAnalysis;
|
||||||
using Microsoft.CodeAnalysis.CSharp;
|
using Microsoft.CodeAnalysis.CSharp;
|
||||||
using Xunit;
|
|
||||||
|
|
||||||
namespace MapTo.Tests.Infrastructure
|
namespace MapTo.Tests.Infrastructure
|
||||||
{
|
{
|
||||||
|
@ -14,7 +13,7 @@ namespace MapTo.Tests.Infrastructure
|
||||||
internal static (Compilation compilation, ImmutableArray<Diagnostic> diagnostics) GetOutputCompilation(
|
internal static (Compilation compilation, ImmutableArray<Diagnostic> diagnostics) GetOutputCompilation(
|
||||||
string source,
|
string source,
|
||||||
bool assertCompilation = false,
|
bool assertCompilation = false,
|
||||||
IDictionary<string, string> analyzerConfigOptions = null,
|
IDictionary<string, string>? analyzerConfigOptions = null,
|
||||||
NullableContextOptions nullableContextOptions = NullableContextOptions.Disable,
|
NullableContextOptions nullableContextOptions = NullableContextOptions.Disable,
|
||||||
LanguageVersion languageVersion = LanguageVersion.CSharp7_3) =>
|
LanguageVersion languageVersion = LanguageVersion.CSharp7_3) =>
|
||||||
GetOutputCompilation(
|
GetOutputCompilation(
|
||||||
|
@ -27,7 +26,7 @@ namespace MapTo.Tests.Infrastructure
|
||||||
internal static (Compilation compilation, ImmutableArray<Diagnostic> diagnostics) GetOutputCompilation(
|
internal static (Compilation compilation, ImmutableArray<Diagnostic> diagnostics) GetOutputCompilation(
|
||||||
IEnumerable<string> sources,
|
IEnumerable<string> sources,
|
||||||
bool assertCompilation = false,
|
bool assertCompilation = false,
|
||||||
IDictionary<string, string> analyzerConfigOptions = null,
|
IDictionary<string, string>? analyzerConfigOptions = null,
|
||||||
NullableContextOptions nullableContextOptions = NullableContextOptions.Disable,
|
NullableContextOptions nullableContextOptions = NullableContextOptions.Disable,
|
||||||
LanguageVersion languageVersion = LanguageVersion.CSharp7_3)
|
LanguageVersion languageVersion = LanguageVersion.CSharp7_3)
|
||||||
{
|
{
|
||||||
|
|
|
@ -9,11 +9,11 @@ namespace MapTo.Tests.Infrastructure
|
||||||
{
|
{
|
||||||
private readonly ImmutableDictionary<string, string> _backing;
|
private readonly ImmutableDictionary<string, string> _backing;
|
||||||
|
|
||||||
public TestAnalyzerConfigOptions(IDictionary<string, string> properties)
|
public TestAnalyzerConfigOptions(IDictionary<string, string>? properties)
|
||||||
{
|
{
|
||||||
_backing = properties?.ToImmutableDictionary(KeyComparer) ?? ImmutableDictionary.Create<string, string>(KeyComparer);
|
_backing = properties?.ToImmutableDictionary(KeyComparer) ?? ImmutableDictionary.Create<string, string>(KeyComparer);
|
||||||
}
|
}
|
||||||
|
|
||||||
public override bool TryGetValue(string key, [NotNullWhen(true)] out string value) => _backing.TryGetValue(key, out value);
|
public override bool TryGetValue(string key, [NotNullWhen(true)] out string? value) => _backing.TryGetValue(key, out value);
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -7,7 +7,7 @@ namespace MapTo.Tests.Infrastructure
|
||||||
{
|
{
|
||||||
internal sealed class TestAnalyzerConfigOptionsProvider : AnalyzerConfigOptionsProvider
|
internal sealed class TestAnalyzerConfigOptionsProvider : AnalyzerConfigOptionsProvider
|
||||||
{
|
{
|
||||||
public TestAnalyzerConfigOptionsProvider(IDictionary<string, string> options)
|
public TestAnalyzerConfigOptionsProvider(IDictionary<string, string>? options)
|
||||||
{
|
{
|
||||||
GlobalOptions = new TestAnalyzerConfigOptions(options);
|
GlobalOptions = new TestAnalyzerConfigOptions(options);
|
||||||
}
|
}
|
||||||
|
|
|
@ -52,18 +52,19 @@ namespace Test.Data.Models
|
||||||
".Trim();
|
".Trim();
|
||||||
// Act
|
// Act
|
||||||
var (compilation, diagnostics) = CSharpGenerator.GetOutputCompilation(source, analyzerConfigOptions: DefaultAnalyzerOptions);
|
var (compilation, diagnostics) = CSharpGenerator.GetOutputCompilation(source, analyzerConfigOptions: DefaultAnalyzerOptions);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
diagnostics.ShouldBeSuccessful();
|
diagnostics.ShouldBeSuccessful();
|
||||||
compilation
|
compilation
|
||||||
.GetGeneratedSyntaxTree("DestinationClass")
|
.GetGeneratedSyntaxTree("DestinationClass")
|
||||||
|
.ShouldNotBeNull()
|
||||||
.GetRoot()
|
.GetRoot()
|
||||||
.DescendantNodes()
|
.DescendantNodes()
|
||||||
.OfType<ConstructorDeclarationSyntax>()
|
.OfType<ConstructorDeclarationSyntax>()
|
||||||
.Count()
|
.Count()
|
||||||
.ShouldBe(1);
|
.ShouldBe(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void When_SecondaryConstructorExistsButDoNotReferencePrivateConstructor_Should_ReportError()
|
public void When_SecondaryConstructorExistsButDoNotReferencePrivateConstructor_Should_ReportError()
|
||||||
{
|
{
|
||||||
|
@ -84,7 +85,7 @@ namespace Test.Data.Models
|
||||||
".Trim();
|
".Trim();
|
||||||
// Act
|
// Act
|
||||||
var (compilation, diagnostics) = CSharpGenerator.GetOutputCompilation(source, analyzerConfigOptions: DefaultAnalyzerOptions);
|
var (compilation, diagnostics) = CSharpGenerator.GetOutputCompilation(source, analyzerConfigOptions: DefaultAnalyzerOptions);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
var constructorSyntax = compilation.SyntaxTrees
|
var constructorSyntax = compilation.SyntaxTrees
|
||||||
.First()
|
.First()
|
||||||
|
@ -92,7 +93,7 @@ namespace Test.Data.Models
|
||||||
.DescendantNodes()
|
.DescendantNodes()
|
||||||
.OfType<ConstructorDeclarationSyntax>()
|
.OfType<ConstructorDeclarationSyntax>()
|
||||||
.Single();
|
.Single();
|
||||||
|
|
||||||
diagnostics.ShouldNotBeSuccessful(DiagnosticsFactory.MissingConstructorArgument(constructorSyntax));
|
diagnostics.ShouldNotBeSuccessful(DiagnosticsFactory.MissingConstructorArgument(constructorSyntax));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue