Permalink
Browse files

Added object support for `.empty`.

Own properties are counted if there's no numeric `length`
property on the object.
  • Loading branch information...
1 parent cb9bece commit d1a9689ea1af7502914638c9fe95a545fbde292b @rauchg rauchg committed Feb 4, 2012
Showing with 18 additions and 2 deletions.
  1. +18 −2 expect.js
View
@@ -144,9 +144,25 @@
*/
Assertion.prototype.empty = function () {
- expect(this.obj).to.have.property('length');
+ var expectation;
+
+ if ('object' == typeof this.obj && null !== this.obj && !isArray(this.obj)) {
+ if ('number' == typeof this.obj.length) {
+ expectation = !this.obj.length;
+ } else {
+ expectation = !keys(this.obj).length;
+ }
+ } else {
+ if ('string' != typeof this.obj) {
+ expect(this.obj).to.be.an('object');
+ }
+
+ expect(this.obj).to.have.property('length');
+ expectation = !this.obj.length;
+ }
+
this.assert(
- 0 === this.obj.length
+ expectation
, 'expected ' + i(this.obj) + ' to be empty'
, 'expected ' + i(this.obj) + ' to not be empty');
return this;

0 comments on commit d1a9689

Please sign in to comment.