/// <summary> /// Checks that the enumerable has a single element, and returns a check on that element. /// </summary> /// <typeparam name="T">The element type of the collection.</typeparam> /// <param name="check">The fluent check to be extended.</param> /// <returns>A check on the single element.</returns> public static ICheckLinkWhich <ICheck <IEnumerable <T> >, ICheck <T> > HasOneElementOnly <T>( this ICheck <IEnumerable <T> > check) { var item = default(T); ExtensibilityHelper.BeginCheck(check) .FailIfNull("The {0} is null, whereas it must have one element.") .Analyze((sut, test) => { using (var enumerator = sut.GetEnumerator()) { if (!enumerator.MoveNext()) { test.Fail("The {0} is empty, whereas it must have one element."); return; } item = enumerator.Current; if (enumerator.MoveNext()) { test.Fail("The {0} contains more than one element, whereas it must have one element only."); } } }) .OnNegate("The {0} has exactly one element, whereas it should not.") .EndCheck(); return(ExtensibilityHelper.BuildCheckLinkWhich(check, item, "single element")); }
/// <summary> /// Checks that the given enumerable does contain all items matching a predicate. /// </summary> /// <typeparam name="T">Type of items.</typeparam> /// <param name="check">Check item.</param> /// <param name="predicate">Predicate to evaluate.</param> /// <returns>A linkable check.</returns> public static ICheckLinkWhich <ICheck <IEnumerable <T> >, ICheck <T> > ContainsOnlyElementsThatMatch <T>( this ICheck <IEnumerable <T> > check, Func <T, bool> predicate) { var item = default(T); string label = null; ExtensibilityHelper.BeginCheck(check). FailIfNull(). Analyze((sut, test) => { var index = 0; using (var scan = sut.GetEnumerator()) { while (scan.MoveNext()) { if (predicate(scan.Current)) { index++; continue; } test.Fail($"The {{0}} does contain an element at index #{index} that does not match the given predicate: ({scan.Current.ToStringProperlyFormatted().DoubleCurlyBraces()})."); item = scan.Current; label = $"element #{index}"; return; } label = "default element"; } }). OnNegate("The {0} contains only element(s) that match the given predicate, whereas it must not."). EndCheck(); return(ExtensibilityHelper.BuildCheckLinkWhich(check, item, label)); }
/// <inheritdoc /> public ICheckLinkWhich <ICheck <T>, ICheck <TU> > IsInstanceOf <TU>() { this.IsInstanceOfType(typeof(TU)); // TODO: restore pattern matching version when appveyor is upgraded if (Value is TU) { return(ExtensibilityHelper.BuildCheckLinkWhich(this, (TU)(object)Value, SutName)); } return(ExtensibilityHelper.BuildCheckLinkWhich(this, default(TU), SutName)); }
/// <summary> /// Checks that the enumerable has a last element, and returns a check on that element. /// </summary> /// <typeparam name="T">The element type of the collection.</typeparam> /// <param name="check">The fluent check to be extended.</param> /// <returns>A check on the last element.</returns> public static ICheckLinkWhich <ICheck <IEnumerable <T> >, ICheck <T> > HasLastElement <T>( this ICheck <IEnumerable <T> > check) { var item = default(T); ExtensibilityHelper.BeginCheck(check) .FailIfNull("The {0} is null, whereas it must have a last element.") .FailWhen(sut => !TryGetLastElement(sut, out item), "The {0} is empty, whereas it must have a last element.", MessageOption.NoCheckedBlock) .OnNegate("The {0} has a last element, whereas it must be empty.") .EndCheck(); return(ExtensibilityHelper.BuildCheckLinkWhich(check, item, "Last element")); }
/// <summary> /// Check a specific item in the <see cref="IEnumerable" />. /// </summary> /// <typeparam name="T"> /// Enumerated type. /// </typeparam> /// <param name="check"> /// The checker. /// </param> /// <param name="index"> /// Index to check. /// </param> /// <returns> /// An extensible checker. /// </returns> public static ICheckLinkWhich <ICheck <IEnumerable <T> >, ICheck <T> > HasElementAt <T>( this ICheck <IEnumerable <T> > check, int index) { var item = default(T); ExtensibilityHelper.BeginCheck(check) .FailIfNull($"The {{0}} is null, whereas it must have an element with number {index}.") .FailWhen(sut => !TryGetElementByNumber(sut, index, out item), $"The {{0}} does not have an element at index {index}.") .OnNegate($"The {{0}} does have an element at index {index} whereas it should not.") .EndCheck(); return(ExtensibilityHelper.BuildCheckLinkWhich(check, item, $"element #{index}")); }
/// <summary> /// Checks that the nullable sut has a value. /// </summary> /// <typeparam name="T">value type</typeparam> /// <param name="check">fluent check</param> /// <returns>A check link offering <see cref="ICheckLinkWhich{TMain,TSub}.Which"/> to access the value.</returns> public static ICheckLinkWhich <ICheck <T?>, ICheck <T> > HasAValue <T>(this ICheck <T?> check) where T : struct { T?val = null; ExtensibilityHelper.BeginCheck(check).FailWhen(sut => { if (!sut.HasValue) { return(true); } val = sut.Value; return(false); }, "The {0} has no value, which is unexpected.", MessageOption.NoCheckedBlock) .OnNegate("The {0} has a value, whereas it must not.").EndCheck(); return(ExtensibilityHelper.BuildCheckLinkWhich(check, val ?? default, "value", val.HasValue)); }
/// <summary> /// Checks that the given enumerable does contain at least one item matching a predicate. /// </summary> /// <typeparam name="T">Type of items.</typeparam> /// <param name="check">Check item.</param> /// <param name="predicate">Predicate to evaluate.</param> /// <returns>A linkable check.</returns> public static ICheckLinkWhich <ICheck <IEnumerable <T> >, ICheck <T> > HasElementThatMatches <T>( this ICheck <IEnumerable <T> > check, Func <T, bool> predicate) { var item = default(T); var label = string.Empty; ExtensibilityHelper.BeginCheck(check). FailsIfNull(). Analyze((sut, test) => { long?index = null; using (var scan = sut.GetEnumerator()) { item = scan.Current; for (var i = 0; scan.MoveNext(); i++) { if (!predicate(scan.Current)) { continue; } index = i; item = scan.Current; break; } if (!index.HasValue) { test.Fails("The {0} does not contain any element that matches the given predicate."); return; } label = $"element #{index}"; } }). Negates("The {0} contains element(s) that matches the given predicate, whereas it must not."). EndCheck(); return(ExtensibilityHelper.BuildCheckLinkWhich(check, item, label)); }
/// <inheritdoc /> public ICheckLinkWhich <ICheck <T>, ICheck <TU> > IsInstanceOf <TU>() { this.IsInstanceOfType(typeof(TU)); return(ExtensibilityHelper.BuildCheckLinkWhich(this, Value is TU ? (TU)(object)Value : default(TU), SutName)); }