Skip to content

Commit

Permalink
Disable null checks by default
Browse files Browse the repository at this point in the history
Fix #23
  • Loading branch information
k94ll13nn3 committed Mar 9, 2022
1 parent 0808b18 commit 375ca97
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 36 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
<AutoConstructor_DisableNullChecking>true</AutoConstructor_DisableNullChecking>
<AutoConstructor_DisableNullChecking>false</AutoConstructor_DisableNullChecking>
```

### Generating XML documentation comment
Expand Down
2 changes: 1 addition & 1 deletion src/AutoConstructor.Generator/AutoConstructorGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ private static void Execute(Compilation compilation, ImmutableArray<ClassDeclara
filename = $"{symbol.ContainingNamespace.ToDisplayString()}.{filename}";
}

bool emitNullChecks = true;
bool emitNullChecks = false;
if (options.TryGetValue("build_property.AutoConstructor_DisableNullChecking", out string? disableNullCheckingSwitch))
{
emitNullChecks = !disableNullCheckingSwitch.Equals("true", StringComparison.OrdinalIgnoreCase);
Expand Down
37 changes: 11 additions & 26 deletions tests/AutoConstructor.Tests/GeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -318,12 +318,7 @@ public Test(string t)
}}
";

(string, SourceText) configFile = ("/.editorconfig", SourceText.From($@"
is_global=true
build_property.AutoConstructor_DisableNullChecking = {disableNullChecks}
"));

await VerifySourceGenerator.RunAsync(code, generated, configFiles: new[] { configFile });
await VerifySourceGenerator.RunAsync(code, generated, configFileContent: $"build_property.AutoConstructor_DisableNullChecking = {disableNullChecks}");
}

[Theory]
Expand Down Expand Up @@ -379,12 +374,10 @@ public Test(string t1, string t2)
";
}

(string, SourceText) configFile = ("/.editorconfig", SourceText.From($@"
is_global=true
build_property.AutoConstructor_GenerateConstructorDocumentation = {generateDocumentation}
"));

await VerifySourceGenerator.RunAsync(code, generated, configFiles: new[] { configFile });
await VerifySourceGenerator.RunAsync(
code,
generated,
configFileContent: $"build_property.AutoConstructor_GenerateConstructorDocumentation = {generateDocumentation}");
}

[Theory]
Expand Down Expand Up @@ -430,23 +423,19 @@ public Test(string t1, string t2)
}}
";

var configSource = SourceText.From(@"
is_global=true
string configFileContent = @"
build_property.AutoConstructor_GenerateConstructorDocumentation = true
");
";

if (hasCustomComment)
{
configSource = SourceText.From($@"
is_global=true
configFileContent = $@"
build_property.AutoConstructor_GenerateConstructorDocumentation = true
build_property.AutoConstructor_ConstructorDocumentationComment = {commentConfig}
");
";
}

(string, SourceText) configFile = ("/.editorconfig", configSource);

await VerifySourceGenerator.RunAsync(code, generated, configFiles: new[] { configFile });
await VerifySourceGenerator.RunAsync(code, generated, configFileContent: configFileContent);
}

[Fact]
Expand Down Expand Up @@ -786,12 +775,8 @@ public Test(int injected, int injectedWithDocumentation, int alsoInjectedEvenWhe
}
}
";
(string, SourceText) configFile = ("/.editorconfig", SourceText.From(@"
is_global=true
build_property.AutoConstructor_GenerateConstructorDocumentation = true
"));

await VerifySourceGenerator.RunAsync(code, generated, configFiles: new[] { configFile });
await VerifySourceGenerator.RunAsync(code, generated, configFileContent: "build_property.AutoConstructor_GenerateConstructorDocumentation = true");
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static async Task RunAsync(
string? generatedName = "Test.Test.g.cs",
bool nullable = false,
IEnumerable<DiagnosticResult>? diagnostics = null,
IEnumerable<(string filename, SourceText content)>? configFiles = null)
string? configFileContent = null)
{
var test = new CSharpSourceGeneratorVerifier<TSourceGenerator>.Test()
{
Expand Down Expand Up @@ -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();
}
Expand Down

0 comments on commit 375ca97

Please sign in to comment.