Skip to content
Myr

negatives.test.js

This file

Path

tests/negatives.test.js

199 lines · 6.8 KiB · JavaScript

Last changed by

Switching to esm-only (#2464)John Geebefore adoptionf4bd470

History of this file

Take it

Switching to esm-only (#2464)John Geebefore adoptionf4bd470

lines 1–4why, one step back
1import { Command } from '../index.js';
2import { createTestCommand } from './testHelpers.js';3import { test, describe } from 'node:test';4import assert from 'node:assert/strict';

Allow negative numbers as optional optional arguments and command arguments when unambiguous (#2339)John Geebefore adoptionf6302de

line 5why, one step back
5

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 6–27why, one step back
6describe('negative numbers', () => {
7 describe('negative numbers in args', () => {8 // boolean is whether is a consumable argument when negative numbers allowed9 const negativeNumbers = [10 ['-.1', true],11 ['-123', true],12 ['-123.45', true],13 ['-1e3', true],14 ['-1e+3', true],15 ['-1e-3', true],16 ['-1.2e3', true],17 ['-1.2e+3', true],18 ['-1.2e-3', true],19 ['-1e-3.0', false], // invalid number format20 ['--1 ', false], // invalid number format21 ['-0', true],22 ['1', true],23 ['-1x', false], // whole string is not a number24 ['-x-1 ', false], // whole string is not a number25 ['', true],26 ['-0x1234', false], // not a plain number27 ];

Allow negative numbers as optional optional arguments and command arguments when unambiguous (#2339)John Geebefore adoptionf6302de

line 28why, one step back
28

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 29–32why, one step back
29 negativeNumbers.forEach(([value, consume]) => {
30 function callProgram(program, args, consume) {31 if (consume) {32 assert.doesNotThrow(() => {

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

line 33why, one step back
33 program.parse(args, { from: 'user' });

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 34–42why, one step back
34 });
35 } else {36 assert.throws(37 () => {38 program.parse(args, { from: 'user' });39 },40 { code: 'commander.unknownOption' },41 );42 }

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

line 43why, one step back
43 }

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 44–51why, one step back
44 test(`when option-argument for short optional is ${value} then consumed=${consume}`, () => {
45 const program = createTestCommand();46 program.option('-o, --optional [value]', 'optional option');47 const args = ['-o', value];48 callProgram(program, args, consume);49 // throws after setting optional to true50 assert.equal(program.opts()['optional'], consume ? value : true);51 });

Allow negative numbers as optional optional arguments and command arguments when unambiguous (#2339)John Geebefore adoptionf6302de

line 52why, one step back
52

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 53–60why, one step back
53 test(`when option-argument for long optional is ${value} then consumed=${consume}`, () => {
54 const program = createTestCommand();55 program.option('-o, --optional [value]', 'optional option');56 const args = ['--optional', value];57 callProgram(program, args, consume);58 // throws after setting optional to true59 assert.equal(program.opts()['optional'], consume ? value : true);60 });

Allow negative numbers as optional optional arguments and command arguments when unambiguous (#2339)John Geebefore adoptionf6302de

line 61why, one step back
61

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 62–72why, one step back
62 test(`when option-argument for short optional... is ${value} then consumed=${consume}`, () => {
63 const program = createTestCommand();64 program.option('-o, --optional [value...]', 'optional option');65 const args = ['-o', 'first', value];66 callProgram(program, args, consume);67 // throws after consuming 'first'68 assert.deepEqual(69 program.opts()['optional'],70 consume ? ['first', value] : ['first'],71 );72 });

Allow negative numbers as optional optional arguments and command arguments when unambiguous (#2339)John Geebefore adoptionf6302de

line 73why, one step back
73

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 74–84why, one step back
74 test(`when option-argument for long optional... is ${value} then consumed=${consume}`, () => {
75 const program = createTestCommand();76 program.option('-o, --optional [value...]', 'optional option');77 const args = ['--optional', 'first', value];78 callProgram(program, args, consume);79 // throws after consuming 'first'80 assert.deepEqual(81 program.opts()['optional'],82 consume ? ['first', value] : ['first'],83 );84 });

Allow negative numbers as optional optional arguments and command arguments when unambiguous (#2339)John Geebefore adoptionf6302de

line 85why, one step back
85

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 86–95why, one step back
86 test(`when command-argument is ${value} then consumed=${consume}`, () => {
87 const program = createTestCommand();88 program.argument('<value>', 'argument');89 const args = [value];90 callProgram(program, args, consume);91 assert.deepEqual(92 consume ? program.args : undefined,93 consume ? [value] : undefined,94 );95 });

Allow negative numbers as optional optional arguments and command arguments when unambiguous (#2339)John Geebefore adoptionf6302de

line 96why, one step back
96

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 97–118why, one step back
97 test(`when digit option defined and option-argument is %s then negative not consumed`, () => {
98 const program = createTestCommand();99 program100 .option('-o, --optional [value]', 'optional option')101 .option('-9', 'register option using digit');102 const args = ['-o', value];103 let customConsume = value[0] !== '-';104 callProgram(program, args, customConsume);105 assert.equal(program.opts()['optional'], customConsume ? value : true);106 });107108 test(`when digit option defined and command-argument is %s then negative not consumed`, () => {109 const program = createTestCommand();110 program.argument('[value]').option('-9', 'register option using digit');111 const args = [value];112 let customConsume = value[0] !== '-';113 callProgram(program, args, customConsume);114 assert.deepEqual(115 customConsume ? program.args : undefined,116 customConsume ? [value] : undefined,117 );118 });

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

line 119why, one step back
119 });

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

line 120why, one step back
120 });

Allow negative numbers as optional optional arguments and command arguments when unambiguous (#2339)John Geebefore adoptionf6302de

line 121why, one step back
121

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 122–149why, one step back
122 test('when complex example with negative numbers then all consumed', () => {
123 const program = new Command();124 program125 .option('-o [value]', 'optional')126 .option('-m <value>', 'required option-argument')127 .option('-O [value...]', 'optional')128 .option('-M <value...>', 'required option-argument')129 .argument('[value...]', 'argument');130 const args = [131 '-10',132 '-O',133 '-40',134 '-41',135 '-M',136 '-50',137 '-51',138 '-o',139 '-20',140 '-m',141 '-30',142 '-11',143 ];144 program.parse(args, { from: 'user' });145 assert.deepEqual(program.opts(), {146 o: '-20',147 m: '-30',148 O: ['-40', '-41'],149 M: ['-50', '-51'],

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

line 150why, one step back
150 });

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

line 151why, one step back
151 assert.deepEqual(program.args, ['-10', '-11']);

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

line 152why, one step back
152 });

Allow negative numbers as optional optional arguments and command arguments when unambiguous (#2339)John Geebefore adoptionf6302de

line 153why, one step back
153

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 154–165why, one step back
154 test('when program has digit option then negatives not allowed in leaf command', () => {
155 const program = createTestCommand();156 program.option('-2', 'double option');157 let leafArgs;158 program159 .command('leaf')160 .argument('[value...]')161 .action((args) => {162 leafArgs = args;163 });164 const args = ['leaf', '-1'];165 assert.throws(() => program.parse(args, { from: 'user' }));

Allow negative numbers as optional optional arguments and command arguments when unambiguous (#2339)John Geebefore adoptionf6302de

lines 166–167why, one step back
166 });
167

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 168–179why, one step back
168 test('when default command without digit option then negatives accepted', () => {
169 const program = new Command();170 let leafArgs;171 program172 .command('leaf', { isDefault: true })173 .argument('[value...]')174 .action((args) => {175 leafArgs = args;176 });177 program.parse(['-1'], { from: 'user' });178 assert.deepEqual(leafArgs, ['-1']);179 });

Allow negative numbers as optional optional arguments and command arguments when unambiguous (#2339)John Geebefore adoptionf6302de

line 180why, one step back
180

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 181–189why, one step back
181 test('when default command with digit option then negative throws', () => {
182 const program = createTestCommand();183 program184 .command('leaf', { isDefault: true })185 .option('-2')186 .argument('[value...]')187 .action(() => {});188 assert.throws(() => program.parse(['-1'], { from: 'user' }));189 });

Allow negative numbers as optional optional arguments and command arguments when unambiguous (#2339)John Geebefore adoptionf6302de

line 190why, one step back
190

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 191–198why, one step back
191 test('when program has subcommand and action handler then negative command-argument unsupported', () => {
192 // Known limitation in parsing. Only allowed negative command-arguments in leaf commands193 // to minimise changes to parsing when added support for negative numbers.194 const program = createTestCommand();195 program.argument('[value...]').action(() => {});196 program.command('leaf').action(() => {});197 assert.throws(() => program.parse(['-1'], { from: 'user' }));198 });

Allow negative numbers as optional optional arguments and command arguments when unambiguous (#2339)John Geebefore adoptionf6302de

line 199why, one step back
199});