private bool InternalTryMatch(Object actual, IMatcher childMatcher, ISelfDescribing selfDescribing) { //so we can print child diagnostics after var childDiag = new MatchDiagnostics(); var matched = childMatcher.Matches(actual, childDiag); Text(matched ? "Match":"Mismatch!"); var desc = Description.With(); if (selfDescribing != null) { desc.Value(selfDescribing); } if (!matched) { Expect.PrintExpectButGot(desc, actual, childMatcher); desc.Value(childDiag); } else { desc.Value(childMatcher); } Child(desc); return(matched); }
/// <summary> /// Performs a type check to ensure the object is of the expected type. /// </summary> /// <param name="actual"></param> /// <param name="diagnostics"></param> /// <returns></returns> public bool Matches(object actual, IMatchDiagnostics diagnostics) { if (actual == null) { switch (m_isNullable) { case Nulls.Allowed: return(Matches((T)actual, diagnostics)); case Nulls.NotAllowed: diagnostics.MisMatched("Expected non null"); return(false); case Nulls.NonNullableType: diagnostics.MisMatched("Wrong type, expected type {0} which is non nullable, but got null instead", typeof(T).FullName); return(false); } } if (!isValidType(actual)) { diagnostics.MisMatched(Description.With() .Text("Wrong type, expected {0} but got {1}", typeof(T).FullName, actual.GetType().FullName) .Value("actual", actual.GetType().FullName) ); return(false); } T val; try { val = wrap(actual); } catch (InvalidCastException e) { throw new InvalidCastException(String.Format("Could not cast {0} to {1}", actual.GetType().FullName, typeof(T).FullName), e); } return(Matches(val, diagnostics)); }
public IMatchDiagnostics MisMatched(string text, params object[] args) { MisMatched(Description.With().Text(text, args)); return(this); }
public bool TryMatch <T>(T actual, String actualName, IMatcher <T> childMatcher) { return(InternalTryMatch(actual, childMatcher, Description.With().Value("named", actualName))); }
public bool TryMatch <T>(T actual, int index, IMatcher <T> childMatcher) { return(InternalTryMatch(actual, childMatcher, Description.With().Value("at position", index))); }