|
| 1 | +import { YargsParser } from './lib/yargs-parser' |
| 2 | + |
| 3 | +const parser = new YargsParser({ |
| 4 | + cwd: () => { return '' }, |
| 5 | + format: (str, arg) => { return str.replace('%s', arg) }, |
| 6 | + normalize: (str) => { return str }, |
| 7 | + resolve: (str) => { return str }, |
| 8 | + require: () => { |
| 9 | + throw Error('loading config from files not currently supported in browser') |
| 10 | + }, |
| 11 | + env: () => {} |
| 12 | +}) |
| 13 | + |
| 14 | +// Workaround the need for proper nodejs typings and externs |
| 15 | +// eslint-disable-next-line no-eval |
| 16 | +const anyDeepEqual = eval('require("assert").strict.deepEqual') as any |
| 17 | +function deepEqual<T> (actual: T, expected: T, message?: string): void { |
| 18 | + anyDeepEqual(actual, expected, message) |
| 19 | +} |
| 20 | + |
| 21 | +// Quoted properties aren't allowed in these lint settings. |
| 22 | +const FLAG_X = 'x' |
| 23 | + |
| 24 | +function runTests () { |
| 25 | + deepEqual(parser.parse([]).argv, { _: [] }, 'empty argv') |
| 26 | + deepEqual( |
| 27 | + parser.parse([`--${FLAG_X}=42`]).argv, |
| 28 | + { _: [], [FLAG_X]: 42 }, |
| 29 | + 'guessed numeric value') |
| 30 | + deepEqual( |
| 31 | + parser.parse([`--${FLAG_X}=str`]).argv, |
| 32 | + { _: [], [FLAG_X]: 'str' }, |
| 33 | + 'guessed string value') |
| 34 | + deepEqual( |
| 35 | + parser.parse([`--no-${FLAG_X}`]).argv, |
| 36 | + { _: [], [FLAG_X]: false }, |
| 37 | + 'guessed boolean negation') |
| 38 | + |
| 39 | + // Default values for types: |
| 40 | + deepEqual( |
| 41 | + parser.parse([`--${FLAG_X}`]).argv, |
| 42 | + { _: [], [FLAG_X]: true }, |
| 43 | + 'guessed boolean default true') |
| 44 | + |
| 45 | + console.log('ok') |
| 46 | +} |
| 47 | +runTests() |
0 commit comments