chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
0 votes
How about IsComparable(object, IComparer)?
asked by ptierney (1.2k points)

7 Answers

0 votes
How about IsComparable(object, IComparer)?

Hi,
Thanks for this idea, can you please elaborate?

Are you talking about System.IComparer?
If so IComparer can compare itself to anouther object so:
IsComparable(IComparer) should be enough
(you would use .Args(Check.IsComparable(somethingThatImplementsIComparer))
Normally IComparer is used for sorted items (larger, equals or smaller), which is not our case - we are looking for boolean answers (valid or not)
answered by richard (3.9k points)
0 votes
I was thinking of an equality check
public class SomeObjectComparer: IComparer
{
   int Compare(object x, object y)
   {
      // compare SomeObjects
   }
}

[Test]
public void Test()
{
   SomeObject compareTo;
   
   mock.ExpectCall("SomeMethod").Args(Check.IsComparable(compareTo, new SomeObjectComparer()));
   
   // true if SomeObjectComparer.Compare(arg, compareTo) == 0
}

but I suppose you could also do:
public enum CompareResult
{
   LessThan
   EqualTo
   GreaterThan
}

[Test]
public void Test()
{
   CompareResult expectedCompareResult = CompareResult.LessThan
   
   mock.ExpectCall("SomeMethod").Args(Check.IsComparable(compareTo, new SomeObjectComparer(), expectedCompareResult));
   
   // true if SomeObjectComparer.Compare(arg, compareTo) < 0; (arg is less than compareTo)
}

Maybe use of IComparer is confusing, it might be better to define a new interface?
answered by ptierney (1.2k points)
0 votes
Ok,
I think that I understand you better now,
Do you think that it is possible to do this as follows:

public class SomeObjectComparer
{ 
   private object expected;
   public SomeObjectComparer(object expected) 
   {
       this.expected = expected;
   }
   public bool Compare(object y) 
   { 
      // compare SomeObjects 
      return expected == y;
   } 
} 

[Test] 
public void Test() 
{ 

  mock.ExpectCall("SomeMethod").Args(
     new ParameterChecker(
       new SomeObjectComparer(expected).Compare)); 
    
   // true if SomeObjectComparer.Compare(compareTo) is true
}
answered by scott (32k points)
0 votes
Thanks Scott, that would do the job, although I like the Check.IsXXX syntax.

Now that I think about it - how about an overload of Check.IsEqual() that takes an IComparer?

No, oh well, the ParameterChecker solution will be fine.
answered by ptierney (1.2k points)
0 votes
Hi,
What do you think about adding:
ComparableCheck.IsEqual(IComparable) that will fail unless IComparable.Compare(recived) is zero.

We can also have ComparableCheck.IsLarger(IComparable) and ComparableCheck.IsSmaller(IComparable)
answered by scott (32k points)
+1 vote
I suppose the only benefit of using IComparer is that you could compare to more than one expected value e.g.

ComparableCheck.IsIn(object[], IComparer)

But I may be labouring the point here.
answered by ptierney (1.2k points)
0 votes
Hi,
I don't think that we will add this request, as I don't see it being useful to many developers. If anyone still wants this feature please post a reply to this topic, if we do find it useful we will add it.
answered by scott (32k points)
...