Skip to content
This repository has been archived by the owner on Oct 21, 2018. It is now read-only.

IEqualityComparer for Validate #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
59 changes: 58 additions & 1 deletion Rhino.Mocks.Tests/Impl/ValidateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,64 @@ namespace Rhino.Mocks.Tests.Impl
{

public class ValidateTests
{
{
private class AllObjectsCreatedEqualEqualityComparerImpl : IEqualityComparer
{
#region Implementation of IEqualityComparer

/// <summary>
/// Determines whether the specified objects are equal.
/// </summary>
/// <returns>
/// true if the specified objects are equal; otherwise, false.
/// </returns>
/// <param name="x">The first object to compare.
/// </param><param name="y">The second object to compare.
/// </param><exception cref="T:System.ArgumentException"><paramref name="x"/> and <paramref name="y"/> are of different types and neither one can handle comparisons with the other.
/// </exception>
public bool Equals(object x, object y)
{
return true;
}

/// <summary>
/// Returns a hash code for the specified object.
/// </summary>
/// <returns>
/// A hash code for the specified object.
/// </returns>
/// <param name="obj">The <see cref="T:System.Object"/> for which a hash code is to be returned.
/// </param><exception cref="T:System.ArgumentNullException">The type of <paramref name="obj"/> is a reference type and <paramref name="obj"/> is null.
/// </exception>
public int GetHashCode(object obj)
{
throw new NotImplementedException();
}

#endregion
}

[Fact]
public void ValidateUseEqualityComparerIfNotNull()
{
string string1 = "string1";
string string2 = "string2";

Assert.False(Validate.AreEqual(string1, string2));

try
{
// Implementation isn't important here. Just want to know that it gets called
Validate.EqualityComparer = new AllObjectsCreatedEqualEqualityComparerImpl();

Assert.True(Validate.AreEqual(string1, string2));
}
finally
{
Validate.EqualityComparer = null;
}
}

[Fact]
public void IsNotNullWhenNotNull()
{
Expand Down
288 changes: 150 additions & 138 deletions Rhino.Mocks/Impl/Validate.cs
Original file line number Diff line number Diff line change
@@ -1,138 +1,150 @@
#region license
// Copyright (c) 2005 - 2007 Ayende Rahien ([email protected])
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
// * Neither the name of Ayende Rahien nor the names of its
// contributors may be used to endorse or promote products derived from this
// software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#endregion


using System;
using Rhino.Mocks.Interfaces;
using System.Collections;

namespace Rhino.Mocks.Impl
{
/// <summary>
/// Validate arguments for methods
/// </summary>
public static class Validate
{
/// <summary>
/// Validate that the passed argument is not null.
/// </summary>
/// <param name="obj">The object to validate</param>
/// <param name="name">The name of the argument</param>
/// <exception cref="ArgumentNullException">
/// If the obj is null, an ArgumentNullException with the passed name
/// is thrown.
/// </exception>
public static void IsNotNull(object obj, string name)
{
if (obj == null)
throw new ArgumentNullException(name);
}

/// <summary>
/// Validate that the arguments are equal.
/// </summary>
/// <param name="expectedArgs">Expected args.</param>
/// <param name="actualArgs">Actual Args.</param>
public static bool ArgsEqual(object[] expectedArgs, object[] actualArgs)
{
return RecursiveCollectionEqual(expectedArgs, actualArgs);
}

/// <summary>
/// Validate that the two arguments are equals, including validation for
/// when the arguments are collections, in which case it will validate their values.
/// </summary>
public static bool AreEqual(object expectedArg, object actualArg)
{
return RecursiveCollectionEqual(new object[] { expectedArg }, new object[] { actualArg });
}

#region Implementation

private static bool RecursiveCollectionEqual(ICollection expectedArgs, ICollection actualArgs)
{
if(expectedArgs == null && actualArgs == null)
return true;
if(expectedArgs==null || actualArgs==null)
return false;

if (expectedArgs.Count != actualArgs.Count)
return false;

IEnumerator expectedArgsEnumerator = expectedArgs.GetEnumerator();
IEnumerator actualArgsEnumerator = actualArgs.GetEnumerator();
while (expectedArgsEnumerator.MoveNext()
&& actualArgsEnumerator.MoveNext())
{
object expected = expectedArgsEnumerator.Current;
object actual = actualArgsEnumerator.Current;

if (expected == null)
{
if (actual == null)
continue;
else
return false;
}

if (SafeEquals(expected, actual))
continue;

if (expected is ICollection)
{
if (!RecursiveCollectionEqual(expected as ICollection, actual as ICollection))
return false;

continue;
}
return false;
}
return true;
}

/// <summary>
/// This method is safe for use even if any of the objects is a mocked object
/// that override equals.
/// </summary>
private static bool SafeEquals(object expected, object actual)
{
IMockedObject expectedMock = expected as IMockedObject;
IMockedObject actualMock = actual as IMockedObject;
//none are mocked object
if (expectedMock == null && actualMock == null)
{
return expected.Equals(actual);
}
//if any of them is a mocked object, use mocks equality
//this may not be what the user is expecting, but it is needed, because
//otherwise we get into endless loop.
return MockedObjectsEquality.Instance.Equals(expected,actual);
}
#endregion
}
}
#region license
// Copyright (c) 2005 - 2007 Ayende Rahien ([email protected])
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
// * Neither the name of Ayende Rahien nor the names of its
// contributors may be used to endorse or promote products derived from this
// software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#endregion


using System;
using Rhino.Mocks.Interfaces;
using System.Collections;

namespace Rhino.Mocks.Impl
{
/// <summary>
/// Validate arguments for methods
/// </summary>
public static class Validate
{
/// <summary>
/// Gets or sets the equality comparer tha is used for testing the equality of two instances.
/// </summary>
/// <remarks>If this is <c>null</c> then default equality is used.</remarks>
/// <value>The equality comparer.</value>
public static IEqualityComparer EqualityComparer { get; set; }

/// <summary>
/// Validate that the passed argument is not null.
/// </summary>
/// <param name="obj">The object to validate</param>
/// <param name="name">The name of the argument</param>
/// <exception cref="ArgumentNullException">
/// If the obj is null, an ArgumentNullException with the passed name
/// is thrown.
/// </exception>
public static void IsNotNull(object obj, string name)
{
if (obj == null)
throw new ArgumentNullException(name);
}

/// <summary>
/// Validate that the arguments are equal.
/// </summary>
/// <param name="expectedArgs">Expected args.</param>
/// <param name="actualArgs">Actual Args.</param>
public static bool ArgsEqual(object[] expectedArgs, object[] actualArgs)
{
return RecursiveCollectionEqual(expectedArgs, actualArgs);
}

/// <summary>
/// Validate that the two arguments are equals, including validation for
/// when the arguments are collections, in which case it will validate their values.
/// </summary>
public static bool AreEqual(object expectedArg, object actualArg)
{
return RecursiveCollectionEqual(new object[] { expectedArg }, new object[] { actualArg });
}

#region Implementation

private static bool RecursiveCollectionEqual(ICollection expectedArgs, ICollection actualArgs)
{
if (expectedArgs == null && actualArgs == null)
return true;
if (expectedArgs == null || actualArgs == null)
return false;

if (expectedArgs.Count != actualArgs.Count)
return false;

IEnumerator expectedArgsEnumerator = expectedArgs.GetEnumerator();
IEnumerator actualArgsEnumerator = actualArgs.GetEnumerator();
while (expectedArgsEnumerator.MoveNext()
&& actualArgsEnumerator.MoveNext())
{
object expected = expectedArgsEnumerator.Current;
object actual = actualArgsEnumerator.Current;

if (expected == null)
{
if (actual == null)
continue;
else
return false;
}

if (SafeEquals(expected, actual))
continue;

if (expected is ICollection)
{
if (!RecursiveCollectionEqual(expected as ICollection, actual as ICollection))
return false;

continue;
}
return false;
}
return true;
}

/// <summary>
/// This method is safe for use even if any of the objects is a mocked object
/// that override equals.
/// </summary>
private static bool SafeEquals(object expected, object actual)
{
IMockedObject expectedMock = expected as IMockedObject;
IMockedObject actualMock = actual as IMockedObject;
//none are mocked object
if (expectedMock == null && actualMock == null)
{
if (EqualityComparer != null)
{
return EqualityComparer.Equals(expected, actual);
}

return expected.Equals(actual);
}
//if any of them is a mocked object, use mocks equality
//this may not be what the user is expecting, but it is needed, because
//otherwise we get into endless loop.
return MockedObjectsEquality.Instance.Equals(expected, actual);
}
#endregion
}
}