Skip to content

Commit

Permalink
Added complimentary method #endWith(). Fill changelog
Browse files Browse the repository at this point in the history
  • Loading branch information
btd committed Sep 11, 2013
1 parent 1ace0fb commit 30d8a32
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
.DS_Store
desktop.ini
desktop.ini
.idea
4 changes: 4 additions & 0 deletions History.md
Original file line number Diff line number Diff line change
@@ -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
==================

Expand Down
19 changes: 18 additions & 1 deletion lib/should.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
Expand Down Expand Up @@ -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()`.
*
Expand Down
21 changes: 21 additions & 0 deletions test/should.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit 30d8a32

Please sign in to comment.