diff --git a/CHANGELOG.md b/CHANGELOG.md index a7ab6bc..c8c8047 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -45,6 +45,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `IDependencyModule` has the new member `InternalApplyGlue`, with a default implementation. The load of the modules has a last step that adds the collections and the `Func` registrations. (#132) +- DM0021 also reads `[Mock]` from `VerificationGlue.Mocks`. A VerificationGlue test with that + `[Mock]` and a `[TestExport]` for the same service on one method now gets DM0021. - `DependencyModules.Testing` depends on `DependencyModules.Runtime` and on `Microsoft.Extensions.DependencyInjection`: version 8.0.1 for `net8.0` and version 10.0.0 for `net10.0`. Before, it depended on `Microsoft.Extensions.DependencyInjection.Abstractions` only. diff --git a/docs/design/verificationglue.md b/docs/design/verificationglue.md index f01a4e5..1808664 100644 --- a/docs/design/verificationglue.md +++ b/docs/design/verificationglue.md @@ -566,8 +566,11 @@ second report would be a duplicate. - **DM0016 and DM0019** read assembly-level module attributes. In a VG project, DM0016 is right unchanged. DM0019 reports only when an `ApplicationModule` was generated, and DM generates one only from a file named `Program.cs` (`BaseSourceGenerator.CreateSourceValueProvider`, - `ApproveFilter`). MTP writes the entry point of a VG project. I did not check that file's name. - If it were `Program.cs`, `[assembly: SutModule]` in any other file would report DM0019 wrongly. + `ApproveFilter`). MTP writes the entry point of a VG project. Microsoft.Testing.Platform.MSBuild + 2.5.3 and 3.0.0-preview name it `MicrosoftTestingPlatformEntryPoint.cs` + (`_TestingPlatformEntryPointSourceName`), which does not end in `Program.cs`. So DM0019 is right + unchanged too. Had it been `Program.cs`, `[assembly: SutModule]` in any other file would have + reported DM0019 wrongly. - DM's `MockAttribute` remarks say that it throws without a mock-support attribute. The remarks get a sentence saying that under VerificationGlue it is a VerificationGlue mock. diff --git a/src/DependencyModules.SourceGenerator.Impl/KnownTypes.cs b/src/DependencyModules.SourceGenerator.Impl/KnownTypes.cs index 36f78f2..c14b5d6 100644 --- a/src/DependencyModules.SourceGenerator.Impl/KnownTypes.cs +++ b/src/DependencyModules.SourceGenerator.Impl/KnownTypes.cs @@ -304,6 +304,28 @@ private static ITypeDefinition Close(string name, ITypeDefinition argument) => } } + /// + /// VerificationGlue types, matched by name. No project here references VerificationGlue. + /// + public static class VerificationGlue + { + public static class Mocks + { + public const string Namespace = "VerificationGlue.Mocks"; + + /// + /// VerificationGlue's own [Mock]. Under VerificationGlue it has the same + /// precedence over [TestExport] as DependencyModules' [Mock], so DM0021 + /// reads both. + /// + public static readonly ITypeDefinition MockAttribute = TypeDefinition.Get( + TypeDefinitionEnum.ClassDefinition, + Namespace, + "MockAttribute" + ); + } + } + public static class System { public static readonly ITypeDefinition ArgumentOutOfRangeException = TypeDefinition.Get( diff --git a/src/DependencyModules.SourceGenerator/TestAttributeDiagnostics.cs b/src/DependencyModules.SourceGenerator/TestAttributeDiagnostics.cs index ea91400..7b18262 100644 --- a/src/DependencyModules.SourceGenerator/TestAttributeDiagnostics.cs +++ b/src/DependencyModules.SourceGenerator/TestAttributeDiagnostics.cs @@ -25,6 +25,12 @@ namespace DependencyModules.SourceGenerator; /// everything under it, and one test overriding that for one argument is what having both scopes is /// for — reporting that would be reporting the feature. /// +/// +/// [Mock] is either DependencyModules.Testing.Attributes.MockAttribute or +/// VerificationGlue.Mocks.MockAttribute. Under VerificationGlue both win over a +/// [TestExport] on the same method. VerificationGlue reports no diagnostic of its own for +/// this, so this is the only report. +/// /// internal static class TestAttributeDiagnostics { @@ -95,11 +101,19 @@ System.Threading.CancellationToken cancellationToken // A keyed mock is registered under its key, so the unkeyed export stays in use. if ( - !Carries( - parameter, - KnownTypes.DependencyModules.Testing.MockAttribute, - context, - cancellationToken + !( + Carries( + parameter, + KnownTypes.DependencyModules.Testing.MockAttribute, + context, + cancellationToken + ) + || Carries( + parameter, + KnownTypes.VerificationGlue.Mocks.MockAttribute, + context, + cancellationToken + ) ) || Carries( parameter, diff --git a/src/DependencyModules.Testing/Attributes/MockAttribute.cs b/src/DependencyModules.Testing/Attributes/MockAttribute.cs index ed812f6..2162247 100644 --- a/src/DependencyModules.Testing/Attributes/MockAttribute.cs +++ b/src/DependencyModules.Testing/Attributes/MockAttribute.cs @@ -16,7 +16,8 @@ namespace DependencyModules.Testing.Attributes; /// Creating the double is delegated to whichever mocking package is in scope, which must implement /// : [NSubstituteSupport], [MoqSupport] or /// [FakeItEasySupport]. Without one this throws rather than silently handing back a real -/// service. +/// service. Under VerificationGlue the parameter is a VerificationGlue mock, the same as one marked +/// with VerificationGlue.Mocks.MockAttribute, and no mock-support attribute is needed. /// /// A library that separates the double from the object it produces may let the parameter name either. /// With Moq, [Mock] IFoo gives the object and Mock<IFoo> gives the mock — and on a diff --git a/tests/DependencyModules.Specs/GeneratorTests/MockTestExportDiagnosticTests.cs b/tests/DependencyModules.Specs/GeneratorTests/MockTestExportDiagnosticTests.cs index a368ec6..093406c 100644 --- a/tests/DependencyModules.Specs/GeneratorTests/MockTestExportDiagnosticTests.cs +++ b/tests/DependencyModules.Specs/GeneratorTests/MockTestExportDiagnosticTests.cs @@ -191,6 +191,176 @@ public void Conflicting([global::DependencyModules.Testing.Attributes.Mock] IThi Single(result.GeneratorDiagnostics, d => d.Id == "DM0021"); } + /// + /// VerificationGlue's [Mock] wins over [TestExport] as DependencyModules' does, so + /// the two spellings get the same report at the same place. + /// + [VerifyTest] + public void VerificationGlueMock_GetsTheSameReportAsDependencyModulesMock() + { + const string body = """ + [TestExport(typeof(IThing), Implementation = typeof(RealThing))] + public void Conflicting([Mock] IThing thing) { } + """; + + var dependencyModules = Single(Run(body).GeneratorDiagnostics, d => d.Id == "DM0021"); + var verificationGlueResult = RunVerificationGlue(body); + var verificationGlue = Single( + verificationGlueResult.GeneratorDiagnostics, + d => d.Id == "DM0021" + ); + + Verify(!verificationGlueResult.Errors.Any()); + var reportedText = verificationGlue + .Location.SourceTree!.GetText() + .ToString(verificationGlue.Location.SourceSpan); + + Verify(dependencyModules.GetMessage() == verificationGlue.GetMessage()); + Verify(dependencyModules.Severity == verificationGlue.Severity); + Verify( + System.IO.Path.GetFileName(verificationGlue.Location.GetLineSpan().Path) == "Test.cs" + ); + Verify(reportedText == "[Mock] IThing thing"); + } + + [VerifyTest] + public void VerificationGlueMock_QualifiedSpelling_IsReported() + { + var result = RunVerificationGlue( + """ + [TestExport(typeof(IThing), Implementation = typeof(RealThing))] + public void Conflicting([global::VerificationGlue.Mocks.Mock] IThing thing) { } + """ + ); + + Verify(!result.Errors.Any()); + Single(result.GeneratorDiagnostics, d => d.Id == "DM0021"); + } + + [VerifyTest] + public void VerificationGlueMock_Keyed_IsNotReported() + { + var result = RunVerificationGlue( + """ + [TestExport(typeof(IThing), Implementation = typeof(RealThing))] + public void Keyed([Mock] [Microsoft.Extensions.DependencyInjection.FromKeyedServices("k")] IThing thing) { } + """ + ); + + Verify(!result.Errors.Any()); + Verify(!result.GeneratorDiagnostics.Any(d => d.Id == "DM0021")); + } + + [VerifyTest] + public void VerificationGlueMock_NamingAnotherService_IsNotReported() + { + var result = RunVerificationGlue( + """ + [TestExport(typeof(IOther), Implementation = typeof(RealOther))] + public void Unrelated([Mock] IThing thing) { } + """ + ); + + Verify(!result.Errors.Any()); + Verify(!result.GeneratorDiagnostics.Any(d => d.Id == "DM0021")); + } + + [VerifyTest] + public void VerificationGlueMock_WithTestExportOnTheClass_IsNotReported() + { + var result = GeneratorTestHarness.Run( + $$""" + {{VerificationGluePreamble}} + + namespace TestNamespace + { + [TestExport(typeof(IThing), Implementation = typeof(RealThing))] + public class Fixture { + public void Overriding([Mock] IThing thing) { } + } + } + """ + ); + + Verify(!result.Errors.Any()); + Verify(!result.GeneratorDiagnostics.Any(d => d.Id == "DM0021")); + } + + /// + /// Matched by namespace and name, so a MockAttribute of another library is not read. + /// + [VerifyTest] + public void AMockAttributeInAnotherNamespace_IsNotReported() + { + var result = GeneratorTestHarness.Run( + $$""" + using DependencyModules.Testing.Attributes; + + namespace Elsewhere + { + [System.AttributeUsage(System.AttributeTargets.Parameter)] + public sealed class MockAttribute : System.Attribute; + } + + namespace TestNamespace + { + public interface IThing; + + public class RealThing : IThing; + + public class Fixture { + [TestExport(typeof(IThing), Implementation = typeof(RealThing))] + public void NotAMock([Elsewhere.Mock] IThing thing) { } + } + } + """ + ); + + Verify(!result.Errors.Any()); + Verify(!result.GeneratorDiagnostics.Any(d => d.Id == "DM0021")); + } + + /// + /// A VerificationGlue test file. The stub stands in for VerificationGlue.Mocks, which no + /// project here references. [TestExport] is imported by alias, because importing both + /// namespaces makes [Mock] ambiguous. + /// + private const string VerificationGluePreamble = """ + using VerificationGlue.Mocks; + using TestExportAttribute = DependencyModules.Testing.Attributes.TestExportAttribute; + + namespace VerificationGlue.Mocks + { + [System.AttributeUsage(System.AttributeTargets.Parameter)] + public sealed class MockAttribute : System.Attribute; + } + + namespace TestNamespace + { + public interface IThing; + + public interface IOther; + + public class RealThing : IThing; + + public class RealOther : IOther; + } + """; + + private static GeneratorResult RunVerificationGlue(string body) => + GeneratorTestHarness.Run( + $$""" + {{VerificationGluePreamble}} + + namespace TestNamespace + { + public class Fixture { + {{body}} + } + } + """ + ); + private const string Preamble = """ using DependencyModules.Runtime.Attributes; using DependencyModules.Testing.Attributes; diff --git a/tests/DependencyModules.Specs/Snapshots/PublicApiTests.SourceGeneratorApi.verified.txt b/tests/DependencyModules.Specs/Snapshots/PublicApiTests.SourceGeneratorApi.verified.txt index e08cf1a..dfb6efc 100644 --- a/tests/DependencyModules.Specs/Snapshots/PublicApiTests.SourceGeneratorApi.verified.txt +++ b/tests/DependencyModules.Specs/Snapshots/PublicApiTests.SourceGeneratorApi.verified.txt @@ -1485,6 +1485,14 @@ namespace DependencyModules.SourceGenerator.Impl public static CSharpAuthor.ITypeDefinition AsyncEnumerable(CSharpAuthor.ITypeDefinition item) { } public static CSharpAuthor.ITypeDefinition ValueTask(CSharpAuthor.ITypeDefinition result) { } } + public static class VerificationGlue + { + public static class Mocks + { + public const string Namespace = "VerificationGlue.Mocks"; + public static readonly CSharpAuthor.ITypeDefinition MockAttribute; + } + } } public class ModuleAttributeWriter : DependencyModules.SourceGenerator.Impl.Utilities.BaseAttributeWriter { diff --git a/website/reference/diagnostics.md b/website/reference/diagnostics.md index 6615d8e..4b105cb 100644 --- a/website/reference/diagnostics.md +++ b/website/reference/diagnostics.md @@ -230,6 +230,8 @@ To correct the problem, set `Realm` on `[Intercept]` to the module that register A test method has `[TestExport]` for a service type and a `[Mock]` parameter of the same type. The mock replaces the `[TestExport]` registration. The generator gives DM0021 only if the test project references `DependencyModules.SourceGenerator`. +`[Mock]` can be `DependencyModules.Testing.Attributes.MockAttribute` or `VerificationGlue.Mocks.MockAttribute`. The generator gives the same DM0021 for both. + The generator gives no DM0021 for the two exceptions, because the `[TestExport]` registration stays in use: - The `[Mock]` parameter has `[FromKeyedServices]`.