@@ -3,6 +3,12 @@ Object.defineProperty(exports, "__esModule", { value: true });
33const util_inspect_1 = require ( "./util-inspect" ) ;
44//A number of useful assertion functions
55//Used in tests and for validations in production
6+ /**
7+ * Throws an error if the given value is not an instance
8+ * of any of the provided constructors
9+ * @param instance The value in question
10+ * @param constructors A constructor or array of constructors to test against
11+ */
612function instanceOf ( instance , constructors ) {
713 if ( ! ( constructors instanceof Array ) )
814 constructors = [ constructors ] ;
@@ -23,12 +29,25 @@ function instanceOf(instance, constructors) {
2329 . join ( ' or ' ) ) ;
2430 }
2531}
32+ /**
33+ * Throws an error if the given value is not an integer
34+ * within the range of integers representable in JavaScript
35+ * @param instance The value in question
36+ */
2637function integer ( instance ) {
2738 instanceOf ( instance , Number ) ;
2839 if ( ! Number . isSafeInteger ( instance ) ) {
2940 throw new RangeError ( util_inspect_1 . inspect ( instance ) + ' is not an integer' ) ;
3041 }
3142}
43+ /**
44+ * Throws an error if a numeric value is not between
45+ * the given bounds
46+ * @param lower The lower bound (inclusive)
47+ * @param value The value in question
48+ * @param upper The upper bound (exclusive)
49+ * @param message An optional message to include in the error message
50+ */
3251function between ( lower , value , upper , message ) {
3352 if ( value < lower || value >= upper ) {
3453 const outOfBoundsMessage = util_inspect_1 . inspect ( value ) +
@@ -43,18 +62,38 @@ function between(lower, value, upper, message) {
4362 throw new RangeError ( outOfBoundsMessage ) ;
4463 }
4564}
65+ /**
66+ * Throws an error if the given value is not an integer
67+ * and in the range that can be represented in an unsigned byte
68+ * @param value The value in question
69+ */
4670function byteUnsignedInteger ( value ) {
4771 integer ( value ) ;
4872 between ( 0 , value , 256 ) ;
4973}
74+ /**
75+ * Throws an error with the specified message
76+ * @param message The message of the thrown error
77+ */
5078function fail ( message ) {
5179 throw new Error ( message ) ;
5280}
53- //Assert that a condition is met if not, throw an error with the specified message
81+ /**
82+ * Throws an error if the provided condition is false,
83+ * using the given error message.
84+ * This function is exported by this module.
85+ */
5486function assert ( condition , message ) {
5587 if ( ! condition )
5688 fail ( message || 'Assertion failed' ) ;
5789}
90+ /**
91+ * Throws an error if the given function does not throw
92+ * an error when executed.
93+ * Can also provide a message string given to [[errorMessage]].
94+ * @param block A function that should throw an error
95+ * @param message The optional message string to match against
96+ */
5897function throws ( block , message ) {
5998 let success = true ;
6099 try {
@@ -67,6 +106,24 @@ function throws(block, message) {
67106 }
68107 assert ( success , message ? 'Was expecting error: ' + message : 'Should throw an error' ) ;
69108}
109+ /**
110+ * Throws an error if the provided values are not "equal".
111+ * This has different meanings for different types of expected values:
112+ * - If `expected` is an `Object`,
113+ * `actual[key]` should "equal" `expected[key]` for each `key` in `expected`
114+ * - If `expected` is an `Array`, the lengths should match
115+ * and corresponding elements should be "equal"
116+ * - If `expected` is a `Map`, the sizes should match and iterating
117+ * should yield "equal" corresponding keys and values
118+ * - If `expected` is a `Set`, `Array.from(actual)` should "equal" `Array.from(expected)`
119+ * - If `expected` is an `ArrayBuffer`, the lengths should match and corresponding
120+ * bytes should be "equal"
121+ * - If `expected` is a `Function`, the names should be equal
122+ * - If `expected` has an `equals()` method, that is used
123+ * - Otherwise, `===` is used
124+ * @param actual
125+ * @param expected
126+ */
70127function equal ( actual , expected ) {
71128 const error = ( ) => new RangeError ( 'Expected ' + util_inspect_1 . inspect ( expected ) + ' but got ' + util_inspect_1 . inspect ( actual ) ) ;
72129 if ( expected ) {
@@ -205,27 +262,24 @@ function equal(actual, expected) {
205262 throw error ( ) ;
206263 }
207264}
265+ /**
266+ * Throws an error if the given value is not an error
267+ * or its message doesn't start with the given message string
268+ * @param err The error value to test
269+ * @param message The string that the error message should start with
270+ */
208271function errorMessage ( err , message ) {
209272 instanceOf ( message , String ) ;
210273 assert ( err !== null && err . message . startsWith ( message ) , 'Message "' + ( err ? err . message : 'No error thrown' ) + '" does not start with "' + message + '"' ) ;
211274}
212275//tslint:disable-next-line:prefer-object-spread
213276exports . default = Object . assign ( assert , {
214- //Assert that the instance is an instance of the constructor, or at least one of the constructors, or a subclass
215277 instanceOf,
216- //Assert that a number is an integer (within the +/-2^53 that can be represented precisely in a double)
217278 integer,
218- //Assert that a number is between the specified values, with an optional message
219279 between,
220- //Assert that a number fits in an unsigned byte
221280 byteUnsignedInteger,
222- //Throw an error
223281 fail,
224- //Assert that the execution of a function throws an error, and that the error message matches the specified one
225282 throws,
226- //Assert that two values are "equal"
227- //What this means depends a lot on the type of the expected value
228283 equal,
229- //Assert that an error's message begins with the specified text
230284 errorMessage
231285} ) ;
0 commit comments