Add node tests

This commit is contained in:
Tim Schaub
2021-04-28 15:00:04 -07:00
parent c301d2413b
commit 7b934a06be
61 changed files with 1216 additions and 449 deletions

8
test/node/.eslintrc Normal file
View File

@@ -0,0 +1,8 @@
{
"env": {
"mocha": true
},
"parserOptions": {
"ecmaVersion": 2017
}
}

36
test/node/expect.js Normal file
View File

@@ -0,0 +1,36 @@
import expect from 'expect.js';
/**
* Assert value is within some tolerance of a number.
* @param {number} n Number.
* @param {number} tol Tolerance.
* @return {expect.Assertion} The assertion.
*/
expect.Assertion.prototype.roughlyEqual = function (n, tol) {
this.assert(
Math.abs(this.obj - n) <= tol,
function () {
return (
'expected ' +
expect.stringify(this.obj) +
' to be within ' +
tol +
' of ' +
n
);
},
function () {
return (
'expected ' +
expect.stringify(this.obj) +
' not to be within ' +
tol +
' of ' +
n
);
}
);
return this;
};
export default expect;

View File

@@ -0,0 +1,36 @@
import AssertionError from '../../../src/ol/AssertionError.js';
import expect from '../expect.js';
import {VERSION} from '../../../src/ol/util.js';
describe('ol/AssertionError.js', function () {
it('generates an error', function () {
const error = new AssertionError(42);
expect(error).to.be.an(Error);
});
it('generates a message with a versioned url', function () {
const error = new AssertionError(42);
const path = VERSION ? VERSION.split('-')[0] : 'latest';
expect(error.message).to.be(
'Assertion failed. See https://openlayers.org/en/' +
path +
'/doc/errors/#42 for details.'
);
});
it('has an error code', function () {
const error = new AssertionError(42);
expect(error.code).to.be(42);
});
it('has a name', function () {
const error = new AssertionError(42);
expect(error.name).to.be('AssertionError');
});
it('is instanceof Error and AssertionError', function () {
const error = new AssertionError(42);
expect(error instanceof Error).to.be(true);
expect(error instanceof AssertionError).to.be(true);
});
});

View File

@@ -0,0 +1,369 @@
import Collection from '../../../src/ol/Collection.js';
import CollectionEventType from '../../../src/ol/CollectionEventType.js';
import expect from '../expect.js';
import sinon from 'sinon';
import {listen} from '../../../src/ol/events.js';
describe('ol/Collection.js', function () {
let collection;
beforeEach(function () {
collection = new Collection();
});
describe('create an empty collection', function () {
it('creates an empty collection', function () {
expect(collection.getLength()).to.eql(0);
expect(collection.getArray()).to.be.empty();
expect(collection.item(0)).to.be(undefined);
});
});
describe('create a collection from an array', function () {
it('creates the expected collection', function () {
const array = [0, 1, 2];
const collection = new Collection(array);
expect(collection.item(0)).to.eql(0);
expect(collection.item(1)).to.eql(1);
expect(collection.item(2)).to.eql(2);
});
});
describe('push to a collection', function () {
it('adds elements to the collection', function () {
const length = collection.push(1);
expect(collection.getLength()).to.eql(length);
expect(collection.getArray()).to.eql([1]);
expect(collection.item(0)).to.eql(1);
});
it('returns the correct new length of the collection', function () {
let length;
listen(collection, 'add', function (event) {
if (event.element === 'remove_me') {
collection.remove(event.element);
}
});
length = collection.push('keep_me');
expect(collection.getLength()).to.eql(length);
length = collection.push('remove_me');
expect(collection.getLength()).to.eql(length);
});
});
describe('pop from a collection', function () {
it('removes elements from the collection', function () {
collection.push(1);
collection.pop();
expect(collection.getLength()).to.eql(0);
expect(collection.getArray()).to.be.empty();
expect(collection.item(0)).to.be(undefined);
});
});
describe('insertAt', function () {
it('inserts elements at the correct location', function () {
collection = new Collection([0, 2]);
collection.insertAt(1, 1);
expect(collection.item(0)).to.eql(0);
expect(collection.item(1)).to.eql(1);
expect(collection.item(2)).to.eql(2);
});
});
describe('setAt', function () {
it('sets at the correct location', function () {
collection.setAt(1, 1);
expect(collection.getLength()).to.eql(2);
expect(collection.item(0)).to.be(undefined);
expect(collection.item(1)).to.eql(1);
});
});
describe('removeAt', function () {
it('removes elements at the correction', function () {
const collection = new Collection([0, 1, 2]);
collection.removeAt(1);
expect(collection.item(0)).to.eql(0);
expect(collection.item(1)).to.eql(2);
});
});
describe('forEach', function () {
let cb;
beforeEach(function () {
cb = sinon.spy();
});
describe('on an empty collection', function () {
it('does not call the callback', function () {
collection.forEach(cb);
expect(cb.called).to.be(false);
});
});
describe('on a non-empty collection', function () {
it('does call the callback', function () {
collection.push(1);
collection.push(2);
collection.forEach(cb);
expect(cb.callCount).to.eql(2);
});
});
});
describe('remove', function () {
it('removes the first matching element', function () {
const collection = new Collection([0, 1, 2]);
expect(collection.remove(1)).to.eql(1);
expect(collection.getArray()).to.eql([0, 2]);
expect(collection.getLength()).to.eql(2);
});
it('fires a remove event', function () {
const collection = new Collection([0, 1, 2]);
const cb = sinon.spy();
listen(collection, CollectionEventType.REMOVE, cb);
expect(collection.remove(1)).to.eql(1);
expect(cb.called).to.be(true);
expect(cb.lastCall.args[0].element).to.eql(1);
});
it('does not remove more than one matching element', function () {
const collection = new Collection([0, 1, 1, 2]);
expect(collection.remove(1)).to.eql(1);
expect(collection.getArray()).to.eql([0, 1, 2]);
expect(collection.getLength()).to.eql(3);
});
it('returns undefined if the element is not found', function () {
const collection = new Collection([0, 1, 2]);
expect(collection.remove(3)).to.be(undefined);
expect(collection.getArray()).to.eql([0, 1, 2]);
expect(collection.getLength()).to.eql(3);
});
});
describe('setAt and event', function () {
it('does dispatch events', function () {
const collection = new Collection(['a', 'b']);
let added, removed, addedIndex, removedIndex;
listen(collection, CollectionEventType.ADD, function (e) {
added = e.element;
addedIndex = e.index;
});
listen(collection, CollectionEventType.REMOVE, function (e) {
removed = e.element;
removedIndex = e.index;
});
collection.setAt(1, 1);
expect(added).to.eql(1);
expect(addedIndex).to.eql(1);
expect(removed).to.eql('b');
expect(removedIndex).to.eql(1);
});
});
describe('removeAt and event', function () {
it('does dispatch events', function () {
const collection = new Collection(['a']);
let removed, removedIndex;
listen(collection, CollectionEventType.REMOVE, function (e) {
removed = e.element;
removedIndex = e.index;
});
collection.pop();
expect(removed).to.eql('a');
expect(removedIndex).to.eql(0);
});
});
describe('insertAt and event', function () {
it('does dispatch events', function () {
const collection = new Collection([0, 2]);
let added, addedIndex;
listen(collection, CollectionEventType.ADD, function (e) {
added = e.element;
addedIndex = e.index;
});
collection.insertAt(1, 1);
expect(added).to.eql(1);
expect(addedIndex).to.eql(1);
});
});
describe('setAt beyond end', function () {
it('triggers events properly', function () {
const added = [],
addedIndexes = [];
listen(collection, CollectionEventType.ADD, function (e) {
added.push(e.element);
addedIndexes.push(e.index);
});
collection.setAt(2, 0);
expect(collection.getLength()).to.eql(3);
expect(collection.item(0)).to.be(undefined);
expect(collection.item(1)).to.be(undefined);
expect(collection.item(2)).to.eql(0);
expect(added.length).to.eql(3);
expect(added[0]).to.eql(undefined);
expect(added[1]).to.eql(undefined);
expect(added[2]).to.eql(0);
expect(addedIndexes).to.eql([0, 1, 2]);
});
});
describe('change:length event', function () {
let collection, cb;
beforeEach(function () {
collection = new Collection([0, 1, 2]);
cb = sinon.spy();
listen(collection, 'change:length', cb);
});
describe('insertAt', function () {
it('triggers change:length event', function () {
collection.insertAt(2, 3);
expect(cb.called).to.be(true);
});
});
describe('removeAt', function () {
it('triggers change:length event', function () {
collection.removeAt(0);
expect(cb.called).to.be(true);
});
});
describe('setAt', function () {
it('does not trigger change:length event', function () {
collection.setAt(1, 1);
expect(cb.called).to.be(false);
});
});
});
describe('add event', function () {
it('triggers add when pushing', function () {
const collection = new Collection();
let elem, addedIndex;
listen(collection, CollectionEventType.ADD, function (e) {
elem = e.element;
addedIndex = e.index;
});
const length = collection.push(1);
expect(elem).to.eql(length);
expect(addedIndex).to.eql(0);
});
});
describe('remove event', function () {
let collection, cb1, cb2;
beforeEach(function () {
collection = new Collection([1]);
cb1 = sinon.spy();
cb2 = sinon.spy();
});
describe('setAt', function () {
it('triggers remove', function () {
listen(collection, CollectionEventType.ADD, cb1);
listen(collection, CollectionEventType.REMOVE, cb2);
collection.setAt(0, 2);
expect(cb2.lastCall.args[0].element).to.eql(1);
expect(cb1.lastCall.args[0].element).to.eql(2);
});
});
describe('pop', function () {
it('triggers remove', function () {
listen(collection, CollectionEventType.REMOVE, cb1);
collection.pop();
expect(cb1.lastCall.args[0].element).to.eql(1);
});
});
});
describe('extending a collection', function () {
it('adds elements to end of the collection', function () {
collection.extend([1, 2]);
expect(collection.getLength()).to.eql(2);
expect(collection.getArray()).to.eql([1, 2]);
expect(collection.item(0)).to.eql(1);
expect(collection.item(1)).to.eql(2);
});
it('fires events', function () {
const collection = new Collection();
const elems = [],
addedIndexes = [];
listen(collection, CollectionEventType.ADD, function (e) {
elems.push(e.element);
addedIndexes.push(e.index);
});
collection.extend([1, 2]);
expect(elems).to.eql([1, 2]);
expect(addedIndexes).to.eql([0, 1]);
});
});
describe('unique collection', function () {
it('allows unique items in the constructor', function () {
new Collection([{}, {}, {}], {unique: true});
});
it('throws if duplicate items are passed to the constructor', function () {
const item = {};
const call = function () {
new Collection([item, item], {unique: true});
};
expect(call).to.throwException();
});
it('allows unique items to be added via push', function () {
const unique = new Collection(undefined, {unique: true});
unique.push({});
unique.push({});
});
it('throws if duplicate items are added via push', function () {
const unique = new Collection(undefined, {unique: true});
const item = {};
unique.push(item);
const call = function () {
unique.push(item);
};
expect(call).to.throwException();
});
it('allows unique items to be added via insertAt', function () {
const unique = new Collection(undefined, {unique: true});
unique.insertAt(0, {});
unique.insertAt(0, {});
});
it('throws if duplicate items are added via insertAt', function () {
const unique = new Collection(undefined, {unique: true});
const item = {};
unique.insertAt(0, item);
const call = function () {
unique.insertAt(0, item);
};
expect(call).to.throwException();
});
it('allows unique items to be added via setAt', function () {
const unique = new Collection(undefined, {unique: true});
unique.setAt(0, {});
unique.setAt(1, {});
});
it('allows items to be reset via setAt', function () {
const unique = new Collection(undefined, {unique: true});
const item = {};
unique.setAt(0, item);
unique.setAt(0, item);
});
it('throws if duplicate items are added via setAt', function () {
const unique = new Collection(undefined, {unique: true});
const item = {};
unique.setAt(0, item);
const call = function () {
unique.setAt(1, item);
};
expect(call).to.throwException();
});
});
});

View File

@@ -0,0 +1,37 @@
import Disposable from '../../../src/ol/Disposable.js';
import expect from '../expect.js';
import sinon from 'sinon';
describe('ol/Disposable.js', function () {
describe('constructor', function () {
it('creates an instance', function () {
const disposable = new Disposable();
expect(disposable).to.be.a(Disposable);
});
});
describe('#disposed', function () {
it('is initially false', function () {
const disposable = new Disposable();
expect(disposable.disposed).to.be(false);
});
it('is true after a call to dispose', function () {
const disposable = new Disposable();
disposable.dispose();
expect(disposable.disposed).to.be(true);
});
});
describe('#dispose()', function () {
it('calls disposeInternal only once', function () {
const disposable = new Disposable();
sinon.spy(disposable, 'disposeInternal');
expect(disposable.disposeInternal.called).to.be(false);
disposable.dispose();
expect(disposable.disposeInternal.callCount).to.be(1);
disposable.dispose();
expect(disposable.disposeInternal.callCount).to.be(1);
});
});
});

View File

@@ -0,0 +1,436 @@
import Feature, {createStyleFunction} from '../../../src/ol/Feature.js';
import Point from '../../../src/ol/geom/Point.js';
import Style from '../../../src/ol/style/Style.js';
import expect from '../expect.js';
import sinon from 'sinon';
import {isEmpty} from '../../../src/ol/obj.js';
describe('ol/Feature.js', function () {
describe('constructor', function () {
it('creates a new feature', function () {
const feature = new Feature();
expect(feature).to.be.a(Feature);
});
it('takes properties', function () {
const feature = new Feature({
foo: 'bar',
});
expect(feature.get('foo')).to.be('bar');
});
it("can store the feature's commonly used id", function () {
const feature = new Feature();
feature.setId('foo');
expect(feature.getId()).to.be('foo');
});
it('will set the default geometry', function () {
const feature = new Feature({
geometry: new Point([10, 20]),
foo: 'bar',
});
const geometry = feature.getGeometry();
expect(geometry).to.be.a(Point);
expect(feature.get('geometry')).to.be(geometry);
});
});
describe('#get()', function () {
it('returns values set at construction', function () {
const feature = new Feature({
a: 'first',
b: 'second',
});
expect(feature.get('a')).to.be('first');
expect(feature.get('b')).to.be('second');
});
it('returns undefined for unset attributes', function () {
const feature = new Feature();
expect(feature.get('a')).to.be(undefined);
});
it('returns values set by set', function () {
const feature = new Feature();
feature.set('a', 'b');
expect(feature.get('a')).to.be('b');
});
});
describe('#getProperties()', function () {
it('returns an object with all attributes', function () {
const point = new Point([15, 30]);
const feature = new Feature({
foo: 'bar',
ten: 10,
geometry: point,
});
const attributes = feature.getProperties();
const keys = Object.keys(attributes);
expect(keys.sort()).to.eql(['foo', 'geometry', 'ten']);
expect(attributes.foo).to.be('bar');
expect(attributes.geometry).to.be(point);
expect(attributes.ten).to.be(10);
});
it('is empty by default', function () {
const feature = new Feature();
const properties = feature.getProperties();
expect(isEmpty(properties)).to.be(true);
});
});
describe('#getGeometry()', function () {
const point = new Point([15, 30]);
it('returns undefined for unset geometry', function () {
const feature = new Feature();
expect(feature.getGeometry()).to.be(undefined);
});
it('returns null for null geometry (constructor)', function () {
const feature = new Feature(null);
expect(feature.getGeometry()).to.be(undefined);
});
it('returns null for null geometry (setGeometry())', function () {
const feature = new Feature();
feature.setGeometry(null);
expect(feature.getGeometry()).to.be(null);
});
it('gets the geometry set at construction', function () {
const feature = new Feature({
geometry: point,
});
expect(feature.getGeometry()).to.be(point);
});
it('gets any geometry set by setGeometry', function () {
const feature = new Feature();
feature.setGeometry(point);
expect(feature.getGeometry()).to.be(point);
const point2 = new Point([1, 2]);
feature.setGeometry(point2);
expect(feature.getGeometry()).to.be(point2);
});
});
describe('#set()', function () {
it('sets values', function () {
const feature = new Feature({
a: 'first',
b: 'second',
});
feature.set('a', 'new');
expect(feature.get('a')).to.be('new');
});
it('can be used to set the geometry', function () {
const point = new Point([3, 4]);
const feature = new Feature({
geometry: new Point([1, 2]),
});
feature.set('geometry', point);
expect(feature.get('geometry')).to.be(point);
expect(feature.getGeometry()).to.be(point);
});
it('can be used to set attributes with arbitrary names', function () {
const feature = new Feature();
feature.set('toString', 'string');
expect(feature.get('toString')).to.be('string');
expect(typeof feature.toString).to.be('function');
feature.set('getGeometry', 'x');
expect(feature.get('getGeometry')).to.be('x');
feature.set('geometry', new Point([1, 2]));
expect(feature.getGeometry()).to.be.a(Point);
});
});
describe('#setGeometry()', function () {
const point = new Point([15, 30]);
it('sets the default geometry', function () {
const feature = new Feature();
feature.setGeometry(point);
expect(feature.get('geometry')).to.be(point);
});
it('replaces previous default geometry', function () {
const feature = new Feature({
geometry: point,
});
expect(feature.getGeometry()).to.be(point);
const point2 = new Point([1, 2]);
feature.setGeometry(point2);
expect(feature.getGeometry()).to.be(point2);
});
});
describe('#setGeometryName()', function () {
const point = new Point([15, 30]);
it('sets property where to to look at geometry', function () {
const feature = new Feature();
feature.setGeometry(point);
expect(feature.getGeometry()).to.be(point);
const point2 = new Point([1, 2]);
feature.set('altGeometry', point2);
expect(feature.getGeometry()).to.be(point);
feature.setGeometryName('altGeometry');
expect(feature.getGeometry()).to.be(point2);
feature.on('change', function () {
expect().fail();
});
point.setCoordinates([0, 2]);
});
it('changes property listener', function () {
const feature = new Feature();
feature.setGeometry(point);
const point2 = new Point([1, 2]);
feature.set('altGeometry', point2);
feature.setGeometryName('altGeometry');
const spy = sinon.spy();
feature.on('change', spy);
point2.setCoordinates([0, 2]);
expect(spy.callCount).to.be(1);
});
it('can use a different geometry name', function () {
const feature = new Feature();
feature.setGeometryName('foo');
const point = new Point([10, 20]);
feature.setGeometry(point);
expect(feature.getGeometry()).to.be(point);
});
});
describe('#setId()', function () {
it('sets the feature identifier', function () {
const feature = new Feature();
expect(feature.getId()).to.be(undefined);
feature.setId('foo');
expect(feature.getId()).to.be('foo');
});
it('accepts a string or number', function () {
const feature = new Feature();
feature.setId('foo');
expect(feature.getId()).to.be('foo');
feature.setId(2);
expect(feature.getId()).to.be(2);
});
it('dispatches the "change" event', function (done) {
const feature = new Feature();
feature.on('change', function () {
expect(feature.getId()).to.be('foo');
done();
});
feature.setId('foo');
});
});
describe('#getStyleFunction()', function () {
const styleFunction = function (feature, resolution) {
return null;
};
it('returns undefined after construction', function () {
const feature = new Feature();
expect(feature.getStyleFunction()).to.be(undefined);
});
it('returns the function passed to setStyle', function () {
const feature = new Feature();
feature.setStyle(styleFunction);
expect(feature.getStyleFunction()).to.be(styleFunction);
});
it('does not get confused with user "styleFunction" property', function () {
const feature = new Feature();
feature.set('styleFunction', 'foo');
expect(feature.getStyleFunction()).to.be(undefined);
});
it('does not get confused with "styleFunction" option', function () {
const feature = new Feature({
styleFunction: 'foo',
});
expect(feature.getStyleFunction()).to.be(undefined);
});
});
describe('#setStyle()', function () {
const style = new Style();
const styleFunction = function (feature, resolution) {
return resolution;
};
it('accepts a single style', function () {
const feature = new Feature();
feature.setStyle(style);
const func = feature.getStyleFunction();
expect(func()).to.eql([style]);
});
it('accepts an array of styles', function () {
const feature = new Feature();
feature.setStyle([style]);
const func = feature.getStyleFunction();
expect(func()).to.eql([style]);
});
it('accepts a style function', function () {
const feature = new Feature();
feature.setStyle(styleFunction);
expect(feature.getStyleFunction()).to.be(styleFunction);
expect(feature.getStyleFunction()(feature, 42)).to.be(42);
});
it('accepts null', function () {
const feature = new Feature();
feature.setStyle(style);
feature.setStyle(null);
expect(feature.getStyle()).to.be(null);
expect(feature.getStyleFunction()).to.be(undefined);
});
it('dispatches a change event', function () {
const feature = new Feature();
const spy = sinon.spy();
feature.on('change', spy);
feature.setStyle(style);
expect(spy.callCount).to.be(1);
});
});
describe('#getStyle()', function () {
const style = new Style();
const styleFunction = function (feature, resolution) {
return null;
};
it('returns what is passed to setStyle', function () {
const feature = new Feature();
expect(feature.getStyle()).to.be(null);
feature.setStyle(style);
expect(feature.getStyle()).to.be(style);
feature.setStyle([style]);
expect(feature.getStyle()).to.eql([style]);
feature.setStyle(styleFunction);
expect(feature.getStyle()).to.be(styleFunction);
});
it('does not get confused with "style" option to constructor', function () {
const feature = new Feature({
style: 'foo',
});
expect(feature.getStyle()).to.be(null);
});
it('does not get confused with user set "style" property', function () {
const feature = new Feature();
feature.set('style', 'foo');
expect(feature.getStyle()).to.be(null);
});
});
describe('#clone', function () {
it('correctly clones features', function () {
const feature = new Feature();
feature.setProperties({'fookey': 'fooval'});
feature.setId(1);
feature.setGeometryName('geom');
const geometry = new Point([1, 2]);
feature.setGeometry(geometry);
const style = new Style({});
feature.setStyle(style);
feature.set('barkey', 'barval');
const clone = feature.clone();
expect(clone.get('fookey')).to.be('fooval');
expect(clone.getId()).to.be(undefined);
expect(clone.getGeometryName()).to.be('geom');
const geometryClone = clone.getGeometry();
expect(geometryClone).not.to.be(geometry);
const coordinates = geometryClone.getFlatCoordinates();
expect(coordinates[0]).to.be(1);
expect(coordinates[1]).to.be(2);
expect(clone.getStyle()).to.be(style);
expect(clone.get('barkey')).to.be('barval');
});
it('correctly clones features with no geometry and no style', function () {
const feature = new Feature();
feature.set('fookey', 'fooval');
const clone = feature.clone();
expect(clone.get('fookey')).to.be('fooval');
expect(clone.getGeometry()).to.be(undefined);
expect(clone.getStyle()).to.be(null);
});
});
describe('#setGeometry()', function () {
it('dispatches a change event when geometry is set to null', function () {
const feature = new Feature({
geometry: new Point([0, 0]),
});
const spy = sinon.spy();
feature.on('change', spy);
feature.setGeometry(null);
expect(spy.callCount).to.be(1);
});
});
});
describe('ol.Feature.createStyleFunction()', function () {
const style = new Style();
it('creates a feature style function from a single style', function () {
const styleFunction = createStyleFunction(style);
expect(styleFunction()).to.eql([style]);
});
it('creates a feature style function from an array of styles', function () {
const styleFunction = createStyleFunction([style]);
expect(styleFunction()).to.eql([style]);
});
it('passes through a function', function () {
const original = function (feature, resolution) {
return [style];
};
const styleFunction = createStyleFunction(original);
expect(styleFunction).to.be(original);
});
it('throws on (some) unexpected input', function () {
expect(function () {
createStyleFunction({bogus: 'input'});
}).to.throwException();
});
});

247
test/node/ol/Object.test.js Normal file
View File

@@ -0,0 +1,247 @@
import BaseObject from '../../../src/ol/Object.js';
import expect from '../expect.js';
import sinon from 'sinon';
import {listen} from '../../../src/ol/events.js';
describe('ol/Object.js', function () {
let o;
beforeEach(function () {
o = new BaseObject();
});
describe('get, set and unset', function () {
describe('get an unset property', function () {
let v;
beforeEach(function () {
v = o.get('k');
});
it('returns undefined', function () {
expect(v).to.be(undefined);
});
});
describe('get a set property', function () {
let v;
beforeEach(function () {
o.set('k', 1);
v = o.get('k');
});
it('returns expected value', function () {
expect(v).to.eql(1);
});
});
describe('unset a set property', function () {
beforeEach(function () {
o.set('k', 1);
});
it('returns undefined', function () {
const v = o.unset('k');
expect(v).to.be(undefined);
});
});
});
describe('#get()', function () {
it('does not return values that are not explicitly set', function () {
const o = new BaseObject();
expect(o.get('constructor')).to.be(undefined);
expect(o.get('hasOwnProperty')).to.be(undefined);
expect(o.get('isPrototypeOf')).to.be(undefined);
expect(o.get('propertyIsEnumerable')).to.be(undefined);
expect(o.get('toLocaleString')).to.be(undefined);
expect(o.get('toString')).to.be(undefined);
expect(o.get('valueOf')).to.be(undefined);
});
});
describe('#set()', function () {
it('can be used with arbitrary names', function () {
const o = new BaseObject();
o.set('set', 'sat');
expect(o.get('set')).to.be('sat');
o.set('get', 'got');
expect(o.get('get')).to.be('got');
o.set('toString', 'string');
expect(o.get('toString')).to.be('string');
expect(typeof o.toString).to.be('function');
});
});
describe('#getKeys()', function () {
it('returns property names set at construction', function () {
const o = new BaseObject({
prop1: 'val1',
prop2: 'val2',
toString: 'string',
get: 'foo',
});
const keys = o.getKeys();
expect(keys.length).to.be(4);
expect(keys.sort()).to.eql(['get', 'prop1', 'prop2', 'toString']);
});
});
describe('setProperties', function () {
it('sets multiple values at once', function () {
o.setProperties({
k1: 1,
k2: 2,
});
expect(o.get('k1')).to.eql(1);
expect(o.get('k2')).to.eql(2);
const keys = o.getKeys().sort();
expect(keys).to.eql(['k1', 'k2']);
});
});
describe('hasProperties', function () {
it('has no properties after creation', function () {
expect(o.hasProperties()).to.eql(false);
});
it('has properties after set', function () {
o.set('foo', 1);
expect(o.hasProperties()).to.eql(true);
});
it('has no properties after unset all', function () {
o.unset('foo');
expect(o.hasProperties()).to.eql(false);
});
});
describe('notify', function () {
let listener1, listener2;
beforeEach(function () {
listener1 = sinon.spy();
listen(o, 'change:k', listener1);
listener2 = sinon.spy();
listen(o, 'propertychange', listener2);
});
it('dispatches events', function () {
o.notify('k', 1);
expect(listener1.calledOnce).to.be(true);
const args = listener1.firstCall.args;
expect(args).to.have.length(1);
const event = args[0];
expect(event.key).to.be('k');
expect(event.oldValue).to.be(1);
});
it('dispatches generic change events to bound objects', function () {
o.notify('k', 1);
expect(listener2.calledOnce).to.be(true);
const args = listener2.firstCall.args;
expect(args).to.have.length(1);
const event = args[0];
expect(event.key).to.be('k');
expect(event.oldValue).to.be(1);
});
});
describe('set', function () {
let listener1, listener2;
beforeEach(function () {
listener1 = sinon.spy();
listen(o, 'change:k', listener1);
listener2 = sinon.spy();
listen(o, 'propertychange', listener2);
});
it('dispatches events to object', function () {
o.set('k', 1);
expect(listener1.called).to.be(true);
expect(o.getKeys()).to.eql(['k']);
});
it('dispatches generic change events to object', function () {
o.set('k', 1);
expect(listener2.calledOnce).to.be(true);
const args = listener2.firstCall.args;
expect(args).to.have.length(1);
const event = args[0];
expect(event.key).to.be('k');
});
it('dispatches events only if the value is different', function () {
o.set('k', 1);
o.set('k', 1);
expect(listener1.calledOnce).to.be(true);
expect(listener2.calledOnce).to.be(true);
});
});
describe('setter', function () {
beforeEach(function () {
o.setX = function (x) {
this.set('x', x);
};
sinon.spy(o, 'setX');
});
it('does not call the setter', function () {
o.set('x', 1);
expect(o.get('x')).to.eql(1);
expect(o.setX.called).to.be(false);
expect(o.getKeys()).to.eql(['x']);
});
});
describe('getter', function () {
beforeEach(function () {
o.getX = function () {
return 1;
};
sinon.spy(o, 'getX');
});
it('does not call the getter', function () {
expect(o.get('x')).to.be(undefined);
expect(o.getX.called).to.be(false);
});
});
describe('create with options', function () {
it('sets the property', function () {
const o = new BaseObject({k: 1});
expect(o.get('k')).to.eql(1);
expect(o.getKeys()).to.eql(['k']);
});
});
describe('case sensitivity', function () {
let listener1, listener2;
beforeEach(function () {
listener1 = sinon.spy();
listen(o, 'change:k', listener1);
listener2 = sinon.spy();
listen(o, 'change:K', listener2);
});
it('dispatches the expected event', function () {
o.set('K', 1);
expect(listener1.called).to.be(false);
expect(listener2.called).to.be(true);
expect(o.getKeys()).to.eql(['K']);
});
});
});

View File

@@ -0,0 +1,148 @@
import EventTarget from '../../../src/ol/events/Target.js';
import Observable, {unByKey} from '../../../src/ol/Observable.js';
import expect from '../expect.js';
import sinon from 'sinon';
describe('ol/Observable.js', function () {
describe('constructor', function () {
it('creates a new observable', function () {
const observable = new Observable();
expect(observable).to.be.a(Observable);
expect(observable).to.be.a(EventTarget);
});
});
describe('#on()', function () {
let observable, listener;
beforeEach(function () {
observable = new Observable();
listener = sinon.spy();
});
it('registers a listener for events of the given type', function () {
observable.on('foo', listener);
observable.dispatchEvent('foo');
expect(listener.calledOnce).to.be(true);
observable.dispatchEvent('foo');
expect(listener.callCount).to.be(2);
});
it('accepts an array of event types', function () {
observable.on(['foo', 'bar'], listener);
observable.dispatchEvent('foo');
expect(listener.calledOnce).to.be(true);
observable.dispatchEvent('bar');
expect(listener.callCount).to.be(2);
});
it('returns a listener key', function () {
const key = observable.on('foo', listener);
expect(typeof key).to.be('object');
});
});
describe('#once()', function () {
let observable, listener;
beforeEach(function () {
observable = new Observable();
listener = sinon.spy();
});
it('registers a listener that is only called once', function () {
observable.once('foo', listener);
observable.dispatchEvent('foo');
expect(listener.calledOnce).to.be(true);
observable.dispatchEvent('foo');
expect(listener.callCount).to.be(1);
});
it('is safe to dispatch events of same type in a once listener', function () {
let callCount = 0;
observable.once('change', function () {
observable.changed();
observable.changed();
});
observable.on('change', function () {
++callCount;
});
expect(function () {
observable.changed();
}).to.not.throwException();
expect(callCount).to.be(3);
});
it('accepts an array of event types (called once for each)', function () {
observable.once(['foo', 'bar'], listener);
observable.dispatchEvent('foo');
expect(listener.calledOnce).to.be(true);
observable.dispatchEvent('foo');
expect(listener.callCount).to.be(1);
observable.dispatchEvent('bar');
expect(listener.callCount).to.be(2);
observable.dispatchEvent('bar');
expect(listener.callCount).to.be(2);
});
it('returns a listener key', function () {
const key = observable.once('foo', listener);
expect(typeof key).to.be('object');
});
it('can be unregistered with un()', function () {
observable.once('foo', listener);
observable.un('foo', listener);
observable.dispatchEvent('foo');
expect(listener.callCount).to.be(0);
});
});
describe('#un()', function () {
let observable, listener;
beforeEach(function () {
observable = new Observable();
listener = sinon.spy();
});
it('unregisters a previously registered listener', function () {
observable.on('foo', listener);
observable.dispatchEvent('foo');
expect(listener.calledOnce).to.be(true);
observable.un('foo', listener);
observable.dispatchEvent('foo');
expect(listener.calledOnce).to.be(true);
});
});
describe('ol.Observable.unByKey()', function () {
let observable, listener;
beforeEach(function () {
observable = new Observable();
listener = sinon.spy();
});
it('unregisters a listener given the key returned by `on`', function () {
const key = observable.on('foo', listener);
observable.dispatchEvent('foo');
expect(listener.calledOnce).to.be(true);
unByKey(key);
observable.dispatchEvent('foo');
expect(listener.callCount).to.be(1);
});
});
});

612
test/node/ol/array.test.js Normal file
View File

@@ -0,0 +1,612 @@
import expect from '../expect.js';
import {
binarySearch,
equals,
extend,
find,
findIndex,
isSorted,
linearFindNearest,
numberSafeCompareFunction,
remove,
reverseSubArray,
stableSort,
} from '../../../src/ol/array.js';
describe('ol/array.js', function () {
describe('binarySearch', function () {
const insertionPoint = function (position) {
return -(position + 1);
};
const revNumCompare = function (a, b) {
return b - a;
};
describe('default comparison on array of String(s)', function () {
const a = [
'1000',
'9',
'AB',
'ABC',
'ABCABC',
'ABD',
'ABDA',
'B',
'B',
'B',
'C',
'CA',
'CC',
'ZZZ',
'ab',
'abc',
'abcabc',
'abd',
'abda',
'b',
'c',
'ca',
'cc',
'zzz',
];
it("should find '1000' at index 0", function () {
expect(binarySearch(a, '1000')).to.be(0);
});
it("should find 'zzz' at index " + (a.length - 1), function () {
expect(binarySearch(a, 'zzz')).to.be(a.length - 1);
});
it("should find 'C' at index 10", function () {
expect(binarySearch(a, 'C')).to.be(10);
});
it("should find 'B' at index 7 || 8 || 9", function () {
const pos = binarySearch(a, 'B');
expect(pos == 7 || pos == 8 || pos == 9).to.be.ok();
});
it("should not find '100'", function () {
const pos = binarySearch(a, '100');
expect(pos < 0).to.be.ok();
});
it("should have an insertion point of 0 for '100'", function () {
const pos = binarySearch(a, '100');
expect(insertionPoint(pos)).to.be(0);
});
it("should not find 'zzz0'", function () {
const pos = binarySearch(a, 'zzz0');
expect(pos < 0).to.be.ok();
});
it(
'should have an insertion point of ' + a.length + " for 'zzz0'",
function () {
const pos = binarySearch(a, 'zzz0');
expect(insertionPoint(pos)).to.be(a.length);
}
);
it("should not find 'BA'", function () {
const pos = binarySearch(a, 'zzz0');
expect(pos < 0).to.be.ok();
});
it("should have an insertion point of 10 for 'BA'", function () {
const pos = binarySearch(a, 'BA');
expect(insertionPoint(pos)).to.be(10);
});
});
describe('0 length array with default comparison', function () {
const b = [];
it("should not find 'a'", function () {
expect(binarySearch(b, 'a') < 0).to.be.ok();
});
it("should have an insertion point of 0 for 'a'", function () {
const pos = binarySearch(b, 'a');
expect(insertionPoint(pos)).to.be(0);
});
});
describe('single element array with default lexiographical comparison', function () {
const c = ['only item'];
it("should find 'only item' at index 0", function () {
expect(binarySearch(c, 'only item')).to.be(0);
});
it("should not find 'a'", function () {
expect(binarySearch(c, 'a') < 0).to.be.ok();
});
it("should have an insertion point of 0 for 'a'", function () {
const pos = binarySearch(c, 'a');
expect(insertionPoint(pos)).to.be(0);
});
it("should not find 'z'", function () {
expect(binarySearch(c, 'z') < 0).to.be.ok();
});
it("should have an insertion point of 1 for 'z'", function () {
const pos = binarySearch(c, 'z');
expect(insertionPoint(pos)).to.be(1);
});
});
describe('default comparison on array of Number(s)', function () {
const d = [
-897123.9,
-321434.58758,
-1321.3124,
-324,
-9,
-3,
0,
0,
0,
0.31255,
5,
142.88888708,
334,
342,
453,
54254,
];
it('should find -897123.9 at index 0', function () {
expect(binarySearch(d, -897123.9)).to.be(0);
});
it('should find 54254 at index ' + (d.length - 1), function () {
expect(binarySearch(d, 54254)).to.be(d.length - 1);
});
it('should find -3 at index 5', function () {
expect(binarySearch(d, -3)).to.be(5);
});
it('should find 0 at index 6 || 7 || 8', function () {
const pos = binarySearch(d, 0);
expect(pos == 6 || pos == 7 || pos == 8).to.be(true);
});
it('should not find -900000', function () {
const pos = binarySearch(d, -900000);
expect(pos < 0).to.be(true);
});
it('should have an insertion point of 0 for -900000', function () {
const pos = binarySearch(d, -900000);
expect(insertionPoint(pos)).to.be(0);
});
it('should not find 54255', function () {
const pos = binarySearch(d, 54255);
expect(pos < 0).to.be(true);
});
it(
'should have an insertion point of ' + d.length + ' for 54255',
function () {
const pos = binarySearch(d, 54255);
expect(insertionPoint(pos)).to.be(d.length);
}
);
it('should not find 1.1', function () {
const pos = binarySearch(d, 1.1);
expect(pos < 0).to.be(true);
});
it('should have an insertion point of 10 for 1.1', function () {
const pos = binarySearch(d, 1.1);
expect(insertionPoint(pos)).to.be(10);
});
});
describe('custom comparison function, which reverse orders numbers', function () {
const e = [
54254,
453,
342,
334,
142.88888708,
5,
0.31255,
0,
0,
0,
-3,
-9,
-324,
-1321.3124,
-321434.58758,
-897123.9,
];
it('should find 54254 at index 0', function () {
const pos = binarySearch(e, 54254, revNumCompare);
expect(pos).to.be(0);
});
it('should find -897123.9 at index ' + (e.length - 1), function () {
const pos = binarySearch(e, -897123.9, revNumCompare);
expect(pos).to.be(e.length - 1);
});
it('should find -3 at index 10', function () {
const pos = binarySearch(e, -3, revNumCompare);
expect(pos).to.be(10);
});
it('should find 0 at index 7 || 8 || 9', function () {
const pos = binarySearch(e, 0, revNumCompare);
expect(pos == 7 || pos == 8 || pos == 9).to.be(true);
});
it('should not find 54254.1', function () {
const pos = binarySearch(e, 54254.1, revNumCompare);
expect(pos < 0).to.be(true);
});
it('should have an insertion point of 0 for 54254.1', function () {
const pos = binarySearch(e, 54254.1, revNumCompare);
expect(insertionPoint(pos)).to.be(0);
});
it('should not find -897124', function () {
const pos = binarySearch(e, -897124, revNumCompare);
expect(pos < 0).to.be(true);
});
it(
'should have an insertion point of ' + e.length + ' for -897124',
function () {
const pos = binarySearch(e, -897124, revNumCompare);
expect(insertionPoint(pos)).to.be(e.length);
}
);
it('should not find 1.1', function () {
const pos = binarySearch(e, 1.1, revNumCompare);
expect(pos < 0).to.be(true);
});
it('should have an insertion point of 0 for 1.1', function () {
const pos = binarySearch(e, 1.1, revNumCompare);
expect(insertionPoint(pos)).to.be(6);
});
});
describe('0 length array with custom comparison function', function () {
const f = [];
it('should not find 0', function () {
const pos = binarySearch(f, 0, revNumCompare);
expect(pos < 0).to.be(true);
});
it('should have an insertion point of 0 for 0', function () {
const pos = binarySearch(f, 0, revNumCompare);
expect(insertionPoint(pos)).to.be(0);
});
});
describe('single element array with custom comparison function', function () {
const g = [1];
it('should find 1 at index 0', function () {
const pos = binarySearch(g, 1, revNumCompare);
expect(pos).to.be(0);
});
it('should not find 2', function () {
const pos = binarySearch(g, 2, revNumCompare);
expect(pos < 0).to.be(true);
});
it('should have an insertion point of 0 for 2', function () {
const pos = binarySearch(g, 2, revNumCompare);
expect(insertionPoint(pos)).to.be(0);
});
it('should not find 0', function () {
const pos = binarySearch(g, 0, revNumCompare);
expect(pos < 0).to.be(true);
});
it('should have an insertion point of 1 for 0', function () {
const pos = binarySearch(g, 0, revNumCompare);
expect(insertionPoint(pos)).to.be(1);
});
});
describe('finding first index when multiple candidates', function () {
it('should find the index of the first 0', function () {
expect(binarySearch([0, 0, 1], 0)).to.be(0);
});
it('should find the index of the first 1', function () {
expect(binarySearch([0, 1, 1], 1)).to.be(1);
});
});
describe("Don't use Array#slice, Function#apply and Function#call", function () {
const a = [1, 5, 7, 11, 13, 16, 19, 24, 28, 31, 33, 36, 40, 50, 52, 55];
const calls = {
'Array#slice': false,
'Function#apply': false,
'Function#call': false,
};
let origArraySlice;
let origFunctionApply;
let origFunctionCall;
it('does not use potentially slow methods (default & custom compare)', function () {
// Mockup (I failed to use sinon.spy and beforeEach-hooks)
origArraySlice = Array.prototype.slice;
origFunctionApply = Function.prototype.apply;
origFunctionCall = Function.prototype.call;
Array.prototype.slice = function () {
calls['Array#slice'] = true;
};
Function.prototype.apply = function () {
calls['Function#apply'] = true;
};
Function.prototype.call = function () {
calls['Function#call'] = true;
};
// Now actually call and test the method twice
binarySearch(a, 48);
binarySearch(a, 13, function (a, b) {
return a > b ? 1 : a < b ? -1 : 0;
});
// Restore mocked up methods
Array.prototype.slice = origArraySlice;
Function.prototype.apply = origFunctionApply;
Function.prototype.call = origFunctionCall;
// Expectations
expect(calls['Array#slice']).to.be(false);
expect(calls['Function#apply']).to.be(false);
expect(calls['Function#call']).to.be(false);
});
});
describe('when items are not found', function () {
const arr = [1, 2, 2, 2, 3, 5, 9];
it('should return the index of where the item would go plus one, negated, if the item is not found', function () {
expect(binarySearch(arr, 4)).to.equal(-6);
});
it('should work even on empty arrays', function () {
expect(binarySearch([], 42)).to.equal(-1);
});
it('should work even on arrays of doubles', function () {
expect(binarySearch([0.0, 0.1, 0.2, 0.3, 0.4], 0.25)).to.equal(-4);
});
});
});
describe('equals', function () {
it('returns true for [] == []', function () {
expect(equals([], [])).to.be(true);
});
it('returns true for [1] == [1]', function () {
expect(equals([1], [1])).to.be(true);
});
it("returns true for ['1'] == ['1']", function () {
expect(equals(['1'], ['1'])).to.be(true);
});
it("returns false for [1] == ['1']", function () {
expect(equals([1], ['1'])).to.be(false);
});
it('returns true for [null] == [null]', function () {
expect(equals([null], [null])).to.be(true);
});
it('returns false for [null] == [undefined]', function () {
expect(equals([null], [undefined])).to.be(false);
});
it('returns true for [1, 2] == [1, 2]', function () {
expect(equals([1, 2], [1, 2])).to.be(true);
});
it('returns false for [1, 2] == [2, 1]', function () {
expect(equals([1, 2], [2, 1])).to.be(false);
});
it('returns false for [1, 2] == [1]', function () {
expect(equals([1, 2], [1])).to.be(false);
});
it('returns false for [1] == [1, 2]', function () {
expect(equals([1], [1, 2])).to.be(false);
});
it('returns false for [{}] == [{}]', function () {
expect(equals([{}], [{}])).to.be(false);
});
});
describe('extend', function () {
it('extends an array in place with an array', function () {
const a = [0, 1];
extend(a, [2, 3]);
expect(a).to.eql([0, 1, 2, 3]);
});
it('extends an array in place with a number', function () {
const a = [0, 1];
extend(a, 2);
expect(a).to.eql([0, 1, 2]);
});
it('extends an array in place with a big array', function () {
const a = [];
let i = 250000; // original test has 1.000.000, but that was too slow
const bigArray = Array(i);
while (i--) {
bigArray[i] = i;
}
extend(a, bigArray);
expect(a).to.eql(bigArray);
});
});
describe('find', function () {
it('finds numbers in an array', function () {
const a = [0, 1, 2, 3];
const b = find(a, function (val, index, a2) {
expect(a).to.equal(a2);
expect(typeof index).to.be('number');
return val > 1;
});
expect(b).to.be(2);
});
it('returns null when an item in an array is not found', function () {
const a = [0, 1, 2, 3];
const b = find(a, function (val, index, a2) {
return val > 100;
});
expect(b).to.be(null);
});
it('finds items in an array-like', function () {
const a = 'abCD';
const b = find(a, function (val, index, a2) {
expect(a).to.equal(a2);
expect(typeof index).to.be('number');
return val >= 'A' && val <= 'Z';
});
expect(b).to.be('C');
});
it('returns null when nothing in an array-like is found', function () {
const a = 'abcd';
const b = find(a, function (val, index, a2) {
return val >= 'A' && val <= 'Z';
});
expect(b).to.be(null);
});
});
describe('findIndex', function () {
it('finds index of numbers in an array', function () {
const a = [0, 1, 2, 3];
const b = findIndex(a, function (val, index, a2) {
expect(a).to.equal(a2);
expect(typeof index).to.be('number');
return val > 1;
});
expect(b).to.be(2);
});
it('returns -1 when an item in an array is not found', function () {
const a = [0, 1, 2, 3];
const b = findIndex(a, function (val, index, a2) {
return val > 100;
});
expect(b).to.be(-1);
});
});
describe('isSorted', function () {
it('works with just an array as argument', function () {
expect(isSorted([1, 2, 3])).to.be(true);
expect(isSorted([1, 2, 2])).to.be(true);
expect(isSorted([1, 2, 1])).to.be(false);
});
it('works with strict comparison without compare function', function () {
expect(isSorted([1, 2, 3], null, true)).to.be(true);
expect(isSorted([1, 2, 2], null, true)).to.be(false);
expect(isSorted([1, 2, 1], null, true)).to.be(false);
});
it('works with a compare function', function () {
function compare(a, b) {
return b - a;
}
expect(isSorted([1, 2, 3], compare)).to.be(false);
expect(isSorted([3, 2, 2], compare)).to.be(true);
});
});
describe('linearFindNearest', function () {
it('returns expected value', function () {
const arr = [1000, 500, 100];
expect(linearFindNearest(arr, 10000, 0)).to.eql(0);
expect(linearFindNearest(arr, 10000, 1)).to.eql(0);
expect(linearFindNearest(arr, 10000, -1)).to.eql(0);
expect(linearFindNearest(arr, 1000, 0)).to.eql(0);
expect(linearFindNearest(arr, 1000, 1)).to.eql(0);
expect(linearFindNearest(arr, 1000, -1)).to.eql(0);
expect(linearFindNearest(arr, 900, 0)).to.eql(0);
expect(linearFindNearest(arr, 900, 1)).to.eql(0);
expect(linearFindNearest(arr, 900, -1)).to.eql(1);
expect(linearFindNearest(arr, 750, 0)).to.eql(1);
expect(linearFindNearest(arr, 750, 1)).to.eql(0);
expect(linearFindNearest(arr, 750, -1)).to.eql(1);
expect(linearFindNearest(arr, 550, 0)).to.eql(1);
expect(linearFindNearest(arr, 550, 1)).to.eql(0);
expect(linearFindNearest(arr, 550, -1)).to.eql(1);
expect(linearFindNearest(arr, 500, 0)).to.eql(1);
expect(linearFindNearest(arr, 500, 1)).to.eql(1);
expect(linearFindNearest(arr, 500, -1)).to.eql(1);
expect(linearFindNearest(arr, 450, 0)).to.eql(1);
expect(linearFindNearest(arr, 450, 1)).to.eql(1);
expect(linearFindNearest(arr, 450, -1)).to.eql(2);
expect(linearFindNearest(arr, 300, 0)).to.eql(2);
expect(linearFindNearest(arr, 300, 1)).to.eql(1);
expect(linearFindNearest(arr, 300, -1)).to.eql(2);
expect(linearFindNearest(arr, 200, 0)).to.eql(2);
expect(linearFindNearest(arr, 200, 1)).to.eql(1);
expect(linearFindNearest(arr, 200, -1)).to.eql(2);
expect(linearFindNearest(arr, 100, 0)).to.eql(2);
expect(linearFindNearest(arr, 100, 1)).to.eql(2);
expect(linearFindNearest(arr, 100, -1)).to.eql(2);
expect(linearFindNearest(arr, 50, 0)).to.eql(2);
expect(linearFindNearest(arr, 50, 1)).to.eql(2);
expect(linearFindNearest(arr, 50, -1)).to.eql(2);
});
});
describe('numberSafeCompareFunction', function () {
it('sorts as expected', function () {
const arr = [40, 200, 3000];
// default sort would yield [200, 3000, 40]
arr.sort(numberSafeCompareFunction);
expect(arr).to.eql(arr);
});
});
describe('remove', function () {
it('removes elements from an array', function () {
const a = ['a', 'b', 'c', 'd'];
remove(a, 'c');
expect(a).to.eql(['a', 'b', 'd']);
remove(a, 'x');
expect(a).to.eql(['a', 'b', 'd']);
});
});
describe('reverseSubArray', function () {
it('returns expected value', function () {
let arr;
const expected = [1, 2, 3, 4, 5, 6];
arr = [1, 5, 4, 3, 2, 6];
reverseSubArray(arr, 1, 4);
expect(arr).to.eql(expected);
arr = [3, 2, 1, 4, 5, 6];
reverseSubArray(arr, 0, 2);
expect(arr).to.eql(expected);
arr = [1, 2, 3, 6, 5, 4];
reverseSubArray(arr, 3, 5);
expect(arr).to.eql(expected);
arr = [6, 5, 4, 3, 2, 1];
reverseSubArray(arr, 0, 5);
expect(arr).to.eql(expected);
});
});
describe('stableSort', function () {
let arr, wantedSortedValues;
beforeEach(function () {
arr = [
{key: 3, val: 'a'},
{key: 2, val: 'b'},
{key: 3, val: 'c'},
{key: 4, val: 'd'},
{key: 3, val: 'e'},
];
wantedSortedValues = ['b', 'a', 'c', 'e', 'd'];
});
it('works on an array with custom comparison function', function () {
function comparisonFn(obj1, obj2) {
return obj1.key - obj2.key;
}
stableSort(arr, comparisonFn);
const sortedValues = [];
for (let i = 0; i < arr.length; i++) {
sortedValues.push(arr[i].val);
}
expect(wantedSortedValues).to.eql(sortedValues);
});
});
});

View File

@@ -0,0 +1,12 @@
import expect from '../expect.js';
import {assert} from '../../../src/ol/asserts.js';
describe('ol/asserts.js', function () {
describe('assert', function () {
it('throws an exception', function () {
expect(function () {
assert(false, 42);
}).to.throwException();
});
});
});

View File

@@ -0,0 +1,283 @@
import Circle from '../../../src/ol/geom/Circle.js';
import Projection from '../../../src/ol/proj/Projection.js';
import expect from '../expect.js';
import {
add as addCoordinate,
closestOnCircle,
closestOnSegment,
equals as coordinatesEqual,
createStringXY,
format as formatCoordinate,
rotate as rotateCoordinate,
scale as scaleCoordinate,
squaredDistanceToSegment,
toStringHDMS,
toStringXY,
wrapX,
} from '../../../src/ol/coordinate.js';
import {get} from '../../../src/ol/proj.js';
describe('ol.coordinate', function () {
describe('#add', function () {
let coordinate, delta;
beforeEach(function () {
coordinate = [50.73, 7.1];
delta = [-2, 3];
});
it('returns a coordinate', function () {
const returnedCoordinate = addCoordinate(coordinate, delta);
expect(returnedCoordinate).to.be.an('array');
expect(returnedCoordinate).to.have.length(2);
});
it('adds the delta', function () {
const returnedCoordinate = addCoordinate(coordinate, delta);
expect(returnedCoordinate[0]).to.eql(48.73);
expect(returnedCoordinate[1]).to.eql(10.1);
});
it('modifies in place', function () {
addCoordinate(coordinate, delta);
expect(coordinate[0]).to.eql(48.73);
expect(coordinate[1]).to.eql(10.1);
});
it('does not produce unexpected results with string delta values', function () {
addCoordinate(
coordinate,
delta.map(function (n) {
return String(n);
})
);
expect(coordinate[0]).to.eql(48.73);
expect(coordinate[1]).to.eql(10.1);
});
});
describe('#equals', function () {
const cologne = [50.93333, 6.95];
const bonn1 = [50.73, 7.1];
const bonn2 = [50.73, 7.1];
it('compares correctly', function () {
const bonnEqualsBonn = coordinatesEqual(bonn1, bonn2);
const bonnEqualsCologne = coordinatesEqual(bonn1, cologne);
expect(bonnEqualsBonn).to.be(true);
expect(bonnEqualsCologne).to.be(false);
});
});
describe('#format', function () {
let coordinate;
beforeEach(function () {
coordinate = [6.6123, 46.7919];
});
it('rounds the values', function () {
const string = formatCoordinate(coordinate, '{x} {y}', 0);
expect(string).to.eql('7 47');
});
it('handles the optional fractionDigits param', function () {
const string = formatCoordinate(coordinate, '{x} {y}', 3);
expect(string).to.eql('6.612 46.792');
});
});
describe('#createStringXY', function () {
let coordinate, created, formatted;
beforeEach(function () {
coordinate = [6.6123, 46.7919];
created = null;
formatted = null;
});
it('returns a CoordinateFormatType', function () {
created = createStringXY();
expect(created).to.be.a('function');
formatted = created(coordinate);
expect(formatted).to.be.a('string');
expect(formatted).to.eql('7, 47');
});
it('respects opt_fractionDigits', function () {
created = createStringXY(3);
expect(created).to.be.a('function');
formatted = created(coordinate);
expect(formatted).to.be.a('string');
expect(formatted).to.eql('6.612, 46.792');
});
});
describe('#closestOnCircle', function () {
const center = [5, 10];
const circle = new Circle(center, 10);
it('can find the closest point on circle', function () {
expect(closestOnCircle([-20, 10], circle)).to.eql([-5, 10]);
});
it('can handle coordinate equal circle center', function () {
expect(closestOnCircle(center, circle)).to.eql([15, 10]);
});
});
describe('#closestOnSegment', function () {
it('can handle points where the foot of the perpendicular is closest', function () {
const point = [2, 5];
const segment = [
[-5, 0],
[10, 0],
];
expect(closestOnSegment(point, segment)).to.eql([2, 0]);
});
it('can handle points where the foot of the perpendicular is not closest', function () {
const point = [0, -6];
const segment = [
[-5, 0],
[0, -1],
];
expect(closestOnSegment(point, segment)).to.eql([0, -1]);
});
});
describe('#format', function () {
it('can deal with undefined coordinate', function () {
expect(formatCoordinate()).to.be('');
});
it('formats a coordinate into a template (default precision is 0)', function () {
const coord = [7.85, 47.983333];
const template = 'Coordinate is ({x}|{y}).';
const got = formatCoordinate(coord, template);
const expected = 'Coordinate is (8|48).';
expect(got).to.be(expected);
});
it('formats a coordinate into a template and respects precision)', function () {
const coord = [7.85, 47.983333];
const template = 'Coordinate is ({x}|{y}).';
const got = formatCoordinate(coord, template, 2);
const expected = 'Coordinate is (7.85|47.98).';
expect(got).to.be(expected);
});
});
describe('#rotate', function () {
it('can rotate point in place', function () {
const coord = [7.85, 47.983333];
const rotateRadians = Math.PI / 2; // 90 degrees
rotateCoordinate(coord, rotateRadians);
expect(coord[0].toFixed(6)).to.eql('-47.983333');
expect(coord[1].toFixed(6)).to.eql('7.850000');
});
it('returns the rotated point', function () {
const coord = [7.85, 47.983333];
const rotateRadians = Math.PI / 2; // 90 degrees
const rotated = rotateCoordinate(coord, rotateRadians);
expect(rotated[0].toFixed(7)).to.eql('-47.9833330');
expect(rotated[1].toFixed(7)).to.eql('7.8500000');
});
});
describe('#scale', function () {
it('can scale point in place', function () {
const coord = [7.85, 47.983333];
const scale = 1.2;
scaleCoordinate(coord, scale);
expect(coord[0].toFixed(7)).to.eql('9.4200000');
expect(coord[1].toFixed(7)).to.eql('57.5799996');
});
it('returns the scaled point', function () {
const coord = [7.85, 47.983333];
const scale = 1.2;
const scaledCoord = scaleCoordinate(coord, scale);
expect(scaledCoord[0].toFixed(7)).to.eql('9.4200000');
expect(scaledCoord[1].toFixed(7)).to.eql('57.5799996');
});
});
describe('#squaredDistanceToSegment', function () {
it('can handle points where the foot of the perpendicular is closest', function () {
const point = [2, 5];
const segment = [
[-5, 0],
[10, 0],
];
expect(squaredDistanceToSegment(point, segment)).to.eql(25);
});
it('can handle points where the foot of the perpendicular is not closest', function () {
const point = [0, -6];
const segment = [
[-5, 0],
[0, -1],
];
expect(squaredDistanceToSegment(point, segment)).to.eql(25);
});
});
describe('#toStringHDMS', function () {
it('returns the empty string on undefined input', function () {
const got = toStringHDMS();
const expected = '';
expect(got).to.be(expected);
});
it('formats with zero fractional digits as default', function () {
const coord = [7.85, 47.983333];
const got = toStringHDMS(coord);
const expected = '47° 59 00″ N 7° 51 00″ E';
expect(got).to.be(expected);
});
it('formats with given fractional digits, if passed', function () {
const coord = [7.85, 47.983333];
const got = toStringHDMS(coord, 3);
const expected = '47° 58 59.999″ N 7° 51 00.000″ E';
expect(got).to.be(expected);
});
});
describe('#toStringXY', function () {
it('formats with zero fractional digits as default', function () {
const coord = [7.85, 47.983333];
const got = toStringXY(coord);
const expected = '8, 48';
expect(got).to.be(expected);
});
it('formats with given fractional digits, if passed', function () {
const coord = [7.85, 47.983333];
const got = toStringXY(coord, 2);
const expected = '7.85, 47.98';
expect(got).to.be(expected);
});
});
describe('wrapX()', function () {
const projection = get('EPSG:4326');
it('leaves real world coordinate untouched', function () {
expect(wrapX([16, 48], projection)).to.eql([16, 48]);
});
it('moves left world coordinate to real world', function () {
expect(wrapX([-344, 48], projection)).to.eql([16, 48]);
});
it('moves right world coordinate to real world', function () {
expect(wrapX([376, 48], projection)).to.eql([16, 48]);
});
it('moves far off left coordinate to real world', function () {
expect(wrapX([-1064, 48], projection)).to.eql([16, 48]);
});
it('moves far off right coordinate to real world', function () {
expect(wrapX([1096, 48], projection)).to.eql([16, 48]);
});
const swiss = new Projection({code: 'EPSG:21781', units: 'm'});
it('leaves non-global projection coordinates untouched', function () {
expect(wrapX([1096, 48], swiss)).to.eql([1096, 48]);
});
});
});

64
test/node/ol/css.test.js Normal file
View File

@@ -0,0 +1,64 @@
import expect from '../expect.js';
import {getFontParameters} from '../../../src/ol/css.js';
describe('ol.css', function () {
describe('getFontParameters()', function () {
const cases = [
{
font: '2em "Open Sans"',
style: 'normal',
weight: 'normal',
families: ['"Open Sans"'],
},
{
font: "2em 'Open Sans'",
style: 'normal',
weight: 'normal',
families: ['"Open Sans"'],
},
{
font: '2em "Open Sans", sans-serif',
style: 'normal',
weight: 'normal',
families: ['"Open Sans"', 'sans-serif'],
},
{
font: 'italic small-caps bolder 16px/3 cursive',
style: 'italic',
weight: 'bolder',
families: ['cursive'],
},
{
font: 'garbage 2px input',
families: null,
},
{
font: '100% fantasy',
style: 'normal',
weight: 'normal',
families: ['fantasy'],
},
];
cases.forEach(function (c, i) {
it('works for ' + c.font, function () {
const font = getFontParameters(c.font);
if (c.families === null) {
expect(font).to.be(null);
return;
}
font.families.forEach(function (family, j) {
// Safari uses single quotes for font families, so we have to do extra work
if (family.charAt(0) === "'") {
// we wouldn't want to do this in the lib since it doesn't properly escape quotes
// but we know that our test cases don't include quotes in font names
font.families[j] = '"' + family.slice(1, -1) + '"';
}
});
expect(font.style).to.eql(c.style);
expect(font.weight).to.eql(c.weight);
expect(font.families).to.eql(c.families);
});
});
});
});

View File

@@ -0,0 +1,96 @@
import EventTarget from '../../../src/ol/events/Target.js';
import expect from '../expect.js';
import sinon from 'sinon';
import {listen, listenOnce, unlistenByKey} from '../../../src/ol/events.js';
describe('ol/events.js', function () {
let add, target;
beforeEach(function () {
target = new EventTarget();
add = sinon.spy(target, 'addEventListener');
});
afterEach(function () {
target.addEventListener.restore();
});
describe('listen()', function () {
it('calls addEventListener on the target', function () {
listen(target, 'foo', function () {});
expect(add.callCount).to.be(1);
});
it('returns a key', function () {
const key = listen(target, 'foo', function () {});
expect(key).to.be.a(Object);
});
it('does not add the same listener twice', function () {
const listener = function () {};
listen(target, 'foo', listener);
listen(target, 'foo', listener);
expect(target.listeners_['foo'].length).to.be(1);
});
it('only treats listeners as same when all args are equal', function () {
const listener = function () {};
listen(target, 'foo', listener, {});
listen(target, 'foo', listener, {});
listen(target, 'foo', listener, undefined);
expect(target.listeners_['foo'].length).to.be(3);
});
});
describe('listenOnce()', function () {
it('creates a one-off listener', function () {
const target = new EventTarget();
const listener = sinon.spy();
listenOnce(target, 'foo', listener);
target.dispatchEvent('foo');
expect(listener.callCount).to.be(1);
target.dispatchEvent('foo');
expect(listener.callCount).to.be(1);
});
it('Adds the same listener twice', function () {
const listener = sinon.spy();
listenOnce(target, 'foo', listener);
listenOnce(target, 'foo', listener);
target.dispatchEvent('foo');
target.dispatchEvent('foo');
target.dispatchEvent('foo');
expect(listener.callCount).to.be(2);
});
});
describe('unlistenByKey()', function () {
it('unregisters previously registered listeners', function () {
const key = listen(target, 'foo', function () {});
unlistenByKey(key);
expect(target.listeners_['foo']).to.be(undefined);
});
it('works with multiple types', function () {
const key = listen(target, ['foo', 'bar'], function () {});
unlistenByKey(key);
expect(target.listeners_['foo']).to.be(undefined);
expect(target.listeners_['bar']).to.be(undefined);
});
});
describe('Listener keys', function () {
it('does not register duplicated listeners', function () {
const target = new EventTarget();
const listener = function () {};
const key1 = listen(target, 'foo', listener);
expect(target.listeners_['foo']).to.eql([listener]);
const key2 = listen(target, 'foo', listener);
expect(target.listeners_['foo']).to.eql([listener]);
expect(key1.listener).to.equal(key2.listener);
});
it('registers multiple listeners if this object is different', function () {
const target = new EventTarget();
const listener = function () {};
const key1 = listen(target, 'foo', listener, {});
const key2 = listen(target, 'foo', listener, {});
expect(key1.listener).to.not.equal(key2.listener);
expect(target.listeners_['foo']).to.eql([key1.listener, key2.listener]);
});
});
});

View File

@@ -0,0 +1,176 @@
import Disposable from '../../../../src/ol/Disposable.js';
import Event from '../../../../src/ol/events/Event.js';
import EventTarget from '../../../../src/ol/events/Target.js';
import expect from '../../expect.js';
import {listen} from '../../../../src/ol/events.js';
describe('ol/events/Target.js', function () {
let called, events, eventTarget, spy1, spy2, spy3;
beforeEach(function () {
called = [];
events = [];
function spy(evt) {
called.push(this.id);
events.push(evt);
}
spy1 = spy.bind({id: 1});
spy2 = spy.bind({id: 2});
spy3 = spy.bind({id: 3});
eventTarget = new EventTarget();
});
describe('constructor', function () {
it('creates an instance', function () {
expect(eventTarget).to.be.a(EventTarget);
expect(eventTarget).to.be.a(Disposable);
});
it('accepts a default target', function (done) {
const defaultTarget = {};
const target = new EventTarget(defaultTarget);
target.addEventListener('my-event', function (event) {
expect(event.target).to.eql(defaultTarget);
done();
});
target.dispatchEvent('my-event');
});
it('does not initialize objects in advance', function () {
expect(eventTarget.pendingRemovals_).to.be(null);
expect(eventTarget.dispatching_).to.be(null);
expect(eventTarget.listeners_).to.be(null);
});
});
describe('#hasListener', function () {
it('reports any listeners when called without argument', function () {
expect(eventTarget.hasListener()).to.be(false);
eventTarget.addEventListener('foo', function () {});
expect(eventTarget.hasListener()).to.be(true);
});
it('reports listeners for the type passed as argument', function () {
eventTarget.addEventListener('foo', function () {});
expect(eventTarget.hasListener('foo')).to.be(true);
expect(eventTarget.hasListener('bar')).to.be(false);
});
});
describe('#addEventListener()', function () {
it('has listeners for each registered type', function () {
eventTarget.addEventListener('foo', spy1);
eventTarget.addEventListener('bar', spy2);
expect(eventTarget.hasListener('foo')).to.be(true);
expect(eventTarget.hasListener('bar')).to.be(true);
});
});
describe('#removeEventListener()', function () {
it('keeps the listeners registry clean', function () {
eventTarget.addEventListener('foo', spy1);
eventTarget.removeEventListener('foo', spy1);
expect(eventTarget.hasListener('foo')).to.be(false);
});
it('removes added listeners from the listeners registry', function () {
eventTarget.addEventListener('foo', spy1);
eventTarget.addEventListener('foo', spy2);
eventTarget.removeEventListener('foo', spy1, false);
expect(eventTarget.listeners_['foo']).to.have.length(1);
});
it('does not remove listeners when the specified listener is not found', function () {
eventTarget.addEventListener('foo', spy1);
eventTarget.addEventListener('foo', spy2);
eventTarget.removeEventListener('foo', undefined);
eventTarget.removeEventListener('foo', spy2);
eventTarget.removeEventListener('foo', spy2);
expect(eventTarget.listeners_['foo']).to.eql([spy1]);
});
});
describe('#dispatchEvent()', function () {
it('calls listeners in the correct order', function () {
eventTarget.addEventListener('foo', spy1);
eventTarget.addEventListener('foo', spy2);
eventTarget.dispatchEvent('foo');
expect(called).to.eql([1, 2]);
});
it('stops propagation when listeners return false', function () {
eventTarget.addEventListener('foo', spy1);
eventTarget.addEventListener(
'foo',
function (evt) {
spy2();
return false;
},
false
);
eventTarget.addEventListener('foo', spy3);
eventTarget.dispatchEvent('foo');
expect(called).to.eql([1, 2]);
});
it('stops propagation when listeners call stopPropagation()', function () {
eventTarget.addEventListener('foo', function (evt) {
spy2();
evt.stopPropagation();
});
eventTarget.addEventListener('foo', spy1);
eventTarget.dispatchEvent('foo');
expect(called).to.eql([2]);
});
it('passes a default ol.events.Event object to listeners', function () {
eventTarget.addEventListener('foo', spy1);
eventTarget.dispatchEvent('foo');
expect(events[0]).to.be.a(Event);
expect(events[0].type).to.be('foo');
expect(events[0].target).to.equal(eventTarget);
});
it('passes a custom event object with target to listeners', function () {
eventTarget.addEventListener('foo', spy1);
const event = {
type: 'foo',
};
eventTarget.dispatchEvent(event);
expect(events[0]).to.equal(event);
expect(events[0].target).to.equal(eventTarget);
});
it('is safe to remove listeners in listeners', function () {
eventTarget.addEventListener('foo', spy3);
eventTarget.addEventListener('foo', function () {
eventTarget.removeEventListener('foo', spy1);
eventTarget.removeEventListener('foo', spy2);
eventTarget.removeEventListener('foo', spy3);
});
eventTarget.addEventListener('foo', spy1);
eventTarget.addEventListener('foo', spy2);
expect(function () {
eventTarget.dispatchEvent('foo');
}).not.to.throwException();
expect(called).to.eql([3]);
expect(eventTarget.listeners_['foo']).to.have.length(1);
});
it('is safe to do weird things in listeners', function () {
eventTarget.addEventListener('foo', spy2);
eventTarget.addEventListener('foo', function weird(evt) {
eventTarget.removeEventListener('foo', weird);
eventTarget.removeEventListener('foo', spy1);
eventTarget.dispatchEvent('foo');
eventTarget.removeEventListener('foo', spy2);
eventTarget.dispatchEvent('foo');
evt.preventDefault();
});
eventTarget.addEventListener('foo', spy1);
expect(function () {
eventTarget.dispatchEvent('foo');
}).not.to.throwException();
expect(called).to.eql([2, 2]);
expect(eventTarget.listeners_['foo']).to.be(undefined);
});
});
describe('#dispose()', function () {
it('cleans up foreign references', function () {
listen(eventTarget, 'foo', spy1);
expect(eventTarget.hasListener('foo')).to.be(true);
eventTarget.dispose();
expect(eventTarget.hasListener('foo')).to.be(false);
});
});
});

View File

@@ -0,0 +1,51 @@
import Event, {
preventDefault,
stopPropagation,
} from '../../../../src/ol/events/Event.js';
import expect from '../../expect.js';
import sinon from 'sinon';
describe('ol/events/Event.js', function () {
describe('constructor', function () {
it('takes a type as argument', function () {
const event = new Event('foo');
expect(event.type).to.be('foo');
});
it('does not set the propagationStopped flag', function () {
const event = new Event('foo');
expect(event.propagationStopped).to.be(undefined);
});
});
describe('#preventDefault', function () {
it('sets the defaultPrevented flag', function () {
const event = new Event('foo');
event.preventDefault();
expect(event.defaultPrevented).to.be(true);
});
it('does the same as #stopPropagation', function () {
const event = new Event('foo');
expect(event.stopPropagation()).to.equal(event.preventDefault());
});
});
describe('ol.events.Event.preventDefault', function () {
it('calls preventDefault on the event object', function () {
const event = {
preventDefault: sinon.spy(),
};
preventDefault(event);
expect(event.preventDefault.called).to.be(true);
});
});
describe('ol.events.Event.stopPropagation', function () {
it('calls preventDefault on the event object', function () {
const event = {
stopPropagation: sinon.spy(),
};
stopPropagation(event);
expect(event.stopPropagation.called).to.be(true);
});
});
});

995
test/node/ol/extent.test.js Normal file
View File

@@ -0,0 +1,995 @@
import * as _ol_extent_ from '../../../src/ol/extent.js';
import expect from '../expect.js';
import proj4 from 'proj4';
import sinon from 'sinon';
import {get, getTransform} from '../../../src/ol/proj.js';
import {register} from '../../../src/ol/proj/proj4.js';
describe('ol/extent.js', function () {
describe('buffer', function () {
it('buffers an extent by some value', function () {
const extent = [-10, -20, 10, 20];
expect(_ol_extent_.buffer(extent, 15)).to.eql([-25, -35, 25, 35]);
});
});
describe('clone', function () {
it('creates a copy of an extent', function () {
const extent = _ol_extent_.createOrUpdate(1, 2, 3, 4);
const clone = _ol_extent_.clone(extent);
expect(_ol_extent_.equals(extent, clone)).to.be(true);
_ol_extent_.extendCoordinate(extent, [10, 20]);
expect(_ol_extent_.equals(extent, clone)).to.be(false);
});
});
describe('closestSquaredDistanceXY', function () {
it('returns correct result when x left of extent', function () {
const extent = _ol_extent_.createOrUpdate(0, 0, 1, 1);
const x = -2;
const y = 0;
expect(_ol_extent_.closestSquaredDistanceXY(extent, x, y)).to.be(4);
});
it('returns correct result when x right of extent', function () {
const extent = _ol_extent_.createOrUpdate(0, 0, 1, 1);
const x = 3;
const y = 0;
expect(_ol_extent_.closestSquaredDistanceXY(extent, x, y)).to.be(4);
});
it('returns correct result for other x values', function () {
const extent = _ol_extent_.createOrUpdate(0, 0, 1, 1);
const x = 0.5;
const y = 3;
expect(_ol_extent_.closestSquaredDistanceXY(extent, x, y)).to.be(4);
});
it('returns correct result when y below extent', function () {
const extent = _ol_extent_.createOrUpdate(0, 0, 1, 1);
const x = 0;
const y = -2;
expect(_ol_extent_.closestSquaredDistanceXY(extent, x, y)).to.be(4);
});
it('returns correct result when y above extent', function () {
const extent = _ol_extent_.createOrUpdate(0, 0, 1, 1);
const x = 0;
const y = 3;
expect(_ol_extent_.closestSquaredDistanceXY(extent, x, y)).to.be(4);
});
it('returns correct result for other y values', function () {
const extent = _ol_extent_.createOrUpdate(0, 0, 1, 1);
const x = 3;
const y = 0.5;
expect(_ol_extent_.closestSquaredDistanceXY(extent, x, y)).to.be(4);
});
});
describe('createOrUpdateFromCoordinate', function () {
it('works when no extent passed', function () {
const coords = [0, 1];
const expected = [0, 1, 0, 1];
const got = _ol_extent_.createOrUpdateFromCoordinate(coords);
expect(got).to.eql(expected);
});
it('updates a passed extent', function () {
const extent = _ol_extent_.createOrUpdate(-4, -7, -3, -6);
const coords = [0, 1];
const expected = [0, 1, 0, 1];
_ol_extent_.createOrUpdateFromCoordinate(coords, extent);
expect(extent).to.eql(expected);
});
});
describe('createOrUpdateFromCoordinates', function () {
it('works when single coordinate and no extent passed', function () {
const coords = [[0, 1]];
const expected = [0, 1, 0, 1];
const got = _ol_extent_.createOrUpdateFromCoordinates(coords);
expect(got).to.eql(expected);
});
it('changes the passed extent when single coordinate', function () {
const extent = _ol_extent_.createOrUpdate(-4, -7, -3, -6);
const coords = [[0, 1]];
const expected = [0, 1, 0, 1];
_ol_extent_.createOrUpdateFromCoordinates(coords, extent);
expect(extent).to.eql(expected);
});
it('works when multiple coordinates and no extent passed', function () {
const coords = [
[0, 1],
[2, 3],
];
const expected = [0, 1, 2, 3];
const got = _ol_extent_.createOrUpdateFromCoordinates(coords);
expect(got).to.eql(expected);
});
it('changes the passed extent when multiple coordinates given', function () {
const extent = _ol_extent_.createOrUpdate(-4, -7, -3, -6);
const coords = [
[0, 1],
[-2, -1],
];
const expected = [-2, -1, 0, 1];
_ol_extent_.createOrUpdateFromCoordinates(coords, extent);
expect(extent).to.eql(expected);
});
});
describe('createOrUpdateFromRings', function () {
it('works when single ring and no extent passed', function () {
const ring = [
[0, 0],
[0, 2],
[2, 2],
[2, 0],
[0, 0],
];
const rings = [ring];
const expected = [0, 0, 2, 2];
const got = _ol_extent_.createOrUpdateFromRings(rings);
expect(got).to.eql(expected);
});
it('changes the passed extent when single ring given', function () {
const ring = [
[0, 0],
[0, 2],
[2, 2],
[2, 0],
[0, 0],
];
const rings = [ring];
const extent = [1, 1, 4, 7];
const expected = [0, 0, 2, 2];
_ol_extent_.createOrUpdateFromRings(rings, extent);
expect(extent).to.eql(expected);
});
it('works when multiple rings and no extent passed', function () {
const ring1 = [
[0, 0],
[0, 2],
[2, 2],
[2, 0],
[0, 0],
];
const ring2 = [
[1, 1],
[1, 3],
[3, 3],
[3, 1],
[1, 1],
];
const rings = [ring1, ring2];
const expected = [0, 0, 3, 3];
const got = _ol_extent_.createOrUpdateFromRings(rings);
expect(got).to.eql(expected);
});
it('changes the passed extent when multiple rings given', function () {
const ring1 = [
[0, 0],
[0, 2],
[2, 2],
[2, 0],
[0, 0],
];
const ring2 = [
[1, 1],
[1, 3],
[3, 3],
[3, 1],
[1, 1],
];
const rings = [ring1, ring2];
const extent = [1, 1, 4, 7];
const expected = [0, 0, 3, 3];
_ol_extent_.createOrUpdateFromRings(rings, extent);
expect(extent).to.eql(expected);
});
});
describe('forEachCorner', function () {
let callbackFalse;
let callbackTrue;
beforeEach(function () {
callbackFalse = sinon.spy(function () {
return false;
});
callbackTrue = sinon.spy(function () {
return true;
});
});
it('calls the passed callback for each corner', function () {
const extent = [1, 2, 3, 4];
_ol_extent_.forEachCorner(extent, callbackFalse);
expect(callbackFalse.callCount).to.be(4);
});
it('calls the passed callback with each corner', function () {
const extent = [1, 2, 3, 4];
_ol_extent_.forEachCorner(extent, callbackFalse);
const firstCallFirstArg = callbackFalse.args[0][0];
const secondCallFirstArg = callbackFalse.args[1][0];
const thirdCallFirstArg = callbackFalse.args[2][0];
const fourthCallFirstArg = callbackFalse.args[3][0];
expect(firstCallFirstArg).to.eql([1, 2]); // bl
expect(secondCallFirstArg).to.eql([3, 2]); // br
expect(thirdCallFirstArg).to.eql([3, 4]); // tr
expect(fourthCallFirstArg).to.eql([1, 4]); // tl
});
it('calls a truthy callback only once', function () {
const extent = [1, 2, 3, 4];
_ol_extent_.forEachCorner(extent, callbackTrue);
expect(callbackTrue.callCount).to.be(1);
});
it('ensures that any corner can cancel the callback execution', function () {
const extent = [1, 2, 3, 4];
const bottomLeftSpy = sinon.spy(function (corner) {
return corner[0] === 1 && corner[1] === 2 ? true : false;
});
const bottomRightSpy = sinon.spy(function (corner) {
return corner[0] === 3 && corner[1] === 2 ? true : false;
});
const topRightSpy = sinon.spy(function (corner) {
return corner[0] === 3 && corner[1] === 4 ? true : false;
});
const topLeftSpy = sinon.spy(function (corner) {
return corner[0] === 1 && corner[1] === 4 ? true : false;
});
_ol_extent_.forEachCorner(extent, bottomLeftSpy);
_ol_extent_.forEachCorner(extent, bottomRightSpy);
_ol_extent_.forEachCorner(extent, topRightSpy);
_ol_extent_.forEachCorner(extent, topLeftSpy);
expect(bottomLeftSpy.callCount).to.be(1);
expect(bottomRightSpy.callCount).to.be(2);
expect(topRightSpy.callCount).to.be(3);
expect(topLeftSpy.callCount).to.be(4);
});
it('returns false eventually, if no invocation returned a truthy value', function () {
const extent = [1, 2, 3, 4];
const spy = sinon.spy(); // will return undefined for each corner
const got = _ol_extent_.forEachCorner(extent, spy);
expect(spy.callCount).to.be(4);
expect(got).to.be(false);
});
});
describe('getArea', function () {
it('returns zero for empty extents', function () {
const emptyExtent = _ol_extent_.createEmpty();
const areaEmpty = _ol_extent_.getArea(emptyExtent);
expect(areaEmpty).to.be(0);
const extentDeltaXZero = [45, 67, 45, 78];
const areaDeltaXZero = _ol_extent_.getArea(extentDeltaXZero);
expect(areaDeltaXZero).to.be(0);
const extentDeltaYZero = [11, 67, 45, 67];
const areaDeltaYZero = _ol_extent_.getArea(extentDeltaYZero);
expect(areaDeltaYZero).to.be(0);
});
it('calculates correct area for other extents', function () {
const extent = [0, 0, 10, 10];
const area = _ol_extent_.getArea(extent);
expect(area).to.be(100);
});
});
describe('getIntersection()', function () {
it('returns the intersection of two extents', function () {
const world = [-180, -90, 180, 90];
const north = [-180, 0, 180, 90];
const farNorth = [-180, 45, 180, 90];
const east = [0, -90, 180, 90];
const farEast = [90, -90, 180, 90];
const south = [-180, -90, 180, 0];
const farSouth = [-180, -90, 180, -45];
const west = [-180, -90, 0, 90];
const farWest = [-180, -90, -90, 90];
const none = _ol_extent_.createEmpty();
expect(_ol_extent_.getIntersection(world, none)).to.eql(none);
expect(_ol_extent_.getIntersection(world, north)).to.eql(north);
expect(_ol_extent_.getIntersection(world, east)).to.eql(east);
expect(_ol_extent_.getIntersection(world, south)).to.eql(south);
expect(_ol_extent_.getIntersection(world, west)).to.eql(west);
expect(_ol_extent_.getIntersection(farEast, farWest)).to.eql(none);
expect(_ol_extent_.getIntersection(farNorth, farSouth)).to.eql(none);
expect(_ol_extent_.getIntersection(north, west)).to.eql([-180, 0, 0, 90]);
expect(_ol_extent_.getIntersection(east, south)).to.eql([0, -90, 180, 0]);
});
it('can take an destination extent', function () {
const world = [-180, -90, 180, 90];
const north = [-180, 0, 180, 90];
const none = _ol_extent_.createEmpty();
let tmpExtent = [-180, 45, 180, 90];
expect(_ol_extent_.getIntersection(world, north, tmpExtent)).to.eql(
north
);
expect(_ol_extent_.getIntersection(world, none, tmpExtent)).to.eql(none);
tmpExtent = [-180, -90, 180, 90];
expect(_ol_extent_.getIntersection(tmpExtent, north, tmpExtent)).to.eql(
north
);
});
});
describe('containsCoordinate', function () {
describe('positive', function () {
it('returns true', function () {
const extent = [1, 2, 3, 4];
expect(_ol_extent_.containsCoordinate(extent, [1, 2])).to.be.ok();
expect(_ol_extent_.containsCoordinate(extent, [1, 3])).to.be.ok();
expect(_ol_extent_.containsCoordinate(extent, [1, 4])).to.be.ok();
expect(_ol_extent_.containsCoordinate(extent, [2, 2])).to.be.ok();
expect(_ol_extent_.containsCoordinate(extent, [2, 3])).to.be.ok();
expect(_ol_extent_.containsCoordinate(extent, [2, 4])).to.be.ok();
expect(_ol_extent_.containsCoordinate(extent, [3, 2])).to.be.ok();
expect(_ol_extent_.containsCoordinate(extent, [3, 3])).to.be.ok();
expect(_ol_extent_.containsCoordinate(extent, [3, 4])).to.be.ok();
});
});
describe('negative', function () {
it('returns false', function () {
const extent = [1, 2, 3, 4];
expect(_ol_extent_.containsCoordinate(extent, [0, 1])).to.not.be();
expect(_ol_extent_.containsCoordinate(extent, [0, 2])).to.not.be();
expect(_ol_extent_.containsCoordinate(extent, [0, 3])).to.not.be();
expect(_ol_extent_.containsCoordinate(extent, [0, 4])).to.not.be();
expect(_ol_extent_.containsCoordinate(extent, [0, 5])).to.not.be();
expect(_ol_extent_.containsCoordinate(extent, [1, 1])).to.not.be();
expect(_ol_extent_.containsCoordinate(extent, [1, 5])).to.not.be();
expect(_ol_extent_.containsCoordinate(extent, [2, 1])).to.not.be();
expect(_ol_extent_.containsCoordinate(extent, [2, 5])).to.not.be();
expect(_ol_extent_.containsCoordinate(extent, [3, 1])).to.not.be();
expect(_ol_extent_.containsCoordinate(extent, [3, 5])).to.not.be();
expect(_ol_extent_.containsCoordinate(extent, [4, 1])).to.not.be();
expect(_ol_extent_.containsCoordinate(extent, [4, 2])).to.not.be();
expect(_ol_extent_.containsCoordinate(extent, [4, 3])).to.not.be();
expect(_ol_extent_.containsCoordinate(extent, [4, 4])).to.not.be();
expect(_ol_extent_.containsCoordinate(extent, [4, 5])).to.not.be();
});
});
});
describe('coordinateRelationship()', function () {
const extent = [-180, -90, 180, 90];
const INTERSECTING = 1;
const ABOVE = 2;
const RIGHT = 4;
const BELOW = 8;
const LEFT = 16;
it('returns intersecting for within', function () {
const rel = _ol_extent_.coordinateRelationship(extent, [0, 0]);
expect(rel).to.be(INTERSECTING);
});
it('returns intersecting for touching top', function () {
const rel = _ol_extent_.coordinateRelationship(extent, [0, 90]);
expect(rel).to.be(INTERSECTING);
});
it('returns intersecting for touching right', function () {
const rel = _ol_extent_.coordinateRelationship(extent, [180, 0]);
expect(rel).to.be(INTERSECTING);
});
it('returns intersecting for touching bottom', function () {
const rel = _ol_extent_.coordinateRelationship(extent, [0, -90]);
expect(rel).to.be(INTERSECTING);
});
it('returns intersecting for touching left', function () {
const rel = _ol_extent_.coordinateRelationship(extent, [-180, 0]);
expect(rel).to.be(INTERSECTING);
});
it('above for north', function () {
const rel = _ol_extent_.coordinateRelationship(extent, [0, 100]);
expect(rel).to.be(ABOVE);
});
it('above and right for northeast', function () {
const rel = _ol_extent_.coordinateRelationship(extent, [190, 100]);
expect(rel & ABOVE).to.be(ABOVE);
expect(rel & RIGHT).to.be(RIGHT);
});
it('right for east', function () {
const rel = _ol_extent_.coordinateRelationship(extent, [190, 0]);
expect(rel).to.be(RIGHT);
});
it('below and right for southeast', function () {
const rel = _ol_extent_.coordinateRelationship(extent, [190, -100]);
expect(rel & BELOW).to.be(BELOW);
expect(rel & RIGHT).to.be(RIGHT);
});
it('below for south', function () {
const rel = _ol_extent_.coordinateRelationship(extent, [0, -100]);
expect(rel).to.be(BELOW);
});
it('below and left for southwest', function () {
const rel = _ol_extent_.coordinateRelationship(extent, [-190, -100]);
expect(rel & BELOW).to.be(BELOW);
expect(rel & LEFT).to.be(LEFT);
});
it('left for west', function () {
const rel = _ol_extent_.coordinateRelationship(extent, [-190, 0]);
expect(rel).to.be(LEFT);
});
it('above and left for northwest', function () {
const rel = _ol_extent_.coordinateRelationship(extent, [-190, 100]);
expect(rel & ABOVE).to.be(ABOVE);
expect(rel & LEFT).to.be(LEFT);
});
});
describe('getCenter', function () {
it('returns the expected center', function () {
const extent = [1, 2, 3, 4];
const center = _ol_extent_.getCenter(extent);
expect(center[0]).to.eql(2);
expect(center[1]).to.eql(3);
});
it('returns [NaN, NaN] for empty extents', function () {
const extent = _ol_extent_.createEmpty();
const center = _ol_extent_.getCenter(extent);
expect('' + center[0]).to.be('NaN');
expect('' + center[1]).to.be('NaN');
});
});
describe('getCorner', function () {
const extent = [1, 2, 3, 4];
it('gets the bottom left', function () {
const corner = 'bottom-left';
expect(_ol_extent_.getCorner(extent, corner)).to.eql([1, 2]);
});
it('gets the bottom right', function () {
const corner = 'bottom-right';
expect(_ol_extent_.getCorner(extent, corner)).to.eql([3, 2]);
});
it('gets the top left', function () {
const corner = 'top-left';
expect(_ol_extent_.getCorner(extent, corner)).to.eql([1, 4]);
});
it('gets the top right', function () {
const corner = 'top-right';
expect(_ol_extent_.getCorner(extent, corner)).to.eql([3, 4]);
});
it('throws exception for unexpected corner', function () {
expect(function () {
_ol_extent_.getCorner(extent, 'foobar');
}).to.throwException();
});
});
describe('getEnlargedArea', function () {
it('returns enlarged area of two extents', function () {
const extent1 = [-1, -1, 0, 0];
const extent2 = [0, 0, 1, 1];
const enlargedArea = _ol_extent_.getEnlargedArea(extent1, extent2);
expect(enlargedArea).to.be(4);
});
});
describe('getForViewAndSize', function () {
it('works for a unit square', function () {
const extent = _ol_extent_.getForViewAndSize([0, 0], 1, 0, [1, 1]);
expect(extent[0]).to.be(-0.5);
expect(extent[2]).to.be(0.5);
expect(extent[1]).to.be(-0.5);
expect(extent[3]).to.be(0.5);
});
it('works for center', function () {
const extent = _ol_extent_.getForViewAndSize([5, 10], 1, 0, [1, 1]);
expect(extent[0]).to.be(4.5);
expect(extent[2]).to.be(5.5);
expect(extent[1]).to.be(9.5);
expect(extent[3]).to.be(10.5);
});
it('works for rotation', function () {
const extent = _ol_extent_.getForViewAndSize([0, 0], 1, Math.PI / 4, [
1,
1,
]);
expect(extent[0]).to.roughlyEqual(-Math.sqrt(0.5), 1e-9);
expect(extent[2]).to.roughlyEqual(Math.sqrt(0.5), 1e-9);
expect(extent[1]).to.roughlyEqual(-Math.sqrt(0.5), 1e-9);
expect(extent[3]).to.roughlyEqual(Math.sqrt(0.5), 1e-9);
});
it('works for resolution', function () {
const extent = _ol_extent_.getForViewAndSize([0, 0], 2, 0, [1, 1]);
expect(extent[0]).to.be(-1);
expect(extent[2]).to.be(1);
expect(extent[1]).to.be(-1);
expect(extent[3]).to.be(1);
});
it('works for size', function () {
const extent = _ol_extent_.getForViewAndSize([0, 0], 1, 0, [10, 5]);
expect(extent[0]).to.be(-5);
expect(extent[2]).to.be(5);
expect(extent[1]).to.be(-2.5);
expect(extent[3]).to.be(2.5);
});
});
describe('getSize', function () {
it('returns the expected size', function () {
const extent = [0, 1, 2, 4];
const size = _ol_extent_.getSize(extent);
expect(size).to.eql([2, 3]);
});
});
describe('getIntersectionArea', function () {
it('returns correct area when extents intersect', function () {
const extent1 = [0, 0, 2, 2];
const extent2 = [1, 1, 3, 3];
const intersectionArea = _ol_extent_.getIntersectionArea(
extent1,
extent2
);
expect(intersectionArea).to.be(1);
});
it('returns 0 when extents do not intersect', function () {
const extent1 = [0, 0, 1, 1];
const extent2 = [2, 2, 3, 3];
const intersectionArea = _ol_extent_.getIntersectionArea(
extent1,
extent2
);
expect(intersectionArea).to.be(0);
});
});
describe('getMargin', function () {
it('returns the correct margin (sum of width and height)', function () {
const extent = [1, 2, 3, 4];
expect(_ol_extent_.getMargin(extent)).to.be(4);
});
});
describe('intersects', function () {
it('returns the expected value', function () {
const intersects = _ol_extent_.intersects;
const extent = [50, 50, 100, 100];
expect(intersects(extent, extent)).to.be(true);
expect(intersects(extent, [20, 20, 80, 80])).to.be(true);
expect(intersects(extent, [20, 50, 80, 100])).to.be(true);
expect(intersects(extent, [20, 80, 80, 120])).to.be(true);
expect(intersects(extent, [50, 20, 100, 80])).to.be(true);
expect(intersects(extent, [50, 80, 100, 120])).to.be(true);
expect(intersects(extent, [80, 20, 120, 80])).to.be(true);
expect(intersects(extent, [80, 50, 120, 100])).to.be(true);
expect(intersects(extent, [80, 80, 120, 120])).to.be(true);
expect(intersects(extent, [20, 20, 120, 120])).to.be(true);
expect(intersects(extent, [70, 70, 80, 80])).to.be(true);
expect(intersects(extent, [10, 10, 30, 30])).to.be(false);
expect(intersects(extent, [30, 10, 70, 30])).to.be(false);
expect(intersects(extent, [50, 10, 100, 30])).to.be(false);
expect(intersects(extent, [80, 10, 120, 30])).to.be(false);
expect(intersects(extent, [120, 10, 140, 30])).to.be(false);
expect(intersects(extent, [10, 30, 30, 70])).to.be(false);
expect(intersects(extent, [120, 30, 140, 70])).to.be(false);
expect(intersects(extent, [10, 50, 30, 100])).to.be(false);
expect(intersects(extent, [120, 50, 140, 100])).to.be(false);
expect(intersects(extent, [10, 80, 30, 120])).to.be(false);
expect(intersects(extent, [120, 80, 140, 120])).to.be(false);
expect(intersects(extent, [10, 120, 30, 140])).to.be(false);
expect(intersects(extent, [30, 120, 70, 140])).to.be(false);
expect(intersects(extent, [50, 120, 100, 140])).to.be(false);
expect(intersects(extent, [80, 120, 120, 140])).to.be(false);
expect(intersects(extent, [120, 120, 140, 140])).to.be(false);
});
});
describe('scaleFromCenter', function () {
it('scales the extent from its center', function () {
const extent = [1, 1, 3, 3];
_ol_extent_.scaleFromCenter(extent, 2);
expect(extent[0]).to.eql(0);
expect(extent[2]).to.eql(4);
expect(extent[1]).to.eql(0);
expect(extent[3]).to.eql(4);
});
});
describe('intersectsSegment()', function () {
const extent = [-180, -90, 180, 90];
const north = [0, 100];
const northeast = [190, 100];
const east = [190, 0];
const southeast = [190, -100];
const south = [0, -100];
const southwest = [-190, -100];
const west = [-190, 0];
const northwest = [-190, 100];
const center = [0, 0];
const top = [0, 90];
const right = [180, 0];
const bottom = [-90, 0];
const left = [-180, 0];
const inside = [10, 10];
it('returns true if contained', function () {
const intersects = _ol_extent_.intersectsSegment(extent, center, inside);
expect(intersects).to.be(true);
});
it('returns true if crosses top', function () {
const intersects = _ol_extent_.intersectsSegment(extent, center, north);
expect(intersects).to.be(true);
});
it('returns true if crosses right', function () {
const intersects = _ol_extent_.intersectsSegment(extent, center, east);
expect(intersects).to.be(true);
});
it('returns true if crosses bottom', function () {
const intersects = _ol_extent_.intersectsSegment(extent, center, south);
expect(intersects).to.be(true);
});
it('returns true if crosses left', function () {
const intersects = _ol_extent_.intersectsSegment(extent, center, west);
expect(intersects).to.be(true);
});
it('returns false if above', function () {
const intersects = _ol_extent_.intersectsSegment(
extent,
northwest,
north
);
expect(intersects).to.be(false);
});
it('returns false if right', function () {
const intersects = _ol_extent_.intersectsSegment(extent, northeast, east);
expect(intersects).to.be(false);
});
it('returns false if below', function () {
const intersects = _ol_extent_.intersectsSegment(
extent,
south,
southwest
);
expect(intersects).to.be(false);
});
it('returns false if left', function () {
const intersects = _ol_extent_.intersectsSegment(extent, west, southwest);
expect(intersects).to.be(false);
});
it('returns true if crosses top to bottom', function () {
const intersects = _ol_extent_.intersectsSegment(extent, north, south);
expect(intersects).to.be(true);
});
it('returns true if crosses bottom to top', function () {
const intersects = _ol_extent_.intersectsSegment(extent, south, north);
expect(intersects).to.be(true);
});
it('returns true if crosses left to right', function () {
const intersects = _ol_extent_.intersectsSegment(extent, west, east);
expect(intersects).to.be(true);
});
it('returns true if crosses right to left', function () {
const intersects = _ol_extent_.intersectsSegment(extent, east, west);
expect(intersects).to.be(true);
});
it('returns true if crosses northwest to east', function () {
const intersects = _ol_extent_.intersectsSegment(extent, northwest, east);
expect(intersects).to.be(true);
});
it('returns true if crosses south to west', function () {
const intersects = _ol_extent_.intersectsSegment(extent, south, west);
expect(intersects).to.be(true);
});
it('returns true if touches top', function () {
const intersects = _ol_extent_.intersectsSegment(extent, northwest, top);
expect(intersects).to.be(true);
});
it('returns true if touches right', function () {
const intersects = _ol_extent_.intersectsSegment(
extent,
southeast,
right
);
expect(intersects).to.be(true);
});
it('returns true if touches bottom', function () {
const intersects = _ol_extent_.intersectsSegment(extent, bottom, south);
expect(intersects).to.be(true);
});
it('returns true if touches left', function () {
const intersects = _ol_extent_.intersectsSegment(extent, left, west);
expect(intersects).to.be(true);
});
it('works for zero length inside', function () {
const intersects = _ol_extent_.intersectsSegment(extent, center, center);
expect(intersects).to.be(true);
});
it('works for zero length outside', function () {
const intersects = _ol_extent_.intersectsSegment(extent, north, north);
expect(intersects).to.be(false);
});
it('works for left/right intersection spanning top to bottom', function () {
const extent = [2, 1, 3, 4];
const start = [0, 0];
const end = [5, 5];
expect(_ol_extent_.intersectsSegment(extent, start, end)).to.be(true);
expect(_ol_extent_.intersectsSegment(extent, end, start)).to.be(true);
});
it('works for top/bottom intersection spanning left to right', function () {
const extent = [1, 2, 4, 3];
const start = [0, 0];
const end = [5, 5];
expect(_ol_extent_.intersectsSegment(extent, start, end)).to.be(true);
expect(_ol_extent_.intersectsSegment(extent, end, start)).to.be(true);
});
});
describe('#applyTransform()', function () {
it('does transform', function () {
const transformFn = getTransform('EPSG:4326', 'EPSG:3857');
const sourceExtent = [-15, -30, 45, 60];
const destinationExtent = _ol_extent_.applyTransform(
sourceExtent,
transformFn
);
expect(destinationExtent).not.to.be(undefined);
expect(destinationExtent).not.to.be(null);
// FIXME check values with third-party tool
expect(destinationExtent[0]).to.roughlyEqual(-1669792.3618991037, 1e-9);
expect(destinationExtent[2]).to.roughlyEqual(5009377.085697311, 1e-9);
expect(destinationExtent[1]).to.roughlyEqual(-3503549.843504376, 1e-8);
expect(destinationExtent[3]).to.roughlyEqual(8399737.889818361, 1e-8);
});
it('takes arbitrary function', function () {
const transformFn = function (input, output, opt_dimension) {
const dimension = opt_dimension !== undefined ? opt_dimension : 2;
if (output === undefined) {
output = new Array(input.length);
}
const n = input.length;
let i;
for (i = 0; i < n; i += dimension) {
output[i] = -input[i];
output[i + 1] = -input[i + 1];
}
return output;
};
const sourceExtent = [-15, -30, 45, 60];
const destinationExtent = _ol_extent_.applyTransform(
sourceExtent,
transformFn
);
expect(destinationExtent).not.to.be(undefined);
expect(destinationExtent).not.to.be(null);
expect(destinationExtent[0]).to.be(-45);
expect(destinationExtent[2]).to.be(15);
expect(destinationExtent[1]).to.be(-60);
expect(destinationExtent[3]).to.be(30);
});
it('can use the stops option', function () {
proj4.defs(
'EPSG:32632',
'+proj=utm +zone=32 +datum=WGS84 +units=m +no_defs'
);
register(proj4);
const transformFn = getTransform('EPSG:4326', 'EPSG:32632');
const sourceExtentN = [6, 0, 12, 84];
const destinationExtentN = _ol_extent_.applyTransform(
sourceExtentN,
transformFn
);
expect(destinationExtentN).not.to.be(undefined);
expect(destinationExtentN).not.to.be(null);
expect(destinationExtentN[0]).to.roughlyEqual(166021.44308053964, 1e-8);
expect(destinationExtentN[2]).to.roughlyEqual(833978.5569194605, 1e-8);
expect(destinationExtentN[1]).to.roughlyEqual(0, 1e-8);
expect(destinationExtentN[3]).to.roughlyEqual(9329005.182447437, 1e-8);
const sourceExtentNS = [6, -84, 12, 84];
const destinationExtentNS = _ol_extent_.applyTransform(
sourceExtentNS,
transformFn
);
expect(destinationExtentNS).not.to.be(undefined);
expect(destinationExtentNS).not.to.be(null);
expect(destinationExtentNS[0]).to.roughlyEqual(465005.34493886377, 1e-8);
expect(destinationExtentNS[2]).to.roughlyEqual(534994.6550611362, 1e-8);
expect(destinationExtentNS[1]).to.roughlyEqual(
-destinationExtentN[3],
1e-8
);
expect(destinationExtentNS[3]).to.roughlyEqual(
destinationExtentN[3],
1e-8
);
const destinationExtentNS2 = _ol_extent_.applyTransform(
sourceExtentNS,
transformFn,
undefined,
2
);
expect(destinationExtentNS2).not.to.be(undefined);
expect(destinationExtentNS2).not.to.be(null);
expect(destinationExtentNS2[0]).to.roughlyEqual(
destinationExtentN[0],
1e-8
);
expect(destinationExtentNS2[2]).to.roughlyEqual(
destinationExtentN[2],
1e-8
);
expect(destinationExtentNS2[1]).to.roughlyEqual(
-destinationExtentN[3],
1e-8
);
expect(destinationExtentNS2[3]).to.roughlyEqual(
destinationExtentN[3],
1e-8
);
});
});
describe('wrapX()', function () {
const projection = get('EPSG:4326');
it('leaves real world extent untouched', function () {
expect(_ol_extent_.wrapX([16, 48, 18, 49], projection)).to.eql([
16,
48,
18,
49,
]);
});
it('moves left world extent to real world', function () {
expect(_ol_extent_.wrapX([-344, 48, -342, 49], projection)).to.eql([
16,
48,
18,
49,
]);
});
it('moves right world extent to real world', function () {
expect(_ol_extent_.wrapX([376, 48, 378, 49], projection)).to.eql([
16,
48,
18,
49,
]);
});
it('moves far off left extent to real world', function () {
expect(_ol_extent_.wrapX([-1064, 48, -1062, 49], projection)).to.eql([
16,
48,
18,
49,
]);
});
it('moves far off right extent to real world', function () {
expect(_ol_extent_.wrapX([1096, 48, 1098, 49], projection)).to.eql([
16,
48,
18,
49,
]);
});
it('leaves -180 crossing extent with real world center untouched', function () {
expect(_ol_extent_.wrapX([-184, 48, 16, 49], projection)).to.eql([
-184,
48,
16,
49,
]);
});
it('moves +180 crossing extent with off-world center to the real world', function () {
expect(_ol_extent_.wrapX([300, 48, 376, 49], projection)).to.eql([
-60,
48,
16,
49,
]);
});
it('produces the same real world extent for shifted extents with center at +/-180', function () {
expect(_ol_extent_.wrapX([360, -90, 720, 90], projection)).to.eql([
-360,
-90,
0,
90,
]);
expect(_ol_extent_.wrapX([0, -90, 360, 90], projection)).to.eql([
-360,
-90,
0,
90,
]);
expect(_ol_extent_.wrapX([-360, -90, 0, 90], projection)).to.eql([
-360,
-90,
0,
90,
]);
});
});
describe('approximatelyEquals', function () {
it('returns true when within tolerance', function () {
expect(
_ol_extent_.approximatelyEquals(
[16, 48, 17, 49],
[16.09, 48, 17, 49],
0.1
)
).to.be(true);
});
it('returns false when not within tolerance', function () {
expect(
_ol_extent_.approximatelyEquals(
[16, 48, 17, 49],
[16.11, 48, 17, 49],
0.1
)
).to.be(false);
});
});
});

View File

@@ -0,0 +1,59 @@
import expect from '../expect.js';
import {memoizeOne} from '../../../src/ol/functions.js';
describe('ol/functions.js', function () {
describe('memoizeOne()', function () {
it('returns the result from the first call when called a second time with the same args', function () {
const arg1 = {};
const arg2 = {};
const arg3 = {};
function call(a1, a2, a3) {
return {};
}
const memoized = memoizeOne(call);
const result = memoized(arg1, arg2, arg3);
expect(memoized(arg1, arg2, arg3)).to.be(result);
});
it('returns the result from the first call when called a second time with the same this object', function () {
const arg1 = {};
const arg2 = {};
const arg3 = {};
function call(a1, a2, a3) {
return {};
}
const memoized = memoizeOne(call);
const thisObj = {};
const result = memoized.call(thisObj, arg1, arg2, arg3);
expect(memoized.call(thisObj, arg1, arg2, arg3)).to.be(result);
});
it('returns a different result when called a second time with the different args', function () {
const arg1 = {};
const arg2 = {};
const arg3 = {};
function call(a1, a2, a3) {
return {};
}
const memoized = memoizeOne(call);
const result = memoized(arg1, arg2, arg3);
expect(memoized(arg3, arg2, arg1)).not.to.be(result);
});
it('returns a different result when called a second time with a different this object', function () {
const arg1 = {};
const arg2 = {};
const arg3 = {};
function call(a1, a2, a3) {
return {};
}
const firstThis = {};
const secondThis = {};
const memoized = memoizeOne(call);
const result = memoized.call(firstThis, arg1, arg2, arg3);
expect(memoized.call(secondThis, arg1, arg2, arg3)).not.to.be(result);
});
});
});

View File

@@ -0,0 +1,266 @@
import Circle from '../../../../src/ol/geom/Circle.js';
import expect from '../../expect.js';
import sinon from 'sinon';
describe('ol/geom/Circle.js', function () {
describe('with a unit circle', function () {
let circle;
beforeEach(function () {
circle = new Circle([0, 0], 1);
});
describe('#clone', function () {
it('returns a clone', function () {
circle.setProperties({foo: 'bar', baz: null});
const clone = circle.clone();
expect(clone).to.be.an(Circle);
expect(clone.getCenter()).to.eql(circle.getCenter());
expect(clone.getCenter()).not.to.be(circle.getCenter());
expect(clone.getRadius()).to.be(circle.getRadius());
expect(clone.getProperties()).to.eql({foo: 'bar', baz: null});
});
});
describe('#intersectsCoordinate', function () {
it('contains the center', function () {
expect(circle.intersectsCoordinate([0, 0])).to.be(true);
});
it('contains points inside the perimeter', function () {
expect(circle.intersectsCoordinate([0.5, 0.5])).to.be(true);
expect(circle.intersectsCoordinate([-0.5, 0.5])).to.be(true);
expect(circle.intersectsCoordinate([-0.5, -0.5])).to.be(true);
expect(circle.intersectsCoordinate([0.5, -0.5])).to.be(true);
});
it('contains points on the perimeter', function () {
expect(circle.intersectsCoordinate([1, 0])).to.be(true);
expect(circle.intersectsCoordinate([0, 1])).to.be(true);
expect(circle.intersectsCoordinate([-1, 0])).to.be(true);
expect(circle.intersectsCoordinate([0, -1])).to.be(true);
});
it('does not contain points outside the perimeter', function () {
expect(circle.intersectsCoordinate([2, 0])).to.be(false);
expect(circle.intersectsCoordinate([1, 1])).to.be(false);
expect(circle.intersectsCoordinate([-2, 0])).to.be(false);
expect(circle.intersectsCoordinate([0, -2])).to.be(false);
});
});
describe('#getCenter', function () {
it('returns the expected value', function () {
expect(circle.getCenter()).to.eql([0, 0]);
});
});
describe('#getClosestPoint', function () {
it('returns the closest point on the perimeter', function () {
let closestPoint;
closestPoint = circle.getClosestPoint([2, 0]);
expect(closestPoint[0]).to.roughlyEqual(1, 1e-15);
expect(closestPoint[1]).to.roughlyEqual(0, 1e-15);
closestPoint = circle.getClosestPoint([2, 2]);
expect(closestPoint[0]).to.roughlyEqual(Math.sqrt(0.5), 1e-15);
expect(closestPoint[1]).to.roughlyEqual(Math.sqrt(0.5), 1e-15);
closestPoint = circle.getClosestPoint([0, 2]);
expect(closestPoint[0]).to.roughlyEqual(0, 1e-15);
expect(closestPoint[1]).to.roughlyEqual(1, 1e-15);
closestPoint = circle.getClosestPoint([-2, 2]);
expect(closestPoint[0]).to.roughlyEqual(-Math.sqrt(0.5), 1e-15);
expect(closestPoint[1]).to.roughlyEqual(Math.sqrt(0.5), 1e-15);
closestPoint = circle.getClosestPoint([-2, 0]);
expect(closestPoint[0]).to.roughlyEqual(-1, 1e-15);
expect(closestPoint[1]).to.roughlyEqual(0, 1e-15);
closestPoint = circle.getClosestPoint([-2, -2]);
expect(closestPoint[0]).to.roughlyEqual(-Math.sqrt(0.5), 1e-15);
expect(closestPoint[1]).to.roughlyEqual(-Math.sqrt(0.5), 1e-15);
closestPoint = circle.getClosestPoint([0, -2]);
expect(closestPoint[0]).to.roughlyEqual(0, 1e-15);
expect(closestPoint[1]).to.roughlyEqual(-1, 1e-15);
closestPoint = circle.getClosestPoint([2, -2]);
expect(closestPoint[0]).to.roughlyEqual(Math.sqrt(0.5), 1e-15);
expect(closestPoint[1]).to.roughlyEqual(-Math.sqrt(0.5), 1e-15);
});
it('maintains Z coordinates', function () {
const circle = new Circle([0, 0, 1], 1);
expect(circle.getLayout()).to.be('XYZ');
const closestPoint = circle.getClosestPoint([2, 0]);
expect(closestPoint).to.have.length(3);
expect(closestPoint[0]).to.roughlyEqual(1, 1e-15);
expect(closestPoint[1]).to.roughlyEqual(0, 1e-15);
expect(closestPoint[2]).to.be(1);
});
it('maintains M coordinates', function () {
const circle = new Circle([0, 0, 2], 1, 'XYM');
const closestPoint = circle.getClosestPoint([2, 0]);
expect(closestPoint).to.have.length(3);
expect(closestPoint[0]).to.roughlyEqual(1, 1e-15);
expect(closestPoint[1]).to.roughlyEqual(0, 1e-15);
expect(closestPoint[2]).to.be(2);
});
it('maintains Z and M coordinates', function () {
const circle = new Circle([0, 0, 1, 2], 1);
expect(circle.getLayout()).to.be('XYZM');
const closestPoint = circle.getClosestPoint([2, 0]);
expect(closestPoint).to.have.length(4);
expect(closestPoint[0]).to.roughlyEqual(1, 1e-15);
expect(closestPoint[1]).to.roughlyEqual(0, 1e-15);
expect(closestPoint[2]).to.be(1);
expect(closestPoint[3]).to.be(2);
});
});
describe('#getExtent', function () {
it('returns the expected value', function () {
expect(circle.getExtent()).to.eql([-1, -1, 1, 1]);
});
});
describe('#getRadius', function () {
it('returns the expected value', function () {
expect(circle.getRadius()).to.be(1);
});
});
describe('#getSimplifiedGeometry', function () {
it('returns the same geometry', function () {
expect(circle.getSimplifiedGeometry(1)).to.be(circle);
});
});
describe('#getType', function () {
it('returns the expected value', function () {
expect(circle.getType()).to.be('Circle');
});
});
describe('#setCenter', function () {
it('sets the center', function () {
circle.setCenter([1, 2]);
expect(circle.getCenter()).to.eql([1, 2]);
});
it('fires a change event', function () {
const spy = sinon.spy();
circle.on('change', spy);
circle.setCenter([1, 2]);
expect(spy.calledOnce).to.be(true);
});
});
describe('#setFlatCoordinates', function () {
it('sets both center and radius', function () {
circle.setFlatCoordinates('XY', [1, 2, 4, 2]);
expect(circle.getCenter()).to.eql([1, 2]);
expect(circle.getRadius()).to.be(3);
});
});
describe('#setRadius', function () {
it('sets the radius', function () {
circle.setRadius(2);
expect(circle.getRadius()).to.be(2);
});
it('fires a change event', function () {
const spy = sinon.spy();
circle.on('change', spy);
circle.setRadius(2);
expect(spy.calledOnce).to.be(true);
});
});
describe('#intersectsExtent', function () {
it('returns false for non-intersecting extents (wide outside own bbox)', function () {
const wideOutsideLeftTop = [-3, 2, -2, 3];
const wideOutsideRightTop = [2, 2, 3, 3];
const wideOutsideRightBottom = [2, -3, 3, -2];
const wideOutsideLeftBottom = [-3, -3, -2, -2];
expect(circle.intersectsExtent(wideOutsideLeftTop)).to.be(false);
expect(circle.intersectsExtent(wideOutsideRightTop)).to.be(false);
expect(circle.intersectsExtent(wideOutsideRightBottom)).to.be(false);
expect(circle.intersectsExtent(wideOutsideLeftBottom)).to.be(false);
});
it('returns false for non-intersecting extents (inside own bbox)', function () {
const nearOutsideLeftTop = [-1, 0.9, -0.9, 1];
const nearOutsideRightTop = [0.9, 0.9, 1, 1];
const nearOutsideRightBottom = [0.9, -1, 1, -0.9];
const nearOutsideLeftBottom = [-1, -1, -0.9, -0.9];
expect(circle.intersectsExtent(nearOutsideLeftTop)).to.be(false);
expect(circle.intersectsExtent(nearOutsideRightTop)).to.be(false);
expect(circle.intersectsExtent(nearOutsideRightBottom)).to.be(false);
expect(circle.intersectsExtent(nearOutsideLeftBottom)).to.be(false);
});
it('returns true for extents that intersect clearly', function () {
const intersectingLeftTop = [-1.5, 0.5, -0.5, 1.5];
const intersectingRightTop = [0.5, 0.5, 1.5, 1.5];
const intersectingRightBottom = [0.5, -1.5, 1.5, -0.5];
const intersectingLeftBottom = [-1.5, -1.5, -0.5, -0.5];
expect(circle.intersectsExtent(intersectingLeftTop)).to.be(true);
expect(circle.intersectsExtent(intersectingRightTop)).to.be(true);
expect(circle.intersectsExtent(intersectingRightBottom)).to.be(true);
expect(circle.intersectsExtent(intersectingLeftBottom)).to.be(true);
});
it('returns true for extents that touch the circumference', function () {
const touchCircumferenceLeft = [-2, 0, -1, 1];
const touchCircumferenceTop = [0, 1, 1, 2];
const touchCircumferenceRight = [1, -1, 2, 0];
const touchCircumferenceBottom = [-1, -2, 0, -1];
expect(circle.intersectsExtent(touchCircumferenceLeft)).to.be(true);
expect(circle.intersectsExtent(touchCircumferenceTop)).to.be(true);
expect(circle.intersectsExtent(touchCircumferenceRight)).to.be(true);
expect(circle.intersectsExtent(touchCircumferenceBottom)).to.be(true);
});
it('returns true for a contained extent', function () {
const containedExtent = [-0.5, -0.5, 0.5, 0.5];
expect(circle.intersectsExtent(containedExtent)).to.be(true);
});
it('returns true for a covering extent', function () {
const bigCoveringExtent = [-5, -5, 5, 5];
expect(circle.intersectsExtent(bigCoveringExtent)).to.be(true);
});
it("returns true for the geom's own extent", function () {
const circleExtent = circle.getExtent();
expect(circle.intersectsExtent(circleExtent)).to.be(true);
});
});
describe('#rotate', function () {
it('rotates the center around the anchor', function () {
circle.setCenter([1, 0]);
circle.rotate(Math.PI / 2, [2, 0]);
expect(circle.getCenter()).to.eql([2, -1]);
expect(circle.getExtent()).to.eql([1, -2, 3, 0]);
});
it('does not change if the anchor equals the center', function () {
const center = [1, 0];
circle.setCenter(center);
const extent = circle.getExtent();
circle.rotate(Math.PI / 2, center);
expect(circle.getCenter()).to.eql(center);
expect(circle.getExtent()).to.eql(extent);
});
});
describe('#translate', function () {
it('translates the circle', function () {
circle.setCenter([1, 1]);
circle.translate(5, 10);
expect(circle.getCenter()).to.eql([6, 11]);
expect(circle.getExtent()).to.eql([5, 10, 7, 12]);
});
});
});
});

View File

@@ -0,0 +1,289 @@
import Geometry from '../../../../src/ol/geom/Geometry.js';
import GeometryCollection from '../../../../src/ol/geom/GeometryCollection.js';
import LineString from '../../../../src/ol/geom/LineString.js';
import Point from '../../../../src/ol/geom/Point.js';
import Polygon from '../../../../src/ol/geom/Polygon.js';
import expect from '../../expect.js';
import sinon from 'sinon';
describe('ol/geom/GeometryCollection.js', function () {
const outer = [
[0, 0],
[0, 10],
[10, 10],
[10, 0],
[0, 0],
];
const inner1 = [
[1, 1],
[2, 1],
[2, 2],
[1, 2],
[1, 1],
];
const inner2 = [
[8, 8],
[9, 8],
[9, 9],
[8, 9],
[8, 8],
];
describe('constructor', function () {
let line, multi, point, poly;
beforeEach(function () {
point = new Point([10, 20]);
line = new LineString([
[10, 20],
[30, 40],
]);
poly = new Polygon([outer, inner1, inner2]);
multi = new GeometryCollection([point, line, poly]);
});
it('creates a geometry collection from an array of geometries', function () {
expect(multi).to.be.a(GeometryCollection);
expect(multi).to.be.a(Geometry);
});
it('fires a change event when one of its component changes', function (done) {
multi.on('change', function () {
done();
});
point.setCoordinates([10, 10]);
});
it('deregister old components', function () {
multi.setGeometries([poly]);
multi.on('change', function () {
expect().fail();
});
point.setCoordinates([10, 10]);
});
it('register new components', function (done) {
const point2 = new Point([10, 20]);
multi.setGeometriesArray([point2]);
multi.on('change', function () {
done();
});
point2.setCoordinates([10, 10]);
});
});
describe('#getGeometries', function () {
it('returns a collection of geometries', function () {
const point = new Point([10, 20]);
const line = new LineString([
[10, 20],
[30, 40],
]);
const poly = new Polygon([outer, inner1, inner2]);
const multi = new GeometryCollection([point, line, poly]);
const geometries = multi.getGeometries();
expect(geometries).to.be.an(Array);
expect(geometries).to.have.length(3);
expect(geometries[0]).to.be.a(Point);
expect(geometries[1]).to.be.a(LineString);
expect(geometries[2]).to.be.a(Polygon);
});
});
describe('#clone()', function () {
it('has a working clone method', function () {
const point = new Point([10, 20]);
const line = new LineString([
[10, 20],
[30, 40],
]);
const poly = new Polygon([outer, inner1, inner2]);
const multi = new GeometryCollection([point, line, poly]);
multi.setProperties({foo: 'bar', baz: null});
const clone = multi.clone();
expect(clone).to.not.be(multi);
const geometries = clone.getGeometries();
expect(geometries[0].getCoordinates()).to.eql([10, 20]);
expect(geometries[1].getCoordinates()).to.eql([
[10, 20],
[30, 40],
]);
expect(geometries[2].getCoordinates()).to.eql([outer, inner1, inner2]);
expect(clone.getProperties()).to.eql({foo: 'bar', baz: null});
});
it('does a deep clone', function () {
const point = new Point([30, 40]);
const originalGeometries = [point];
const multi = new GeometryCollection(originalGeometries);
const clone = multi.clone();
const clonedGeometries = clone.getGeometries();
expect(clonedGeometries).not.to.be(originalGeometries);
expect(clonedGeometries).to.have.length(originalGeometries.length);
expect(clonedGeometries).to.have.length(1);
expect(clonedGeometries[0]).not.to.be(originalGeometries[0]);
expect(clonedGeometries[0].getCoordinates()).to.eql(
originalGeometries[0].getCoordinates()
);
});
});
describe('#getExtent()', function () {
it('returns the bounding extent', function () {
const point = new Point([10, 2]);
const line = new LineString([
[1, 20],
[30, 40],
]);
const multi = new GeometryCollection([point, line]);
const extent = multi.getExtent();
expect(extent[0]).to.be(1);
expect(extent[2]).to.be(30);
expect(extent[1]).to.be(2);
expect(extent[3]).to.be(40);
});
});
describe('#intersectsExtent()', function () {
let point, line, poly, multi;
beforeEach(function () {
point = new Point([5, 20]);
line = new LineString([
[10, 20],
[30, 40],
]);
poly = new Polygon([outer, inner1, inner2]);
multi = new GeometryCollection([point, line, poly]);
});
it('returns true for intersecting point', function () {
expect(multi.intersectsExtent([5, 20, 5, 20])).to.be(true);
});
it('returns true for intersecting part of lineString', function () {
expect(multi.intersectsExtent([25, 35, 30, 40])).to.be(true);
});
it('returns true for intersecting part of polygon', function () {
expect(multi.intersectsExtent([0, 0, 5, 5])).to.be(true);
});
it('returns false for non-matching extent within own extent', function () {
const extent = [0, 35, 5, 40];
expect(poly.intersectsExtent(extent)).to.be(false);
});
});
describe('#setGeometries', function () {
let line, multi, point, poly;
beforeEach(function () {
point = new Point([10, 20]);
line = new LineString([
[10, 20],
[30, 40],
]);
poly = new Polygon([outer, inner1, inner2]);
multi = new GeometryCollection([point, line, poly]);
});
it('fires a change event', function () {
const listener = sinon.spy();
multi.on('change', listener);
multi.setGeometries([point, line, poly]);
expect(listener.calledOnce).to.be(true);
});
it('updates the extent', function () {
expect(multi.getExtent()).to.eql([0, 0, 30, 40]);
line.setCoordinates([
[10, 20],
[300, 400],
]);
expect(multi.getExtent()).to.eql([0, 0, 300, 400]);
});
});
describe('#scale()', function () {
it('scales a collection', function () {
const geom = new GeometryCollection([
new Point([-1, -2]),
new LineString([
[0, 0],
[1, 2],
]),
]);
geom.scale(10);
const geometries = geom.getGeometries();
expect(geometries[0].getCoordinates()).to.eql([-10, -20]);
expect(geometries[1].getCoordinates()).to.eql([
[0, 0],
[10, 20],
]);
});
it('accepts sx and sy', function () {
const geom = new GeometryCollection([
new Point([-1, -2]),
new LineString([
[0, 0],
[1, 2],
]),
]);
geom.scale(2, 3);
const geometries = geom.getGeometries();
expect(geometries[0].getCoordinates()).to.eql([-2, -6]);
expect(geometries[1].getCoordinates()).to.eql([
[0, 0],
[2, 6],
]);
});
it('accepts an anchor', function () {
const geom = new GeometryCollection([
new Point([-1, -2]),
new LineString([
[0, 0],
[1, 2],
]),
]);
geom.scale(10, 15, [-1, -2]);
const geometries = geom.getGeometries();
expect(geometries[0].getCoordinates()).to.eql([-1, -2]);
expect(geometries[1].getCoordinates()).to.eql([
[9, 28],
[19, 58],
]);
});
});
describe('#transform()', function () {
let line, multi, point;
beforeEach(function () {
point = new Point([10, 20]);
line = new LineString([
[10, 20],
[30, 40],
]);
multi = new GeometryCollection([point, line]);
});
it('transforms all geometries', function () {
multi.transform('EPSG:4326', 'EPSG:3857');
const geometries = multi.getGeometries();
expect(geometries[0]).to.be.a(Point);
expect(geometries[1]).to.be.a(LineString);
let coords = geometries[0].getCoordinates();
expect(coords[0]).to.roughlyEqual(1113194.9, 1e-2);
expect(coords[1]).to.roughlyEqual(2273030.92, 1e-2);
coords = geometries[1].getCoordinates();
expect(coords[0][0]).to.roughlyEqual(1113194.9, 1e-2);
expect(coords[0][1]).to.roughlyEqual(2273030.92, 1e-2);
expect(coords[1][0]).to.roughlyEqual(3339584.72, 1e-2);
expect(coords[1][1]).to.roughlyEqual(4865942.27, 1e-2);
});
});
});

View File

@@ -0,0 +1,526 @@
import LineString from '../../../../src/ol/geom/LineString.js';
import expect from '../../expect.js';
import sinon from 'sinon';
import {isEmpty} from '../../../../src/ol/extent.js';
describe('ol/geom/LineString.js', function () {
it('cannot be constructed with a null geometry', function () {
expect(function () {
return new LineString(null);
}).to.throwException();
});
describe('construct empty', function () {
let lineString;
beforeEach(function () {
lineString = new LineString([]);
});
it('defaults to layout XY', function () {
expect(lineString.getLayout()).to.be('XY');
});
it('has empty coordinates', function () {
expect(lineString.getCoordinates()).to.be.empty();
});
it('has an empty extent', function () {
expect(isEmpty(lineString.getExtent())).to.be(true);
});
it('has empty flat coordinates', function () {
expect(lineString.getFlatCoordinates()).to.be.empty();
});
it('has stride the expected stride', function () {
expect(lineString.getStride()).to.be(2);
});
it('can append coordinates', function () {
lineString.appendCoordinate([1, 2]);
expect(lineString.getCoordinates()).to.eql([[1, 2]]);
lineString.appendCoordinate([3, 4]);
expect(lineString.getCoordinates()).to.eql([
[1, 2],
[3, 4],
]);
});
});
describe('construct with 2D coordinates', function () {
let lineString;
beforeEach(function () {
lineString = new LineString([
[1, 2],
[3, 4],
]);
});
it('has the expected layout', function () {
expect(lineString.getLayout()).to.be('XY');
});
it('has the expected coordinates', function () {
expect(lineString.getCoordinates()).to.eql([
[1, 2],
[3, 4],
]);
});
it('has the expected extent', function () {
expect(lineString.getExtent()).to.eql([1, 2, 3, 4]);
});
it('has the expected flat coordinates', function () {
expect(lineString.getFlatCoordinates()).to.eql([1, 2, 3, 4]);
});
it('has stride the expected stride', function () {
expect(lineString.getStride()).to.be(2);
});
describe('#intersectsCoordinate', function () {
it('returns true for an intersecting coordinate', function () {
expect(lineString.intersectsCoordinate([1.5, 2.5])).to.be(true);
});
});
describe('#intersectsExtent', function () {
it('return false for non matching extent', function () {
expect(lineString.intersectsExtent([1, 3, 1.9, 4])).to.be(false);
});
it('return true for extent on midpoint', function () {
expect(lineString.intersectsExtent([2, 3, 4, 3])).to.be(true);
});
it("returns true for the geom's own extent", function () {
expect(lineString.intersectsExtent(lineString.getExtent())).to.be(true);
});
});
describe('#intersectsCoordinate', function () {
it('detects intersecting coordinates', function () {
expect(lineString.intersectsCoordinate([1, 2])).to.be(true);
});
});
describe('#getClosestPoint', function () {
it('uses existing vertices', function () {
const closestPoint = lineString.getClosestPoint([0.9, 1.8]);
expect(closestPoint).to.eql([1, 2]);
});
});
describe('#getCoordinateAt', function () {
it('return the first point when fraction is 0', function () {
expect(lineString.getCoordinateAt(0)).to.eql([1, 2]);
});
it('return the last point when fraction is 1', function () {
expect(lineString.getCoordinateAt(1)).to.eql([3, 4]);
});
it('return the mid point when fraction is 0.5', function () {
expect(lineString.getCoordinateAt(0.5)).to.eql([2, 3]);
});
});
});
describe('construct with 3D coordinates', function () {
let lineString;
beforeEach(function () {
lineString = new LineString([
[1, 2, 3],
[4, 5, 6],
]);
});
it('has the expected layout', function () {
expect(lineString.getLayout()).to.be('XYZ');
});
it('has the expected coordinates', function () {
expect(lineString.getCoordinates()).to.eql([
[1, 2, 3],
[4, 5, 6],
]);
});
it('has the expected extent', function () {
expect(lineString.getExtent()).to.eql([1, 2, 4, 5]);
});
it('has the expected flat coordinates', function () {
expect(lineString.getFlatCoordinates()).to.eql([1, 2, 3, 4, 5, 6]);
});
it('has the expected stride', function () {
expect(lineString.getStride()).to.be(3);
});
describe('#intersectsExtent', function () {
it('return false for non matching extent', function () {
expect(lineString.intersectsExtent([1, 3, 1.9, 4])).to.be(false);
});
it('return true for extent on midpoint', function () {
expect(lineString.intersectsExtent([2, 3, 4, 3])).to.be(true);
});
it("returns true for the geom's own extent", function () {
expect(lineString.intersectsExtent(lineString.getExtent())).to.be(true);
});
});
});
describe('construct with 3D coordinates and layout XYM', function () {
let lineString;
beforeEach(function () {
lineString = new LineString(
[
[1, 2, 3],
[4, 5, 6],
],
'XYM'
);
});
it('has the expected layout', function () {
expect(lineString.getLayout()).to.be('XYM');
});
it('has the expected coordinates', function () {
expect(lineString.getCoordinates()).to.eql([
[1, 2, 3],
[4, 5, 6],
]);
});
it('has the expected extent', function () {
expect(lineString.getExtent()).to.eql([1, 2, 4, 5]);
});
it('has the expected flat coordinates', function () {
expect(lineString.getFlatCoordinates()).to.eql([1, 2, 3, 4, 5, 6]);
});
it('has the expected stride', function () {
expect(lineString.getStride()).to.be(3);
});
describe('#intersectsExtent', function () {
it('return false for non matching extent', function () {
expect(lineString.intersectsExtent([1, 3, 1.9, 4])).to.be(false);
});
it('return true for extent on midpoint', function () {
expect(lineString.intersectsExtent([2, 3, 4, 3])).to.be(true);
});
it("returns true for the geom's own extent", function () {
expect(lineString.intersectsExtent(lineString.getExtent())).to.be(true);
});
});
});
describe('construct with 4D coordinates', function () {
let lineString;
beforeEach(function () {
lineString = new LineString([
[1, 2, 3, 4],
[5, 6, 7, 8],
]);
});
it('has the expected layout', function () {
expect(lineString.getLayout()).to.be('XYZM');
});
it('has the expected coordinates', function () {
expect(lineString.getCoordinates()).to.eql([
[1, 2, 3, 4],
[5, 6, 7, 8],
]);
});
it('has the expected extent', function () {
expect(lineString.getExtent()).to.eql([1, 2, 5, 6]);
});
it('has the expected flat coordinates', function () {
expect(lineString.getFlatCoordinates()).to.eql([1, 2, 3, 4, 5, 6, 7, 8]);
});
it('has the expected stride', function () {
expect(lineString.getStride()).to.be(4);
});
describe('#intersectsExtent', function () {
it('return false for non matching extent', function () {
expect(lineString.intersectsExtent([1, 3, 1.9, 4])).to.be(false);
});
it('return true for extent on midpoint', function () {
expect(lineString.intersectsExtent([2, 3, 4, 3])).to.be(true);
});
it("returns true for the geom's own extent", function () {
expect(lineString.intersectsExtent(lineString.getExtent())).to.be(true);
});
});
});
describe('#scale()', function () {
it('scales a linestring', function () {
const geom = new LineString([
[-10, -20],
[10, 20],
]);
geom.scale(10);
const coordinates = geom.getCoordinates();
expect(coordinates).to.eql([
[-100, -200],
[100, 200],
]);
});
it('accepts sx and sy', function () {
const geom = new LineString([
[-10, -20],
[10, 20],
]);
geom.scale(2, 3);
const coordinates = geom.getCoordinates();
expect(coordinates).to.eql([
[-20, -60],
[20, 60],
]);
});
it('accepts an anchor', function () {
const geom = new LineString([
[-10, -20],
[10, 20],
]);
geom.scale(3, 2, [10, 20]);
const coordinates = geom.getCoordinates();
expect(coordinates).to.eql([
[-50, -60],
[10, 20],
]);
});
});
describe('with a simple line string', function () {
let lineString;
beforeEach(function () {
lineString = new LineString([
[0, 0],
[1.5, 1],
[3, 3],
[5, 1],
[6, 3.5],
[7, 5],
]);
});
describe('#getFirstCoordinate', function () {
it('returns the expected result', function () {
expect(lineString.getFirstCoordinate()).to.eql([0, 0]);
});
});
describe('#getFlatMidpoint', function () {
it('returns the expected result', function () {
const midpoint = lineString.getFlatMidpoint();
expect(midpoint).to.be.an(Array);
expect(midpoint).to.have.length(2);
expect(midpoint[0]).to.roughlyEqual(4, 1e-1);
expect(midpoint[1]).to.roughlyEqual(2, 1e-1);
});
});
describe('#getLastCoordinate', function () {
it('returns the expected result', function () {
expect(lineString.getLastCoordinate()).to.eql([7, 5]);
});
});
describe('#simplify', function () {
it('returns a simplified geometry', function () {
const simplified = lineString.simplify(1);
expect(simplified).to.be.an(LineString);
expect(simplified.getCoordinates()).to.eql([
[0, 0],
[3, 3],
[5, 1],
[7, 5],
]);
});
it('does not modify the original', function () {
lineString.simplify(1);
expect(lineString.getCoordinates()).to.eql([
[0, 0],
[1.5, 1],
[3, 3],
[5, 1],
[6, 3.5],
[7, 5],
]);
});
it('delegates to the internal method', function () {
const simplified = lineString.simplify(2);
const internal = lineString.getSimplifiedGeometry(4);
expect(simplified.getCoordinates()).to.eql(internal.getCoordinates());
});
});
describe('#getSimplifiedGeometry', function () {
it('returns the expectedResult', function () {
const simplifiedGeometry = lineString.getSimplifiedGeometry(1);
expect(simplifiedGeometry).to.be.an(LineString);
expect(simplifiedGeometry.getCoordinates()).to.eql([
[0, 0],
[3, 3],
[5, 1],
[7, 5],
]);
});
it('remembers the minimum squared tolerance', function () {
sinon.spy(lineString, 'getSimplifiedGeometryInternal');
const simplifiedGeometry1 = lineString.getSimplifiedGeometry(0.05);
expect(lineString.getSimplifiedGeometryInternal.callCount).to.be(1);
expect(simplifiedGeometry1).to.be(lineString);
const simplifiedGeometry2 = lineString.getSimplifiedGeometry(0.01);
expect(lineString.getSimplifiedGeometryInternal.callCount).to.be(1);
expect(simplifiedGeometry2).to.be(lineString);
});
});
describe('#getCoordinateAt', function () {
it('return the first point when fraction is 0', function () {
expect(lineString.getCoordinateAt(0)).to.eql([0, 0]);
});
it('return the last point when fraction is 1', function () {
expect(lineString.getCoordinateAt(1)).to.eql([7, 5]);
});
it('return the mid point when fraction is 0.5', function () {
const midpoint = lineString.getFlatMidpoint();
expect(lineString.getCoordinateAt(0.5)).to.eql(midpoint);
});
});
});
describe('with a simple XYM coordinates', function () {
let lineString;
beforeEach(function () {
lineString = new LineString(
[
[1, 2, 3],
[4, 5, 6],
],
'XYM'
);
});
describe('#getCoordinateAt', function () {
it('returns the expected value', function () {
expect(lineString.getCoordinateAt(0.5)).to.eql([2.5, 3.5, 4.5]);
});
});
describe('#getCoordinateAtM', function () {
it('returns the expected value', function () {
expect(lineString.getCoordinateAtM(2, false)).to.be(null);
expect(lineString.getCoordinateAtM(2, true)).to.eql([1, 2, 2]);
expect(lineString.getCoordinateAtM(3, false)).to.eql([1, 2, 3]);
expect(lineString.getCoordinateAtM(3, true)).to.eql([1, 2, 3]);
expect(lineString.getCoordinateAtM(4, false)).to.eql([2, 3, 4]);
expect(lineString.getCoordinateAtM(4, true)).to.eql([2, 3, 4]);
expect(lineString.getCoordinateAtM(5, false)).to.eql([3, 4, 5]);
expect(lineString.getCoordinateAtM(5, true)).to.eql([3, 4, 5]);
expect(lineString.getCoordinateAtM(6, false)).to.eql([4, 5, 6]);
expect(lineString.getCoordinateAtM(6, true)).to.eql([4, 5, 6]);
expect(lineString.getCoordinateAtM(7, false)).to.eql(null);
expect(lineString.getCoordinateAtM(7, true)).to.eql([4, 5, 7]);
});
});
});
describe('with several XYZM coordinates', function () {
let lineString;
beforeEach(function () {
lineString = new LineString([
[0, 0, 0, 0],
[1, -1, 2, 1],
[2, -2, 4, 2],
[4, -4, 8, 4],
[8, -8, 16, 8],
[12, -12, 24, 12],
[14, -14, 28, 14],
[15, -15, 30, 15],
[16, -16, 32, 16],
[18, -18, 36, 18],
[22, -22, 44, 22],
]);
});
describe('#getCoordinateAt', function () {
it('returns the expected value', function () {
expect(lineString.getCoordinateAt(0.5)).to.eql([11, -11, 22, 11]);
});
});
describe('#getCoordinateAtM', function () {
it('returns the expected value', function () {
expect(lineString.getLayout()).to.be('XYZM');
let m;
for (m = 0; m <= 22; m += 0.5) {
expect(lineString.getCoordinateAtM(m, true)).to.eql([
m,
-m,
2 * m,
m,
]);
}
});
});
});
describe('#containsXY()', function () {
let lineString;
beforeEach(function () {
lineString = new LineString([
[0, 0, 0, 0],
[1, -1, 2, 1],
[2, -2, 4, 2],
[4, -4, 8, 4],
[8, -8, 16, 8],
[12, -12, 24, 12],
[14, -14, 28, 14],
[15, -15, 30, 15],
[16, -16, 32, 16],
[18, -18, 36, 18],
[22, -22, 44, 22],
]);
});
it('does contain XY', function () {
expect(lineString.containsXY(1, -1)).to.be(true);
expect(lineString.containsXY(16, -16)).to.be(true);
expect(lineString.containsXY(3, -3)).to.be(true);
});
it('does not contain XY', function () {
expect(lineString.containsXY(1, 3)).to.be(false);
expect(lineString.containsXY(2, 2)).to.be(false);
expect(lineString.containsXY(2, 3)).to.be(false);
});
});
});

View File

@@ -0,0 +1,663 @@
import LineString from '../../../../src/ol/geom/LineString.js';
import MultiLineString from '../../../../src/ol/geom/MultiLineString.js';
import expect from '../../expect.js';
import {isEmpty} from '../../../../src/ol/extent.js';
describe('ol/geom/MultiLineString.js', function () {
it('cannot be constructed with a null geometry', function () {
expect(function () {
return new MultiLineString(null);
}).to.throwException();
});
describe('construct empty', function () {
let multiLineString;
beforeEach(function () {
multiLineString = new MultiLineString([]);
});
it('defaults to layout XY', function () {
expect(multiLineString.getLayout()).to.be('XY');
});
it('has empty coordinates', function () {
expect(multiLineString.getCoordinates()).to.be.empty();
});
it('has an empty extent', function () {
expect(isEmpty(multiLineString.getExtent())).to.be(true);
});
it('has empty flat coordinates', function () {
expect(multiLineString.getFlatCoordinates()).to.be.empty();
});
it('has stride the expected stride', function () {
expect(multiLineString.getStride()).to.be(2);
});
it('can append line strings', function () {
multiLineString.appendLineString(
new LineString([
[1, 2],
[3, 4],
])
);
expect(multiLineString.getCoordinates()).to.eql([
[
[1, 2],
[3, 4],
],
]);
multiLineString.appendLineString(
new LineString([
[5, 6],
[7, 8],
])
);
expect(multiLineString.getCoordinates()).to.eql([
[
[1, 2],
[3, 4],
],
[
[5, 6],
[7, 8],
],
]);
});
});
describe('construct with 2D coordinates', function () {
let multiLineString;
beforeEach(function () {
multiLineString = new MultiLineString([
[
[1, 2],
[3, 4],
],
[
[5, 6],
[7, 8],
],
]);
});
it('has the expected layout', function () {
expect(multiLineString.getLayout()).to.be('XY');
});
it('has the expected coordinates', function () {
expect(multiLineString.getCoordinates()).to.eql([
[
[1, 2],
[3, 4],
],
[
[5, 6],
[7, 8],
],
]);
});
it('has the expected extent', function () {
expect(multiLineString.getExtent()).to.eql([1, 2, 7, 8]);
});
it('has the expected flat coordinates', function () {
expect(multiLineString.getFlatCoordinates()).to.eql([
1,
2,
3,
4,
5,
6,
7,
8,
]);
});
it('has stride the expected stride', function () {
expect(multiLineString.getStride()).to.be(2);
});
describe('#getFlatMidpoints', function () {
it('returns the expected result', function () {
expect(multiLineString.getFlatMidpoints()).to.eql([2, 3, 6, 7]);
});
});
describe('#intersectsExtent()', function () {
it('returns true for intersecting part of lineString', function () {
expect(multiLineString.intersectsExtent([1, 2, 2, 3])).to.be(true);
});
it('returns false for non-matching extent within own extent', function () {
expect(multiLineString.intersectsExtent([1, 7, 2, 8])).to.be(false);
});
});
});
describe('construct with 3D coordinates', function () {
let multiLineString;
beforeEach(function () {
multiLineString = new MultiLineString([
[
[1, 2, 3],
[4, 5, 6],
],
[
[7, 8, 9],
[10, 11, 12],
],
]);
});
it('has the expected layout', function () {
expect(multiLineString.getLayout()).to.be('XYZ');
});
it('has the expected coordinates', function () {
expect(multiLineString.getCoordinates()).to.eql([
[
[1, 2, 3],
[4, 5, 6],
],
[
[7, 8, 9],
[10, 11, 12],
],
]);
});
it('has the expected extent', function () {
expect(multiLineString.getExtent()).to.eql([1, 2, 10, 11]);
});
it('has the expected flat coordinates', function () {
expect(multiLineString.getFlatCoordinates()).to.eql([
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
]);
});
it('has stride the expected stride', function () {
expect(multiLineString.getStride()).to.be(3);
});
});
describe('construct with 3D coordinates and layout XYM', function () {
let multiLineString;
beforeEach(function () {
multiLineString = new MultiLineString(
[
[
[1, 2, 3],
[4, 5, 6],
],
[
[7, 8, 9],
[10, 11, 12],
],
],
'XYM'
);
});
it('has the expected layout', function () {
expect(multiLineString.getLayout()).to.be('XYM');
});
it('has the expected coordinates', function () {
expect(multiLineString.getCoordinates()).to.eql([
[
[1, 2, 3],
[4, 5, 6],
],
[
[7, 8, 9],
[10, 11, 12],
],
]);
});
it('has the expected extent', function () {
expect(multiLineString.getExtent()).to.eql([1, 2, 10, 11]);
});
it('has the expected flat coordinates', function () {
expect(multiLineString.getFlatCoordinates()).to.eql([
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
]);
});
it('has stride the expected stride', function () {
expect(multiLineString.getStride()).to.be(3);
});
it('can return individual line strings', function () {
const lineString0 = multiLineString.getLineString(0);
expect(lineString0).to.be.an(LineString);
expect(lineString0.getLayout()).to.be('XYM');
expect(lineString0.getCoordinates()).to.eql([
[1, 2, 3],
[4, 5, 6],
]);
const lineString1 = multiLineString.getLineString(1);
expect(lineString1).to.be.an(LineString);
expect(lineString1.getLayout()).to.be('XYM');
expect(lineString1.getCoordinates()).to.eql([
[7, 8, 9],
[10, 11, 12],
]);
});
describe('#getCoordinateAtM', function () {
describe('with extrapolation and interpolation', function () {
it('returns the expected value', function () {
expect(multiLineString.getCoordinateAtM(0, true, true)).to.eql([
1,
2,
0,
]);
expect(multiLineString.getCoordinateAtM(3, true, true)).to.eql([
1,
2,
3,
]);
expect(multiLineString.getCoordinateAtM(4.5, true, true)).to.eql([
2.5,
3.5,
4.5,
]);
expect(multiLineString.getCoordinateAtM(6, true, true)).to.eql([
4,
5,
6,
]);
expect(multiLineString.getCoordinateAtM(7.5, true, true)).to.eql([
5.5,
6.5,
7.5,
]);
expect(multiLineString.getCoordinateAtM(9, true, true)).to.eql([
7,
8,
9,
]);
expect(multiLineString.getCoordinateAtM(10.5, true, true)).to.eql([
8.5,
9.5,
10.5,
]);
expect(multiLineString.getCoordinateAtM(12, true, true)).to.eql([
10,
11,
12,
]);
expect(multiLineString.getCoordinateAtM(15, true, true)).to.eql([
10,
11,
15,
]);
});
});
describe('with extrapolation and no interpolation', function () {
it('returns the expected value', function () {
expect(multiLineString.getCoordinateAtM(0, true, false)).to.eql([
1,
2,
0,
]);
expect(multiLineString.getCoordinateAtM(3, true, false)).to.eql([
1,
2,
3,
]);
expect(multiLineString.getCoordinateAtM(4.5, true, false)).to.eql([
2.5,
3.5,
4.5,
]);
expect(multiLineString.getCoordinateAtM(6, true, false)).to.eql([
4,
5,
6,
]);
expect(multiLineString.getCoordinateAtM(7.5, true, false)).to.be(
null
);
expect(multiLineString.getCoordinateAtM(9, true, false)).to.eql([
7,
8,
9,
]);
expect(multiLineString.getCoordinateAtM(10.5, true, false)).to.eql([
8.5,
9.5,
10.5,
]);
expect(multiLineString.getCoordinateAtM(12, true, false)).to.eql([
10,
11,
12,
]);
expect(multiLineString.getCoordinateAtM(15, true, false)).to.eql([
10,
11,
15,
]);
});
});
describe('with no extrapolation and interpolation', function () {
it('returns the expected value', function () {
expect(multiLineString.getCoordinateAtM(0, false, true)).to.eql(null);
expect(multiLineString.getCoordinateAtM(3, false, true)).to.eql([
1,
2,
3,
]);
expect(multiLineString.getCoordinateAtM(4.5, false, true)).to.eql([
2.5,
3.5,
4.5,
]);
expect(multiLineString.getCoordinateAtM(6, false, true)).to.eql([
4,
5,
6,
]);
expect(multiLineString.getCoordinateAtM(7.5, false, true)).to.eql([
5.5,
6.5,
7.5,
]);
expect(multiLineString.getCoordinateAtM(9, false, true)).to.eql([
7,
8,
9,
]);
expect(multiLineString.getCoordinateAtM(10.5, false, true)).to.eql([
8.5,
9.5,
10.5,
]);
expect(multiLineString.getCoordinateAtM(12, false, true)).to.eql([
10,
11,
12,
]);
expect(multiLineString.getCoordinateAtM(15, false, true)).to.eql(
null
);
});
});
describe('with no extrapolation or interpolation', function () {
it('returns the expected value', function () {
expect(multiLineString.getCoordinateAtM(0, false, false)).to.eql(
null
);
expect(multiLineString.getCoordinateAtM(3, false, false)).to.eql([
1,
2,
3,
]);
expect(multiLineString.getCoordinateAtM(4.5, false, false)).to.eql([
2.5,
3.5,
4.5,
]);
expect(multiLineString.getCoordinateAtM(6, false, false)).to.eql([
4,
5,
6,
]);
expect(multiLineString.getCoordinateAtM(7.5, false, false)).to.eql(
null
);
expect(multiLineString.getCoordinateAtM(9, false, false)).to.eql([
7,
8,
9,
]);
expect(multiLineString.getCoordinateAtM(10.5, false, false)).to.eql([
8.5,
9.5,
10.5,
]);
expect(multiLineString.getCoordinateAtM(12, false, false)).to.eql([
10,
11,
12,
]);
expect(multiLineString.getCoordinateAtM(15, false, false)).to.eql(
null
);
});
});
});
});
describe('construct with 4D coordinates', function () {
let multiLineString;
beforeEach(function () {
multiLineString = new MultiLineString([
[
[1, 2, 3, 4],
[5, 6, 7, 8],
],
[
[9, 10, 11, 12],
[13, 14, 15, 16],
],
]);
});
it('has the expected layout', function () {
expect(multiLineString.getLayout()).to.be('XYZM');
});
it('has the expected coordinates', function () {
expect(multiLineString.getCoordinates()).to.eql([
[
[1, 2, 3, 4],
[5, 6, 7, 8],
],
[
[9, 10, 11, 12],
[13, 14, 15, 16],
],
]);
});
it('has the expected extent', function () {
expect(multiLineString.getExtent()).to.eql([1, 2, 13, 14]);
});
it('has the expected flat coordinates', function () {
expect(multiLineString.getFlatCoordinates()).to.eql([
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
]);
});
it('has stride the expected stride', function () {
expect(multiLineString.getStride()).to.be(4);
});
});
describe('#scale()', function () {
it('scales a multi-linestring', function () {
const geom = new MultiLineString([
[
[-10, -20],
[10, 20],
],
[
[5, -10],
[-5, 10],
],
]);
geom.scale(10);
const coordinates = geom.getCoordinates();
expect(coordinates).to.eql([
[
[-100, -200],
[100, 200],
],
[
[50, -100],
[-50, 100],
],
]);
});
it('accepts sx and sy', function () {
const geom = new MultiLineString([
[
[-10, -20],
[10, 20],
],
[
[5, -10],
[-5, 10],
],
]);
geom.scale(2, 3);
const coordinates = geom.getCoordinates();
expect(coordinates).to.eql([
[
[-20, -60],
[20, 60],
],
[
[10, -30],
[-10, 30],
],
]);
});
it('accepts an anchor', function () {
const geom = new MultiLineString([
[
[-10, -20],
[10, 20],
],
[
[5, -10],
[-5, 10],
],
]);
geom.scale(3, 2, [10, 20]);
const coordinates = geom.getCoordinates();
expect(coordinates).to.eql([
[
[-50, -60],
[10, 20],
],
[
[-5, -40],
[-35, 0],
],
]);
});
});
describe('#setLineStrings', function () {
it('sets the line strings', function () {
const lineString1 = new LineString([
[1, 2],
[3, 4],
]);
const lineString2 = new LineString([
[5, 6],
[7, 8],
]);
const multiLineString = new MultiLineString([lineString1, lineString2]);
expect(multiLineString.getFlatCoordinates()).to.eql([
1,
2,
3,
4,
5,
6,
7,
8,
]);
expect(multiLineString.getEnds()).to.eql([4, 8]);
const coordinates = multiLineString.getCoordinates();
expect(coordinates[0]).to.eql(lineString1.getCoordinates());
expect(coordinates[1]).to.eql(lineString2.getCoordinates());
});
});
describe('#containsXY()', function () {
let multiLineString;
beforeEach(function () {
multiLineString = new MultiLineString([
[
[1, 2, 3],
[4, 5, 6],
],
[
[-1, -1, 9],
[2, 2, 12],
],
]);
});
it('does contain XY', function () {
expect(multiLineString.containsXY(1, 2)).to.be(true);
expect(multiLineString.containsXY(4, 5)).to.be(true);
expect(multiLineString.containsXY(3, 4)).to.be(true);
expect(multiLineString.containsXY(-1, -1)).to.be(true);
expect(multiLineString.containsXY(2, 2)).to.be(true);
expect(multiLineString.containsXY(0, 0)).to.be(true);
});
it('does not contain XY', function () {
expect(multiLineString.containsXY(1, 3)).to.be(false);
expect(multiLineString.containsXY(2, 11)).to.be(false);
expect(multiLineString.containsXY(-2, 3)).to.be(false);
});
});
});

View File

@@ -0,0 +1,351 @@
import MultiPoint from '../../../../src/ol/geom/MultiPoint.js';
import Point from '../../../../src/ol/geom/Point.js';
import expect from '../../expect.js';
import sinon from 'sinon';
import {isEmpty} from '../../../../src/ol/extent.js';
describe('ol/geom/MultiPoint.js', function () {
it('cannot be constructed with a null geometry', function () {
expect(function () {
return new MultiPoint(null);
}).to.throwException();
});
describe('construct empty', function () {
let multiPoint;
beforeEach(function () {
multiPoint = new MultiPoint([]);
});
it('defaults to layout XY', function () {
expect(multiPoint.getLayout()).to.be('XY');
});
it('has empty coordinates', function () {
expect(multiPoint.getCoordinates()).to.be.empty();
});
it('has an empty extent', function () {
expect(isEmpty(multiPoint.getExtent())).to.be(true);
});
it('has empty flat coordinates', function () {
expect(multiPoint.getFlatCoordinates()).to.be.empty();
});
it('has stride the expected stride', function () {
expect(multiPoint.getStride()).to.be(2);
});
it('can append points', function () {
multiPoint.appendPoint(new Point([1, 2]));
expect(multiPoint.getCoordinates()).to.eql([[1, 2]]);
multiPoint.appendPoint(new Point([3, 4]));
expect(multiPoint.getCoordinates()).to.eql([
[1, 2],
[3, 4],
]);
});
});
describe('construct with 2D coordinates', function () {
let multiPoint;
beforeEach(function () {
multiPoint = new MultiPoint([
[1, 2],
[3, 4],
]);
});
it('has the expected layout', function () {
expect(multiPoint.getLayout()).to.be('XY');
});
it('has the expected coordinates', function () {
expect(multiPoint.getCoordinates()).to.eql([
[1, 2],
[3, 4],
]);
});
it('has the expected extent', function () {
expect(multiPoint.getExtent()).to.eql([1, 2, 3, 4]);
});
it('has the expected flat coordinates', function () {
expect(multiPoint.getFlatCoordinates()).to.eql([1, 2, 3, 4]);
});
it('has stride the expected stride', function () {
expect(multiPoint.getStride()).to.be(2);
});
describe('#intersectsExtent()', function () {
it('returns true for extent covering a point', function () {
expect(multiPoint.intersectsExtent([1, 2, 2, 2])).to.be(true);
});
it('returns false for non-matching extent within own extent', function () {
expect(multiPoint.intersectsExtent([2, 3, 2, 4])).to.be(false);
});
});
});
describe('construct with 3D coordinates', function () {
let multiPoint;
beforeEach(function () {
multiPoint = new MultiPoint([
[1, 2, 3],
[4, 5, 6],
]);
});
it('has the expected layout', function () {
expect(multiPoint.getLayout()).to.be('XYZ');
});
it('has the expected coordinates', function () {
expect(multiPoint.getCoordinates()).to.eql([
[1, 2, 3],
[4, 5, 6],
]);
});
it('has the expected extent', function () {
expect(multiPoint.getExtent()).to.eql([1, 2, 4, 5]);
});
it('has the expected flat coordinates', function () {
expect(multiPoint.getFlatCoordinates()).to.eql([1, 2, 3, 4, 5, 6]);
});
it('has the expected stride', function () {
expect(multiPoint.getStride()).to.be(3);
});
});
describe('construct with 3D coordinates and layout XYM', function () {
let multiPoint;
beforeEach(function () {
multiPoint = new MultiPoint(
[
[1, 2, 3],
[4, 5, 6],
],
'XYM'
);
});
it('has the expected layout', function () {
expect(multiPoint.getLayout()).to.be('XYM');
});
it('has the expected coordinates', function () {
expect(multiPoint.getCoordinates()).to.eql([
[1, 2, 3],
[4, 5, 6],
]);
});
it('has the expected extent', function () {
expect(multiPoint.getExtent()).to.eql([1, 2, 4, 5]);
});
it('has the expected flat coordinates', function () {
expect(multiPoint.getFlatCoordinates()).to.eql([1, 2, 3, 4, 5, 6]);
});
it('has the expected stride', function () {
expect(multiPoint.getStride()).to.be(3);
});
it('can return individual points', function () {
const point0 = multiPoint.getPoint(0);
expect(point0.getLayout()).to.be('XYM');
expect(point0.getCoordinates()).to.eql([1, 2, 3]);
const point1 = multiPoint.getPoint(1);
expect(point1.getLayout()).to.be('XYM');
expect(point1.getCoordinates()).to.eql([4, 5, 6]);
});
it('can return all points', function () {
const points = multiPoint.getPoints();
expect(points).to.have.length(2);
expect(points[0]).to.be.an(Point);
expect(points[0].getLayout()).to.be('XYM');
expect(points[0].getCoordinates()).to.eql([1, 2, 3]);
expect(points[1]).to.be.an(Point);
expect(points[1].getLayout()).to.be('XYM');
expect(points[1].getCoordinates()).to.eql([4, 5, 6]);
});
});
describe('construct with 4D coordinates', function () {
let multiPoint;
beforeEach(function () {
multiPoint = new MultiPoint([
[1, 2, 3, 4],
[5, 6, 7, 8],
]);
});
it('has the expected layout', function () {
expect(multiPoint.getLayout()).to.be('XYZM');
});
it('has the expected coordinates', function () {
expect(multiPoint.getCoordinates()).to.eql([
[1, 2, 3, 4],
[5, 6, 7, 8],
]);
});
it('has the expected extent', function () {
expect(multiPoint.getExtent()).to.eql([1, 2, 5, 6]);
});
it('has the expected flat coordinates', function () {
expect(multiPoint.getFlatCoordinates()).to.eql([1, 2, 3, 4, 5, 6, 7, 8]);
});
it('has the expected stride', function () {
expect(multiPoint.getStride()).to.be(4);
});
describe('#getClosestPoint', function () {
it('preserves extra dimensions', function () {
const closestPoint = multiPoint.getClosestPoint([6, 6]);
expect(closestPoint).to.eql([5, 6, 7, 8]);
});
});
});
describe('#scale()', function () {
it('scales a multi-point', function () {
const geom = new MultiPoint([
[-10, -20],
[10, 20],
]);
geom.scale(10);
const coordinates = geom.getCoordinates();
expect(coordinates).to.eql([
[-100, -200],
[100, 200],
]);
});
it('accepts sx and sy', function () {
const geom = new MultiPoint([
[-10, -20],
[10, 20],
]);
geom.scale(2, 3);
const coordinates = geom.getCoordinates();
expect(coordinates).to.eql([
[-20, -60],
[20, 60],
]);
});
it('accepts an anchor', function () {
const geom = new MultiPoint([
[-10, -20],
[10, 20],
]);
geom.scale(3, 2, [-10, -20]);
const coordinates = geom.getCoordinates();
expect(coordinates).to.eql([
[-10, -20],
[50, 60],
]);
});
});
describe('#applyTransform()', function () {
let multi, transform;
beforeEach(function () {
multi = new MultiPoint([
[1, 2],
[3, 4],
]);
transform = sinon.spy();
});
it('calls a transform function', function () {
multi.applyTransform(transform);
expect(transform.calledOnce).to.be(true);
const args = transform.firstCall.args;
expect(args).to.have.length(3);
expect(args[0]).to.be(multi.getFlatCoordinates()); // input coords
expect(args[1]).to.be(multi.getFlatCoordinates()); // output coords
expect(args[2]).to.be(2); // dimension
});
it('allows for modification of coordinates', function () {
const mod = function (input, output, dimension) {
const copy = input.slice();
for (let i = 0, ii = copy.length; i < ii; i += dimension) {
output[i] = copy[i + 1];
output[i + 1] = copy[i];
}
};
multi.applyTransform(mod);
expect(multi.getCoordinates()).to.eql([
[2, 1],
[4, 3],
]);
});
it('returns undefined', function () {
const got = multi.applyTransform(transform);
expect(got).to.be(undefined);
});
});
describe('#transform()', function () {
it('transforms a geometry given CRS identifiers', function () {
const multi = new MultiPoint([
[-111, 45],
[111, -45],
]).transform('EPSG:4326', 'EPSG:3857');
expect(multi).to.be.a(MultiPoint);
const coords = multi.getCoordinates();
expect(coords[0][0]).to.roughlyEqual(-12356463.47, 1e-2);
expect(coords[0][1]).to.roughlyEqual(5621521.48, 1e-2);
expect(coords[1][0]).to.roughlyEqual(12356463.47, 1e-2);
expect(coords[1][1]).to.roughlyEqual(-5621521.48, 1e-2);
});
});
describe('#containsXY()', function () {
it('does contain XY', function () {
const multi = new MultiPoint([
[1, 2],
[10, 20],
]);
expect(multi.containsXY(1, 2)).to.be(true);
expect(multi.containsXY(10, 20)).to.be(true);
});
it('does not contain XY', function () {
const multi = new MultiPoint([
[1, 2],
[10, 20],
]);
expect(multi.containsXY(1, 3)).to.be(false);
expect(multi.containsXY(2, 2)).to.be(false);
expect(multi.containsXY(2, 3)).to.be(false);
expect(multi.containsXY(10, 30)).to.be(false);
expect(multi.containsXY(20, 20)).to.be(false);
expect(multi.containsXY(20, 30)).to.be(false);
});
});
});

View File

@@ -0,0 +1,460 @@
import MultiPolygon from '../../../../src/ol/geom/MultiPolygon.js';
import Polygon from '../../../../src/ol/geom/Polygon.js';
import expect from '../../expect.js';
describe('ol/geom/MultiPolygon.js', function () {
it('cannot be constructed with a null geometry', function () {
expect(function () {
return new MultiPolygon(null);
}).to.throwException();
});
describe('with a null MultiPolygon', function () {
it('can append polygons', function () {
const multiPolygon = new MultiPolygon([
new Polygon([
[
[0, 0],
[0, 2],
[1, 1],
[2, 0],
],
]),
]);
expect(multiPolygon.getCoordinates()).to.eql([
[
[
[0, 0],
[0, 2],
[1, 1],
[2, 0],
],
],
]);
multiPolygon.appendPolygon(
new Polygon([
[
[3, 0],
[4, 1],
[5, 2],
[5, 0],
],
])
);
expect(multiPolygon.getCoordinates()).to.eql([
[
[
[0, 0],
[0, 2],
[1, 1],
[2, 0],
],
],
[
[
[3, 0],
[4, 1],
[5, 2],
[5, 0],
],
],
]);
expect(multiPolygon.getPolygons().length).to.eql(2);
});
});
describe('with an empty MultiPolygon', function () {
let multiPolygon;
beforeEach(function () {
multiPolygon = new MultiPolygon([]);
});
it('can append polygons', function () {
multiPolygon.appendPolygon(
new Polygon([
[
[0, 0],
[0, 2],
[1, 1],
[2, 0],
],
])
);
expect(multiPolygon.getCoordinates()).to.eql([
[
[
[0, 0],
[0, 2],
[1, 1],
[2, 0],
],
],
]);
multiPolygon.appendPolygon(
new Polygon([
[
[3, 0],
[4, 1],
[5, 2],
[5, 0],
],
])
);
expect(multiPolygon.getCoordinates()).to.eql([
[
[
[0, 0],
[0, 2],
[1, 1],
[2, 0],
],
],
[
[
[3, 0],
[4, 1],
[5, 2],
[5, 0],
],
],
]);
expect(multiPolygon.getPolygons().length).to.eql(2);
});
});
describe('#scale()', function () {
it('scales a multi-polygon', function () {
const geom = new MultiPolygon([
[
[
[-1, -2],
[1, -2],
[1, 2],
[-1, 2],
[-1, -2],
],
],
]);
geom.scale(10);
const coordinates = geom.getCoordinates();
expect(coordinates).to.eql([
[
[
[-10, -20],
[10, -20],
[10, 20],
[-10, 20],
[-10, -20],
],
],
]);
});
it('accepts sx and sy', function () {
const geom = new MultiPolygon([
[
[
[-1, -2],
[1, -2],
[1, 2],
[-1, 2],
[-1, -2],
],
],
]);
geom.scale(2, 3);
const coordinates = geom.getCoordinates();
expect(coordinates).to.eql([
[
[
[-2, -6],
[2, -6],
[2, 6],
[-2, 6],
[-2, -6],
],
],
]);
});
it('accepts an anchor', function () {
const geom = new MultiPolygon([
[
[
[-1, -2],
[1, -2],
[1, 2],
[-1, 2],
[-1, -2],
],
],
]);
geom.scale(3, 2, [-1, -2]);
const coordinates = geom.getCoordinates();
expect(coordinates).to.eql([
[
[
[-1, -2],
[5, -2],
[5, 6],
[-1, 6],
[-1, -2],
],
],
]);
});
});
describe('with a simple MultiPolygon', function () {
let multiPolygon;
beforeEach(function () {
multiPolygon = new MultiPolygon([
[
[
[0, 0],
[0, 2],
[1, 1],
[2, 0],
],
],
[
[
[3, 0],
[4, 1],
[5, 2],
[5, 0],
],
],
]);
});
it('can return individual polygons', function () {
const polygon0 = multiPolygon.getPolygon(0);
expect(polygon0).to.be.an(Polygon);
expect(polygon0.getCoordinates()).to.eql([
[
[0, 0],
[0, 2],
[1, 1],
[2, 0],
],
]);
const polygon1 = multiPolygon.getPolygon(1);
expect(polygon1).to.be.an(Polygon);
expect(polygon1.getCoordinates()).to.eql([
[
[3, 0],
[4, 1],
[5, 2],
[5, 0],
],
]);
});
it('can return all polygons', function () {
const polygons = multiPolygon.getPolygons();
expect(polygons).to.be.an(Array);
expect(polygons).to.have.length(2);
expect(polygons[0]).to.be.an(Polygon);
expect(polygons[0].getCoordinates()).to.eql([
[
[0, 0],
[0, 2],
[1, 1],
[2, 0],
],
]);
expect(polygons[1]).to.be.an(Polygon);
expect(polygons[1].getCoordinates()).to.eql([
[
[3, 0],
[4, 1],
[5, 2],
[5, 0],
],
]);
});
describe('#clone()', function () {
it('has the expected endss_', function () {
multiPolygon.setProperties({foo: 'bar', baz: null});
const clone = multiPolygon.clone();
expect(multiPolygon.endss_).to.eql(clone.endss_);
expect(clone.getProperties()).to.eql({foo: 'bar', baz: null});
});
});
describe('#getCoordinates()', function () {
const cw = [
[-180, -90],
[-180, 90],
[180, 90],
[180, -90],
[-180, -90],
];
const cw2 = [
[-140, -60],
[-140, 60],
[140, 60],
[140, -60],
[-140, -60],
];
const ccw = [
[-180, -90],
[180, -90],
[180, 90],
[-180, 90],
[-180, -90],
];
const ccw2 = [
[-140, -60],
[140, -60],
[140, 60],
[-140, 60],
[-140, -60],
];
const right = new MultiPolygon([
[ccw, cw],
[ccw2, cw2],
]);
const left = new MultiPolygon([
[cw, ccw],
[cw2, ccw2],
]);
it('returns coordinates as they were constructed', function () {
expect(right.getCoordinates()).to.eql([
[ccw, cw],
[ccw2, cw2],
]);
expect(left.getCoordinates()).to.eql([
[cw, ccw],
[cw2, ccw2],
]);
});
it('can return coordinates with right-hand orientation', function () {
expect(right.getCoordinates(true)).to.eql([
[ccw, cw],
[ccw2, cw2],
]);
expect(left.getCoordinates(true)).to.eql([
[ccw, cw],
[ccw2, cw2],
]);
});
it('can return coordinates with left-hand orientation', function () {
expect(right.getCoordinates(false)).to.eql([
[cw, ccw],
[cw2, ccw2],
]);
expect(left.getCoordinates(false)).to.eql([
[cw, ccw],
[cw2, ccw2],
]);
});
});
describe('#getExtent()', function () {
it('returns expected result', function () {
expect(multiPolygon.getExtent()).to.eql([0, 0, 5, 2]);
});
});
describe('#getSimplifiedGeometry', function () {
it('returns the expected result', function () {
const simplifiedGeometry = multiPolygon.getSimplifiedGeometry(1);
expect(simplifiedGeometry).to.be.an(MultiPolygon);
expect(simplifiedGeometry.getCoordinates()).to.eql([
[
[
[0, 0],
[0, 2],
[2, 0],
],
],
[
[
[3, 0],
[5, 2],
[5, 0],
],
],
]);
});
});
describe('#intersectsExtent()', function () {
it('returns true for extent of of each polygon', function () {
const polygons = multiPolygon.getPolygons();
for (let i = 0; i < polygons.length; i++) {
expect(multiPolygon.intersectsExtent(polygons[i].getExtent())).to.be(
true
);
}
});
it('returns false for non-matching extent within own extent', function () {
expect(multiPolygon.intersectsExtent([2.1, 0, 2.9, 2])).to.be(false);
});
});
});
describe('#getArea', function () {
it('works with a clockwise and a counterclockwise Polygon', function () {
const multiPolygon = new MultiPolygon([
[
[
[1, 3],
[1, 2],
[0, 2],
[1, 3],
],
], // clockwise polygon with area 0.5
[
[
[2, 1],
[2, 0.5],
[3, 1],
[2, 1],
],
], // counterclockwise polygon with area 0.25
]);
expect(multiPolygon.getArea()).to.be(0.75);
});
});
describe('#getInteriorPoints', function () {
it('returns XYM multipoint with intersection width as M', function () {
const geom = new MultiPolygon([
[
[
[0, 0],
[0, 1],
[1, 1],
[1, 0],
[0, 0],
],
],
[
[
[1, 1],
[1, 2],
[2, 2],
[2, 1],
[1, 1],
],
],
]);
const interiorPoints = geom.getInteriorPoints();
expect(interiorPoints.getType()).to.be('MultiPoint');
expect(interiorPoints.layout).to.be('XYM');
expect(interiorPoints.getCoordinates()).to.eql([
[0.5, 0.5, 1],
[1.5, 1.5, 1],
]);
});
});
});

View File

@@ -0,0 +1,259 @@
import Point from '../../../../src/ol/geom/Point.js';
import expect from '../../expect.js';
import sinon from 'sinon';
import {
get as getProjection,
getTransformFromProjections,
} from '../../../../src/ol/proj.js';
describe('ol/geom/Point.js', function () {
it('cannot be constructed with a null geometry', function () {
expect(function () {
return new Point(null);
}).to.throwException();
});
describe('construct with 2D coordinates', function () {
let point;
beforeEach(function () {
point = new Point([1, 2]);
});
it('has the expected layout', function () {
expect(point.getLayout()).to.be('XY');
});
it('has the expected coordinates', function () {
expect(point.getCoordinates()).to.eql([1, 2]);
});
it('has the expected extent', function () {
expect(point.getExtent()).to.eql([1, 2, 1, 2]);
});
it('has the expected flat coordinates', function () {
expect(point.getFlatCoordinates()).to.eql([1, 2]);
});
it('has stride the expected stride', function () {
expect(point.getStride()).to.be(2);
});
it('does not intersect non matching extent', function () {
expect(point.intersectsExtent([0, 0, 10, 0.5])).to.be(false);
});
it("does intersect it's extent", function () {
expect(point.intersectsExtent(point.getExtent())).to.be(true);
});
});
describe('construct with 3D coordinates and layout XYM', function () {
let point;
beforeEach(function () {
point = new Point([1, 2, 3], 'XYM');
});
it('has the expected layout', function () {
expect(point.getLayout()).to.be('XYM');
});
it('has the expected coordinates', function () {
expect(point.getCoordinates()).to.eql([1, 2, 3]);
});
it('has the expected extent', function () {
expect(point.getExtent()).to.eql([1, 2, 1, 2]);
});
it('has the expected flat coordinates', function () {
expect(point.getFlatCoordinates()).to.eql([1, 2, 3]);
});
it('has the expected stride', function () {
expect(point.getStride()).to.be(3);
});
it('does not intersect non matching extent', function () {
expect(point.intersectsExtent([0, 0, 10, 0.5])).to.be(false);
});
it("does intersect it's extent", function () {
expect(point.intersectsExtent(point.getExtent())).to.be(true);
});
});
describe('construct with 4D coordinates', function () {
let point;
beforeEach(function () {
point = new Point([1, 2, 3, 4]);
});
it('has the expected layout', function () {
expect(point.getLayout()).to.be('XYZM');
});
it('has the expected coordinates', function () {
expect(point.getCoordinates()).to.eql([1, 2, 3, 4]);
});
it('has the expected extent', function () {
expect(point.getExtent()).to.eql([1, 2, 1, 2]);
});
it('has the expected flat coordinates', function () {
expect(point.getFlatCoordinates()).to.eql([1, 2, 3, 4]);
});
it('has the expected stride', function () {
expect(point.getStride()).to.be(4);
});
it('does not intersect non matching extent', function () {
expect(point.intersectsExtent([0, 0, 10, 0.5])).to.be(false);
});
it("does intersect it's extent", function () {
expect(point.intersectsExtent(point.getExtent())).to.be(true);
});
describe('#getClosestPoint', function () {
it('preseves extra dimensions', function () {
const closestPoint = point.getClosestPoint([0, 0]);
expect(closestPoint).to.eql([1, 2, 3, 4]);
});
});
});
describe('#scale()', function () {
it('scales a point', function () {
const geom = new Point([1, 2]);
geom.scale(10e6);
const coordinates = geom.getCoordinates();
expect(coordinates).to.eql([1, 2]);
});
it('accepts sx and sy', function () {
const geom = new Point([1, 2]);
geom.scale(1e6, -42);
const coordinates = geom.getCoordinates();
expect(coordinates).to.eql([1, 2]);
});
it('accepts an anchor', function () {
const geom = new Point([1, 2]);
geom.scale(10, 15, [0, 0]);
const coordinates = geom.getCoordinates();
expect(coordinates).to.eql([10, 30]);
});
});
describe('#simplifyTransformed()', function () {
it('returns the same result if called twice with the same arguments', function () {
const geom = new Point([1, 2]);
const source = getProjection('EPSG:4326');
const dest = getProjection('EPSG:3857');
const transform = getTransformFromProjections(source, dest);
const squaredTolerance = 0.5;
const first = geom.simplifyTransformed(squaredTolerance, transform);
const second = geom.simplifyTransformed(squaredTolerance, transform);
expect(second).to.be(first);
});
it('returns a different result if called with a different tolerance', function () {
const geom = new Point([1, 2]);
const source = getProjection('EPSG:4326');
const dest = getProjection('EPSG:3857');
const transform = getTransformFromProjections(source, dest);
const squaredTolerance = 0.5;
const first = geom.simplifyTransformed(squaredTolerance, transform);
const second = geom.simplifyTransformed(squaredTolerance * 2, transform);
expect(second).not.to.be(first);
});
it('returns a different result if called after geometry modification', function () {
const geom = new Point([1, 2]);
const source = getProjection('EPSG:4326');
const dest = getProjection('EPSG:3857');
const transform = getTransformFromProjections(source, dest);
const squaredTolerance = 0.5;
const first = geom.simplifyTransformed(squaredTolerance, transform);
geom.setCoordinates([3, 4]);
const second = geom.simplifyTransformed(squaredTolerance * 2, transform);
expect(second).not.to.be(first);
});
});
describe('#applyTransform()', function () {
let point, transform;
beforeEach(function () {
point = new Point([1, 2]);
transform = sinon.spy();
});
it('calls a transform function', function () {
point.applyTransform(transform);
expect(transform.calledOnce).to.be(true);
const args = transform.firstCall.args;
expect(args).to.have.length(3);
expect(args[0]).to.be(point.getFlatCoordinates()); // input coords
expect(args[1]).to.be(point.getFlatCoordinates()); // output coords
expect(args[2]).to.be(2); // dimension
});
it('allows for modification of coordinates', function () {
const mod = function (input, output, dimension) {
const copy = input.slice();
output[1] = copy[0];
output[0] = copy[1];
};
point.applyTransform(mod);
expect(point.getCoordinates()).to.eql([2, 1]);
});
it('returns undefined', function () {
const got = point.applyTransform(transform);
expect(got).to.be(undefined);
});
});
describe('#transform()', function () {
it('transforms a geometry given CRS identifiers', function () {
const point = new Point([-111, 45]).transform('EPSG:4326', 'EPSG:3857');
expect(point).to.be.a(Point);
const coords = point.getCoordinates();
expect(coords[0]).to.roughlyEqual(-12356463.47, 1e-2);
expect(coords[1]).to.roughlyEqual(5621521.48, 1e-2);
});
it('modifies the original', function () {
const point = new Point([-111, 45]);
point.transform('EPSG:4326', 'EPSG:3857');
const coords = point.getCoordinates();
expect(coords[0]).to.roughlyEqual(-12356463.47, 1e-2);
expect(coords[1]).to.roughlyEqual(5621521.48, 1e-2);
});
});
describe('#containsXY()', function () {
it('does contain XY', function () {
const point = new Point([1, 2]);
expect(point.containsXY(1, 2)).to.be(true);
});
it('does not contain XY', function () {
const point = new Point([1, 2]);
expect(point.containsXY(1, 3)).to.be(false);
expect(point.containsXY(2, 2)).to.be(false);
expect(point.containsXY(2, 3)).to.be(false);
});
});
});

View File

@@ -0,0 +1,775 @@
import Circle from '../../../../src/ol/geom/Circle.js';
import LinearRing from '../../../../src/ol/geom/LinearRing.js';
import Polygon, {
fromCircle,
fromExtent,
} from '../../../../src/ol/geom/Polygon.js';
import expect from '../../expect.js';
import {boundingExtent, isEmpty} from '../../../../src/ol/extent.js';
describe('ol/geom/Polygon.js', function () {
it('cannot be constructed with a null geometry', function () {
expect(function () {
return new Polygon(null);
}).to.throwException();
});
describe('construct empty', function () {
let polygon;
beforeEach(function () {
polygon = new Polygon([]);
});
it('defaults to layout XY', function () {
expect(polygon.getLayout()).to.be('XY');
});
it('has empty coordinates', function () {
expect(polygon.getCoordinates()).to.be.empty();
});
it('has an empty extent', function () {
expect(isEmpty(polygon.getExtent())).to.be(true);
});
it('has empty flat coordinates', function () {
expect(polygon.getFlatCoordinates()).to.be.empty();
});
it('has stride the expected stride', function () {
expect(polygon.getStride()).to.be(2);
});
it('can append linear rings', function () {
polygon.appendLinearRing(
new LinearRing([
[1, 2],
[3, 4],
[5, 6],
])
);
expect(polygon.getCoordinates()).to.eql([
[
[1, 2],
[3, 4],
[5, 6],
],
]);
polygon.appendLinearRing(
new LinearRing([
[7, 8],
[9, 10],
[11, 12],
])
);
expect(polygon.getCoordinates()).to.eql([
[
[1, 2],
[3, 4],
[5, 6],
],
[
[7, 8],
[9, 10],
[11, 12],
],
]);
});
});
describe('construct with 2D coordinates', function () {
let outerRing, innerRing, polygon, flatCoordinates;
let outsideOuter, inside, insideInner;
beforeEach(function () {
outerRing = [
[0, 1],
[1, 4],
[4, 3],
[3, 0],
];
innerRing = [
[2, 2],
[3, 2],
[3, 3],
[2, 3],
];
polygon = new Polygon([outerRing, innerRing]);
flatCoordinates = [0, 1, 1, 4, 4, 3, 3, 0, 2, 2, 3, 2, 3, 3, 2, 3];
outsideOuter = [0, 4];
inside = [1.5, 1.5];
insideInner = [2.5, 3.5];
});
it('has the expected layout', function () {
expect(polygon.getLayout()).to.be('XY');
});
it('has the expected coordinates', function () {
expect(polygon.getCoordinates()).to.eql([outerRing, innerRing]);
});
it('has the expected extent', function () {
expect(polygon.getExtent()).to.eql([0, 0, 4, 4]);
});
it('has the expected flat coordinates', function () {
expect(polygon.getFlatCoordinates()).to.eql(flatCoordinates);
});
it('has stride the expected stride', function () {
expect(polygon.getStride()).to.be(2);
});
it('can return individual rings', function () {
expect(polygon.getLinearRing(0).getCoordinates()).to.eql(outerRing);
expect(polygon.getLinearRing(1).getCoordinates()).to.eql(innerRing);
});
it('has the expected rings', function () {
const linearRings = polygon.getLinearRings();
expect(linearRings).to.be.an(Array);
expect(linearRings).to.have.length(2);
expect(linearRings[0]).to.be.an(LinearRing);
expect(linearRings[0].getCoordinates()).to.eql(outerRing);
expect(linearRings[1]).to.be.an(LinearRing);
expect(linearRings[1].getCoordinates()).to.eql(innerRing);
});
it('does not reverse any rings', function () {
outerRing.reverse();
innerRing.reverse();
polygon = new Polygon([outerRing, innerRing]);
const coordinates = polygon.getCoordinates();
expect(coordinates[0]).to.eql(outerRing);
expect(coordinates[1]).to.eql(innerRing);
});
it('does not contain outside coordinates', function () {
expect(polygon.intersectsCoordinate(outsideOuter)).to.be(false);
});
it('does contain inside coordinates', function () {
expect(polygon.intersectsCoordinate(inside)).to.be(true);
});
it('does not contain inside inner coordinates', function () {
expect(polygon.intersectsCoordinate(insideInner)).to.be(false);
});
describe('#getCoordinates()', function () {
const cw = [
[-180, -90],
[-180, 90],
[180, 90],
[180, -90],
[-180, -90],
];
const ccw = [
[-180, -90],
[180, -90],
[180, 90],
[-180, 90],
[-180, -90],
];
const right = new Polygon([ccw, cw]);
const left = new Polygon([cw, ccw]);
it('returns coordinates as they were constructed', function () {
expect(right.getCoordinates()).to.eql([ccw, cw]);
expect(left.getCoordinates()).to.eql([cw, ccw]);
});
it('can return coordinates with right-hand orientation', function () {
expect(right.getCoordinates(true)).to.eql([ccw, cw]);
expect(left.getCoordinates(true)).to.eql([ccw, cw]);
});
it('can return coordinates with left-hand orientation', function () {
expect(right.getCoordinates(false)).to.eql([cw, ccw]);
expect(left.getCoordinates(false)).to.eql([cw, ccw]);
});
});
describe('#getOrientedFlatCoordinates', function () {
it('reverses the outer ring if necessary', function () {
outerRing.reverse();
polygon = new Polygon([outerRing, innerRing]);
expect(polygon.getOrientedFlatCoordinates()).to.eql(flatCoordinates);
});
it('reverses inner rings if necessary', function () {
innerRing.reverse();
polygon = new Polygon([outerRing, innerRing]);
expect(polygon.getOrientedFlatCoordinates()).to.eql(flatCoordinates);
});
it('reverses all rings if necessary', function () {
outerRing.reverse();
innerRing.reverse();
polygon = new Polygon([outerRing, innerRing]);
expect(polygon.getOrientedFlatCoordinates()).to.eql(flatCoordinates);
});
});
});
describe('construct with 3D coordinates', function () {
let outerRing, innerRing, polygon, flatCoordinates;
let outsideOuter, inside, insideInner;
beforeEach(function () {
outerRing = [
[0, 0, 1],
[4, 4, 2],
[4, 0, 3],
];
innerRing = [
[2, 1, 4],
[3, 1, 5],
[3, 2, 6],
];
polygon = new Polygon([outerRing, innerRing]);
flatCoordinates = [0, 0, 1, 4, 4, 2, 4, 0, 3, 2, 1, 4, 3, 1, 5, 3, 2, 6];
outsideOuter = [1, 3];
inside = [3.5, 0.5];
insideInner = [2.9, 1.1];
});
it('has the expected layout', function () {
expect(polygon.getLayout()).to.be('XYZ');
});
it('has the expected coordinates', function () {
expect(polygon.getCoordinates()).to.eql([outerRing, innerRing]);
});
it('has the expected extent', function () {
expect(polygon.getExtent()).to.eql([0, 0, 4, 4]);
});
it('has the expected flat coordinates', function () {
expect(polygon.getFlatCoordinates()).to.eql(flatCoordinates);
});
it('has stride the expected stride', function () {
expect(polygon.getStride()).to.be(3);
});
it('does not contain outside coordinates', function () {
expect(polygon.intersectsCoordinate(outsideOuter)).to.be(false);
});
it('does contain inside coordinates', function () {
expect(polygon.intersectsCoordinate(inside)).to.be(true);
});
it('does not contain inside inner coordinates', function () {
expect(polygon.intersectsCoordinate(insideInner)).to.be(false);
});
describe('#intersectsExtent', function () {
it('does not intersect outside extent', function () {
expect(polygon.intersectsExtent(boundingExtent([outsideOuter]))).to.be(
false
);
});
it('does intersect inside extent', function () {
expect(polygon.intersectsExtent(boundingExtent([inside]))).to.be(true);
});
it('does intersect boundary extent', function () {
const firstMidX = (outerRing[0][0] + outerRing[1][0]) / 2;
const firstMidY = (outerRing[0][1] + outerRing[1][1]) / 2;
expect(
polygon.intersectsExtent(boundingExtent([[firstMidX, firstMidY]]))
).to.be(true);
});
it('does not intersect extent fully contained by inner ring', function () {
expect(polygon.intersectsExtent(boundingExtent([insideInner]))).to.be(
false
);
});
});
describe('#getOrientedFlatCoordinates', function () {
it('reverses the outer ring if necessary', function () {
outerRing.reverse();
polygon = new Polygon([outerRing, innerRing]);
expect(polygon.getOrientedFlatCoordinates()).to.eql(flatCoordinates);
});
it('reverses inner rings if necessary', function () {
innerRing.reverse();
polygon = new Polygon([outerRing, innerRing]);
expect(polygon.getOrientedFlatCoordinates()).to.eql(flatCoordinates);
});
it('reverses all rings if necessary', function () {
outerRing.reverse();
innerRing.reverse();
polygon = new Polygon([outerRing, innerRing]);
expect(polygon.getOrientedFlatCoordinates()).to.eql(flatCoordinates);
});
});
});
describe('construct with 3D coordinates and layout XYM', function () {
let outerRing, innerRing, polygon, flatCoordinates;
let outsideOuter, inside, insideInner;
beforeEach(function () {
outerRing = [
[0, 0, 1],
[4, 4, 2],
[4, 0, 3],
];
innerRing = [
[2, 1, 4],
[3, 1, 5],
[3, 2, 6],
];
polygon = new Polygon([outerRing, innerRing], 'XYM');
flatCoordinates = [0, 0, 1, 4, 4, 2, 4, 0, 3, 2, 1, 4, 3, 1, 5, 3, 2, 6];
outsideOuter = [1, 3];
inside = [3.5, 0.5];
insideInner = [2.9, 1.1];
});
it('has the expected layout', function () {
expect(polygon.getLayout()).to.be('XYM');
});
it('has the expected coordinates', function () {
expect(polygon.getCoordinates()).to.eql([outerRing, innerRing]);
});
it('has the expected extent', function () {
expect(polygon.getExtent()).to.eql([0, 0, 4, 4]);
});
it('has the expected flat coordinates', function () {
expect(polygon.getFlatCoordinates()).to.eql(flatCoordinates);
});
it('has stride the expected stride', function () {
expect(polygon.getStride()).to.be(3);
});
it('does not contain outside coordinates', function () {
expect(polygon.intersectsCoordinate(outsideOuter)).to.be(false);
});
it('does contain inside coordinates', function () {
expect(polygon.intersectsCoordinate(inside)).to.be(true);
});
it('does not contain inside inner coordinates', function () {
expect(polygon.intersectsCoordinate(insideInner)).to.be(false);
});
describe('#intersectsExtent', function () {
it('does not intersect outside extent', function () {
expect(polygon.intersectsExtent(boundingExtent([outsideOuter]))).to.be(
false
);
});
it('does intersect inside extent', function () {
expect(polygon.intersectsExtent(boundingExtent([inside]))).to.be(true);
});
it('does intersect boundary extent', function () {
const firstMidX = (outerRing[0][0] + outerRing[1][0]) / 2;
const firstMidY = (outerRing[0][1] + outerRing[1][1]) / 2;
expect(
polygon.intersectsExtent(boundingExtent([[firstMidX, firstMidY]]))
).to.be(true);
});
it('does not intersect extent fully contained by inner ring', function () {
expect(polygon.intersectsExtent(boundingExtent([insideInner]))).to.be(
false
);
});
});
describe('#getOrientedFlatCoordinates', function () {
it('reverses the outer ring if necessary', function () {
outerRing.reverse();
polygon = new Polygon([outerRing, innerRing]);
expect(polygon.getOrientedFlatCoordinates()).to.eql(flatCoordinates);
});
it('reverses inner rings if necessary', function () {
innerRing.reverse();
polygon = new Polygon([outerRing, innerRing]);
expect(polygon.getOrientedFlatCoordinates()).to.eql(flatCoordinates);
});
it('reverses all rings if necessary', function () {
outerRing.reverse();
innerRing.reverse();
polygon = new Polygon([outerRing, innerRing]);
expect(polygon.getOrientedFlatCoordinates()).to.eql(flatCoordinates);
});
});
});
describe('construct with 4D coordinates', function () {
let outerRing, innerRing1, innerRing2, polygon, flatCoordinates;
let outsideOuter, inside, insideInner1, insideInner2;
beforeEach(function () {
outerRing = [
[0, 6, 1, 2],
[6, 6, 3, 4],
[3, 0, 5, 6],
];
innerRing1 = [
[2, 4, 7, 8],
[4, 4, 9, 10],
[4, 5, 11, 12],
[2, 5, 13, 14],
];
innerRing2 = [
[3, 2, 15, 16],
[4, 3, 17, 18],
[2, 3, 19, 20],
];
polygon = new Polygon([outerRing, innerRing1, innerRing2]);
flatCoordinates = [
0,
6,
1,
2,
6,
6,
3,
4,
3,
0,
5,
6,
2,
4,
7,
8,
4,
4,
9,
10,
4,
5,
11,
12,
2,
5,
13,
14,
3,
2,
15,
16,
4,
3,
17,
18,
2,
3,
19,
20,
];
outsideOuter = [1, 1];
inside = [3, 1];
insideInner1 = [2.5, 4.5];
insideInner2 = [3, 2.5];
});
it('has the expected layout', function () {
expect(polygon.getLayout()).to.be('XYZM');
});
it('has the expected coordinates', function () {
expect(polygon.getCoordinates()).to.eql([
outerRing,
innerRing1,
innerRing2,
]);
});
it('has the expected extent', function () {
expect(polygon.getExtent()).to.eql([0, 0, 6, 6]);
});
it('has the expected flat coordinates', function () {
expect(polygon.getFlatCoordinates()).to.eql(flatCoordinates);
});
it('has stride the expected stride', function () {
expect(polygon.getStride()).to.be(4);
});
it('does not contain outside coordinates', function () {
expect(polygon.intersectsCoordinate(outsideOuter)).to.be(false);
});
it('does contain inside coordinates', function () {
expect(polygon.intersectsCoordinate(inside)).to.be(true);
});
it('does not contain inside inner coordinates', function () {
expect(polygon.intersectsCoordinate(insideInner1)).to.be(false);
expect(polygon.intersectsCoordinate(insideInner2)).to.be(false);
});
describe('#intersectsExtent', function () {
it('does not intersect outside extent', function () {
expect(polygon.intersectsExtent(boundingExtent([outsideOuter]))).to.be(
false
);
});
it('does intersect inside extent', function () {
expect(polygon.intersectsExtent(boundingExtent([inside]))).to.be(true);
});
it('does intersect boundary extent', function () {
const firstMidX = (outerRing[0][0] + outerRing[1][0]) / 2;
const firstMidY = (outerRing[0][1] + outerRing[1][1]) / 2;
expect(
polygon.intersectsExtent(boundingExtent([[firstMidX, firstMidY]]))
).to.be(true);
});
it('does not intersect extent fully contained by inner ring', function () {
expect(polygon.intersectsExtent(boundingExtent([insideInner1]))).to.be(
false
);
expect(polygon.intersectsExtent(boundingExtent([insideInner2]))).to.be(
false
);
});
});
describe('#getOrientedFlatCoordinates', function () {
it('reverses the outer ring if necessary', function () {
outerRing.reverse();
polygon = new Polygon([outerRing, innerRing1, innerRing2]);
expect(polygon.getOrientedFlatCoordinates()).to.eql(flatCoordinates);
});
it('reverses inner rings if necessary', function () {
innerRing1.reverse();
innerRing2.reverse();
polygon = new Polygon([outerRing, innerRing1, innerRing2]);
expect(polygon.getOrientedFlatCoordinates()).to.eql(flatCoordinates);
});
it('reverses all rings if necessary', function () {
outerRing.reverse();
innerRing1.reverse();
innerRing2.reverse();
polygon = new Polygon([outerRing, innerRing1, innerRing2]);
expect(polygon.getOrientedFlatCoordinates()).to.eql(flatCoordinates);
});
});
});
describe('with a simple polygon', function () {
let polygon;
beforeEach(function () {
polygon = new Polygon([
[
[3, 0],
[1, 3],
[0, 6],
[2, 6],
[3, 7],
[4, 6],
[6, 6],
[4, 3],
],
]);
});
describe('#getSimplifiedGeometry', function () {
it('returns the expected result', function () {
const simplifiedGeometry = polygon.getSimplifiedGeometry(9);
expect(simplifiedGeometry).to.be.an(Polygon);
expect(simplifiedGeometry.getCoordinates()).to.eql([
[
[3, 0],
[0, 3],
[0, 6],
[6, 6],
[3, 3],
],
]);
});
});
});
describe('#scale()', function () {
it('scales a polygon', function () {
const geom = new Polygon([
[
[-1, -2],
[1, -2],
[1, 2],
[-1, 2],
[-1, -2],
],
]);
geom.scale(10);
const coordinates = geom.getCoordinates();
expect(coordinates).to.eql([
[
[-10, -20],
[10, -20],
[10, 20],
[-10, 20],
[-10, -20],
],
]);
});
it('accepts sx and sy', function () {
const geom = new Polygon([
[
[-1, -2],
[1, -2],
[1, 2],
[-1, 2],
[-1, -2],
],
]);
geom.scale(2, 3);
const coordinates = geom.getCoordinates();
expect(coordinates).to.eql([
[
[-2, -6],
[2, -6],
[2, 6],
[-2, 6],
[-2, -6],
],
]);
});
it('accepts an anchor', function () {
const geom = new Polygon([
[
[-1, -2],
[1, -2],
[1, 2],
[-1, 2],
[-1, -2],
],
]);
geom.scale(3, 2, [-1, -2]);
const coordinates = geom.getCoordinates();
expect(coordinates).to.eql([
[
[-1, -2],
[5, -2],
[5, 6],
[-1, 6],
[-1, -2],
],
]);
});
});
describe('#getInteriorPoint', function () {
it('returns XYM point with intersection width as M', function () {
const geom = new Polygon([
[
[0, 0],
[0, 1],
[1, 1],
[1, 0],
[0, 0],
],
]);
const interiorPoint = geom.getInteriorPoint();
expect(interiorPoint.getType()).to.be('Point');
expect(interiorPoint.layout).to.be('XYM');
expect(interiorPoint.getCoordinates()).to.eql([0.5, 0.5, 1]);
});
it('returns XYM point for donut polygons', function () {
const geom = new Polygon([
[
[0.5, 0.5],
[0.5, 2.5],
[2.5, 2.5],
[2.5, 0.5],
[0.5, 0.5],
],
[
[1, 1],
[2, 1],
[2, 2],
[1, 2],
[1, 1],
],
]);
const interiorPoint = geom.getInteriorPoint();
expect(interiorPoint.getType()).to.be('Point');
expect(interiorPoint.layout).to.be('XYM');
expect(interiorPoint.getCoordinates()).to.eql([0.75, 1.5, 0.5]);
});
});
describe('fromExtent()', function () {
it('creates the correct polygon', function () {
const extent = [1, 2, 3, 5];
const polygon = fromExtent(extent);
const flatCoordinates = polygon.getFlatCoordinates();
expect(flatCoordinates).to.eql([1, 2, 1, 5, 3, 5, 3, 2, 1, 2]);
const orientedFlatCoordinates = polygon.getOrientedFlatCoordinates();
expect(orientedFlatCoordinates).to.eql([1, 2, 1, 5, 3, 5, 3, 2, 1, 2]);
});
});
describe('fromCircle()', function () {
it('creates a regular polygon', function () {
const circle = new Circle([0, 0, 0], 1, 'XYZ');
const polygon = fromCircle(circle);
const coordinates = polygon.getLinearRing(0).getCoordinates();
expect(coordinates[0].length).to.eql(3);
expect(coordinates[0][2]).to.eql(0);
expect(coordinates[32]).to.eql(coordinates[0]);
// east
expect(coordinates[0][0]).to.roughlyEqual(1, 1e-9);
expect(coordinates[0][1]).to.roughlyEqual(0, 1e-9);
// south
expect(coordinates[8][0]).to.roughlyEqual(0, 1e-9);
expect(coordinates[8][1]).to.roughlyEqual(1, 1e-9);
// west
expect(coordinates[16][0]).to.roughlyEqual(-1, 1e-9);
expect(coordinates[16][1]).to.roughlyEqual(0, 1e-9);
// north
expect(coordinates[24][0]).to.roughlyEqual(0, 1e-9);
expect(coordinates[24][1]).to.roughlyEqual(-1, 1e-9);
});
it('creates a regular polygon with custom sides and angle', function () {
const circle = new Circle([0, 0], 1);
const polygon = fromCircle(circle, 4, Math.PI / 2);
const coordinates = polygon.getLinearRing(0).getCoordinates();
expect(coordinates[4]).to.eql(coordinates[0]);
expect(coordinates[0][0]).to.roughlyEqual(0, 1e-9);
expect(coordinates[0][1]).to.roughlyEqual(1, 1e-9);
});
it('creates a regular polygon, maintaining ZM values', () => {
const circle = new Circle([0, 0, 1, 1], 1, 'XYZM');
const polygon = fromCircle(circle);
const coordinates = polygon.getLinearRing(0).getCoordinates();
expect(coordinates[0][2]).to.eql(1);
expect(coordinates[0][3]).to.eql(1);
});
});
});

View File

@@ -0,0 +1,28 @@
import expect from '../../../expect.js';
import {linearRing, linearRings} from '../../../../../src/ol/geom/flat/area.js';
describe('ol/geom/flat/area.js', function () {
describe('linearRing', function () {
it('calculates the area of a triangle', function () {
const area = linearRing([0, 0, 0.5, 1, 1, 0], 0, 6, 2);
expect(area).to.be(0.5);
});
it('calculates the area of a unit square', function () {
const area = linearRing([0, 0, 0, 1, 1, 1, 1, 0], 0, 8, 2);
expect(area).to.be(1);
});
});
describe('linearRings', function () {
it('calculates the area with holes', function () {
const area = linearRings(
[0, 0, 0, 3, 3, 3, 3, 0, 1, 1, 2, 1, 2, 2, 1, 2],
0,
[8, 16],
2
);
expect(area).to.be(8);
});
});
});

View File

@@ -0,0 +1,86 @@
import MultiPolygon from '../../../../../src/ol/geom/MultiPolygon.js';
import expect from '../../../expect.js';
import {linearRingss as linearRingssCenter} from '../../../../../src/ol/geom/flat/center.js';
describe('ol/geom/flat/center.js', function () {
describe('linearRingss', function () {
it('calculates the center of a square', function () {
const squareMultiPoly = new MultiPolygon([
[
[
[0, 0],
[0, 1],
[1, 1],
[1, 0],
[0, 0],
],
],
]);
const got = linearRingssCenter(
squareMultiPoly.flatCoordinates,
0,
squareMultiPoly.endss_,
2
);
expect(got).to.eql([0.5, 0.5]);
});
it('calculates the centers of two squares', function () {
const squareMultiPoly = new MultiPolygon([
[
[
[0, 0],
[0, 1],
[1, 1],
[1, 0],
[0, 0],
],
],
[
[
[3, 0],
[3, 1],
[4, 1],
[4, 0],
[3, 0],
],
],
]);
const got = linearRingssCenter(
squareMultiPoly.flatCoordinates,
0,
squareMultiPoly.endss_,
2
);
expect(got).to.eql([0.5, 0.5, 3.5, 0.5]);
});
it('does not care about holes', function () {
const polywithHole = new MultiPolygon([
[
[
[0, 0],
[0, 5],
[5, 5],
[5, 0],
[0, 0],
],
[
[1, 1],
[1, 4],
[4, 4],
[4, 1],
[1, 1],
],
],
]);
const got = linearRingssCenter(
polywithHole.flatCoordinates,
0,
polywithHole.endss_,
2
);
expect(got).to.eql([2.5, 2.5]);
});
});
});

View File

@@ -0,0 +1,391 @@
import expect from '../../../expect.js';
import {
assignClosestPoint,
maxSquaredDelta,
} from '../../../../../src/ol/geom/flat/closest.js';
describe('ol/geom/flat/closest.js', function () {
describe('with simple data', function () {
const flatCoordinates = [0, 0, 1, 0, 3, 0, 5, 0, 6, 0, 8, 0, 11, 0];
describe('maxSquaredDelta', function () {
it('returns the expected value in simple cases', function () {
expect(
maxSquaredDelta(flatCoordinates, 0, flatCoordinates.length, 2, 0)
).to.be(9);
});
});
describe('assignClosestPoint', function () {
it('returns the expected value', function () {
const maxDelta = Math.sqrt(
maxSquaredDelta(flatCoordinates, 0, flatCoordinates.length, 2, 0)
);
expect(maxDelta).to.be(3);
const closestPoint = [NaN, NaN];
expect(
assignClosestPoint(
flatCoordinates,
0,
flatCoordinates.length,
2,
maxDelta,
false,
0,
0,
closestPoint,
Infinity
)
).to.be(0);
expect(closestPoint).to.eql([0, 0]);
expect(
assignClosestPoint(
flatCoordinates,
0,
flatCoordinates.length,
2,
maxDelta,
false,
4,
1,
closestPoint,
Infinity
)
).to.be(1);
expect(closestPoint).to.eql([4, 0]);
expect(
assignClosestPoint(
flatCoordinates,
0,
flatCoordinates.length,
2,
maxDelta,
false,
5,
2,
closestPoint,
Infinity
)
).to.be(4);
expect(closestPoint).to.eql([5, 0]);
expect(
assignClosestPoint(
flatCoordinates,
0,
flatCoordinates.length,
2,
maxDelta,
false,
10,
100,
closestPoint,
Infinity
)
).to.be(10000);
expect(closestPoint).to.eql([10, 0]);
});
});
});
describe('with real data', function () {
const flatCoordinates = [
224.55,
250.15,
226.91,
244.19,
233.31,
241.45,
234.98,
236.06,
244.21,
232.76,
262.59,
215.31,
267.76,
213.81,
273.57,
201.84,
273.12,
192.16,
277.62,
189.03,
280.36,
181.41,
286.51,
177.74,
292.41,
159.37,
296.91,
155.64,
314.95,
151.37,
319.75,
145.16,
330.33,
137.57,
341.48,
139.96,
369.98,
137.89,
387.39,
142.51,
391.28,
139.39,
409.52,
141.14,
414.82,
139.75,
427.72,
127.3,
439.6,
119.74,
474.93,
107.87,
486.51,
106.75,
489.2,
109.45,
493.79,
108.63,
504.74,
119.66,
512.96,
122.35,
518.63,
120.89,
524.09,
126.88,
529.57,
127.86,
534.21,
140.93,
539.27,
147.24,
567.69,
148.91,
575.25,
157.26,
580.62,
158.15,
601.53,
156.85,
617.74,
159.86,
622.0,
167.04,
629.55,
194.6,
638.9,
195.61,
641.26,
200.81,
651.77,
204.56,
671.55,
222.55,
683.68,
217.45,
695.25,
219.15,
700.64,
217.98,
703.12,
214.36,
712.26,
215.87,
721.49,
212.81,
727.81,
213.36,
729.98,
208.73,
735.32,
208.2,
739.94,
204.77,
769.98,
208.42,
779.6,
216.87,
784.2,
218.16,
800.24,
214.62,
810.53,
219.73,
817.19,
226.82,
820.77,
236.17,
827.23,
236.16,
829.89,
239.89,
851.0,
248.94,
859.88,
255.49,
865.21,
268.53,
857.95,
280.3,
865.48,
291.45,
866.81,
298.66,
864.68,
302.71,
867.79,
306.17,
859.87,
311.37,
860.08,
314.35,
858.29,
314.94,
858.1,
327.6,
854.54,
335.4,
860.92,
343.0,
856.43,
350.15,
851.42,
352.96,
849.84,
359.59,
854.56,
365.53,
849.74,
370.38,
844.09,
371.89,
844.75,
380.44,
841.52,
383.67,
839.57,
390.4,
845.59,
399.05,
848.4,
407.55,
843.71,
411.3,
844.09,
419.88,
839.51,
432.76,
841.33,
441.04,
847.62,
449.22,
847.16,
458.44,
851.38,
462.79,
853.97,
471.15,
866.36,
480.77,
];
describe('maxSquaredDelta', function () {
it('returns the expected value', function () {
expect(
maxSquaredDelta(flatCoordinates, 0, flatCoordinates.length, 2, 0)
).to.roughlyEqual(1389.1058, 1e-9);
});
});
describe('assignClosestPoint', function () {
it('returns the expected value', function () {
const maxDelta = Math.sqrt(
maxSquaredDelta(flatCoordinates, 0, flatCoordinates.length, 2, 0)
);
expect(maxDelta).to.roughlyEqual(Math.sqrt(1389.1058), 1e-9);
const closestPoint = [NaN, NaN];
expect(
assignClosestPoint(
flatCoordinates,
0,
flatCoordinates.length,
2,
maxDelta,
false,
0,
0,
closestPoint,
Infinity
)
).to.roughlyEqual(110902.405, 1e-9);
expect(closestPoint).to.eql([292.41, 159.37]);
expect(
assignClosestPoint(
flatCoordinates,
0,
flatCoordinates.length,
2,
maxDelta,
false,
500,
500,
closestPoint,
Infinity
)
).to.roughlyEqual(106407.905, 1e-9);
expect(closestPoint).to.eql([671.55, 222.55]);
expect(
assignClosestPoint(
flatCoordinates,
0,
flatCoordinates.length,
2,
maxDelta,
false,
1000,
500,
closestPoint,
Infinity
)
).to.roughlyEqual(18229.4425, 1e-9);
expect(closestPoint).to.eql([866.36, 480.77]);
});
});
});
describe('with multi-dimensional data', function () {
const flatCoordinates = [0, 0, 10, -10, 2, 2, 30, -20];
const stride = 4;
describe('assignClosestPoint', function () {
it('interpolates M coordinates', function () {
const maxDelta = Math.sqrt(
maxSquaredDelta(flatCoordinates, 0, flatCoordinates.length, stride, 0)
);
expect(maxDelta).to.roughlyEqual(Math.sqrt(8), 1e-9);
const closestPoint = [NaN, NaN];
expect(
assignClosestPoint(
flatCoordinates,
0,
flatCoordinates.length,
stride,
maxDelta,
false,
1,
1,
closestPoint,
Infinity
)
).to.roughlyEqual(0, 1e-9);
expect(closestPoint).to.have.length(stride);
expect(closestPoint[0]).to.be(1);
expect(closestPoint[1]).to.be(1);
expect(closestPoint[2]).to.be(20);
expect(closestPoint[3]).to.be(-15);
});
});
});
});

View File

@@ -0,0 +1,97 @@
import expect from '../../../expect.js';
import {linearRingContainsXY} from '../../../../../src/ol/geom/flat/contains.js';
describe('ol/geom/flat/contains.js', function () {
describe('with simple data', function () {
const flatCoordinatesSimple = [0, 0, 1, 0, 1, 1, 0, 1];
const flatCoordinatesNonSimple = [
0,
0,
4,
0,
4,
3,
1,
3,
1,
2,
3,
2,
3,
1,
2,
1,
2,
4,
0,
4,
];
describe('linearRingContainsXY', function () {
it('returns true for point inside a simple polygon', function () {
expect(
linearRingContainsXY(
flatCoordinatesSimple,
0,
flatCoordinatesSimple.length,
2,
0.5,
0.5
)
).to.be(true);
});
it('returns false for point outside a simple polygon', function () {
expect(
linearRingContainsXY(
flatCoordinatesSimple,
0,
flatCoordinatesSimple.length,
2,
1.5,
1.5
)
).to.be(false);
});
it('returns true for point inside a non-simple polygon', function () {
expect(
linearRingContainsXY(
flatCoordinatesNonSimple,
0,
flatCoordinatesNonSimple.length,
2,
1,
1
)
).to.be(true);
});
it('returns true for point inside an overlap of a non-simple polygon', function () {
expect(
linearRingContainsXY(
flatCoordinatesNonSimple,
0,
flatCoordinatesNonSimple.length,
2,
1.5,
2.5
)
).to.be(true);
});
it('returns false for a point inside a hole of a non-simple polygon', function () {
expect(
linearRingContainsXY(
flatCoordinatesNonSimple,
0,
flatCoordinatesNonSimple.length,
2,
2.5,
1.5
)
).to.be(false);
});
});
});
});

View File

@@ -0,0 +1,55 @@
import expect from '../../../expect.js';
import {
deflateCoordinates,
deflateCoordinatesArray,
} from '../../../../../src/ol/geom/flat/deflate.js';
describe('ol/geom/flat/deflate.js', function () {
describe('deflateCoordinates', function () {
let flatCoordinates;
beforeEach(function () {
flatCoordinates = [];
});
it('flattens coordinates', function () {
const offset = deflateCoordinates(
flatCoordinates,
0,
[
[1, 2],
[3, 4],
],
2
);
expect(offset).to.be(4);
expect(flatCoordinates).to.eql([1, 2, 3, 4]);
});
});
describe('deflateCoordinatesArray', function () {
let flatCoordinates;
beforeEach(function () {
flatCoordinates = [];
});
it('flattens arrays of coordinates', function () {
const ends = deflateCoordinatesArray(
flatCoordinates,
0,
[
[
[1, 2],
[3, 4],
],
[
[5, 6],
[7, 8],
],
],
2
);
expect(ends).to.eql([4, 8]);
expect(flatCoordinates).to.eql([1, 2, 3, 4, 5, 6, 7, 8]);
});
});
});

View File

@@ -0,0 +1,32 @@
import expect from '../../../expect.js';
import {flipXY} from '../../../../../src/ol/geom/flat/flip.js';
describe('ol/geom/flat/flip.js', function () {
describe('flipXY', function () {
it('can flip XY coordinates', function () {
const flatCoordinates = flipXY([1, 2, 3, 4], 0, 4, 2);
expect(flatCoordinates).to.eql([2, 1, 4, 3]);
});
it('can flip XY coordinates while preserving other dimensions', function () {
const flatCoordinates = flipXY([1, 2, 3, 4, 5, 6, 7, 8], 0, 8, 4);
expect(flatCoordinates).to.eql([2, 1, 3, 4, 6, 5, 7, 8]);
});
it('can flip XY coordinates in place', function () {
const flatCoordinates = [1, 2, 3, 4];
expect(flipXY(flatCoordinates, 0, 4, 2, flatCoordinates)).to.be(
flatCoordinates
);
expect(flatCoordinates).to.eql([2, 1, 4, 3]);
});
it('can flip XY coordinates in place while preserving other dimensions', function () {
const flatCoordinates = [1, 2, 3, 4, 5, 6, 7, 8, 9];
expect(flipXY(flatCoordinates, 0, 9, 3, flatCoordinates)).to.be(
flatCoordinates
);
expect(flatCoordinates).to.eql([2, 1, 3, 5, 4, 6, 8, 7, 9]);
});
});
});

View File

@@ -0,0 +1,38 @@
import expect from '../../../expect.js';
import {
inflateCoordinates,
inflateCoordinatesArray,
} from '../../../../../src/ol/geom/flat/inflate.js';
describe('ol/geom/flat/inflate.js', function () {
describe('inflateCoordinates', function () {
it('inflates coordinates', function () {
const coordinates = inflateCoordinates([1, 2, 3, 4], 0, 4, 2);
expect(coordinates).to.eql([
[1, 2],
[3, 4],
]);
});
});
describe('inflateCoordinatesArray', function () {
it('inflates arrays of coordinates', function () {
const coordinatess = inflateCoordinatesArray(
[1, 2, 3, 4, 5, 6, 7, 8],
0,
[4, 8],
2
);
expect(coordinatess).to.eql([
[
[1, 2],
[3, 4],
],
[
[5, 6],
[7, 8],
],
]);
});
});
});

View File

@@ -0,0 +1,68 @@
import expect from '../../../expect.js';
import {interpolatePoint} from '../../../../../src/ol/geom/flat/interpolate.js';
describe('ol/geom/flat/interpolate.js', function () {
describe('interpolatePoint', function () {
it('returns the expected value for single points', function () {
const flatCoordinates = [0, 1];
const point = interpolatePoint(flatCoordinates, 0, 2, 2, 0.5);
expect(point).to.eql([0, 1]);
});
it('returns the expected value for simple line segments', function () {
const flatCoordinates = [0, 1, 2, 3];
const point = interpolatePoint(flatCoordinates, 0, 4, 2, 0.5);
expect(point).to.eql([1, 2]);
});
it(
'returns the expected value when the mid point is an existing ' +
'coordinate',
function () {
const flatCoordinates = [0, 1, 2, 3, 4, 5];
const point = interpolatePoint(flatCoordinates, 0, 6, 2, 0.5);
expect(point).to.eql([2, 3]);
}
);
it('also when vertices are repeated', function () {
const flatCoordinates = [0, 1, 2, 3, 2, 3, 4, 5];
const point = interpolatePoint(flatCoordinates, 0, 8, 2, 0.5);
expect(point).to.eql([2, 3]);
});
it(
'returns the expected value when the midpoint falls halfway between ' +
'two existing coordinates',
function () {
const flatCoordinates = [0, 1, 2, 3, 4, 5, 6, 7];
const point = interpolatePoint(flatCoordinates, 0, 8, 2, 0.5);
expect(point).to.eql([3, 4]);
}
);
it('also when vertices are repeated', function () {
const flatCoordinates = [0, 1, 2, 3, 2, 3, 4, 5, 6, 7];
const point = interpolatePoint(flatCoordinates, 0, 10, 2, 0.5);
expect(point).to.eql([3, 4]);
});
it('returns the expected value when the coordinates are not evenly spaced', function () {
const flatCoordinates = [0, 1, 2, 3, 6, 7];
const point = interpolatePoint(flatCoordinates, 0, 6, 2, 0.5);
expect(point).to.eql([3, 4]);
});
it('also when vertices are repeated', function () {
const flatCoordinates = [0, 1, 2, 3, 2, 3, 6, 7];
const point = interpolatePoint(flatCoordinates, 0, 8, 2, 0.5);
expect(point).to.eql([3, 4]);
});
it('returns the expected value when using opt_dest', function () {
const flatCoordinates = [0, 1, 2, 3, 6, 7];
const point = interpolatePoint(flatCoordinates, 0, 6, 2, 0.5, [0, 0]);
expect(point).to.eql([3, 4]);
});
});
});

View File

@@ -0,0 +1,203 @@
import expect from '../../../expect.js';
import {
intersectsLineString,
intersectsLinearRing,
intersectsLinearRingArray,
} from '../../../../../src/ol/geom/flat/intersectsextent.js';
describe('ol/geom/flat/intersectsextent.js', function () {
describe('intersectsLineString', function () {
let flatCoordinates;
beforeEach(function () {
flatCoordinates = [0, 0, 1, 1, 2, 2];
});
describe('linestring envelope does not intersect the extent', function () {
it('returns false', function () {
const extent = [3, 3, 4, 4];
const r = intersectsLineString(
flatCoordinates,
0,
flatCoordinates.length,
2,
extent
);
expect(r).to.be(false);
});
});
describe('linestring envelope within the extent', function () {
it('returns true', function () {
const extent = [-1, -1, 3, 3];
const r = intersectsLineString(
flatCoordinates,
0,
flatCoordinates.length,
2,
extent
);
expect(r).to.be(true);
});
});
describe('linestring envelope bisected by an edge of the extent', function () {
it('returns true', function () {
const extent = [-0.1, 0.1, 2.1, 0.1];
const r = intersectsLineString(
flatCoordinates,
0,
flatCoordinates.length,
2,
extent
);
expect(r).to.be(true);
});
});
describe('a segment intersects the extent', function () {
it('returns true', function () {
const extent = [-0.5, -0.5, 0.5, 0.5];
const r = intersectsLineString(
flatCoordinates,
0,
flatCoordinates.length,
2,
extent
);
expect(r).to.be(true);
});
});
describe('no segments intersect the extent', function () {
it('returns false', function () {
const extent = [0.5, 1.5, 1, 1.75];
const r = intersectsLineString(
flatCoordinates,
0,
flatCoordinates.length,
2,
extent
);
expect(r).to.be(false);
});
it('returns false', function () {
const extent = [1, 0.25, 1.5, 0.5];
const r = intersectsLineString(
flatCoordinates,
0,
flatCoordinates.length,
2,
extent
);
expect(r).to.be(false);
});
});
});
describe('intersectsLinearRing', function () {
let flatCoordinates;
beforeEach(function () {
flatCoordinates = [0, 0, 1, 1, 2, 0, 1, -1, 0, 0];
});
describe('boundary intersects the extent', function () {
it('returns true', function () {
const extent = [1.5, 0.0, 2.5, 1.0];
const r = intersectsLinearRing(
flatCoordinates,
0,
flatCoordinates.length,
2,
extent
);
expect(r).to.be(true);
});
});
describe(
'boundary does not intersect the extent and ring does not ' +
'contain a corner of the extent',
function () {
it('returns false', function () {
const extent = [2.0, 0.5, 3, 1.5];
const r = intersectsLinearRing(
flatCoordinates,
0,
flatCoordinates.length,
2,
extent
);
expect(r).to.be(false);
});
}
);
describe('ring contains the extent', function () {
it('returns true', function () {
const extent = [0.75, -0.25, 1.25, 0.25];
const r = intersectsLinearRing(
flatCoordinates,
0,
flatCoordinates.length,
2,
extent
);
expect(r).to.be(true);
});
});
});
describe('intersectsLinearRingArray', function () {
let flatCoordinates;
let ends;
beforeEach(function () {
flatCoordinates = [
0,
0,
0,
10,
10,
10,
10,
0,
0,
0,
/*hole*/ 2,
2,
8,
2,
8,
4,
5,
5,
8,
6,
8,
8,
2,
8,
2,
2,
];
ends = [10, flatCoordinates.length];
});
describe('ring with hole where hole contains the extent', function () {
it('returns true', function () {
const extent = [3, 3, 3.5, 3.5];
const r = intersectsLinearRingArray(
flatCoordinates,
0,
ends,
2,
extent
);
expect(r).to.be(false);
});
});
describe('ring with hole intersects the extent', function () {
it('returns true', function () {
const extent = [3, 3, 6, 6];
const r = intersectsLinearRingArray(
flatCoordinates,
0,
ends,
2,
extent
);
expect(r).to.be(true);
});
});
});
});

View File

@@ -0,0 +1,89 @@
import expect from '../../../expect.js';
import {
lineStringLength,
linearRingLength,
} from '../../../../../src/ol/geom/flat/length.js';
describe('ol/geom/flat/length.js', function () {
describe('lineStringLength', function () {
describe('stride = 2', function () {
const flatCoords = [0, 0, 1, 0, 1, 1, 0, 1];
const stride = 2;
it('calculates the total length of a lineString', function () {
const offset = 0;
const end = 8;
const expected = 3;
const got = lineStringLength(flatCoords, offset, end, stride);
expect(got).to.be(expected);
});
it('calculates a partwise length of a lineString (offset)', function () {
const offset = 2;
const end = 8;
const expected = 2;
const got = lineStringLength(flatCoords, offset, end, stride);
expect(got).to.be(expected);
});
it('calculates a partwise length of a lineString (end)', function () {
const offset = 0;
const end = 4;
const expected = 1;
const got = lineStringLength(flatCoords, offset, end, stride);
expect(got).to.be(expected);
});
});
describe('stride = 3', function () {
const flatCoords = [0, 0, 42, 1, 0, 42, 1, 1, 42, 0, 1, 42];
const stride = 3;
it('calculates the total length of a lineString', function () {
const offset = 0;
const end = 12;
const expected = 3;
const got = lineStringLength(flatCoords, offset, end, stride);
expect(got).to.be(expected);
});
it('calculates a partwise length of a lineString (offset)', function () {
const offset = 3;
const end = 12;
const expected = 2;
const got = lineStringLength(flatCoords, offset, end, stride);
expect(got).to.be(expected);
});
it('calculates a partwise length of a lineString (end)', function () {
const offset = 0;
const end = 6;
const expected = 1;
const got = lineStringLength(flatCoords, offset, end, stride);
expect(got).to.be(expected);
});
});
});
describe('linearRingLength', function () {
it('calculates the total length of a simple linearRing', function () {
const flatCoords = [0, 0, 1, 0, 1, 1, 0, 1];
const stride = 2;
const offset = 0;
const end = 8;
const expected = 4;
const got = linearRingLength(flatCoords, offset, end, stride);
expect(got).to.be(expected);
});
it('calculates the total length of a figure-8 linearRing', function () {
const flatCoords = [0, 0, 1, 0, 1, 1, 0, 1, 0, -1, -1, -1, -1, 0];
const stride = 2;
const offset = 0;
const end = 14;
const expected = 8;
const got = linearRingLength(flatCoords, offset, end, stride);
expect(got).to.be(expected);
});
});
});

View File

@@ -0,0 +1,443 @@
import expect from '../../../expect.js';
import {
linearRingIsClockwise,
linearRingsAreOriented,
linearRingssAreOriented,
orientLinearRings,
orientLinearRingsArray,
} from '../../../../../src/ol/geom/flat/orient.js';
describe('ol/geom/flat/orient.js', function () {
describe('linearRingIsClockwise', function () {
it('identifies clockwise rings', function () {
const flatCoordinates = [0, 1, 1, 4, 4, 3, 3, 0];
const isClockwise = linearRingIsClockwise(
flatCoordinates,
0,
flatCoordinates.length,
2
);
expect(isClockwise).to.be(true);
});
it('identifies anti-clockwise rings', function () {
const flatCoordinates = [2, 2, 3, 2, 3, 3, 2, 3];
const isClockwise = linearRingIsClockwise(
flatCoordinates,
0,
flatCoordinates.length,
2
);
expect(isClockwise).to.be(false);
});
it('identifies clockwise with duplicated coordinates', function () {
const flatCoordinates = [0, 1, 0, 1, 1, 4, 1, 4, 4, 3, 4, 3, 3, 0, 3, 0];
const isClockwise = linearRingIsClockwise(
flatCoordinates,
0,
flatCoordinates.length,
2
);
expect(isClockwise).to.be(true);
});
it('identifies anti-clockwise with duplicated coordinates', function () {
const flatCoordinates = [2, 2, 2, 2, 3, 2, 3, 2, 3, 3, 3, 3, 2, 3, 2, 3];
const isClockwise = linearRingIsClockwise(
flatCoordinates,
0,
flatCoordinates.length,
2
);
expect(isClockwise).to.be(false);
});
it('identifies clockwise when last coordinate equals first', function () {
const flatCoordinates = [0, 1, 1, 4, 4, 3, 3, 0, 0, 1];
const isClockwise = linearRingIsClockwise(
flatCoordinates,
0,
flatCoordinates.length,
2
);
expect(isClockwise).to.be(true);
});
it('identifies anti-clockwise when last coordinate equals first', function () {
const flatCoordinates = [2, 2, 3, 2, 3, 3, 2, 3, 2, 2];
const isClockwise = linearRingIsClockwise(
flatCoordinates,
0,
flatCoordinates.length,
2
);
expect(isClockwise).to.be(false);
});
it('returns undefined when ring has too few vertices', function () {
const flatCoordinates = [2, 2, 3, 2];
const isClockwise = linearRingIsClockwise(
flatCoordinates,
0,
flatCoordinates.length,
2
);
expect(isClockwise).to.be(undefined);
});
});
describe('linearRingsAreOriented', function () {
const oriented = linearRingsAreOriented;
const rightCoords = [
-180,
-90,
180,
-90,
180,
90,
-180,
90,
-180,
-90,
-100,
-45,
-100,
45,
100,
45,
100,
-45,
-100,
-45,
];
const leftCoords = [
-180,
-90,
-180,
90,
180,
90,
180,
-90,
-180,
-90,
-100,
-45,
100,
-45,
100,
45,
-100,
45,
-100,
-45,
];
const ends = [10, 20];
it('checks for left-hand orientation by default', function () {
expect(oriented(rightCoords, 0, ends, 2)).to.be(false);
expect(oriented(leftCoords, 0, ends, 2)).to.be(true);
});
it('can check for right-hand orientation', function () {
expect(oriented(rightCoords, 0, ends, 2, true)).to.be(true);
expect(oriented(leftCoords, 0, ends, 2, true)).to.be(false);
});
});
describe('linearRingssAreOriented', function () {
const oriented = linearRingssAreOriented;
const rightCoords = [
-180,
-90,
180,
-90,
180,
90,
-180,
90,
-180,
-90,
-100,
-45,
-100,
45,
100,
45,
100,
-45,
-100,
-45,
-180,
-90,
180,
-90,
180,
90,
-180,
90,
-180,
-90,
-100,
-45,
-100,
45,
100,
45,
100,
-45,
-100,
-45,
];
const leftCoords = [
-180,
-90,
-180,
90,
180,
90,
180,
-90,
-180,
-90,
-100,
-45,
100,
-45,
100,
45,
-100,
45,
-100,
-45,
-180,
-90,
-180,
90,
180,
90,
180,
-90,
-180,
-90,
-100,
-45,
100,
-45,
100,
45,
-100,
45,
-100,
-45,
];
const ends = [
[10, 20],
[30, 40],
];
it('checks for left-hand orientation by default', function () {
expect(oriented(rightCoords, 0, ends, 2)).to.be(false);
expect(oriented(leftCoords, 0, ends, 2)).to.be(true);
});
it('can check for right-hand orientation', function () {
expect(oriented(rightCoords, 0, ends, 2, true)).to.be(true);
expect(oriented(leftCoords, 0, ends, 2, true)).to.be(false);
});
});
describe('orientLinearRings', function () {
const orient = orientLinearRings;
const rightCoords = [
-180,
-90,
180,
-90,
180,
90,
-180,
90,
-180,
-90,
-100,
-45,
-100,
45,
100,
45,
100,
-45,
-100,
-45,
];
const leftCoords = [
-180,
-90,
-180,
90,
180,
90,
180,
-90,
-180,
-90,
-100,
-45,
100,
-45,
100,
45,
-100,
45,
-100,
-45,
];
const ends = [10, 20];
it('orients using the left-hand rule by default', function () {
const rightClone = rightCoords.slice();
orient(rightClone, 0, ends, 2);
expect(rightClone).to.eql(leftCoords);
const leftClone = leftCoords.slice();
orient(leftClone, 0, ends, 2);
expect(leftClone).to.eql(leftCoords);
});
it('can orient using the right-hand rule', function () {
const rightClone = rightCoords.slice();
orient(rightClone, 0, ends, 2, true);
expect(rightClone).to.eql(rightCoords);
const leftClone = leftCoords.slice();
orient(leftClone, 0, ends, 2, true);
expect(leftClone).to.eql(rightCoords);
});
});
describe('orientLinearRingsArray', function () {
const orient = orientLinearRingsArray;
const rightCoords = [
-180,
-90,
180,
-90,
180,
90,
-180,
90,
-180,
-90,
-100,
-45,
-100,
45,
100,
45,
100,
-45,
-100,
-45,
-180,
-90,
180,
-90,
180,
90,
-180,
90,
-180,
-90,
-100,
-45,
-100,
45,
100,
45,
100,
-45,
-100,
-45,
];
const leftCoords = [
-180,
-90,
-180,
90,
180,
90,
180,
-90,
-180,
-90,
-100,
-45,
100,
-45,
100,
45,
-100,
45,
-100,
-45,
-180,
-90,
-180,
90,
180,
90,
180,
-90,
-180,
-90,
-100,
-45,
100,
-45,
100,
45,
-100,
45,
-100,
-45,
];
const ends = [
[10, 20],
[30, 40],
];
it('orients using the left-hand rule by default', function () {
const rightClone = rightCoords.slice();
orient(rightClone, 0, ends, 2);
expect(rightClone).to.eql(leftCoords);
const leftClone = leftCoords.slice();
orient(leftClone, 0, ends, 2);
expect(leftClone).to.eql(leftCoords);
});
it('can orient using the right-hand rule', function () {
const rightClone = rightCoords.slice();
orient(rightClone, 0, ends, 2, true);
expect(rightClone).to.eql(rightCoords);
const leftClone = leftCoords.slice();
orient(leftClone, 0, ends, 2, true);
expect(leftClone).to.eql(rightCoords);
});
});
});

View File

@@ -0,0 +1,136 @@
import expect from '../../../expect.js';
import {coordinates as reverseCoordinates} from '../../../../../src/ol/geom/flat/reverse.js';
describe('ol/geom/flat/reverse.js', function () {
describe('coordinates', function () {
describe('with a stride of 2', function () {
it('can reverse empty flat coordinates', function () {
const flatCoordinates = [];
reverseCoordinates(flatCoordinates, 0, flatCoordinates.length, 2);
expect(flatCoordinates).to.be.empty();
});
it('can reverse one flat coordinates', function () {
const flatCoordinates = [1, 2];
reverseCoordinates(flatCoordinates, 0, flatCoordinates.length, 2);
expect(flatCoordinates).to.eql([1, 2]);
});
it('can reverse two flat coordinates', function () {
const flatCoordinates = [1, 2, 3, 4];
reverseCoordinates(flatCoordinates, 0, flatCoordinates.length, 2);
expect(flatCoordinates).to.eql([3, 4, 1, 2]);
});
it('can reverse three flat coordinates', function () {
const flatCoordinates = [1, 2, 3, 4, 5, 6];
reverseCoordinates(flatCoordinates, 0, flatCoordinates.length, 2);
expect(flatCoordinates).to.eql([5, 6, 3, 4, 1, 2]);
});
it('can reverse four flat coordinates', function () {
const flatCoordinates = [1, 2, 3, 4, 5, 6, 7, 8];
reverseCoordinates(flatCoordinates, 0, flatCoordinates.length, 2);
expect(flatCoordinates).to.eql([7, 8, 5, 6, 3, 4, 1, 2]);
});
});
describe('with a stride of 3', function () {
it('can reverse empty flat coordinates', function () {
const flatCoordinates = [];
reverseCoordinates(flatCoordinates, 0, flatCoordinates.length, 3);
expect(flatCoordinates).to.be.empty();
});
it('can reverse one flat coordinates', function () {
const flatCoordinates = [1, 2, 3];
reverseCoordinates(flatCoordinates, 0, flatCoordinates.length, 3);
expect(flatCoordinates).to.eql([1, 2, 3]);
});
it('can reverse two flat coordinates', function () {
const flatCoordinates = [1, 2, 3, 4, 5, 6];
reverseCoordinates(flatCoordinates, 0, flatCoordinates.length, 3);
expect(flatCoordinates).to.eql([4, 5, 6, 1, 2, 3]);
});
it('can reverse three flat coordinates', function () {
const flatCoordinates = [1, 2, 3, 4, 5, 6, 7, 8, 9];
reverseCoordinates(flatCoordinates, 0, flatCoordinates.length, 3);
expect(flatCoordinates).to.eql([7, 8, 9, 4, 5, 6, 1, 2, 3]);
});
it('can reverse four flat coordinates', function () {
const flatCoordinates = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
reverseCoordinates(flatCoordinates, 0, flatCoordinates.length, 3);
expect(flatCoordinates).to.eql([10, 11, 12, 7, 8, 9, 4, 5, 6, 1, 2, 3]);
});
});
describe('with a stride of 4', function () {
it('can reverse empty flat coordinates', function () {
const flatCoordinates = [];
reverseCoordinates(flatCoordinates, 0, flatCoordinates.length, 4);
expect(flatCoordinates).to.be.empty();
});
it('can reverse one flat coordinates', function () {
const flatCoordinates = [1, 2, 3, 4];
reverseCoordinates(flatCoordinates, 0, flatCoordinates.length, 4);
expect(flatCoordinates).to.eql([1, 2, 3, 4]);
});
it('can reverse two flat coordinates', function () {
const flatCoordinates = [1, 2, 3, 4, 5, 6, 7, 8];
reverseCoordinates(flatCoordinates, 0, flatCoordinates.length, 4);
expect(flatCoordinates).to.eql([5, 6, 7, 8, 1, 2, 3, 4]);
});
it('can reverse three flat coordinates', function () {
const flatCoordinates = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
reverseCoordinates(flatCoordinates, 0, flatCoordinates.length, 4);
expect(flatCoordinates).to.eql([9, 10, 11, 12, 5, 6, 7, 8, 1, 2, 3, 4]);
});
it('can reverse four flat coordinates', function () {
const flatCoordinates = [
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
];
reverseCoordinates(flatCoordinates, 0, flatCoordinates.length, 4);
expect(flatCoordinates).to.eql([
13,
14,
15,
16,
9,
10,
11,
12,
5,
6,
7,
8,
1,
2,
3,
4,
]);
});
});
});
});

View File

@@ -0,0 +1,54 @@
import expect from '../../../expect.js';
import sinon from 'sinon';
import {forEach as forEachSegment} from '../../../../../src/ol/geom/flat/segments.js';
describe('ol/geom/flat/segments.js', function () {
describe('forEach', function () {
let flatCoordinates, offset, end, stride;
beforeEach(function () {
flatCoordinates = [0, 0, 1, 1, 2, 2, 3, 3];
offset = 0;
end = 8;
stride = 2;
});
describe('callback returns undefined', function () {
it('executes the callback for each segment', function () {
const args = [];
const spy = sinon.spy(function (point1, point2) {
args.push([point1[0], point1[1], point2[0], point2[1]]);
});
const ret = forEachSegment(flatCoordinates, offset, end, stride, spy);
expect(spy.callCount).to.be(3);
expect(args[0][0]).to.be(0);
expect(args[0][1]).to.be(0);
expect(args[0][2]).to.be(1);
expect(args[0][3]).to.be(1);
expect(args[1][0]).to.be(1);
expect(args[1][1]).to.be(1);
expect(args[1][2]).to.be(2);
expect(args[1][3]).to.be(2);
expect(args[2][0]).to.be(2);
expect(args[2][1]).to.be(2);
expect(args[2][2]).to.be(3);
expect(args[2][3]).to.be(3);
expect(ret).to.be(false);
});
});
describe('callback returns true', function () {
it('executes the callback for the first segment', function () {
const args = [];
const spy = sinon.spy(function (point1, point2) {
args.push([point1[0], point1[1], point2[0], point2[1]]);
return true;
});
const ret = forEachSegment(flatCoordinates, offset, end, stride, spy);
expect(spy.callCount).to.be(1);
expect(args[0][0]).to.be(0);
expect(args[0][1]).to.be(0);
expect(args[0][2]).to.be(1);
expect(args[0][3]).to.be(1);
expect(ret).to.be(true);
});
});
});
});

View File

@@ -0,0 +1,916 @@
import expect from '../../../expect.js';
import {
douglasPeucker,
quantize,
radialDistance,
simplifyLineString,
} from '../../../../../src/ol/geom/flat/simplify.js';
describe('ol/geom/flat/simplify.js', function () {
const flatCoordinates = [
224.55,
250.15,
226.91,
244.19,
233.31,
241.45,
234.98,
236.06,
244.21,
232.76,
262.59,
215.31,
267.76,
213.81,
273.57,
201.84,
273.12,
192.16,
277.62,
189.03,
280.36,
181.41,
286.51,
177.74,
292.41,
159.37,
296.91,
155.64,
314.95,
151.37,
319.75,
145.16,
330.33,
137.57,
341.48,
139.96,
369.98,
137.89,
387.39,
142.51,
391.28,
139.39,
409.52,
141.14,
414.82,
139.75,
427.72,
127.3,
439.6,
119.74,
474.93,
107.87,
486.51,
106.75,
489.2,
109.45,
493.79,
108.63,
504.74,
119.66,
512.96,
122.35,
518.63,
120.89,
524.09,
126.88,
529.57,
127.86,
534.21,
140.93,
539.27,
147.24,
567.69,
148.91,
575.25,
157.26,
580.62,
158.15,
601.53,
156.85,
617.74,
159.86,
622.0,
167.04,
629.55,
194.6,
638.9,
195.61,
641.26,
200.81,
651.77,
204.56,
671.55,
222.55,
683.68,
217.45,
695.25,
219.15,
700.64,
217.98,
703.12,
214.36,
712.26,
215.87,
721.49,
212.81,
727.81,
213.36,
729.98,
208.73,
735.32,
208.2,
739.94,
204.77,
769.98,
208.42,
779.6,
216.87,
784.2,
218.16,
800.24,
214.62,
810.53,
219.73,
817.19,
226.82,
820.77,
236.17,
827.23,
236.16,
829.89,
239.89,
851.0,
248.94,
859.88,
255.49,
865.21,
268.53,
857.95,
280.3,
865.48,
291.45,
866.81,
298.66,
864.68,
302.71,
867.79,
306.17,
859.87,
311.37,
860.08,
314.35,
858.29,
314.94,
858.1,
327.6,
854.54,
335.4,
860.92,
343.0,
856.43,
350.15,
851.42,
352.96,
849.84,
359.59,
854.56,
365.53,
849.74,
370.38,
844.09,
371.89,
844.75,
380.44,
841.52,
383.67,
839.57,
390.4,
845.59,
399.05,
848.4,
407.55,
843.71,
411.3,
844.09,
419.88,
839.51,
432.76,
841.33,
441.04,
847.62,
449.22,
847.16,
458.44,
851.38,
462.79,
853.97,
471.15,
866.36,
480.77,
];
const simplifiedRadiallyFlatCoordinates = [
224.55,
250.15,
226.91,
244.19,
233.31,
241.45,
234.98,
236.06,
244.21,
232.76,
262.59,
215.31,
267.76,
213.81,
273.57,
201.84,
273.12,
192.16,
277.62,
189.03,
280.36,
181.41,
286.51,
177.74,
292.41,
159.37,
296.91,
155.64,
314.95,
151.37,
319.75,
145.16,
330.33,
137.57,
341.48,
139.96,
369.98,
137.89,
387.39,
142.51,
409.52,
141.14,
414.82,
139.75,
427.72,
127.3,
439.6,
119.74,
474.93,
107.87,
486.51,
106.75,
493.79,
108.63,
504.74,
119.66,
512.96,
122.35,
518.63,
120.89,
524.09,
126.88,
529.57,
127.86,
534.21,
140.93,
539.27,
147.24,
567.69,
148.91,
575.25,
157.26,
580.62,
158.15,
601.53,
156.85,
617.74,
159.86,
622.0,
167.04,
629.55,
194.6,
638.9,
195.61,
641.26,
200.81,
651.77,
204.56,
671.55,
222.55,
683.68,
217.45,
695.25,
219.15,
700.64,
217.98,
712.26,
215.87,
721.49,
212.81,
727.81,
213.36,
729.98,
208.73,
735.32,
208.2,
739.94,
204.77,
769.98,
208.42,
779.6,
216.87,
800.24,
214.62,
810.53,
219.73,
817.19,
226.82,
820.77,
236.17,
827.23,
236.16,
851.0,
248.94,
859.88,
255.49,
865.21,
268.53,
857.95,
280.3,
865.48,
291.45,
866.81,
298.66,
867.79,
306.17,
859.87,
311.37,
858.1,
327.6,
854.54,
335.4,
860.92,
343.0,
856.43,
350.15,
851.42,
352.96,
849.84,
359.59,
854.56,
365.53,
849.74,
370.38,
844.09,
371.89,
844.75,
380.44,
839.57,
390.4,
845.59,
399.05,
848.4,
407.55,
843.71,
411.3,
844.09,
419.88,
839.51,
432.76,
841.33,
441.04,
847.62,
449.22,
847.16,
458.44,
851.38,
462.79,
853.97,
471.15,
866.36,
480.77,
];
const simplifiedFlatCoordinates = [
224.55,
250.15,
267.76,
213.81,
296.91,
155.64,
330.33,
137.57,
409.52,
141.14,
439.6,
119.74,
486.51,
106.75,
529.57,
127.86,
539.27,
147.24,
617.74,
159.86,
629.55,
194.6,
671.55,
222.55,
727.81,
213.36,
739.94,
204.77,
769.98,
208.42,
779.6,
216.87,
800.24,
214.62,
820.77,
236.17,
859.88,
255.49,
865.21,
268.53,
857.95,
280.3,
867.79,
306.17,
859.87,
311.37,
854.54,
335.4,
860.92,
343.0,
849.84,
359.59,
854.56,
365.53,
844.09,
371.89,
839.57,
390.4,
848.4,
407.55,
839.51,
432.76,
853.97,
471.15,
866.36,
480.77,
];
const simplifiedHighQualityFlatCoordinates = [
224.55,
250.15,
267.76,
213.81,
296.91,
155.64,
330.33,
137.57,
409.52,
141.14,
439.6,
119.74,
486.51,
106.75,
529.57,
127.86,
539.27,
147.24,
617.74,
159.86,
629.55,
194.6,
671.55,
222.55,
727.81,
213.36,
739.94,
204.77,
769.98,
208.42,
784.2,
218.16,
800.24,
214.62,
820.77,
236.17,
859.88,
255.49,
865.21,
268.53,
857.95,
280.3,
867.79,
306.17,
858.29,
314.94,
854.54,
335.4,
860.92,
343.0,
849.84,
359.59,
854.56,
365.53,
844.09,
371.89,
839.57,
390.4,
848.4,
407.55,
839.51,
432.76,
853.97,
471.15,
866.36,
480.77,
];
describe('simplifyLineString', function () {
it('works with empty line strings', function () {
expect(simplifyLineString([], 0, 0, 2, 1, true)).to.eql([]);
expect(simplifyLineString([], 0, 0, 2, 1, false)).to.eql([]);
});
it('works with a line string with a single point', function () {
expect(simplifyLineString([1, 2], 0, 2, 2, 1, true)).to.eql([1, 2]);
expect(simplifyLineString([1, 2], 0, 2, 2, 1, false)).to.eql([1, 2]);
});
it('returns the expected result with low quality', function () {
const result = simplifyLineString(
flatCoordinates,
0,
flatCoordinates.length,
2,
25,
false
);
expect(result.length).to.be(simplifiedFlatCoordinates.length);
expect(result).to.eql(simplifiedFlatCoordinates);
});
it('returns the expected result with high quality', function () {
const result = simplifyLineString(
flatCoordinates,
0,
flatCoordinates.length,
2,
25,
true
);
expect(result.length).to.be(simplifiedHighQualityFlatCoordinates.length);
expect(result).to.eql(simplifiedHighQualityFlatCoordinates);
});
});
describe('radialDistance', function () {
let dest;
beforeEach(function () {
dest = [];
});
it('works with empty line strings', function () {
expect(radialDistance([], 0, 0, 2, 1, dest, 0)).to.be(0);
expect(dest).to.eql([]);
});
it('works with a line string with a single point', function () {
expect(radialDistance([1, 2], 0, 2, 2, 1, dest, 0)).to.be(2);
expect(dest).to.eql([1, 2]);
});
it('works with a line string with two points', function () {
expect(radialDistance([1, 2, 3, 4], 0, 4, 2, 1, dest, 0)).to.be(4);
expect(dest).to.eql([1, 2, 3, 4]);
});
it('works when the points are widely spaced', function () {
expect(
radialDistance([0, 0, 1, 0, 2, 0, 3, 0], 0, 8, 2, 0.5, dest, 0)
).to.be(8);
expect(dest).to.eql([0, 0, 1, 0, 2, 0, 3, 0]);
});
it('works when the spacing matches the tolerance', function () {
expect(
radialDistance([0, 0, 1, 0, 2, 0, 3, 0], 0, 8, 2, 1, dest, 0)
).to.be(6);
expect(dest).to.eql([0, 0, 2, 0, 3, 0]);
});
it('works when the points are closely spaced', function () {
expect(
radialDistance([0, 0, 1, 0, 2, 0, 3, 0], 0, 8, 2, 1.5, dest, 0)
).to.be(6);
expect(dest).to.eql([0, 0, 2, 0, 3, 0]);
});
it('works when the line oscillates with widely spaced points', function () {
expect(
radialDistance(
[0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1],
0,
12,
2,
1,
dest,
0
)
).to.be(12);
expect(dest).to.eql([0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1]);
});
it('works when the line oscillates with closely spaced points', function () {
expect(
radialDistance(
[0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1],
0,
12,
2,
2,
dest,
0
)
).to.be(4);
expect(dest).to.eql([0, 0, 1, 1]);
});
it('works when the line oscillates within the tolerance', function () {
expect(
radialDistance(
[0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0],
0,
14,
2,
2,
dest,
0
)
).to.be(2);
expect(dest).to.eql([0, 0]);
});
it('works with real data', function () {
expect(
radialDistance(
flatCoordinates,
0,
flatCoordinates.length,
2,
25,
dest,
0
)
).to.be(simplifiedRadiallyFlatCoordinates.length);
expect(dest).to.eql(simplifiedRadiallyFlatCoordinates);
});
});
describe('douglasPeucker', function () {
let dest;
beforeEach(function () {
dest = [];
});
it('works with empty line strings', function () {
expect(douglasPeucker([], 0, 0, 2, 1, dest, 0)).to.be(0);
expect(dest).to.eql([]);
});
it('works with a line string with a single point', function () {
expect(douglasPeucker([1, 2], 0, 2, 2, 1, dest, 0)).to.be(2);
expect(dest).to.eql([1, 2]);
});
it('works with a line string with two points', function () {
expect(douglasPeucker([1, 2, 3, 4], 0, 4, 2, 1, dest, 0)).to.be(4);
expect(dest).to.eql([1, 2, 3, 4]);
});
it('works when the points are widely spaced', function () {
expect(
douglasPeucker([0, 0, 1, 0, 2, 0, 3, 0], 0, 8, 2, 0.5, dest, 0)
).to.be(4);
expect(dest).to.eql([0, 0, 3, 0]);
});
it('works when the spacing matches the tolerance', function () {
expect(
douglasPeucker([0, 0, 1, 0, 2, 0, 3, 0], 0, 8, 2, 1, dest, 0)
).to.be(4);
expect(dest).to.eql([0, 0, 3, 0]);
});
it('works when the points are closely spaced', function () {
expect(
douglasPeucker([0, 0, 1, 0, 2, 0, 3, 0], 0, 8, 2, 1.5, dest, 0)
).to.be(4);
expect(dest).to.eql([0, 0, 3, 0]);
});
it('does not elimnate points outside the tolerance', function () {
expect(douglasPeucker([0, 0, 1, 1, 2, 0], 0, 6, 2, 0.5, dest, 0)).to.be(
6
);
expect(dest).to.eql([0, 0, 1, 1, 2, 0]);
});
it('does eliminate points within the tolerance', function () {
expect(douglasPeucker([0, 0, 1, 1, 2, 0], 0, 6, 2, 2, dest, 0)).to.be(4);
expect(dest).to.eql([0, 0, 2, 0]);
});
it('does not eliminate multiple points outside the tolerance', function () {
expect(
douglasPeucker([0, 0, 1, 1, 1, -1, 2, 0], 0, 8, 2, 0.5, dest, 0)
).to.be(8);
expect(dest).to.eql([0, 0, 1, 1, 1, -1, 2, 0]);
});
it('does eliminate multiple points within the tolerance', function () {
expect(
douglasPeucker([0, 0, 1, 1, 1, -1, 2, 0], 0, 8, 2, 2, dest, 0)
).to.be(4);
expect(dest).to.eql([0, 0, 2, 0]);
});
it('works when the line oscillates with widely spaced points', function () {
expect(
douglasPeucker(
[0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1],
0,
12,
2,
1,
dest,
0
)
).to.be(4);
expect(dest).to.eql([0, 0, 1, 1]);
});
it('works when the line oscillates with closely spaced points', function () {
expect(
douglasPeucker(
[0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1],
0,
12,
2,
2,
dest,
0
)
).to.be(4);
expect(dest).to.eql([0, 0, 1, 1]);
});
it('works when the line oscillates within the tolerance', function () {
expect(
douglasPeucker(
[0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0],
0,
14,
2,
2,
dest,
0
)
).to.be(4);
expect(dest).to.eql([0, 0, 0, 0]);
});
it('works on small triangles', function () {
expect(
douglasPeucker([3, 0, 4, 1, 5, 2, 5, 0], 0, 8, 2, 1, dest, 0)
).to.be(6);
expect(dest).to.eql([3, 0, 5, 2, 5, 0]);
});
it('is the same as high quality simplification', function () {
expect(
douglasPeucker(
flatCoordinates,
0,
flatCoordinates.length,
2,
25,
dest,
0
)
).to.be(simplifiedHighQualityFlatCoordinates.length);
expect(dest).to.eql(simplifiedHighQualityFlatCoordinates);
});
});
describe('quantize', function () {
it('handles empty coordinates', function () {
const simplifiedFlatCoordinates = [];
expect(quantize([], 0, 0, 2, 2, simplifiedFlatCoordinates, 0)).to.be(0);
expect(simplifiedFlatCoordinates).to.be.empty();
});
it('expands points to a zero-length line', function () {
const simplifiedFlatCoordinates = [];
expect(
quantize([0, 0, 0, 0], 0, 4, 2, 2, simplifiedFlatCoordinates, 0)
).to.be(4);
expect(simplifiedFlatCoordinates).to.eql([0, 0, 0, 0]);
});
it('snaps near-by points to the same value', function () {
const simplifiedFlatCoordinates = [];
expect(
quantize([0.1, 0, 0, 0.1], 0, 4, 2, 2, simplifiedFlatCoordinates, 0)
).to.be(4);
expect(simplifiedFlatCoordinates).to.eql([0, 0, 0, 0]);
});
it('eliminates duplicate snapped points', function () {
const simplifiedFlatCoordinates = [];
expect(
quantize(
[0.1, 0, 2, 0, 2.1, 0, 2, 0.1, 1.9, 0, 2, -0.1],
0,
12,
2,
2,
simplifiedFlatCoordinates,
0
)
).to.be(4);
expect(simplifiedFlatCoordinates).to.eql([0, 0, 2, 0]);
});
it('eliminates horizontal colinear points', function () {
const simplifiedFlatCoordinates = [];
expect(
quantize(
[0, 0, 2, 0, 4, 0, 6, 0],
0,
8,
2,
2,
simplifiedFlatCoordinates,
0
)
).to.be(4);
expect(simplifiedFlatCoordinates).to.eql([0, 0, 6, 0]);
});
it('eliminates vertical colinear points', function () {
const simplifiedFlatCoordinates = [];
expect(
quantize(
[0, 0, 0, -2, 0, -4, 0, -6],
0,
8,
2,
2,
simplifiedFlatCoordinates,
0
)
).to.be(4);
expect(simplifiedFlatCoordinates).to.eql([0, 0, 0, -6]);
});
it('eliminates diagonal colinear points', function () {
const simplifiedFlatCoordinates = [];
expect(
quantize(
[0, 0, 2, -2, 4, -4, 6, -6],
0,
8,
2,
2,
simplifiedFlatCoordinates,
0
)
).to.be(4);
expect(simplifiedFlatCoordinates).to.eql([0, 0, 6, -6]);
});
it('handles switchbacks', function () {
const simplifiedFlatCoordinates = [];
expect(
quantize(
[0, 0, 2, 0, 0, 0, 4, 0],
0,
8,
2,
2,
simplifiedFlatCoordinates,
0
)
).to.be(8);
expect(simplifiedFlatCoordinates).to.eql([0, 0, 2, 0, 0, 0, 4, 0]);
});
});
});

View File

@@ -0,0 +1,70 @@
import expect from '../../../expect.js';
import {matchingChunk} from '../../../../../src/ol/geom/flat/straightchunk.js';
describe('ol/geom/flat/straightchunk.js', function () {
describe('matchingChunk', function () {
describe('single segment with stride == 3', function () {
const flatCoords = [0, 0, 42, 1, 1, 42];
const stride = 3;
it('returns whole line with angle delta', function () {
const got = matchingChunk(Math.PI / 4, flatCoords, 0, 6, stride);
expect(got).to.eql([0, 6]);
});
it('returns whole line with zero angle delta', function () {
const got = matchingChunk(0, flatCoords, 0, 6, stride);
expect(got).to.eql([0, 6]);
});
});
describe('short line string', function () {
const flatCoords = [0, 0, 1, 0, 1, 1, 0, 1];
const stride = 2;
it('returns whole line if straight enough', function () {
const got = matchingChunk(Math.PI, flatCoords, 0, 8, stride);
expect(got).to.eql([0, 8]);
});
it('returns first matching chunk if all chunk lengths are the same', function () {
const got = matchingChunk(Math.PI / 4, flatCoords, 0, 8, stride);
expect(got).to.eql([0, 4]);
});
});
describe('longer line string', function () {
const flatCoords = [
0,
0,
1,
0,
1,
1,
0,
1,
0,
-1,
-1,
-1,
-1,
0,
-1,
2,
-2,
4,
];
const stride = 2;
it('returns stright chunk from within the linestring', function () {
const got = matchingChunk(0, flatCoords, 0, 18, stride);
expect(got).to.eql([10, 16]);
});
it('returns long chunk at the end if angle and length within threshold', function () {
const got = matchingChunk(Math.PI / 4, flatCoords, 0, 18, stride);
expect(got).to.eql([10, 18]);
});
});
});
});

View File

@@ -0,0 +1,245 @@
import expect from '../../../expect.js';
import {drawTextOnPath} from '../../../../../src/ol/geom/flat/textpath.js';
import {lineStringLength} from '../../../../../src/ol/geom/flat/length.js';
describe('ol/geom/flat/drawTextOnPath.js', function () {
const horizontal = [0, 0, 100, 0];
const vertical = [0, 0, 0, 100];
const diagonal = [0, 0, 100, 100];
const reverse = [100, 0, 0, 100];
const angled = [0, 0, 100, 100, 200, 0];
const reverseangled = [151, 17, 163, 22, 159, 30, 150, 30, 143, 24, 151, 17];
function measureAndCacheTextWidth(font, text, cache) {
return 10 * text.length;
}
it('center-aligns text on a horizontal line', function () {
const startM = 50 - 15;
const instructions = drawTextOnPath(
horizontal,
0,
horizontal.length,
2,
'foo',
startM,
Infinity,
1,
measureAndCacheTextWidth,
'',
{}
);
expect(instructions).to.eql([[50, 0, 15, 0, 'foo']]);
});
it('left-aligns text on a horizontal line', function () {
const instructions = drawTextOnPath(
horizontal,
0,
horizontal.length,
2,
'foo',
0,
Infinity,
1,
measureAndCacheTextWidth,
'',
{}
);
expect(instructions).to.eql([[15, 0, 15, 0, 'foo']]);
});
it('right-aligns text on a horizontal line', function () {
const startM = 100 - 30;
const instructions = drawTextOnPath(
horizontal,
0,
horizontal.length,
2,
'foo',
startM,
Infinity,
1,
measureAndCacheTextWidth,
'',
{}
);
expect(instructions).to.eql([[85, 0, 15, 0, 'foo']]);
});
it('draws text on a vertical line', function () {
const startM = 50 - 15;
const instructions = drawTextOnPath(
vertical,
0,
vertical.length,
2,
'foo',
startM,
Infinity,
1,
measureAndCacheTextWidth,
'',
{}
);
const a = (90 * Math.PI) / 180;
expect(instructions).to.eql([[0, 50, 15, a, 'foo']]);
});
it('draws text on a diagonal line', function () {
const startM = Math.sqrt(2) * 50 - 15;
const instructions = drawTextOnPath(
diagonal,
0,
diagonal.length,
2,
'foo',
startM,
Infinity,
1,
measureAndCacheTextWidth,
'',
{}
);
expect(instructions[0][3]).to.be((45 * Math.PI) / 180);
expect(instructions.length).to.be(1);
});
it('draws reverse text on a diagonal line', function () {
const startM = Math.sqrt(2) * 50 - 15;
const instructions = drawTextOnPath(
reverse,
0,
reverse.length,
2,
'foo',
startM,
Infinity,
1,
measureAndCacheTextWidth,
'',
{}
);
expect(instructions[0][3]).to.be((-45 * Math.PI) / 180);
expect(instructions.length).to.be(1);
});
it('renders long text with extrapolation', function () {
const startM = 50 - 75;
const instructions = drawTextOnPath(
horizontal,
0,
horizontal.length,
2,
'foo-foo-foo-foo',
startM,
Infinity,
1,
measureAndCacheTextWidth,
'',
{}
);
expect(instructions[0]).to.eql([50, 0, 75, 0, 'foo-foo-foo-foo']);
expect(instructions.length).to.be(1);
});
it('renders angled text', function () {
const length = lineStringLength(angled, 0, angled.length, 2);
const startM = length / 2 - 20;
const instructions = drawTextOnPath(
angled,
0,
angled.length,
2,
'fooo',
startM,
Infinity,
1,
measureAndCacheTextWidth,
'',
{}
);
expect(instructions[0][3]).to.eql((45 * Math.PI) / 180);
expect(instructions[0][4]).to.be('fo');
expect(instructions[1][3]).to.eql((-45 * Math.PI) / 180);
expect(instructions[1][4]).to.be('oo');
});
it('respects maxAngle', function () {
const length = lineStringLength(angled, 0, angled.length, 2);
const startM = length / 2 - 15;
const instructions = drawTextOnPath(
angled,
0,
angled.length,
2,
'foo',
startM,
Math.PI / 4,
1,
measureAndCacheTextWidth,
'',
{}
);
expect(instructions).to.be(null);
});
it('uses the smallest angle for maxAngleDelta', function () {
const length = lineStringLength(reverseangled, 0, reverseangled.length, 2);
const startM = length / 2 - 15;
const instructions = drawTextOnPath(
reverseangled,
0,
reverseangled.length,
2,
'foo',
startM,
Math.PI,
1,
measureAndCacheTextWidth,
'',
{}
);
expect(instructions).to.not.be(undefined);
});
it('respects the offset option', function () {
const length = lineStringLength(angled, 2, angled.length, 2);
const startM = length / 2 - 15;
const instructions = drawTextOnPath(
angled,
2,
angled.length,
2,
'foo',
startM,
Infinity,
1,
measureAndCacheTextWidth,
'',
{}
);
expect(instructions[0][3]).to.be((-45 * Math.PI) / 180);
expect(instructions.length).to.be(1);
});
it('respects the end option', function () {
const length = lineStringLength(angled, 0, 4, 2);
const startM = length / 2 - 15;
const instructions = drawTextOnPath(
angled,
0,
4,
2,
'foo',
startM,
Infinity,
1,
measureAndCacheTextWidth,
'',
{}
);
expect(instructions[0][3]).to.be((45 * Math.PI) / 180);
expect(instructions.length).to.be(1);
});
});

View File

@@ -0,0 +1,48 @@
import expect from '../../../expect.js';
import {lineStringIsClosed} from '../../../../../src/ol/geom/flat/topology.js';
describe('ol/geom/flat/topology.js', function () {
describe('lineStringIsClosed', function () {
it('identifies closed lines aka boundaries', function () {
const flatCoordinates = [0, 0, 3, 0, 0, 3, 0, 0];
const isClosed = lineStringIsClosed(
flatCoordinates,
0,
flatCoordinates.length,
2
);
expect(isClosed).to.be(true);
});
it('identifies regular linestrings', function () {
const flatCoordinates = [0, 0, 3, 0, 0, 3, 5, 2];
const isClosed = lineStringIsClosed(
flatCoordinates,
0,
flatCoordinates.length,
2
);
expect(isClosed).to.be(false);
});
it('identifies degenerate boundaries', function () {
let flatCoordinates = [0, 0, 3, 0, 0, 0];
let isClosed = lineStringIsClosed(
flatCoordinates,
0,
flatCoordinates.length,
2
);
expect(isClosed).to.be(false);
flatCoordinates = [0, 0, 1, 1, 3, 3, 5, 5, 0, 0];
isClosed = lineStringIsClosed(
flatCoordinates,
0,
flatCoordinates.length,
2
);
expect(isClosed).to.be(false);
});
});
});

View File

@@ -0,0 +1,215 @@
import MultiPolygon from '../../../../../src/ol/geom/MultiPolygon.js';
import expect from '../../../expect.js';
import {rotate, translate} from '../../../../../src/ol/geom/flat/transform.js';
import {transformGeom2D} from '../../../../../src/ol/geom/SimpleGeometry.js';
describe('ol/geom/flat/transform.js', function () {
describe('transform2D', function () {
it('transforms a Simple Geometry to 2D', function () {
const multiPolygonGeometry = new MultiPolygon([
[
[
[-80.736061, 28.788576000000006, 0],
[-80.763557, 28.821799999999996, 0],
[-80.817406, 28.895123999999996, 0],
[-80.891304, 29.013130000000004, 0],
[-80.916512, 29.071560000000005, 0],
[-80.899323, 29.061249000000004, 0],
[-80.862663, 28.991361999999995, 0],
[-80.736061, 28.788576000000006, 0],
],
],
[
[
[-82.102127, 26.585724, 0],
[-82.067139, 26.497208, 0],
[-82.097641, 26.493585999999993, 0],
[-82.135895, 26.642279000000002, 0],
[-82.183495, 26.683082999999996, 0],
[-82.128838, 26.693342, 0],
[-82.102127, 26.585724, 0],
],
],
]).transform('EPSG:4326', 'EPSG:3857');
const transform = [
0.0004088332670837288,
0,
0,
-0.0004088332670837288,
4480.991370439071,
1529.5752568707105,
];
const pixelCoordinates = transformGeom2D(
multiPolygonGeometry,
transform,
[]
);
expect(pixelCoordinates[0]).to.roughlyEqual(806.6035275946265, 1e-9);
expect(pixelCoordinates[1]).to.roughlyEqual(160.48916296287916, 1e-9);
expect(pixelCoordinates[2]).to.roughlyEqual(805.3521540835154, 1e-9);
expect(pixelCoordinates[3]).to.roughlyEqual(158.76358389011807, 1e-9);
expect(pixelCoordinates[4]).to.roughlyEqual(802.9014262612932, 1e-9);
expect(pixelCoordinates[5]).to.roughlyEqual(154.95335187132082, 1e-9);
expect(pixelCoordinates[6]).to.roughlyEqual(799.5382461724039, 1e-9);
expect(pixelCoordinates[7]).to.roughlyEqual(148.815592819916, 1e-9);
expect(pixelCoordinates[8]).to.roughlyEqual(798.3910020835165, 1e-9);
expect(pixelCoordinates[9]).to.roughlyEqual(145.77392230456553, 1e-9);
expect(pixelCoordinates[10]).to.roughlyEqual(799.1732925724045, 1e-9);
expect(pixelCoordinates[11]).to.roughlyEqual(146.31080369865776, 1e-9);
expect(pixelCoordinates[12]).to.roughlyEqual(800.8417299057378, 1e-9);
expect(pixelCoordinates[13]).to.roughlyEqual(149.94832216046188, 1e-9);
expect(pixelCoordinates[14]).to.roughlyEqual(806.6035275946265, 1e-9);
expect(pixelCoordinates[15]).to.roughlyEqual(160.48916296287916, 1e-9);
expect(pixelCoordinates[16]).to.roughlyEqual(744.4323460835158, 1e-9);
expect(pixelCoordinates[17]).to.roughlyEqual(273.7179168205373, 1e-9);
expect(pixelCoordinates[18]).to.roughlyEqual(746.0246888390716, 1e-9);
expect(pixelCoordinates[19]).to.roughlyEqual(278.22094795365365, 1e-9);
expect(pixelCoordinates[20]).to.roughlyEqual(744.6365089279602, 1e-9);
expect(pixelCoordinates[21]).to.roughlyEqual(278.40513424671826, 1e-9);
expect(pixelCoordinates[22]).to.roughlyEqual(742.8955268835157, 1e-9);
expect(pixelCoordinates[23]).to.roughlyEqual(270.83899948444764, 1e-9);
expect(pixelCoordinates[24]).to.roughlyEqual(740.7291979946272, 1e-9);
expect(pixelCoordinates[25]).to.roughlyEqual(268.76099731369345, 1e-9);
expect(pixelCoordinates[26]).to.roughlyEqual(743.2166987946266, 1e-9);
expect(pixelCoordinates[27]).to.roughlyEqual(268.23842607400616, 1e-9);
expect(pixelCoordinates[28]).to.roughlyEqual(744.4323460835158, 1e-9);
expect(pixelCoordinates[29]).to.roughlyEqual(273.7179168205373, 1e-9);
});
});
describe('translate', function () {
it('translates the coordinates array', function () {
const multiPolygon = new MultiPolygon([
[
[
[0, 0, 2],
[0, 1, 2],
[1, 1, 2],
[1, 0, 2],
[0, 0, 2],
],
],
[
[
[2, 2, 3],
[2, 3, 3],
[3, 3, 3],
[3, 2, 3],
[2, 2, 3],
],
],
]);
const flatCoordinates = multiPolygon.getFlatCoordinates();
const deltaX = 1;
const deltaY = 2;
translate(
flatCoordinates,
0,
flatCoordinates.length,
multiPolygon.getStride(),
deltaX,
deltaY,
flatCoordinates
);
expect(flatCoordinates).to.eql([
1,
2,
2,
1,
3,
2,
2,
3,
2,
2,
2,
2,
1,
2,
2,
3,
4,
3,
3,
5,
3,
4,
5,
3,
4,
4,
3,
3,
4,
3,
]);
});
});
describe('rotate', function () {
it('rotates the coordinates array', function () {
const multiPolygon = new MultiPolygon([
[
[
[0, 0, 2],
[0, 1, 2],
[1, 1, 2],
[1, 0, 2],
[0, 0, 2],
],
],
[
[
[2, 2, 3],
[2, 3, 3],
[3, 3, 3],
[3, 2, 3],
[2, 2, 3],
],
],
]);
const flatCoordinates = multiPolygon.getFlatCoordinates();
const angle = Math.PI / 2;
const anchor = [0, 1];
rotate(
flatCoordinates,
0,
flatCoordinates.length,
multiPolygon.getStride(),
angle,
anchor,
flatCoordinates
);
expect(flatCoordinates[0]).to.roughlyEqual(1, 1e-9);
expect(flatCoordinates[1]).to.roughlyEqual(1, 1e-9);
expect(flatCoordinates[2]).to.roughlyEqual(2, 1e-9);
expect(flatCoordinates[3]).to.roughlyEqual(0, 1e-9);
expect(flatCoordinates[4]).to.roughlyEqual(1, 1e-9);
expect(flatCoordinates[5]).to.roughlyEqual(2, 1e-9);
expect(flatCoordinates[6]).to.roughlyEqual(Math.cos(angle), 1e-9);
expect(flatCoordinates[7]).to.roughlyEqual(2, 1e-9);
expect(flatCoordinates[8]).to.roughlyEqual(2, 1e-9);
expect(flatCoordinates[9]).to.roughlyEqual(1, 1e-9);
expect(flatCoordinates[10]).to.roughlyEqual(2, 1e-9);
expect(flatCoordinates[11]).to.roughlyEqual(2, 1e-9);
expect(flatCoordinates[12]).to.roughlyEqual(1, 1e-9);
expect(flatCoordinates[13]).to.roughlyEqual(1, 1e-9);
expect(flatCoordinates[14]).to.roughlyEqual(2, 1e-9);
expect(flatCoordinates[15]).to.roughlyEqual(-1, 1e-9);
expect(flatCoordinates[16]).to.roughlyEqual(3, 1e-9);
expect(flatCoordinates[17]).to.roughlyEqual(3, 1e-9);
expect(flatCoordinates[18]).to.roughlyEqual(-2, 1e-9);
expect(flatCoordinates[19]).to.roughlyEqual(3, 1e-9);
expect(flatCoordinates[20]).to.roughlyEqual(3, 1e-9);
expect(flatCoordinates[21]).to.roughlyEqual(-2, 1e-9);
expect(flatCoordinates[22]).to.roughlyEqual(4, 1e-9);
expect(flatCoordinates[23]).to.roughlyEqual(3, 1e-9);
expect(flatCoordinates[24]).to.roughlyEqual(-1, 1e-9);
expect(flatCoordinates[25]).to.roughlyEqual(4, 1e-9);
expect(flatCoordinates[26]).to.roughlyEqual(3, 1e-9);
expect(flatCoordinates[27]).to.roughlyEqual(-1, 1e-9);
expect(flatCoordinates[28]).to.roughlyEqual(3, 1e-9);
expect(flatCoordinates[29]).to.roughlyEqual(3, 1e-9);
});
});
});

165
test/node/ol/math.test.js Normal file
View File

@@ -0,0 +1,165 @@
import expect from '../expect.js';
import {
clamp,
cosh,
lerp,
log2,
modulo,
solveLinearSystem,
toDegrees,
toRadians,
} from '../../../src/ol/math.js';
describe('ol/math.js', () => {
describe('clamp', function () {
it('returns the correct value at -Infinity', function () {
expect(clamp(-Infinity, 10, 20)).to.eql(10);
});
it('returns the correct value at min', function () {
expect(clamp(10, 10, 20)).to.eql(10);
});
it('returns the correct value at mid point', function () {
expect(clamp(15, 10, 20)).to.eql(15);
});
it('returns the correct value at max', function () {
expect(clamp(20, 10, 20)).to.eql(20);
});
it('returns the correct value at Infinity', function () {
expect(clamp(Infinity, 10, 20)).to.eql(20);
});
});
describe('cosh', function () {
it('returns the correct value at -Infinity', function () {
expect(cosh(-Infinity)).to.eql(Infinity);
});
it('returns the correct value at -1', function () {
expect(cosh(-1)).to.roughlyEqual(1.5430806348152437, 1e-9);
});
it('returns the correct value at 0', function () {
expect(cosh(0)).to.eql(1);
});
it('returns the correct value at 1', function () {
expect(cosh(1)).to.roughlyEqual(1.5430806348152437, 1e-9);
});
it('returns the correct value at Infinity', function () {
expect(cosh(Infinity)).to.eql(Infinity);
});
});
describe('log2', function () {
it('returns the correct value at Infinity', function () {
expect(log2(Infinity)).to.eql(Infinity);
});
it('returns the correct value at 3', function () {
expect(log2(3)).to.roughlyEqual(1.584962500721156, 1e-9);
});
it('returns the correct value at 2', function () {
expect(log2(2)).to.eql(1);
});
it('returns the correct value at 1', function () {
expect(log2(1)).to.eql(0);
});
it('returns the correct value at 0', function () {
expect(log2(0)).to.eql(-Infinity);
});
it('returns the correct value at -1', function () {
expect(log2(-1).toString()).to.eql('NaN');
});
});
describe('solveLinearSystem', function () {
it('calculates correctly', function () {
const result = solveLinearSystem([
[2, 1, 3, 1],
[2, 6, 8, 3],
[6, 8, 18, 5],
]);
expect(result[0]).to.roughlyEqual(0.3, 1e-9);
expect(result[1]).to.roughlyEqual(0.4, 1e-9);
expect(result[2]).to.roughlyEqual(0, 1e-9);
});
it('can handle singular matrix', function () {
const result = solveLinearSystem([
[2, 1, 3, 1],
[2, 6, 8, 3],
[2, 1, 3, 1],
]);
expect(result).to.be(null);
});
});
describe('toDegrees', function () {
it('returns the correct value at -π', function () {
expect(toDegrees(-Math.PI)).to.be(-180);
});
it('returns the correct value at 0', function () {
expect(toDegrees(0)).to.be(0);
});
it('returns the correct value at π', function () {
expect(toDegrees(Math.PI)).to.be(180);
});
});
describe('toRadians', function () {
it('returns the correct value at -180', function () {
expect(toRadians(-180)).to.be(-Math.PI);
});
it('returns the correct value at 0', function () {
expect(toRadians(0)).to.be(0);
});
it('returns the correct value at 180', function () {
expect(toRadians(180)).to.be(Math.PI);
});
});
describe('modulo', function () {
it('256 / 8 returns 0', function () {
expect(modulo(256, 8)).to.be(0);
});
it('positive and positive returns a positive ', function () {
expect(modulo(7, 8)).to.be(7);
});
it('same Dividend and Divisor returns 0', function () {
expect(modulo(4, 4)).to.be(0);
});
it('negative and positive returns positive', function () {
expect(modulo(-3, 4)).to.be(1);
});
it('negative and negative returns negative', function () {
expect(modulo(-4, -5)).to.be(-4);
expect(modulo(-3, -4)).to.be(-3);
});
it('positive and negative returns negative', function () {
expect(modulo(3, -4)).to.be(-1);
expect(modulo(1, -5)).to.be(-4);
expect(modulo(6, -5)).to.be(-4);
});
});
describe('lerp', function () {
it('correctly interpolated numbers', function () {
expect(lerp(0, 0, 0)).to.be(0);
expect(lerp(0, 1, 0)).to.be(0);
expect(lerp(1, 11, 5)).to.be(51);
});
it('correctly interpolates floats', function () {
expect(lerp(0, 1, 0.5)).to.be(0.5);
expect(lerp(0.25, 0.75, 0.5)).to.be(0.5);
});
});
});

67
test/node/ol/obj.test.js Normal file
View File

@@ -0,0 +1,67 @@
import expect from '../expect.js';
import {assign, clear, getValues, isEmpty} from '../../../src/ol/obj.js';
describe('ol/obj.js', () => {
describe('assign()', function () {
it('is an alias for Object.assign() where available', function () {
if (typeof Object.assign === 'function') {
expect(assign).to.be(Object.assign);
}
});
it('assigns properties from a source object to a target object', function () {
const source = {
sourceProp1: 'sourceValue1',
sourceProp2: 'sourceValue2',
};
const target = {
sourceProp1: 'overridden',
targetProp1: 'targetValue1',
};
const assigned = assign(target, source);
expect(assigned).to.be(target);
expect(assigned.sourceProp1).to.be('sourceValue1');
expect(assigned.sourceProp2).to.be('sourceValue2');
expect(assigned.targetProp1).to.be('targetValue1');
});
it('throws a TypeError with `undefined` as target', function () {
expect(() => assign()).to.throwException(
/Cannot convert undefined or null to object/
);
});
it('throws a TypeError with `null` as target', function () {
expect(() => assign(null)).to.throwException(
/Cannot convert undefined or null to object/
);
});
});
describe('clear()', function () {
it('removes all properties from an object', function () {
expect(isEmpty(clear({foo: 'bar'}))).to.be(true);
expect(isEmpty(clear({foo: 'bar', num: 42}))).to.be(true);
expect(isEmpty(clear({}))).to.be(true);
expect(isEmpty(clear(null))).to.be(true);
});
});
describe('getValues()', function () {
it('gets a list of property values from an object', function () {
expect(getValues({foo: 'bar', num: 42}).sort()).to.eql([42, 'bar']);
expect(getValues([])).to.eql([]);
});
});
describe('isEmpty()', function () {
it('checks if an object has any properties', function () {
expect(isEmpty({})).to.be(true);
expect(isEmpty(null)).to.be(true);
expect(isEmpty({foo: 'bar'})).to.be(false);
expect(isEmpty({foo: false})).to.be(false);
});
});
});

871
test/node/ol/proj.test.js Normal file
View File

@@ -0,0 +1,871 @@
import Projection from '../../../src/ol/proj/Projection.js';
import Units from '../../../src/ol/proj/Units.js';
import expect from '../expect.js';
import proj4 from 'proj4';
import {HALF_SIZE} from '../../../src/ol/proj/epsg3857.js';
import {
METERS_PER_UNIT,
addCommon,
clearAllProjections,
clearUserProjection,
equivalent,
fromLonLat,
fromUserCoordinate,
fromUserExtent,
getPointResolution,
get as getProjection,
getTransform,
getTransformFromProjections,
getUserProjection,
setUserProjection,
toLonLat,
toUserCoordinate,
toUserExtent,
transform,
transformExtent,
useGeographic,
} from '../../../src/ol/proj.js';
import {METERS_PER_UNIT as metersPerDegree} from '../../../src/ol/proj/epsg4326.js';
import {register} from '../../../src/ol/proj/proj4.js';
describe('ol/proj.js', function () {
afterEach(function () {
clearAllProjections();
clearUserProjection();
addCommon();
});
describe('useGeographic()', function () {
it('sets the user projection to Geographic/WGS-84', function () {
useGeographic();
const projection = getUserProjection();
expect(projection).to.be(getProjection('EPSG:4326'));
});
});
describe('getUserProjection()', function () {
it('returns null by default', function () {
expect(getUserProjection()).to.be(null);
});
it('returns the user projection if set', function () {
const projection = getProjection('EPSG:4326');
setUserProjection(projection);
expect(getUserProjection()).to.be(projection);
});
});
describe('setUserProjection()', function () {
it('accepts a string identifier', function () {
const projection = getProjection('EPSG:4326');
setUserProjection('EPSG:4326');
expect(getUserProjection()).to.be(projection);
});
});
describe('clearUserProjection()', function () {
it('clears the user projection', function () {
useGeographic();
clearUserProjection();
expect(getUserProjection()).to.be(null);
});
});
describe('toUserCoordinate()', function () {
it('transforms a point to the user projection', function () {
useGeographic();
const coordinate = fromLonLat([-110, 45]);
const user = toUserCoordinate(coordinate, 'EPSG:3857');
const transformed = transform(coordinate, 'EPSG:3857', 'EPSG:4326');
expect(user).to.eql(transformed);
expect(user).not.to.eql(coordinate);
});
it('returns the original if no user projection is set', function () {
const coordinate = fromLonLat([-110, 45]);
const user = toUserCoordinate(coordinate, 'EPSG:3857');
expect(user).to.be(coordinate);
});
});
describe('fromUserCoordinate()', function () {
it('transforms a point from the user projection', function () {
useGeographic();
const user = [-110, 45];
const coordinate = fromUserCoordinate(user, 'EPSG:3857');
const transformed = transform(user, 'EPSG:4326', 'EPSG:3857');
expect(coordinate).to.eql(transformed);
expect(user).not.to.eql(coordinate);
});
it('returns the original if no user projection is set', function () {
const user = fromLonLat([-110, 45]);
const coordinate = fromUserCoordinate(user, 'EPSG:3857');
expect(coordinate).to.be(user);
});
});
describe('toUserExtent()', function () {
it('transforms an extent to the user projection', function () {
useGeographic();
const extent = transformExtent(
[-110, 45, -100, 50],
'EPSG:4326',
'EPSG:3857'
);
const user = toUserExtent(extent, 'EPSG:3857');
const transformed = transformExtent(extent, 'EPSG:3857', 'EPSG:4326');
expect(user).to.eql(transformed);
expect(user).not.to.eql(extent);
});
it('returns the original if no user projection is set', function () {
const extent = transformExtent(
[-110, 45, -100, 50],
'EPSG:4326',
'EPSG:3857'
);
const user = toUserExtent(extent, 'EPSG:3857');
expect(user).to.be(extent);
});
});
describe('fromUserExtent()', function () {
it('transforms an extent from the user projection', function () {
useGeographic();
const user = [-110, 45, -100, 50];
const extent = fromUserExtent(user, 'EPSG:3857');
const transformed = transformExtent(user, 'EPSG:4326', 'EPSG:3857');
expect(extent).to.eql(transformed);
expect(extent).not.to.eql(user);
});
it('returns the original if no user projection is set', function () {
const user = transformExtent(
[-110, 45, -100, 50],
'EPSG:4326',
'EPSG:3857'
);
const extent = fromUserExtent(user, 'EPSG:3857');
expect(extent).to.be(user);
});
});
describe('toLonLat()', function () {
const cases = [
{
from: [0, 0],
to: [0, 0],
},
{
from: [-12356463.478053365, 5700582.732404122],
to: [-111, 45.5],
},
{
from: [2 * HALF_SIZE - 12356463.478053365, 5700582.732404122],
to: [-111, 45.5],
},
{
from: [-4 * HALF_SIZE - 12356463.478053365, 5700582.732404122],
to: [-111, 45.5],
},
];
cases.forEach(function (c) {
it('works for ' + c.from.join(', '), function () {
const lonLat = toLonLat(c.from);
expect(lonLat[0]).to.roughlyEqual(c.to[0], 1e-9);
expect(lonLat[1]).to.roughlyEqual(c.to[1], 1e-9);
});
});
});
describe('projection equivalence', function () {
function _testAllEquivalent(codes) {
const projections = codes.map(getProjection);
projections.forEach(function (source) {
projections.forEach(function (destination) {
expect(equivalent(source, destination)).to.be.ok();
});
});
}
it('gives that 3857, 102100, 102113, 900913 are equivalent ', function () {
_testAllEquivalent([
'EPSG:3857',
'EPSG:102100',
'EPSG:102113',
'EPSG:900913',
]);
});
it('gives that custom 3413 is equivalent to self', function () {
const code = 'EPSG:3413';
const source = new Projection({
code: code,
});
const destination = new Projection({
code: code,
});
expect(equivalent(source, destination)).to.be.ok();
});
it('gives that default 3857 is equivalent to self', function () {
_testAllEquivalent(['EPSG:3857', 'EPSG:3857']);
});
it(
'gives that CRS:84, urn:ogc:def:crs:EPSG:6.6:4326, EPSG:4326, ' +
'urn:x-ogc:def:crs:EPSG:6.6:4326 are equivalent',
function () {
_testAllEquivalent([
'CRS:84',
'urn:ogc:def:crs:EPSG:6.6:4326',
'urn:x-ogc:def:crs:EPSG:6.6:4326',
'EPSG:4326',
]);
}
);
it('requires code and units to be equal for projection evquivalence', function () {
const proj1 = new Projection({
code: 'EPSG:3857',
units: 'm',
});
const proj2 = new Projection({
code: 'EPSG:3857',
units: 'tile-pixels',
});
expect(equivalent(proj1, proj2)).to.not.be.ok();
});
});
describe('identify transform', function () {
it('returns a new object, with same coord values', function () {
const epsg4326 = getProjection('EPSG:4326');
const uniqueObject = {};
const sourcePoint = [uniqueObject, uniqueObject];
const destinationPoint = transform(sourcePoint, epsg4326, epsg4326);
expect(sourcePoint === destinationPoint).to.not.be();
expect(destinationPoint[0] === sourcePoint[0]).to.be.ok();
expect(destinationPoint[1] === sourcePoint[1]).to.be.ok();
});
});
describe('transform 0,0 from 4326 to 3857', function () {
it('returns expected value', function () {
const point = transform([0, 0], 'EPSG:4326', 'EPSG:3857');
expect(point).not.to.be(undefined);
expect(point).not.to.be(null);
expect(point[1]).to.roughlyEqual(0, 1e-9);
});
});
describe('transform 0,0 from 3857 to 4326', function () {
it('returns expected value', function () {
const point = transform([0, 0], 'EPSG:3857', 'EPSG:4326');
expect(point).not.to.be(undefined);
expect(point).not.to.be(null);
expect(point[0]).to.eql(0);
expect(point[1]).to.eql(0);
});
});
describe('transform from 4326 to 3857 (Alastaira)', function () {
// https://alastaira.wordpress.com/2011/01/23/the-google-maps-bing-maps-spherical-mercator-projection/
it('returns expected value using ol.proj.transform', function () {
const point = transform(
[-5.625, 52.4827802220782],
'EPSG:4326',
'EPSG:900913'
);
expect(point).not.to.be(undefined);
expect(point).not.to.be(null);
expect(point[0]).to.roughlyEqual(-626172.13571216376, 1e-9);
expect(point[1]).to.roughlyEqual(6887893.4928337997, 1e-8);
});
it('returns expected value using ol.proj.fromLonLat', function () {
const point = fromLonLat([-5.625, 52.4827802220782]);
expect(point).not.to.be(undefined);
expect(point).not.to.be(null);
expect(point[0]).to.roughlyEqual(-626172.13571216376, 1e-9);
expect(point[1]).to.roughlyEqual(6887893.4928337997, 1e-8);
});
});
describe('transform from 3857 to 4326 (Alastaira)', function () {
// https://alastaira.wordpress.com/2011/01/23/the-google-maps-bing-maps-spherical-mercator-projection/
it('returns expected value using ol.proj.transform', function () {
const point = transform(
[-626172.13571216376, 6887893.4928337997],
'EPSG:900913',
'EPSG:4326'
);
expect(point).not.to.be(undefined);
expect(point).not.to.be(null);
expect(point[0]).to.roughlyEqual(-5.625, 1e-9);
expect(point[1]).to.roughlyEqual(52.4827802220782, 1e-9);
});
it('returns expected value using ol.proj.toLonLat', function () {
const point = toLonLat([-626172.13571216376, 6887893.4928337997]);
expect(point).not.to.be(undefined);
expect(point).not.to.be(null);
expect(point[0]).to.roughlyEqual(-5.625, 1e-9);
expect(point[1]).to.roughlyEqual(52.4827802220782, 1e-9);
});
});
describe('canWrapX()', function () {
it('requires an extent for allowing wrapX', function () {
let proj = new Projection({
code: 'foo',
global: true,
});
expect(proj.canWrapX()).to.be(false);
proj.setExtent([1, 2, 3, 4]);
expect(proj.canWrapX()).to.be(true);
proj = new Projection({
code: 'foo',
global: true,
extent: [1, 2, 3, 4],
});
expect(proj.canWrapX()).to.be(true);
proj.setExtent(null);
expect(proj.canWrapX()).to.be(false);
});
it('requires global to be true for allowing wrapX', function () {
let proj = new Projection({
code: 'foo',
extent: [1, 2, 3, 4],
});
expect(proj.canWrapX()).to.be(false);
proj.setGlobal(true);
expect(proj.canWrapX()).to.be(true);
proj = new Projection({
code: 'foo',
global: true,
extent: [1, 2, 3, 4],
});
expect(proj.canWrapX()).to.be(true);
proj.setGlobal(false);
expect(proj.canWrapX()).to.be(false);
});
});
describe('transformExtent()', function () {
it('transforms an extent given projection identifiers', function () {
const sourceExtent = [-15, -30, 45, 60];
const destinationExtent = transformExtent(
sourceExtent,
'EPSG:4326',
'EPSG:3857'
);
expect(destinationExtent).not.to.be(undefined);
expect(destinationExtent).not.to.be(null);
expect(destinationExtent[0]).to.roughlyEqual(-1669792.3618991037, 1e-9);
expect(destinationExtent[2]).to.roughlyEqual(5009377.085697311, 1e-9);
expect(destinationExtent[1]).to.roughlyEqual(-3503549.843504376, 1e-8);
expect(destinationExtent[3]).to.roughlyEqual(8399737.889818361, 1e-8);
});
});
describe('getPointResolution()', function () {
it('returns the correct point resolution for EPSG:4326', function () {
let pointResolution = getPointResolution('EPSG:4326', 1, [0, 0]);
expect(pointResolution).to.be(1);
pointResolution = getPointResolution('EPSG:4326', 1, [0, 52]);
expect(pointResolution).to.be(1);
});
it('returns the correct point resolution for EPSG:4326 with custom units', function () {
let pointResolution = getPointResolution('EPSG:4326', 1, [0, 0], 'm');
expect(pointResolution).to.roughlyEqual(111195.0802335329, 1e-5);
pointResolution = getPointResolution('EPSG:4326', 1, [0, 52], 'm');
expect(pointResolution).to.roughlyEqual(89826.53390979706, 1e-5);
});
it('returns the correct point resolution for EPSG:3857', function () {
let pointResolution = getPointResolution('EPSG:3857', 1, [0, 0]);
expect(pointResolution).to.be(1);
pointResolution = getPointResolution('EPSG:3857', 1, fromLonLat([0, 52]));
expect(pointResolution).to.roughlyEqual(0.615661, 1e-5);
});
it('returns the correct point resolution for EPSG:3857 with custom units', function () {
let pointResolution = getPointResolution(
'EPSG:3857',
METERS_PER_UNIT['degrees'],
[0, 0],
'degrees'
);
expect(pointResolution).to.be(1);
pointResolution = getPointResolution(
'EPSG:4326',
1,
fromLonLat([0, 52]),
'degrees'
);
expect(pointResolution).to.be(1);
});
it('returns the nominal resolution for projections without transforms', function () {
const projection = new Projection({
code: 'foo',
units: 'ft',
});
let pointResolution = getPointResolution(projection, 2, [0, 0]);
expect(pointResolution).to.be(2);
pointResolution = getPointResolution(projection, 2, [0, 0], 'm');
expect(pointResolution).to.be(0.6096);
});
});
describe('Proj4js integration', function () {
afterEach(function () {
delete proj4.defs['EPSG:21781'];
clearAllProjections();
addCommon();
});
it('creates ol.proj.Projection instance from EPSG:21781', function () {
proj4.defs(
'EPSG:21781',
'+proj=somerc +lat_0=46.95240555555556 +lon_0=7.439583333333333 ' +
'+k_0=1 +x_0=600000 +y_0=200000 +ellps=bessel ' +
'+towgs84=674.374,15.056,405.346,0,0,0,0 +units=m +no_defs'
);
register(proj4);
const proj = getProjection('EPSG:21781');
expect(proj.getCode()).to.eql('EPSG:21781');
expect(proj.getUnits()).to.eql('m');
expect(proj.getMetersPerUnit()).to.eql(1);
});
it('creates ol.proj.Projection instance from EPSG:3739', function () {
proj4.defs(
'EPSG:3739',
'+proj=tmerc +lat_0=40.5 +lon_0=-110.0833333333333 +k=0.9999375 ' +
'+x_0=800000.0000101599 +y_0=99999.99998983997 +ellps=GRS80 ' +
'+towgs84=0,0,0,0,0,0,0 +units=us-ft +no_defs'
);
register(proj4);
const proj = getProjection('EPSG:3739');
expect(proj.getCode()).to.eql('EPSG:3739');
expect(proj.getUnits()).to.eql('us-ft');
expect(proj.getMetersPerUnit()).to.eql(1200 / 3937);
delete proj4.defs['EPSG:3739'];
});
it('creates ol.proj.Projection instance from EPSG:4258', function () {
proj4.defs(
'EPSG:4258',
'+proj=longlat +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +no_defs'
);
register(proj4);
const proj = getProjection('EPSG:4258');
expect(proj.getCode()).to.eql('EPSG:4258');
expect(proj.getUnits()).to.eql('degrees');
expect(proj.getMetersPerUnit()).to.eql(METERS_PER_UNIT[Units.DEGREES]);
delete proj4.defs['EPSG:4258'];
});
it('allows Proj4js projections to be used transparently', function () {
register(proj4);
const point = transform(
[-626172.13571216376, 6887893.4928337997],
'GOOGLE',
'WGS84'
);
expect(point[0]).to.roughlyEqual(-5.625, 1e-9);
expect(point[1]).to.roughlyEqual(52.4827802220782, 1e-9);
});
it('allows new Proj4js projections to be defined', function () {
proj4.defs(
'EPSG:21781',
'+proj=somerc +lat_0=46.95240555555556 +lon_0=7.439583333333333 ' +
'+k_0=1 +x_0=600000 +y_0=200000 +ellps=bessel ' +
'+towgs84=674.374,15.056,405.346,0,0,0,0 +units=m +no_defs'
);
register(proj4);
const point = transform(
[7.439583333333333, 46.95240555555556],
'EPSG:4326',
'EPSG:21781'
);
expect(point[0]).to.roughlyEqual(600072.3, 1);
expect(point[1]).to.roughlyEqual(200146.976, 1);
});
it('works with ol.proj.fromLonLat and ol.proj.toLonLat', function () {
proj4.defs(
'EPSG:21781',
'+proj=somerc +lat_0=46.95240555555556 +lon_0=7.439583333333333 ' +
'+k_0=1 +x_0=600000 +y_0=200000 +ellps=bessel ' +
'+towgs84=674.374,15.056,405.346,0,0,0,0 +units=m +no_defs'
);
register(proj4);
const lonLat = [7.439583333333333, 46.95240555555556];
let point = fromLonLat(lonLat, 'EPSG:21781');
expect(point[0]).to.roughlyEqual(600072.3, 1);
expect(point[1]).to.roughlyEqual(200146.976, 1);
point = toLonLat(point, 'EPSG:21781');
expect(point[0]).to.roughlyEqual(lonLat[0], 1);
expect(point[1]).to.roughlyEqual(lonLat[1], 1);
});
it('caches the new Proj4js projections given their srsCode', function () {
proj4.defs(
'EPSG:21781',
'+proj=somerc +lat_0=46.95240555555556 +lon_0=7.439583333333333 ' +
'+k_0=1 +x_0=600000 +y_0=200000 +ellps=bessel ' +
'+towgs84=674.374,15.056,405.346,0,0,0,0 +units=m +no_defs'
);
const code = 'urn:ogc:def:crs:EPSG:21781';
const srsCode = 'EPSG:21781';
proj4.defs(code, proj4.defs(srsCode));
register(proj4);
const proj = getProjection(code);
const proj2 = getProjection(srsCode);
expect(equivalent(proj2, proj)).to.be(true);
delete proj4.defs[code];
});
it('numerically estimates point scale at the equator', function () {
register(proj4);
const googleProjection = getProjection('GOOGLE');
expect(getPointResolution(googleProjection, 1, [0, 0])).to.roughlyEqual(
1,
1e-1
);
});
it('numerically estimates point scale at various latitudes', function () {
register(proj4);
const epsg3857Projection = getProjection('EPSG:3857');
const googleProjection = getProjection('GOOGLE');
let point, y;
for (y = -20; y <= 20; ++y) {
point = [0, 1000000 * y];
expect(getPointResolution(googleProjection, 1, point)).to.roughlyEqual(
getPointResolution(epsg3857Projection, 1, point),
1e-1
);
}
});
it('numerically estimates point scale at various points', function () {
register(proj4);
const epsg3857Projection = getProjection('EPSG:3857');
const googleProjection = getProjection('GOOGLE');
let point, x, y;
for (x = -20; x <= 20; x += 2) {
for (y = -20; y <= 20; y += 2) {
point = [1000000 * x, 1000000 * y];
expect(
getPointResolution(googleProjection, 1, point)
).to.roughlyEqual(
getPointResolution(epsg3857Projection, 1, point),
1e-1
);
}
}
});
it('does not overwrite existing projections in the registry', function () {
register(proj4);
const epsg4326 = getProjection('EPSG:4326');
new Projection({
code: 'EPSG:4326',
units: 'degrees',
extent: [-45, -45, 45, 45],
});
expect(getProjection('EPSG:4326')).to.equal(epsg4326);
});
it('uses safe transform functions', function () {
register(proj4);
const wgs84 = getProjection('WGS84');
const epsg4326 = getProjection('EPSG:4326');
wgs84.setExtent(epsg4326.getExtent());
wgs84.setGlobal(true);
const google = getProjection('GOOGLE');
const epsg3857 = getProjection('EPSG:3857');
google.setExtent(epsg3857.getExtent());
google.setGlobal(true);
const coord = [-190, 85];
let expected = transform(coord, wgs84, google);
let got = transform(coord, epsg4326, epsg3857);
expect(got[0]).to.roughlyEqual(expected[0], 1e-7);
expect(got[1]).to.roughlyEqual(expected[1], 1e-7);
expected = transform(expected, google, wgs84);
got = transform(got, epsg3857, epsg4326);
expect(got[0]).to.roughlyEqual(expected[0], 1e-7);
expect(got[1]).to.roughlyEqual(expected[1], 1e-7);
});
});
describe('ol.proj.getTransformFromProjections()', function () {
beforeEach(function () {
register(proj4);
});
it('returns a transform function', function () {
const transform = getTransformFromProjections(
getProjection('GOOGLE'),
getProjection('EPSG:4326')
);
expect(typeof transform).to.be('function');
const output = transform([-12000000, 5000000]);
expect(output[0]).to.roughlyEqual(-107.79783409434258, 1e-9);
expect(output[1]).to.roughlyEqual(40.91627447067577, 1e-9);
});
it('works for longer arrays', function () {
const transform = getTransformFromProjections(
getProjection('GOOGLE'),
getProjection('EPSG:4326')
);
expect(typeof transform).to.be('function');
const output = transform([-12000000, 5000000, -12000000, 5000000]);
expect(output[0]).to.roughlyEqual(-107.79783409434258, 1e-9);
expect(output[1]).to.roughlyEqual(40.91627447067577, 1e-9);
expect(output[2]).to.roughlyEqual(-107.79783409434258, 1e-9);
expect(output[3]).to.roughlyEqual(40.91627447067577, 1e-9);
});
});
describe('ol.proj.getTransform()', function () {
beforeEach(function () {
register(proj4);
});
it('returns a function', function () {
const transform = getTransform('GOOGLE', 'EPSG:4326');
expect(typeof transform).to.be('function');
});
it('returns a transform function', function () {
const transform = getTransform('GOOGLE', 'EPSG:4326');
expect(typeof transform).to.be('function');
const output = transform([-626172.13571216376, 6887893.4928337997]);
expect(output[0]).to.roughlyEqual(-5.625, 1e-9);
expect(output[1]).to.roughlyEqual(52.4827802220782, 1e-9);
});
it('works for longer arrays of coordinate values', function () {
const transform = getTransform('GOOGLE', 'EPSG:4326');
expect(typeof transform).to.be('function');
const output = transform([
-626172.13571216376,
6887893.4928337997,
-12000000,
5000000,
-626172.13571216376,
6887893.4928337997,
]);
expect(output[0]).to.roughlyEqual(-5.625, 1e-9);
expect(output[1]).to.roughlyEqual(52.4827802220782, 1e-9);
expect(output[2]).to.roughlyEqual(-107.79783409434258, 1e-9);
expect(output[3]).to.roughlyEqual(40.91627447067577, 1e-9);
expect(output[4]).to.roughlyEqual(-5.625, 1e-9);
expect(output[5]).to.roughlyEqual(52.4827802220782, 1e-9);
});
it('accepts an optional destination array', function () {
const transform = getTransform('EPSG:3857', 'EPSG:4326');
const input = [-12000000, 5000000];
const output = [];
const got = transform(input, output);
expect(got).to.be(output);
expect(output[0]).to.roughlyEqual(-107.79783409434258, 1e-9);
expect(output[1]).to.roughlyEqual(40.91627447067577, 1e-9);
expect(input).to.eql([-12000000, 5000000]);
});
it('accepts a dimension', function () {
const transform = getTransform('GOOGLE', 'EPSG:4326');
expect(typeof transform).to.be('function');
const dimension = 3;
const output = transform(
[
-626172.13571216376,
6887893.4928337997,
100,
-12000000,
5000000,
200,
-626172.13571216376,
6887893.4928337997,
300,
],
undefined,
dimension
);
expect(output[0]).to.roughlyEqual(-5.625, 1e-9);
expect(output[1]).to.roughlyEqual(52.4827802220782, 1e-9);
expect(output[2]).to.be(100);
expect(output[3]).to.roughlyEqual(-107.79783409434258, 1e-9);
expect(output[4]).to.roughlyEqual(40.91627447067577, 1e-9);
expect(output[5]).to.be(200);
expect(output[6]).to.roughlyEqual(-5.625, 1e-9);
expect(output[7]).to.roughlyEqual(52.4827802220782, 1e-9);
expect(output[8]).to.be(300);
});
});
describe('ol.proj.transform()', function () {
it('transforms a 2d coordinate', function () {
const got = transform([-10, -20], 'EPSG:4326', 'EPSG:3857');
expect(got).to.have.length(2);
expect(got[0]).to.roughlyEqual(-1113194.9079327357, 1e-3);
expect(got[1]).to.roughlyEqual(-2273030.92698769, 1e-3);
});
it('transforms a 3d coordinate', function () {
const got = transform([-10, -20, 3], 'EPSG:4326', 'EPSG:3857');
expect(got).to.have.length(3);
expect(got[0]).to.roughlyEqual(-1113194.9079327357, 1e-3);
expect(got[1]).to.roughlyEqual(-2273030.92698769, 1e-3);
expect(got[2]).to.be(3);
});
it('transforms a 4d coordinate', function () {
const got = transform([-10, -20, 3, 4], 'EPSG:4326', 'EPSG:3857');
expect(got).to.have.length(4);
expect(got[0]).to.roughlyEqual(-1113194.9079327357, 1e-3);
expect(got[1]).to.roughlyEqual(-2273030.92698769, 1e-3);
expect(got[2]).to.be(3);
expect(got[3]).to.be(4);
});
it('works with 3d points and proj4 defs', function () {
proj4.defs(
'custom',
'+proj=somerc +lat_0=46.95240555555556 +lon_0=7.439583333333333 ' +
'+k_0=1 +x_0=600000 +y_0=200000 +ellps=bessel ' +
'+towgs84=674.374,15.056,405.346,0,0,0,0 +units=m +no_defs'
);
register(proj4);
const got = transform([-111, 45.5, 123], 'EPSG:4326', 'custom');
expect(got).to.have.length(3);
expect(got[0]).to.roughlyEqual(-6601512.194209638, 1);
expect(got[1]).to.roughlyEqual(6145843.802742112, 1);
expect(got[2]).to.be(123);
delete proj4.defs.custom;
clearAllProjections();
addCommon();
});
it('does not flip axis order', function () {
proj4.defs('enu', '+proj=longlat');
proj4.defs('neu', '+proj=longlat +axis=neu');
register(proj4);
const got = transform([1, 2], 'neu', 'enu');
expect(got).to.eql([1, 2]);
delete proj4.defs.enu;
delete proj4.defs.neu;
clearAllProjections();
addCommon();
});
});
describe('ol.proj.Projection.prototype.getMetersPerUnit()', function () {
beforeEach(function () {
proj4.defs(
'EPSG:26782',
'+proj=lcc +lat_1=29.3 +lat_2=30.7 +lat_0=28.66666666666667 ' +
'+lon_0=-91.33333333333333 +x_0=609601.2192024384 +y_0=0 ' +
'+ellps=clrk66 +datum=NAD27 +to_meter=0.3048006096012192 +no_defs'
);
proj4.defs(
'EPSG:3739',
'+proj=tmerc +lat_0=40.5 ' +
'+lon_0=-110.0833333333333 +k=0.9999375 +x_0=800000.0000101599 ' +
'+y_0=99999.99998983997 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 ' +
'+units=us-ft +no_defs'
);
proj4.defs(
'EPSG:4269',
'GEOGCS["NAD83",' +
'DATUM["North_American_Datum_1983",' +
'SPHEROID["GRS 1980",6378137,298.257222101,' +
'AUTHORITY["EPSG","7019"]],' +
'AUTHORITY["EPSG","6269"]],' +
'PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],' +
'UNIT["degree",0.01745329251994328,AUTHORITY["EPSG","9122"]],' +
'AUTHORITY["EPSG","4269"]]'
);
proj4.defs(
'EPSG:4279',
'GEOGCS["OS(SN)80",DATUM["OS_SN_1980",' +
'SPHEROID["Airy 1830",6377563.396,299.3249646,' +
'AUTHORITY["EPSG","7001"]],' +
'AUTHORITY["EPSG","6279"]],' +
'PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],' +
'UNIT["degree",0.01745329251994328,AUTHORITY["EPSG","9122"]],' +
'AUTHORITY["EPSG","4279"]]'
);
register(proj4);
});
afterEach(function () {
delete proj4.defs['EPSG:26782'];
delete proj4.defs['EPSG:3739'];
delete proj4.defs['EPSG:4269'];
delete proj4.defs['EPSG:4279'];
clearAllProjections();
addCommon();
});
it('returns value in meters', function () {
const epsg4326 = getProjection('EPSG:4326');
expect(epsg4326.getMetersPerUnit()).to.eql(metersPerDegree);
});
it('works for proj4js projections without units', function () {
const epsg26782 = getProjection('EPSG:26782');
expect(epsg26782.getMetersPerUnit()).to.eql(0.3048006096012192);
});
it('works for proj4js projections with units other than m', function () {
const epsg3739 = getProjection('EPSG:3739');
expect(epsg3739.getMetersPerUnit()).to.eql(1200 / 3937);
});
it('works for proj4js OGC WKT GEOGCS projections', function () {
const epsg4269 = getProjection('EPSG:4269');
expect(epsg4269.getMetersPerUnit()).to.eql(6378137 * 0.01745329251994328);
const epsg4279 = getProjection('EPSG:4279');
expect(epsg4279.getMetersPerUnit()).to.eql(
6377563.396 * 0.01745329251994328
);
});
});
});

View File

@@ -0,0 +1,266 @@
import LinkedList from '../../../../src/ol/structs/LinkedList.js';
import expect from '../../expect.js';
describe('ol/structs/LinkedList.js', function () {
let ll;
const item = {};
const item2 = {};
beforeEach(function () {
ll = new LinkedList();
});
it('defaults to circular', function () {
expect(ll.circular_).to.be(true);
});
it('creates an empty list', function () {
expect(ll.length_).to.be(0);
expect(ll.first_).to.be(undefined);
expect(ll.last_).to.be(undefined);
expect(ll.head_).to.be(undefined);
});
describe('#insertItem', function () {
beforeEach(function () {
ll.insertItem(item);
});
it('inserts an item into the list', function () {
expect(ll.length_).to.be(1);
});
it('sets the cursor to the inserted item', function () {
expect(ll.head_.data).to.be(item);
});
it('links the previous item to the new one', function () {
ll.insertItem(item2);
expect(ll.head_.prev.data).to.be(item);
expect(ll.head_.prev.next.data).to.be(item2);
});
});
describe('#removeItem', function () {
const item3 = {};
beforeEach(function () {
ll.insertItem(item);
ll.insertItem(item2);
ll.insertItem(item3);
});
it('removes the current item', function () {
ll.removeItem();
expect(ll.length_).to.be(2);
expect(ll.head_.data).not.to.be(item3);
});
it('sets the cursor to the next item if possible', function () {
ll.removeItem();
expect(ll.head_.data).to.be(item);
});
it('otherwise sets the cursor to the previous item', function () {
ll = new LinkedList(false);
ll.insertItem(item);
ll.insertItem(item2);
ll.insertItem(item3);
ll.removeItem();
expect(ll.head_.data).to.be(item2);
});
it('empties a list with only one item', function () {
ll = new LinkedList();
ll.insertItem(item);
ll.removeItem();
expect(ll.length_).to.be(0);
expect(ll.head_).to.be(undefined);
expect(ll.first_).to.be(undefined);
expect(ll.last_).to.be(undefined);
});
});
describe('#firstItem', function () {
it('sets the cursor to the first item and returns its data', function () {
ll.insertItem(item);
ll.insertItem(item2);
const i = ll.firstItem();
expect(i).to.be(item);
expect(ll.head_.data).to.be(item);
});
it('returns undefined on empty list', function () {
const i = ll.firstItem();
expect(i).to.be(undefined);
});
});
describe('#lastItem', function () {
it('sets the cursor to the last item and returns its data', function () {
ll.insertItem(item);
ll.insertItem(item2);
ll.firstItem();
const i = ll.lastItem();
expect(i).to.be(item2);
expect(ll.head_.data).to.be(item2);
});
it('returns undefined on empty list', function () {
const i = ll.lastItem();
expect(i).to.be(undefined);
});
});
describe('#nextItem', function () {
it('sets the cursor to the next item and returns its data', function () {
ll.insertItem(item);
ll.insertItem(item2);
ll.firstItem();
const i = ll.nextItem();
expect(i).to.be(item2);
expect(ll.head_.data).to.be(item2);
});
it('returns undefined on empty list', function () {
const i = ll.nextItem();
expect(i).to.be(undefined);
});
});
describe('#prevItem', function () {
it('sets the cursor to the previous item and returns its data', function () {
ll.insertItem(item);
ll.insertItem(item2);
const i = ll.prevItem();
expect(i).to.be(item);
expect(ll.head_.data).to.be(item);
});
it('returns undefined on empty list', function () {
const i = ll.prevItem();
expect(i).to.be(undefined);
});
});
describe('#getNextItem', function () {
it('returns the data of the next item without stepping the cursor', function () {
ll.insertItem(item);
ll.insertItem(item2);
ll.firstItem();
const i = ll.getNextItem();
expect(i).to.be(item2);
expect(ll.head_.data).to.be(item);
});
it('returns undefined on empty list', function () {
const i = ll.getNextItem();
expect(i).to.be(undefined);
});
});
describe('#getPrevItem', function () {
it('returns the data of the previous item without stepping the cursor', function () {
ll.insertItem(item);
ll.insertItem(item2);
const i = ll.getPrevItem();
expect(i).to.be(item);
expect(ll.head_.data).to.be(item2);
});
it('returns undefined on empty list', function () {
const i = ll.getPrevItem();
expect(i).to.be(undefined);
});
});
describe('#getCurrItem', function () {
it('returns the data of the current item', function () {
const item3 = {};
ll.insertItem(item);
ll.insertItem(item2);
ll.insertItem(item3);
ll.prevItem();
const i = ll.getCurrItem();
expect(i).to.be(item2);
expect(ll.head_.data).to.be(item2);
});
it('returns undefined on empty list', function () {
const i = ll.getCurrItem();
expect(i).to.be(undefined);
});
});
describe('#getLength', function () {
it('returns the length of the list', function () {
ll.insertItem(item);
ll.insertItem(item2);
const l = ll.getLength();
expect(l).to.be(2);
});
});
describe('#concat', function () {
let ll2, item3;
beforeEach(function () {
item3 = {};
ll2 = new LinkedList();
ll2.insertItem(item);
ll2.insertItem(item2);
ll2.insertItem(item3);
});
it('concatenates a second list with the current one', function () {
const item4 = {};
const item5 = {};
const item6 = {};
ll.insertItem(item4);
ll.insertItem(item5);
ll.insertItem(item6);
ll.prevItem();
ll.concat(ll2);
expect(ll.length_).to.be(6);
expect(ll.head_.data).to.be(item5);
expect(ll.head_.next.data).to.be(item);
expect(ll.head_.next.next.next.next.data).to.be(item6);
});
it('receives the second list if the current one is empty', function () {
ll.concat(ll2);
expect(ll.length_).to.be(3);
expect(ll.first_.data).to.be(item);
expect(ll.last_.data).to.be(item3);
expect(ll.head_.data).to.be(item3);
});
it('destroys the second list', function () {
ll.concat(ll2);
expect(ll2.length_).to.be(0);
expect(ll2.first_).to.be(undefined);
expect(ll2.last_).to.be(undefined);
expect(ll2.head_).to.be(undefined);
});
});
describe('when circular', function () {
beforeEach(function () {
ll = new LinkedList();
ll.insertItem(item);
});
describe('#insertItem', function () {
it('initializes the list in a circular way', function () {
expect(ll.head_.prev.data).to.be(item);
expect(ll.head_.next.data).to.be(item);
});
});
describe('#setFirstItem', function () {
it('resets the first item to the current one', function () {
ll.insertItem(item2);
ll.setFirstItem();
expect(ll.first_.data).to.be(item2);
expect(ll.last_.data).to.be(item);
});
});
});
});

View File

@@ -0,0 +1,293 @@
import LRUCache from '../../../../src/ol/structs/LRUCache.js';
import expect from '../../expect.js';
describe('ol/structs/LRUCache.js', function () {
let lruCache;
function fillLRUCache(lruCache) {
lruCache.set('a', 0);
lruCache.set('b', 1);
lruCache.set('c', 2);
lruCache.set('d', 3);
}
beforeEach(function () {
lruCache = new LRUCache();
});
describe('empty cache', function () {
it('has size zero', function () {
expect(lruCache.getCount()).to.eql(0);
});
it('has no keys', function () {
expect(lruCache.getKeys()).to.eql([]);
});
it('has no values', function () {
expect(lruCache.getValues()).to.eql([]);
});
});
describe('populating', function () {
it('returns the correct size', function () {
fillLRUCache(lruCache);
expect(lruCache.getCount()).to.eql(4);
});
it('contains the correct keys in the correct order', function () {
fillLRUCache(lruCache);
expect(lruCache.getKeys()).to.eql(['d', 'c', 'b', 'a']);
});
it('contains the correct values in the correct order', function () {
fillLRUCache(lruCache);
expect(lruCache.getValues()).to.eql([3, 2, 1, 0]);
});
it('reports which keys are contained', function () {
fillLRUCache(lruCache);
expect(lruCache.containsKey('a')).to.be.ok();
expect(lruCache.containsKey('b')).to.be.ok();
expect(lruCache.containsKey('c')).to.be.ok();
expect(lruCache.containsKey('d')).to.be.ok();
expect(lruCache.containsKey('e')).to.not.be();
});
});
describe('getting the oldest key', function () {
it('moves the key to newest position', function () {
fillLRUCache(lruCache);
lruCache.get('a');
expect(lruCache.getCount()).to.eql(4);
expect(lruCache.getKeys()).to.eql(['a', 'd', 'c', 'b']);
expect(lruCache.getValues()).to.eql([0, 3, 2, 1]);
});
});
describe('getting a key in the middle', function () {
it('moves the key to newest position', function () {
fillLRUCache(lruCache);
lruCache.get('b');
expect(lruCache.getCount()).to.eql(4);
expect(lruCache.getKeys()).to.eql(['b', 'd', 'c', 'a']);
expect(lruCache.getValues()).to.eql([1, 3, 2, 0]);
});
});
describe('getting the newest key', function () {
it('maintains the key to newest position', function () {
fillLRUCache(lruCache);
lruCache.get('d');
expect(lruCache.getCount()).to.eql(4);
expect(lruCache.getKeys()).to.eql(['d', 'c', 'b', 'a']);
expect(lruCache.getValues()).to.eql([3, 2, 1, 0]);
});
});
describe('replacing value of a key', function () {
it('moves the key to newest position', function () {
fillLRUCache(lruCache);
lruCache.replace('b', 4);
expect(lruCache.getCount()).to.eql(4);
expect(lruCache.getKeys()).to.eql(['b', 'd', 'c', 'a']);
expect(lruCache.getValues()).to.eql([4, 3, 2, 0]);
});
});
describe('setting a new value', function () {
it('adds it as the newest value', function () {
fillLRUCache(lruCache);
lruCache.set('e', 4);
expect(lruCache.getKeys()).to.eql(['e', 'd', 'c', 'b', 'a']);
expect(lruCache.getValues()).to.eql([4, 3, 2, 1, 0]);
});
});
describe('setting an existing value', function () {
it('raises an exception', function () {
fillLRUCache(lruCache);
expect(function () {
lruCache.set('a', 0);
}).to.throwException();
});
});
describe('disallowed keys', function () {
it('setting raises an exception', function () {
expect(function () {
lruCache.set('constructor', 0);
}).to.throwException();
expect(function () {
lruCache.set('hasOwnProperty', 0);
}).to.throwException();
expect(function () {
lruCache.set('isPrototypeOf', 0);
}).to.throwException();
expect(function () {
lruCache.set('propertyIsEnumerable', 0);
}).to.throwException();
expect(function () {
lruCache.set('toLocaleString', 0);
}).to.throwException();
expect(function () {
lruCache.set('toString', 0);
}).to.throwException();
expect(function () {
lruCache.set('valueOf', 0);
}).to.throwException();
});
it('getting returns false', function () {
expect(lruCache.containsKey('constructor')).to.not.be();
expect(lruCache.containsKey('hasOwnProperty')).to.not.be();
expect(lruCache.containsKey('isPrototypeOf')).to.not.be();
expect(lruCache.containsKey('propertyIsEnumerable')).to.not.be();
expect(lruCache.containsKey('toLocaleString')).to.not.be();
expect(lruCache.containsKey('toString')).to.not.be();
expect(lruCache.containsKey('valueOf')).to.not.be();
});
});
describe('popping a value', function () {
it('returns the least-recent-used value', function () {
fillLRUCache(lruCache);
expect(lruCache.pop()).to.eql(0);
expect(lruCache.getCount()).to.eql(3);
expect(lruCache.containsKey('a')).to.not.be();
expect(lruCache.pop()).to.eql(1);
expect(lruCache.getCount()).to.eql(2);
expect(lruCache.containsKey('b')).to.not.be();
expect(lruCache.pop()).to.eql(2);
expect(lruCache.getCount()).to.eql(1);
expect(lruCache.containsKey('c')).to.not.be();
expect(lruCache.pop()).to.eql(3);
expect(lruCache.getCount()).to.eql(0);
expect(lruCache.containsKey('d')).to.not.be();
});
});
describe('#peekFirstKey()', function () {
it('returns the newest key in the cache', function () {
const cache = new LRUCache();
cache.set('oldest', 'oldest');
cache.set('oldish', 'oldish');
cache.set('newish', 'newish');
cache.set('newest', 'newest');
expect(cache.peekFirstKey()).to.eql('newest');
});
it('works if the cache has one item', function () {
const cache = new LRUCache();
cache.set('key', 'value');
expect(cache.peekFirstKey()).to.eql('key');
});
it('throws if the cache is empty', function () {
const cache = new LRUCache();
expect(function () {
cache.peekFirstKey();
}).to.throwException();
});
});
describe('peeking at the last value', function () {
it('returns the last key', function () {
fillLRUCache(lruCache);
expect(lruCache.peekLast()).to.eql(0);
});
it('throws an exception when the cache is empty', function () {
expect(function () {
lruCache.peekLast();
}).to.throwException();
});
});
describe('peeking at the last key', function () {
it('returns the last key', function () {
fillLRUCache(lruCache);
expect(lruCache.peekLastKey()).to.eql('a');
});
it('throws an exception when the cache is empty', function () {
expect(function () {
lruCache.peekLastKey();
}).to.throwException();
});
});
describe('#remove()', function () {
it('removes an item from the cache', function () {
const cache = new LRUCache();
cache.set('oldest', 'oldest');
cache.set('oldish', 'oldish');
cache.set('newish', 'newish');
cache.set('newest', 'newest');
cache.remove('oldish');
expect(cache.getCount()).to.eql(3);
expect(cache.getValues()).to.eql(['newest', 'newish', 'oldest']);
});
it('works when removing the oldest item', function () {
const cache = new LRUCache();
cache.set('oldest', 'oldest');
cache.set('oldish', 'oldish');
cache.set('newish', 'newish');
cache.set('newest', 'newest');
cache.remove('oldest');
expect(cache.getCount()).to.eql(3);
expect(cache.peekLastKey()).to.eql('oldish');
expect(cache.getValues()).to.eql(['newest', 'newish', 'oldish']);
});
it('works when removing the newest item', function () {
const cache = new LRUCache();
cache.set('oldest', 'oldest');
cache.set('oldish', 'oldish');
cache.set('newish', 'newish');
cache.set('newest', 'newest');
cache.remove('newest');
expect(cache.getCount()).to.eql(3);
expect(cache.peekFirstKey()).to.eql('newish');
expect(cache.getValues()).to.eql(['newish', 'oldish', 'oldest']);
});
it('returns the removed item', function () {
const cache = new LRUCache();
const item = {};
cache.set('key', item);
const returned = cache.remove('key');
expect(returned).to.be(item);
});
it('throws if the key does not exist', function () {
const cache = new LRUCache();
cache.set('foo', 'foo');
cache.set('bar', 'bar');
const call = function () {
cache.remove('bam');
};
expect(call).to.throwException();
});
});
describe('clearing the cache', function () {
it('clears the cache', function () {
fillLRUCache(lruCache);
lruCache.clear();
expect(lruCache.getCount()).to.eql(0);
expect(lruCache.getKeys()).to.eql([]);
expect(lruCache.getValues()).to.eql([]);
});
});
describe('setting the cache size', function () {
it('sets the cache size', function () {
lruCache.setSize(2);
expect(lruCache.highWaterMark).to.be(2);
fillLRUCache(lruCache);
while (lruCache.canExpireCache()) {
lruCache.pop();
}
expect(lruCache.getKeys().length).to.be(2);
});
});
});

View File

@@ -0,0 +1,157 @@
import PriorityQueue, {DROP} from '../../../../src/ol/structs/PriorityQueue.js';
import expect from '../../expect.js';
describe('ol/structs/PriorityQueue.js', function () {
const identity = function (a) {
return a;
};
describe('when empty', function () {
let pq;
beforeEach(function () {
pq = new PriorityQueue(identity, identity);
});
it('is empty', function () {
expect(pq.isEmpty()).to.be(true);
});
it('enqueue adds an element', function () {
const added = pq.enqueue(0);
expect(added).to.be(true);
expect(pq.elements_).to.eql([0]);
expect(pq.priorities_).to.eql([0]);
});
it('do not enqueue element with DROP priority', function () {
const added = pq.enqueue(Infinity);
expect(added).to.be(false);
expect(pq.elements_).to.eql([]);
expect(pq.priorities_).to.eql([]);
});
});
describe('when populated', function () {
let elements, pq;
beforeEach(function () {
elements = [];
pq = new PriorityQueue(identity, identity);
let element, i;
for (i = 0; i < 32; ++i) {
element = Math.random();
pq.enqueue(element);
elements.push(element);
}
});
it('dequeues elements in the correct order', function () {
elements.sort();
let i;
for (i = 0; i < elements.length; ++i) {
expect(pq.dequeue()).to.be(elements[i]);
}
expect(pq.isEmpty()).to.be(true);
});
});
describe('with an impure priority function', function () {
let pq, target;
beforeEach(function () {
target = 0.5;
pq = new PriorityQueue(function (element) {
return Math.abs(element - target);
}, identity);
let i;
for (i = 0; i < 32; ++i) {
pq.enqueue(Math.random());
}
});
it('dequeue elements in the correct order', function () {
let lastDelta = 0;
let delta;
while (!pq.isEmpty()) {
delta = Math.abs(pq.dequeue() - target);
expect(lastDelta <= delta).to.be(true);
lastDelta = delta;
}
});
it('allows reprioritization', function () {
const target = 0.5;
pq.reprioritize();
let lastDelta = 0;
let delta;
while (!pq.isEmpty()) {
delta = Math.abs(pq.dequeue() - target);
expect(lastDelta <= delta).to.be(true);
lastDelta = delta;
}
});
it('allows dropping during reprioritization', function () {
const target = 0.5;
let i = 0;
pq.priorityFunction_ = function (element) {
if (i++ % 2 === 0) {
return Math.abs(element - target);
} else {
return DROP;
}
};
pq.reprioritize();
expect(pq.getCount()).to.be(16);
let lastDelta = 0;
let delta;
while (!pq.isEmpty()) {
delta = Math.abs(pq.dequeue() - target);
expect(lastDelta <= delta).to.be(true);
lastDelta = delta;
}
});
});
describe('tracks elements in the queue', function () {
let pq;
beforeEach(function () {
pq = new PriorityQueue(identity, identity);
pq.enqueue('a');
pq.enqueue('b');
pq.enqueue('c');
});
it('tracks which elements have been queued', function () {
expect(pq.isQueued('a')).to.be(true);
expect(pq.isQueued('b')).to.be(true);
expect(pq.isQueued('c')).to.be(true);
});
it('tracks which elements have not been queued', function () {
expect(pq.isQueued('d')).to.be(false);
});
it('raises an error when an queued element is re-queued', function () {
expect(function () {
pq.enqueue('a');
}).to.throwException();
});
it('tracks which elements have be dequeued', function () {
expect(pq.isQueued('a')).to.be(true);
expect(pq.isQueued('b')).to.be(true);
expect(pq.isQueued('c')).to.be(true);
expect(pq.dequeue()).to.be('a');
expect(pq.isQueued('a')).to.be(false);
expect(pq.isQueued('b')).to.be(true);
expect(pq.isQueued('c')).to.be(true);
expect(pq.dequeue()).to.be('b');
expect(pq.isQueued('a')).to.be(false);
expect(pq.isQueued('b')).to.be(false);
expect(pq.isQueued('c')).to.be(true);
expect(pq.dequeue()).to.be('c');
expect(pq.isQueued('a')).to.be(false);
expect(pq.isQueued('b')).to.be(false);
expect(pq.isQueued('c')).to.be(false);
});
});
});

View File

@@ -0,0 +1,308 @@
import RBush from '../../../../src/ol/structs/RBush.js';
import expect from '../../expect.js';
describe('ol/structs/RBush.js', function () {
let rBush;
beforeEach(function () {
rBush = new RBush();
});
describe('when empty', function () {
describe('#getAll', function () {
it('returns the expected number of objects', function () {
expect(rBush.getAll()).to.be.empty();
});
});
describe('#isEmpty', function () {
it('returns true', function () {
expect(rBush.isEmpty()).to.be(true);
});
});
});
describe('with a single object', function () {
let obj;
beforeEach(function () {
obj = {};
rBush.insert([0, 0, 1, 1], obj);
});
it('can update the object', function () {
expect(rBush.getInExtent([0, 0, 1, 1])).to.eql([obj]);
rBush.update([2, 2, 3, 3], obj);
expect(rBush.getInExtent([0, 0, 1, 1])).to.be.empty();
expect(rBush.getAll()).to.eql([obj]);
expect(rBush.getInExtent([2, 2, 3, 3])).to.eql([obj]);
});
it("don't throws an exception if the extent is not modified", function () {
expect(function () {
rBush.forEach(function (value) {
rBush.update([0, 0, 1, 1], obj);
});
}).not.to.throwException();
});
});
describe('with a few objects', function () {
let objs;
beforeEach(function () {
objs = [{}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}];
rBush.insert([0, 0, 1, 1], objs[0]);
rBush.insert([1, 1, 4, 4], objs[1]);
rBush.insert([2, 2, 3, 3], objs[2]);
rBush.insert([-5, -5, -4, -4], objs[3]);
rBush.insert([-4, -4, -1, -1], objs[4]);
rBush.insert([-3, -3, -2, -2], objs[5]);
rBush.insert([-3, -3, -2, -2], objs[6]);
rBush.insert([-3, -3, -2, -2], objs[7]);
rBush.insert([-3, -3, -2, -2], objs[8]);
rBush.insert([-3, -3, -2, -2], objs[9]);
rBush.insert([-3, -3, -2, -2], objs[10]);
});
describe('#forEach', function () {
it('called for all the objects', function () {
let i = 0;
rBush.forEach(function () {
++i;
});
expect(i).to.be(objs.length);
});
it('stops when the function returns true', function () {
let i = 0;
const result = rBush.forEach(function () {
return ++i >= 4;
});
expect(i).to.be(4);
expect(result).to.be(true);
});
});
describe('#getInExtent', function () {
it('returns the expected objects', function () {
let result;
result = rBush.getInExtent([2, 2, 3, 3]);
expect(result).to.contain(objs[1]);
expect(result).to.contain(objs[2]);
expect(result.length).to.be(2);
result = rBush.getInExtent([-1, -1, 2, 2]);
expect(result).to.contain(objs[0]);
expect(result).to.contain(objs[1]);
expect(result).to.contain(objs[2]);
expect(result).to.contain(objs[4]);
expect(result.length).to.be(4);
});
it('returns an empty array when given a disjoint extent', function () {
expect(rBush.getInExtent([5, 5, 6, 6]).length).to.be(0);
});
});
describe('#isEmpty', function () {
it('returns false', function () {
expect(rBush.isEmpty()).to.be(false);
});
});
describe('#remove', function () {
it('can remove each object', function () {
let i, ii;
for (i = 0, ii = objs.length; i < ii; ++i) {
expect(rBush.getAll()).to.contain(objs[i]);
rBush.remove(objs[i]);
expect(rBush.getAll()).not.to.contain(objs[i]);
}
});
});
});
describe('with 100 objects', function () {
let extents, objs;
beforeEach(function () {
extents = [];
objs = [];
let i;
for (i = 0; i < 100; ++i) {
extents[i] = [i - 0.1, i - 0.1, i + 0.1, i + 0.1];
objs[i] = {id: i};
rBush.insert(extents[i], objs[i]);
}
});
describe('#getInExtent', function () {
it('returns the expected objects', function () {
let i, ii;
for (i = 0, ii = objs.length; i < ii; ++i) {
expect(rBush.getInExtent(extents[i])).to.eql([objs[i]]);
}
});
});
describe('#isEmpty', function () {
it('returns false', function () {
expect(rBush.isEmpty()).to.be(false);
});
});
describe('#remove', function () {
it('can remove each object in turn', function () {
let i, ii;
for (i = 0, ii = objs.length; i < ii; ++i) {
expect(rBush.getInExtent(extents[i])).to.eql([objs[i]]);
rBush.remove(objs[i]);
expect(rBush.getInExtent(extents[i])).to.be.empty();
}
expect(rBush.getAll()).to.be.empty();
expect(rBush.isEmpty()).to.be(true);
});
it('can remove objects in random order', function () {
let i, ii, j;
// https://en.wikipedia.org/wiki/Random_permutation
const indexes = [];
for (i = 0, ii = objs.length; i < ii; ++i) {
j = Math.floor(Math.random() * (i + 1));
indexes[i] = indexes[j];
indexes[j] = i;
}
for (i = 0, ii = objs.length; i < ii; ++i) {
const index = indexes[i];
expect(rBush.getInExtent(extents[index])).to.eql([objs[index]]);
rBush.remove(objs[index]);
expect(rBush.getInExtent(extents[index])).to.be.empty();
}
expect(rBush.getAll()).to.be.empty();
expect(rBush.isEmpty()).to.be(true);
});
});
});
describe('with 1000 objects', function () {
beforeEach(function () {
let i;
for (i = 0; i < 1000; ++i) {
const min = [Math.random() * 10000, Math.random() * 10000];
const max = [
min[0] + Math.random() * 500,
min[1] + Math.random() * 500,
];
const extent = [min[0], min[1], max[0], max[1]];
rBush.insert(extent, {id: i});
}
});
describe('#getAll', function () {
it('returns the expected number of objects', function () {
expect(rBush.getAll().length).to.be(1000);
});
});
describe('#getInExtent', function () {
it('returns the expected number of objects', function () {
expect(rBush.getInExtent([0, 0, 10600, 10600]).length).to.be(1000);
});
it('can perform 1000 in-extent searches', function () {
let n = 0;
let i;
for (i = 0; i < 1000; ++i) {
const min = [Math.random() * 10000, Math.random() * 10000];
const max = [
min[0] + Math.random() * 500,
min[1] + Math.random() * 500,
];
const extent = [min[0], min[1], max[0], max[1]];
n += rBush.getInExtent(extent).length;
}
expect(n).not.to.be(0);
});
it('can perform 1000 out-of-extent searches', function () {
let n = 0;
let i;
for (i = 0; i < 1000; ++i) {
const min = [
-(Math.random() * 10000 + 501),
-(Math.random() * 10000 + 501),
];
const max = [
min[0] + Math.random() * 500,
min[1] + Math.random() * 500,
];
const extent = [min[0], min[1], max[0], max[1]];
n += rBush.getInExtent(extent).length;
}
expect(n).to.be(0);
});
});
describe('#insert', function () {
it('can insert another 1000 objects', function () {
let i;
for (i = 1000; i < 2000; ++i) {
const min = [Math.random() * 10000, Math.random() * 10000];
const max = [
min[0] + Math.random() * 500,
min[1] + Math.random() * 500,
];
const extent = [min[0], min[1], max[0], max[1]];
rBush.insert(extent, {id: i});
}
expect(rBush.getInExtent([0, 0, 10600, 10600]).length).to.be(2000);
});
});
describe('#isEmpty', function () {
it('returns false', function () {
expect(rBush.isEmpty()).to.be(false);
});
});
describe('#remove', function () {
it('can remove all 1000 objects', function () {
const objs = rBush.getAll();
let i, value;
for (i = objs.length - 1; i >= 0; --i) {
value = objs[i];
rBush.remove(value);
}
expect(rBush.isEmpty()).to.be(true);
});
});
});
describe('#getExtent', function () {
it('gets the extent', function () {
const obj = {};
rBush.insert([0, 0, 1, 1], obj);
expect(rBush.getExtent()).to.eql([0, 0, 1, 1]);
});
});
describe('#concat', function () {
it('concatenates two RBush objects', function () {
const obj1 = {};
const obj2 = {};
const rBush2 = new RBush();
rBush.insert([0, 0, 1, 1], obj1);
rBush2.insert([0, 0, 2, 2], obj2);
rBush.concat(rBush2);
expect(rBush.getExtent()).to.eql([0, 0, 2, 2]);
expect(rBush.getAll().length).to.be(2);
});
it("preserves the concatenated object's references", function () {
const obj1 = {};
const obj2 = {};
const rBush2 = new RBush();
rBush.insert([0, 0, 1, 1], obj1);
rBush2.insert([0, 0, 2, 2], obj2);
rBush.concat(rBush2);
rBush.update([0, 0, 3, 3], obj2);
expect(rBush.getExtent()).to.eql([0, 0, 3, 3]);
});
});
});

File diff suppressed because it is too large Load Diff

25
test/node/ol/util.test.js Normal file
View File

@@ -0,0 +1,25 @@
import expect from '../expect.js';
import {getUid} from '../../../src/ol/util.js';
describe('ol/util.js', () => {
describe('getUid()', function () {
it('is constant once generated', function () {
const a = {};
expect(getUid(a)).to.be(getUid(a));
});
it('generates a strictly increasing sequence', function () {
const a = {};
const b = {};
const c = {};
getUid(a);
getUid(c);
getUid(b);
// uid order should be a < c < b
expect(getUid(a)).to.be.lessThan(getUid(c));
expect(getUid(c)).to.be.lessThan(getUid(b));
expect(getUid(a)).to.be.lessThan(getUid(b));
});
});
});

View File

@@ -0,0 +1,36 @@
import expect from '../../expect.js';
import {create, fromTransform} from '../../../../src/ol/vec/mat4.js';
describe('ol/vec/mat4.js', function () {
describe('create()', function () {
it('returns the expected matrix', function () {
expect(create()).to.eql([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]);
});
});
describe('fromTransform()', function () {
it('sets the expected transform on the matrix', function () {
const transform = [1, 2, 3, 4, 5, 6];
const result = create();
expect(fromTransform(result, transform)).to.eql([
1,
2,
0,
0,
3,
4,
0,
0,
0,
0,
1,
0,
5,
6,
0,
1,
]);
expect(result).to.eql([1, 2, 0, 0, 3, 4, 0, 0, 0, 0, 1, 0, 5, 6, 0, 1]);
});
});
});

11
test/node/readme.md Normal file
View File

@@ -0,0 +1,11 @@
# Node.js Based Tests
The tests in this directory are run in Node.js. To run the tests:
npm run test-node
To attach a debugger to the tests, add a `debugger` statement in the module that you want to debug and run the tests with the `--inspect-brk` flag:
npm run test-node -- --inspect-brk
Then open chrome://inspect/ and attach to the remote target (or see https://nodejs.org/en/docs/guides/debugging-getting-started/ for other options).