options.default.test.js
This file
Path
tests/options.default.test.js
193 lines · 7.9 KiB · JavaScript
Last changed by
Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194const { Command, Option } = require('../');
describe('.option() with default 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' }); expect(program.opts().debug).toBe(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' }); expect(program.opts().debug).toBe(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' }); expect(program.opts().debug).toBe('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' }); expect(program.opts().port).toBe(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' }); expect(program.opts().port).toBe('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' }); expect(program.opts().port).toBe(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' }); expect(program.opts().port).toBe('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' }); expect(program.opts().colour).toBe('RGB'); });});
describe('Option with default 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' }); expect(program.opts().debug).toBe(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' }); expect(program.opts().debug).toBe(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' }); expect(program.opts().debug).toBe('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' }); expect(program.opts().port).toBe(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' }); expect(program.opts().port).toBe('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' }); expect(program.opts().port).toBe(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' }); expect(program.opts().port).toBe('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' }); expect(program.opts().colour).toBe('RGB'); });});
// Fairly obvious this needs to happen, but was broken for optional in past!describe('default overwritten by specified option', () => { 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' }); expect(program.opts().debug).toBe(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' }); expect(program.opts().debug).toBe(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' }); expect(program.opts().debug).toBe(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' }); expect(program.opts().port).toBe('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' }); expect(program.opts().port).toBe('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' }); expect(program.opts().port).toBe('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' }); expect(program.opts().port).toBe('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' }); expect(program.opts().port).toBe(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' }); expect(program.opts().port).toBe(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' }); expect(program.opts().colour).toBe(false); });});