Skip to content

[Bug]: mock for ref struct event args generates wrong code #6808

Closed

Description

@DemoBytom

Bug report checklist

  • I've searched existing issues to ensure this isn't a duplicate
  • I've read the documentation and this isn't expected behavior
  • I'm using the latest version of TUnit

Description

An interface with an event that has ref struct event args cannot be mocked, produces code that cannot be compiled.

Given following code:

public class RefStructEvent
{
    [Test]
    public void SomeTest()
    {
        var fooMock = Mock.Of<IFoo>(MockBehavior.Strict);
    }

    public ref struct FooRefStruct
    {
    }

    public interface IFoo
    {
        event EventHandler<FooRefStruct> FooRefStructEvent;
    }
}

Following compiler errors are reported:

TUnit.Mocks.SourceGenerator\TUnit.Mocks.SourceGenerator.MockGenerator\Demo_Engine_Core_UTs_RefStructEvent_IFoo_MockMembers.g.cs(11,137,11,147): error CS0030: Cannot convert type 'Demo.Engine.Core.UTs.RefStructEvent.FooRefStruct' to 'object'
TUnit.Mocks.SourceGenerator\TUnit.Mocks.SourceGenerator.MockGenerator\Demo_Engine_Core_UTs_RefStructEvent_IFoo_MockImplFactory.g.cs(40,49,40,112): error CS0030: Cannot convert type 'object' to 'Demo.Engine.Core.UTs.RefStructEvent.FooRefStruct'

The offending code:
Demo_Engine_Core_UTs_RefStructEvent_IFoo_MockMemberExtensions

        public static void RaiseFooRefStructEvent(this global::TUnit.Mocks.Mock<global::Demo.Engine.Core.UTs.RefStructEvent.IFoo> mock, global::Demo.Engine.Core.UTs.RefStructEvent.FooRefStruct e)
        {
            ((global::TUnit.Mocks.IRaisable)global::TUnit.Mocks.MockRegistry.GetEngine(mock).Raisable!).RaiseEvent("FooRefStructEvent", (object?)e);
        }

RefStructEvent_IFooMockImpl

        [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
        public void RaiseEvent(string eventName, object? args)
        {
            switch (eventName)
            {
                case "FooRefStructEvent":
                    {
                        Raise_FooRefStructEvent((global::Demo.Engine.Core.UTs.RefStructEvent.FooRefStruct)args!);
                        break;
                    }
                default:
                    throw new global::System.InvalidOperationException($"No event named '{eventName}' exists on this mock.");
            }
        }

Expected Behavior

I expect no compile time error.

Actual Behavior

Compile time errors.

Steps to Reproduce

  1. Create following test class
  2. Compile the test project:
public class RefStructEvent
{
    [Test]
    public void SomeTest()
    {
        var fooMock = Mock.Of<IFoo>(MockBehavior.Strict);
    }

    public ref struct FooRefStruct
    {

    }

    public interface IFoo
    {
        event EventHandler<FooRefStruct> FooRefStructEvent;
    }
}

TUnit Version

1.67.0

.NET Version

.NET 10

Operating System

Windows

IDE / Test Runner

dotnet CLI (dotnet test / dotnet run)

Error Output / Stack Trace

TUnit.Mocks.SourceGenerator\TUnit.Mocks.SourceGenerator.MockGenerator\Demo_Engine_Core_UTs_RefStructEvent_IFoo_MockMembers.g.cs(11,137,11,147): error CS0030: Cannot convert type 'Demo.Engine.Core.UTs.RefStructEvent.FooRefStruct' to 'object'
TUnit.Mocks.SourceGenerator\TUnit.Mocks.SourceGenerator.MockGenerator\Demo_Engine_Core_UTs_RefStructEvent_IFoo_MockImplFactory.g.cs(40,49,40,112): error CS0030: Cannot convert type 'object' to 'Demo.Engine.Core.UTs.RefStructEvent.FooRefStruct'

Additional Context

No response

IDE-Specific Issue?

  • I've confirmed this issue occurs when running via dotnet test or dotnet run, not just in my IDE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions