-
Notifications
You must be signed in to change notification settings - Fork 991
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix a couple of bad interactions between aliases and defaults
- Alias + boolean + default is broken (#7) - Default values and options with dashes not working (#20)
- Loading branch information
Showing
3 changed files
with
126 additions
and
0 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,82 @@ | ||
var should = require('chai').should(), | ||
yargs = require('../'); | ||
|
||
describe('parse', function () { | ||
|
||
describe('defaults', function () { | ||
function checkNoArgs(argv, hasAlias) { | ||
it('should set defaults if no args', function() { | ||
var result = argv.parse([ ]); | ||
result.should.have.property('flag', true); | ||
if (hasAlias) { | ||
result.should.have.property('f', true); | ||
} | ||
}); | ||
} | ||
|
||
function checkExtraArg(argv, hasAlias) { | ||
it('should set defaults if one extra arg', function() { | ||
var result = argv.parse([ 'extra' ]); | ||
result.should.have.property('flag', true); | ||
result.should.have.property('_').and.deep.equal(['extra']); | ||
if (hasAlias) { | ||
result.should.have.property('f', true); | ||
} | ||
}); | ||
} | ||
|
||
function checkStringArg(argv, hasAlias) { | ||
it('should set defaults even if arg looks like a string', function() { | ||
var result = argv.parse([ '--flag', 'extra' ]); | ||
result.should.have.property('flag', true); | ||
result.should.have.property('_').and.deep.equal(['extra']); | ||
if (hasAlias) { | ||
result.should.have.property('f', true); | ||
} | ||
}); | ||
} | ||
|
||
describe('for options with aliases', function () { | ||
var args = yargs().options({ | ||
flag : { | ||
alias : 'f', | ||
default : true | ||
} | ||
}); | ||
|
||
checkNoArgs(args, true); | ||
checkExtraArg(args, true); | ||
// This test case should fail, because we didn't specify that the | ||
// option is a boolean | ||
// checkStringArg(args, true); | ||
}); | ||
|
||
describe('for typed options without aliases', function () { | ||
var args = yargs().options({ | ||
flag : { | ||
type : 'boolean', | ||
default : true | ||
} | ||
}); | ||
|
||
checkNoArgs(args); | ||
checkExtraArg(args); | ||
checkStringArg(args); | ||
}); | ||
|
||
describe('for typed options with aliases', function () { | ||
var args = yargs().options({ | ||
flag : { | ||
alias : 'f', | ||
type : 'boolean', | ||
default : true | ||
} | ||
}); | ||
|
||
checkNoArgs(args, true); | ||
checkExtraArg(args, true); | ||
checkStringArg(args, true); | ||
}); | ||
}); | ||
|
||
}); |