argument.custom-processing.test.js
This file
Path
tests/argument.custom-processing.test.js
207 lines · 6.8 KiB · JavaScript
Last changed by
Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c
History of this fileTake it
Extend .argument() to add default value and custom parse for command-argument (#1508)John Geebefore adoptionf99d6fa
lines 1–3why, one step back1const commander = require('../');23// Testing default value and custom processing behaviours.Support argument processing without action handler (#1529)John Geebefore adoption4663597
line 4why, one step back4// Some double assertions in tests to check action argument and .processedArgExtend .argument() to add default value and custom parse for command-argument (#1508)John Geebefore adoptionf99d6fa
lines 5–37why, one step back56test('when argument not specified then callback not called', () => {7 const mockCoercion = jest.fn();8 const program = new commander.Command();9 program10 .argument('[n]', 'number', mockCoercion)11 .action(() => {});12 program.parse([], { from: 'user' });13 expect(mockCoercion).not.toHaveBeenCalled();14});1516test('when argument not specified then action argument undefined', () => {17 let actionValue = 'foo';18 const program = new commander.Command();19 program20 .argument('[n]', 'number', parseFloat)21 .action((arg) => {22 actionValue = arg;23 });24 program.parse([], { from: 'user' });25 expect(actionValue).toBeUndefined();26});2728test('when custom with starting value and argument not specified then callback not called', () => {29 const mockCoercion = jest.fn();30 const program = new commander.Command();31 program32 .argument('[n]', 'number', parseFloat, 1)33 .action(() => {});34 program.parse([], { from: 'user' });35 expect(mockCoercion).not.toHaveBeenCalled();36});37Support argument processing without action handler (#1529)John Geebefore adoption4663597
line 38why, one step back38test('when custom with starting value and argument not specified with action handler then action argument is starting value', () => {Extend .argument() to add default value and custom parse for command-argument (#1508)John Geebefore adoptionf99d6fa
lines 39–48why, one step back39 const startingValue = 1;40 let actionValue;41 const program = new commander.Command();42 program43 .argument('[n]', 'number', parseFloat, startingValue)44 .action((arg) => {45 actionValue = arg;46 });47 program.parse([], { from: 'user' });48 expect(actionValue).toEqual(startingValue);Support argument processing without action handler (#1529)John Geebefore adoption4663597
line 49why, one step back49 expect(program.processedArgs).toEqual([startingValue]);Extend .argument() to add default value and custom parse for command-argument (#1508)John Geebefore adoptionf99d6fa
lines 50–51why, one step back50});51Support argument processing without action handler (#1529)John Geebefore adoption4663597
lines 52–61why, one step back52test('when custom with starting value and argument not specified without action handler then .processedArgs has starting value', () => {53 const startingValue = 1;54 const program = new commander.Command();55 program56 .argument('[n]', 'number', parseFloat, startingValue);57 program.parse([], { from: 'user' });58 expect(program.processedArgs).toEqual([startingValue]);59});6061test('when default value is defined (without custom processing) and argument not specified with action handler then action argument is default value', () => {Extend .argument() to add default value and custom parse for command-argument (#1508)John Geebefore adoptionf99d6fa
lines 62–71why, one step back62 const defaultValue = 1;63 let actionValue;64 const program = new commander.Command();65 program66 .argument('[n]', 'number', defaultValue)67 .action((arg) => {68 actionValue = arg;69 });70 program.parse([], { from: 'user' });71 expect(actionValue).toEqual(defaultValue);Support argument processing without action handler (#1529)John Geebefore adoption4663597
line 72why, one step back72 expect(program.processedArgs).toEqual([defaultValue]);Extend .argument() to add default value and custom parse for command-argument (#1508)John Geebefore adoptionf99d6fa
lines 73–74why, one step back73});74Support argument processing without action handler (#1529)John Geebefore adoption4663597
lines 75–83why, one step back75test('when default value is defined (without custom processing) and argument not specified without action handler then .processedArgs is default value', () => {76 const defaultValue = 1;77 const program = new commander.Command();78 program79 .argument('[n]', 'number', defaultValue);80 program.parse([], { from: 'user' });81 expect(program.processedArgs).toEqual([defaultValue]);82});83Extend .argument() to add default value and custom parse for command-argument (#1508)John Geebefore adoptionf99d6fa
lines 84–94why, one step back84test('when argument specified then callback called with value', () => {85 const mockCoercion = jest.fn();86 const value = '1';87 const program = new commander.Command();88 program89 .argument('[n]', 'number', mockCoercion)90 .action(() => {});91 program.parse([value], { from: 'user' });92 expect(mockCoercion).toHaveBeenCalledWith(value, undefined);93});94Support argument processing without action handler (#1529)John Geebefore adoption4663597
line 95why, one step back95test('when argument specified with action handler then action value is as returned from callback', () => {Extend .argument() to add default value and custom parse for command-argument (#1508)John Geebefore adoptionf99d6fa
lines 96–107why, one step back96 const callbackResult = 2;97 let actionValue;98 const program = new commander.Command();99 program100 .argument('[n]', 'number', () => {101 return callbackResult;102 })103 .action((arg) => {104 actionValue = arg;105 });106 program.parse(['node', 'test', 'alpha']);107 expect(actionValue).toEqual(callbackResult);Support argument processing without action handler (#1529)John Geebefore adoption4663597
line 108why, one step back108 expect(program.processedArgs).toEqual([callbackResult]);Extend .argument() to add default value and custom parse for command-argument (#1508)John Geebefore adoptionf99d6fa
lines 109–110why, one step back109});110Support argument processing without action handler (#1529)John Geebefore adoption4663597
lines 111–121why, one step back111test('when argument specified without action handler then .processedArgs is as returned from callback', () => {112 const callbackResult = 2;113 const program = new commander.Command();114 program115 .argument('[n]', 'number', () => {116 return callbackResult;117 });118 program.parse(['node', 'test', 'alpha']);119 expect(program.processedArgs).toEqual([callbackResult]);120});121Extend .argument() to add default value and custom parse for command-argument (#1508)John Geebefore adoptionf99d6fa
lines 122–159why, one step back122test('when argument specified then program.args has original rather than custom', () => {123 // This is as intended, so check behaviour.124 const callbackResult = 2;125 const program = new commander.Command();126 program127 .argument('[n]', 'number', () => {128 return callbackResult;129 })130 .action(() => {});131 program.parse(['node', 'test', 'alpha']);132 expect(program.args).toEqual(['alpha']);133});134135test('when custom with starting value and argument specified then callback called with value and starting value', () => {136 const mockCoercion = jest.fn();137 const startingValue = 1;138 const value = '2';139 const program = new commander.Command()140 .argument('[n]', 'number', mockCoercion, startingValue)141 .action(() => {});142 program.parse(['node', 'test', value]);143 expect(mockCoercion).toHaveBeenCalledWith(value, startingValue);144});145146test('when variadic argument specified multiple times then callback called with value and previousValue', () => {147 const mockCoercion = jest.fn().mockImplementation(() => {148 return 'callback';149 });150 const program = new commander.Command();151 program152 .argument('<n...>', 'number', mockCoercion)153 .action(() => {});154 program.parse(['1', '2'], { from: 'user' });155 expect(mockCoercion).toHaveBeenCalledTimes(2);156 expect(mockCoercion).toHaveBeenNthCalledWith(1, '1', undefined);157 expect(mockCoercion).toHaveBeenNthCalledWith(2, '2', 'callback');158});159Support argument processing without action handler (#1529)John Geebefore adoption4663597
lines 160–167why, one step back160test('when variadic argument without action handler then .processedArg has array', () => {161 const program = new commander.Command();162 program163 .argument('<n...>', 'number');164 program.parse(['1', '2'], { from: 'user' });165 expect(program.processedArgs).toEqual([['1', '2']]);166});167Extend .argument() to add default value and custom parse for command-argument (#1508)John Geebefore adoptionf99d6fa
lines 168–177why, one step back168test('when parseFloat "1e2" then action argument is 100', () => {169 let actionValue;170 const program = new commander.Command();171 program172 .argument('<number>', 'float argument', parseFloat)173 .action((arg) => {174 actionValue = arg;175 });176 program.parse(['1e2'], { from: 'user' });177 expect(actionValue).toEqual(100);Support argument processing without action handler (#1529)John Geebefore adoption4663597
line 178why, one step back178 expect(program.processedArgs).toEqual([actionValue]);Extend .argument() to add default value and custom parse for command-argument (#1508)John Geebefore adoptionf99d6fa
lines 179–186why, one step back179});180181test('when defined default value for required argument then throw', () => {182 const program = new commander.Command();183 expect(() => {184 program.argument('<number>', 'float argument', 4);185 }).toThrow();186});Add test to cover exception from custom argument processing (#1515)John Geebefore adoptionca514ae
lines 187–207why, one step back187188test('when custom processing for argument throws plain error then not CommanderError caught', () => {189 function justSayNo(value) {190 throw new Error('no no no');191 }192 const program = new commander.Command();193 program194 .exitOverride()195 .argument('[n]', 'number', justSayNo)196 .action(() => {});197198 let caughtErr;199 try {200 program.parse(['green'], { from: 'user' });201 } catch (err) {202 caughtErr = err;203 }204205 expect(caughtErr).toBeInstanceOf(Error);206 expect(caughtErr).not.toBeInstanceOf(commander.CommanderError);207});