diff --git a/Moq.AutoMocker.Generators.Tests/GeneratorsTests.cs b/Moq.AutoMocker.Generators.Tests/GeneratorsTests.cs index cc6e156..2c7a9a0 100644 --- a/Moq.AutoMocker.Generators.Tests/GeneratorsTests.cs +++ b/Moq.AutoMocker.Generators.Tests/GeneratorsTests.cs @@ -28,19 +28,21 @@ public async Task Generation_WithProjectThatDoesNotReferenceAutoMocker_ProducesD [TestMethod] public async Task Generation_WithDecoratedNonPartialClass_ProducesDiagnosticError() { - var code = @" -using Moq.AutoMock; + var code = """ -namespace TestNamespace; + using Moq.AutoMock; -[ConstructorTests(TargetType = typeof(Controller))] -public class ControllerTests -{ - -} + namespace TestNamespace; + + [ConstructorTests(TargetType = typeof(Controller))] + public class ControllerTests + { + + } -public class Controller { } -"; + public class Controller { } + + """; var expectedResult = DiagnosticResult.CompilerError(Diagnostics.TestClassesMustBePartial.DiagnosticId) .WithSpan(6, 1, 10, 2) @@ -58,19 +60,21 @@ public class Controller { } [TestMethod] public async Task Generation_WithNoTargetTypeSpecified_ProducesDiagnosticError() { - var code = @" -using Moq.AutoMock; + var code = """ -namespace TestNamespace; + using Moq.AutoMock; -[ConstructorTests] -public class ControllerTests -{ - -} + namespace TestNamespace; + + [ConstructorTests] + public class ControllerTests + { + + } + + public class Controller { } -public class Controller { } -"; + """; var expectedResult = DiagnosticResult.CompilerError(Diagnostics.MustSpecifyTargetType.DiagnosticId) .WithSpan(6, 2, 6, 18) @@ -121,6 +125,9 @@ public void ControllerConstructor_WithNullILoggerController_ThrowsArgumentNullEx Moq.AutoMock.AutoMocker mocker = new Moq.AutoMock.AutoMocker(); AutoMockerTestSetup(mocker, "ControllerConstructor_WithNullILoggerController_ThrowsArgumentNullException"); ControllerConstructor_WithNullILoggerController_ThrowsArgumentNullExceptionSetup(mocker); + using(System.IDisposable __mockerDisposable = mocker.AsDisposable()) + { + } } } @@ -145,32 +152,35 @@ public void ControllerConstructor_WithNullILoggerController_ThrowsArgumentNullEx [TestMethod] public async Task Generation_WithValueTypeParameter_DoesNotGenerateTest() { - var code = @" -using Moq.AutoMock; -using System.Threading; + var code = """ + using Moq.AutoMock; + using System.Threading; -namespace TestNamespace; + namespace TestNamespace; -[ConstructorTests(typeof(Controller))] -public partial class ControllerTests -{ - -} + [ConstructorTests(typeof(Controller))] + public partial class ControllerTests + { + + } -public class Controller -{ - public Controller(CancellationToken token) { } -} -"; - string expected = @"namespace TestNamespace -{ - partial class ControllerTests - { - partial void AutoMockerTestSetup(Moq.AutoMock.AutoMocker mocker, string testName); + public class Controller + { + public Controller(CancellationToken token) { } + } - } -} -"; + """; + string expected = """ + namespace TestNamespace + { + partial class ControllerTests + { + partial void AutoMockerTestSetup(Moq.AutoMock.AutoMocker mocker, string testName); + + } + } + + """; await new VerifyCS.Test { diff --git a/Moq.AutoMocker.Generators.Tests/TestGeneratorTests.cs b/Moq.AutoMocker.Generators.Tests/TestGeneratorTests.cs index f381bdd..64c9e28 100644 --- a/Moq.AutoMocker.Generators.Tests/TestGeneratorTests.cs +++ b/Moq.AutoMocker.Generators.Tests/TestGeneratorTests.cs @@ -27,19 +27,21 @@ public async Task Generation_WithProjectThatDoesNotReferenceAutoMocker_ProducesD [TestMethod] public async Task Generation_WithDecoratedNonPartialClass_ProducesDiagnosticError() { - var code = @" -using Moq.AutoMock; + var code = """ -namespace TestNamespace; + using Moq.AutoMock; -[ConstructorTests(TargetType = typeof(Controller))] -public class ControllerTests -{ - -} + namespace TestNamespace; + + [ConstructorTests(TargetType = typeof(Controller))] + public class ControllerTests + { + + } + + public class Controller { } -public class Controller { } -"; + """; var expectedResult = DiagnosticResult.CompilerError(Diagnostics.TestClassesMustBePartial.DiagnosticId) .WithSpan(6, 1, 10, 2) @@ -57,19 +59,21 @@ public class Controller { } [TestMethod] public async Task Generation_WithNoTargetTypeSpecified_ProducesDiagnosticError() { - var code = @" -using Moq.AutoMock; + var code = """ -namespace TestNamespace; + using Moq.AutoMock; -[ConstructorTests] -public class ControllerTests -{ - -} + namespace TestNamespace; + + [ConstructorTests] + public class ControllerTests + { + + } + + public class Controller { } -public class Controller { } -"; + """; var expectedResult = DiagnosticResult.CompilerError(Diagnostics.MustSpecifyTargetType.DiagnosticId) .WithSpan(6, 2, 6, 18) @@ -88,42 +92,49 @@ public class Controller { } [Description("Issue 142")] public async Task Generation_WithGenericParameter_RemovesInvalidCharactersFromTestsName() { - var code = @" -using Moq.AutoMock; + var code = """ -namespace TestNamespace; + using Moq.AutoMock; -[ConstructorTests(typeof(Controller))] -public partial class ControllerTests -{ - -} + namespace TestNamespace; -public class Controller -{ - public Controller(ILogger logger) { } -} + [ConstructorTests(typeof(Controller))] + public partial class ControllerTests + { + + } -public interface ILogger { } -"; - string expected = @"namespace TestNamespace -{ - partial class ControllerTests - { - partial void AutoMockerTestSetup(Moq.AutoMock.AutoMocker mocker, string testName); + public class Controller + { + public Controller(ILogger logger) { } + } - partial void ControllerConstructor_WithNullILoggerController_ThrowsArgumentNullExceptionSetup(Moq.AutoMock.AutoMocker mocker); + public interface ILogger { } - public void ControllerConstructor_WithNullILoggerController_ThrowsArgumentNullException() - { - Moq.AutoMock.AutoMocker mocker = new Moq.AutoMock.AutoMocker(); - AutoMockerTestSetup(mocker, ""ControllerConstructor_WithNullILoggerController_ThrowsArgumentNullException""); - ControllerConstructor_WithNullILoggerController_ThrowsArgumentNullExceptionSetup(mocker); - } + """; + string expected = """ + namespace TestNamespace + { + partial class ControllerTests + { + partial void AutoMockerTestSetup(Moq.AutoMock.AutoMocker mocker, string testName); - } -} -"; + partial void ControllerConstructor_WithNullILoggerController_ThrowsArgumentNullExceptionSetup(Moq.AutoMock.AutoMocker mocker); + + public void ControllerConstructor_WithNullILoggerController_ThrowsArgumentNullException() + { + Moq.AutoMock.AutoMocker mocker = new Moq.AutoMock.AutoMocker(); + AutoMockerTestSetup(mocker, "ControllerConstructor_WithNullILoggerController_ThrowsArgumentNullException"); + ControllerConstructor_WithNullILoggerController_ThrowsArgumentNullExceptionSetup(mocker); + using(System.IDisposable __mockerDisposable = mocker.AsDisposable()) + { + } + } + + } + } + + """; await new VerifyCS.Test { @@ -142,32 +153,36 @@ public void ControllerConstructor_WithNullILoggerController_ThrowsArgumentNullEx [TestMethod] public async Task Generation_WithValueTypeParameter_DoesNotGenerateTest() { - var code = @" -using Moq.AutoMock; -using System.Threading; + var code = """ -namespace TestNamespace; + using Moq.AutoMock; + using System.Threading; -[ConstructorTests(typeof(Controller))] -public partial class ControllerTests -{ - -} + namespace TestNamespace; -public class Controller -{ - public Controller(CancellationToken token) { } -} -"; - string expected = @"namespace TestNamespace -{ - partial class ControllerTests - { - partial void AutoMockerTestSetup(Moq.AutoMock.AutoMocker mocker, string testName); + [ConstructorTests(typeof(Controller))] + public partial class ControllerTests + { + + } - } -} -"; + public class Controller + { + public Controller(CancellationToken token) { } + } + + """; + string expected = """ + namespace TestNamespace + { + partial class ControllerTests + { + partial void AutoMockerTestSetup(Moq.AutoMock.AutoMocker mocker, string testName); + + } + } + + """; await new VerifyCS.Test { @@ -186,32 +201,36 @@ partial class ControllerTests [TestMethod] public async Task Generation_ParameterWithDefaultValue_DoesNotGenerateTest() { - var code = @" -using Moq.AutoMock; -using System.Threading; + var code = """ -namespace TestNamespace; + using Moq.AutoMock; + using System.Threading; -[ConstructorTests(typeof(Controller), Behavior = TestGenerationBehavior.IgnoreNullableParameters)] -public partial class ControllerTests -{ - -} + namespace TestNamespace; -public class Controller -{ - public Controller(string name = null) { } -} -"; - string expected = @"namespace TestNamespace -{ - partial class ControllerTests - { - partial void AutoMockerTestSetup(Moq.AutoMock.AutoMocker mocker, string testName); + [ConstructorTests(typeof(Controller), Behavior = TestGenerationBehavior.IgnoreNullableParameters)] + public partial class ControllerTests + { + + } - } -} -"; + public class Controller + { + public Controller(string name = null) { } + } + + """; + string expected = """ + namespace TestNamespace + { + partial class ControllerTests + { + partial void AutoMockerTestSetup(Moq.AutoMock.AutoMocker mocker, string testName); + + } + } + + """; await new VerifyCS.Test { @@ -230,41 +249,48 @@ partial class ControllerTests [TestMethod] public async Task Generation_ParameterWithEmptyString_DoesGenerateTest() { - var code = @" -using Moq.AutoMock; -using System.Threading; + var code = """ -namespace TestNamespace; + using Moq.AutoMock; + using System.Threading; -[ConstructorTests(typeof(Controller), Behavior = TestGenerationBehavior.IgnoreNullableParameters)] -public partial class ControllerTests -{ - -} + namespace TestNamespace; -public class Controller -{ - public Controller(string name = """") { } -} -"; - string expected = @"namespace TestNamespace -{ - partial class ControllerTests - { - partial void AutoMockerTestSetup(Moq.AutoMock.AutoMocker mocker, string testName); + [ConstructorTests(typeof(Controller), Behavior = TestGenerationBehavior.IgnoreNullableParameters)] + public partial class ControllerTests + { + + } - partial void ControllerConstructor_WithNullstring_ThrowsArgumentNullExceptionSetup(Moq.AutoMock.AutoMocker mocker); + public class Controller + { + public Controller(string name = "") { } + } - public void ControllerConstructor_WithNullstring_ThrowsArgumentNullException() - { - Moq.AutoMock.AutoMocker mocker = new Moq.AutoMock.AutoMocker(); - AutoMockerTestSetup(mocker, ""ControllerConstructor_WithNullstring_ThrowsArgumentNullException""); - ControllerConstructor_WithNullstring_ThrowsArgumentNullExceptionSetup(mocker); - } + """; + 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); + using(System.IDisposable __mockerDisposable = mocker.AsDisposable()) + { + } + } + + } + } + + """; await new VerifyCS.Test { @@ -283,33 +309,37 @@ public void ControllerConstructor_WithNullstring_ThrowsArgumentNullException() [TestMethod] public async Task Generation_ParameterWithNullableString_DoesNotGenerateTest() { - var code = @" -#nullable enable -using Moq.AutoMock; -using System.Threading; + var code = """ -namespace TestNamespace; + #nullable enable + using Moq.AutoMock; + using System.Threading; -[ConstructorTests(typeof(Controller), TestGenerationBehavior.IgnoreNullableParameters)] -public partial class ControllerTests -{ - -} + namespace TestNamespace; -public class Controller -{ - public Controller(string? name) { } -} -"; - string expected = @"namespace TestNamespace -{ - partial class ControllerTests - { - partial void AutoMockerTestSetup(Moq.AutoMock.AutoMocker mocker, string testName); + [ConstructorTests(typeof(Controller), TestGenerationBehavior.IgnoreNullableParameters)] + public partial class ControllerTests + { + + } - } -} -"; + public class Controller + { + public Controller(string? name) { } + } + + """; + string expected = """ + namespace TestNamespace + { + partial class ControllerTests + { + partial void AutoMockerTestSetup(Moq.AutoMock.AutoMocker mocker, string testName); + + } + } + + """; await new VerifyCS.Test { @@ -328,33 +358,37 @@ partial class ControllerTests [TestMethod] public async Task Generation_ParameterWithNullableStringAndDefaultValue_DoesNotGenerateTest() { - var code = @" -#nullable enable -using Moq.AutoMock; -using System.Threading; + var code = """ -namespace TestNamespace; + #nullable enable + using Moq.AutoMock; + using System.Threading; -[ConstructorTests(typeof(Controller), TestGenerationBehavior.IgnoreNullableParameters)] -public partial class ControllerTests -{ - -} + namespace TestNamespace; -public class Controller -{ - public Controller(string? name = null) { } -} -"; - string expected = @"namespace TestNamespace -{ - partial class ControllerTests - { - partial void AutoMockerTestSetup(Moq.AutoMock.AutoMocker mocker, string testName); + [ConstructorTests(typeof(Controller), TestGenerationBehavior.IgnoreNullableParameters)] + public partial class ControllerTests + { + + } - } -} -"; + public class Controller + { + public Controller(string? name = null) { } + } + + """; + string expected = """ + namespace TestNamespace + { + partial class ControllerTests + { + partial void AutoMockerTestSetup(Moq.AutoMock.AutoMocker mocker, string testName); + + } + } + + """; await new VerifyCS.Test { @@ -373,32 +407,36 @@ partial class ControllerTests [TestMethod] public async Task Generation_ParameterWithNullableValueType_DoesNotGenerateTest() { - var code = @" -using Moq.AutoMock; -using System.Threading; + var code = """ -namespace TestNamespace; + using Moq.AutoMock; + using System.Threading; -[ConstructorTests(typeof(Controller), TestGenerationBehavior.IgnoreNullableParameters)] -public partial class ControllerTests -{ - -} + namespace TestNamespace; -public class Controller -{ - public Controller(int? age) { } -} -"; - string expected = @"namespace TestNamespace -{ - partial class ControllerTests - { - partial void AutoMockerTestSetup(Moq.AutoMock.AutoMocker mocker, string testName); + [ConstructorTests(typeof(Controller), TestGenerationBehavior.IgnoreNullableParameters)] + public partial class ControllerTests + { + + } - } -} -"; + public class Controller + { + public Controller(int? age) { } + } + + """; + string expected = """ + namespace TestNamespace + { + partial class ControllerTests + { + partial void AutoMockerTestSetup(Moq.AutoMock.AutoMocker mocker, string testName); + + } + } + + """; await new VerifyCS.Test { @@ -417,33 +455,37 @@ partial class ControllerTests [TestMethod] public async Task Generation_ParameterWithValueType_DoesNotGenerateTest() { - var code = @" -using Moq.AutoMock; -using System.Threading; + var code = """ -namespace TestNamespace; + using Moq.AutoMock; + using System.Threading; -[ConstructorTests(typeof(Controller), TestGenerationBehavior.IgnoreNullableParameters)] -public partial class ControllerTests -{ - -} + namespace TestNamespace; -public class Controller -{ - public Controller(int years) - { } -} -"; - string expected = @"namespace TestNamespace -{ - partial class ControllerTests - { - partial void AutoMockerTestSetup(Moq.AutoMock.AutoMocker mocker, string testName); + [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 { @@ -462,48 +504,55 @@ partial class ControllerTests [TestMethod] public async Task Generation_ParametersTypesWithValueTypeBetweenReferenceTypes_OnlyGeneratesTestsForReferenceType() { - var code = @" -#nullable enable -using Moq.AutoMock; -using System.Threading; + var code = """ -namespace TestNamespace; + #nullable enable + using Moq.AutoMock; + using System.Threading; -[ConstructorTests(typeof(Controller), TestGenerationBehavior.IgnoreNullableParameters)] -public partial class ControllerTests -{ - -} + namespace TestNamespace; -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); + [ConstructorTests(typeof(Controller), TestGenerationBehavior.IgnoreNullableParameters)] + public partial class ControllerTests + { + + } - partial void ControllerConstructor_WithNullstring_ThrowsArgumentNullExceptionSetup(Moq.AutoMock.AutoMocker mocker); + public class Controller + { + public Controller( + string name, + int years, + string? nullableName) + { } + } - 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(); - } + """; + 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); + using(System.IDisposable __mockerDisposable = mocker.AsDisposable()) + { + var years = mocker.Get(); + var nullableName = mocker.Get(); + } + } - } -} -"; + } + } + + """; await new VerifyCS.Test { diff --git a/Moq.AutoMocker.Generators/UnitTestSourceGenerator.cs b/Moq.AutoMocker.Generators/UnitTestSourceGenerator.cs index 344c236..77b5614 100644 --- a/Moq.AutoMocker.Generators/UnitTestSourceGenerator.cs +++ b/Moq.AutoMocker.Generators/UnitTestSourceGenerator.cs @@ -81,12 +81,15 @@ public void Execute(GeneratorExecutionContext context) builder.AppendLine(" Moq.AutoMock.AutoMocker mocker = new Moq.AutoMock.AutoMocker();"); builder.AppendLine($" AutoMockerTestSetup(mocker, \"{testName}\");"); builder.AppendLine($" {testName}Setup(mocker);"); + builder.AppendLine($" using(System.IDisposable __mockerDisposable = mocker.AsDisposable())"); + builder.AppendLine($" {{"); + const string indent = " "; for (int i = 0; i < test.Parameters?.Count; i++) { if (i == test.NullParameterIndex) continue; Parameter parameter = test.Parameters[i]; - builder.AppendLine($" var {parameter.Name} = mocker.Get<{parameter.ParameterType}>();"); + builder.AppendLine($"{indent}var {parameter.Name} = mocker.Get<{parameter.ParameterType}>();"); } string constructorInvocation = $"_ = new {testClass.Sut!.FullName}({string.Join(",", GetParameterNames(test))})"; @@ -94,19 +97,19 @@ public void Execute(GeneratorExecutionContext context) switch (testingFramework) { case TargetTestingFramework.MSTest: - builder.AppendLine($" System.ArgumentNullException ex = global::Microsoft.VisualStudio.TestTools.UnitTesting.Assert.ThrowsException(() => {constructorInvocation});"); - builder.AppendLine($" global::Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(\"{test.NullParameterName}\", ex.ParamName);"); + builder.AppendLine($"{indent}System.ArgumentNullException ex = global::Microsoft.VisualStudio.TestTools.UnitTesting.Assert.ThrowsException(() => {constructorInvocation});"); + builder.AppendLine($"{indent}global::Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(\"{test.NullParameterName}\", ex.ParamName);"); break; case TargetTestingFramework.Xunit: - builder.AppendLine($" System.ArgumentNullException ex = global::Xunit.Assert.Throws(() => {constructorInvocation});"); - builder.AppendLine($" global::Xunit.Assert.Equal(\"{test.NullParameterName}\", ex.ParamName);"); + builder.AppendLine($"{indent}System.ArgumentNullException ex = global::Xunit.Assert.Throws(() => {constructorInvocation});"); + builder.AppendLine($"{indent}global::Xunit.Assert.Equal(\"{test.NullParameterName}\", ex.ParamName);"); break; case TargetTestingFramework.NUnit: - builder.AppendLine($" System.ArgumentNullException ex = global::NUnit.Framework.Assert.Throws(() => {constructorInvocation});"); - builder.AppendLine($" global::NUnit.Framework.Assert.That(\"{test.NullParameterName}\" == ex.ParamName);"); + builder.AppendLine($"{indent}System.ArgumentNullException ex = global::NUnit.Framework.Assert.Throws(() => {constructorInvocation});"); + builder.AppendLine($"{indent}global::NUnit.Framework.Assert.That(\"{test.NullParameterName}\" == ex.ParamName);"); break; } - + builder.AppendLine(" }"); builder.AppendLine(" }"); builder.AppendLine(); }