/// <summary> /// Mark the scenario as failed if the match failed. If it passed _dont_ mark scenario as having been run /// as this could be called from any step( given, check, when...) /// </summary> private void FailIfNotPassed <T>(Boolean passed, T actual, IMatcher <T> matcher, IMatchDiagnostics diagnostics) { MarkLastStepPassed(passed); if (!passed) { Passed(false); var desc = new Description(); desc.Child("Steps were", StepsToString()); desc.Child("expected", matcher); if (actual is String) { desc.Child("but was", "'" + actual + "'"); } else { desc.Child("but was", actual); } desc.Text("==== Diagnostics ===="); desc.Child(diagnostics); TestFirstAssert.Fail(Environment.NewLine + desc.ToString()); } }
public sealed override bool Matches(T actual, IMatchDiagnostics diagnostics) { if (actual == null) { diagnostics.MisMatched("Expected non null instance"); return(false); } foreach (var matcher in m_pocoPropertyMatchers) { PropertyInfo property = GetPropertyForTypeNamed(actual.GetType(), matcher.PropertyName); if (property == null) { diagnostics.MisMatched( Description.With() .Value("propertyExists", false)); return(false); } var propertyValue = property.GetValue(actual, null); if (!diagnostics.TryMatch(propertyValue, matcher)) { return(false); } } foreach (var matcher in m_pocoMatchers) { if (!diagnostics.TryMatch(actual, matcher)) { return(false); } } return(true); }
public bool Matches(Object actual, IMatchDiagnostics diagnostics) { if (actual == null) { if (m_isNullableType)//the matcher should be able to handle the nulls, let it deal with it. It may match for null { return(diagnostics.TryMatch(null, m_propertyValueMatcher)); } diagnostics.MisMatched(Description.With() .Value("expected type to be nullable") .Value("for type", m_propertyType.FullName) .Value("expected", "not null") ); return(false); } if (m_propertyType.IsInstanceOfType(actual)) { return(diagnostics.TryMatch(actual, m_propertyValueMatcher)); } diagnostics.MisMatched( Description.With() .Value("Incorrect type") .Value("expected type", m_propertyType.FullName) .Value("actual type", actual.GetType().FullName)); return(false); }
internal void AddMisMatchMessage(IMatchDiagnostics diagnostics) { if (MatchersRemain()) { diagnostics.Child("didn't match", m_remainingMatchers); } }
public override bool Matches(IEnumerable actual, IMatchDiagnostics diagnostics) { if (actual == null) { diagnostics.MisMatched("items are null"); return(false); } var list = AsEfficientList(actual); if (m_matchers.Count != list.Count) { diagnostics .Value("num items") .Value("expected", m_matchers.Count) .Value("actual", list.Count); return(false); } for (int i = 0; i < m_matchers.Count; i++) { var matcher = m_matchers[i]; var item = list[i]; if (!diagnostics.TryMatch(item, i, matcher)) { return(false); } } return(true); }
public override bool Matches(DateTimeOffset?actual, IMatchDiagnostics diagnostics) { if (actual == null) { diagnostics.MisMatched("Null"); return(false); } var within = m_expectWithin.TotalMilliseconds + (m_inclusive?1:0);//if not inclusive let's be just a little more if (m_expectAfter.HasValue) { double diffPosIfBefore = (ApplyOffset(m_expectAfter) - actual.Value).TotalMilliseconds; if (diffPosIfBefore >= within) { return(false); } } if (m_expectBefore.HasValue) { double diffPosIfAfter = (actual.Value - ApplyOffset(m_expectBefore)).TotalMilliseconds; if (diffPosIfAfter >= within) { return(false); } } return(true); }
public override bool Matches(IEnumerable actual, IMatchDiagnostics diagnostics) { if (actual == null) { diagnostics.MisMatched("items are null"); return(false); } var list = AsEfficientList(actual); int pos = 0; for (; pos < list.Count; pos++) { var item = list[pos]; foreach (var matcher in m_matchers) { //prevent useless diagnostics messages appearing in output as we call the matchers //repeatably as we try to find a match var childDiag = diagnostics.NewChild(); if (childDiag.TryMatch(item, matcher)) { diagnostics.Value("position", pos); diagnostics.Value("item", item); diagnostics.MisMatched(childDiag); return(false); } } } return(true); }
public override bool Matches(IEnumerable instance, IMatchDiagnostics diag) { var enummerator = instance.GetEnumerator(); int count = 0; while (enummerator.MoveNext()) { count++; } return(diag.TryMatch(count, "count", m_countMatcher)); }
internal void AddMisMatchMessage(IMatchDiagnostics diagnostics, Object lastMatchedItem, int itemPosition, IEnumerable items) { if (HasNextMatcher()) { diagnostics.Text("not all matchers matched, matchers which didn't match were"); diagnostics.Child("matchedUpToItemAtPosition", itemPosition); diagnostics.Child("matchedUpToItem", lastMatchedItem); diagnostics.Child("lastMatchingMatcher", CurrentMatcher()); var remaining = m_matchers.GetRange(m_currentMatcherIdx, m_matchers.Count - m_currentMatcherIdx); diagnostics.Children("remainingMatchers", remaining); diagnostics.Children("items", items); } }
private static bool TryMatch <T>(T actual, IMatcher <T?> matcher, IMatchDiagnostics diagnostics) where T : struct { try { //force newline as diagnostics don't end with one and it messes up the console when interleaved with //log4net to console logging return(diagnostics.TryMatch(actual, matcher)); } finally { diagnostics.Text(""); } }
public override bool Matches(string actual, IMatchDiagnostics diag) { if (m_expectVal.Equals(actual)) { diag.Matched(Description.With().Value("value", actual)); return(true); } else { diag.MisMatched(Description.With().Value("expected", m_expectVal).Value("actual", actual)); return(false); } }
public override bool Matches(IDictionary <string, string> actual, IMatchDiagnostics diag) { bool matches = true; foreach (var key in m_expectedKeyValues) { string actualValue; if (actual.TryGetValue(key.Key, out actualValue)) { matches &= diag.TryMatch(actualValue, "Value for " + key.Key, key.Value); } else { matches &= diag.TryMatch(null, "Value for " + key.Key, key.Value); } } return(matches); }
public override bool Matches(DateTimeOffset?actual, IMatchDiagnostics diagnostics) { if (actual == null && m_expect == null) { diagnostics.Matched("Null"); return(true); } if (actual == null) { diagnostics.MisMatched("Was null"); return(false); } if (m_expect == null) { diagnostics.MisMatched("Expected null"); return(false); } return(diagnostics.TryMatch(actual, GetOrBuild())); }
public bool Matches(object actual, IMatchDiagnostics diag) { if (actual == null) { diag.MisMatched("null instance"); return(false); } if (!m_expectType.IsInstanceOfType(actual)) { diag.MisMatched(diag.NewChild().Text("Wrong type").Value("actualType", actual.GetType().FullName)); return(false); } if (m_matcher != null) { return(diag.TryMatch((T)actual, m_matcher)); } return(true); }
internal bool Matches(System.Object item, int itemPos, IMatchDiagnostics diagnostics) { //collect all the mismatches for later var children = new List <IMatchDiagnostics>(m_remainingMatchers.Count); foreach (var matcher in m_remainingMatchers) { //want to keep non matchign matchers clear var childDiag = diagnostics.NewChild(); if (childDiag.TryMatch(item, matcher)) { diagnostics.Child(childDiag); m_remainingMatchers.Remove(matcher); return(true); } children.Add(childDiag); } //lets print all the mis matches diagnostics.Children(children); return(false); }
public override bool Matches(T[] actual, IMatchDiagnostics diag) { for (int i = 0; i < m_expect.Length || i < actual.Length; i++) { if (i >= m_expect.Length) { diag.MisMatched("mismatched at [{0}], expected end of array, but got [{1}],\nlengths {2},\nexpect end\nbut got\n{3}" , i , ToPrettyVal(actual[i]) , LengthDiffMessage(actual, m_expect) , GetSegmentWithOffsetAndLength(actual, i, 30) ); return(false); } if (i >= actual.Length) { diag.MisMatched("mismatched at [{0}], expected [{1}], but got end, of array\nlengths {2},\nexpect \n{3}\nbut got end of array" , i , ToPrettyVal(m_expect[i]) , LengthDiffMessage(actual, m_expect) , GetSegmentWithOffsetAndLength(m_expect, i, 30) ); return(false); } if (!m_equalMatcher.Invoke(m_expect[i], actual[i])) { diag.MisMatched("mismatched at [{0}], expected {1}, but got [{2}], lengths {3},\nexpect\n{4}\nbut got\n{5}" , i , ToPrettyVal(m_expect[i]) , ToPrettyVal(actual[i]) , LengthDiffMessage(actual, m_expect) , GetSegmentWithOffsetAndLength(m_expect, i, 30) , GetSegmentWithOffsetAndLength(actual, i, 30) ); return(false); } } return(true); }
public override bool Matches(IEnumerable actual, IMatchDiagnostics diagnostics) { if (actual == null) { diagnostics.MisMatched(Description.With().Value("items are null").Value("expected", "not null")); return(false); } var items = AsEfficientList(actual); var matcher = new MatchContext(m_matchers); if (!matcher.CanStillMatch()) { //no matchers so all ok return(true); } matcher.NextMatcher(); Object lastMatchedItem = default(T); int lastMatchedItemIdx = -1; for (int i = 0; i < items.Count && matcher.CanStillMatch(); i++) { var item = items[i]; if (matcher.Matches(item, i, diagnostics)) { lastMatchedItem = item; lastMatchedItemIdx = i; matcher.NextMatcher(); } } //check all matchers matched if (matcher.CanStillMatch()) { matcher.AddMisMatchMessage(diagnostics, lastMatchedItem, lastMatchedItemIdx, items); return(false); } return(true); }
/// <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)); }
/// <summary> /// Return a Moq version of this matcher which also logs to the given match diagnostics. /// See <see cref="MatcherMoqExtensions"/> for usage /// </summary> public static T VerifyLogTo <T>(this IMatcher <T?> matcher, IMatchDiagnostics diagnostics) where T : struct { return(It.Is(matcher.ToMoqExpression(diagnostics))); }
private static Expression <Func <T, bool> > ToMoqExpression <T>(this IMatcher <T?> matcher, IMatchDiagnostics diagnostics) where T : struct { return((T actual) => TryMatch(actual, matcher, diagnostics)); }
internal bool Matches(Object item, int itemPos, IMatchDiagnostics diagnostics) { var matcher = CurrentMatcher(); return(diagnostics.TryMatch(item, itemPos, matcher)); }
public override bool Matches(IEnumerable actual, IMatchDiagnostics diagnostics) { if (actual == null) { diagnostics.MisMatched("items are null"); return(false); } var list = AsEfficientList(actual); var passed = true; var ctxt = new MatchContext(m_matchers); int pos = 0; for (; pos < list.Count; pos++) { var item = list[pos]; var matchPassed = false; if (ctxt.MatchersRemain()) { if (m_failOnAdditionalItems) { matchPassed = ctxt.Matches(item, pos, diagnostics); } else { //prevent useless diagnostics messages appearing in output as we call the matchers //repeatably as we try to find a match var childDiag = diagnostics.NewChild(); matchPassed = ctxt.Matches(item, pos, childDiag); if (matchPassed) { diagnostics.Value(childDiag); } } } if (!matchPassed) { if (m_failOnAdditionalItems) { passed = false; } } if (!passed) { var child = diagnostics.NewChild(); child.Value("position", pos); child.Value("item", item); diagnostics.MisMatched(child); break; } } if (ctxt.MatchersRemain()) { passed = false; } if (!passed) { if (!ctxt.MatchersRemain()) { diagnostics.Text("All matchers matched"); } ctxt.AddMisMatchMessage(diagnostics); if (pos < list.Count) { diagnostics.Child("Additional items not matched", SubSet(pos, list)); } } return(passed); }
/// <summary> /// Sub classes must override this /// </summary> public abstract bool Matches(T actual, IMatchDiagnostics diag);
public override bool Matches(T actual, IMatchDiagnostics diag) { return(diag.TryMatch(actual, "MyField", m_childMatcher)); }
public abstract bool Matches(IEnumerable actual, IMatchDiagnostics diagnostics);
/// <summary> /// If passed, then mark scenario as having been run. Should only be called from 'Thens' /// </summary> private void ThenAssertMatchPassed <T>(Boolean passed, T actual, IMatcher <T> matcher, IMatchDiagnostics diagnostics) { Passed(passed); FailIfNotPassed(passed, actual, matcher, diagnostics); }
public override bool Matches(T instance, IMatchDiagnostics diag) { return(m_matchers.Any(matcher => matcher.Matches(instance, diag))); }
public override bool Matches(TActual instance, IMatchDiagnostics diag) { return(m_matchFunc.Invoke(instance, diag)); }
public override bool Matches(T actual, IMatchDiagnostics diag) { return(!m_matcher.Matches(actual, diag)); }
public override bool Matches(IEnumerable <T> actual, IMatchDiagnostics diagnostics) { return(Matches(actual as IEnumerable, diagnostics)); }