Update of /cvsroot/nmock/nmock2/src/NMock2.Test/Matchers In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv24181/src/NMock2.Test/Matchers Added Files: DescriptionOverrideTest.cs ToStringMatcherTest.cs MethodNameMatcherTest.cs MockMatcher.cs MatcherWithDescription.cs ComparisonMatcherTest.cs AndMatcherTest.cs EqualMatcherTest.cs SameMatcherTest.cs ArgumentsMatcherTest.cs NotMatcherTest.cs PropertyMatcherTest.cs StringContainsMatcherTest.cs AlwaysMatcherTest.cs OrMatcherTest.cs NullMatcherTest.cs Log Message: first upload of nmock2 --- NEW FILE: PropertyMatcherTest.cs --- using NMock2.Matchers; using NUnit.Framework; namespace NMock2.Test.Matchers { [TestFixture] public class PropertyMatcherTest { [Test] public void MatchesObjectWithNamedPropertyAndMatchingPropertyValue() { ObjectWithProperties o = new ObjectWithProperties(); object aValue = new NamedObject("aValue"); o.A = aValue; Matcher m = new PropertyMatcher("A", Is.Same(aValue) ); Assert.IsTrue(m.Matches(o), "should match o"); } [Test] public void DoesNotMatchObjectIfPropertyMatcherDoesNotMatch() { ObjectWithProperties o = new ObjectWithProperties(); object aValue = new NamedObject("aValue"); object otherValue = new NamedObject("otherValue"); o.A = aValue; Matcher m = new PropertyMatcher("A", new SameMatcher(otherValue) ); Assert.IsFalse(m.Matches(o), "should match o"); } [Test] public void DoesNotMatchObjectIfItDoesNotHaveNamedProperty() { ObjectWithProperties o = new ObjectWithProperties(); Matcher m = new PropertyMatcher("OtherProperty", new AlwaysMatcher(true,"anything")); Assert.IsFalse(m.Matches(o), "should not match o"); } [Test] public void DoesNotMatchWriteOnlyProperty() { ObjectWithProperties o = new ObjectWithProperties(); Matcher m = new PropertyMatcher("WriteOnlyProperty", new AlwaysMatcher(true,"anything")); Assert.IsFalse(m.Matches(o), "should not match o"); } [Test] public void DoesNotMatchPrivateProperty() { ObjectWithProperties o = new ObjectWithProperties(); Matcher m = new PropertyMatcher("PrivateProperty", new AlwaysMatcher(true,"anything")); Assert.IsFalse(m.Matches(o), "should not match o"); } public class ObjectWithProperties { private object a; public object A { get { return a; } set { a = value; } } public object WriteOnlyProperty { set {} } private object PrivateProperty { get { return "value"; } set {} } public void AMethodToGetAroundCompilerWarnings() { this.PrivateProperty = "something"; } } } } --- NEW FILE: ComparisonMatcherTest.cs --- using System; using NUnit.Framework; using NMock2.Matchers; namespace NMock2.Test.Matchers { [TestFixture] public class ComparisonMatcherTest { [Test] public void MatchesAComparisonOfAComparableValue() { Matcher matcher; matcher = new ComparisonMatcher(10, -1, 0); Assert.IsTrue(matcher.Matches(9), "less"); Assert.IsTrue(matcher.Matches(10), "equal"); Assert.IsFalse(matcher.Matches(11), "greater"); matcher = new ComparisonMatcher(10, -1, -1 ); Assert.IsTrue(matcher.Matches(9), "less"); Assert.IsFalse(matcher.Matches(10), "equal"); Assert.IsFalse(matcher.Matches(11), "greater"); matcher = new ComparisonMatcher(10, 0, 1 ); Assert.IsFalse(matcher.Matches(9), "less"); Assert.IsTrue(matcher.Matches(10), "equal"); Assert.IsTrue(matcher.Matches(11), "greater"); } [Test] public void DoesNotMatchObjectOfDifferentType() { Assert.IsFalse((new ComparisonMatcher(10,0,0)).Matches("a string")); } [Test,ExpectedException(typeof(ArgumentException))] public void CannotCreateComparisonThatMatchesAnything() { new ComparisonMatcher(10, -1, 1); } [Test] public void CanSpecifyMinAndMaxComparisonResultInAnyOrder() { Matcher matcher = new ComparisonMatcher(10, 0, -1); Assert.IsTrue(matcher.Matches(9), "less"); Assert.IsTrue(matcher.Matches(10), "equal"); Assert.IsFalse(matcher.Matches(11), "greater"); } [Test] public void HasReadableDescription() { AssertDescription.IsEqual(new ComparisonMatcher(10, -1, -1), "? < <10>"); AssertDescription.IsEqual(new ComparisonMatcher(10, -1, 0), "? <= <10>"); AssertDescription.IsEqual(new ComparisonMatcher(10, 0, 0), "? = <10>"); AssertDescription.IsEqual(new ComparisonMatcher(10, 0, 1), "? >= <10>"); AssertDescription.IsEqual(new ComparisonMatcher(10, 1, 1), "? > <10>"); } } } --- NEW FILE: StringContainsMatcherTest.cs --- using NUnit.Framework; using NMock2.Matchers; namespace NMock2.Test.Matchers { [TestFixture] public class StringContainsMatcherTest { [Test] public void MatchesIfStringArgumentContainsGivenSubstring() { string substring = "SUBSTRING"; Matcher matcher = new StringContainsMatcher(substring); Assert.IsTrue(matcher.Matches(substring), "arg is substring"); Assert.IsTrue(matcher.Matches(substring + "X"), "arg starts with substring"); Assert.IsTrue(matcher.Matches("X" + substring), "arg ends with substring"); Assert.IsTrue(matcher.Matches("X" + substring + "X"), "arg contains substring"); Assert.IsFalse(matcher.Matches("XX"), "arg does not contain substring"); Assert.IsFalse(matcher.Matches(null), "arg is null"); Assert.IsFalse(matcher.Matches(new object()), "arg is not a string"); } [Test] public void HasAReadableDescription() { string substring = "substring"; AssertDescription.IsEqual(new StringContainsMatcher(substring), "containing \"" + substring + "\""); } } } --- NEW FILE: MatcherWithDescription.cs --- using System; using System.IO; namespace NMock2.Test.Matchers { public class MatcherWithDescription : Matcher { private readonly string description; public MatcherWithDescription(string description) { this.description = description; } public override bool Matches(object o) { throw new NotImplementedException(); } public override void DescribeTo(TextWriter writer) { writer.Write(description); } } } --- NEW FILE: AndMatcherTest.cs --- using NUnit.Framework; using NMock2.Matchers; namespace NMock2.Test.Matchers { [TestFixture] public class AndMatcherTest { static readonly object ignored = new object(); static readonly Matcher TRUE = new AlwaysMatcher(true,"TRUE"); static readonly Matcher FALSE = new AlwaysMatcher(false, "FALSE"); static readonly object[,] truthTable = { {FALSE, FALSE, false}, {FALSE, TRUE, false}, {TRUE, FALSE, false}, {TRUE, TRUE, true} }; [Test] public void CalculatesLogicalConjunctionOfTwoMatchers() { for (int i = 0; i < truthTable.GetLength(0); i++) { Matcher matcher = new AndMatcher((Matcher)truthTable[i,0], (Matcher)truthTable[i,1]); Assert.AreEqual( truthTable[i,2], matcher.Matches(ignored) ); } } [Test] public void CanUseOperatorOverloadingAsSyntacticSugar() { for (int i = 0; i < truthTable.GetLength(0); i++) { Matcher arg1 = (Matcher)truthTable[i,0]; Matcher arg2 = (Matcher)truthTable[i,1]; Matcher matcher = arg1 & arg2; Assert.AreEqual( truthTable[i,2], matcher.Matches(ignored) ); } } [Test] public void HasAReadableDescription() { Matcher left = new MatcherWithDescription("<left>"); Matcher right = new MatcherWithDescription("<right>"); AssertDescription.IsComposed( new AndMatcher(left,right), "`{0}' and `{1}'", left, right); } } } --- NEW FILE: DescriptionOverrideTest.cs --- (This appears to be a binary file; contents omitted.) --- NEW FILE: MockMatcher.cs --- using System.IO; using NUnit.Framework; namespace NMock2.Test.Matchers { class MockMatcher : Matcher { public object ExpectedMatchesArg = null; public bool MatchesResult = false; public int MatchesCallCount = 0; public override bool Matches(object o) { MatchesCallCount++; Assert.AreEqual(ExpectedMatchesArg, o, "Matches arg"); return MatchesResult; } public void AssertMatchesCalled(string messageFormat, params object[] formatArgs) { AssertMatchesCalled(1, messageFormat, formatArgs); } public void AssertMatchesCalled(int times, string messageFormat, params object[] formatArgs) { Assert.AreEqual(times, MatchesCallCount, messageFormat, formatArgs); } public TextWriter ExpectedDescribeToWriter = null; public string DescribeToOutput = ""; public int DescribeToCallCount = 0; public override void DescribeTo(TextWriter writer) { DescribeToCallCount++; if (ExpectedDescribeToWriter != null) { Assert.AreSame(ExpectedDescribeToWriter, writer, "DescribeTo writer"); } writer.Write(DescribeToOutput); } } } --- NEW FILE: AlwaysMatcherTest.cs --- using NUnit.Framework; using NMock2.Matchers; namespace NMock2.Test.Matchers { [TestFixture] public class AlwaysMatcherTest { [Test] public void AlwaysReturnsFixedBooleanValueFromMatchesMethod() { Matcher matcher = new AlwaysMatcher(true, ""); Assert.IsTrue(matcher.Matches("something")); Assert.IsTrue(matcher.Matches("something else")); Assert.IsTrue(matcher.Matches(null)); Assert.IsTrue(matcher.Matches(1)); Assert.IsTrue(matcher.Matches(1.0)); Assert.IsTrue(matcher.Matches(new object())); matcher = new AlwaysMatcher(false, ""); Assert.IsFalse(matcher.Matches("something")); Assert.IsFalse(matcher.Matches("something else")); Assert.IsFalse(matcher.Matches(null)); Assert.IsFalse(matcher.Matches(1)); Assert.IsFalse(matcher.Matches(1.0)); Assert.IsFalse(matcher.Matches(new object())); } [Test] public void IsGivenADescription() { string description = "DESCRIPTION"; bool irrelevantFlag = false; AssertDescription.IsEqual(new AlwaysMatcher(irrelevantFlag, description), description); } } } --- NEW FILE: SameMatcherTest.cs --- using NUnit.Framework; using NMock2.Matchers; namespace NMock2.Test.Matchers { [TestFixture] public class SameMatcherTest { [Test] public void MatchesSameObject() { object same = new object(); object other = new object(); Matcher matcher = new SameMatcher(same); Assert.IsTrue(matcher.Matches(same), "same"); Assert.IsFalse(matcher.Matches(other), "other"); } [Test] public void IsNullSafe() { Assert.IsTrue( new SameMatcher(null).Matches(null), "null matches null" ); Assert.IsFalse( new SameMatcher("not null").Matches(null), "not null does not match null" ); Assert.IsFalse( new SameMatcher(null).Matches("not null"), "null does not match not null" ); } [Test] public void HasAReadableDescription() { object same = new object(); AssertDescription.IsEqual(new SameMatcher(same), "same as <"+same+">"); } } } --- NEW FILE: ToStringMatcherTest.cs --- using NUnit.Framework; using NMock2.Matchers; namespace NMock2.Test.Matchers { [TestFixture] public class ToStringMatcherTest { NamedObject arg; MockMatcher stringMatcher; Matcher matcher; [SetUp] public void SetUp() { arg = new NamedObject("arg"); stringMatcher = new MockMatcher(); matcher = new ToStringMatcher(this.stringMatcher); } [Test] public void PassesResultOfToStringToOtherMatcher() { stringMatcher.ExpectedMatchesArg = arg.ToString(); stringMatcher.MatchesResult = true; Assert.AreEqual(stringMatcher.MatchesResult, matcher.Matches(arg), "result"); stringMatcher.AssertMatchesCalled("should have passed string representation to stringMatcher"); } [Test] public void ReturnsAReadableDescription() { stringMatcher.DescribeToOutput = "<stringMatcher description>"; AssertDescription.IsComposed(matcher, "an object with a string representation that is {0}", stringMatcher); } } } --- NEW FILE: EqualMatcherTest.cs --- using NUnit.Framework; using NMock2.Matchers; namespace NMock2.Test.Matchers { [TestFixture] public class EqualMatcherTest { const string EXPECTED = "expected"; [Test] public void ComparesArgumentForEqualityToExpectedObject() { Matcher matcher = new EqualMatcher(EXPECTED); Assert.IsTrue( matcher.Matches(EXPECTED), "same object" ); Assert.IsTrue( matcher.Matches(EXPECTED.Clone()), "equal object" ); Assert.IsFalse( matcher.Matches("not expected"), "unequal object" ); } [Test] public void IsNullSafe() { Assert.IsTrue( new EqualMatcher(null).Matches(null), "null matches null" ); Assert.IsFalse( new EqualMatcher("not null").Matches(null), "not null does not match null" ); Assert.IsFalse( new EqualMatcher(null).Matches("not null"), "null does not match not null" ); } [Test] public void ComparesArraysForEqualityByContents() { int[] expected = {1,2}; int[] equal = {1,2}; int[] inequal = {2,3}; int[] longer = {1,2,3}; int[] shorter = {1}; int[] empty = {}; int[,] otherRank = {{1,2},{3,4}}; Matcher matcher = new EqualMatcher(expected); Assert.IsTrue(matcher.Matches(expected), "same array"); Assert.IsTrue(matcher.Matches(equal), "same contents"); Assert.IsFalse(matcher.Matches(inequal), "different contents"); Assert.IsFalse(matcher.Matches(longer), "longer"); Assert.IsFalse(matcher.Matches(shorter), "shorter"); Assert.IsFalse(matcher.Matches(empty), "empty"); Assert.IsFalse(matcher.Matches(otherRank), "other rank"); } [Test] public void RecursivelyComparesArrayContentsOfNestedArrays() { int[][] expected = new int[][] {new int[]{1,2},new int[]{3,4}}; int[][] equal = new int[][] {new int[]{1,2},new int[]{3,4}}; int[][] inequal = new int[][] {new int[]{2,3},new int[]{4,5}}; Matcher matcher = new EqualMatcher(expected); Assert.IsTrue(matcher.Matches(expected), "same array"); Assert.IsTrue(matcher.Matches(equal), "same contents"); Assert.IsFalse(matcher.Matches(inequal), "different contents"); } [Test] public void CanCompareAutoboxedValues() { Matcher matcher = new EqualMatcher(1); Assert.IsTrue(matcher.Matches(1), "equal value"); Assert.IsFalse(matcher.Matches(2), "other value"); } [Test] public void HasAReadableDescription() { NamedObject value = new NamedObject("value"); AssertDescription.IsEqual(new EqualMatcher(value), "equal to <"+value+">"); } } } --- NEW FILE: OrMatcherTest.cs --- using NUnit.Framework; using NMock2.Matchers; namespace NMock2.Test.Matchers { [TestFixture] public class OrMatcherTest { static readonly object ignored = new object(); static readonly Matcher TRUE = new AlwaysMatcher(true,"TRUE"); static readonly Matcher FALSE = new AlwaysMatcher(false, "FALSE"); static readonly object[,] truthTable = { {FALSE, FALSE, false}, {FALSE, TRUE, true}, {TRUE, FALSE, true}, {TRUE, TRUE, true} }; [Test] public void CalculatesLogicalDisjunctionOfTwoMatchers() { for (int i = 0; i < truthTable.GetLength(0); i++) { Matcher arg1 = (Matcher)truthTable[i,0]; Matcher arg2 = (Matcher)truthTable[i,1]; Matcher matcher = new OrMatcher(arg1, arg2); Assert.AreEqual( truthTable[i,2], matcher.Matches(ignored) ); } } [Test] public void CanUseOperatorOverloadingAsSyntacticSugar() { for (int i = 0; i < truthTable.GetLength(0); i++) { Matcher arg1 = (Matcher)truthTable[i,0]; Matcher arg2 = (Matcher)truthTable[i,1]; Matcher matcher = arg1 | arg2; Assert.AreEqual( truthTable[i,2], matcher.Matches(ignored) ); } } [Test] public void HasAReadableDescription() { Matcher left = new MatcherWithDescription("<left>"); Matcher right = new MatcherWithDescription("<right>"); AssertDescription.IsComposed( new OrMatcher(left,right), "`{0}' or `{1}'", left, right); } } } --- NEW FILE: MethodNameMatcherTest.cs --- (This appears to be a binary file; contents omitted.) --- NEW FILE: NullMatcherTest.cs --- using NUnit.Framework; using NMock2.Matchers; namespace NMock2.Test.Matchers { [TestFixture] public class NullMatcherTest { [Test] public void MatchesNullReferences() { Matcher matcher = new NullMatcher(); Assert.IsTrue( matcher.Matches(null), "null"); Assert.IsFalse( matcher.Matches(new object()), "not null"); } [Test] public void ProvidesAReadableDescription() { AssertDescription.IsEqual(new NullMatcher(), "null"); } } } --- NEW FILE: NotMatcherTest.cs --- using NUnit.Framework; using NMock2.Matchers; namespace NMock2.Test.Matchers { [TestFixture] public class NotMatcherTest { static readonly object ignored = new object(); static readonly Matcher TRUE = new AlwaysMatcher(true,"TRUE"); static readonly Matcher FALSE = new AlwaysMatcher(false, "FALSE"); [Test] public void CalculatesTheLogicalNegationOfAMatcher() { Assert.IsTrue( new NotMatcher(FALSE).Matches(ignored), "not false" ); Assert.IsFalse( new NotMatcher(TRUE).Matches(ignored), "not true" ); } [Test] public void CanUseOperatorOverloadingAsSyntacticSugar() { Assert.IsTrue( (!FALSE).Matches(ignored), "not false" ); Assert.IsFalse( (!TRUE).Matches(ignored), "not true" ); } [Test] public void HasAReadableDescription() { Matcher negated = new MatcherWithDescription("<negated>"); NotMatcher notMatcher = new NotMatcher(negated); AssertDescription.IsComposed(notMatcher, "not {0}", negated ); } } } --- NEW FILE: ArgumentsMatcherTest.cs --- (This appears to be a binary file; contents omitted.) |