-
-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Generator with nullable or optional parameters (#273)
* Test generator * #WIP * NuGet config * Proof of working * Fix build error * Auto stash before merge of "BMichaelis/TestGenerator" and "BenjaminMichaelis/BMichaelis/TestGenerator" * More test, working generator * Removing caching of symbol * Add tests, adjust docs * More cleanup * revert global.json update * Revert "NuGet config". Will be done in #270 This reverts commit b990391. --------- Co-authored-by: Kevin Bost <kitokeboo@gmail.com>
- Loading branch information
1 parent
580a55d
commit f1d4414
Showing
12 changed files
with
675 additions
and
221 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
GeneratorTests/Moq.AutoMock.Generator.Example.MSTest/ControllerNullableParametersTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
namespace Moq.AutoMock.Generator.Example.MSTest; | ||
|
||
[TestClass] | ||
[ConstructorTests(typeof(ControllerWithSomeNullableParameters), Behavior = TestGenerationBehavior.IgnoreNullableParameters)] | ||
public partial class ControllerNullableParametersTests | ||
{ | ||
partial void ControllerWithSomeNullableParametersConstructor_WithNullstring_ThrowsArgumentNullExceptionSetup(AutoMocker mocker) | ||
{ | ||
mocker.Use<string>(""); | ||
mocker.Use<int?>(42); | ||
mocker.Use<int>(24); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
GeneratorTests/Moq.AutoMock.Generator.Example.NUnit/ControllerNullableParametersTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
namespace Moq.AutoMock.Generator.Example.NUnit; | ||
|
||
[ConstructorTests(typeof(ControllerWithSomeNullableParameters), Behavior = TestGenerationBehavior.IgnoreNullableParameters)] | ||
public partial class ControllerNullableParametersTests | ||
{ | ||
partial void AutoMockerTestSetup(Moq.AutoMock.AutoMocker mocker, string testName) | ||
{ | ||
mocker.Use<string>(""); | ||
mocker.Use<int?>(42); | ||
mocker.Use<int>(24); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
GeneratorTests/Moq.AutoMock.Generator.Example.xUnit/ControllerNullableParametersTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
namespace Moq.AutoMock.Generator.Example.xUnit; | ||
|
||
[ConstructorTests(typeof(ControllerWithSomeNullableParameters), Behavior = TestGenerationBehavior.IgnoreNullableParameters)] | ||
public partial class ControllerNullableParametersTests | ||
{ | ||
partial void AutoMockerTestSetup(Moq.AutoMock.AutoMocker mocker, string testName) | ||
{ | ||
mocker.Use<string>(""); | ||
mocker.Use<int?>(42); | ||
mocker.Use<int>(24); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
GeneratorTests/Moq.AutoMocker.Generator.Example/ControllerWithSomeNullableParameters.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
namespace Moq.AutoMock.Generator.Example; | ||
|
||
public class ControllerWithSomeNullableParameters | ||
{ | ||
public ControllerWithSomeNullableParameters( | ||
string name, // Test generated | ||
int years, // No test generated | ||
string? nullableName, // No test generated | ||
string? testName = null, // No test generated | ||
string foo = null!, // No test generated | ||
int? age = null) // No test generated | ||
{ | ||
ArgumentNullException.ThrowIfNull(name); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
namespace Moq.AutoMock; | ||
|
||
/// <summary> | ||
/// Control the behavior of the test generator, such as whether to skip nullable parameters. | ||
/// </summary> | ||
public enum TestGenerationBehavior | ||
{ | ||
/// <summary> | ||
/// The default behavior, which is to generate tests for all parameters. | ||
/// </summary> | ||
Default, | ||
|
||
/// <summary> | ||
/// Skip generating tests for parameters that meet one of the following criteria: | ||
/// <list type="bullet"> | ||
/// <item><description>Nullable reference types are enabled and the parameter type is a nullable reference type.</description></item> | ||
/// <item><description>The parameter is a nullable value type.</description></item> | ||
/// <item><description>The parameter has a default value of null.</description></item> | ||
/// </list> | ||
/// </summary> | ||
IgnoreNullableParameters | ||
} |
Oops, something went wrong.