1
0
mirror of https://github.com/thangisme/notes.git synced 2024-11-01 01:37:37 -04:00
notes/node_modules/yargs/test/parse_defaults.js
Patrick Marsceill b7b0d0d7bf
Initial commit
2017-03-09 13:16:08 -05:00

83 lines
2.6 KiB
JavaScript

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);
});
});
});