Skip to content

Loading…

Extend sugar to support booleans #15

Merged
merged 1 commit into from

2 participants

@carter-thaxton

This lets syntax like: "true.should_be(true)" and "false.should_not_be(true)" to work properly.

@carter-thaxton

The use of valueOf() is somewhat unfortunate. It ultimately comes down to the fact that "new Boolean(false) !== false", however "new Boolean(false) == false".

Really, I think this should get fixed in Jasmine's own internal equals_() method, but that's a bit tougher to swallow. They have a special case for Number, but not for Boolean. In the meantime, this seems pretty good, since booleans don't usually get converted to objects unless we use this sugar syntax anyway.

@marcinbunsch marcinbunsch merged commit b2a2674 into futuresimple:master
@marcinbunsch

Awesome, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 15, 2012
  1. @carter-thaxton
Showing with 5 additions and 0 deletions.
  1. +5 −0 lib/jessie/sugar.js
View
5 lib/jessie/sugar.js
@@ -2,9 +2,11 @@
exports.sugar = function() {
var Sugar = {
should_be: function(compare) { expect(this).toEqual(compare); },
+ should_be_value: function(compare) { expect(this.valueOf()).toEqual(compare.valueOf()); },
should_be_a: function(object) { expect(this.constructor).toEqual(object) },
should_be_an_instance_of: function(object) { expect(true).toEqual(this instanceof(object)) },
should_not_be: function(compare) { expect(this).not.toEqual(compare); },
+ should_not_be_value: function(compare) { expect(this.valueOf()).not.toEqual(compare.valueOf()); },
should_match: function(compare) { expect(this).toMatch(compare); },
should_not_match: function(compare) { expect(this).not.toMatch(compare); },
should_have_been_called: function() { expect(this).toHaveBeenCalled(); },
@@ -18,14 +20,17 @@ exports.sugar = function() {
String.prototype.emitJasmine = function() { return this };
Number.prototype.emitJasmine = function() { return this };
Array.prototype.emitJasmine = function() { return this };
+ Boolean.prototype.emitJasmine = function() { return this };
String.prototype.should_be = Sugar.should_be
Number.prototype.should_be = Sugar.should_be
Array.prototype.should_be = Sugar.should_be
+ Boolean.prototype.should_be = Sugar.should_be_value
String.prototype.should_not_be = Sugar.should_not_be
Number.prototype.should_not_be = Sugar.should_not_be
Array.prototype.should_not_be = Sugar.should_not_be
+ Boolean.prototype.should_not_be = Sugar.should_not_be_value
String.prototype.should_be_a = Sugar.should_be_a
Number.prototype.should_be_a = Sugar.should_be_a
Something went wrong with that request. Please try again.