options.default.test.js
This file
Path
tests/options.default.test.js
195 lines · 8.1 KiB · JavaScript
Last changed by
Switching to esm-only (#2464)John Geebefore adoptionf4bd470
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196import { Command, Option } from '../index.js';import { test, describe } from 'node:test';import assert from 'node:assert/strict';
describe('Command.option() with default value and option not specified in parse', () => { test('when boolean option with boolean default then value is default', () => { const program = new Command(); program.option('-d, --debug', 'description', false); program.parse([], { from: 'user' }); assert.equal(program.opts().debug, false); });
test('when boolean option with number zero default then value is zero', () => { const program = new Command(); program.option('-d, --debug', 'description', 0); program.parse([], { from: 'user' }); assert.equal(program.opts().debug, 0); });
test('when boolean option with string default then value is default', () => { const program = new Command(); program.option('-d, --debug', 'description', 'default'); program.parse([], { from: 'user' }); assert.equal(program.opts().debug, 'default'); });
test('when required option-argument and default number then value is default', () => { const program = new Command(); program.option('-p, --port <port-number>', 'description', 80); program.parse([], { from: 'user' }); assert.equal(program.opts().port, 80); });
test('when required option-argument and default string then value is default', () => { const program = new Command(); program.option('-p, --port <port-number>', 'description', 'foo'); program.parse([], { from: 'user' }); assert.equal(program.opts().port, 'foo'); });
test('when optional option-argument and default number then value is default', () => { const program = new Command(); program.option('-p, --port [port-number]', 'description', 80); program.parse([], { from: 'user' }); assert.equal(program.opts().port, 80); });
test('when optional option-argument and default string then value is default', () => { const program = new Command(); program.option('-p, --port [port-number]', 'description', 'foo'); program.parse([], { from: 'user' }); assert.equal(program.opts().port, 'foo'); });
test('when negated and default string then value is default', () => { // Bit tricky thinking about what default means for a negated option, but treat as with other options. const program = new Command(); program.option('--no-colour', 'description', 'RGB'); program.parse([], { from: 'user' }); assert.equal(program.opts().colour, 'RGB'); });});
describe('Option with default value and option not specified in parse', () => { test('when boolean option with boolean default then value is default', () => { const program = new Command(); program.addOption(new Option('-d, --debug').default(false)); program.parse([], { from: 'user' }); assert.equal(program.opts().debug, false); });
test('when boolean option with number zero default then value is zero', () => { const program = new Command(); program.addOption(new Option('-d, --debug').default(0)); program.parse([], { from: 'user' }); assert.equal(program.opts().debug, 0); });
test('when boolean option with string default then value is default', () => { const program = new Command(); program.addOption(new Option('-d, --debug').default('default')); program.parse([], { from: 'user' }); assert.equal(program.opts().debug, 'default'); });
test('when required option-argument and default number then value is default', () => { const program = new Command(); program.addOption(new Option('-p, --port <port-number>').default(80)); program.parse([], { from: 'user' }); assert.equal(program.opts().port, 80); });
test('when required option-argument and default string then value is default', () => { const program = new Command(); program.addOption(new Option('-p, --port <port-number>').default('foo')); program.parse([], { from: 'user' }); assert.equal(program.opts().port, 'foo'); });
test('when optional option-argument and default number then value is default', () => { const program = new Command(); program.addOption(new Option('-p, --port [port-number]').default(80)); program.parse([], { from: 'user' }); assert.equal(program.opts().port, 80); });
test('when optional option-argument and default string then value is default', () => { const program = new Command(); program.addOption(new Option('-p, --port [port-number]').default('foo')); program.parse([], { from: 'user' }); assert.equal(program.opts().port, 'foo'); });
test('when negated and default string then value is default', () => { // Bit tricky thinking about what default means for a negated option, but treat as with other options. const program = new Command(); program.addOption(new Option('--no-colour').default('RGB')); program.parse([], { from: 'user' }); assert.equal(program.opts().colour, 'RGB'); });});
// Fairly obvious this needs to happen, but was broken for optional in past!describe('option default value overwritten by option specified in parse', () => { test('when boolean option with boolean default then value is true', () => { const program = new Command(); program.option('-d, --debug', 'description', false); program.parse(['-d'], { from: 'user' }); assert.equal(program.opts().debug, true); });
test('when boolean option with number zero default then value is true', () => { const program = new Command(); program.option('-d, --debug', 'description', 0); program.parse(['-d'], { from: 'user' }); assert.equal(program.opts().debug, true); });
test('when boolean option with string default then value is true', () => { const program = new Command(); program.option('-d, --debug', 'description', 'default'); program.parse(['-d'], { from: 'user' }); assert.equal(program.opts().debug, true); });
test('when required option-argument and default number then value is from args', () => { const program = new Command(); program.option('-p, --port <port-number>', 'description', 80); program.parse(['-p', '1234'], { from: 'user' }); assert.equal(program.opts().port, '1234'); });
test('when required option-argument and default string then value is from args', () => { const program = new Command(); program.option('-p, --port <port-number>', 'description', 'foo'); program.parse(['-p', '1234'], { from: 'user' }); assert.equal(program.opts().port, '1234'); });
test('when optional option-argument and default number and option-argument specified then value is from args', () => { const program = new Command(); program.option('-p, --port [port-number]', 'description', 80); program.parse(['-p', '1234'], { from: 'user' }); assert.equal(program.opts().port, '1234'); });
test('when optional option-argument and default string and option-argument specified then value is from args', () => { const program = new Command(); program.option('-p, --port [port-number]', 'description', 'foo'); program.parse(['-p', '1234'], { from: 'user' }); assert.equal(program.opts().port, '1234'); });
test('when optional option-argument and default number and option-argument not specified then value is true', () => { const program = new Command(); program.option('-p, --port [port-number]', 'description', 80); program.parse(['-p'], { from: 'user' }); assert.equal(program.opts().port, true); });
test('when optional option-argument and default string and option-argument not specified then value is true', () => { const program = new Command(); program.option('-p, --port [port-number]', 'description', 'foo'); program.parse(['-p'], { from: 'user' }); assert.equal(program.opts().port, true); });
test('when negated and default string then value is false', () => { // Bit tricky thinking about what default means for a negated option, but treat as with other options. const program = new Command(); program.option('--no-colour', 'description', 'RGB'); program.parse(['--no-colour'], { from: 'user' }); assert.equal(program.opts().colour, false); });});