From 375ca974480406e75cab83add07b073a135546bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Gallienne?= Date: Wed, 9 Mar 2022 20:39:12 +0100 Subject: [PATCH] Disable null checks by default Fix #23 --- README.md | 8 ++-- .../AutoConstructorGenerator.cs | 2 +- tests/AutoConstructor.Tests/GeneratorTests.cs | 37 ++++++------------- .../CSharpSourceGeneratorVerifier`1.cs | 11 +++--- 4 files changed, 22 insertions(+), 36 deletions(-) diff --git a/README.md b/README.md index 7f9609b..66d8dee 100644 --- a/README.md +++ b/README.md @@ -82,7 +82,7 @@ and even use a parameter from another field not annotated with `AutoConstructorI ### Get-only properties -Since version 3.0.0, get-only properties (`public int Property { get; }`) are injected by the generator by default. +Get-only properties (`public int Property { get; }`) are injected by the generator by default. The behavior of the injection can be modified using [auto-implemented property field-targeted attributes](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-7.3/auto-prop-field-attrs) on its backing field. The following code show an injected get-only property with a custom injecter: ```csharp @@ -96,11 +96,11 @@ public int Property { get; } ### Generating `ArgumentNullException` -By default, null checks with `ArgumentNullException` will be generated when needed. -To disable this behavior, set `AutoConstructor_DisableNullChecking` to `false` in the project file: +By default, null checks with `ArgumentNullException` are not generated when needed. +To enable this behavior, set `AutoConstructor_DisableNullChecking` to `false` in the project file: ``` xml -true +false ``` ### Generating XML documentation comment diff --git a/src/AutoConstructor.Generator/AutoConstructorGenerator.cs b/src/AutoConstructor.Generator/AutoConstructorGenerator.cs index 219db23..3ebc581 100644 --- a/src/AutoConstructor.Generator/AutoConstructorGenerator.cs +++ b/src/AutoConstructor.Generator/AutoConstructorGenerator.cs @@ -93,7 +93,7 @@ private static void Execute(Compilation compilation, ImmutableArray? diagnostics = null, - IEnumerable<(string filename, SourceText content)>? configFiles = null) + string? configFileContent = null) { var test = new CSharpSourceGeneratorVerifier.Test() { @@ -55,10 +55,11 @@ public static async Task RunAsync( test.TestState.ExpectedDiagnostics.AddRange(diagnostics); } - if (configFiles is not null) - { - test.TestState.AnalyzerConfigFiles.AddRange(configFiles); - } + // Enable null checks for the tests. + test.TestState.AnalyzerConfigFiles.Add(("/.editorconfig", SourceText.From($@" +is_global=true +build_property.AutoConstructor_DisableNullChecking = false +{configFileContent}"))); await test.RunAsync(); }