-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Factor out copySettings so can be used in programs using addCommand. * Fill out tests for properties for copySettings * Add TypeScript * Rename
- Loading branch information
1 parent
80054ba
commit 5517d25
Showing
5 changed files
with
181 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
const commander = require('../'); | ||
|
||
// Tests some private properties as simpler than pure tests of observable behaviours. | ||
// Testing before and after values in some cases, to ensure value actually changes (when copied). | ||
|
||
test('when add subcommand with .command() then calls copyInheritedSettings from parent', () => { | ||
const program = new commander.Command(); | ||
|
||
// This is a bit intrusive, but check expectation that copyInheritedSettings is called internally. | ||
const copySettingMock = jest.fn(); | ||
program.createCommand = (name) => { | ||
const cmd = new commander.Command(name); | ||
cmd.copyInheritedSettings = copySettingMock; | ||
return cmd; | ||
}; | ||
program.command('sub'); | ||
|
||
expect(copySettingMock).toHaveBeenCalledWith(program); | ||
}); | ||
|
||
describe('copyInheritedSettings property tests', () => { | ||
test('when copyInheritedSettings then copies outputConfiguration(config)', () => { | ||
const source = new commander.Command(); | ||
const cmd = new commander.Command(); | ||
|
||
source.configureOutput({ foo: 'bar' }); | ||
cmd.copyInheritedSettings(source); | ||
expect(cmd.configureOutput().foo).toEqual('bar'); | ||
}); | ||
|
||
test('when copyInheritedSettings then copies helpOption(false)', () => { | ||
const source = new commander.Command(); | ||
const cmd = new commander.Command(); | ||
expect(cmd._hasHelpOption).toBeTruthy(); | ||
|
||
source.helpOption(false); | ||
cmd.copyInheritedSettings(source); | ||
expect(cmd._hasHelpOption).toBeFalsy(); | ||
}); | ||
|
||
test('when copyInheritedSettings then copies helpOption(flags, description)', () => { | ||
const source = new commander.Command(); | ||
const cmd = new commander.Command(); | ||
|
||
source.helpOption('-Z, --zz', 'ddd'); | ||
cmd.copyInheritedSettings(source); | ||
expect(cmd._helpFlags).toBe('-Z, --zz'); | ||
expect(cmd._helpDescription).toBe('ddd'); | ||
expect(cmd._helpShortFlag).toBe('-Z'); | ||
expect(cmd._helpLongFlag).toBe('--zz'); | ||
}); | ||
|
||
test('when copyInheritedSettings then copies addHelpCommand(name, description)', () => { | ||
const source = new commander.Command(); | ||
const cmd = new commander.Command(); | ||
|
||
source.addHelpCommand('HELP [cmd]', 'ddd'); | ||
cmd.copyInheritedSettings(source); | ||
expect(cmd._helpCommandName).toBe('HELP'); | ||
expect(cmd._helpCommandnameAndArgs).toBe('HELP [cmd]'); | ||
expect(cmd._helpCommandDescription).toBe('ddd'); | ||
}); | ||
|
||
test('when copyInheritedSettings then copies configureHelp(config)', () => { | ||
const source = new commander.Command(); | ||
const cmd = new commander.Command(); | ||
|
||
const configuration = { foo: 'bar', helpWidth: 123, sortSubcommands: true }; | ||
source.configureHelp(configuration); | ||
cmd.copyInheritedSettings(source); | ||
expect(cmd.configureHelp()).toEqual(configuration); | ||
}); | ||
|
||
test('when copyInheritedSettings then copies exitOverride()', () => { | ||
const source = new commander.Command(); | ||
const cmd = new commander.Command(); | ||
|
||
expect(cmd._exitCallback).toBeFalsy(); | ||
source.exitOverride(); | ||
cmd.copyInheritedSettings(source); | ||
expect(cmd._exitCallback).toBeTruthy(); // actually a function | ||
}); | ||
|
||
test('when copyInheritedSettings then copies storeOptionsAsProperties()', () => { | ||
const source = new commander.Command(); | ||
const cmd = new commander.Command(); | ||
|
||
expect(cmd._storeOptionsAsProperties).toBeFalsy(); | ||
source.storeOptionsAsProperties(); | ||
cmd.copyInheritedSettings(source); | ||
expect(cmd._storeOptionsAsProperties).toBeTruthy(); | ||
}); | ||
|
||
test('when copyInheritedSettings then copies combineFlagAndOptionalValue()', () => { | ||
const source = new commander.Command(); | ||
const cmd = new commander.Command(); | ||
|
||
expect(cmd._combineFlagAndOptionalValue).toBeTruthy(); | ||
source.combineFlagAndOptionalValue(false); | ||
cmd.copyInheritedSettings(source); | ||
expect(cmd._combineFlagAndOptionalValue).toBeFalsy(); | ||
}); | ||
|
||
test('when copyInheritedSettings then copies allowExcessArguments()', () => { | ||
const source = new commander.Command(); | ||
const cmd = new commander.Command(); | ||
|
||
expect(cmd._allowExcessArguments).toBeTruthy(); | ||
source.allowExcessArguments(false); | ||
cmd.copyInheritedSettings(source); | ||
expect(cmd._allowExcessArguments).toBeFalsy(); | ||
}); | ||
|
||
test('when copyInheritedSettings then copies enablePositionalOptions()', () => { | ||
const source = new commander.Command(); | ||
const cmd = new commander.Command(); | ||
|
||
expect(cmd._enablePositionalOptions).toBeFalsy(); | ||
source.enablePositionalOptions(); | ||
cmd.copyInheritedSettings(source); | ||
expect(cmd._enablePositionalOptions).toBeTruthy(); | ||
}); | ||
|
||
test('when copyInheritedSettings then copies showHelpAfterError()', () => { | ||
const source = new commander.Command(); | ||
const cmd = new commander.Command(); | ||
|
||
expect(cmd._showHelpAfterError).toBeFalsy(); | ||
source.showHelpAfterError(); | ||
cmd.copyInheritedSettings(source); | ||
expect(cmd._showHelpAfterError).toBeTruthy(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters