/// <summary> /// Checks that the checked <see cref="IEnumerable"/> contains the expected list of items only once. /// </summary> /// <param name="check"> /// The chained fluent check. /// </param> /// <returns> /// A check link. /// </returns> public static ExtendableCheckLink <ICheck <IEnumerable <T> >, IEnumerable <T> > Once <T>(this ExtendableCheckLink <ICheck <IEnumerable <T> >, IEnumerable <T> > check) { IExtendableCheckLink <ICheck <IEnumerable <T> >, IEnumerable <T> > chainedCheckLink = check; ImplementOnce(ExtensibilityHelper.BeginCheck(chainedCheckLink.AccessCheck), chainedCheckLink.OriginalComparand); return(check); }
/// <summary> /// Checks that the checked <see cref="string"/> contains the expected list of strings in the correct order. /// </summary> /// <param name="chainedCheckLink"> /// The chained fluent check. /// </param> /// <returns> /// A check link. /// </returns> public static IExtendableCheckLink <string, string[]> InThatOrder(this IExtendableCheckLink <string, string[]> chainedCheckLink) { var checker = ExtensibilityHelper.ExtractChecker(chainedCheckLink.And); var value = checker.Value; var comparand = chainedCheckLink.OriginalComparand; var lastIndex = 0; foreach (var text in comparand) { lastIndex = value.IndexOf(text, lastIndex); if (lastIndex < 0) { // failed var message = checker.BuildMessage( "The {0} does not contain the expected strings in the correct order.") .For("string") .On(value) .And.Expected(comparand) .Label("Expected content: "); throw new FluentCheckException(message.ToString()); } } return(chainedCheckLink); }
/// <summary> /// Checks that the checked <see cref="IEnumerable"/> contains only the authorized items. Can only be used after a call to Contains. /// </summary> /// <param name="check"> /// The chained fluent check. /// </param> /// <returns> /// A check link. /// </returns> public static ExtendableCheckLink <ICheck <IEnumerable>, IEnumerable> Only(this ExtendableCheckLink <ICheck <IEnumerable>, IEnumerable> check) { IExtendableCheckLink <ICheck <IEnumerable>, IEnumerable> chainedCheckLink = check; chainedCheckLink.AccessCheck.IsOnlyMadeOf(chainedCheckLink.OriginalComparand); return(check); }
/// <summary> /// Checks that the checked <see cref="IEnumerable"/> contains the expected list of items only once. /// </summary> /// <param name="chainedCheckLink"> /// The chained fluent check. /// </param> /// <returns> /// A check link. /// </returns> public static IExtendableCheckLink <IEnumerable, IEnumerable> Once(this IExtendableCheckLink <IEnumerable, IEnumerable> chainedCheckLink) { ExtensibilityHelper.BeginCheck(chainedCheckLink.And). ComparingTo(chainedCheckLink.OriginalComparand, "once of", ""). CantBeNegated(). Analyze((sut, test) => { var itemIndex = 0; var expectedList = ToNewList(chainedCheckLink); var listedItems = new List <object>(); foreach (var item in sut) { if (expectedList.Contains(item)) { listedItems.Add(item); expectedList.Remove(item); } else if (listedItems.Contains(item)) { test.Fail( $"The {{0}} has extra occurrences of the expected items. Item [{item.ToStringProperlyFormatted().DoubleCurlyBraces()}] at position {itemIndex} is redundant."); return; } itemIndex++; } }).EndCheck(); return(chainedCheckLink); }
/// <summary> /// Checks that the checked <see cref="IEnumerable"/> contains the expected list of items only once. /// </summary> /// <param name="chainedCheckLink"> /// The chained fluent check. /// </param> /// <returns> /// A check link. /// </returns> public static IExtendableCheckLink <IEnumerable, IEnumerable> Once(this IExtendableCheckLink <IEnumerable, IEnumerable> chainedCheckLink) { var checker = ExtensibilityHelper.ExtractChecker(chainedCheckLink.And); var itemIndex = 0; var expectedList = ConvertToList(chainedCheckLink); var listedItems = new List <object>(); Debug.Assert(checker != null, "checker != null"); foreach (var item in checker.Value) { if (expectedList.Contains(item)) { listedItems.Add(item); expectedList.Remove(item); } else if (listedItems.Contains(item)) { // failure, we found one extra occurrence of one item var message = checker.BuildMessage( string.Format( "The {{0}} has extra occurrences of the expected items. Item [{0}] at position {1} is redundant.", item.ToStringProperlyFormatted().DoubleCurlyBraces(), itemIndex)) .ExpectedValues(chainedCheckLink.OriginalComparand); throw new FluentCheckException(message.ToString()); } itemIndex++; } return(chainedCheckLink); }
/// <summary> /// Checks that the checked <see cref="string"/> contains the expected list of strings in the correct order. /// </summary> /// <param name="chainedCheckLink"> /// The chained fluent check. /// </param> /// <returns> /// A check link. /// </returns> public static IExtendableCheckLink <ICheck <string>, string[]> InThatOrder(this ExtendableCheckLink <ICheck <string>, string[]> chainedCheckLink) { IExtendableCheckLink <ICheck <string>, string[]> context = chainedCheckLink; ExtensibilityHelper.BeginCheck(context.AccessCheck). CantBeNegated("InThatOrder"). Analyze((sut, test) => { var lastIndex = 0; foreach (var content in context.OriginalComparand) { lastIndex = sut.IndexOf(content, lastIndex, StringComparison.Ordinal); if (lastIndex >= 0) { continue; } test.Fail("The {0} does not contain the expected strings in the correct order."); return; } }). DefineExpectedValues(context.OriginalComparand, context.OriginalComparand.Length, "in this order", "") .EndCheck(); return(chainedCheckLink); }
/// <summary> /// Checks that the checked <see cref="string"/> contains the expected list of strings only once. /// </summary> /// <param name="chainedCheckLink"> /// The chained fluent check. /// </param> /// <returns> /// A check link. /// </returns> public static IExtendableCheckLink <string, string[]> Once(this IExtendableCheckLink <string, string[]> chainedCheckLink) { var checker = chainedCheckLink.And as ICheckForExtensibility <string, ICheck <string> >; var value = checker.Value; var comparand = chainedCheckLink.OriginalComparand; foreach (var text in comparand) { var firstIndex = value.IndexOf(text); var lastIndexOf = value.LastIndexOf(text); if (firstIndex != lastIndexOf) { // failed var message = FluentMessage.BuildMessage(string.Format("The {{0}} contains {0} at {1} and {2}, where as it must contains it once.", text.ToStringProperlyFormated().DoubleCurlyBraces(), firstIndex, lastIndexOf)) .For("string") .On(value) .And.Expected(comparand) .Label("Expected content once"); throw new FluentCheckException(message.ToString()); } } return(chainedCheckLink); }
/// <summary> /// Checks that the checked <see cref="IEnumerable"/> contains the expected list of items only once. /// </summary> /// <param name="check"> /// The chained fluent check. /// </param> /// <returns> bv /// A check link. /// </returns> public static ExtendableCheckLink <ICheck <IEnumerable>, IEnumerable> Once(this ExtendableCheckLink <ICheck <IEnumerable>, IEnumerable> check) { IExtendableCheckLink <ICheck <IEnumerable>, IEnumerable> chainedCheckLink = check; ImplementOnce( ExtensibilityHelper.BeginCheckAs(chainedCheckLink.AccessCheck, enumerable => enumerable.Cast <object>()), chainedCheckLink.OriginalComparand.Cast <object>()); return(check); }
/// <summary> /// Checks that the checked <see cref="IEnumerable"/> contains items in the expected order. /// </summary> /// <param name="check"> /// The chained fluent check. /// </param> /// <returns> /// A check link. /// </returns> public static IExtendableCheckLink <ICheck <IEnumerable>, IEnumerable> InThatOrder(this IExtendableCheckLink <ICheck <IEnumerable>, IEnumerable> check) { IExtendableCheckLink <ICheck <IEnumerable>, IEnumerable> chainedCheckLink = check; var orderedList = new List <object>(chainedCheckLink.OriginalComparand.Cast <object>()); var checker = ExtensibilityHelper.BeginCheckAs(chainedCheckLink.AccessCheck, enumerable => enumerable.Cast <object>()); ImplementInThatOrder(checker, orderedList); return(check); }
/// <summary> /// Checks that the checked <see cref="IEnumerable"/> contains items in the expected order. /// </summary> /// <param name="check"> /// The chained fluent check. /// </param> /// <typeparam name="T">Enumerated item type.</typeparam> /// <returns> /// A check link. /// </returns> public static IExtendableCheckLink <ICheck <IEnumerable <T> >, IEnumerable <T> > InThatOrder <T>(this IExtendableCheckLink <ICheck <IEnumerable <T> >, IEnumerable <T> > check) { IExtendableCheckLink <ICheck <IEnumerable <T> >, IEnumerable <T> > chainedCheckLink = check; var orderedList = new List <T>(chainedCheckLink.OriginalComparand); var checker = ExtensibilityHelper.BeginCheck(chainedCheckLink.AccessCheck); ImplementInThatOrder(checker, orderedList); return(check); }
private static List <object> ToNewList(IExtendableCheckLink <IEnumerable, IEnumerable> chainedCheckLink) { var orderedList = new List <object>(); foreach (var item in chainedCheckLink.OriginalComparand) { orderedList.Add(item); } return(orderedList); }
/// <summary> /// Checks that the checked <see cref="string"/> contains the expected list of strings only once. /// </summary> /// <param name="chainedCheckLink"> /// The chained fluent check. /// </param> /// <returns> /// A check link. /// </returns> public static IExtendableCheckLink <string, string[]> Once(this IExtendableCheckLink <string, string[]> chainedCheckLink) { ExtensibilityHelper.BeginCheck(chainedCheckLink.And). CantBeNegated(). Analyze((sut, test) => { foreach (var content in chainedCheckLink.OriginalComparand) { var firstIndex = sut.IndexOf(content, StringComparison.Ordinal); var lastIndexOf = sut.LastIndexOf(content, StringComparison.Ordinal); if (firstIndex != lastIndexOf) { test.Fail( $"The {{0}} contains {content.ToStringProperlyFormatted().DoubleCurlyBraces()} at {firstIndex} and {lastIndexOf}, where as it must contains it once."); return; } } }). DefineExpectedValues(chainedCheckLink.OriginalComparand, chainedCheckLink.OriginalComparand.Length, "once", "").EndCheck(); return(chainedCheckLink); }
/// <summary> /// Checks that the checked <see cref="IEnumerable"/> contains items in the expected order. /// </summary> /// <param name="chainedCheckLink"> /// The chained fluent check. /// </param> /// <returns> /// A check link. /// </returns> public static IExtendableCheckLink <IEnumerable, IEnumerable> InThatOrder(this IExtendableCheckLink <IEnumerable, IEnumerable> chainedCheckLink) { ExtensibilityHelper.BeginCheck(chainedCheckLink.And). ComparingTo(chainedCheckLink.OriginalComparand, "in that order", "in another order"). CantBeNegated(). Analyze((sut, test) => { var orderedList = ToNewList(chainedCheckLink); var failingIndex = 0; var scanIndex = 0; foreach (var item in sut) { if (item != orderedList[scanIndex]) { var failed = false; // if current item is part of current list, check order var index = orderedList.IndexOf(item, scanIndex); if (index < 0) { // if not found at the end of the list, try the full list index = orderedList.IndexOf(item); if (index >= 0) { failed = true; } } else { var currentReference = orderedList[scanIndex]; // skip all similar entries in the expected list (tolerance: the checked enumerables may not contains as many instances of one item as expected while (currentReference == orderedList[++scanIndex] && scanIndex < orderedList.Count) { } // check if skipped only similar items if (scanIndex < index) { failed = true; } } test.FailWhen(_ => failed, string.Format( "The {{0}} does not follow to the expected order. Item [{0}] appears too {2} in the list, at index '{1}'.", item.ToStringProperlyFormatted().DoubleCurlyBraces(), failingIndex, index > scanIndex ? "early" : "late")); if (index >= 0) { scanIndex = index; } } failingIndex++; } }).EndCheck(); return(chainedCheckLink); }
/// <summary> /// Checks that the checked <see cref="IEnumerable"/> contains only the authorized items. Can only be used after a call to Contains. /// </summary> /// <param name="chainedCheckLink"> /// The chained fluent check. /// </param> /// <returns> /// A check link. /// </returns> public static IExtendableCheckLink <IEnumerable, IEnumerable> Only(this IExtendableCheckLink <IEnumerable, IEnumerable> chainedCheckLink) { chainedCheckLink.And.IsOnlyMadeOf(chainedCheckLink.OriginalComparand); return(chainedCheckLink); }
private static List<object> ConvertToList(IExtendableCheckLink<IEnumerable, IEnumerable> chainedCheckLink) { var orderedList = new List<object>(); foreach (var item in chainedCheckLink.OriginalComparand) { orderedList.Add(item); } return orderedList; }
/// <summary> /// Checks that the checked <see cref="IEnumerable"/> contains items in the expected order. /// </summary> /// <param name="chainedCheckLink"> /// The chained fluent check. /// </param> /// <returns> /// A check link. /// </returns> public static IExtendableCheckLink <IEnumerable, IEnumerable> InThatOrder(this IExtendableCheckLink <IEnumerable, IEnumerable> chainedCheckLink) { var checker = ExtensibilityHelper.ExtractChecker(chainedCheckLink.And); var orderedList = ConvertToList(chainedCheckLink); var failingIndex = 0; var scanIndex = 0; Debug.Assert(checker != null, "checker != null"); foreach (var item in checker.Value) { if (item != orderedList[scanIndex]) { var failed = false; // if current item is part of current list, check order var index = orderedList.IndexOf(item, scanIndex); if (index < 0) { // if not found at the end of the list, try the full list index = orderedList.IndexOf(item); if (index >= 0) { failed = true; } } else { var currentReference = orderedList[scanIndex]; // skip all similar entries in the expected list (tolerance: the checked enumerables may not contains as many instances of one item as expected while (currentReference == orderedList[++scanIndex] && scanIndex < orderedList.Count) { } // check if skipped only similar items if (scanIndex < index) { failed = true; } } if (failed) { // check failed. Now we have to refine the issue type. // we assume that Contains was executed (imposed by chaining syntax) // the item violating the order is the previous one! var message = checker.BuildMessage( string.Format( "The {{0}} does not follow to the expected order. Item [{0}] appears too {2} in the list, at index '{1}'.", item.ToStringProperlyFormatted().DoubleCurlyBraces(), failingIndex, index > scanIndex ? "early" : "late")) .ExpectedValues(chainedCheckLink.OriginalComparand); throw new FluentCheckException(message.ToString()); } if (index >= 0) { scanIndex = index; } } failingIndex++; } return(chainedCheckLink); }