[Test]
public void Strings_MyConstraint()
{
const string expected = "Joe";
const string actual = "joe";
var myConstraint = new MyConstraint(expected);
Assert.That(actual, Has.Some.Matches(myConstraint));
}
public class MyConstraint : Constraint
{
private readonly object m_expected;
public MyConstraint(object expectedValue)
{
m_expected = expectedValue;
}
#region Implementation of abstract methods
public override bool Matches(object actual)
{
return false;
//return ((string)m_expected).ToLower() == ((string)actual).ToLower();
}
public override void WriteDescriptionTo(MessageWriter writer)
{
throw new NotImplementedException();
}
#endregion
}
--
You received this message because you are subscribed to the Google Groups "NUnit-Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nunit-discus...@googlegroups.com.
To post to this group, send email to nunit-...@googlegroups.com.
Visit this group at http://groups.google.com/group/nunit-discuss.
For more options, visit https://groups.google.com/d/optout.
Such a constraint already exists:
Assert.That(actual, Is.EqualTo(expected).Ignor
e
Case)
[Test]
public void SameEvenChars_MyConstraint_Pass()
{
const string expected = "Tom";
const string actual = "Joe";
Assert.That(actual, Is.EqualTo(expected)
.OnlyEvenChars(expected));
}
[Test]
[ExpectedException(typeof(AssertionException))]
public void NotSameEvenChars_MyConstraint_Failed()
{
const string expected = "Tom";
const string actual = "Sam";
Assert.That(actual, Is.EqualTo(expected)
.OnlyEvenChars(expected));
}
public class EvenCharsConstraint : EqualConstraint
{
private readonly object m_expected;
private object m_actual;
public EvenCharsConstraint(object expectedValue)
: base(expectedValue)
{
m_expected = expectedValue;
}
#region Implementation of abstract methods
public override bool Matches(object actual)
{
m_actual = actual;
bool matches = base.Matches(actual);
if (!matches && m_expected is String && m_actual is String)
{
string expectedString = (string) m_expected;
string actualString = (string) actual;
if (expectedString.Length == actualString.Length)
{
bool result = !expectedString.Where((expectedChar, index) => (index + 1) % 2 == 0 && expectedChar != actualString[index]).Any();
matches = result;
}
}
return matches;
}
#endregion
}
public static class MyIs
{
public static EqualConstraint OnlyEvenChars(this EqualConstraint equalConstraint, object expected)
{
return new EvenCharsConstraint(expected);
}
}
[Test]
public void SameEvenChars_MyConstraint_Pass()
{
const string expected = "Tom";
const string actual = "Joe";
Assert.That(actual, Is.EqualTo(expected)
.OnlyEvenChars());
}
public class EvenCharsEqualityComparer : IEqualityComparer
{
public new bool Equals(object x, object y)
{
if (x is String && y is String)
{
string xString = x as String;
string yString = y as String;
bool result = (xString.Length == yString.Length);
result = result && !yString.Where((expectedChar, index) => (index + 1) % 2 == 0 && expectedChar != xString[index]).Any();
return result;
}
var equalConstraint = new EqualConstraint(y);
return equalConstraint.Matches(x);
}
public int GetHashCode(object obj)
{
return base.GetHashCode();
}
}