Skip to content
Myr

help.suggestion.test.js

This file

Path

tests/help.suggestion.test.js

251 lines · 8.7 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–3why, one step back
1import { test, describe } from 'node:test';
2import assert from 'node:assert/strict';3import { Command, Option } from '../index.js';

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 4–13why, one step back
4
5// Note: setting up shared command configuration in getSuggestion,6// and looking for possible subcommand 'sub'.78function getSuggestion(program, arg) {9 let message = '';10 program11 .showSuggestionAfterError() // make sure on12 .exitOverride()13 .configureOutput({

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 14–16why, one step back
14 writeErr: (str) => {
15 message = str;16 },

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 17–25why, one step back
17 });
18 // Do the same setup for subcommand.19 const sub = program._findCommand('sub');20 if (sub) sub.copyInheritedSettings(program);2122 try {23 // Passing in an array for a few of the tests.24 const args = Array.isArray(arg) ? arg : [arg];25 program.parse(args, { from: 'user' });

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 26–28why, one step back
26 } catch (err) {
27 /* empty */28 }

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 29–33why, one step back
29
30 const match = message.match(/Did you mean (one of )?(.*)\?/);31 return match ? match[2] : null;32}33

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

lines 34–70why, one step back
34describe('Command.showSuggestionAfterError()', () => {
35 describe('command suggestions', () => {36 const commandSuggestionTable = [37 ['yyy', ['zzz'], null, 'none similar'],38 ['a', ['b'], null, 'one edit away but not similar'],39 ['a', ['ab'], 'ab', 'one edit away'],40 ['ab', ['a'], null, 'one edit away'],41 ['at', ['cat'], 'cat', '1 insertion'],42 ['cat', ['at'], 'at', '1 deletion'],43 ['bat', ['cat'], 'cat', '1 substitution'],44 ['act', ['cat'], 'cat', '1 transposition'],45 ['cxx', ['cat'], null, '2 edits away and short string'],46 ['caxx', ['cart'], 'cart', '2 edits away and longer string'],47 [48 '1234567',49 ['1234567890'],50 '1234567890',51 '3 edits away is similar for long string',52 ],53 ['123456', ['1234567890'], null, '4 edits is too far'],54 ['xat', ['rat', 'cat', 'bat'], 'bat, cat, rat', 'sorted possibles'],55 [56 'cart',57 ['camb', 'cant', 'bard'],58 'cant',59 'only closest of different edit distances',60 ],61 ];62 commandSuggestionTable.forEach(63 ([arg, commandNames, expected, description]) => {64 test(`when cli of ${arg} and commands ${JSON.stringify(commandNames)} then suggest ${expected} because ${description}`, () => {65 const program = new Command();66 commandNames.forEach((name) => {67 program.command(name);68 });69 const suggestion = getSuggestion(program, arg);70 assert.equal(suggestion, expected);

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

line 71why, one step back
71 });

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

lines 72–74why, one step back
72 },
73 );74 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 75why, one step back
75

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

lines 76–81why, one step back
76 test('when similar alias then suggest alias', () => {
77 const program = new Command();78 program.command('xyz').alias('car');79 const suggestion = getSuggestion(program, 'bar');80 assert.equal(suggestion, 'car');81 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 82why, one step back
82

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

lines 83–88why, one step back
83 test('when similar hidden alias then not suggested', () => {
84 const program = new Command();85 program.command('xyz').alias('visible').alias('silent');86 const suggestion = getSuggestion(program, 'slent');87 assert.equal(suggestion, null);88 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 89why, one step back
89

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

lines 90–97why, one step back
90 test('when similar command and alias then suggest both', () => {
91 const program = new Command();92 program.command('aaaaa').alias('cat');93 program.command('bat');94 program.command('ccccc');95 const suggestion = getSuggestion(program, 'mat');96 assert.equal(suggestion, 'bat, cat');97 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 98why, one step back
98

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

lines 99–104why, one step back
99 test('when implicit help command then help is candidate for suggestion', () => {
100 const program = new Command();101 program.command('sub');102 const suggestion = getSuggestion(program, 'hepl');103 assert.equal(suggestion, 'help');104 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 105why, one step back
105

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

lines 106–112why, one step back
106 test('when help command disabled then not candidate for suggestion', () => {
107 const program = new Command();108 program.addHelpCommand(false);109 program.command('sub');110 const suggestion = getSuggestion(program, 'hepl');111 assert.equal(suggestion, null);112 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 113why, one step back
113

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

lines 114–118why, one step back
114 test('when default help option then --help is candidate for suggestion', () => {
115 const program = new Command();116 const suggestion = getSuggestion(program, '--hepl');117 assert.equal(suggestion, '--help');118 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 119why, one step back
119

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

lines 120–125why, one step back
120 test('when custom help option then --custom-help is candidate for suggestion', () => {
121 const program = new Command();122 program.helpOption('-H, --custom-help');123 const suggestion = getSuggestion(program, '--custom-hepl');124 assert.equal(suggestion, '--custom-help');125 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 126why, one step back
126

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

lines 127–132why, one step back
127 test('when help option disabled then not candidate for suggestion', () => {
128 const program = new Command();129 program.helpOption(false);130 const suggestion = getSuggestion(program, '--hepl');131 assert.equal(suggestion, null);132 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 133why, one step back
133

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

lines 134–142why, one step back
134 test('when command:* listener and unknown command then no suggestion', () => {
135 // Because one use for command:* was to handle unknown commands.136 // Listener actually stops error being thrown, but we just care about affect on suggestion in this test.137 const program = new Command();138 program.on('command:*', () => {});139 program.command('rat');140 const suggestion = getSuggestion(program, 'cat');141 assert.equal(suggestion, null);142 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 143why, one step back
143

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

lines 144–175why, one step back
144 describe('option suggestions', () => {
145 const optionSuggestionTable = [146 ['--yyy', ['--zzz'], null, 'none similar'],147 ['--a', ['--b'], null, 'one edit away but not similar'],148 ['--a', ['--ab'], '--ab', 'one edit away'],149 ['--ab', ['--a'], null, 'one edit away'],150 ['--at', ['--cat'], '--cat', '1 insertion'],151 ['--cat', ['--at'], '--at', '1 deletion'],152 ['--bat', ['--cat'], '--cat', '1 substitution'],153 ['--act', ['--cat'], '--cat', '1 transposition'],154 ['--cxx', ['--cat'], null, '2 edits away and short string'],155 ['--caxx', ['--cart'], '--cart', '2 edits away and longer string'],156 [157 '--1234567',158 ['--1234567890'],159 '--1234567890',160 '3 edits away is similar for long string',161 ],162 ['--123456', ['--1234567890'], null, '4 edits is too far'],163 [164 '--xat',165 ['--rat', '--cat', '--bat'],166 '--bat, --cat, --rat',167 'sorted possibles',168 ],169 [170 '--cart',171 ['--camb', '--cant', '--bard'],172 '--cant',173 'only closest of different edit distances',174 ],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–185why, one step back
177 optionSuggestionTable.forEach(
178 ([arg, commandNames, expected, description]) => {179 test(`when cli of ${arg} and options ${JSON.stringify(commandNames)} then suggest ${expected} because ${description}`, () => {180 const program = new Command();181 commandNames.forEach((name) => {182 program.option(name);183 });184 const suggestion = getSuggestion(program, arg);185 assert.equal(suggestion, expected);

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

line 186why, one step back
186 });

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

lines 187–189why, one step back
187 },
188 );189 });

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

line 190why, one step back
190

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

lines 191–197why, one step back
191 test('when no options then no suggestion', () => {
192 // Checking nothing blows up as much as no suggestion!193 const program = new Command();194 program.helpOption(false);195 const suggestion = getSuggestion(program, '--option');196 assert.equal(suggestion, null);197 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 198why, one step back
198

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

lines 199–204why, one step back
199 test('when subcommand option then candidate for subcommand option suggestion', () => {
200 const program = new Command();201 program.command('sub').option('-l,--local');202 const suggestion = getSuggestion(program, ['sub', '--loca']);203 assert.equal(suggestion, '--local');204 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 205why, one step back
205

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

lines 206–212why, one step back
206 test('when global option then candidate for subcommand option suggestion', () => {
207 const program = new Command();208 program.option('-g, --global');209 program.command('sub');210 const suggestion = getSuggestion(program, ['sub', '--globla']);211 assert.equal(suggestion, '--global');212 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 213why, one step back
213

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

lines 214–221why, one step back
214 test('when global option but positionalOptions then not candidate for subcommand suggestion', () => {
215 const program = new Command();216 program.enablePositionalOptions();217 program.option('-g, --global');218 program.command('sub');219 const suggestion = getSuggestion(program, ['sub', '--globla']);220 assert.equal(suggestion, null);221 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 222why, one step back
222

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

lines 223–229why, one step back
223 test('when global and local options then both candidates', () => {
224 const program = new Command();225 program.option('--cat');226 program.command('sub').option('--rat');227 const suggestion = getSuggestion(program, ['sub', '--bat']);228 assert.equal(suggestion, '--cat, --rat');229 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 230why, one step back
230

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

lines 231–236why, one step back
231 test('when command hidden then not suggested as candidate', () => {
232 const program = new Command();233 program.command('secret', { hidden: true });234 const suggestion = getSuggestion(program, 'secrt');235 assert.equal(suggestion, null);236 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 237why, one step back
237

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

lines 238–243why, one step back
238 test('when option hidden then not suggested as candidate', () => {
239 const program = new Command();240 program.addOption(new Option('--secret').hideHelp());241 const suggestion = getSuggestion(program, '--secrt');242 assert.equal(suggestion, null);243 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 244why, one step back
244

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

lines 245–250why, one step back
245 test('when may be duplicate identical candidates then only return one', () => {
246 const program = new Command();247 program.command('sub');248 const suggestion = getSuggestion(program, ['sub', '--hepl']);249 assert.equal(suggestion, '--help');250 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 251why, one step back
251});