unexpected
Advanced tools
Comparing version 1.0.9 to 1.0.10
@@ -592,15 +592,15 @@ // Copyright (c) 2013 Sune Simonsen <sune@we-knowhow.dk> | ||
expect.addAssertion('to be an array whose items satisfy', function (callback) { | ||
var errors = []; | ||
var obj = this.obj; | ||
expect.addAssertion('to be a map whose values satisfy', function (callback) { | ||
if ('function' !== typeof callback) { | ||
throw new Error('Assertions "' + this.testDescription + '" expects a functions as argument'); | ||
} | ||
expect(obj, 'to be an array'); | ||
expect(this.obj, 'to be an object'); | ||
forEach(obj, function (item, index) { | ||
var obj = this.obj; | ||
var errors = []; | ||
forEach(getKeys(obj), function (key) { | ||
try { | ||
callback(item); | ||
callback(obj[key]); | ||
} catch (e) { | ||
errors.push(' ' + index + ': ' + e.message.replace(/\n/g, '\n ')); | ||
errors.push(' ' + key + ': ' + e.message.replace(/\n/g, '\n ')); | ||
} | ||
@@ -618,6 +618,14 @@ }); | ||
expect.addAssertion('to be a map whose values satisfy', function (callback) { | ||
expect.addAssertion('to be an array whose items satisfy', function (callback) { | ||
if ('function' !== typeof callback) { | ||
throw new Error('Assertions "' + this.testDescription + '" expects a functions as argument'); | ||
} | ||
expect(this.obj, 'to be an array'); | ||
expect(this.obj, 'to be a map whose values satisfy', callback); | ||
}); | ||
expect.addAssertion('to be a map whose keys satisfy', function (callback) { | ||
if ('function' !== typeof callback) { | ||
throw new Error('Assertions "' + this.testDescription + '" expects a functions as argument'); | ||
} | ||
expect(this.obj, 'to be an object'); | ||
@@ -627,5 +635,6 @@ | ||
var errors = []; | ||
forEach(getKeys(obj), function (key) { | ||
var keys = getKeys(obj); | ||
forEach(keys, function (key) { | ||
try { | ||
callback(obj[key]); | ||
callback(key); | ||
} catch (e) { | ||
@@ -637,5 +646,3 @@ errors.push(' ' + key + ': ' + e.message.replace(/\n/g, '\n ')); | ||
if (errors.length > 0) { | ||
var objectString = inspect(obj); | ||
var prefix = /\n/.test(objectString) ? '\n' : ' '; | ||
var message = 'failed expectation in' + prefix + objectString + ':\n' + | ||
var message = 'failed expectation on keys ' + keys.join(', ') + ':\n' + | ||
errors.join('\n'); | ||
@@ -646,2 +653,3 @@ throw new Error(message); | ||
Assertion.prototype.inspect = inspect; | ||
@@ -648,0 +656,0 @@ Assertion.prototype.eql = eql; |
{ | ||
"name": "unexpected", | ||
"version": "1.0.9", | ||
"version": "1.0.10", | ||
"author": "Sune Sloth Simonsen <sune@we-knowhow.dk>", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -64,3 +64,3 @@ # Unexpected | ||
```js | ||
define(['unexpected/lib/unexpected.js'], function (expect) { | ||
define(['unexpected/lib/unexpected'], function (expect) { | ||
// Your code | ||
@@ -335,2 +335,28 @@ }); | ||
**map whose keys satify**: will run an assertion function for each key in a map | ||
```js | ||
expect({ foo: 0, bar: 1, baz: 2, qux: 3 }, | ||
'to be a map whose keys satisfy', function (key) { | ||
expect(key, 'to match', /[a-z]{3}/); | ||
}); | ||
``` | ||
Using this assertion result in very detailed error reporting show in the below example: | ||
```js | ||
expect({ foo: 0, bar: 1, baz: 2, qux: 3, quux: 4 }, | ||
'to be a map whose keys satisfy', function (key) { | ||
expect(key, 'to have length', 3); | ||
}); | ||
``` | ||
will output: | ||
``` | ||
failed expectation on keys foo, bar, baz, qux, quux: | ||
quux: expected 'quux' to have length 3 | ||
``` | ||
**map whose values satify**: will run an assertion function for each value in a map | ||
@@ -350,4 +376,4 @@ | ||
'to be a map whose values satisfy', function (arr) { | ||
expect(arr, 'to be a map whose values satisfy', function (value) { | ||
expect(value, 'to be a number'); | ||
expect(arr, 'to be an array whose items satisfy', function (item) { | ||
expect(item, 'to be a number'); | ||
}); | ||
@@ -354,0 +380,0 @@ }); |
@@ -686,4 +686,4 @@ /*global describe, it, expect*/ | ||
expect({ foo: [0, 1, 2], bar: [4, '5', 6], baz: [7, 8, '9'] }, 'to be a map whose values satisfy', function (arr) { | ||
expect(arr, 'to be a map whose values satisfy', function (value) { | ||
expect(value, 'to be a number'); | ||
expect(arr, 'to be an array whose items satisfy', function (item) { | ||
expect(item, 'to be a number'); | ||
}); | ||
@@ -703,2 +703,48 @@ }); | ||
describe('to be a map whose keys satisfy assertion', function () { | ||
it('only accepts a function', function () { | ||
expect(function () { | ||
expect([1,2,3], 'to be a map whose keys satisfy'); | ||
}, 'to throw', 'Assertions "to be a map whose keys satisfy" expects a functions as argument'); | ||
expect(function () { | ||
expect([1,2,3], 'to be a map whose keys satisfy', 42); | ||
}, 'to throw', 'Assertions "to be a map whose keys satisfy" expects a functions as argument'); | ||
}); | ||
it('only accepts objects as the target', function () { | ||
expect(function () { | ||
expect(42, 'to be a map whose keys satisfy', function (key) {}); | ||
}, 'to throw', "expected 42 to be an 'object'"); | ||
}); | ||
it('asserts that the given callback does not throw for any keys in the map', function () { | ||
expect({ foo: 0, bar: 1, baz: 2, qux: 3 }, 'to be a map whose keys satisfy', function (key) { | ||
expect(key, 'not to be empty'); | ||
}); | ||
expect({ foo: 0, bar: 1, baz: 2, qux: 3 }, 'to be a map whose keys satisfy', function (key) { | ||
expect(key, 'to match', /[a-z]{3}/); | ||
}); | ||
}); | ||
it('fails when the assertion fails', function () { | ||
expect(function () { | ||
expect({ foo: 0, bar: 1, Baz: 2, qux: 3 }, 'to be a map whose keys satisfy', function (key) { | ||
expect(key, 'to match', /[a-z]{3}/); | ||
}); | ||
}, 'to throw', /expected 'Baz' to match/); | ||
}); | ||
it('provides a detailed report of where failures occur', function () { | ||
expect(function () { | ||
expect({ foo: 0, bar: 1, baz: 2, qux: 3, quux: 4 }, 'to be a map whose keys satisfy', function (key) { | ||
expect(key, 'to have length', 3); | ||
}); | ||
}, 'to throw', | ||
"failed expectation on keys foo, bar, baz, qux, quux:\n" + | ||
" quux: expected 'quux' to have length 3"); | ||
}); | ||
}); | ||
it('throws if the assertion does not exists', function () { | ||
@@ -705,0 +751,0 @@ expect(function () { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
221458
6635
472