/// <summary> /// Expect the matcher to fail on the given actual /// </summary> protected void AssertFails <T>(T actual, IMatcher <T> matcher) { var diagnostics = new MatchDiagnostics(); if (diagnostics.TryMatch(actual, matcher)) { Assert.Fail("Matcher was expected to fail but didn't for:\n" + actual.ToPrettyString() + "\nexpected:\n" + diagnostics); } }
/// <summary> /// Convenience method to assert an instance matches the given matcher /// </summary> /// <param name="matcher">the matcher to invoke</param> /// <param name="actual">the instance to pass to the matcher</param> public static void AssertMatch(this IMatcher matcher, Object actual) { var diag = new MatchDiagnostics(); if (!diag.TryMatch(actual, matcher)) { TestFirstAssert.Fail(Environment.NewLine + diag); } }
/// <summary> /// Expect the matcher to pass on the given actual /// </summary> protected void AssertPasses <T>(T actual, IMatcher <T> matcher) { var diagnostics = new MatchDiagnostics(); if (!diagnostics.TryMatch(actual, matcher)) { Assert.Fail("Matcher was expected to pass but didn't.\n" + diagnostics); } // Console.WriteLine(diagnostics.ToString()); }
public void TryMatchPassTest() { var diag = new MatchDiagnostics(); var matcher = new MyMatcher("MyValue"); var matched = diag.TryMatch("MyValue", "MyProperty", matcher); Assert.IsTrue(matched, "Expected matcher to match"); var expect = new StringBuilder(); expect.AppendLine("Match"); expect.AppendLine(Indent + "named:MyProperty"); expect.AppendLine(Indent + "Equals:MyValue"); AssertEquals(expect.ToString(), diag.ToString()); }
/// <summary> /// Expect the matcher to fail on the given actual. Tests the failure message /// </summary> protected void AssertFails <T>(T actual, IMatcher <T> matcher, IMatcher <String> messageMatcher) { var diagnostics = new MatchDiagnostics(); if (diagnostics.TryMatch(actual, matcher)) { Assert.Fail("Matcher was expected to fail but didn't for:\n" + actual.ToPrettyString() + "\nexpected:\n" + diagnostics); } else { String msg = matcher.ToString(); if (!messageMatcher.Matches(msg)) { Assert.Fail("Expected diagnostics failure message string match:\n" + messageMatcher + "\n but got '" + msg + "'"); } } }
public void AppendMatchedFieldTest() { var diag = new MatchDiagnostics() .Matched(Description.With().Value("on field", "field1").Value("actual", "fieldValue1")) .Matched(Description.With().Value("on field", "field2").Value("actual", "fieldValue2")) .ToString(); var expect = new StringBuilder(); expect.AppendLine("Match"); expect.AppendLine(Indent + "on field:field1"); expect.AppendLine(Indent + "actual:fieldValue1"); expect.AppendLine("Match"); expect.AppendLine(Indent + "on field:field2"); expect.AppendLine(Indent + "actual:fieldValue2"); AssertEquals(expect.ToString(), diag); }
public void TryMatchFailTest() { var diag = new MatchDiagnostics(); var matcher = new MyMatcher("MyValue"); var matched = diag.TryMatch("MyWrongValue", "MyProperty", matcher); Assert.IsFalse(matched, "Expected matcher to _not_ match"); var expect = new StringBuilder(); expect.AppendLine("Mismatch!"); expect.AppendLine(Indent + "named:MyProperty"); expect.AppendLine(Indent + "matcherType:" + matcher.GetType()); expect.AppendLine(Indent + "expected:"); expect.AppendLine(Indent + Indent + "Equals:MyValue"); expect.AppendLine(Indent + "but was (string,length 12,quoted):"); expect.AppendLine(Indent + Indent + "'MyWrongValue'"); expect.AppendLine(Indent + "Mismatch!"); expect.AppendLine(Indent + Indent + "expected:MyValue"); expect.AppendLine(Indent + Indent + "actual:MyWrongValue"); AssertEquals(expect.ToString(), diag.ToString()); }