options.custom-processing.test.js
This file
Path
tests/options.custom-processing.test.js
142 lines · 5.3 KiB · JavaScript
Last changed by
Switching to esm-only (#2464)John Geebefore adoptionf4bd470
History of this fileTake it
The walk stopped after 200 commits: lines older than 471ec40 point at it.
Switching to esm-only (#2464)John Geebefore adoptionf4bd470
lines 1–3why, one step back1import * as commander from '../index.js';2import { test, describe } from 'node:test';3import assert from 'node:assert/strict';Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 4–21why, one step back45function myParseInt(value, dummyPrevious) {6 // parseInt takes a string and a radix7 return parseInt(value, 10);8}910function increaseVerbosity(dummyValue, previous) {11 return previous + 1;12}1314function collect(value, previous) {15 return previous.concat([value]);16}1718function commaSeparatedList(value, dummyPrevious) {19 return value.split(',');20}21Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 22–29why, one step back22describe('custom processing function for option', () => {23 test('when option not specified then callback not called', (t) => {24 const mockCoercion = t.mock.fn();25 const program = new commander.Command();26 program.option('-i, --integer <n>', 'number', mockCoercion);27 program.parse(['node', 'test']);28 assert.equal(mockCoercion.mock.callCount(), 0);29 });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 30why, one step back30Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 31–36why, one step back31 test('when option not specified then value is undefined', () => {32 const program = new commander.Command();33 program.option('-i, --integer <n>', 'number', myParseInt);34 program.parse(['node', 'test']);35 assert.equal(program.opts().integer, undefined);36 });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 37why, one step back37Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 38–44why, one step back38 test('when starting value is defined and option not specified then callback not called', (t) => {39 const mockCoercion = t.mock.fn();40 const program = new commander.Command();41 program.option('-i, --integer <n>', 'number', mockCoercion, 1);42 program.parse(['node', 'test']);43 assert.equal(mockCoercion.mock.callCount(), 0);44 });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 45why, one step back45Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 46–53why, one step back46 test('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 program.option('-i, --integer <n>', 'number', myParseInt, startingValue);51 program.parse(['node', 'test']);52 assert.equal(program.opts().integer, startingValue);53 });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 54why, one step back54Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 55–62why, one step back55 test('when option specified then callback called with value and previous', (t) => {56 const mockCoercion = t.mock.fn();57 const value = '1';58 const program = new commander.Command();59 program.option('-i, --integer <n>', 'number', mockCoercion);60 program.parse(['node', 'test', '-i', value]);61 assert.deepEqual(mockCoercion.mock.calls[0].arguments, [value, undefined]);62 });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 63why, one step back63Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 64–71why, one step back64 test('when option specified then value is as returned from callback', () => {65 const callbackResult = 2;66 const program = new commander.Command();67 program.option('-i, --integer <n>', 'number', () => {68 return callbackResult;69 });70 program.parse(['node', 'test', '-i', '0']);71 assert.equal(program.opts().integer, callbackResult);Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 72why, one step back72 });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 73why, one step back73Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 74–85why, one step back74 test('when starting value is defined and option specified then callback called with value and starting value', (t) => {75 const mockCoercion = t.mock.fn();76 const startingValue = 1;77 const value = '2';78 const program = new commander.Command();79 program.option('-i, --integer <n>', 'number', mockCoercion, startingValue);80 program.parse(['node', 'test', '-i', value]);81 assert.deepEqual(mockCoercion.mock.calls[0].arguments, [82 value,83 startingValue,84 ]);85 });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 86why, one step back86Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 87–95why, one step back87 test('when option specified multiple times then callback called with value and previousValue', (t) => {88 const mockCoercion = t.mock.fn(() => 'callback');89 const program = new commander.Command();90 program.option('-i, --integer <n>', 'number', mockCoercion);91 program.parse(['node', 'test', '-i', '1', '-i', '2']);92 assert.equal(mockCoercion.mock.callCount(), 2);93 assert.deepEqual(mockCoercion.mock.calls[0].arguments, ['1', undefined]);94 assert.deepEqual(mockCoercion.mock.calls[1].arguments, ['2', 'callback']);95 });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 96why, one step back96Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
line 97why, one step back97 // Now some functional tests like the examples in the README!Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 98why, one step back98Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 99–104why, one step back99 test('when parseFloat "1e2" then value is 100', () => {100 const program = new commander.Command();101 program.option('-f, --float <number>', 'float argument', parseFloat);102 program.parse(['node', 'test', '-f', '1e2']);103 assert.equal(program.opts().float, 100);104 });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 105why, one step back105Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 106–111why, one step back106 test('when myParseInt "1" then value is 1', () => {107 const program = new commander.Command();108 program.option('-i, --integer <number>', 'integer argument', myParseInt);109 program.parse(['node', 'test', '-i', '1']);110 assert.equal(program.opts().integer, 1);111 });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 112why, one step back112Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 113–123why, one step back113 test('when increaseVerbosity -v -v -v then value is 3', () => {114 const program = new commander.Command();115 program.option(116 '-v, --verbose',117 'verbosity that can be increased',118 increaseVerbosity,119 0,120 );121 program.parse(['node', 'test', '-v', '-v', '-v']);122 assert.equal(program.opts().verbose, 3);123 });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 124why, one step back124Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 125–130why, one step back125 test('when collect -c a -c b -c c then value is [a, b, c]', () => {126 const program = new commander.Command();127 program.option('-c, --collect <value>', 'repeatable value', collect, []);128 program.parse(['node', 'test', '-c', 'a', '-c', 'b', '-c', 'c']);129 assert.deepEqual(program.opts().collect, ['a', 'b', 'c']);130 });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 131why, one step back131Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 132–141why, one step back132 test('when commaSeparatedList x,y,z then value is [x, y, z]', () => {133 const program = new commander.Command();134 program.option(135 '-l, --list <items>',136 'comma separated list',137 commaSeparatedList,138 );139 program.parse(['node', 'test', '--list', 'x,y,z']);140 assert.deepEqual(program.opts().list, ['x', 'y', 'z']);141 });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 142why, one step back142});