Skip to content

Commit

Permalink
Merge pull request #349 from unexpectedjs/feature/disallowEmptyArray
Browse files Browse the repository at this point in the history
to have keys satisfying, to have values satisfying: Disallow an empty array
  • Loading branch information
papandreou committed Nov 18, 2016
2 parents e2f26c2 + 7b8aeaa commit d4fb296
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
13 changes: 10 additions & 3 deletions lib/assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,11 @@ module.exports = function (expect) {
'<object> to be (a map|a hash|an object) whose values [exhaustively] satisfy <assertion>'
], function (expect, subject, nextArg) {
expect.errorMode = 'nested';
expect(subject, 'not to equal', {});
if (expect.subjectType.is('array')) {
expect(subject, 'not to equal', []);
} else {
expect(subject, 'not to equal', {});
}
expect.errorMode = 'bubble';

var keys = expect.subjectType.getKeys(subject);
Expand Down Expand Up @@ -769,8 +773,11 @@ module.exports = function (expect) {
], function (expect, subject) {
var extraArgs = Array.prototype.slice.call(arguments, 2);
expect.errorMode = 'nested';
expect(subject, 'to be an object');
expect(subject, 'not to equal', {});
if (expect.subjectType.is('array')) {
expect(subject, 'not to equal', []);
} else {
expect(subject, 'not to equal', {});
}
expect.errorMode = 'default';

var keys = expect.subjectType.getKeys(subject);
Expand Down
8 changes: 8 additions & 0 deletions test/assertions/to-have-keys-satisfying.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ describe('to have keys satisfying assertion', function () {
" expected {} not to equal {}");
});

it('fails for an empty array', function () {
expect(function () {
expect([], 'to have keys satisfying', 123);
}, 'to throw',
"expected [] to have keys satisfying 123\n" +
" expected [] not to equal []");
});

it('should work with non-enumerable keys returned by the getKeys function of the subject type', function () {
expect(function () {
expect(new Error('foo'), 'to have keys satisfying', /bar/);
Expand Down
8 changes: 8 additions & 0 deletions test/assertions/to-have-values-satisfying.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ describe('to have values satisfying assertion', function () {
" expected {} not to equal {}");
});

it('fails for an empty array', function () {
expect(function () {
expect([], 'to have values satisfying', 123);
}, 'to throw',
"expected [] to have values satisfying 123\n" +
" expected [] not to equal []");
});

it('fails if the given array is empty', function () {
expect(function () {
expect([], 'to have items satisfying', function (item) {
Expand Down

0 comments on commit d4fb296

Please sign in to comment.