|
| 1 | +/* global assert */ |
| 2 | +/* eslint max-len: 0 */ |
| 3 | +'use strict'; |
| 4 | + |
| 5 | +const { test } = require('./utils'); |
| 6 | +const { parseArgs } = require('../index.js'); |
| 7 | + |
| 8 | +test('default must be a boolean when option type is boolean', () => { |
| 9 | + const args = []; |
| 10 | + const options = { alpha: { type: 'boolean', default: 'not a boolean' } }; |
| 11 | + assert.throws(() => { |
| 12 | + parseArgs({ args, options }); |
| 13 | + }, /options\.alpha\.default must be Boolean/ |
| 14 | + ); |
| 15 | +}); |
| 16 | + |
| 17 | +test('default must accept undefined value', () => { |
| 18 | + const args = []; |
| 19 | + const options = { alpha: { type: 'boolean', default: undefined } }; |
| 20 | + const result = parseArgs({ args, options }); |
| 21 | + const expected = { |
| 22 | + values: { |
| 23 | + __proto__: null, |
| 24 | + }, |
| 25 | + positionals: [] |
| 26 | + }; |
| 27 | + assert.deepStrictEqual(result, expected); |
| 28 | +}); |
| 29 | + |
| 30 | +test('default must be a boolean array when option type is boolean and multiple', () => { |
| 31 | + const args = []; |
| 32 | + const options = { alpha: { type: 'boolean', multiple: true, default: 'not an array' } }; |
| 33 | + assert.throws(() => { |
| 34 | + parseArgs({ args, options }); |
| 35 | + }, /options\.alpha\.default must be Array/ |
| 36 | + ); |
| 37 | +}); |
| 38 | + |
| 39 | +test('default must be a boolean array when option type is string and multiple is true', () => { |
| 40 | + const args = []; |
| 41 | + const options = { alpha: { type: 'boolean', multiple: true, default: [true, true, 42] } }; |
| 42 | + assert.throws(() => { |
| 43 | + parseArgs({ args, options }); |
| 44 | + }, /options\.alpha\.default\[2\] must be Boolean/ |
| 45 | + ); |
| 46 | +}); |
| 47 | + |
| 48 | +test('default must be a string when option type is string', () => { |
| 49 | + const args = []; |
| 50 | + const options = { alpha: { type: 'string', default: true } }; |
| 51 | + assert.throws(() => { |
| 52 | + parseArgs({ args, options }); |
| 53 | + }, /options\.alpha\.default must be String/ |
| 54 | + ); |
| 55 | +}); |
| 56 | + |
| 57 | +test('default must be an array when option type is string and multiple is true', () => { |
| 58 | + const args = []; |
| 59 | + const options = { alpha: { type: 'string', multiple: true, default: 'not an array' } }; |
| 60 | + assert.throws(() => { |
| 61 | + parseArgs({ args, options }); |
| 62 | + }, /options\.alpha\.default must be Array/ |
| 63 | + ); |
| 64 | +}); |
| 65 | + |
| 66 | +test('default must be a string array when option type is string and multiple is true', () => { |
| 67 | + const args = []; |
| 68 | + const options = { alpha: { type: 'string', multiple: true, default: ['str', 42] } }; |
| 69 | + assert.throws(() => { |
| 70 | + parseArgs({ args, options }); |
| 71 | + }, /options\.alpha\.default\[1\] must be String/ |
| 72 | + ); |
| 73 | +}); |
| 74 | + |
| 75 | +test('default accepted input when multiple is true', () => { |
| 76 | + const args = ['--inputStringArr', 'c', '--inputStringArr', 'd', '--inputBoolArr', '--inputBoolArr']; |
| 77 | + const options = { |
| 78 | + inputStringArr: { type: 'string', multiple: true, default: ['a', 'b'] }, |
| 79 | + emptyStringArr: { type: 'string', multiple: true, default: [] }, |
| 80 | + fullStringArr: { type: 'string', multiple: true, default: ['a', 'b'] }, |
| 81 | + inputBoolArr: { type: 'boolean', multiple: true, default: [false, true, false] }, |
| 82 | + emptyBoolArr: { type: 'boolean', multiple: true, default: [] }, |
| 83 | + fullBoolArr: { type: 'boolean', multiple: true, default: [false, true, false] }, |
| 84 | + }; |
| 85 | + const expected = { values: { __proto__: null, |
| 86 | + inputStringArr: ['c', 'd'], |
| 87 | + inputBoolArr: [true, true], |
| 88 | + emptyStringArr: [], |
| 89 | + fullStringArr: ['a', 'b'], |
| 90 | + emptyBoolArr: [], |
| 91 | + fullBoolArr: [false, true, false] }, |
| 92 | + positionals: [] }; |
| 93 | + const result = parseArgs({ args, options }); |
| 94 | + assert.deepStrictEqual(result, expected); |
| 95 | +}); |
| 96 | + |
| 97 | +test('when default is set, the option must be added as result', () => { |
| 98 | + const args = []; |
| 99 | + const options = { |
| 100 | + a: { type: 'string', default: 'HELLO' }, |
| 101 | + b: { type: 'boolean', default: false }, |
| 102 | + c: { type: 'boolean', default: true } |
| 103 | + }; |
| 104 | + const expected = { values: { __proto__: null, a: 'HELLO', b: false, c: true }, positionals: [] }; |
| 105 | + |
| 106 | + const result = parseArgs({ args, options }); |
| 107 | + assert.deepStrictEqual(result, expected); |
| 108 | +}); |
| 109 | + |
| 110 | +test('when default is set, the args value takes precedence', () => { |
| 111 | + const args = ['--a', 'WORLD', '--b', '-c']; |
| 112 | + const options = { |
| 113 | + a: { type: 'string', default: 'HELLO' }, |
| 114 | + b: { type: 'boolean', default: false }, |
| 115 | + c: { type: 'boolean', default: true } |
| 116 | + }; |
| 117 | + const expected = { values: { __proto__: null, a: 'WORLD', b: true, c: true }, positionals: [] }; |
| 118 | + |
| 119 | + const result = parseArgs({ args, options }); |
| 120 | + assert.deepStrictEqual(result, expected); |
| 121 | +}); |
| 122 | + |
| 123 | +test('tokens should not include the default options', () => { |
| 124 | + const args = []; |
| 125 | + const options = { |
| 126 | + a: { type: 'string', default: 'HELLO' }, |
| 127 | + b: { type: 'boolean', default: false }, |
| 128 | + c: { type: 'boolean', default: true } |
| 129 | + }; |
| 130 | + |
| 131 | + const expectedTokens = []; |
| 132 | + |
| 133 | + const { tokens } = parseArgs({ args, options, tokens: true }); |
| 134 | + assert.deepStrictEqual(tokens, expectedTokens); |
| 135 | +}); |
| 136 | + |
| 137 | +test('tokens:true should not include the default options after the args input', () => { |
| 138 | + const args = ['--z', 'zero', 'positional-item']; |
| 139 | + const options = { |
| 140 | + z: { type: 'string' }, |
| 141 | + a: { type: 'string', default: 'HELLO' }, |
| 142 | + b: { type: 'boolean', default: false }, |
| 143 | + c: { type: 'boolean', default: true } |
| 144 | + }; |
| 145 | + |
| 146 | + const expectedTokens = [ |
| 147 | + { kind: 'option', name: 'z', rawName: '--z', index: 0, value: 'zero', inlineValue: false }, |
| 148 | + { kind: 'positional', index: 2, value: 'positional-item' }, |
| 149 | + ]; |
| 150 | + |
| 151 | + const { tokens } = parseArgs({ args, options, tokens: true, allowPositionals: true }); |
| 152 | + assert.deepStrictEqual(tokens, expectedTokens); |
| 153 | +}); |
| 154 | + |
| 155 | +test('proto as default value must be ignored', () => { |
| 156 | + const args = []; |
| 157 | + const options = Object.create(null); |
| 158 | + |
| 159 | + // eslint-disable-next-line no-proto |
| 160 | + options.__proto__ = { type: 'string', default: 'HELLO' }; |
| 161 | + |
| 162 | + const result = parseArgs({ args, options, allowPositionals: true }); |
| 163 | + const expected = { values: { __proto__: null }, positionals: [] }; |
| 164 | + assert.deepStrictEqual(result, expected); |
| 165 | +}); |
| 166 | + |
| 167 | + |
| 168 | +test('multiple as false should expect a String', () => { |
| 169 | + const args = []; |
| 170 | + const options = { alpha: { type: 'string', multiple: false, default: ['array'] } }; |
| 171 | + assert.throws(() => { |
| 172 | + parseArgs({ args, options }); |
| 173 | + }, / must be String got array/); |
| 174 | +}); |
0 commit comments