Skip to content
Myr

argument.variadic.test.js

This file

Path

tests/argument.variadic.test.js

133 lines · 4.6 KiB · JavaScript

Last changed by

Merge from develop into release/15.x (with minor conflicts and test migrations)John Geebefore adoption26e3ba3

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–3why, one step back
1import * 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–6why, one step back
4
5// Testing variadic arguments. Testing all the action arguments, but could test just variadicArg.6

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

line 7why, one step back
7describe('Command variadic argument using .argument()', (t) => {

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

lines 8–9why, one step back
8 test('when no extra arguments specified for program then variadic arg is empty array', (t) => {
9 const actionMock = t.mock.fn();

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 10why, one step back
10 const program = new commander.Command();

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 11why, one step back
11 program.argument('<id>').argument('[variadicArg...]').action(actionMock);

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 12–14why, one step back
12
13 program.parse(['node', 'test', 'id']);14

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

lines 15–17why, one step back
15 const callArgs = actionMock.mock.calls[0].arguments;
16 assert.equal(callArgs[0], 'id');17 assert.deepEqual(callArgs[1], []);

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 18–19why, one step back
18 });
19

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

lines 20–21why, one step back
20 test('when extra arguments specified for program then variadic arg is array of values', (t) => {
21 const actionMock = t.mock.fn();

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 22–30why, one step back
22 const program = new commander.Command();
23 program24 .addArgument(new commander.Argument('<id>'))25 .argument('[variadicArg...]')26 .action(actionMock);27 const extraArguments = ['a', 'b', 'c'];2829 program.parse(['node', 'test', 'id', ...extraArguments]);30

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

lines 31–33why, one step back
31 const callArgs = actionMock.mock.calls[0].arguments;
32 assert.equal(callArgs[0], 'id');33 assert.deepEqual(callArgs[1], extraArguments);

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 34–35why, one step back
34 });
35

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

lines 36–37why, one step back
36 test('when no extra arguments specified for command then variadic arg is empty array', (t) => {
37 const actionMock = t.mock.fn();

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 38why, one step back
38 const program = new commander.Command();

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 39why, one step back
39 const cmd = program.command('sub [variadicArg...]').action(actionMock);

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 40–42why, one step back
40
41 program.parse(['node', 'test', 'sub']);42

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

lines 43–44why, one step back
43 const callArgs = actionMock.mock.calls[0].arguments;
44 assert.deepEqual(callArgs[0], []);

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 45–46why, one step back
45 });
46

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

lines 47–48why, one step back
47 test('when extra arguments specified for command then variadic arg is array of values', (t) => {
48 const actionMock = t.mock.fn();

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 49why, one step back
49 const program = new commander.Command();

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 50why, one step back
50 const cmd = program.command('sub [variadicArg...]').action(actionMock);

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 51–54why, one step back
51 const extraArguments = ['a', 'b', 'c'];
5253 program.parse(['node', 'test', 'sub', ...extraArguments]);54

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

lines 55–56why, one step back
55 const callArgs = actionMock.mock.calls[0].arguments;
56 assert.deepEqual(callArgs[0], extraArguments);

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 57–61why, one step back
57 });
5859 test('when program variadic argument not last then error', () => {60 const program = new commander.Command();61

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

line 62why, one step back
62 assert.throws(() => {

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 63why, one step back
63 program.argument('<variadicArg...>').argument('[optionalArg]');

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

line 64why, one step back
64 }, /only the last argument can be variadic 'variadicArg'/);

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 65–69why, one step back
65 });
6667 test('when command variadic argument not last then error', () => {68 const program = new commander.Command();69

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

line 70why, one step back
70 assert.throws(() => {

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 71why, one step back
71 program.command('sub <variadicArg...> [optionalArg]');

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

line 72why, one step back
72 }, /only the last argument can be variadic 'variadicArg'/);

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 73–76why, one step back
73 });
7475 test('when variadic argument then usage shows variadic', () => {76 const program = new commander.Command();

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 77why, one step back
77 program.name('foo').argument('[args...]');

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 78why, one step back
78

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

line 79why, one step back
79 assert.equal(program.usage(), '[options] [args...]');

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 80–86why, one step back
80 });
8182 test('when variadic used with choices and one value then set in array', () => {83 const program = new commander.Command();84 let passedArg;85 program86 .addArgument(new commander.Argument('<value...>').choices(['one', 'two']))

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 87–89why, one step back
87 .action((value) => {
88 passedArg = value;89 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 90–91why, one step back
90
91 program.parse(['one'], { from: 'user' });

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

line 92why, one step back
92 assert.deepEqual(passedArg, ['one']);

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 93–99why, one step back
93 });
9495 test('when variadic used with choices and two values then set in array', () => {96 const program = new commander.Command();97 let passedArg;98 program99 .addArgument(new commander.Argument('<value...>').choices(['one', 'two']))

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 100–102why, one step back
100 .action((value) => {
101 passedArg = value;102 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 103–104why, one step back
103
104 program.parse(['one', 'two'], { from: 'user' });

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

line 105why, one step back
105 assert.deepEqual(passedArg, ['one', 'two']);

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 106why, one step back
106 });

Merge from develop (#2482)John Geebefore adoptionb51c0ea

lines 107–119why, one step back
107
108 test('when variadic has default array then specified value is used instead of default (not appended)', () => {109 const program = new commander.Command();110 let passedArg;111 program112 .addArgument(new commander.Argument('[value...]').default(['DEFAULT']))113 .action((value) => {114 passedArg = value;115 });116117 program.parse(['one', 'two'], { from: 'user' });118 assert.deepEqual(passedArg, ['one', 'two']);119 });

Merge from develop into release/15.x (with minor conflicts and test migrations)John Geebefore adoption26e3ba3

lines 120–132why, one step back
120
121 test('when variadic has default array then specified value is used instead of default (not appended)', () => {122 const program = new commander.Command();123 let passedArg;124 program125 .addArgument(new commander.Argument('[value...]').default(['DEFAULT']))126 .action((value) => {127 passedArg = value;128 });129130 program.parse(['one', 'two'], { from: 'user' });131 assert.deepEqual(passedArg, ['one', 'two']);132 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 133why, one step back
133});