Skip to content

Fix type cast bug in ParamArrayMatcher #912

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

Merged
merged 3 commits into from
Aug 29, 2019
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
25 changes: 15 additions & 10 deletions src/Moq/Match.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// All rights reserved. Licensed under the BSD 3-Clause License; see License.txt.

using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq.Expressions;
using System.Reflection;
Expand Down Expand Up @@ -108,24 +109,28 @@ internal Match(Predicate<T> condition, Expression<Func<T>> renderExpression, Act

internal override bool Matches(object value)
{
bool canCastValueToT;
return CanCast(value) && this.Condition((T)value);
}

internal override void SetupEvaluatedSuccessfully(object value)
{
Debug.Assert(this.Matches(value));
Debug.Assert(CanCast(value));

this.Success?.Invoke((T)value);
}

private static bool CanCast(object value)
{
if (value != null)
{
canCastValueToT = value is T;
return value is T;
}
else
{
var t = typeof(T);
canCastValueToT = !t.IsValueType || (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>));
return !t.IsValueType || (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>));
}

return canCastValueToT && this.Condition((T)value);
}

internal override void SetupEvaluatedSuccessfully(object value)
{
this.Success?.Invoke((T)value);
}

/// <inheritdoc/>
Expand Down
3 changes: 3 additions & 0 deletions src/Moq/Matchers/AnyMatcher.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD.
// All rights reserved. Licensed under the BSD 3-Clause License; see License.txt.

using System.Diagnostics;

namespace Moq.Matchers
{
internal sealed class AnyMatcher : IMatcher
Expand All @@ -15,6 +17,7 @@ private AnyMatcher()

public void SetupEvaluatedSuccessfully(object value)
{
Debug.Assert(this.Matches(value));
}
}
}
3 changes: 2 additions & 1 deletion src/Moq/Matchers/ConstantMatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// All rights reserved. Licensed under the BSD 3-Clause License; see License.txt.

using System.Collections;
using System.Diagnostics;
using System.Linq;
using System.Linq.Expressions;

namespace Moq.Matchers
{
Expand Down Expand Up @@ -37,6 +37,7 @@ public bool Matches(object value)

public void SetupEvaluatedSuccessfully(object value)
{
Debug.Assert(this.Matches(value));
}

private bool MatchesEnumerable(IEnumerable enumerable)
Expand Down
3 changes: 2 additions & 1 deletion src/Moq/Matchers/ExpressionMatcher.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD.
// All rights reserved. Licensed under the BSD 3-Clause License; see License.txt.

using System;
using System.Diagnostics;
using System.Linq.Expressions;

namespace Moq.Matchers
Expand All @@ -23,6 +23,7 @@ public bool Matches(object value)

public void SetupEvaluatedSuccessfully(object value)
{
Debug.Assert(this.Matches(value));
}
}
}
2 changes: 2 additions & 0 deletions src/Moq/Matchers/LazyEvalMatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// All rights reserved. Licensed under the BSD 3-Clause License; see License.txt.

using System;
using System.Diagnostics;
using System.Linq.Expressions;

namespace Moq.Matchers
Expand All @@ -28,6 +29,7 @@ public bool Matches(object value)

public void SetupEvaluatedSuccessfully(object value)
{
Debug.Assert(this.Matches(value));
}
}
}
2 changes: 2 additions & 0 deletions src/Moq/Matchers/MatcherAttributeMatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Linq.Expressions;
Expand Down Expand Up @@ -91,6 +92,7 @@ public bool Matches(object value)

public void SetupEvaluatedSuccessfully(object value)
{
Debug.Assert(this.Matches(value));
}
}
}
8 changes: 6 additions & 2 deletions src/Moq/Matchers/ParamArrayMatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,13 @@ public bool Matches(object value)

public void SetupEvaluatedSuccessfully(object value)
{
foreach (var matcher in this.matchers)
Debug.Assert(this.Matches(value));
Debug.Assert(value is Array array && array.Length == this.matchers.Length);

var values = (Array)value;
for (int i = 0, n = this.matchers.Length; i < n; ++i)
{
matcher.SetupEvaluatedSuccessfully(value);
this.matchers[i].SetupEvaluatedSuccessfully(values.GetValue(i));
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/Moq/Matchers/RefMatcher.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD.
// All rights reserved. Licensed under the BSD 3-Clause License; see License.txt.

using System.Diagnostics;

namespace Moq.Matchers
{
internal class RefMatcher : IMatcher
Expand All @@ -22,6 +24,7 @@ public bool Matches(object value)

public void SetupEvaluatedSuccessfully(object value)
{
Debug.Assert(this.Matches(value));
}
}
}
54 changes: 54 additions & 0 deletions tests/Moq.Tests/Matchers/ParamArrayMatcherFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD.
// All rights reserved. Licensed under the BSD 3-Clause License; see License.txt.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Xunit;

namespace Moq.Tests.Matchers
{
public class ParamArrayMatcherFixture
{
[Theory]
[InlineData(42, "", true)]
[InlineData(42, null, true)]
[InlineData(3.141f, "", false)]
[InlineData(null, "", false)]
public void Matches_several_matchers_from_params_array(object first, object second, bool shouldMatch)
{
var seconds = new List<string>();
var methodCallExpr = (MethodCallExpression)ToExpression<IX>(x => x.Method(It.IsAny<int>(), Capture.In(seconds))).Body;
var expr = methodCallExpr.Arguments.Single();
var parameter = typeof(IX).GetMethod("Method").GetParameters().Single();

var (matcher, _) = MatcherFactory.CreateMatcher(expr, parameter);

Assert.Equal(shouldMatch, matcher.Matches(new object[] { first, second }));
}

[Fact]
public void SetupEvaluatedSuccessfully_succeeds_for_matching_values()
{
var seconds = new List<string>();
var methodCallExpr = (MethodCallExpression)ToExpression<IX>(x => x.Method(It.IsAny<int>(), Capture.In(seconds))).Body;
var expr = methodCallExpr.Arguments.Single();
var parameter = typeof(IX).GetMethod("Method").GetParameters().Single();

var (matcher, _) = MatcherFactory.CreateMatcher(expr, parameter);

matcher.SetupEvaluatedSuccessfully(new object[] { 42, "" });
}

private LambdaExpression ToExpression<T>(Expression<Action<T>> expr)
{
return expr;
}

public interface IX
{
void Method(params object[] args);
}
}
}