Ensure Functions
Flowata provides a suite of functions designed especially for validating formulas or expressions, making them invaluable tools for unit testing. Rather than simply returning error objects or success indicators, these functions throw errors when the conditions aren't ensured. When the conditions are met, they operate silently, ensuring that your tests are robust and only raise alarms when necessary.
ensure(actual, expected) or ensureEqual(actual, expected)
Ensures that the actual
value equals the expected
value. Throws an error if they're different.
Example:
ensure(5 + 5, 10);
ensureEqual(5 + 5, 10);
ensureTrue(value)
Ensures that the given value
is true
. Throws an error if not.
Example:
ensureTrue(5 > 3);
ensureFalse(value)
Ensures that the given value
is false
. Throws an error if not.
Example:
ensureFalse(3 < 2);
ensureToMatch(inputValue, compareWith)
This function takes an inputValue
and compares it with the compareWith
value, checking the values separately instead of comparing the references. If you want to ensure the type but allow any value, you can use matchType('number')
.
Examples:
ensureToMatch(5, 5); // Ensures that the values match.
ensureToMatch('hello', 'world'); // Throws an error because the values do not match.
ensureToMatch('5', matchType('string')); // Ensures that the type matches but allows any value.
ensureToMatch(123, matchType('number')); // Ensures that the type matches but allows any numeric value.
Using ensureToMatch
with Arrays:
ensureToMatch([1, 2, 3], [1, 2, 3]); // Ensures that the arrays match.
ensureToMatch([1, 2, 3], [3, 2, 1]); // Throws an error because the arrays do not match in order.
const expectedArray = matchType('array');
ensureToMatch([1, 2, 3], expectedArray); // Ensures that the type matches but allows any array value.
ensureToMatch(['apple', 'banana'], expectedArray); // Ensures that the type matches but allows any array value.
Using ensureToMatch
with Objects:
ensureToMatch({ name: 'Alice', age: 30 }, { name: 'Alice', age: 30 }); // Ensures that the objects match.
ensureToMatch({ name: 'Alice', age: 30 }, { age: 30, name: 'Alice' }); // Throws an error because the objects do not match in property order.
const expectedObject = matchType('object');
ensureToMatch({ color: 'red', shape: 'circle' }, expectedObject); // Ensures that the type matches but allows any object value.
ensureToMatch({ city: 'New York', population: 1000000 }, expectedObject); // Ensures that the type matches but allows any object value.
Using ensureToMatch
with Arrays for a Specific Index:
const expectedArrayWithSpecificValue = [matchType('number'), 2, 3];
ensureToMatch([1, 2, 3], expectedArrayWithSpecificValue); // Ensures that the first value is a number and the rest match.
ensureToMatch(['abc', 2, 3], expectedArrayWithSpecificValue); // This would throw an error because the array does not match
ensureToMatch(['apple', 'banana'], [matchType('string'), 'banana']); // Ensures that the first value is a string and the second matches 'banana'.
Using ensureToMatch
with Objects for a Specific Key:
const expectedObjectWithSpecificValue = { name: 'Alice', age: matchType('number') };
ensureToMatch({ name: 'Alice', age: 30 }, expectedObjectWithSpecificValue); // Ensures that the 'age' property is a number and the 'name' property matches 'Alice'.
ensureToMatch({ color: 'red', shape: 'circle' }, { shape: 'circle', color: matchType('string') }); // Ensures that the 'color' property is a string and the 'shape' property matches 'circle'.
In these examples, ensureToMatch
is used to validate that a specific index in an array or a key in an object matches a certain type or value. This allows you to specify detailed expectations for your data structures.