Skip to content
Myr

argument.custom-processing.test.js

This file

Path

tests/argument.custom-processing.test.js

195 lines · 7.3 KiB · JavaScript

Last changed by

Switching to esm-only (#2464)John Geebefore adoptionf4bd470

History of this file

Take it

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

Switching to esm-only (#2464)John Geebefore adoptionf4bd470

lines 1–4why, one step back
1import * as commander from '../index.js';
2import { createTestCommand } from './testHelpers.js';3import { test, describe } from 'node:test';4import assert from 'node:assert/strict';

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 5–8why, one step back
5
6// Testing default value and custom processing behaviours.7// Some double assertions in tests to check action argument and .processedArg8

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 9–16why, one step back
9describe('custom processing function for command-argument', () => {
10 test('when argument not specified then callback not called', (t) => {11 const mockCoercion = t.mock.fn();12 const program = new commander.Command();13 program.argument('[n]', 'number', mockCoercion).action(() => {});14 program.parse([], { from: 'user' });15 assert.equal(mockCoercion.mock.callCount(), 0);16 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 17why, one step back
17

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 18–25why, one step back
18 test('when argument not specified then action argument undefined', () => {
19 let actionValue = 'foo';20 const program = new commander.Command();21 program.argument('[n]', 'number', parseFloat).action((arg) => {22 actionValue = arg;23 });24 program.parse([], { from: 'user' });25 assert.equal(actionValue, undefined);

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 26why, one step back
26 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 27why, one step back
27

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 28–34why, one step back
28 test('when custom with starting value and argument not specified then callback not called', (t) => {
29 const mockCoercion = t.mock.fn();30 const program = new commander.Command();31 program.argument('[n]', 'number', parseFloat, 1).action(() => {});32 program.parse([], { from: 'user' });33 assert.equal(mockCoercion.mock.callCount(), 0);34 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 35why, one step back
35

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 36–47why, one step back
36 test('when custom with starting value and argument not specified with action handler then action argument is starting value', () => {
37 const startingValue = 1;38 let actionValue;39 const program = new commander.Command();40 program41 .argument('[n]', 'number', parseFloat, startingValue)42 .action((arg) => {43 actionValue = arg;44 });45 program.parse([], { from: 'user' });46 assert.equal(actionValue, startingValue);47 assert.deepEqual(program.processedArgs, [startingValue]);

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 48why, one step back
48 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 49why, one step back
49

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 50–56why, one step back
50 test('when custom with starting value and argument not specified without action handler then .processedArgs has starting value', () => {
51 const startingValue = 1;52 const program = new commander.Command();53 program.argument('[n]', 'number', parseFloat, startingValue);54 program.parse([], { from: 'user' });55 assert.deepEqual(program.processedArgs, [startingValue]);56 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 57why, one step back
57

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 58–67why, one step back
58 test('when default value is defined (without custom processing) and argument not specified with action handler then action argument is default value', () => {
59 const defaultValue = 1;60 let actionValue;61 const program = new commander.Command();62 program.argument('[n]', 'number', defaultValue).action((arg) => {63 actionValue = arg;64 });65 program.parse([], { from: 'user' });66 assert.equal(actionValue, defaultValue);67 assert.deepEqual(program.processedArgs, [defaultValue]);

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 68why, one step back
68 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 69why, one step back
69

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 70–76why, one step back
70 test('when default value is defined (without custom processing) and argument not specified without action handler then .processedArgs is default value', () => {
71 const defaultValue = 1;72 const program = new commander.Command();73 program.argument('[n]', 'number', defaultValue);74 program.parse([], { from: 'user' });75 assert.deepEqual(program.processedArgs, [defaultValue]);76 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 77why, one step back
77

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 78–85why, one step back
78 test('when argument specified then callback called with value', (t) => {
79 const mockCoercion = t.mock.fn();80 const value = '1';81 const program = new commander.Command();82 program.argument('[n]', 'number', mockCoercion).action(() => {});83 program.parse([value], { from: 'user' });84 assert.deepEqual(mockCoercion.mock.calls[0].arguments, [value, undefined]);85 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 86why, one step back
86

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 87–106why, one step back
87 test('when argument specified with action handler then action value is as returned from callback', () => {
88 const callbackResult = 2;89 let actionValue;90 const program = new commander.Command();91 program92 .argument('[n]', 'number', () => {93 return callbackResult;94 })95 .action((arg) => {96 actionValue = arg;97 });98 program.parse(['node', 'test', 'alpha']);99 assert.equal(actionValue, callbackResult);100 assert.deepEqual(program.processedArgs, [callbackResult]);101 });102103 test('when argument specified without action handler then .processedArgs is as returned from callback', () => {104 const callbackResult = 2;105 const program = new commander.Command();106 program.argument('[n]', 'number', () => {

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 107–108why, one step back
107 return callbackResult;
108 });

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 109–111why, one step back
109 program.parse(['node', 'test', 'alpha']);
110 assert.deepEqual(program.processedArgs, [callbackResult]);111 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 112why, one step back
112

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 113–123why, one step back
113 test('when argument specified then program.args has original rather than custom', () => {
114 // This is as intended, so check behaviour.115 const callbackResult = 2;116 const program = new commander.Command();117 program118 .argument('[n]', 'number', () => {119 return callbackResult;120 })121 .action(() => {});122 program.parse(['node', 'test', 'alpha']);123 assert.deepEqual(program.args, ['alpha']);

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 124why, one step back
124 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 125why, one step back
125

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 126–138why, one step back
126 test('when custom with starting value and argument specified then callback called with value and starting value', (t) => {
127 const mockCoercion = t.mock.fn();128 const startingValue = 1;129 const value = '2';130 const program = new commander.Command()131 .argument('[n]', 'number', mockCoercion, startingValue)132 .action(() => {});133 program.parse(['node', 'test', value]);134 assert.deepEqual(mockCoercion.mock.calls[0].arguments, [135 value,136 startingValue,137 ]);138 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 139why, one step back
139

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 140–150why, one step back
140 test('when variadic argument specified multiple times then callback called with value and previousValue', (t) => {
141 const mockCoercion = t.mock.fn(() => {142 return 'callback';143 });144 const program = new commander.Command();145 program.argument('<n...>', 'number', mockCoercion).action(() => {});146 program.parse(['1', '2'], { from: 'user' });147 assert.equal(mockCoercion.mock.callCount(), 2);148 assert.deepEqual(mockCoercion.mock.calls[0].arguments, ['1', undefined]);149 assert.deepEqual(mockCoercion.mock.calls[1].arguments, ['2', 'callback']);150 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 151why, one step back
151

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 152–156why, one step back
152 test('when variadic argument without action handler then .processedArg has array', () => {
153 const program = new commander.Command();154 program.argument('<n...>', 'number');155 program.parse(['1', '2'], { from: 'user' });156 assert.deepEqual(program.processedArgs, [['1', '2']]);

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 157–158why, one step back
157 });
158

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 159–167why, one step back
159 test('when parseFloat "1e2" then action argument is 100', () => {
160 let actionValue;161 const program = new commander.Command();162 program.argument('<number>', 'float argument', parseFloat).action((arg) => {163 actionValue = arg;164 });165 program.parse(['1e2'], { from: 'user' });166 assert.equal(actionValue, 100);167 assert.deepEqual(program.processedArgs, [actionValue]);

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 168why, one step back
168 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 169why, one step back
169

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 170–174why, one step back
170 test('when defined default value for required argument then throw', () => {
171 const program = new commander.Command();172 assert.throws(() => {173 program.argument('<number>', 'float argument', 4);174 });

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

line 175why, one step back
175 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 176why, one step back
176

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 177–182why, one step back
177 test('when custom processing for argument throws plain error then not CommanderError caught', () => {
178 function justSayNo(value) {179 throw new Error('plain error');180 }181 const program = createTestCommand();182 program.argument('[n]', 'number', justSayNo).action(() => {});

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 183why, one step back
183

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 184–194why, one step back
184 assert.throws(
185 () => {186 program.parse(['green'], { from: 'user' });187 },188 (err) => {189 assert.equal(err instanceof Error, true);190 assert.equal(err instanceof commander.CommanderError, false);191 return true;192 },193 );194 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 195why, one step back
195});