options.custom-processing.test.js
This file
Path
tests/options.custom-processing.test.js
151 lines · 5.2 KiB · JavaScript
Last changed by
Merge branch 'develop' into release/7.xJohn Geebefore adoptiond90ef12
History of this fileTake it
Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 1–65why, one step back1const commander = require('../');23function myParseInt(value, dummyPrevious) {4 // parseInt takes a string and a radix5 return parseInt(value, 10);6}78function increaseVerbosity(dummyValue, previous) {9 return previous + 1;10}1112function collect(value, previous) {13 return previous.concat([value]);14}1516function commaSeparatedList(value, dummyPrevious) {17 return value.split(',');18}1920test('when option not specified then callback not called', () => {21 const mockCoercion = jest.fn();22 const program = new commander.Command();23 program24 .option('-i, --integer <n>', 'number', mockCoercion);25 program.parse(['node', 'test']);26 expect(mockCoercion).not.toHaveBeenCalled();27});2829test('when option not specified then value is undefined', () => {30 const program = new commander.Command();31 program32 .option('-i, --integer <n>', 'number', myParseInt);33 program.parse(['node', 'test']);34 expect(program.integer).toBeUndefined();35});3637test('when starting value is defined and option not specified then callback not called', () => {38 const mockCoercion = jest.fn();39 const program = new commander.Command();40 program41 .option('-i, --integer <n>', 'number', mockCoercion, 1);42 program.parse(['node', 'test']);43 expect(mockCoercion).not.toHaveBeenCalled();44});4546test('when starting value is defined and option not specified then value is starting value', () => {47 // NB: Can not specify a starting value for a boolean flag! Discovered when writing this test...48 const startingValue = 1;49 const program = new commander.Command();50 program51 .option('-i, --integer <n>', 'number', myParseInt, startingValue);52 program.parse(['node', 'test']);53 expect(program.integer).toBe(startingValue);54});5556test('when option specified then callback called with value', () => {57 const mockCoercion = jest.fn();58 const value = '1';59 const program = new commander.Command();60 program61 .option('-i, --integer <n>', 'number', mockCoercion);62 program.parse(['node', 'test', '-i', value]);63 expect(mockCoercion).toHaveBeenCalledWith(value, undefined);64});65Merge branch 'develop' into release/7.xJohn Geebefore adoptiond90ef12
line 66why, one step back66test('when option specified then value is as returned from callback', () => {Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 67–100why, one step back67 const callbackResult = 2;68 const program = new commander.Command();69 program70 .option('-i, --integer <n>', 'number', () => {71 return callbackResult;72 });73 program.parse(['node', 'test', '-i', '0']);74 expect(program.integer).toBe(callbackResult);75});7677test('when starting value is defined and option specified then callback called with value and starting value', () => {78 const mockCoercion = jest.fn();79 const startingValue = 1;80 const value = '2';81 const program = new commander.Command();82 program83 .option('-i, --integer <n>', 'number', mockCoercion, startingValue);84 program.parse(['node', 'test', '-i', value]);85 expect(mockCoercion).toHaveBeenCalledWith(value, startingValue);86});8788test('when option specified multiple times then callback called with value and previousValue', () => {89 const mockCoercion = jest.fn().mockImplementation(() => {90 return 'callback';91 });92 const program = new commander.Command();93 program94 .option('-i, --integer <n>', 'number', mockCoercion);95 program.parse(['node', 'test', '-i', '1', '-i', '2']);96 expect(mockCoercion).toHaveBeenCalledTimes(2);97 expect(mockCoercion).toHaveBeenNthCalledWith(1, '1', undefined);98 expect(mockCoercion).toHaveBeenNthCalledWith(2, '2', 'callback');99});100Enhance Option class to allow hiding help, specifying choices, and change how default value displayed in help (#1331)John Geebefore adoptiona05a8bc
lines 101–110why, one step back101// this is the happy path, testing failure case in command.exitOverride.test.js102test('when option argument in choices then option set', () => {103 const program = new commander.Command();104 program105 .exitOverride()106 .addOption(new commander.Option('--colour <shade>').choices(['red', 'blue']));107 program.parse(['--colour', 'red'], { from: 'user' });108 expect(program.colour).toBe('red');109});110Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 111–151why, one step back111// Now some functional tests like the examples in the README!112113test('when parseFloat "1e2" then value is 100', () => {114 const program = new commander.Command();115 program116 .option('-f, --float <number>', 'float argument', parseFloat);117 program.parse(['node', 'test', '-f', '1e2']);118 expect(program.float).toBe(100);119});120121test('when myParseInt "1" then value is 1', () => {122 const program = new commander.Command();123 program124 .option('-i, --integer <number>', 'integer argument', myParseInt);125 program.parse(['node', 'test', '-i', '1']);126 expect(program.integer).toBe(1);127});128129test('when increaseVerbosity -v -v -v then value is 3', () => {130 const program = new commander.Command();131 program132 .option('-v, --verbose', 'verbosity that can be increased', increaseVerbosity, 0);133 program.parse(['node', 'test', '-v', '-v', '-v']);134 expect(program.verbose).toBe(3);135});136137test('when collect -c a -c b -c c then value is [a, b, c]', () => {138 const program = new commander.Command();139 program140 .option('-c, --collect <value>', 'repeatable value', collect, []);141 program.parse(['node', 'test', '-c', 'a', '-c', 'b', '-c', 'c']);142 expect(program.collect).toEqual(['a', 'b', 'c']);143});144145test('when commaSeparatedList x,y,z then value is [x, y, z]', () => {146 const program = new commander.Command();147 program148 .option('-l, --list <items>', 'comma separated list', commaSeparatedList);149 program.parse(['node', 'test', '--list', 'x,y,z']);150 expect(program.list).toEqual(['x', 'y', 'z']);151});