|
| 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 readFile = require( './index' ); |
| 20 | + |
| 21 | +const onLoad = ( error: Error | null, file: string | Buffer ) => { |
| 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 | + readFile( 'beepboop', 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 | + readFile( false, onLoad ); // $ExpectError |
| 38 | + readFile( true, onLoad ); // $ExpectError |
| 39 | + readFile( null, onLoad ); // $ExpectError |
| 40 | + readFile( undefined, onLoad ); // $ExpectError |
| 41 | + readFile( [], onLoad ); // $ExpectError |
| 42 | + readFile( {}, onLoad ); // $ExpectError |
| 43 | + readFile( ( 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 | + readFile( '/path/to/beepboop', 'abc' ); // $ExpectError |
| 49 | + readFile( '/path/to/beepboop', 1 ); // $ExpectError |
| 50 | + readFile( '/path/to/beepboop', false ); // $ExpectError |
| 51 | + readFile( '/path/to/beepboop', true ); // $ExpectError |
| 52 | + readFile( '/path/to/beepboop', null ); // $ExpectError |
| 53 | + readFile( '/path/to/beepboop', undefined ); // $ExpectError |
| 54 | + readFile( '/path/to/beepboop', [] ); // $ExpectError |
| 55 | + readFile( '/path/to/beepboop', {} ); // $ExpectError |
| 56 | + readFile( '/path/to/beepboop', ( 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 or string... |
| 60 | +{ |
| 61 | + readFile( 'beepboop', false, onLoad ); // $ExpectError |
| 62 | + readFile( 'beepboop', true, onLoad ); // $ExpectError |
| 63 | + readFile( 'beepboop', null, onLoad ); // $ExpectError |
| 64 | + readFile( 'beepboop', undefined, onLoad ); // $ExpectError |
| 65 | + readFile( 'beepboop', 123, onLoad ); // $ExpectError |
| 66 | + readFile( 'beepboop', [], onLoad ); // $ExpectError |
| 67 | + readFile( 'beepboop', ( x: number ): number => x, onLoad ); // $ExpectError |
| 68 | +} |
| 69 | + |
| 70 | +// The compiler throws an error if the function is provided an `encoding` option which is not a string or null... |
| 71 | +{ |
| 72 | + readFile( 'beepboop', { 'encoding': 123 }, onLoad ); // $ExpectError |
| 73 | + readFile( 'beepboop', { 'encoding': true }, onLoad ); // $ExpectError |
| 74 | + readFile( 'beepboop', { 'encoding': false }, onLoad ); // $ExpectError |
| 75 | + readFile( 'beepboop', { 'encoding': [] }, onLoad ); // $ExpectError |
| 76 | + readFile( 'beepboop', { 'encoding': {} }, onLoad ); // $ExpectError |
| 77 | + readFile( 'beepboop', { 'encoding': ( x: number ): number => x }, onLoad ); // $ExpectError |
| 78 | +} |
| 79 | + |
| 80 | +// The compiler throws an error if the function is provided a `flag` option which is not a string... |
| 81 | +{ |
| 82 | + readFile( 'beepboop', { 'flag': 123 }, onLoad ); // $ExpectError |
| 83 | + readFile( 'beepboop', { 'flag': true }, onLoad ); // $ExpectError |
| 84 | + readFile( 'beepboop', { 'flag': false }, onLoad ); // $ExpectError |
| 85 | + readFile( 'beepboop', { 'flag': null }, onLoad ); // $ExpectError |
| 86 | + readFile( 'beepboop', { 'flag': [] }, onLoad ); // $ExpectError |
| 87 | + readFile( 'beepboop', { 'flag': {} }, onLoad ); // $ExpectError |
| 88 | + readFile( 'beepboop', { 'flag': ( x: number ): number => x }, onLoad ); // $ExpectError |
| 89 | +} |
| 90 | + |
| 91 | +// The compiler throws an error if the function is provided an unsupported number of arguments... |
| 92 | +{ |
| 93 | + readFile(); // $ExpectError |
| 94 | + readFile( 'C:\\foo\\bar\\baz\\beepboop' ); // $ExpectError |
| 95 | +} |
| 96 | + |
| 97 | +// Attached to main export is a `sync` method which returns a string or an error... |
| 98 | +{ |
| 99 | + readFile.sync( 'beepboop' ); // $ExpectType string | Buffer | Error |
| 100 | +} |
| 101 | + |
| 102 | +// The compiler throws an error if the `sync` method is provided a first argument which is not a string, buffer, or file descriptor... |
| 103 | +{ |
| 104 | + readFile.sync( false ); // $ExpectError |
| 105 | + readFile.sync( true ); // $ExpectError |
| 106 | + readFile.sync( null ); // $ExpectError |
| 107 | + readFile.sync( undefined ); // $ExpectError |
| 108 | + readFile.sync( [] ); // $ExpectError |
| 109 | + readFile.sync( {} ); // $ExpectError |
| 110 | + readFile.sync( ( x: number ): number => x ); // $ExpectError |
| 111 | +} |
| 112 | + |
| 113 | +// The compiler throws an error if the `sync` method is provided an options argument which is not an object or string... |
| 114 | +{ |
| 115 | + readFile.sync( 'beepboop', null ); // $ExpectError |
| 116 | + readFile.sync( 'beepboop', true ); // $ExpectError |
| 117 | + readFile.sync( 'beepboop', false ); // $ExpectError |
| 118 | + readFile.sync( 'beepboop', 123 ); // $ExpectError |
| 119 | + readFile.sync( 'beepboop', [] ); // $ExpectError |
| 120 | + readFile.sync( 'beepboop', ( x: number ): number => x ); // $ExpectError |
| 121 | +} |
| 122 | + |
| 123 | +// The compiler throws an error if the `sync` method is provided an `encoding` option which is not a string or null... |
| 124 | +{ |
| 125 | + readFile.sync( 'beepboop', { 'encoding': 123 } ); // $ExpectError |
| 126 | + readFile.sync( 'beepboop', { 'encoding': true } ); // $ExpectError |
| 127 | + readFile.sync( 'beepboop', { 'encoding': false } ); // $ExpectError |
| 128 | + readFile.sync( 'beepboop', { 'encoding': [] } ); // $ExpectError |
| 129 | + readFile.sync( 'beepboop', { 'encoding': {} } ); // $ExpectError |
| 130 | + readFile.sync( 'beepboop', { 'encoding': ( x: number ): number => x } ); // $ExpectError |
| 131 | +} |
| 132 | + |
| 133 | +// The compiler throws an error if the `sync` method is provided a `flag` option which is not a string... |
| 134 | +{ |
| 135 | + readFile.sync( 'beepboop', { 'flag': 123 } ); // $ExpectError |
| 136 | + readFile.sync( 'beepboop', { 'flag': true } ); // $ExpectError |
| 137 | + readFile.sync( 'beepboop', { 'flag': false } ); // $ExpectError |
| 138 | + readFile.sync( 'beepboop', { 'flag': null } ); // $ExpectError |
| 139 | + readFile.sync( 'beepboop', { 'flag': [] } ); // $ExpectError |
| 140 | + readFile.sync( 'beepboop', { 'flag': {} } ); // $ExpectError |
| 141 | + readFile.sync( 'beepboop', { 'flag': ( x: number ): number => x } ); // $ExpectError |
| 142 | +} |
| 143 | + |
| 144 | +// The compiler throws an error if the `sync` method is provided an unsupported number of arguments... |
| 145 | +{ |
| 146 | + readFile.sync(); // $ExpectError |
| 147 | +} |
0 commit comments