Skip to content

Commit c7f9e3c

Browse files
nekaabindexzero
authored andcommitted
added assert.isNotEmpty and assert.isDefined
1 parent 484b5f4 commit c7f9e3c

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

lib/assert/macros.js

+10
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ assert.isEmpty = function (actual, message) {
104104
assert.fail(actual, 0, message || "expected {actual} to be empty", "length", assert.isEmpty);
105105
}
106106
};
107+
assert.isNotEmpty = function (actual, message) {
108+
if ((isObject(actual) && Object.keys(actual).length === 0) || actual.length === 0) {
109+
assert.fail(actual, 0, message || "expected {actual} to be not empty", "length", assert.isNotEmpty);
110+
}
111+
};
107112

108113
assert.length = function (actual, expected, message) {
109114
if (actual.length !== expected) {
@@ -152,6 +157,11 @@ assert.isUndefined = function (actual, message) {
152157
assert.fail(actual, undefined, message || "expected {actual} to be {expected}", "===", assert.isUndefined);
153158
}
154159
};
160+
assert.isDefined = function (actual, message) {
161+
if(actual === undefined) {
162+
assert.fail(actual, 0, message || "expected {actual} to be defined", "===", assert.isDefined);
163+
}
164+
};
155165
assert.isString = function (actual, message) {
156166
assertTypeOf(actual, 'string', message || "expected {actual} to be a String", assert.isString);
157167
};

test/assert-test.js

+16
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ vows.describe('vows/assert').addBatch({
1616
assert.length("hello world", 11);
1717
assert.length([1, 2, 3], 3);
1818
},
19+
"`isDefined`": function (assert) {
20+
assert.isDefined(null);
21+
assertError(assert.isDefined, undefined);
22+
},
1923
"`include`": function (assert) {
2024
assert.include("hello world", "world");
2125
assert.include([0, 42, 0], 42);
@@ -78,6 +82,10 @@ vows.describe('vows/assert').addBatch({
7882
assert.isUndefined(undefined);
7983
assertError(assert.isUndefined, null);
8084
},
85+
"`isDefined`": function (assert) {
86+
assert.isDefined(null);
87+
assertError(assert.isDefined, undefined);
88+
},
8189
"`isNull`": function (assert) {
8290
assert.isNull(null);
8391
assertError(assert.isNull, 0);
@@ -101,6 +109,14 @@ vows.describe('vows/assert').addBatch({
101109
assert.isEmpty({});
102110
assert.isEmpty([]);
103111
assert.isEmpty("");
112+
},
113+
"`isNotEmpty`": function (assert) {
114+
assert.isNotEmpty({goo:true});
115+
assert.isNotEmpty([1]);
116+
assert.isNotEmpty(" ");
117+
assertError(assert.isNotEmpty, {});
118+
assertError(assert.isNotEmpty, []);
119+
assertError(assert.isNotEmpty, "");
104120
}
105121
}
106122
}).export(module);

0 commit comments

Comments
 (0)