From 98c899e0782caa86f34c148f1beacd3afdf09208 Mon Sep 17 00:00:00 2001 From: Mohammadreza Taikandi Date: Tue, 29 Jun 2021 07:14:00 +0100 Subject: [PATCH] Enabled nullable reference type check. --- test/MapTo.Tests/Common.cs | 18 ++++++++++++------ .../MapTo.Tests/Extensions/RoslynExtensions.cs | 2 +- .../Extensions/ShouldlyExtensions.cs | 18 +++++++++--------- .../Infrastructure/CSharpGenerator.cs | 5 ++--- .../TestAnalyzerConfigOptions.cs | 4 ++-- .../TestAnalyzerConfigOptionsProvider.cs | 2 +- test/MapTo.Tests/MappedClassesTests.cs | 9 +++++---- 7 files changed, 32 insertions(+), 26 deletions(-) diff --git a/test/MapTo.Tests/Common.cs b/test/MapTo.Tests/Common.cs index 374c489..f9113c9 100644 --- a/test/MapTo.Tests/Common.cs +++ b/test/MapTo.Tests/Common.cs @@ -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 employeeClassSource = null, Func managerClassSource = null, Func employeeViewModelSource = null, Func managerViewModelSource = null, bool useDifferentViewModelNamespace = false) + internal static string[] GetEmployeeManagerSourceText( + Func? employeeClassSource = null, + Func? managerClassSource = null, + Func? employeeViewModelSource = null, + Func? 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 PropertyBuilder = null, - Action SourcePropertyBuilder = null, - IEnumerable Usings = null); + Action? PropertyBuilder = null, + Action? SourcePropertyBuilder = null, + IEnumerable? Usings = null); } } \ No newline at end of file diff --git a/test/MapTo.Tests/Extensions/RoslynExtensions.cs b/test/MapTo.Tests/Extensions/RoslynExtensions.cs index 98d5b70..e21be73 100644 --- a/test/MapTo.Tests/Extensions/RoslynExtensions.cs +++ b/test/MapTo.Tests/Extensions/RoslynExtensions.cs @@ -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) diff --git a/test/MapTo.Tests/Extensions/ShouldlyExtensions.cs b/test/MapTo.Tests/Extensions/ShouldlyExtensions.cs index e080ac9..0daa7a7 100644 --- a/test/MapTo.Tests/Extensions/ShouldlyExtensions.cs +++ b/test/MapTo.Tests/Extensions/ShouldlyExtensions.cs @@ -11,7 +11,7 @@ namespace MapTo.Tests.Extensions { internal static class ShouldlyExtensions { - internal static void ShouldContainSource(this IEnumerable syntaxTree, string typeName, string expectedSource, string customMessage = null) + internal static void ShouldContainSource(this IEnumerable 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, string typeName, string expectedSource, string customMessage = null) + internal static void ShouldContainPartialSource(this IEnumerable 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 diagnostics, Compilation compilation = null, IEnumerable ignoreDiagnosticsIds = null) + internal static void ShouldBeSuccessful(this IEnumerable diagnostics, Compilation? compilation = null, IEnumerable? 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); } } } diff --git a/test/MapTo.Tests/Infrastructure/CSharpGenerator.cs b/test/MapTo.Tests/Infrastructure/CSharpGenerator.cs index ae98a85..da1ec81 100644 --- a/test/MapTo.Tests/Infrastructure/CSharpGenerator.cs +++ b/test/MapTo.Tests/Infrastructure/CSharpGenerator.cs @@ -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 diagnostics) GetOutputCompilation( string source, bool assertCompilation = false, - IDictionary analyzerConfigOptions = null, + IDictionary? 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 diagnostics) GetOutputCompilation( IEnumerable sources, bool assertCompilation = false, - IDictionary analyzerConfigOptions = null, + IDictionary? analyzerConfigOptions = null, NullableContextOptions nullableContextOptions = NullableContextOptions.Disable, LanguageVersion languageVersion = LanguageVersion.CSharp7_3) { diff --git a/test/MapTo.Tests/Infrastructure/TestAnalyzerConfigOptions.cs b/test/MapTo.Tests/Infrastructure/TestAnalyzerConfigOptions.cs index 092e58b..dd13340 100644 --- a/test/MapTo.Tests/Infrastructure/TestAnalyzerConfigOptions.cs +++ b/test/MapTo.Tests/Infrastructure/TestAnalyzerConfigOptions.cs @@ -9,11 +9,11 @@ namespace MapTo.Tests.Infrastructure { private readonly ImmutableDictionary _backing; - public TestAnalyzerConfigOptions(IDictionary properties) + 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); + 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 index 91e2ae3..914901d 100644 --- a/test/MapTo.Tests/Infrastructure/TestAnalyzerConfigOptionsProvider.cs +++ b/test/MapTo.Tests/Infrastructure/TestAnalyzerConfigOptionsProvider.cs @@ -7,7 +7,7 @@ namespace MapTo.Tests.Infrastructure { internal sealed class TestAnalyzerConfigOptionsProvider : AnalyzerConfigOptionsProvider { - public TestAnalyzerConfigOptionsProvider(IDictionary options) + public TestAnalyzerConfigOptionsProvider(IDictionary? options) { GlobalOptions = new TestAnalyzerConfigOptions(options); } diff --git a/test/MapTo.Tests/MappedClassesTests.cs b/test/MapTo.Tests/MappedClassesTests.cs index 84d6a24..d120da2 100644 --- a/test/MapTo.Tests/MappedClassesTests.cs +++ b/test/MapTo.Tests/MappedClassesTests.cs @@ -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() .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() .Single(); - + diagnostics.ShouldNotBeSuccessful(DiagnosticsFactory.MissingConstructorArgument(constructorSyntax)); }