chai-leaflet
Advanced tools
Comparing version 0.0.11 to 0.0.12
@@ -6,134 +6,131 @@ /** | ||
*/ | ||
(function (factory, window) { | ||
(function (factory) { | ||
if (typeof define === 'function' && define.amd) { | ||
// AMD | ||
define(['chai'], factory); | ||
} else if (typeof module !== 'undefined') { | ||
// Node/CommonJS | ||
module.exports = factory(require('chai')); | ||
} else if (typeof window !== 'undefined') { | ||
chai.use(factory); | ||
} | ||
})(function (chai) { | ||
var Assertion = chai.Assertion; | ||
// define an AMD module that relies on 'chai' | ||
if (typeof define === 'function' && define.amd) { | ||
define(['chai'], factory); | ||
// define a Common JS module that relies on 'chai' | ||
} else if (typeof exports === 'object') { | ||
module.exports = factory(require('chai')); | ||
} | ||
function almostEqual (a, b, delta) { | ||
return Math.abs(a - b) < delta; | ||
} | ||
if (typeof window !== 'undefined' && typeof chai !== 'undefined') { | ||
chai.use(factory); | ||
} | ||
}(function (chai) { | ||
var Assertion = chai.Assertion; | ||
function deepAlmostEqual (actual, expected, delta, error) { | ||
if (delta === undefined) { | ||
throw new Error('No delta provided'); | ||
} | ||
error = error || {}; | ||
error.message = ''; | ||
function almostEqual(a, b, delta) { | ||
return Math.abs(a - b) < delta; | ||
} | ||
var i; | ||
if (Array.isArray(actual)) { | ||
for (i = 0; i < actual.length; i++) { | ||
if (!deepAlmostEqual(actual[i], expected[i], delta)) { | ||
error.message = ' at index ' + i; | ||
if (Array.isArray(actual[i]) || typeof actual[i] === 'object') { | ||
error.message += ': ' + actual[i] + ' should be almost equal to ' + expected[i]; | ||
} | ||
function deepAlmostEqual(actual, expected, delta, error) { | ||
if (delta === undefined) { | ||
throw new Error('No delta provided'); | ||
} | ||
error = error || {}; | ||
error.message = ''; | ||
return false; | ||
} | ||
} | ||
} else if (typeof actual === 'object') { | ||
for (i in actual) { | ||
if (typeof actual[i] === 'function') { | ||
continue; | ||
} | ||
if (!deepAlmostEqual(actual[i], expected[i], delta)) { | ||
error.message = ' at key ' + i; | ||
if (Array.isArray(actual[i]) || typeof actual[i] === 'object') { | ||
error.message += ': ' + actual[i] + ' should be almost equal to ' + expected[i]; | ||
} | ||
var i; | ||
if (Array.isArray(actual)) { | ||
for (i = 0; i < actual.length; i++) { | ||
if (!deepAlmostEqual(actual[i], expected[i], delta)) { | ||
error.message = ' at index ' + i; | ||
if (Array.isArray(actual[i]) || typeof actual[i] === 'object') { | ||
error.message += ': ' + actual[i] + ' should be almost equal to ' + expected[i]; | ||
} | ||
return false; | ||
} | ||
} | ||
} else { | ||
return almostEqual(actual, expected, delta); | ||
} | ||
return true; | ||
} | ||
return false; | ||
} | ||
} | ||
} else if (typeof actual === 'object') { | ||
for (i in actual) { | ||
if (typeof actual[i] === 'function') { | ||
continue; | ||
} | ||
if (!deepAlmostEqual(actual[i], expected[i], delta)) { | ||
error.message = ' at key ' + i; | ||
if (Array.isArray(actual[i]) || typeof actual[i] === 'object') { | ||
error.message += ': ' + actual[i] + ' should be almost equal to ' + expected[i]; | ||
} | ||
Assertion.addMethod('deepAlmostEqual', function (expected, delta) { | ||
var error = {}; | ||
this.assert( | ||
deepAlmostEqual(this._obj, expected, delta, error), | ||
'expected #{act} to be almost equal to #{exp}' + error.message, | ||
'expected #{act} to be not almost equal to #{exp}' + error.message, | ||
expected, | ||
this._obj | ||
); | ||
}); | ||
return false; | ||
} | ||
} | ||
} else { | ||
return almostEqual(actual, expected, delta); | ||
} | ||
return true; | ||
} | ||
function nearLatLng (expected, delta) { | ||
delta = delta || 1e-4; | ||
Assertion.addMethod('deepAlmostEqual', function (expected, delta) { | ||
var error = {}; | ||
this.assert( | ||
deepAlmostEqual(this._obj, expected, delta, error), | ||
'expected #{act} to be almost equal to #{exp}' + error.message, | ||
'expected #{act} to be not almost equal to #{exp}' + error.message, | ||
expected, | ||
this._obj | ||
); | ||
}); | ||
new Assertion( | ||
this._obj, | ||
'expected #{act} to be a L.LatLng object' | ||
).to.be.an.instanceof(L.LatLng); | ||
function nearLatLng(expected, delta) { | ||
delta = delta || 1e-4; | ||
var actual = this._obj; | ||
expected = L.latLng(expected); | ||
new Assertion( | ||
this._obj, | ||
'expected #{act} to be a L.LatLng object' | ||
).to.be.an.instanceof(L.LatLng); | ||
this.assert( | ||
deepAlmostEqual(actual, expected, delta), | ||
'expected #{act} to be near #{exp}', | ||
'expected #{act} not to be near #{exp}', | ||
expected.toString(), | ||
actual.toString() | ||
); | ||
} | ||
Assertion.addMethod('nearLatLng', nearLatLng); | ||
Assertion.addMethod('near', nearLatLng); | ||
var actual = this._obj; | ||
expected = L.latLng(expected); | ||
Assertion.addMethod('zoom', function (zoom) { | ||
new Assertion( | ||
this._obj, | ||
'expected #{act} to be a L.Map' | ||
).to.be.an.instanceof(L.Map); | ||
this.assert( | ||
deepAlmostEqual(actual, expected, delta), | ||
'expected #{act} to be near #{exp}', | ||
'expected #{act} not to be near #{exp}', | ||
expected.toString(), | ||
actual.toString() | ||
); | ||
} | ||
Assertion.addMethod('nearLatLng', nearLatLng); | ||
Assertion.addMethod('near', nearLatLng); | ||
new Assertion( | ||
zoom, | ||
'expect zoom to be a number' | ||
).to.be.a('number'); | ||
Assertion.addMethod('zoom', function (zoom) { | ||
new Assertion( | ||
this._obj, | ||
'expected #{act} to be a L.Map' | ||
).to.be.an.instanceof(L.Map); | ||
var actual = this._obj.getZoom(); | ||
new Assertion( | ||
zoom, | ||
'expect zoom to be a number' | ||
).to.be.a('number'); | ||
this.assert( | ||
actual === zoom, | ||
'expected zoom #{act} to be #{exp}', | ||
'expected zoom #{act} not to be #{exp}', | ||
zoom, | ||
actual | ||
); | ||
}); | ||
var actual = this._obj.getZoom(); | ||
Assertion.addMethod('view', function (center, zoom, delta) { | ||
new Assertion( | ||
this._obj, | ||
'expected #{act} to be a L.Map' | ||
).to.be.an.instanceof(L.Map); | ||
this.assert( | ||
actual === zoom, | ||
'expected zoom #{act} to be #{exp}', | ||
'expected zoom #{act} not to be #{exp}', | ||
zoom, | ||
actual | ||
); | ||
}); | ||
var map = this._obj; | ||
Assertion.addMethod('view', function (center, zoom, delta) { | ||
new Assertion( | ||
this._obj, | ||
'expected #{act} to be a L.Map' | ||
).to.be.an.instanceof(L.Map); | ||
L.latLng(center) | ||
.should.be.nearLatLng(map.getCenter(), delta); | ||
var map = this._obj; | ||
if (zoom !== undefined) { | ||
map.should.have.zoom(zoom); | ||
} | ||
}); | ||
L.latLng(center) | ||
.should.be.nearLatLng(map.getCenter(), delta); | ||
return chai; | ||
if (zoom !== undefined) { | ||
map.should.have.zoom(zoom); | ||
} | ||
}); | ||
return chai; | ||
}, window)); | ||
}); |
{ | ||
"name": "chai-leaflet", | ||
"version": "0.0.11", | ||
"version": "0.0.12", | ||
"description": "Chai assertions to use with Leaflet map apps", | ||
@@ -8,5 +8,5 @@ "main": "chai-leaflet.js", | ||
"test": "npm run lint && npm run test-browser", | ||
"lint": "eslint chai-leaflet.js", | ||
"lint": "eslint chai-leaflet.js test/*", | ||
"test-node": "mocha test/test.js", | ||
"test-browser": "mocha-phantomjs test/index.html", | ||
"test-browser": "phantomjs ./node_modules/mocha-phantomjs-core/mocha-phantomjs-core.js test/index.html", | ||
"release": "mversion patch -m" | ||
@@ -30,7 +30,7 @@ }, | ||
"chai": "^3.5.0", | ||
"eslint": "^1.10.3", | ||
"leaflet": "~0.7.5", | ||
"eslint": "^2.7.0", | ||
"eslint-plugin-html": "^1.4.0", | ||
"leaflet": "^1.0.0-rc.1", | ||
"mocha": "^2.4.5", | ||
"mocha-phantomjs": "^4.0.2", | ||
"mversion": "^1.10.1", | ||
"mocha-phantomjs-core": "^2.0.1", | ||
"phantomjs-prebuilt": "^2.1.4" | ||
@@ -37,0 +37,0 @@ }, |
249
test/test.js
'use strict'; | ||
if (typeof window === 'undefined') { | ||
try { | ||
var L = require('leaflet-headless'); | ||
} catch(e) { | ||
throw 'Package leaflet-headless needs to be installed: npm install leaflet-headless'; | ||
} | ||
var chai = require('chai'); | ||
try { | ||
var L = require('leaflet-headless'); | ||
} catch (e) { | ||
throw 'Package leaflet-headless needs to be installed: npm install leaflet-headless'; | ||
} | ||
var chai = require('chai'); | ||
require('../chai-leaflet.js'); | ||
require('../chai-leaflet.js'); | ||
} | ||
describe('chai-leaflet', function () { | ||
chai.should(); | ||
chai.should(); | ||
describe('deepAlmostEqual', function () { | ||
it('array of numbers', function () { | ||
[1, 2].should.be.deepAlmostEqual([1, 2], 0.1); | ||
[1.0, 2.0].should.be.deepAlmostEqual([1, 2], 0.1); | ||
[1.01, 2.02].should.be.deepAlmostEqual([1, 2], 0.1); | ||
}); | ||
describe('deepAlmostEqual', function () { | ||
it('array of numbers', function () { | ||
[1, 2].should.be.deepAlmostEqual([1, 2], 0.1); | ||
[1.0, 2.0].should.be.deepAlmostEqual([1, 2], 0.1); | ||
[1.01, 2.02].should.be.deepAlmostEqual([1, 2], 0.1); | ||
}); | ||
it('negated version', function () { | ||
[1, 2].should.not.be.deepAlmostEqual([1, 3], 1); | ||
}); | ||
it('negated version', function () { | ||
[1, 2].should.not.be.deepAlmostEqual([1, 3], 1); | ||
}); | ||
it('nested arrays with numbers', function () { | ||
var array = [ | ||
[1.001, 1002], | ||
[1, 2], | ||
[3, 4], | ||
[ [4, 5], [5, 6] ], | ||
3.44 | ||
]; | ||
it('nested arrays with numbers', function () { | ||
var array = [ | ||
[1.001, 1002], | ||
[1, 2], | ||
[3, 4], | ||
[ [4, 5], [5, 6] ], | ||
3.44 | ||
]; | ||
array.should.be.deepAlmostEqual(array, 0.1); | ||
}); | ||
array.should.be.deepAlmostEqual(array, 0.1); | ||
}); | ||
it('map with arrays and numbers', function () { | ||
var obj = { | ||
foo: [1, 3.00], | ||
floats: 0.3234, | ||
options: { | ||
center: [3, 4] | ||
} | ||
}; | ||
it('map with arrays and numbers', function () { | ||
var obj = { | ||
foo: [1, 3.00], | ||
floats: 0.3234, | ||
options: { | ||
center: [3, 4] | ||
} | ||
}; | ||
obj.should.be.deepAlmostEqual(obj, 0.1); | ||
}); | ||
obj.should.be.deepAlmostEqual(obj, 0.1); | ||
}); | ||
describe('error messages', function () { | ||
var error, fn; | ||
beforeEach(function () { | ||
error = ''; | ||
fn = null; | ||
}); | ||
describe('error messages', function () { | ||
var fn; | ||
it('should report the right index', function () { | ||
fn = function () { | ||
[1, 2, 3, 4].should.be.deepAlmostEqual([1, 2, 3, 5], 0.1); | ||
}; | ||
it('should report the right index', function () { | ||
fn = function () { | ||
[1, 2, 3, 4].should.be.deepAlmostEqual([1, 2, 3, 5], 0.1); | ||
}; | ||
fn.should.throw('expected [ 1, 2, 3, 4 ] to be almost equal to [ 1, 2, 3, 5 ] at index 3'); | ||
}); | ||
it('should display the second level if type is array/object', function () { | ||
fn = function () { | ||
[ | ||
[1, 2], | ||
[2, 3] | ||
].should.be.deepAlmostEqual([ | ||
[1, 2], | ||
[4, 4] | ||
], 0.1); | ||
}; | ||
fn.should.throw('expected [ [ 1, 2 ], [ 2, 3 ] ] to be almost equal to ' + | ||
'[ [ 1, 2 ], [ 4, 4 ] ] at index 1: 2,3 should be almost equal to 4,4'); | ||
}); | ||
fn.should.throw('expected [ 1, 2, 3, 4 ] to be almost equal to [ 1, 2, 3, 5 ] at index 3'); | ||
}); | ||
it('should display the second level if type is array/object', function () { | ||
fn = function () { | ||
[ | ||
[1, 2], | ||
[2, 3] | ||
].should.be.deepAlmostEqual([ | ||
[1, 2], | ||
[4, 4] | ||
], 0.1); | ||
}; | ||
fn.should.throw('expected [ [ 1, 2 ], [ 2, 3 ] ] to be almost equal to ' + | ||
'[ [ 1, 2 ], [ 4, 4 ] ] at index 1: 2,3 should be almost equal to 4,4'); | ||
}); | ||
it('should report the key for objects', function () { | ||
fn = function () { | ||
var obj = { | ||
foo: [1, 2], | ||
bar: [1, 2, 3] | ||
}; | ||
it('should report the key for objects', function () { | ||
fn = function () { | ||
var obj = { | ||
foo: [1, 2], | ||
bar: [1, 2, 3] | ||
}; | ||
obj.should.be.deepAlmostEqual({ | ||
foo: [1, 2], | ||
bar: [1, 3, 4] | ||
}, 0.1); | ||
}; | ||
fn.should.throw('expected { foo: [ 1, 2 ], bar: [ 1, 2, 3 ] } to be almost equal to ' + | ||
'{ foo: [ 1, 2 ], bar: [ 1, 3, 4 ] } at key bar: 1,2,3 should be almost equal to 1,3,4'); | ||
}); | ||
}); | ||
}); | ||
obj.should.be.deepAlmostEqual({ | ||
foo: [1, 2], | ||
bar: [1, 3, 4] | ||
}, 0.1); | ||
}; | ||
fn.should.throw('expected { foo: [ 1, 2 ], bar: [ 1, 2, 3 ] } to be almost equal to ' + | ||
'{ foo: [ 1, 2 ], bar: [ 1, 3, 4 ] } at key bar: 1,2,3 should be almost equal to 1,3,4'); | ||
}); | ||
}); | ||
}); | ||
describe('should.be.', function () { | ||
describe('near', function () { | ||
it('works', function () { | ||
var latlng = L.latLng(51, 4); | ||
describe('should.be.', function () { | ||
describe('near', function () { | ||
it('works', function () { | ||
var latlng = L.latLng(51, 4); | ||
latlng.should.be.near(latlng); | ||
latlng.should.be.near([51, 4]); | ||
}); | ||
latlng.should.be.near(latlng); | ||
latlng.should.be.near([51, 4]); | ||
}); | ||
it('negating works', function () { | ||
var latlng = L.latLng(51, 4); | ||
latlng.should.not.be.near(L.latLng([4, 51])); | ||
}); | ||
it('negating works', function () { | ||
var latlng = L.latLng(51, 4); | ||
latlng.should.not.be.near(L.latLng([4, 51])); | ||
}); | ||
it('with bigger delta', function () { | ||
var latlng = L.latLng(51.0001, 4.0009); | ||
it('with bigger delta', function () { | ||
var latlng = L.latLng(51.0001, 4.0009); | ||
var delta = 0.1; | ||
var delta = 0.1; | ||
latlng.should.be.near(L.latLng(51.01, 4.01), delta); | ||
latlng.should.not.be.near(L.latLng(51.4, 4.4), delta); | ||
}); | ||
}); | ||
}); | ||
latlng.should.be.near(L.latLng(51.01, 4.01), delta); | ||
latlng.should.not.be.near(L.latLng(51.4, 4.4), delta); | ||
}); | ||
}); | ||
}); | ||
describe('should.have.', function () { | ||
describe('zoom', function () { | ||
it('fails for non-map objects', function () { | ||
var fn = function () { | ||
L.latLng([1, 2]).should.have.zoom([1, 2]); | ||
}; | ||
describe('should.have.', function () { | ||
describe('zoom', function () { | ||
it('fails for non-map objects', function () { | ||
var fn = function () { | ||
L.latLng([1, 2]).should.have.zoom([1, 2]); | ||
}; | ||
fn.should.throw; | ||
}); | ||
fn.should.throw; | ||
}); | ||
it('checks zoom correctly', function () { | ||
var map = L.map(document.createElement('div')); | ||
it('checks zoom correctly', function () { | ||
var map = L.map(document.createElement('div')); | ||
map.setView([1, 2], 3); | ||
map.setView([1, 2], 3); | ||
map.should.have.zoom(3); | ||
map.should.not.have.zoom(4); | ||
}); | ||
}); | ||
map.should.have.zoom(3); | ||
map.should.not.have.zoom(4); | ||
}); | ||
}); | ||
describe('view', function () { | ||
it('fails for non-map objects', function () { | ||
var fn = function () { | ||
L.latLng([1, 2]).should.have.view([1, 2]); | ||
}; | ||
describe('view', function () { | ||
it('fails for non-map objects', function () { | ||
var fn = function () { | ||
L.latLng([1, 2]).should.have.view([1, 2]); | ||
}; | ||
fn.should.throw; | ||
}); | ||
fn.should.throw; | ||
}); | ||
it('checks view correctly', function () { | ||
var map = L.map(document.createElement('div')); | ||
it('checks view correctly', function () { | ||
var map = L.map(document.createElement('div')); | ||
map.setView([1, 2], 3); | ||
map.setView([1, 2], 3); | ||
map.should.have.view([1, 2], 3); | ||
}); | ||
}); | ||
}); | ||
map.should.have.view([1, 2], 3); | ||
}); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
13725
242