Skip to content

Commit

Permalink
added assert.isNotEmpty and assert.isDefined
Browse files Browse the repository at this point in the history
  • Loading branch information
nekaab authored and indexzero committed Aug 12, 2011
1 parent 484b5f4 commit c7f9e3c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/assert/macros.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ assert.isEmpty = function (actual, message) {
assert.fail(actual, 0, message || "expected {actual} to be empty", "length", assert.isEmpty);
}
};
assert.isNotEmpty = function (actual, message) {
if ((isObject(actual) && Object.keys(actual).length === 0) || actual.length === 0) {
assert.fail(actual, 0, message || "expected {actual} to be not empty", "length", assert.isNotEmpty);
}
};

assert.length = function (actual, expected, message) {
if (actual.length !== expected) {
Expand Down Expand Up @@ -152,6 +157,11 @@ assert.isUndefined = function (actual, message) {
assert.fail(actual, undefined, message || "expected {actual} to be {expected}", "===", assert.isUndefined);
}
};
assert.isDefined = function (actual, message) {
if(actual === undefined) {
assert.fail(actual, 0, message || "expected {actual} to be defined", "===", assert.isDefined);
}
};
assert.isString = function (actual, message) {
assertTypeOf(actual, 'string', message || "expected {actual} to be a String", assert.isString);
};
Expand Down
16 changes: 16 additions & 0 deletions test/assert-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ vows.describe('vows/assert').addBatch({
assert.length("hello world", 11);
assert.length([1, 2, 3], 3);
},
"`isDefined`": function (assert) {
assert.isDefined(null);
assertError(assert.isDefined, undefined);
},
"`include`": function (assert) {
assert.include("hello world", "world");
assert.include([0, 42, 0], 42);
Expand Down Expand Up @@ -78,6 +82,10 @@ vows.describe('vows/assert').addBatch({
assert.isUndefined(undefined);
assertError(assert.isUndefined, null);
},
"`isDefined`": function (assert) {
assert.isDefined(null);
assertError(assert.isDefined, undefined);
},
"`isNull`": function (assert) {
assert.isNull(null);
assertError(assert.isNull, 0);
Expand All @@ -101,6 +109,14 @@ vows.describe('vows/assert').addBatch({
assert.isEmpty({});
assert.isEmpty([]);
assert.isEmpty("");
},
"`isNotEmpty`": function (assert) {
assert.isNotEmpty({goo:true});
assert.isNotEmpty([1]);
assert.isNotEmpty(" ");
assertError(assert.isNotEmpty, {});
assertError(assert.isNotEmpty, []);
assertError(assert.isNotEmpty, "");
}
}
}).export(module);
Expand Down

0 comments on commit c7f9e3c

Please sign in to comment.