Merge pull request #374 from twpayne/functionality-not-form

You can't extend expect.js outside expect.js
This commit is contained in:
Tom Payne
2013-03-15 07:55:45 -07:00
5 changed files with 237 additions and 49 deletions

View File

@@ -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_.
*
@@ -459,6 +477,64 @@
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 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 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.
*

View File

@@ -22,7 +22,8 @@
<div id="mocha"></div>
<script type="text/javascript" src="jquery-1.9.1/jquery.min.js"></script>
<script type="text/javascript" src="expect-0.2.0/expect.js"></script>
<!-- Extended expect.js w/ various methods, see #374 for the differences to 0.2.0 -->
<script type="text/javascript" src="expect-0.2.0-ol3/expect.js"></script>
<script type="text/javascript" src="sinon-1.6.0/sinon.js"></script>
<script type="text/javascript" src="mocha-1.8.1/mocha.js"></script>
<script type="text/javascript" src="test-extensions.js"></script>

131
test/spec/ol/expect.test.js Normal file
View File

@@ -0,0 +1,131 @@
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);
});
});
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);
});
});
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);
});
});
});

View File

@@ -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));
});
});

View File

@@ -19,26 +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);
};
expect.Assertion.prototype.called = function() {
return this.obj.called;
};
expect.Assertion.prototype.equalArray = function(other) {
return goog.array.equals(this.obj, other);
};
// helper functions for async testing
(function(global) {