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

Implement It.Is, It.IsIn, It.IsNotIn with a comparer overload #1064

Merged
merged 1 commit into from
Oct 1, 2020
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: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file.

The format is loosely based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).

## Unreleased

#### Added

* New method overloads for `It.Is`, `It.IsIn`, and `It.IsNotIn` that compare values using a custom `IEqualityComparer<T>` (@weitzhandler, #1064)
Implement It.Is, It.IsIn, It.IsNotIn with a comparer overload (#1059)


## 4.14.6 (2020-09-30)

Expand Down
39 changes: 39 additions & 0 deletions src/Moq/It.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,23 @@ public static TValue Is<TValue>(Expression<Func<object, Type, bool>> match)
Expression.Lambda<Func<TValue>>(Expression.Call(thisMethod.MakeGenericMethod(typeof(TValue)), match)));
}

/// <summary>
/// Matches any value that equals the <paramref name="value"/> using the <paramref name="comparer"/>.
/// To use the default comparer for the specified object, specify the value inline,
/// i.e. <code>mock.Verify(service => service.DoWork(value))</code>.
/// <para>
/// Use this overload when you specify a value and a comparer.
/// </para>
/// </summary>
/// <param name="value">The value to match with.</param>
/// <param name="comparer">An <see cref="IEqualityComparer{T}"/> with which the values should be compared.</param>
/// <typeparam name="TValue">Type of the argument to check.</typeparam>
[EditorBrowsable(EditorBrowsableState.Advanced)]
public static TValue Is<TValue>(TValue value, IEqualityComparer<TValue> comparer)
{
return Match.Create<TValue>(actual => comparer.Equals(actual, value), () => It.Is<TValue>(value, comparer));
}

/// <summary>
/// Matches any value that is in the range specified.
/// </summary>
Expand Down Expand Up @@ -234,6 +251,17 @@ public static TValue IsIn<TValue>(IEnumerable<TValue> items)
return Match<TValue>.Create(value => items.Contains(value), () => It.IsIn(items));
weitzhandler marked this conversation as resolved.
Show resolved Hide resolved
}

/// <summary>
/// Matches any value that is present in the sequence specified.
/// </summary>
/// <param name="items">The sequence of possible values.</param>
/// <param name="comparer">An <see cref="IEqualityComparer{T}"/> with which the values should be compared.</param>
/// <typeparam name="TValue">Type of the argument to check.</typeparam>
public static TValue IsIn<TValue>(IEnumerable<TValue> items, IEqualityComparer<TValue> comparer)
{
return Match<TValue>.Create(value => items.Contains(value, comparer), () => It.IsIn(items, comparer));
}

/// <summary>
/// Matches any value that is present in the sequence specified.
/// </summary>
Expand Down Expand Up @@ -276,6 +304,17 @@ public static TValue IsNotIn<TValue>(IEnumerable<TValue> items)
return Match<TValue>.Create(value => !items.Contains(value), () => It.IsNotIn(items));
}

/// <summary>
/// Matches any value that is not found in the sequence specified.
/// </summary>
/// <param name="items">The sequence of disallowed values.</param>
/// <param name="comparer">An <see cref="IEqualityComparer{T}"/> with which the values should be compared.</param>
/// <typeparam name="TValue">Type of the argument to check.</typeparam>
public static TValue IsNotIn<TValue>(IEnumerable<TValue> items, IEqualityComparer<TValue> comparer)
{
return Match<TValue>.Create(value => !items.Contains(value, comparer), () => It.IsNotIn(items, comparer));
}

/// <summary>
/// Matches any value that is not found in the sequence specified.
/// </summary>
Expand Down
61 changes: 61 additions & 0 deletions tests/Moq.Tests/CustomTypeMatchersFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// All rights reserved. Licensed under the BSD 3-Clause License; see License.txt.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;

using Xunit;
Expand Down Expand Up @@ -160,6 +162,46 @@ public void It_Is_works_with_custom_matcher()
Assert.Equal(2, invocationCount);
}

[Fact]
public void It_Is_works_with_custom_comparer()
{
var acceptableArg = "FOO";

var invocationCount = 0;
var mock = new Mock<IX>();
mock.Setup(m => m.Method(It.Is<string>(acceptableArg, StringComparer.OrdinalIgnoreCase)))
.Callback((object arg) => invocationCount++);

mock.Object.Method("foo");
mock.Object.Method("bar");
Assert.Equal(1, invocationCount);

mock.Object.Method("FOO");
mock.Object.Method("foo");
mock.Object.Method((string)null);
Assert.Equal(3, invocationCount);
}

[Fact]
public void It_Is_object_works_with_custom_comparer()
{
var acceptableArg = "FOO";

var invocationCount = 0;
var mock = new Mock<IX>();
mock.Setup(m => m.Method(It.Is<object>(acceptableArg, new ObjectStringOrdinalIgnoreCaseComparer())))
.Callback((object arg) => invocationCount++);
stakx marked this conversation as resolved.
Show resolved Hide resolved

mock.Object.Method("foo");
mock.Object.Method("bar");
Assert.Equal(1, invocationCount);

mock.Object.Method("FOO");
mock.Object.Method("foo");
mock.Object.Method((string)null);
Assert.Equal(3, invocationCount);
}

public interface IX
{
void Method<T>();
Expand Down Expand Up @@ -229,5 +271,24 @@ public struct AnyStruct : ITypeMatcher
{
public bool Matches(Type typeArgument) => typeArgument.IsValueType;
}

public class ObjectStringOrdinalIgnoreCaseComparer : IEqualityComparer<object>
{
private static IEqualityComparer<string> InternalComparer => StringComparer.OrdinalIgnoreCase;

public new bool Equals(object x, object y)
{
Debug.Assert(x is string && y is string);

return InternalComparer.Equals((string)x, (string)y);
}

public int GetHashCode(object obj)
{
Debug.Assert(obj is string);

return InternalComparer.GetHashCode((string)obj);
}
}
}
}
39 changes: 39 additions & 0 deletions tests/Moq.Tests/MatchersFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,25 @@ public void MatchesIsInEnumerable()
Assert.Equal(2, mock.Object.Echo(9));
}

[Fact]
public void MatchesIsInEnumerableWithCustomComparer()
{
var acceptableArgs = Enumerable.Repeat("foo", 1);
var unacceptableArgs = Enumerable.Repeat("bar", 1);

var mock = new Mock<IFoo>();

mock.Setup(x => x.Execute(It.IsIn(acceptableArgs, StringComparer.OrdinalIgnoreCase))).Returns("foo");
mock.Setup(x => x.Execute(It.IsIn(unacceptableArgs, StringComparer.OrdinalIgnoreCase))).Returns("bar");

Assert.Equal("foo", mock.Object.Execute("foo"));
Assert.Equal("foo", mock.Object.Execute("FOO"));
Assert.Equal("foo", mock.Object.Execute("FoO"));

Assert.Equal("bar", mock.Object.Execute("bar"));
Assert.Equal("bar", mock.Object.Execute("BAR"));
}

[Fact]
public void MatchesIsInVariadicParameters()
{
Expand Down Expand Up @@ -112,6 +131,26 @@ public void MatchesIsNotInEnumerable()
Assert.Equal(1, mock.Object.Echo(9));
}

[Fact]
public void MatchesIsNotInEnumerableWithCustomComparer()
{
var acceptableArgs = new[] { "foo", "bar" };

var mock = new Mock<IFoo>();

mock.Setup(x => x.Execute(It.IsNotIn(acceptableArgs, StringComparer.OrdinalIgnoreCase))).Returns("foo");

Assert.Equal("foo", mock.Object.Execute("baz"));
Assert.Equal("foo", mock.Object.Execute("alpha"));

Assert.Equal(default, mock.Object.Execute("foo"));
Assert.Equal(default, mock.Object.Execute("FOO"));
Assert.Equal(default, mock.Object.Execute("FoO"));
Assert.Equal(default, mock.Object.Execute("Bar"));
Assert.Equal(default, mock.Object.Execute("BAR"));
Assert.Equal(default, mock.Object.Execute("bar"));
}

[Fact]
public void MatchesIsNotInVariadicParameters()
{
Expand Down