Permalink
Browse files

The assertion's object should be a non-empty array.

  • Loading branch information...
RubenVerborgh committed Jan 7, 2013
1 parent 67fe266 commit 1ebbeb5382cd2130a4693ca80ebe065dfcb664a8
Showing with 39 additions and 29 deletions.
  1. +2 −0 .jshintrc
  2. +29 −29 lib/chai-things.js
  3. +8 −0 test/something.coffee
View
@@ -4,8 +4,10 @@
"define": true // AMD define function
},
"es5": true, // Allow ES5 features.
+ "expr": true, // Allow expressions as statement.
"indent": 2, // The code uses an indentation width of 2 spaces.
"node": true, // Allow use of node.js globals.
"trailing": true, // Do not leave trailing whitespace.
+ "validthis":true, // Allow `this` in functions.
"white": true // Enable scrict whitespace checking.
}
View
@@ -12,37 +12,51 @@
}(function chaiAsPromised(chai, utils) {
"use strict";
- var Assertion = chai.Assertion;
- var assert = chai.assert;
+ var Assertion = chai.Assertion,
+ expect = chai.expect;
// An assertion on some values in an array
function SomethingAssertion(targetArray, baseAssertion) {
+ var somethingAssertion = this;
// Add a customized version of Assertion properties
Object.getOwnPropertyNames(Assertion.prototype).forEach(function (propertyName) {
// Only add non-predefined properties
- if (propertyName in this)
+ if (propertyName === "something")
return;
var property = Object.getOwnPropertyDescriptor(Assertion.prototype, propertyName);
// If the item is a chai assertion function
if (typeof property.value === "function") {
// Make it with a function that tries the assertion on every array element
utils.addMethod(this, propertyName, function () {
- var asserter = property.value,
+ var object = utils.flag(this, "object"),
+ asserter = property.value,
args = arguments,
assertionError;
+ // The assertion's object should be a non-empty array
+ expect(object).to.be.an('array');
+ expect(object).to.be.not.empty;
+
// Try the assertion on every array element
- for (var i = 0; i < targetArray.length; i++) {
- // Set the assertion object to the array element
- utils.flag(this, "object", targetArray[i]);
+ for (var i = 0; i < object.length; i++) {
+ // Create assertion for the current element
+ var itemAssertion = new Assertion();
+ utils.transferFlags(somethingAssertion, itemAssertion);
+ utils.flag(itemAssertion, "object", object[i]);
+ itemAssertion.assert = assert;
// Return if the assertion succeeds, store the error otherwise
- try {
- return asserter.apply(this, args);
- }
- catch (error) {
- assertionError = error;
- }
+ try { return asserter.apply(itemAssertion, args); }
+ catch (error) { assertionError = error; }
+ }
+ // Change the assertion message to an array viewpoint
+ function assert(test, positive, negative, expected, actual) {
+ utils.flag(itemAssertion, "object", object);
+ Assertion.prototype.assert.call(this, test,
+ positive.replace("#{this}", "an element of #{this}"),
+ negative.replace("#{this}", "an element of #{this}"),
+ expected, actual);
}
+ // Restore original assertion object
// If we reach this point, no element that satisfies the assertion has been found
throw assertionError;
});
@@ -68,24 +82,10 @@
}
}, this);
- this._targetArray = targetArray;
+ // Set the assertion object to the array
+ utils.flag(this, "object", targetArray);
}
- // SomethingAssertion members
- SomethingAssertion.prototype = {
- // Change the assertion message to an array viewpoint
- assert: function (test, positive, negative, expected, actual) {
- utils.flag(this, "object", this._targetArray);
- Assertion.prototype.assert.call(this,
- test,
- positive.replace("#{this}", "an element of #{this}"),
- negative.replace("#{this}", "an element of #{this}"),
- expected,
- actual);
- },
- something: null,
- };
-
// Define the something property on all Chai assertions
Assertion.addProperty("something", function () {
return new SomethingAssertion(this._obj, this);
View
@@ -1,6 +1,14 @@
describe "include.something", ->
describe "without negation", ->
+ it "should assert whether the object is an array", ->
+ (() -> "not an array".should.include.something.that.deep.equals { a: 1 }).
+ should.throw("expected 'not an array' to be an array")
+
+ it "should assert whether the array is non-empty", ->
+ (() -> [].should.include.something.that.deep.equals { a: 1 }).
+ should.throw("expected [] not to be empty")
+
it "passes if an equal element exists", ->
[{ a: 1 }, { b: 2 }].should.include.something.that.deep.equals { b: 2 }

0 comments on commit 1ebbeb5

Please sign in to comment.