Enabled nullable reference type check.

This commit is contained in:
Mohammadreza Taikandi 2021-06-29 07:14:00 +01:00
parent c3bdc8f008
commit 98c899e078
7 changed files with 32 additions and 26 deletions

View File

@ -6,6 +6,7 @@ using MapTo.Sources;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Shouldly;
namespace MapTo.Tests
{
@ -21,7 +22,7 @@ namespace MapTo.Tests
[GeneratorExecutionContextExtensions.GetBuildPropertyName(nameof(SourceGenerationOptions.GenerateXmlDocument))] = "false"
};
internal static string GetSourceText(SourceGeneratorOptions options = null)
internal static string GetSourceText(SourceGeneratorOptions? options = null)
{
const string ns = "Test";
options ??= new SourceGeneratorOptions();
@ -95,7 +96,12 @@ namespace MapTo.Tests
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[]
{
@ -231,7 +237,7 @@ namespace Test.ViewModels
var propSyntax = GetPropertyDeclarationSyntax(syntaxTree, propertyName, targetClass);
var semanticModel = compilation.GetSemanticModel(syntaxTree);
return semanticModel.GetDeclaredSymbol(propSyntax);
return semanticModel.GetDeclaredSymbol(propSyntax).ShouldNotBeNull();
}
internal record SourceGeneratorOptions(
@ -239,8 +245,8 @@ namespace Test.ViewModels
string SourceClassNamespace = "Test.Models",
int ClassPropertiesCount = 3,
int SourceClassPropertiesCount = 3,
Action<SourceBuilder> PropertyBuilder = null,
Action<SourceBuilder> SourcePropertyBuilder = null,
IEnumerable<string> Usings = null);
Action<SourceBuilder>? PropertyBuilder = null,
Action<SourceBuilder>? SourcePropertyBuilder = null,
IEnumerable<string>? Usings = null);
}
}

View File

@ -7,7 +7,7 @@ namespace MapTo.Tests.Extensions
{
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"));
internal static string PrintSyntaxTree(this Compilation compilation)

View File

@ -11,7 +11,7 @@ namespace MapTo.Tests.Extensions
{
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
.Select(s => s.ToString().Trim())
@ -21,7 +21,7 @@ namespace MapTo.Tests.Extensions
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
.Select(s => s.ToString().Trim())
@ -31,14 +31,14 @@ namespace MapTo.Tests.Extensions
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();
syntax.ShouldNotBeNullOrWhiteSpace();
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
.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();
Assert.NotNull(actualDiagnostics);
Assert.Equal(expectedError.Id, actualDiagnostics.Id);
Assert.Equal(expectedError.Descriptor.Id, actualDiagnostics.Descriptor.Id);
Assert.Equal(expectedError.Descriptor.Description, actualDiagnostics.Descriptor.Description);
Assert.Equal(expectedError.Descriptor.Title, actualDiagnostics.Descriptor.Title);
Assert.Equal(expectedError.Id, actualDiagnostics?.Id);
Assert.Equal(expectedError.Descriptor.Id, actualDiagnostics?.Descriptor.Id);
Assert.Equal(expectedError.Descriptor.Description, actualDiagnostics?.Descriptor.Description);
Assert.Equal(expectedError.Descriptor.Title, actualDiagnostics?.Descriptor.Title);
if (expectedError.Location != Location.None)
{
Assert.Equal(expectedError.Location, actualDiagnostics.Location);
Assert.Equal(expectedError.Location, actualDiagnostics?.Location);
}
}
}

View File

@ -5,7 +5,6 @@ using System.Linq;
using MapTo.Tests.Extensions;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Xunit;
namespace MapTo.Tests.Infrastructure
{
@ -14,7 +13,7 @@ namespace MapTo.Tests.Infrastructure
internal static (Compilation compilation, ImmutableArray<Diagnostic> diagnostics) GetOutputCompilation(
string source,
bool assertCompilation = false,
IDictionary<string, string> analyzerConfigOptions = null,
IDictionary<string, string>? analyzerConfigOptions = null,
NullableContextOptions nullableContextOptions = NullableContextOptions.Disable,
LanguageVersion languageVersion = LanguageVersion.CSharp7_3) =>
GetOutputCompilation(
@ -27,7 +26,7 @@ namespace MapTo.Tests.Infrastructure
internal static (Compilation compilation, ImmutableArray<Diagnostic> diagnostics) GetOutputCompilation(
IEnumerable<string> sources,
bool assertCompilation = false,
IDictionary<string, string> analyzerConfigOptions = null,
IDictionary<string, string>? analyzerConfigOptions = null,
NullableContextOptions nullableContextOptions = NullableContextOptions.Disable,
LanguageVersion languageVersion = LanguageVersion.CSharp7_3)
{

View File

@ -9,11 +9,11 @@ namespace MapTo.Tests.Infrastructure
{
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);
}
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);
}
}

View File

@ -7,7 +7,7 @@ namespace MapTo.Tests.Infrastructure
{
internal sealed class TestAnalyzerConfigOptionsProvider : AnalyzerConfigOptionsProvider
{
public TestAnalyzerConfigOptionsProvider(IDictionary<string, string> options)
public TestAnalyzerConfigOptionsProvider(IDictionary<string, string>? options)
{
GlobalOptions = new TestAnalyzerConfigOptions(options);
}

View File

@ -52,18 +52,19 @@ namespace Test.Data.Models
".Trim();
// Act
var (compilation, diagnostics) = CSharpGenerator.GetOutputCompilation(source, analyzerConfigOptions: DefaultAnalyzerOptions);
// Assert
diagnostics.ShouldBeSuccessful();
compilation
.GetGeneratedSyntaxTree("DestinationClass")
.ShouldNotBeNull()
.GetRoot()
.DescendantNodes()
.OfType<ConstructorDeclarationSyntax>()
.Count()
.ShouldBe(1);
}
[Fact]
public void When_SecondaryConstructorExistsButDoNotReferencePrivateConstructor_Should_ReportError()
{
@ -84,7 +85,7 @@ namespace Test.Data.Models
".Trim();
// Act
var (compilation, diagnostics) = CSharpGenerator.GetOutputCompilation(source, analyzerConfigOptions: DefaultAnalyzerOptions);
// Assert
var constructorSyntax = compilation.SyntaxTrees
.First()
@ -92,7 +93,7 @@ namespace Test.Data.Models
.DescendantNodes()
.OfType<ConstructorDeclarationSyntax>()
.Single();
diagnostics.ShouldNotBeSuccessful(DiagnosticsFactory.MissingConstructorArgument(constructorSyntax));
}