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

Support indexer with multiple arguments #694

Merged
merged 2 commits into from
Oct 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions src/Moq/Mock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -664,13 +664,12 @@ private static TSetupPhrase SetupSetImpl<T, TSetupPhrase>(

matchers[valueIndex] = new MatchExpression(last.Match);

if (arguments.Length == 2)
for (int i = 0; i < arguments.Length - 1; i++)
stakx marked this conversation as resolved.
Show resolved Hide resolved
{
// TODO: what about multi-index setters?
// Add the index value for the property indexer
values[0] = GetValueExpression(arguments[0], parameters[0].ParameterType);
values[i] = GetValueExpression(arguments[i], parameters[i].ParameterType);
// TODO: No matcher supported now for the index
matchers[0] = values[0];
matchers[i] = values[i];
}

var lambda = Expression.Lambda(
Expand Down
45 changes: 45 additions & 0 deletions tests/Moq.Tests/CallbacksFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,47 @@ public void CallbackWithRefParameterCannotModifyNonRefParameter()
Assert.Equal("input", value);
}

[Fact]
public void CallbackWithIndexerSetter()
{
int x = default(int);
int result = default(int);

var mock = new Mock<IFoo>();
mock.SetupSet(f => f[10] = It.IsAny<int>())
.Callback(new Action<int, int>((x_, result_) =>
{
x = x_;
result = result_;
}));

mock.Object[10] = 5;
Assert.Equal(10, x);
Assert.Equal(5, result);
}

[Fact]
public void CallbackWithMultipleArgumentIndexerSetterWithoutAny()
{
int x = default(int);
int y = default(int);
int result = default(int);

var mock = new Mock<IFoo>();
mock.SetupSet(f => f[3, 13] = It.IsAny<int>())
.Callback(new Action<int, int, int>((x_, y_, result_) =>
{
x = x_;
y = y_;
result = result_;
}));

mock.Object[3, 13] = 2;
Assert.Equal(3, x);
Assert.Equal(13, y);
Assert.Equal(2, result);
}

public interface IInterface
{
void Method(Derived b);
Expand Down Expand Up @@ -515,6 +556,10 @@ public interface IFoo
string Execute(ref string arg1, string arg2);

int Value { get; set; }

int this[int x] { get; set; }

int this[int x, int y] { get; set; }
}

public delegate void ExecuteRHandler(ref string arg1);
Expand Down