From 30d8a32c0bac55e519286e0b5ded7be949d381a4 Mon Sep 17 00:00:00 2001 From: Denis Bardadym Date: Wed, 11 Sep 2013 14:57:31 +0400 Subject: [PATCH] Added complimentary method #endWith(). Fill changelog --- .gitignore | 3 ++- History.md | 4 ++++ lib/should.js | 19 ++++++++++++++++++- test/should.test.js | 21 +++++++++++++++++++++ 4 files changed, 45 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 754aae0..a51bb6b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ node_modules .DS_Store -desktop.ini \ No newline at end of file +desktop.ini +.idea diff --git a/History.md b/History.md index b8a7af4..d00fd2c 100644 --- a/History.md +++ b/History.md @@ -1,4 +1,8 @@ + * added tests and docs for `#approximately(value, delta, description)` + * added `#beginWith(prefix, description)` [fredr] + * added `#endWith(suffix, description)` + 1.2.2 / 2013-02-19 ================== diff --git a/lib/should.js b/lib/should.js index 625394d..0bbb1c9 100644 --- a/lib/should.js +++ b/lib/should.js @@ -72,7 +72,10 @@ exports.not.exist = exports.not.exists = function(obj, msg){ Object.defineProperty(Object.prototype, 'should', { set: function(){}, get: function(){ - return new Assertion(this.valueOf() == this ? this.valueOf() : this); + var textRepr = toString.call(this); + var notObject = (textRepr != '[object Object]'); + var notDate = (textRepr != '[object Date]'); + return new Assertion(notDate && notObject ? this.valueOf() : this); }, configurable: true }); @@ -506,6 +509,20 @@ Assertion.prototype = { return this; }, + /** + * Assert that string ends with `str`. + * @param {String} str + * @param {String} desc + * @api public + */ + + endWith: function(str, desc) { + this.assert(-1 !== this.obj.indexOf(str, this.obj.length - str.length) + , function() { return 'expected ' + this.inspect + ' to end with ' + i(str) + (desc ? " | " + desc : "") } + , function() { return 'expected ' + this.inspect + ' to not end with ' + i(str) + (desc ? " | " + desc : "") }); + return this; + }, + /** * Assert that `obj` is present via `.indexOf()`. * diff --git a/test/should.test.js b/test/should.test.js index 18717cd..856623e 100644 --- a/test/should.test.js +++ b/test/should.test.js @@ -454,6 +454,27 @@ module.exports = { }, "expected 'foobar' to not start with 'foo' | baz"); }, + 'test endWith()': function() { + 'foobar'.should.endWith('bar'); + 'foobar'.should.not.endWith('foo'); + + err(function() { + 'foobar'.should.endWith('foo'); + }, "expected 'foobar' to end with 'foo'"); + + err(function() { + 'foobar'.should.not.endWith('bar'); + }, "expected 'foobar' to not end with 'bar'"); + + err(function() { + 'foobar'.should.endWith('foo', 'baz'); + }, "expected 'foobar' to end with 'foo' | baz"); + + err(function() { + 'foobar'.should.not.endWith('bar', 'baz'); + }, "expected 'foobar' to not end with 'bar' | baz"); + }, + 'test include() with string': function(){ 'foobar'.should.include('bar'); 'foobar'.should.include('foo');