-
Notifications
You must be signed in to change notification settings - Fork 0
Array assertions
As you'd expect, array assertions deal with the elements in an array. Compare this with property and simple assertions, which deal with entity properties and whole entities, respectively.
Checks whether all elements in the contextual array satisfy the given predicate.
Fluent scope: Preserves/unchanged.
allSatisfy(
predicate: (el: T, i?: number) => boolean
): T extends any[] ? IFluentCore<T> : void;
Assert(1,2,3).allSatisfy(e => e < 4);
Checks whether any elements in the contextual array satisfy the given predicate.
Fluent scope: Preserves/unchanged.
anySatisfy(
predicate: (t: T) => boolean
): T extends any[] ? IFluentCore<T> : void;
Assert(1,2,3).anySatisfy(e => e % 2 === 0);
Verifies that the contextual value (an array) contains elements matching the given assertions.
Note that, unlike with hasProperties, hasElements isn't inherently recursive (though you can explicitly recurse with lambda assertions over individual elements). Any nested arrays are compared with a deep equals. The reason for this is that the semantics of implicit recursion over positional data structures isn't immediately obvious. What does it mean when LocationMode is "startsWith," for example? Should all nested arrays also use "startsWith?" So, we figure its better to keep it simple.
Fluent scope: Preserves/unchanged.
hasElements(
expected: Array<any>,
location: LocationMode = LocationMode.contains,
elMode: MatchMode = MatchMode.normal
): IFluentCore<T>;
Assert(results).hasElements([4,5,6]);
Assert(results).hasElements([(e: number) => e > 3]);
// passes because of 2nd item, not 1st:
Assert(["a-pattern", /a-pattern/])
.hasElements([/a-pattern/], LocationMode.contains, MatchMode.literal);
Assert(results)
.hasElements([a => a.equals(2)], null, MatchMode.asserts);
Assert(results) // or use alias
.hasAsserts([a => a.equals(2)]);
See MatchMode and LocationMode in the Appendix.
Checks whether the contextual array contains at least one element. If so, you can narrow the assertion scope to that first element with the 'that' operator.
Fluent scope: Can be narrowed to the first element (see that).
hasFirst(): T extends any[] | string ? INarrowableFluentCore<T, T[0]> : void;
Assert([1,2,3]).hasFirst();
Assert([]).not.hasFirst();
Assert(["the", "quick", "brown", "fox"])
.hasFirst()
.that.equals("the");
Assert("asdf")
.hasFirst()
.that.equals("a");
Checks whether the contextual array contains at least one element. If so, you can narrow the assertion scope to the last element with the 'that' operator.
Fluent scope: Can be narrowed to the last element (see that).
hasLast(): T extends any[] | string ? INarrowableFluentCore<T, T[0]> : void;
Assert([1,2,3]).hasLast();
Assert([]).not.hasLast();
Assert(["the", "quick", "brown", "fox"])
.hasLast()
.that.equals("fox");
Assert("asdf")
.hasLast()
.that.equals("f");
Checks whether the contextual array has an element at the given, zero-based index. If so, you can narrow the assertion scope to that element with the 'that' operator.
Fluent scope: Can be narrowed to the nth element (see that).
hasNth<N extends number>(n: N):
T extends any[] | string ? INarrowableFluentCore<T, T[N]> : void;
Assert([1,2,3]).hasNth(1);
Assert([]).not.hasNth(1);
Assert(["the", "quick", "brown", "fox"])
.hasNth(2)
.that.equals("brown");
Assert("asdf")
.hasNth(3)
.that.equals("f");