|
| 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 readJSON = 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 | + readJSON( 'package.json', 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 | + readJSON( false, onLoad ); // $ExpectError |
| 38 | + readJSON( true, onLoad ); // $ExpectError |
| 39 | + readJSON( null, onLoad ); // $ExpectError |
| 40 | + readJSON( undefined, onLoad ); // $ExpectError |
| 41 | + readJSON( [], onLoad ); // $ExpectError |
| 42 | + readJSON( {}, onLoad ); // $ExpectError |
| 43 | + readJSON( ( 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 | + readJSON( '/path/to/package.json', 'abc' ); // $ExpectError |
| 49 | + readJSON( '/path/to/package.json', 1 ); // $ExpectError |
| 50 | + readJSON( '/path/to/package.json', false ); // $ExpectError |
| 51 | + readJSON( '/path/to/package.json', true ); // $ExpectError |
| 52 | + readJSON( '/path/to/package.json', null ); // $ExpectError |
| 53 | + readJSON( '/path/to/package.json', undefined ); // $ExpectError |
| 54 | + readJSON( '/path/to/package.json', [] ); // $ExpectError |
| 55 | + readJSON( '/path/to/package.json', {} ); // $ExpectError |
| 56 | + readJSON( '/path/to/package.json', ( 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 | + readJSON( 'package.json', false, onLoad ); // $ExpectError |
| 62 | + readJSON( 'package.json', true, onLoad ); // $ExpectError |
| 63 | + readJSON( 'package.json', null, onLoad ); // $ExpectError |
| 64 | + readJSON( 'package.json', undefined, onLoad ); // $ExpectError |
| 65 | + readJSON( 'package.json', 123, onLoad ); // $ExpectError |
| 66 | + readJSON( 'package.json', [], onLoad ); // $ExpectError |
| 67 | + readJSON( 'package.json', ( 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 | + readJSON( 'package.json', { 'encoding': 123 }, onLoad ); // $ExpectError |
| 73 | + readJSON( 'package.json', { 'encoding': true }, onLoad ); // $ExpectError |
| 74 | + readJSON( 'package.json', { 'encoding': false }, onLoad ); // $ExpectError |
| 75 | + readJSON( 'package.json', { 'encoding': [] }, onLoad ); // $ExpectError |
| 76 | + readJSON( 'package.json', { 'encoding': {} }, onLoad ); // $ExpectError |
| 77 | + readJSON( 'package.json', { '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 | + readJSON( 'package.json', { 'flag': 123 }, onLoad ); // $ExpectError |
| 83 | + readJSON( 'package.json', { 'flag': true }, onLoad ); // $ExpectError |
| 84 | + readJSON( 'package.json', { 'flag': false }, onLoad ); // $ExpectError |
| 85 | + readJSON( 'package.json', { 'flag': null }, onLoad ); // $ExpectError |
| 86 | + readJSON( 'package.json', { 'flag': [] }, onLoad ); // $ExpectError |
| 87 | + readJSON( 'package.json', { 'flag': {} }, onLoad ); // $ExpectError |
| 88 | + readJSON( 'package.json', { 'flag': ( x: number ): number => x }, onLoad ); // $ExpectError |
| 89 | +} |
| 90 | + |
| 91 | +// The compiler throws an error if the function is provided a `reviver` option which is not a function... |
| 92 | +{ |
| 93 | + readJSON( 'package.json', { 'reviver': 123 }, onLoad ); // $ExpectError |
| 94 | + readJSON( 'package.json', { 'reviver': 'abc' }, onLoad ); // $ExpectError |
| 95 | + readJSON( 'package.json', { 'reviver': true }, onLoad ); // $ExpectError |
| 96 | + readJSON( 'package.json', { 'reviver': false }, onLoad ); // $ExpectError |
| 97 | + readJSON( 'package.json', { 'reviver': null }, onLoad ); // $ExpectError |
| 98 | + readJSON( 'package.json', { 'reviver': [] }, onLoad ); // $ExpectError |
| 99 | + readJSON( 'package.json', { 'reviver': {} }, onLoad ); // $ExpectError |
| 100 | +} |
| 101 | + |
| 102 | +// The compiler throws an error if the function is provided an unsupported number of arguments... |
| 103 | +{ |
| 104 | + readJSON(); // $ExpectError |
| 105 | + readJSON( 'C:\\foo\\bar\\baz\\package.json' ); // $ExpectError |
| 106 | +} |
| 107 | + |
| 108 | +// Attached to main export is a `sync` method which returns a string or an error... |
| 109 | +{ |
| 110 | + readJSON.sync( 'package.json' ); // $ExpectType string | Error |
| 111 | +} |
| 112 | + |
| 113 | +// The compiler throws an error if the `sync` method is provided a first argument which is not a string, buffer, or file descriptor... |
| 114 | +{ |
| 115 | + readJSON.sync( false ); // $ExpectError |
| 116 | + readJSON.sync( true ); // $ExpectError |
| 117 | + readJSON.sync( null ); // $ExpectError |
| 118 | + readJSON.sync( undefined ); // $ExpectError |
| 119 | + readJSON.sync( [] ); // $ExpectError |
| 120 | + readJSON.sync( {} ); // $ExpectError |
| 121 | + readJSON.sync( ( x: number ): number => x ); // $ExpectError |
| 122 | +} |
| 123 | + |
| 124 | +// The compiler throws an error if the `sync` method is provided an options argument which is not an object or string... |
| 125 | +{ |
| 126 | + readJSON.sync( 'package.json', null ); // $ExpectError |
| 127 | + readJSON.sync( 'package.json', true ); // $ExpectError |
| 128 | + readJSON.sync( 'package.json', false ); // $ExpectError |
| 129 | + readJSON.sync( 'package.json', 123 ); // $ExpectError |
| 130 | + readJSON.sync( 'package.json', [] ); // $ExpectError |
| 131 | + readJSON.sync( 'package.json', ( x: number ): number => x ); // $ExpectError |
| 132 | +} |
| 133 | + |
| 134 | +// The compiler throws an error if the `sync` method is provided an `encoding` option which is not a string or null... |
| 135 | +{ |
| 136 | + readJSON.sync( 'package.json', { 'encoding': 123 } ); // $ExpectError |
| 137 | + readJSON.sync( 'package.json', { 'encoding': true } ); // $ExpectError |
| 138 | + readJSON.sync( 'package.json', { 'encoding': false } ); // $ExpectError |
| 139 | + readJSON.sync( 'package.json', { 'encoding': [] } ); // $ExpectError |
| 140 | + readJSON.sync( 'package.json', { 'encoding': {} } ); // $ExpectError |
| 141 | + readJSON.sync( 'package.json', { 'encoding': ( x: number ): number => x } ); // $ExpectError |
| 142 | +} |
| 143 | + |
| 144 | +// The compiler throws an error if the `sync` method is provided a `flag` option which is not a string... |
| 145 | +{ |
| 146 | + readJSON.sync( 'package.json', { 'flag': 123 } ); // $ExpectError |
| 147 | + readJSON.sync( 'package.json', { 'flag': true } ); // $ExpectError |
| 148 | + readJSON.sync( 'package.json', { 'flag': false } ); // $ExpectError |
| 149 | + readJSON.sync( 'package.json', { 'flag': null } ); // $ExpectError |
| 150 | + readJSON.sync( 'package.json', { 'flag': [] } ); // $ExpectError |
| 151 | + readJSON.sync( 'package.json', { 'flag': {} } ); // $ExpectError |
| 152 | + readJSON.sync( 'package.json', { 'flag': ( x: number ): number => x } ); // $ExpectError |
| 153 | +} |
| 154 | + |
| 155 | +// The compiler throws an error if the `sync` method is provided a `reviver` option which is not a function... |
| 156 | +{ |
| 157 | + readJSON.sync( 'package.json', { 'reviver': 'abc' } ); // $ExpectError |
| 158 | + readJSON.sync( 'package.json', { 'reviver': 123 } ); // $ExpectError |
| 159 | + readJSON.sync( 'package.json', { 'reviver': true } ); // $ExpectError |
| 160 | + readJSON.sync( 'package.json', { 'reviver': false } ); // $ExpectError |
| 161 | + readJSON.sync( 'package.json', { 'reviver': null } ); // $ExpectError |
| 162 | + readJSON.sync( 'package.json', { 'reviver': [] } ); // $ExpectError |
| 163 | + readJSON.sync( 'package.json', { 'reviver': {} } ); // $ExpectError |
| 164 | +} |
| 165 | + |
| 166 | +// The compiler throws an error if the `sync` method is provided an unsupported number of arguments... |
| 167 | +{ |
| 168 | + readJSON.sync(); // $ExpectError |
| 169 | +} |
0 commit comments