Update of /cvsroot/nmock/nmock2/src/NMock2/Matchers In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv24181/src/NMock2/Matchers Added Files: AlwaysMatcher.cs BinaryOperator.cs NullMatcher.cs OrMatcher.cs NotMatcher.cs PropertyMatcher.cs ComparisonMatcher.cs DescriptionOverride.cs MethodNameMatcher.cs EqualMatcher.cs ToStringMatcher.cs StringContainsMatcher.cs SameMatcher.cs AndMatcher.cs ArgumentsMatcher.cs IndexSetterArgumentsMatcher.cs IndexGetterArgumentsMatcher.cs Log Message: first upload of nmock2 --- NEW FILE: DescriptionOverride.cs --- (This appears to be a binary file; contents omitted.) --- NEW FILE: SameMatcher.cs --- using System.IO; namespace NMock2.Matchers { public class SameMatcher : Matcher { private object expected; public SameMatcher(object expected) { this.expected = expected; } public override bool Matches(object o) { return expected == o; } public override void DescribeTo(TextWriter writer) { writer.Write("same as "); writer.Write(expected); } } } --- NEW FILE: ComparisonMatcher.cs --- using System; using System.IO; namespace NMock2.Matchers { public class ComparisonMatcher : Matcher { private readonly IComparable value; private readonly int minComparisonResult; private readonly int maxComparisonResult; public ComparisonMatcher(IComparable value, int comparisonResult1, int comparisonResult2) { this.value = value; this.minComparisonResult = Math.Min(comparisonResult1, comparisonResult2) ; this.maxComparisonResult = Math.Max(comparisonResult1, comparisonResult2) ; if (minComparisonResult == -1 && maxComparisonResult == 1) { throw new ArgumentException("comparison result range too large", "comparisonResult1, comparisonResult2"); } } public override bool Matches(object o) { if (o.GetType() == value.GetType()) { int comparisonResult = -(value.CompareTo(o)); return comparisonResult >= minComparisonResult && comparisonResult <= maxComparisonResult; } else { return false; } } public override void DescribeTo(TextWriter writer) { writer.Write("? "); if (minComparisonResult == -1) writer.Write("<"); if (maxComparisonResult == 1) writer.Write(">"); if (minComparisonResult == 0 || maxComparisonResult == 0) writer.Write("="); writer.Write(" "); writer.Write(value); } } } --- NEW FILE: ToStringMatcher.cs --- using System.IO; namespace NMock2.Matchers { public class ToStringMatcher : Matcher { private readonly Matcher stringMatcher; public ToStringMatcher(Matcher stringMatcher) { this.stringMatcher = stringMatcher; } public override bool Matches(object o) { return stringMatcher.Matches(o.ToString()); } public override void DescribeTo(TextWriter writer) { writer.Write("an object with a string representation that is "); stringMatcher.DescribeTo(writer); } } } --- NEW FILE: ArgumentsMatcher.cs --- (This appears to be a binary file; contents omitted.) --- NEW FILE: StringContainsMatcher.cs --- using System.IO; namespace NMock2.Matchers { public class StringContainsMatcher : Matcher { private readonly string substring; public StringContainsMatcher(string substring) { this.substring = substring; } public override bool Matches(object o) { return o != null && o is string && ((string)o).IndexOf(substring) >= 0; } public override void DescribeTo(TextWriter writer) { writer.Write("containing "); writer.Write((object)substring); } } } --- NEW FILE: IndexSetterArgumentsMatcher.cs --- using System.IO; namespace NMock2.Matchers { public class IndexSetterArgumentsMatcher : ArgumentsMatcher { public IndexSetterArgumentsMatcher(params Matcher[] valueMatchers) : base(valueMatchers) { } public override void DescribeTo(TextWriter writer) { writer.Write("["); WriteListOfMatchers(MatcherCount()-1, writer); writer.Write("] = ("); LastMatcher().DescribeTo(writer); writer.Write(")"); } } } --- NEW FILE: NotMatcher.cs --- using System.IO; namespace NMock2.Matchers { public class NotMatcher : Matcher { private readonly Matcher negated; public NotMatcher(Matcher negated) { this.negated = negated; } public override bool Matches(object o) { return !negated.Matches(o); } public override void DescribeTo(TextWriter writer) { writer.Write("not "); negated.DescribeTo(writer); } } } --- NEW FILE: AlwaysMatcher.cs --- using System.IO; namespace NMock2.Matchers { public class AlwaysMatcher : Matcher { private bool matches; private string description; public AlwaysMatcher(bool matches, string description) { this.matches = matches; this.description = description; } public override bool Matches(object o) { return matches; } public override void DescribeTo(TextWriter writer) { writer.Write(description); } } } --- NEW FILE: MethodNameMatcher.cs --- (This appears to be a binary file; contents omitted.) --- NEW FILE: PropertyMatcher.cs --- using System; using System.IO; using System.Reflection; namespace NMock2.Matchers { public class PropertyMatcher : Matcher { private readonly string propertyName; private readonly Matcher valueMatcher; public PropertyMatcher(string propertyName, Matcher valueMatcher) { this.propertyName = propertyName; this.valueMatcher = valueMatcher; } public override bool Matches(object o) { Type type = o.GetType(); PropertyInfo property = type.GetProperty(propertyName, BindingFlags.Public|BindingFlags.Instance); if (property == null) return false; if (!property.CanRead) return false; object value = property.GetValue(o, null); return valueMatcher.Matches(value); } public override void DescribeTo(TextWriter writer) { throw new NotImplementedException(); } } } --- NEW FILE: AndMatcher.cs --- using System.IO; namespace NMock2.Matchers { public class AndMatcher : BinaryOperator { public AndMatcher(Matcher left, Matcher right) : base(left,right) { } public override bool Matches(object o) { return left.Matches(o) && right.Matches(o); } public override void DescribeTo(TextWriter writer) { writer.Write("`"); left.DescribeTo(writer); writer.Write("' and `"); right.DescribeTo(writer); writer.Write("'"); } } } --- NEW FILE: EqualMatcher.cs --- using System; using System.IO; namespace NMock2.Matchers { public class EqualMatcher : Matcher { private readonly object expected; public EqualMatcher(object expected) { this.expected = expected; } public override bool Matches(object actual) { return AreEqual(this.expected, actual); } private bool AreEqual(object o1, object o2) { if (o1 is Array) { return o2 is Array && ArraysEqual( (Array)o1, (Array)o2 ); } else { return Object.Equals(o1,o2); } } private bool ArraysEqual(Array a1, Array a2) { return a1.Rank == a2.Rank && ArrayLengthsEqual(a1,a2) && ArrayElementsEqual(a1,a2); } private bool ArrayLengthsEqual(Array a1, Array a2) { if (a1.Rank != a2.Rank) return false; for (int dimension = 0; dimension < a1.Rank; dimension++) { if (a1.GetLength(dimension) != a2.GetLength(dimension)) return false; } return true; } private bool ArrayElementsEqual(Array a1, Array a2) { for (int i = 0; i < a1.Length; i++) { if (!AreEqual(a1.GetValue(i), a2.GetValue(i))) return false; } return true; } public override void DescribeTo(TextWriter writer) { writer.Write("equal to "); writer.Write(expected); } } } --- NEW FILE: IndexGetterArgumentsMatcher.cs --- using System.IO; namespace NMock2.Matchers { public class IndexGetterArgumentsMatcher : ArgumentsMatcher { public IndexGetterArgumentsMatcher(params Matcher[] valueMatchers) : base(valueMatchers) { } public override void DescribeTo(TextWriter writer) { writer.Write("["); WriteListOfMatchers(MatcherCount(), writer); writer.Write("]"); } } } --- NEW FILE: BinaryOperator.cs --- namespace NMock2.Matchers { public abstract class BinaryOperator : Matcher { protected readonly Matcher right; protected readonly Matcher left; protected BinaryOperator(Matcher left, Matcher right) { this.left = left; this.right = right; } } } --- NEW FILE: OrMatcher.cs --- using System.IO; namespace NMock2.Matchers { public class OrMatcher : BinaryOperator { public OrMatcher(Matcher left, Matcher right) : base(left,right) { } public override bool Matches(object o) { return left.Matches(o) || right.Matches(o); } public override void DescribeTo(TextWriter writer) { writer.Write("`"); left.DescribeTo(writer); writer.Write("' or `"); right.DescribeTo(writer); writer.Write("'"); } } } --- NEW FILE: NullMatcher.cs --- using System.IO; namespace NMock2.Matchers { public class NullMatcher : Matcher { public override bool Matches(object o) { return o == null; } public override void DescribeTo(TextWriter writer) { writer.Write("null"); } } } |