Skip to content

Commit 575c6a9

Browse files
committed
Another fix for issue devlooped#278 that no longer causes issue devlooped#295.
There is still problematic code in the Interceptor for comparing the arguments of calls, but for now it no longer causes a problem with propeties with arguments as the expression string builder now reports the arguments for property reads. I've added a comment explaining the flaws (as I see them) in the Interceptor code in the hope that it will help when someone next as a problem, or at least stops them missing the same thing as I did earlier.
1 parent f293e9f commit 575c6a9

File tree

3 files changed

+35
-17
lines changed

3 files changed

+35
-17
lines changed

Source/ExpressionStringBuilder.cs

+8-4
Original file line numberDiff line numberDiff line change
@@ -343,15 +343,19 @@ private void ToStringMethodCall(MethodCallExpression node)
343343
else if (node.Method.IsPropertyIndexerSetter())
344344
{
345345
this.builder.Append("[");
346-
AsCommaSeparatedValues(node.Arguments
347-
.Skip(paramFrom)
348-
.Take(node.Arguments.Count - paramFrom), ToString);
346+
AsCommaSeparatedValues(node.Arguments.Skip(paramFrom), ToString);
349347
this.builder.Append("] = ");
350348
ToString(node.Arguments.Last());
351349
}
352350
else if (node.Method.IsPropertyGetter())
353351
{
354-
this.builder.Append(".").Append(node.Method.Name.Substring(4));
352+
this.builder.Append(".").Append(node.Method.Name.Substring(4));
353+
if (node.Arguments.Count > paramFrom)
354+
{
355+
this.builder.Append("[");
356+
AsCommaSeparatedValues(node.Arguments.Skip(paramFrom), ToString);
357+
this.builder.Append("]");
358+
}
355359
}
356360
else if (node.Method.IsPropertySetter())
357361
{

Source/Interceptor.cs

+11-11
Original file line numberDiff line numberDiff line change
@@ -169,20 +169,20 @@ public override bool Equals(object obj)
169169
}
170170

171171
var eq = key.fixedString == this.fixedString && key.values.Count == this.values.Count;
172-
if(!eq)
172+
173+
//the code below is broken as it uses an OR when checking the arguments, this means that if any pair of arguments match
174+
//the result is a match.
175+
//This is only going to hit some edge cases as for the most part the fixed string above spots arguments correctly.
176+
//Fixing this really needs a reworking of the GetHashCode, and also some sorting out of how to compare value types that have been
177+
//boxed correctly (another problem this code has)
178+
var index = 0;
179+
while (eq && index < this.values.Count)
173180
{
174-
return false;
181+
eq |= this.values[index] == key.values[index];
182+
index++;
175183
}
176184

177-
for(int index=0; index < values.Count; index++)
178-
{
179-
if (this.values[index] != key.values[index])
180-
{
181-
return false;
182-
}
183-
}
184-
185-
return eq;
185+
return eq;
186186
}
187187

188188
public override int GetHashCode()

UnitTests/MatchersFixture.cs

+16-2
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,20 @@ public void MatchingNonNullableValueTypeForNullableParameterDoesNotMatchNull()
266266
Assert.Equal(0, mock.Object.TakesNullableParameter(null));
267267
}
268268

269-
private int GetToRange()
269+
[Fact]
270+
public void MultipleMatchingSetupsWithMultiplValueTypeArgumentsReplaceEachOtherForVerify()
271+
{
272+
var mock = new Mock<IFoo>();
273+
274+
mock.Setup(x => x.TakesTwoValueTypes(1, 2)).Verifiable();
275+
mock.Setup(x => x.TakesTwoValueTypes(1, 2)).Verifiable();
276+
277+
mock.Object.TakesTwoValueTypes(1, 2);
278+
279+
mock.Verify();
280+
}
281+
282+
private int GetToRange()
270283
{
271284
return 5;
272285
}
@@ -283,6 +296,7 @@ public interface IFoo
283296
int DoAddition(int[] numbers);
284297
int[] Items { get; set; }
285298
int TakesNullableParameter(int? value);
286-
}
299+
void TakesTwoValueTypes(int a, int b);
300+
}
287301
}
288302
}

0 commit comments

Comments
 (0)