Skip to content

Commit

Permalink
test coverage for the falsy check() usage
Browse files Browse the repository at this point in the history
  • Loading branch information
James Halliday committed Nov 7, 2010
1 parent a4843a9 commit 56b2b1d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name" : "optimist",
"version" : "0.0.5",
"version" : "0.0.6",
"description" : "Light-weight option parsing with an argv hash. No optstrings attached.",
"modules" : {
"index" : "./lib/optimist.js",
Expand Down
44 changes: 43 additions & 1 deletion test/usage.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ exports.checkPass = function (assert) {
return optimist('-x 10 -y 20'.split(' '))
.usage('Usage: $0 -x NUM -y NUM')
.check(function (argv) {
return 'x' in argv && 'y' in argv
if (!('x' in argv)) throw 'You forgot about -x';
if (!('y' in argv)) throw 'You forgot about -y';
})
.argv;
});
Expand Down Expand Up @@ -66,6 +67,47 @@ exports.checkFail = function (assert) {
});
};

exports.checkCondPass = function (assert) {
function checker (argv) {
return 'x' in argv && 'y' in argv;
}

var r = checkUsage(function () {
return optimist('-x 10 -y 20'.split(' '))
.usage('Usage: $0 -x NUM -y NUM')
.check(checker)
.argv;
});
assert.deepEqual(r, {
result : { x : 10, y : 20, _ : [], $0 : './usage' },
errors : [],
logs : [],
exit : false,
});
};

exports.checkCondFail = function (assert) {
function checker (argv) {
return 'x' in argv && 'y' in argv;
}

var r = checkUsage(function () {
return optimist('-x 10 -z 20'.split(' '))
.usage('Usage: $0 -x NUM -y NUM')
.check(checker)
.argv;
});
assert.deepEqual(r, {
result : { x : 10, z : 20, _ : [], $0 : './usage' },
errors : [
'Usage: ./usage -x NUM -y NUM',
'Argument check failed: ' + checker.toString()
],
logs : [],
exit: true,
});
};

exports.countPass = function (assert) {
var r = checkUsage(function () {
return optimist('1 2 3 --moo'.split(' '))
Expand Down

0 comments on commit 56b2b1d

Please sign in to comment.