Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not able to define behaviour for overloaded functions with different parameters. #141

Open
AndreasDDi opened this issue Apr 14, 2023 · 1 comment

Comments

@AndreasDDi
Copy link
Contributor

AndreasDDi commented Apr 14, 2023

We recently upated the version of Delphi-Mocks we where using and with this version it looks like we can not define differen behaviour for overloaded functions.

  IMockIntf = interface(IInvokable)
    ['{E473945B-D350-4535-A981-B66A5B58DA21}']
    function Test(const Arg1: string): TArray<string>; overload;
    function Test(const Arg1, Arg2: string): TArray<string>; overload;
    function Test(const Arg1, Arg2, Arg3: string): TArray<string>; overload;
  end;

Given this interface it was possible (and needed) to define a return value for alle 3 functions separatly.

  FMock := TMock<IMockIntf>.Create;

  FMock.Setup.WillReturn('Test1').When.Test(It0.IsAny<string>);
  FMock.Setup.WillReturn('Test2').When.Test(It0.IsAny<string>, It1.IsAny<string>);
  FMock.Setup.WillReturn('Test3').When.Test(It0.IsAny<string>, It1.IsAny<string>, It3.IsAny<string>);

And than test the behaviour

procedure TMyTestObject.Test1;
begin
  var x := FMock.Instance.Test('A');
  Assert.AreEqual('Test1', x);
end;

procedure TMyTestObject.Test2;
begin
  var x := FMock.Instance.Test('A', 'B');
  Assert.AreEqual('Test2', x);
end;

procedure TMyTestObject.Test3;
begin
  var x := FMock.Instance.Test('A', 'B', 'C');
  Assert.AreEqual('Test3', x);
end;

Unfortenatly with the latest version this is not working anymore. I looks like the Mock sees the Test functions as one and will not allow this:

EMockSetupException with message '[IMockIntf] already defines Will Return When for method [Test]'

If I use AllowRedefineBehaviorDefinitions := True I can define the behaviour, but than the function with 2 parameters returns the behaviour with 3 parameter 'Test3' and the function with 3 parameter raises an Access violation at address 00000000.

I used the latest version in combination with Delphi 11.3.

@AndreasDDi
Copy link
Contributor Author

I think this is caused by the change in TBehavior.Match where the line

if (Length(FMatchers) > 0) and (Length(Args) = (Length(FMatchers) + 1)) then

is changed to

if Length(FMatchers) > 0 then

and wil not take the parameter count into consideration.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant