diff --git a/GeneratorTests/Moq.AutoMock.Generator.Example.xUnit/ControllerStringTests.cs b/GeneratorTests/Moq.AutoMock.Generator.Example.xUnit/ControllerStringTests.cs index cd8f470..4af2a6c 100644 --- a/GeneratorTests/Moq.AutoMock.Generator.Example.xUnit/ControllerStringTests.cs +++ b/GeneratorTests/Moq.AutoMock.Generator.Example.xUnit/ControllerStringTests.cs @@ -6,5 +6,7 @@ public partial class ControllerStringTests partial void AutoMockerTestSetup(Moq.AutoMock.AutoMocker mocker, string testName) { mocker.Use(""); + mocker.Use(42); + mocker.Use(24); } } diff --git a/GeneratorTests/Moq.AutoMocker.Generator.Example/ControllerString.cs b/GeneratorTests/Moq.AutoMocker.Generator.Example/ControllerString.cs index bfbfb1d..7ef2773 100644 --- a/GeneratorTests/Moq.AutoMocker.Generator.Example/ControllerString.cs +++ b/GeneratorTests/Moq.AutoMocker.Generator.Example/ControllerString.cs @@ -3,18 +3,14 @@ public class ControllerString { public ControllerString( - string name, + string name, + int years, string? nullableName, string? testName = null, string foo = null!, - int? i = null) + int? age = null) { - Name = name ?? throw new ArgumentNullException(nameof(name)); - NullableName = nullableName; - TestName = testName; + ArgumentNullException.ThrowIfNull(name); } - public string Name { get; } = ""; - public string? NullableName { get; } - public string? TestName { get; } } diff --git a/Moq.AutoMocker.TestGenerator.Tests/TestGeneratorTests.cs b/Moq.AutoMocker.TestGenerator.Tests/TestGeneratorTests.cs index 5cc7a38..af67d9b 100644 --- a/Moq.AutoMocker.TestGenerator.Tests/TestGeneratorTests.cs +++ b/Moq.AutoMocker.TestGenerator.Tests/TestGeneratorTests.cs @@ -370,6 +370,111 @@ partial class ControllerTests }.RunAsync(); } + [TestMethod] + public async Task Generation_ParameterWithValueType_DoesNotGenerateTest() + { + var code = @" +using Moq.AutoMock; +using System.Threading; + +namespace TestNamespace; + +[ConstructorTests(typeof(Controller), TestGenerationBehavior.IgnoreNullableParameters)] +public partial class ControllerTests +{ + +} + +public class Controller +{ + public Controller(int years) + { } +} +"; + string expected = @"namespace TestNamespace +{ + partial class ControllerTests + { + partial void AutoMockerTestSetup(Moq.AutoMock.AutoMocker mocker, string testName); + + } +} +"; + + await new VerifyCS.Test + { + TestCode = code, + TestState = + { + GeneratedSources = + { + GetSourceFile(expected, "ControllerTests.g.cs") + } + } + + }.RunAsync(); + } + + [TestMethod] + public async Task Generation_ParametersTypesWithValueTypeBetweenReferenceTypes_OnlyGeneratesTestsForReferenceType() + { + var code = @" +#nullable enable +using Moq.AutoMock; +using System.Threading; + +namespace TestNamespace; + +[ConstructorTests(typeof(Controller), TestGenerationBehavior.IgnoreNullableParameters)] +public partial class ControllerTests +{ + +} + +public class Controller +{ + public Controller( + string name, + int years, + string? nullableName) + { } +} +"; + string expected = @"namespace TestNamespace +{ + partial class ControllerTests + { + partial void AutoMockerTestSetup(Moq.AutoMock.AutoMocker mocker, string testName); + + partial void ControllerConstructor_WithNullstring_ThrowsArgumentNullExceptionSetup(Moq.AutoMock.AutoMocker mocker); + + public void ControllerConstructor_WithNullstring_ThrowsArgumentNullException() + { + Moq.AutoMock.AutoMocker mocker = new Moq.AutoMock.AutoMocker(); + AutoMockerTestSetup(mocker, ""ControllerConstructor_WithNullstring_ThrowsArgumentNullException""); + ControllerConstructor_WithNullstring_ThrowsArgumentNullExceptionSetup(mocker); + var years = mocker.Get(); + var nullableName = mocker.Get(); + } + + } +} +"; + + await new VerifyCS.Test + { + TestCode = code, + TestState = + { + GeneratedSources = + { + GetSourceFile(expected, "ControllerTests.g.cs") + } + } + + }.RunAsync(); + } + private static (string FileName, SourceText SourceText) GetSourceFile(string content, string fileName) { return (Path.Combine("Moq.AutoMocker.TestGenerator", "Moq.AutoMocker.TestGenerator.UnitTestSourceGenerator", fileName), SourceText.From(content, Encoding.UTF8)); diff --git a/Moq.AutoMocker.TestGenerator/GeneratorTargetClass.cs b/Moq.AutoMocker.TestGenerator/GeneratorTargetClass.cs index 01224e1..a62e6a9 100644 --- a/Moq.AutoMocker.TestGenerator/GeneratorTargetClass.cs +++ b/Moq.AutoMocker.TestGenerator/GeneratorTargetClass.cs @@ -42,9 +42,12 @@ public Parameter(IParameterSymbol symbol) { IsNullable = true; } + + IsValueType = symbol.Type.IsValueType; } private IParameterSymbol Symbol { get; } + public bool IsValueType { get; } public bool IsNullable { get; } public string Name => Symbol.Name; public string ParameterType => Symbol.Type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat); diff --git a/Moq.AutoMocker.TestGenerator/SyntaxReceiver.cs b/Moq.AutoMocker.TestGenerator/SyntaxReceiver.cs index 06b72d4..a6d0984 100644 --- a/Moq.AutoMocker.TestGenerator/SyntaxReceiver.cs +++ b/Moq.AutoMocker.TestGenerator/SyntaxReceiver.cs @@ -73,7 +73,6 @@ public void OnVisitSyntaxNode(GeneratorSyntaxContext context) int nullIndex = 0; foreach (IParameterSymbol parameter in ctor.Parameters) { - if (parameter.Type.IsValueType) continue; sut.NullConstructorParameterTests.Add(new NullConstructorParameterTest() { Parameters = parameters, diff --git a/Moq.AutoMocker.TestGenerator/UnitTestSourceGenerator.cs b/Moq.AutoMocker.TestGenerator/UnitTestSourceGenerator.cs index b1bf9f4..5ee4cf7 100644 --- a/Moq.AutoMocker.TestGenerator/UnitTestSourceGenerator.cs +++ b/Moq.AutoMocker.TestGenerator/UnitTestSourceGenerator.cs @@ -41,6 +41,10 @@ public void Execute(GeneratorExecutionContext context) foreach (NullConstructorParameterTest test in testClass.Sut?.NullConstructorParameterTests ?? Enumerable.Empty()) { + if (test.Parameters?[test.NullParameterIndex].IsValueType == true) + { + continue; + } // If SkipNullableParameters is true and the parameter is a nullable reference type, skip it if (testClass.SkipNullableParameters && test.Parameters?[test.NullParameterIndex].IsNullable == true) {