|
| 1 | +/* |
| 2 | +* @license Apache-2.0 |
| 3 | +* |
| 4 | +* Copyright (c) 2020 The Stdlib Authors. |
| 5 | +* |
| 6 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +* you may not use this file except in compliance with the License. |
| 8 | +* You may obtain a copy of the License at |
| 9 | +* |
| 10 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +* |
| 12 | +* Unless required by applicable law or agreed to in writing, software |
| 13 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +* See the License for the specific language governing permissions and |
| 16 | +* limitations under the License. |
| 17 | +*/ |
| 18 | + |
| 19 | +import readWASM = require( './index' ); |
| 20 | + |
| 21 | +const onLoad = ( error: Error | null, file: Uint8Array ) => { |
| 22 | + if ( error || !file ) { |
| 23 | + throw error; |
| 24 | + } |
| 25 | +}; |
| 26 | + |
| 27 | + |
| 28 | +// TESTS // |
| 29 | + |
| 30 | +// The function does not have a return value... |
| 31 | +{ |
| 32 | + readWASM( 'foo.wasm', onLoad ); // $ExpectType void |
| 33 | +} |
| 34 | + |
| 35 | +// The compiler throws an error if the function is provided a first argument which is not a string, buffer, or file descriptor... |
| 36 | +{ |
| 37 | + readWASM( false, onLoad ); // $ExpectError |
| 38 | + readWASM( true, onLoad ); // $ExpectError |
| 39 | + readWASM( null, onLoad ); // $ExpectError |
| 40 | + readWASM( undefined, onLoad ); // $ExpectError |
| 41 | + readWASM( [], onLoad ); // $ExpectError |
| 42 | + readWASM( {}, onLoad ); // $ExpectError |
| 43 | + readWASM( ( x: number ): number => x, onLoad ); // $ExpectError |
| 44 | +} |
| 45 | + |
| 46 | +// The compiler throws an error if the function is provided a callback argument which is not a function with the expected signature... |
| 47 | +{ |
| 48 | + readWASM( '/path/to/foo.wasm', 'abc' ); // $ExpectError |
| 49 | + readWASM( '/path/to/foo.wasm', 1 ); // $ExpectError |
| 50 | + readWASM( '/path/to/foo.wasm', false ); // $ExpectError |
| 51 | + readWASM( '/path/to/foo.wasm', true ); // $ExpectError |
| 52 | + readWASM( '/path/to/foo.wasm', null ); // $ExpectError |
| 53 | + readWASM( '/path/to/foo.wasm', undefined ); // $ExpectError |
| 54 | + readWASM( '/path/to/foo.wasm', [] ); // $ExpectError |
| 55 | + readWASM( '/path/to/foo.wasm', {} ); // $ExpectError |
| 56 | + readWASM( '/path/to/foo.wasm', ( x: number ): number => x ); // $ExpectError |
| 57 | +} |
| 58 | + |
| 59 | +// The compiler throws an error if the function is provided an options argument which is not an object... |
| 60 | +{ |
| 61 | + readWASM( 'foo.wasm', false, onLoad ); // $ExpectError |
| 62 | + readWASM( 'foo.wasm', true, onLoad ); // $ExpectError |
| 63 | + readWASM( 'foo.wasm', null, onLoad ); // $ExpectError |
| 64 | + readWASM( 'foo.wasm', undefined, onLoad ); // $ExpectError |
| 65 | + readWASM( 'foo.wasm', 123, onLoad ); // $ExpectError |
| 66 | + readWASM( 'foo.wasm', [], onLoad ); // $ExpectError |
| 67 | + readWASM( 'foo.wasm', ( x: number ): number => x, onLoad ); // $ExpectError |
| 68 | +} |
| 69 | + |
| 70 | +// The compiler throws an error if the function is provided a `flag` option which is not a string... |
| 71 | +{ |
| 72 | + readWASM( 'foo.wasm', { 'flag': 123 }, onLoad ); // $ExpectError |
| 73 | + readWASM( 'foo.wasm', { 'flag': true }, onLoad ); // $ExpectError |
| 74 | + readWASM( 'foo.wasm', { 'flag': false }, onLoad ); // $ExpectError |
| 75 | + readWASM( 'foo.wasm', { 'flag': null }, onLoad ); // $ExpectError |
| 76 | + readWASM( 'foo.wasm', { 'flag': [] }, onLoad ); // $ExpectError |
| 77 | + readWASM( 'foo.wasm', { 'flag': {} }, onLoad ); // $ExpectError |
| 78 | + readWASM( 'foo.wasm', { 'flag': ( x: number ): number => x }, onLoad ); // $ExpectError |
| 79 | +} |
| 80 | + |
| 81 | +// The compiler throws an error if the function is provided an unsupported number of arguments... |
| 82 | +{ |
| 83 | + readWASM(); // $ExpectError |
| 84 | + readWASM( 'C:\\foo\\bar\\baz\\foo.wasm' ); // $ExpectError |
| 85 | +} |
| 86 | + |
| 87 | +// Attached to main export is a `sync` method which returns file contents as a Uint8Array or an error... |
| 88 | +{ |
| 89 | + readWASM.sync( 'foo.wasm' ); // $ExpectType Error | Uint8Array |
| 90 | +} |
| 91 | + |
| 92 | +// The compiler throws an error if the `sync` method is provided a first argument which is not a string, buffer, or file descriptor... |
| 93 | +{ |
| 94 | + readWASM.sync( false ); // $ExpectError |
| 95 | + readWASM.sync( true ); // $ExpectError |
| 96 | + readWASM.sync( null ); // $ExpectError |
| 97 | + readWASM.sync( undefined ); // $ExpectError |
| 98 | + readWASM.sync( [] ); // $ExpectError |
| 99 | + readWASM.sync( {} ); // $ExpectError |
| 100 | + readWASM.sync( ( x: number ): number => x ); // $ExpectError |
| 101 | +} |
| 102 | + |
| 103 | +// The compiler throws an error if the `sync` method is provided an options argument which is not an object... |
| 104 | +{ |
| 105 | + readWASM.sync( 'foo.wasm', null ); // $ExpectError |
| 106 | + readWASM.sync( 'foo.wasm', true ); // $ExpectError |
| 107 | + readWASM.sync( 'foo.wasm', false ); // $ExpectError |
| 108 | + readWASM.sync( 'foo.wasm', 123 ); // $ExpectError |
| 109 | + readWASM.sync( 'foo.wasm', [] ); // $ExpectError |
| 110 | + readWASM.sync( 'foo.wasm', ( x: number ): number => x ); // $ExpectError |
| 111 | +} |
| 112 | + |
| 113 | +// The compiler throws an error if the `sync` method is provided a `flag` option which is not a string... |
| 114 | +{ |
| 115 | + readWASM.sync( 'foo.wasm', { 'flag': 123 } ); // $ExpectError |
| 116 | + readWASM.sync( 'foo.wasm', { 'flag': true } ); // $ExpectError |
| 117 | + readWASM.sync( 'foo.wasm', { 'flag': false } ); // $ExpectError |
| 118 | + readWASM.sync( 'foo.wasm', { 'flag': null } ); // $ExpectError |
| 119 | + readWASM.sync( 'foo.wasm', { 'flag': [] } ); // $ExpectError |
| 120 | + readWASM.sync( 'foo.wasm', { 'flag': {} } ); // $ExpectError |
| 121 | + readWASM.sync( 'foo.wasm', { 'flag': ( x: number ): number => x } ); // $ExpectError |
| 122 | +} |
| 123 | + |
| 124 | +// The compiler throws an error if the `sync` method is provided an unsupported number of arguments... |
| 125 | +{ |
| 126 | + readWASM.sync(); // $ExpectError |
| 127 | +} |
0 commit comments