-
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)]);Only requires that the items in the expected array exist within the contextual value with no regard to relative position.
Requires that the contextual value begin with the expected elements, in order.
Requires that the contextual value end with the expected elements, in order.
Requires that the contextual value contain the expected values as a subsequence.
For any lambda property assertions, wrap the actual value in an Assert and pass that in as the first parameter. Nevertheless, if the return value of the lambda is a strict (===) true or false, then true is pass, false is fail. Other property assertion types function as normal.
Forces literal interpretation of property values, even if they are lambdas or regular expressions. Similar to deepEquals, but will only check defined properties, rather than failing when one is missing.
Tries to interpret assertion properties as a person might. If the property is a:
- Lambda - then either assert, or use the boolean return value to determine pass/fail (true/false).
- RegExp - then, if the actual value is a string, check for a match. If its a regular expression, perform a strict equals (===) on their string representations. Otherwise, throw.
- all else - perform a deep, strict equals.
A lambda assertion should be of the form
(actualValue: any) => boolean | IFluentNode | void;where actualValue is the current property's value, and IFluentNode is the return value of an assertion (lets the framework know you didn't intend a boolean assertion).
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(["the", "quick", "brown", "fox"])
.hasNth(2)
.that.equals("brown");
Assert("asdf").hasNth(3).that.equals("f");