From bddaecbe69f7070efe5f2dbb8dd0be6453c60731 Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Thu, 14 Mar 2013 23:33:58 +0100 Subject: [PATCH 1/6] Make it clear that ol3's expect.js is different --- test/{expect-0.2.0 => expect-0.2.0-ol3}/expect.js | 0 test/ol.html | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) rename test/{expect-0.2.0 => expect-0.2.0-ol3}/expect.js (100%) diff --git a/test/expect-0.2.0/expect.js b/test/expect-0.2.0-ol3/expect.js similarity index 100% rename from test/expect-0.2.0/expect.js rename to test/expect-0.2.0-ol3/expect.js diff --git a/test/ol.html b/test/ol.html index 0860f18003..094b5ebbff 100644 --- a/test/ol.html +++ b/test/ol.html @@ -22,7 +22,7 @@
- + From 62011dab28b47ef9a24870734775743a82d0a2d0 Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Thu, 14 Mar 2013 23:55:25 +0100 Subject: [PATCH 2/6] Move roughlyEqual into expect.js --- test/expect-0.2.0-ol3/expect.js | 18 ++++++++++++++ test/spec/ol/expect.test.js | 43 +++++++++++++++++++++++++++++++++ test/test-extensions.js | 5 ---- 3 files changed, 61 insertions(+), 5 deletions(-) create mode 100644 test/spec/ol/expect.test.js diff --git a/test/expect-0.2.0-ol3/expect.js b/test/expect-0.2.0-ol3/expect.js index 58c7049e0e..24fc5d7437 100644 --- a/test/expect-0.2.0-ol3/expect.js +++ b/test/expect-0.2.0-ol3/expect.js @@ -299,6 +299,24 @@ return this; }; + /** + * Assert value is within _tol_ of _n_. + * + * @param {Number} n + * @param {Number} tol + * + * @api public + */ + + Assertion.prototype.roughlyEqual = + Assertion.prototype.kindaEqual = function(n, tol) { + this.assert( + Math.abs(this.obj - n) <= tol + , function(){ return 'expected ' + i(this.obj) + ' to be within ' + tol + ' of ' + n } + , function(){ return 'expected ' + i(this.obj) + ' not to be within ' + tol + ' of ' + n }); + return this; + }; + /** * Assert string value matches _regexp_. * diff --git a/test/spec/ol/expect.test.js b/test/spec/ol/expect.test.js new file mode 100644 index 0000000000..8c71af21c8 --- /dev/null +++ b/test/spec/ol/expect.test.js @@ -0,0 +1,43 @@ +goog.provide('ol.test.expect.js'); + + +describe('expect.js', function() { + + describe('roughlyEqual', function() { + + it('can tell the difference between 1 and 3', function() { + expect(1).not.to.roughlyEqual(3, 1); + }); + + it('really can tell the difference between 1 and 3', function() { + expect(function() { + expect(1).to.roughlyEqual(3, 0.5); + }).to.throwException(); + }); + + }); + + describe('kindaEqual', function() { + + it('thinks that 1 ain\'t so different from 2', function() { + expect(1).to.kindaEqual(2, 1); + }); + + it('knows that, like, 1 and 2 would, like, totally dig each other', + function() { + expect(function() { + expect(1).to.kindaEqual(2, 1); + }).not.to.throwException(); + }); + + }); + + describe('roughlyEqual and kindaEqual', function() { + + it('it\'s like they\'re soul mates', function() { + expect(expect(0).to.roughlyEqual).to.be(expect(1).to.kindaEqual); + }); + + }); + +}); diff --git a/test/test-extensions.js b/test/test-extensions.js index 333bce0cb9..eeec0751ff 100644 --- a/test/test-extensions.js +++ b/test/test-extensions.js @@ -19,11 +19,6 @@ function waitsFor(condition, message, timeout, callback) { } -expect.Assertion.prototype.roughlyEqual = function(other, tol) { - return Math.abs(this.actual - other) <= tol; -}; - - expect.Assertion.prototype.intersectWith = function(other) { return this.obj.intersects(other); }; From 7c7c4df3fc6b93ca4ce653a1a13ac4cb440313df Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Fri, 15 Mar 2013 00:25:49 +0100 Subject: [PATCH 3/6] Move called into expect.js --- test/expect-0.2.0-ol3/expect.js | 15 ++++++++++ test/spec/ol/expect.test.js | 49 +++++++++++++++++++++++++++++++++ test/test-extensions.js | 5 ---- 3 files changed, 64 insertions(+), 5 deletions(-) diff --git a/test/expect-0.2.0-ol3/expect.js b/test/expect-0.2.0-ol3/expect.js index 24fc5d7437..5d2d57b184 100644 --- a/test/expect-0.2.0-ol3/expect.js +++ b/test/expect-0.2.0-ol3/expect.js @@ -477,6 +477,21 @@ return this; }; + + /** + * Assert that a sinon spy was called. + * + * @api public + */ + Assertion.prototype.called = + Assertion.prototype.totallyWantsToSpeakToYou = function() { + this.assert( + this.obj.called + , function(){ return 'expected ' + i(this.obj) + ' to be called' } + , function(){ return 'expected ' + i(this.obj) + ' not to be called' }); + return this; + }; + /** * Assert a failure. * diff --git a/test/spec/ol/expect.test.js b/test/spec/ol/expect.test.js index 8c71af21c8..e3f1cab072 100644 --- a/test/spec/ol/expect.test.js +++ b/test/spec/ol/expect.test.js @@ -40,4 +40,53 @@ describe('expect.js', function() { }); + describe('called', function() { + + var telephone; + beforeEach(function() { + telephone = sinon.spy(); + }); + + it('has caller ID', function() { + telephone(); + expect(telephone).to.be.called(); + }); + + it('also knows when it\'s speaking to the hand', function() { + (function() {})(); + expect(telephone).not.to.be.called(); + }); + + }); + + describe('totallyWantsToSpeakToYou', function() { + + var callMeMaybe; + beforeEach(function() { + callMeMaybe = sinon.spy(); + }); + + it('reminds you that you forgot', function() { + expect(function() { + expect(callMeMaybe).to.be.called(); + }).to.throwException(); + }); + + it('gets moody all too quickly', function() { + callMeMaybe(); + expect(function() { + expect(callMeMaybe).not.to.be.called(); + }).to.throwException(); + }); + + }); + + describe('called and totallyWantsToSpeakToYou', function() { + + it('are best friends forever \u2665', function() { + expect(expect(0).to.called).to.be(expect(1).to.totallyWantsToSpeakToYou); + }); + + }); + }); diff --git a/test/test-extensions.js b/test/test-extensions.js index eeec0751ff..22c3cab3a5 100644 --- a/test/test-extensions.js +++ b/test/test-extensions.js @@ -24,11 +24,6 @@ expect.Assertion.prototype.intersectWith = function(other) { }; -expect.Assertion.prototype.called = function() { - return this.obj.called; -}; - - expect.Assertion.prototype.equalArray = function(other) { return goog.array.equals(this.obj, other); }; From 79c77a63698a3fc76217891c311c224451b93b23 Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Fri, 15 Mar 2013 00:37:05 +0100 Subject: [PATCH 4/6] Move intersect into expect.js --- test/expect-0.2.0-ol3/expect.js | 17 ++++++++++ test/spec/ol/rectangle.test.js | 56 ++++++++++++++++----------------- test/test-extensions.js | 5 --- 3 files changed, 45 insertions(+), 33 deletions(-) diff --git a/test/expect-0.2.0-ol3/expect.js b/test/expect-0.2.0-ol3/expect.js index 5d2d57b184..7b268b63dc 100644 --- a/test/expect-0.2.0-ol3/expect.js +++ b/test/expect-0.2.0-ol3/expect.js @@ -492,6 +492,23 @@ return this; }; + /** + * Assert that that objects intersect. + * FIXME this is ol3 specific + * + * @param {Object} other + * + * @api public + */ + Assertion.prototype.intersect = + Assertion.prototype.wentToSchoolWith = function(other) { + this.assert( + this.obj.intersects(other) + , function(){ return 'expected ' + i(this.obj) + ' to intersect ' + i(other) } + , function(){ return 'expected ' + i(this.obj) + ' not to intersect ' + i(other) }); + return this; + }; + /** * Assert a failure. * diff --git a/test/spec/ol/rectangle.test.js b/test/spec/ol/rectangle.test.js index 50bd9a4df5..0bfdc3cec7 100644 --- a/test/spec/ol/rectangle.test.js +++ b/test/spec/ol/rectangle.test.js @@ -11,7 +11,7 @@ describe('ol.Rectangle', function() { }); }); - describe('intersects', function() { + describe('intersect', function() { var rectangle1; @@ -20,48 +20,48 @@ describe('ol.Rectangle', function() { }); it('returns the expected value', function() { - expect(rectangle1).to.intersectWith(rectangle1); - expect(rectangle1).to.intersectWith(new ol.Rectangle(20, 20, 80, 80)); - expect(rectangle1).to.intersectWith(new ol.Rectangle(20, 50, 80, 100)); - expect(rectangle1).to.intersectWith(new ol.Rectangle(20, 80, 80, 120)); - expect(rectangle1).to.intersectWith(new ol.Rectangle(50, 20, 100, 80)); - expect(rectangle1).to.intersectWith(new ol.Rectangle(50, 80, 100, 120)); - expect(rectangle1).to.intersectWith(new ol.Rectangle(80, 20, 120, 80)); - expect(rectangle1).to.intersectWith(new ol.Rectangle(80, 50, 120, 100)); - expect(rectangle1).to.intersectWith(new ol.Rectangle(80, 80, 120, 120)); - expect(rectangle1).to.intersectWith(new ol.Rectangle(20, 20, 120, 120)); - expect(rectangle1).to.intersectWith(new ol.Rectangle(70, 70, 80, 80)); - expect(rectangle1).to.not.intersectWith( + expect(rectangle1).to.intersect(rectangle1); + expect(rectangle1).to.intersect(new ol.Rectangle(20, 20, 80, 80)); + expect(rectangle1).to.intersect(new ol.Rectangle(20, 50, 80, 100)); + expect(rectangle1).to.intersect(new ol.Rectangle(20, 80, 80, 120)); + expect(rectangle1).to.intersect(new ol.Rectangle(50, 20, 100, 80)); + expect(rectangle1).to.intersect(new ol.Rectangle(50, 80, 100, 120)); + expect(rectangle1).to.intersect(new ol.Rectangle(80, 20, 120, 80)); + expect(rectangle1).to.intersect(new ol.Rectangle(80, 50, 120, 100)); + expect(rectangle1).to.intersect(new ol.Rectangle(80, 80, 120, 120)); + expect(rectangle1).to.intersect(new ol.Rectangle(20, 20, 120, 120)); + expect(rectangle1).to.intersect(new ol.Rectangle(70, 70, 80, 80)); + expect(rectangle1).to.not.intersect( new ol.Rectangle(10, 10, 30, 30)); - expect(rectangle1).to.not.intersectWith( + expect(rectangle1).to.not.intersect( new ol.Rectangle(30, 10, 70, 30)); - expect(rectangle1).to.not.intersectWith( + expect(rectangle1).to.not.intersect( new ol.Rectangle(50, 10, 100, 30)); - expect(rectangle1).to.not.intersectWith( + expect(rectangle1).to.not.intersect( new ol.Rectangle(80, 10, 120, 30)); - expect(rectangle1).to.not.intersectWith( + expect(rectangle1).to.not.intersect( new ol.Rectangle(120, 10, 140, 30)); - expect(rectangle1).to.not.intersectWith( + expect(rectangle1).to.not.intersect( new ol.Rectangle(10, 30, 30, 70)); - expect(rectangle1).to.not.intersectWith( + expect(rectangle1).to.not.intersect( new ol.Rectangle(120, 30, 140, 70)); - expect(rectangle1).to.not.intersectWith( + expect(rectangle1).to.not.intersect( new ol.Rectangle(10, 50, 30, 100)); - expect(rectangle1).to.not.intersectWith( + expect(rectangle1).to.not.intersect( new ol.Rectangle(120, 50, 140, 100)); - expect(rectangle1).to.not.intersectWith( + expect(rectangle1).to.not.intersect( new ol.Rectangle(10, 80, 30, 120)); - expect(rectangle1).to.not.intersectWith( + expect(rectangle1).to.not.intersect( new ol.Rectangle(120, 80, 140, 120)); - expect(rectangle1).to.not.intersectWith( + expect(rectangle1).to.not.intersect( new ol.Rectangle(10, 120, 30, 140)); - expect(rectangle1).to.not.intersectWith( + expect(rectangle1).to.not.intersect( new ol.Rectangle(30, 120, 70, 140)); - expect(rectangle1).to.not.intersectWith( + expect(rectangle1).to.not.intersect( new ol.Rectangle(50, 120, 100, 140)); - expect(rectangle1).to.not.intersectWith( + expect(rectangle1).to.not.intersect( new ol.Rectangle(80, 120, 120, 140)); - expect(rectangle1).to.not.intersectWith( + expect(rectangle1).to.not.intersect( new ol.Rectangle(120, 120, 140, 140)); }); }); diff --git a/test/test-extensions.js b/test/test-extensions.js index 22c3cab3a5..2cb2503e30 100644 --- a/test/test-extensions.js +++ b/test/test-extensions.js @@ -19,11 +19,6 @@ function waitsFor(condition, message, timeout, callback) { } -expect.Assertion.prototype.intersectWith = function(other) { - return this.obj.intersects(other); -}; - - expect.Assertion.prototype.equalArray = function(other) { return goog.array.equals(this.obj, other); }; From c9f43b4da726459b9c6c9bfde486f8e4f8ea8af3 Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Fri, 15 Mar 2013 01:11:25 +0100 Subject: [PATCH 5/6] Move equalArray into expect.js --- test/expect-0.2.0-ol3/expect.js | 26 ++++++++++++++++++++++ test/spec/ol/expect.test.js | 39 +++++++++++++++++++++++++++++++++ test/test-extensions.js | 5 ----- 3 files changed, 65 insertions(+), 5 deletions(-) diff --git a/test/expect-0.2.0-ol3/expect.js b/test/expect-0.2.0-ol3/expect.js index 7b268b63dc..2fe81d4575 100644 --- a/test/expect-0.2.0-ol3/expect.js +++ b/test/expect-0.2.0-ol3/expect.js @@ -509,6 +509,32 @@ return this; }; + /** + * Assert that arrays have the same value. + * + * @param {Array} other + * + * @api public + */ + Assertion.prototype.equalArray = + Assertion.prototype.preferItBeforeItWasFamous = function(other) { + var equal = this.obj.length == other.length; + if (equal) { + var j = 0; + for (j = 0; j < other.length; ++j) { + if (this.obj[j] !== other[j]) { + equal = false; + break; + } + } + } + this.assert( + equal + , function(){ return 'expected ' + i(this.obj) + ' to have the same array value as ' + i(other) } + , function(){ return 'expected ' + i(this.obj) + ' not to have the same array value as ' + i(other) }); + return this; + }; + /** * Assert a failure. * diff --git a/test/spec/ol/expect.test.js b/test/spec/ol/expect.test.js index e3f1cab072..427c861da7 100644 --- a/test/spec/ol/expect.test.js +++ b/test/spec/ol/expect.test.js @@ -89,4 +89,43 @@ describe('expect.js', function() { }); + describe('equalArray', function() { + + it('knows who\'s cool', function() { + expect(['me']).to.equalArray(['me']); + }); + + it('knows who isn\'t', function() { + expect(['you']).not.to.equalArray([]); + }); + + }); + + describe('preferItBeforeItWasFamous', function() { + + it('respects the artist\'s privacy', function() { + expect(function() { + expect(['David', 'Bowie']).to.preferItBeforeItWasFamous( + ['David', 'Robert', 'Jones']); + }).to.throwException(); + }); + + it('prefers to keep some things quiet', function() { + expect(function() { + expect(['Cyrus']).to.preferItBeforeItWasFamous(['Cyrus']); + }).not.to.throwException(); + + }); + + }); + + describe('equalArray and preferItBeforeItWasFamous', function() { + + it('should record a duet together \u266c', function() { + expect(expect(['David Bowie']).to.equalArray).to.be( + expect(['Miley Cyrus']).to.preferItBeforeItWasFamous); + }); + + }); + }); diff --git a/test/test-extensions.js b/test/test-extensions.js index 2cb2503e30..76c127646b 100644 --- a/test/test-extensions.js +++ b/test/test-extensions.js @@ -19,11 +19,6 @@ function waitsFor(condition, message, timeout, callback) { } -expect.Assertion.prototype.equalArray = function(other) { - return goog.array.equals(this.obj, other); -}; - - // helper functions for async testing (function(global) { From af6de5582afb1fefa3c45ac11940ae8ea27c44de Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Fri, 15 Mar 2013 15:29:13 +0100 Subject: [PATCH 6/6] Add comment to highlight use of modified expect.js Hat tip @marcjansen --- test/ol.html | 1 + 1 file changed, 1 insertion(+) diff --git a/test/ol.html b/test/ol.html index 094b5ebbff..7831aa0962 100644 --- a/test/ol.html +++ b/test/ol.html @@ -22,6 +22,7 @@
+