From f6c1ca9a1c290751206459a99bc7f2ba0c3b188b Mon Sep 17 00:00:00 2001 From: Kevin Bost Date: Thu, 5 Feb 2026 22:34:25 -0800 Subject: [PATCH] Adds test coverage for primary constructors Updates generator test classes to include `TestContext` and pass cancellation tokens to `RunAsync`, alongside adding a new test for primary constructor types. --- .../ApplicationInsightsGeneratorTests.cs | 10 ++- .../FakeLoggingGeneratorTests.cs | 10 ++- .../GeneratorsTests.cs | 12 +-- .../KeyedServicesGeneratorTests.cs | 10 ++- .../OptionsGeneratorTests.cs | 10 ++- .../TestGeneratorTests.cs | 88 ++++++++++++++++--- 6 files changed, 105 insertions(+), 35 deletions(-) diff --git a/Moq.AutoMocker.Generators.Tests/ApplicationInsightsGeneratorTests.cs b/Moq.AutoMocker.Generators.Tests/ApplicationInsightsGeneratorTests.cs index 1f3890c..f7f8252 100644 --- a/Moq.AutoMocker.Generators.Tests/ApplicationInsightsGeneratorTests.cs +++ b/Moq.AutoMocker.Generators.Tests/ApplicationInsightsGeneratorTests.cs @@ -132,7 +132,7 @@ public async Task WhenApplicationInsightsAssemblyIsNotReferenced_NoGenerationOcc { TestCode = "", ReferenceApplicationInsights = false - }.RunAsync(); + }.RunAsync(TestContext.CancellationToken); } [TestMethod] @@ -150,7 +150,7 @@ public async Task WhenApplicationInsightsAssemblyIsReferenced_ExtensionMethodIsG GetSourceFile(ExpectedApplicationInsightsGeneratedFile, "AutoMockerApplicationInsightsExtensions.g.cs") } } - }.RunAsync(); + }.RunAsync(TestContext.CancellationToken); } [TestMethod] @@ -162,7 +162,7 @@ public async Task WhenGeneratorIsDisabled_NoGenerationOccurs() TestCode = "" }; test.SetGlobalOption("build_property.EnableMoqAutoMockerApplicationInsightsGenerator", "false"); - await test.RunAsync(); + await test.RunAsync(TestContext.CancellationToken); } [TestMethod] @@ -182,11 +182,13 @@ public async Task WhenGeneratorIsExplicitlyEnabled_ExtensionMethodIsGenerated() } }; test.SetGlobalOption("build_property.EnableMoqAutoMockerApplicationInsightsGenerator", "true"); - await test.RunAsync(); + await test.RunAsync(TestContext.CancellationToken); } private static (string FileName, SourceText SourceText) GetSourceFile(string content, string fileName) { return (Path.Combine("Moq.AutoMocker.Generators", "Moq.AutoMocker.Generators.ApplicationInsightsExtensionSourceGenerator", fileName), SourceText.From(content, Encoding.UTF8)); } + + public TestContext TestContext { get; set; } } diff --git a/Moq.AutoMocker.Generators.Tests/FakeLoggingGeneratorTests.cs b/Moq.AutoMocker.Generators.Tests/FakeLoggingGeneratorTests.cs index f6b6392..81900ac 100644 --- a/Moq.AutoMocker.Generators.Tests/FakeLoggingGeneratorTests.cs +++ b/Moq.AutoMocker.Generators.Tests/FakeLoggingGeneratorTests.cs @@ -113,7 +113,7 @@ public async Task WhenFakeLoggingAssemblyIsNotReferenced_NoGenerationOccurs() { TestCode = "", ReferenceFakeLogging = false - }.RunAsync(); + }.RunAsync(TestContext.CancellationToken); } [TestMethod] @@ -131,7 +131,7 @@ public async Task WhenFakeLoggingAssemblyIsReferenced_ExtensionMethodIsGenerated GetSourceFile(ExpectedFakeLoggingGeneratedFile, "AutoMockerFakeLoggingExtensions.g.cs") } } - }.RunAsync(); + }.RunAsync(TestContext.CancellationToken); } [TestMethod] @@ -143,7 +143,7 @@ public async Task WhenGeneratorIsDisabled_NoGenerationOccurs() TestCode = "" }; test.SetGlobalOption("build_property.EnableMoqAutoMockerFakeLoggingGenerator", "false"); - await test.RunAsync(); + await test.RunAsync(TestContext.CancellationToken); } [TestMethod] @@ -163,11 +163,13 @@ public async Task WhenGeneratorIsExplicitlyEnabled_ExtensionMethodIsGenerated() } }; test.SetGlobalOption("build_property.EnableMoqAutoMockerFakeLoggingGenerator", "true"); - await test.RunAsync(); + await test.RunAsync(TestContext.CancellationToken); } private static (string FileName, SourceText SourceText) GetSourceFile(string content, string fileName) { return (Path.Combine("Moq.AutoMocker.Generators", "Moq.AutoMocker.Generators.FakeLoggingExtensionSourceGenerator", fileName), SourceText.From(content, Encoding.UTF8)); } + + public TestContext TestContext { get; set; } } diff --git a/Moq.AutoMocker.Generators.Tests/GeneratorsTests.cs b/Moq.AutoMocker.Generators.Tests/GeneratorsTests.cs index 7131d12..a5b4893 100644 --- a/Moq.AutoMocker.Generators.Tests/GeneratorsTests.cs +++ b/Moq.AutoMocker.Generators.Tests/GeneratorsTests.cs @@ -26,7 +26,7 @@ public async Task Generation_WithProjectThatDoesNotReferenceAutoMocker_ProducesD { expectedResult } - }.RunAsync(); + }.RunAsync(TestContext.CancellationToken); } [TestMethod] @@ -58,7 +58,7 @@ public class Controller { } { expectedResult } - }.RunAsync(); + }.RunAsync(TestContext.CancellationToken); } [TestMethod] @@ -90,7 +90,7 @@ public class Controller { } { expectedResult } - }.RunAsync(); + }.RunAsync(TestContext.CancellationToken); } [TestMethod] @@ -150,7 +150,7 @@ public void ControllerConstructor_WithNullILoggerController_ThrowsArgumentNullEx } } - }.RunAsync(); + }.RunAsync(TestContext.CancellationToken); } [TestMethod] @@ -197,11 +197,13 @@ partial class ControllerTests } } - }.RunAsync(); + }.RunAsync(TestContext.CancellationToken); } private static (string FileName, SourceText SourceText) GetSourceFile(string content, string fileName) { return (Path.Combine("Moq.AutoMocker.Generators", "Moq.AutoMocker.Generators.UnitTestSourceGenerator", fileName), SourceText.From(content, Encoding.UTF8)); } + + public TestContext TestContext { get; set; } } diff --git a/Moq.AutoMocker.Generators.Tests/KeyedServicesGeneratorTests.cs b/Moq.AutoMocker.Generators.Tests/KeyedServicesGeneratorTests.cs index 8db049a..f0a1627 100644 --- a/Moq.AutoMocker.Generators.Tests/KeyedServicesGeneratorTests.cs +++ b/Moq.AutoMocker.Generators.Tests/KeyedServicesGeneratorTests.cs @@ -256,7 +256,7 @@ public async Task WhenDependencyInjectionAssemblyIsNotReferenced_NoGenerationOcc TestCode = "", ReferenceDependencyInjection = false - }.RunAsync(); + }.RunAsync(TestContext.CancellationToken); } [TestMethod] @@ -275,7 +275,7 @@ public async Task WhenDependencyInjectionAssemblyIsReferenced_ExtensionMethodIsG } } - }.RunAsync(); + }.RunAsync(TestContext.CancellationToken); } [TestMethod] @@ -287,7 +287,7 @@ public async Task WhenGeneratorIsDisabled_NoGenerationOccurs() TestCode = "" }; test.SetGlobalOption("build_property.EnableMoqAutoMockerKeyedServicesGenerator", "false"); - await test.RunAsync(); + await test.RunAsync(TestContext.CancellationToken); } [TestMethod] @@ -307,11 +307,13 @@ public async Task WhenGeneratorIsExplicitlyEnabled_ExtensionMethodIsGenerated() } }; test.SetGlobalOption("build_property.EnableMoqAutoMockerKeyedServicesGenerator", "true"); - await test.RunAsync(); + await test.RunAsync(TestContext.CancellationToken); } private static (string FileName, SourceText SourceText) GetSourceFile(string content, string fileName) { return (Path.Combine("Moq.AutoMocker.Generators", "Moq.AutoMocker.Generators.KeyedServicesExtensionSourceGenerator", fileName), SourceText.From(content, Encoding.UTF8)); } + + public TestContext TestContext { get; set; } } diff --git a/Moq.AutoMocker.Generators.Tests/OptionsGeneratorTests.cs b/Moq.AutoMocker.Generators.Tests/OptionsGeneratorTests.cs index 94ae96b..5f92a82 100644 --- a/Moq.AutoMocker.Generators.Tests/OptionsGeneratorTests.cs +++ b/Moq.AutoMocker.Generators.Tests/OptionsGeneratorTests.cs @@ -63,7 +63,7 @@ public async Task WhenOptionsAbstractionAssemblyIsNotReferenced_NoGenerationOccu TestCode = "", ReferenceOptionsAbstractions = false - }.RunAsync(); + }.RunAsync(TestContext.CancellationToken); } [TestMethod] @@ -82,7 +82,7 @@ public async Task WhenOptionsAbstractionAssemblyIsReferenced_ExtensionMethodIsGe } } - }.RunAsync(); + }.RunAsync(TestContext.CancellationToken); } [TestMethod] @@ -94,7 +94,7 @@ public async Task WhenGeneratorIsDisabled_NoGenerationOccurs() TestCode = "" }; test.SetGlobalOption("build_property.EnableMoqAutoMockerOptionsGenerator", "false"); - await test.RunAsync(); + await test.RunAsync(TestContext.CancellationToken); } [TestMethod] @@ -114,11 +114,13 @@ public async Task WhenGeneratorIsExplicitlyEnabled_ExtensionMethodIsGenerated() } }; test.SetGlobalOption("build_property.EnableMoqAutoMockerOptionsGenerator", "true"); - await test.RunAsync(); + await test.RunAsync(TestContext.CancellationToken); } private static (string FileName, SourceText SourceText) GetSourceFile(string content, string fileName) { return (Path.Combine("Moq.AutoMocker.Generators", "Moq.AutoMocker.Generators.OptionsExtensionSourceGenerator", fileName), SourceText.From(content, Encoding.UTF8)); } + + public TestContext TestContext { get; set; } } diff --git a/Moq.AutoMocker.Generators.Tests/TestGeneratorTests.cs b/Moq.AutoMocker.Generators.Tests/TestGeneratorTests.cs index 6a06c19..59f8693 100644 --- a/Moq.AutoMocker.Generators.Tests/TestGeneratorTests.cs +++ b/Moq.AutoMocker.Generators.Tests/TestGeneratorTests.cs @@ -25,7 +25,7 @@ public async Task Generation_WithProjectThatDoesNotReferenceAutoMocker_ProducesD { expectedResult } - }.RunAsync(); + }.RunAsync(TestContext.CancellationToken); } [TestMethod] @@ -57,7 +57,7 @@ public class Controller { } { expectedResult } - }.RunAsync(); + }.RunAsync(TestContext.CancellationToken); } [TestMethod] @@ -89,7 +89,7 @@ public class Controller { } { expectedResult } - }.RunAsync(); + }.RunAsync(TestContext.CancellationToken); } [TestMethod] @@ -151,7 +151,65 @@ public void ControllerConstructor_WithNullILoggerController_ThrowsArgumentNullEx } } - }.RunAsync(); + }.RunAsync(TestContext.CancellationToken); + } + + [TestMethod] + public async Task Generation_WithPrimaryConstructor_GeneratesTest() + { + var code = """ + + using Moq.AutoMock; + + namespace TestNamespace; + + [ConstructorTests(typeof(Controller))] + public partial class ControllerTests + { + + } + + public class Controller(ILogger logger); + + public interface ILogger { } + + """; + 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 + { + TestCode = code, + TestState = + { + GeneratedSources = + { + GetSourceFile(expected, "TestNamespace.ControllerTests.g.cs") + } + } + + }.RunAsync(TestContext.CancellationToken); } [TestMethod] @@ -199,7 +257,7 @@ partial class ControllerTests } } - }.RunAsync(); + }.RunAsync(TestContext.CancellationToken); } [TestMethod] @@ -247,7 +305,7 @@ partial class ControllerTests } } - }.RunAsync(); + }.RunAsync(TestContext.CancellationToken); } [TestMethod] @@ -307,7 +365,7 @@ public void ControllerConstructor_WithNullstring_ThrowsArgumentNullException() } } - }.RunAsync(); + }.RunAsync(TestContext.CancellationToken); } [TestMethod] @@ -356,7 +414,7 @@ partial class ControllerTests } } - }.RunAsync(); + }.RunAsync(TestContext.CancellationToken); } [TestMethod] @@ -405,7 +463,7 @@ partial class ControllerTests } } - }.RunAsync(); + }.RunAsync(TestContext.CancellationToken); } [TestMethod] @@ -453,7 +511,7 @@ partial class ControllerTests } } - }.RunAsync(); + }.RunAsync(TestContext.CancellationToken); } [TestMethod] @@ -502,7 +560,7 @@ partial class ControllerTests } } - }.RunAsync(); + }.RunAsync(TestContext.CancellationToken); } [TestMethod] @@ -569,7 +627,7 @@ public void ControllerConstructor_WithNullstring_ThrowsArgumentNullException() } } - }.RunAsync(); + }.RunAsync(TestContext.CancellationToken); } [TestMethod] @@ -671,7 +729,7 @@ public void ControllerConstructor_WithNullILogger_ThrowsArgumentNullException() } } - }.RunAsync(); + }.RunAsync(TestContext.CancellationToken); } [TestMethod] @@ -769,11 +827,13 @@ public void ControllerConstructor_WithNullIService_ThrowsArgumentNullException() { expectedWarning } - }.RunAsync(); + }.RunAsync(TestContext.CancellationToken); } private static (string FileName, SourceText SourceText) GetSourceFile(string content, string fileName) { return (Path.Combine("Moq.AutoMocker.Generators", "Moq.AutoMocker.Generators.UnitTestSourceGenerator", fileName), SourceText.From(content, Encoding.UTF8)); } + + public TestContext TestContext { get; set; } }