Skip to content
Myr

options.custom-processing.test.js

This file

Path

tests/options.custom-processing.test.js

141 lines · 4.9 KiB · JavaScript

Last changed by

Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c

History of this file

Take it

The walk stopped after 200 commits: lines older than f47bdf5 point at it.

Tidy up example now check for required command arguments (#1320)John Geebefore adoptionf47bdf5

lines 1–33why, one step back
1const 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']);

Change to safe storage of options by default, and change action parameters (#1409)John Geebefore adoptionf76bc71

line 34why, one step back
34 expect(program.opts().integer).toBeUndefined();

Tidy up example now check for required command arguments (#1320)John Geebefore adoptionf47bdf5

lines 35–52why, one step back
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']);

Change to safe storage of options by default, and change action parameters (#1409)John Geebefore adoptionf76bc71

line 53why, one step back
53 expect(program.opts().integer).toBe(startingValue);

Tidy up example now check for required command arguments (#1320)John Geebefore adoptionf47bdf5

lines 54–65why, one step back
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});65

Merge branch 'develop' into release/7.xJohn Geebefore adoptiond90ef12

line 66why, one step back
66test('when option specified then value is as returned from callback', () => {

Tidy up example now check for required command arguments (#1320)John Geebefore adoptionf47bdf5

lines 67–73why, one step back
67 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']);

Change to safe storage of options by default, and change action parameters (#1409)John Geebefore adoptionf76bc71

line 74why, one step back
74 expect(program.opts().integer).toBe(callbackResult);

Tidy up example now check for required command arguments (#1320)John Geebefore adoptionf47bdf5

lines 75–107why, one step back
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});100101// Now some functional tests like the examples in the README!102103test('when parseFloat "1e2" then value is 100', () => {104 const program = new commander.Command();105 program106 .option('-f, --float <number>', 'float argument', parseFloat);107 program.parse(['node', 'test', '-f', '1e2']);

Change to safe storage of options by default, and change action parameters (#1409)John Geebefore adoptionf76bc71

line 108why, one step back
108 expect(program.opts().float).toBe(100);

Tidy up example now check for required command arguments (#1320)John Geebefore adoptionf47bdf5

lines 109–115why, one step back
109});
110111test('when myParseInt "1" then value is 1', () => {112 const program = new commander.Command();113 program114 .option('-i, --integer <number>', 'integer argument', myParseInt);115 program.parse(['node', 'test', '-i', '1']);

Change to safe storage of options by default, and change action parameters (#1409)John Geebefore adoptionf76bc71

line 116why, one step back
116 expect(program.opts().integer).toBe(1);

Tidy up example now check for required command arguments (#1320)John Geebefore adoptionf47bdf5

lines 117–123why, one step back
117});
118119test('when increaseVerbosity -v -v -v then value is 3', () => {120 const program = new commander.Command();121 program122 .option('-v, --verbose', 'verbosity that can be increased', increaseVerbosity, 0);123 program.parse(['node', 'test', '-v', '-v', '-v']);

Change to safe storage of options by default, and change action parameters (#1409)John Geebefore adoptionf76bc71

line 124why, one step back
124 expect(program.opts().verbose).toBe(3);

Tidy up example now check for required command arguments (#1320)John Geebefore adoptionf47bdf5

lines 125–131why, one step back
125});
126127test('when collect -c a -c b -c c then value is [a, b, c]', () => {128 const program = new commander.Command();129 program130 .option('-c, --collect <value>', 'repeatable value', collect, []);131 program.parse(['node', 'test', '-c', 'a', '-c', 'b', '-c', 'c']);

Change to safe storage of options by default, and change action parameters (#1409)John Geebefore adoptionf76bc71

line 132why, one step back
132 expect(program.opts().collect).toEqual(['a', 'b', 'c']);

Tidy up example now check for required command arguments (#1320)John Geebefore adoptionf47bdf5

lines 133–139why, one step back
133});
134135test('when commaSeparatedList x,y,z then value is [x, y, z]', () => {136 const program = new commander.Command();137 program138 .option('-l, --list <items>', 'comma separated list', commaSeparatedList);139 program.parse(['node', 'test', '--list', 'x,y,z']);

Change to safe storage of options by default, and change action parameters (#1409)John Geebefore adoptionf76bc71

line 140why, one step back
140 expect(program.opts().list).toEqual(['x', 'y', 'z']);

Tidy up example now check for required command arguments (#1320)John Geebefore adoptionf47bdf5

line 141why, one step back
141});