Skip to content

Commit

Permalink
Test coverage (#1433)
Browse files Browse the repository at this point in the history
* Test for just flags and just description

* Edge case test for wrap with trivial input

* Add obscure edge case for coverage

* Add test for variadic argument usage/help

* Suppress tests that uncredited coverage and not currently chasing

* Revert "Suppress tests that uncredited coverage and not currently chasing"

This reverts commit d3c9146.
  • Loading branch information
shadowspawn committed Jan 11, 2021
1 parent ff301fa commit 891e23d
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 1 deletion.
9 changes: 9 additions & 0 deletions tests/args.variadic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,13 @@ describe('variadic argument', () => {
program.command('sub <variadicArg...> [optionalArg]');
}).toThrow("only the last argument can be variadic 'variadicArg'");
});

test('when variadic argument then usage shows variadic', () => {
const program = new commander.Command();
program
.name('foo')
.arguments('[args...]');

expect(program.usage()).toBe('[options] [args...]');
});
});
16 changes: 16 additions & 0 deletions tests/command.helpOption.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,22 @@ describe('helpOption', () => {
expect(helpInformation).toMatch(/-C,--custom-help +custom help output/);
});

test('when helpOption has just flags then helpInformation includes default description', () => {
const program = new commander.Command();
program
.helpOption('-C,--custom-help');
const helpInformation = program.helpInformation();
expect(helpInformation).toMatch(/-C,--custom-help +display help for command/);
});

test('when helpOption has just description then helpInformation includes default flags', () => {
const program = new commander.Command();
program
.helpOption(undefined, 'custom help output');
const helpInformation = program.helpInformation();
expect(helpInformation).toMatch(/-h, --help +custom help output/);
});

test('when helpOption(false) then helpInformation does not include --help', () => {
const program = new commander.Command();
program
Expand Down
9 changes: 8 additions & 1 deletion tests/help.wrap.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,20 @@ const commander = require('../');
// There is some overlap with the higher level Command tests (which predate Help).

describe('wrap', () => {
test('when string fits into width then no wrap', () => {
test('when string fits into width then returns input', () => {
const text = 'a '.repeat(24) + 'a';
const helper = new commander.Help();
const wrapped = helper.wrap(text, 50, 3);
expect(wrapped).toEqual(text);
});

test('when string shorter than indent then returns input', () => {
const text = 'a';
const helper = new commander.Help();
const wrapped = helper.wrap(text, 50, 3);
expect(wrapped).toEqual(text);
});

test('when string exceeds width then wrap', () => {
const text = 'a '.repeat(30) + 'a';
const helper = new commander.Help();
Expand Down
10 changes: 10 additions & 0 deletions tests/options.bool.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,16 @@ describe('boolean flag with non-boolean default', () => {
program.parse(['node', 'test', '--olives']);
expect(program.opts().olives).toBe(flagValue);
});

test('when flag implied and negated then value is false', () => {
const flagValue = 'black';
const program = new commander.Command();
program
.option('-v, --olives', 'Add olives? Sorry we only have black.', flagValue)
.option('--no-olives');
program.parse(['node', 'test', '--olives', '--no-olives']);
expect(program.opts().olives).toBe(false);
});
});

// Regression test for #1301 with `-no-` in middle of option
Expand Down

0 comments on commit 891e23d

Please sign in to comment.