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
Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c
lines 1–193why, one step back1const { Command, Option } = require('../');23describe('.option() with default and option not specified in parse', () => {4 test('when boolean option with boolean default then value is default', () => {5 const program = new Command();6 program.option('-d, --debug', 'description', false);7 program.parse([], { from: 'user' });8 expect(program.opts().debug).toBe(false);9 });1011 test('when boolean option with number zero default then value is zero', () => {12 const program = new Command();13 program.option('-d, --debug', 'description', 0);14 program.parse([], { from: 'user' });15 expect(program.opts().debug).toBe(0);16 });1718 test('when boolean option with string default then value is default', () => {19 const program = new Command();20 program.option('-d, --debug', 'description', 'default');21 program.parse([], { from: 'user' });22 expect(program.opts().debug).toBe('default');23 });2425 test('when required option-argument and default number then value is default', () => {26 const program = new Command();27 program.option('-p, --port <port-number>', 'description', 80);28 program.parse([], { from: 'user' });29 expect(program.opts().port).toBe(80);30 });3132 test('when required option-argument and default string then value is default', () => {33 const program = new Command();34 program.option('-p, --port <port-number>', 'description', 'foo');35 program.parse([], { from: 'user' });36 expect(program.opts().port).toBe('foo');37 });3839 test('when optional option-argument and default number then value is default', () => {40 const program = new Command();41 program.option('-p, --port [port-number]', 'description', 80);42 program.parse([], { from: 'user' });43 expect(program.opts().port).toBe(80);44 });4546 test('when optional option-argument and default string then value is default', () => {47 const program = new Command();48 program.option('-p, --port [port-number]', 'description', 'foo');49 program.parse([], { from: 'user' });50 expect(program.opts().port).toBe('foo');51 });5253 test('when negated and default string then value is default', () => {54 // Bit tricky thinking about what default means for a negated option, but treat as with other options.55 const program = new Command();56 program.option('--no-colour', 'description', 'RGB');57 program.parse([], { from: 'user' });58 expect(program.opts().colour).toBe('RGB');59 });60});6162describe('Option with default and option not specified in parse', () => {63 test('when boolean option with boolean default then value is default', () => {64 const program = new Command();65 program.addOption(new Option('-d, --debug').default(false));66 program.parse([], { from: 'user' });67 expect(program.opts().debug).toBe(false);68 });6970 test('when boolean option with number zero default then value is zero', () => {71 const program = new Command();72 program.addOption(new Option('-d, --debug').default(0));73 program.parse([], { from: 'user' });74 expect(program.opts().debug).toBe(0);75 });7677 test('when boolean option with string default then value is default', () => {78 const program = new Command();79 program.addOption(new Option('-d, --debug').default('default'));80 program.parse([], { from: 'user' });81 expect(program.opts().debug).toBe('default');82 });8384 test('when required option-argument and default number then value is default', () => {85 const program = new Command();86 program.addOption(new Option('-p, --port <port-number>').default(80));87 program.parse([], { from: 'user' });88 expect(program.opts().port).toBe(80);89 });9091 test('when required option-argument and default string then value is default', () => {92 const program = new Command();93 program.addOption(new Option('-p, --port <port-number>').default('foo'));94 program.parse([], { from: 'user' });95 expect(program.opts().port).toBe('foo');96 });9798 test('when optional option-argument and default number then value is default', () => {99 const program = new Command();100 program.addOption(new Option('-p, --port [port-number]').default(80));101 program.parse([], { from: 'user' });102 expect(program.opts().port).toBe(80);103 });104105 test('when optional option-argument and default string then value is default', () => {106 const program = new Command();107 program.addOption(new Option('-p, --port [port-number]').default('foo'));108 program.parse([], { from: 'user' });109 expect(program.opts().port).toBe('foo');110 });111112 test('when negated and default string then value is default', () => {113 // Bit tricky thinking about what default means for a negated option, but treat as with other options.114 const program = new Command();115 program.addOption(new Option('--no-colour').default('RGB'));116 program.parse([], { from: 'user' });117 expect(program.opts().colour).toBe('RGB');118 });119});120121// Fairly obvious this needs to happen, but was broken for optional in past!122describe('default overwritten by specified option', () => {123 test('when boolean option with boolean default then value is true', () => {124 const program = new Command();125 program.option('-d, --debug', 'description', false);126 program.parse(['-d'], { from: 'user' });127 expect(program.opts().debug).toBe(true);128 });129130 test('when boolean option with number zero default then value is true', () => {131 const program = new Command();132 program.option('-d, --debug', 'description', 0);133 program.parse(['-d'], { from: 'user' });134 expect(program.opts().debug).toBe(true);135 });136137 test('when boolean option with string default then value is true', () => {138 const program = new Command();139 program.option('-d, --debug', 'description', 'default');140 program.parse(['-d'], { from: 'user' });141 expect(program.opts().debug).toBe(true);142 });143144 test('when required option-argument and default number then value is from args', () => {145 const program = new Command();146 program.option('-p, --port <port-number>', 'description', 80);147 program.parse(['-p', '1234'], { from: 'user' });148 expect(program.opts().port).toBe('1234');149 });150151 test('when required option-argument and default string then value is from args', () => {152 const program = new Command();153 program.option('-p, --port <port-number>', 'description', 'foo');154 program.parse(['-p', '1234'], { from: 'user' });155 expect(program.opts().port).toBe('1234');156 });157158 test('when optional option-argument and default number and option-argument specified then value is from args', () => {159 const program = new Command();160 program.option('-p, --port [port-number]', 'description', 80);161 program.parse(['-p', '1234'], { from: 'user' });162 expect(program.opts().port).toBe('1234');163 });164165 test('when optional option-argument and default string and option-argument specified then value is from args', () => {166 const program = new Command();167 program.option('-p, --port [port-number]', 'description', 'foo');168 program.parse(['-p', '1234'], { from: 'user' });169 expect(program.opts().port).toBe('1234');170 });171172 test('when optional option-argument and default number and option-argument not specified then value is true', () => {173 const program = new Command();174 program.option('-p, --port [port-number]', 'description', 80);175 program.parse(['-p'], { from: 'user' });176 expect(program.opts().port).toBe(true);177 });178179 test('when optional option-argument and default string and option-argument not specified then value is true', () => {180 const program = new Command();181 program.option('-p, --port [port-number]', 'description', 'foo');182 program.parse(['-p'], { from: 'user' });183 expect(program.opts().port).toBe(true);184 });185186 test('when negated and default string then value is false', () => {187 // Bit tricky thinking about what default means for a negated option, but treat as with other options.188 const program = new Command();189 program.option('--no-colour', 'description', 'RGB');190 program.parse(['--no-colour'], { from: 'user' });191 expect(program.opts().colour).toBe(false);192 });193});