unexpected
Advanced tools
Comparing version 1.0.8 to 1.0.9
@@ -593,13 +593,14 @@ // 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; | ||
if ('function' !== typeof callback) { | ||
throw new Error('Assertions "to be an array whose items satisfy" expects a functions as argument'); | ||
throw new Error('Assertions "' + this.testDescription + '" expects a functions as argument'); | ||
} | ||
expect(this.obj, 'to be an array'); | ||
expect(obj, 'to be an array'); | ||
var errors = []; | ||
forEach(this.obj, function (item, index) { | ||
forEach(obj, function (item, index) { | ||
try { | ||
callback(item); | ||
} catch (e) { | ||
errors.push(' ' + index + ': ' + e.message.replace(/\n/, '\n ')); | ||
errors.push(' ' + index + ': ' + e.message.replace(/\n/g, '\n ')); | ||
} | ||
@@ -609,3 +610,5 @@ }); | ||
if (errors.length > 0) { | ||
var message = 'failed expectation in ' + inspect(this.obj) + '\n' + | ||
var objectString = inspect(obj); | ||
var prefix = /\n/.test(objectString) ? '\n' : ' '; | ||
var message = 'failed expectation in' + prefix + objectString + ':\n' + | ||
errors.join('\n'); | ||
@@ -616,2 +619,27 @@ throw new Error(message); | ||
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(this.obj, 'to be an object'); | ||
var obj = this.obj; | ||
var errors = []; | ||
forEach(getKeys(obj), function (key) { | ||
try { | ||
callback(obj[key]); | ||
} catch (e) { | ||
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' + | ||
errors.join('\n'); | ||
throw new Error(message); | ||
} | ||
}); | ||
Assertion.prototype.inspect = inspect; | ||
@@ -618,0 +646,0 @@ Assertion.prototype.eql = eql; |
{ | ||
"name": "unexpected", | ||
"version": "1.0.8", | ||
"version": "1.0.9", | ||
"author": "Sune Sloth Simonsen <sune@we-knowhow.dk>", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -316,3 +316,4 @@ # Unexpected | ||
```js | ||
expect([[0, 1, 2], [4, '5', 6], [7, 8, '9']], 'to be an array whose items satisfy', function (arr) { | ||
expect([[0, 1, 2], [4, '5', 6], [7, 8, '9']], | ||
'to be an array whose items satisfy', function (arr) { | ||
expect(arr, 'to be an array whose items satisfy', function (item) { | ||
@@ -327,9 +328,42 @@ expect(item, 'to be a number'); | ||
``` | ||
failed expectation in [ [ 0, 1, 2 ], [ 4, '5', 6 ], [ 7, 8, '9' ] ] | ||
1: failed expectation in [ 4, '5', 6 ] | ||
1: expected '5' to be a 'number' | ||
2: failed expectation in [ 7, 8, '9' ] | ||
2: expected '9' to be a 'number' | ||
failed expectation in [ [ 0, 1, 2 ], [ 4, '5', 6 ], [ 7, 8, '9' ] ]: | ||
1: failed expectation in [ 4, '5', 6 ]: | ||
1: expected '5' to be a 'number' | ||
2: failed expectation in [ 7, 8, '9' ]: | ||
2: expected '9' to be a 'number' | ||
``` | ||
**map whose values satify**: will run an assertion function for each value in a map | ||
```js | ||
expect({ foo: 0, bar: 1, baz: 2, qux: 3 }, | ||
'to be a map whose values satisfy', function (value) { | ||
expect(value, 'to be a number'); | ||
}); | ||
``` | ||
Using this assertion result in very detailed error reporting show in the below example: | ||
```js | ||
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'); | ||
}); | ||
}); | ||
``` | ||
will output: | ||
``` | ||
failed expectation in | ||
{ foo: [ 0, 1, 2 ], | ||
bar: [ 4, '5', 6 ], | ||
baz: [ 7, 8, '9' ] }: | ||
bar: failed expectation in [ 4, '5', 6 ]: | ||
1: expected '5' to be a 'number' | ||
baz: failed expectation in [ 7, 8, '9' ]: | ||
2: expected '9' to be a 'number' | ||
``` | ||
## Print all registered assertions to the console | ||
@@ -336,0 +370,0 @@ |
@@ -615,5 +615,5 @@ /*global describe, it, expect*/ | ||
}, 'to throw', | ||
"failed expectation in [ 0, 1, '2', 3, 4 ]\n" + | ||
" 2: expected '2' to be a 'number'\n" + | ||
" 4: expected 4 to be less than 4"); | ||
"failed expectation in [ 0, 1, '2', 3, 4 ]:\n" + | ||
" 2: expected '2' to be a 'number'\n" + | ||
" 4: expected 4 to be less than 4"); | ||
}); | ||
@@ -629,10 +629,76 @@ | ||
}, 'to throw', | ||
"failed expectation in [ [ 0, 1, 2 ], [ 4, '5', 6 ], [ 7, 8, '9' ] ]\n" + | ||
" 1: failed expectation in [ 4, '5', 6 ]\n" + | ||
" 1: expected '5' to be a 'number'\n" + | ||
" 2: failed expectation in [ 7, 8, '9' ]\n" + | ||
" 2: expected '9' to be a 'number'"); | ||
"failed expectation in [ [ 0, 1, 2 ], [ 4, '5', 6 ], [ 7, 8, '9' ] ]:\n" + | ||
" 1: failed expectation in [ 4, '5', 6 ]:\n" + | ||
" 1: expected '5' to be a 'number'\n" + | ||
" 2: failed expectation in [ 7, 8, '9' ]:\n" + | ||
" 2: expected '9' to be a 'number'"); | ||
}); | ||
}); | ||
describe('to be a map whose values satisfy assertion', function () { | ||
it('only accepts a function', function () { | ||
expect(function () { | ||
expect([1,2,3], 'to be a map whose values satisfy'); | ||
}, 'to throw', 'Assertions "to be a map whose values satisfy" expects a functions as argument'); | ||
expect(function () { | ||
expect([1,2,3], 'to be a map whose values satisfy', 42); | ||
}, 'to throw', 'Assertions "to be a map whose values satisfy" expects a functions as argument'); | ||
}); | ||
it('only accepts objects as the target', function () { | ||
expect(function () { | ||
expect(42, 'to be a map whose values satisfy', function (value) {}); | ||
}, 'to throw', "expected 42 to be an 'object'"); | ||
}); | ||
it('asserts that the given callback does not throw for any values in the map', function () { | ||
expect({ foo: 0, bar: 1, baz: 2, qux: 3 }, 'to be a map whose values satisfy', function (value) { | ||
expect(value, 'to be a number'); | ||
}); | ||
expect({ foo: '0', bar: '1', baz: '2', qux: '3' }, 'to be a map whose values satisfy', function (value) { | ||
expect(value, 'not to be a number'); | ||
}); | ||
}); | ||
it('fails when the assertion fails', function () { | ||
expect(function () { | ||
expect({ foo: '0', bar: 1, baz: '2', qux: '3' }, 'to be a map whose values satisfy', function (value) { | ||
expect(value, 'not to be a number'); | ||
}); | ||
}, 'to throw', /expected 1 not to be a 'number'/); | ||
}); | ||
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 values satisfy', function (value) { | ||
expect(value, 'to be a number'); | ||
expect(value, 'to be less than', 4); | ||
}); | ||
}, 'to throw', | ||
"failed expectation in { foo: 0, bar: 1, baz: '2', qux: 3, quux: 4 }:\n" + | ||
" baz: expected '2' to be a 'number'\n" + | ||
" quux: expected 4 to be less than 4"); | ||
}); | ||
it('indents failure reports of nested assertions correctly', function () { | ||
expect(function () { | ||
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'); | ||
}); | ||
}); | ||
}, 'to throw', | ||
"failed expectation in\n" + | ||
"{ foo: [ 0, 1, 2 ],\n" + | ||
" bar: [ 4, '5', 6 ],\n" + | ||
" baz: [ 7, 8, '9' ] }:\n" + | ||
" bar: failed expectation in [ 4, '5', 6 ]:\n" + | ||
" 1: expected '5' to be a 'number'\n" + | ||
" baz: failed expectation in [ 7, 8, '9' ]:\n" + | ||
" 2: expected '9' to be a 'number'"); | ||
}); | ||
}); | ||
it('throws if the assertion does not exists', function () { | ||
@@ -639,0 +705,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
218453
6590
446