Skip to content

Commit

Permalink
Removed support for the non-empty flag for 'to be an array of'
Browse files Browse the repository at this point in the history
  • Loading branch information
sunesimonsen committed Mar 31, 2015
1 parent 9c509d1 commit 6356afb
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 18 deletions.
17 changes: 6 additions & 11 deletions lib/assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -453,24 +453,19 @@ module.exports = function (expect) {
});

expect.addAssertion('array-like', 'to be an array of', function (expect, subject, itemType) {
this.errorMode = 'nested';
expect(subject, 'to be non-empty');
this.errorMode = 'default';
expect(subject, 'to be an array whose items satisfy', 'to be a', itemType);
});

expect.addAssertion('array-like', 'to be a non-empty array of', function (expect, subject, itemType) {
if (this.flags['non-empty']) {
expect(subject, 'to be non-empty');
}
expect(subject, 'to be an array of', itemType);
});

expect.addAssertion('array-like', 'to be an array of (strings|numbers|booleans|arrays|objects|functions|regexps|regexes|regular expressions)', function (expect, subject) {
this.errorMode = 'nested';
expect(subject, 'to be non-empty');
this.errorMode = 'default';
expect(subject, 'to be an array of', this.alternations[0].replace(/e?s$/, ''));
});

expect.addAssertion('array-like', 'to be a non-empty array of (strings|numbers|booleans|arrays|objects|functions|regexps|regexes|regular expressions)', function (expect, subject) {
expect(subject, 'to be a non-empty array of', this.alternations[0].replace(/e?s$/, ''));
});

expect.addAssertion('object', 'to be (a map|a hash|an object) whose (keys|properties) satisfy', function (expect, subject) {
var extraArgs = Array.prototype.slice.call(arguments, 2);
if (extraArgs.length === 0) {
Expand Down
30 changes: 23 additions & 7 deletions test/unexpected.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,13 +260,6 @@ describe('unexpected', function () {
expect('abc', 'to be a non-empty string');
expect([], 'to be an', 'array');
expect([], 'to be an array');
expect(['abc'], 'to be an array of strings');
expect([{}], 'to be an array of objects');
expect([{}], 'to be a non-empty array of objects');
expect([/foo/, /bar/], 'to be a non-empty array of regexps');
expect([/foo/, /bar/], 'to be a non-empty array of regexes');
expect([[], [], []], 'to be an array of arrays');
expect(['abc'], 'to be a non-empty array of strings');
expect([], 'to be an empty array');
expect({}, 'to be an', Object);
expect([123], 'to be a non-empty array');
Expand Down Expand Up @@ -333,6 +326,29 @@ describe('unexpected', function () {
});
});

describe('to a an array of assertion', function () {
it('fails if the given array is empty', function () {
expect(function () {
expect([], 'to be an array of strings');
}, 'to throw',
"expected [] to be an array of strings\n" +
" expected [] to be non-empty");
});

it('asserts that all items in the array has the specified type', function () {
expect(['abc'], 'to be an array of strings');
expect([{}], 'to be an array of objects');
expect([/foo/, /bar/], 'to be an array of regexps');
expect([/foo/, /bar/], 'to be an array of regexes');
});

it('fails if any item in the array has another type than what is expected', function () {
expect(function () {
expect(['abc', 123], 'to be an array of strings');
}, 'to throw', "expected [ 'abc', 123 ] to be an array of strings");
});
});

describe('equal assertion', function () {
it('asserts deep equality that works with objects', function () {
expect({ a: 'b' }, 'to equal', { a: 'b' });
Expand Down

0 comments on commit 6356afb

Please sign in to comment.