From fd7033b28d0e9f7e4d31d824006a9768bc048386 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Valentin=20Breu=C3=9F?= Date: Wed, 29 Apr 2026 19:48:23 +0200 Subject: [PATCH] fix: skip IMockSetup parent for interfaces with only static abstract events --- .../Sources/Sources.MockClass.cs | 4 +- .../MockTests.ClassTests.MethodTests.cs | 79 +++++++++++++------ 2 files changed, 55 insertions(+), 28 deletions(-) diff --git a/Source/Mockolate.SourceGenerators/Sources/Sources.MockClass.cs b/Source/Mockolate.SourceGenerators/Sources/Sources.MockClass.cs index cbf67cf8..c7b84996 100644 --- a/Source/Mockolate.SourceGenerators/Sources/Sources.MockClass.cs +++ b/Source/Mockolate.SourceGenerators/Sources/Sources.MockClass.cs @@ -1153,7 +1153,7 @@ static bool TryCastWithDefaultValue(object?[] values, int index, TValue sb.AppendXmlSummary($"Set up the mock of .", "\t"); sb.Append("\tinternal interface IMockSetupFor").Append(name); - if (!hasStaticMembers) + if (!hasStaticMembers && !hasStaticEvents) { sb.Append(" : global::Mockolate.Setup.IMockSetup<").Append(@class.ClassFullName).Append(">").AppendLine(); } @@ -1235,7 +1235,7 @@ static bool TryCastWithDefaultValue(object?[] values, int index, TValue sb.AppendXmlSummary($"Verify interactions with the mock of .", "\t"); sb.Append("\tinternal interface IMockVerifyFor").Append(name); - if (!hasStaticMembers) + if (!hasStaticMembers && !hasStaticEvents) { sb.Append(" : global::Mockolate.Verify.IMockVerify<").Append(@class.ClassFullName).Append(">").AppendLine(); } diff --git a/Tests/Mockolate.SourceGenerators.Tests/MockTests.ClassTests.MethodTests.cs b/Tests/Mockolate.SourceGenerators.Tests/MockTests.ClassTests.MethodTests.cs index 44690860..fd09cc35 100644 --- a/Tests/Mockolate.SourceGenerators.Tests/MockTests.ClassTests.MethodTests.cs +++ b/Tests/Mockolate.SourceGenerators.Tests/MockTests.ClassTests.MethodTests.cs @@ -417,6 +417,32 @@ await That(result.Sources["Mock.IMyService__IMyServiceBase1__IMyServiceBase2.g.c .Contains("long global::MyCode.IMyServiceBase2.Value()").Once(); } + [Fact] + public async Task ParameterNamedI_ShouldNotCollideWithVerifyLambdaVariable() + { + GeneratorResult result = Generator + .Run(""" + using Mockolate; + + namespace MyCode; + + public class Program + { + public static void Main(string[] args) + { + _ = IMyService.CreateMock(); + } + } + + public interface IMyService + { + void Do(int i, string s); + } + """); + + await That(result.Diagnostics).IsEmpty(); + } + [Fact] public async Task ShouldImplementAllMethodsFromInterfaces() { @@ -1249,6 +1275,33 @@ await That(result.Sources["Mock.IMyService.g.cs"]) """).IgnoringNewlineStyle(); } + [Fact] + public async Task StaticAbstractEventOnly_ShouldCompile() + { + GeneratorResult result = Generator + .Run(""" + using System; + using Mockolate; + + namespace MyCode; + + public class Program + { + public static void Main(string[] args) + { + _ = IMyService.CreateMock(); + } + } + + public interface IMyService + { + static abstract event Action AbstractStaticEvent; + } + """); + + await That(result.Diagnostics).IsEmpty(); + } + [Fact] public async Task VirtualMethodOverride_WithConstrainedGeneric_ShouldNotRepeatConstraints() { @@ -1290,32 +1343,6 @@ public override bool MyMethod(T entity) .DoesNotContain("public override bool MyMethod(T entity)\n\t\t\twhere T :").IgnoringNewlineStyle() .Because("CS0460: constraints on override methods are inherited from the base method"); } - - [Fact] - public async Task ParameterNamedI_ShouldNotCollideWithVerifyLambdaVariable() - { - GeneratorResult result = Generator - .Run(""" - using Mockolate; - - namespace MyCode; - - public class Program - { - public static void Main(string[] args) - { - _ = IMyService.CreateMock(); - } - } - - public interface IMyService - { - void Do(int i, string s); - } - """); - - await That(result.Diagnostics).IsEmpty(); - } } } }