Skip to content

Commit

Permalink
to begin with, to end with, to contain: Error out if passed a needle …
Browse files Browse the repository at this point in the history
…of the empty string.
  • Loading branch information
papandreou committed Jul 30, 2015
1 parent bdf7b0e commit ea08ef3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/assertions.js
Expand Up @@ -267,6 +267,11 @@ module.exports = function (expect) {
expect.addAssertion('string', '[not] to contain', function (expect, subject) {
var flags = this.flags;
var args = Array.prototype.slice.call(arguments, 2);
args.forEach(function (arg) {
if (String(arg) === '') {
throw new Error("The '" + this.testDescription + "' assertion does not support the empty string");
}
}, this);
expect.withError(function () {
args.forEach(function (arg) {
expect(subject.indexOf(arg) !== -1, '[not] to be truthy');
Expand Down Expand Up @@ -366,6 +371,9 @@ module.exports = function (expect) {
expect.addAssertion('string', '[not] to begin with', function (expect, subject, value) {
var flags = this.flags;
var stringValue = String(value);
if (stringValue === '') {
throw new Error("The '" + this.testDescription + "' assertion does not support a prefix of the empty string");
}
expect.withError(function () {
expect(subject.substr(0, stringValue.length), '[not] to equal', stringValue);
}, function (err) {
Expand Down Expand Up @@ -394,6 +402,9 @@ module.exports = function (expect) {
expect.addAssertion('string', '[not] to end with', function (expect, subject, value) {
var flags = this.flags;
var stringValue = String(value);
if (stringValue === '') {
throw new Error("The '" + this.testDescription + "' assertion does not support a suffix of the empty string");
}
expect.withError(function () {
expect(subject.substr(-stringValue.length), '[not] to equal', stringValue);
}, function (err) {
Expand Down
18 changes: 18 additions & 0 deletions test/unexpected.spec.js
Expand Up @@ -1698,6 +1698,12 @@ describe('unexpected', function () {
});

describe('contain assertion', function () {
it('should throw an error when one of the arguments is the empty string', function () {
expect(function () {
expect('foo', 'to contain', 'bar', '');
}, 'to throw', "The 'to contain' assertion does not support the empty string");
});

it('asserts indexOf for a string', function () {
expect('hello world', 'to contain', 'world');
});
Expand Down Expand Up @@ -1816,6 +1822,12 @@ describe('unexpected', function () {
});

describe('to begin with assertion', function () {
it('should throw an error when the expected prefix is the empty string', function () {
expect(function () {
expect('foo', 'to begin with', '');
}, 'to throw', "The 'to begin with' assertion does not support a prefix of the empty string");
});

describe('without the "not" flag', function () {
it('asserts equality with a string', function () {
expect('hello', 'to begin with', 'hello');
Expand Down Expand Up @@ -1912,6 +1924,12 @@ describe('unexpected', function () {
});

describe('to end with assertion', function () {
it('should throw an error when the expected suffix is the empty string', function () {
expect(function () {
expect('foo', 'to end with', '');
}, 'to throw', "The 'to end with' assertion does not support a suffix of the empty string");
});

describe('without the "not" flag', function () {
it('asserts equality with a string', function () {
expect('hello', 'to end with', 'hello');
Expand Down

0 comments on commit ea08ef3

Please sign in to comment.