Merge pull request #12264 from tschaub/test-node

Run selected format tests in Node
This commit is contained in:
Tim Schaub
2021-05-02 14:32:20 -06:00
committed by GitHub
13 changed files with 383 additions and 982 deletions

View File

@@ -1,611 +0,0 @@
import {
binarySearch,
equals,
extend,
find,
findIndex,
isSorted,
linearFindNearest,
numberSafeCompareFunction,
remove,
reverseSubArray,
stableSort,
} from '../../../../src/ol/array.js';
describe('ol.array', 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

@@ -1,256 +0,0 @@
import Feature from '../../../../../src/ol/Feature.js';
import FeatureFormat from '../../../../../src/ol/format/Feature.js';
import MultiPolygon from '../../../../../src/ol/geom/MultiPolygon.js';
import Polygon from '../../../../../src/ol/geom/Polygon.js';
import TopoJSON from '../../../../../src/ol/format/TopoJSON.js';
import {transform} from '../../../../../src/ol/proj.js';
const aruba = {
type: 'Topology',
transform: {
scale: [0.036003600360036005, 0.017361589674592462],
translate: [-180, -89.99892578124998],
},
objects: {
aruba: {
type: 'Polygon',
properties: {
prop0: 'value0',
},
arcs: [[0]],
id: 533,
},
},
arcs: [
[
[3058, 5901],
[0, -2],
[-2, 1],
[-1, 3],
[-2, 3],
[0, 3],
[1, 1],
[1, -3],
[2, -5],
[1, -1],
],
],
};
const zeroId = {
type: 'Topology',
objects: {
foobar: {
type: 'Point',
id: 0,
coordinates: [0, 42],
},
},
};
const nullGeometry = {
type: 'Topology',
objects: {
foobar: {
type: null,
properties: {
prop0: 'value0',
},
id: 533,
},
},
};
describe('ol.format.TopoJSON', function () {
let format;
before(function () {
format = new TopoJSON();
});
describe('constructor', function () {
it('creates a new format', function () {
expect(format).to.be.a(FeatureFormat);
expect(format).to.be.a(TopoJSON);
});
});
describe('#readFeaturesFromTopology_()', function () {
it('creates an array of features from a topology', function () {
const features = format.readFeaturesFromObject(aruba);
expect(features).to.have.length(1);
const feature = features[0];
expect(feature).to.be.a(Feature);
const geometry = feature.getGeometry();
expect(geometry).to.be.a(Polygon);
// Parses identifier
expect(feature.getId()).to.be(533);
// Parses properties
expect(feature.get('prop0')).to.be('value0');
expect(geometry.getExtent()).to.eql([
-70.08100810081008,
12.417091709170947,
-69.9009900990099,
12.608069195591469,
]);
});
it('can read a feature with id equal to 0', function () {
const features = format.readFeaturesFromObject(zeroId);
expect(features).to.have.length(1);
const feature = features[0];
expect(feature).to.be.a(Feature);
expect(feature.getId()).to.be(0);
});
it('can read a feature with null geometry', function () {
const features = format.readFeaturesFromObject(nullGeometry);
expect(features).to.have.length(1);
const feature = features[0];
expect(feature).to.be.a(Feature);
expect(feature.getGeometry()).to.be(null);
expect(feature.getId()).to.be(533);
expect(feature.get('prop0')).to.be('value0');
});
});
describe('#readFeatures()', function () {
it('parses simple.json', function (done) {
afterLoadText('spec/ol/format/topojson/simple.json', function (text) {
const features = format.readFeatures(text);
expect(features.length).to.be(3);
const point = features[0].getGeometry();
expect(point.getType()).to.be('Point');
expect(point.getFlatCoordinates()).to.eql([102, 0.5]);
const line = features[1].getGeometry();
expect(line.getType()).to.be('LineString');
expect(line.getFlatCoordinates()).to.eql([
102,
0,
103,
1,
104,
0,
105,
1,
]);
const polygon = features[2].getGeometry();
expect(polygon.getType()).to.be('Polygon');
expect(polygon.getFlatCoordinates()).to.eql([
100,
0,
100,
1,
101,
1,
101,
0,
100,
0,
]);
done();
});
});
it('parses simple.json and transforms', function (done) {
afterLoadText('spec/ol/format/topojson/simple.json', function (text) {
const features = format.readFeatures(text, {
featureProjection: 'EPSG:3857',
});
expect(features.length).to.be(3);
const point = features[0].getGeometry();
expect(point.getType()).to.be('Point');
expect(features[0].getGeometry().getCoordinates()).to.eql(
transform([102.0, 0.5], 'EPSG:4326', 'EPSG:3857')
);
const line = features[1].getGeometry();
expect(line.getType()).to.be('LineString');
expect(line.getCoordinates()).to.eql([
transform([102.0, 0.0], 'EPSG:4326', 'EPSG:3857'),
transform([103.0, 1.0], 'EPSG:4326', 'EPSG:3857'),
transform([104.0, 0.0], 'EPSG:4326', 'EPSG:3857'),
transform([105.0, 1.0], 'EPSG:4326', 'EPSG:3857'),
]);
const polygon = features[2].getGeometry();
expect(polygon.getType()).to.be('Polygon');
expect(polygon.getCoordinates()).to.eql([
[
transform([100.0, 0.0], 'EPSG:4326', 'EPSG:3857'),
transform([100.0, 1.0], 'EPSG:4326', 'EPSG:3857'),
transform([101.0, 1.0], 'EPSG:4326', 'EPSG:3857'),
transform([101.0, 0.0], 'EPSG:4326', 'EPSG:3857'),
transform([100.0, 0.0], 'EPSG:4326', 'EPSG:3857'),
],
]);
done();
});
});
it('parses world-110m.json', function (done) {
afterLoadText('spec/ol/format/topojson/world-110m.json', function (text) {
const features = format.readFeatures(text);
expect(features.length).to.be(178);
const first = features[0];
expect(first).to.be.a(Feature);
const firstGeom = first.getGeometry();
expect(firstGeom).to.be.a(MultiPolygon);
expect(firstGeom.getExtent()).to.eql([
-180,
-85.60903777459777,
180,
83.64513000000002,
]);
const last = features[177];
expect(last).to.be.a(Feature);
const lastGeom = last.getGeometry();
expect(lastGeom).to.be.a(Polygon);
expect(lastGeom.getExtent()).to.eql([
25.26325263252633,
-22.271802279310577,
32.848528485284874,
-15.50833810039586,
]);
done();
});
});
it("sets the topology's child names as feature property", function (done) {
afterLoadText('spec/ol/format/topojson/world-110m.json', function (text) {
const format = new TopoJSON({
layerName: 'layer',
});
const features = format.readFeatures(text);
expect(features[0].get('layer')).to.be('land');
expect(features[177].get('layer')).to.be('countries');
done();
});
});
it("only parses features from specified topology's children", function (done) {
afterLoadText('spec/ol/format/topojson/world-110m.json', function (text) {
const format = new TopoJSON({
layers: ['land'],
});
const features = format.readFeatures(text);
expect(features.length).to.be(1);
done();
});
});
});
});

View File

@@ -17,7 +17,7 @@ import {
} from '../../../src/ol/coordinate.js';
import {get} from '../../../src/ol/proj.js';
describe('ol.coordinate', function () {
describe('ol/coordinate.js', function () {
describe('#add', function () {
let coordinate, delta;

View File

@@ -1,16 +1,18 @@
import EsriJSON from '../../../../../src/ol/format/EsriJSON.js';
import Feature from '../../../../../src/ol/Feature.js';
import LineString from '../../../../../src/ol/geom/LineString.js';
import LinearRing from '../../../../../src/ol/geom/LinearRing.js';
import MultiLineString from '../../../../../src/ol/geom/MultiLineString.js';
import MultiPoint from '../../../../../src/ol/geom/MultiPoint.js';
import MultiPolygon from '../../../../../src/ol/geom/MultiPolygon.js';
import Point from '../../../../../src/ol/geom/Point.js';
import Polygon from '../../../../../src/ol/geom/Polygon.js';
import {equals} from '../../../../../src/ol/extent.js';
import {get as getProjection, transform} from '../../../../../src/ol/proj.js';
import EsriJSON from '../../../../src/ol/format/EsriJSON.js';
import Feature from '../../../../src/ol/Feature.js';
import LineString from '../../../../src/ol/geom/LineString.js';
import LinearRing from '../../../../src/ol/geom/LinearRing.js';
import MultiLineString from '../../../../src/ol/geom/MultiLineString.js';
import MultiPoint from '../../../../src/ol/geom/MultiPoint.js';
import MultiPolygon from '../../../../src/ol/geom/MultiPolygon.js';
import Point from '../../../../src/ol/geom/Point.js';
import Polygon from '../../../../src/ol/geom/Polygon.js';
import expect from '../../expect.js';
import fse from 'fs-extra';
import {equals} from '../../../../src/ol/extent.js';
import {get as getProjection, transform} from '../../../../src/ol/proj.js';
describe('ol.format.EsriJSON', function () {
describe('ol/format/EsriJSON.js', function () {
let format;
beforeEach(function () {
format = new EsriJSON();
@@ -374,42 +376,43 @@ describe('ol.format.EsriJSON', function () {
expect(secondGeom).to.be.a(LineString);
});
it('parses ksfields.geojson', function (done) {
afterLoadText('spec/ol/format/esrijson/ksfields.json', function (text) {
const result = format.readFeatures(text);
expect(result.length).to.be(9);
it('parses ksfields.geojson', async () => {
const text = await fse.readFile(
'test/node/ol/format/EsriJSON/ksfields.json',
{encoding: 'utf8'}
);
const result = format.readFeatures(text);
expect(result.length).to.be(9);
const first = result[0];
expect(first).to.be.a(Feature);
expect(first.get('field_name')).to.be('EUDORA');
expect(first.getId()).to.be(6406);
const firstGeom = first.getGeometry();
expect(firstGeom).to.be.a(Polygon);
expect(
equals(firstGeom.getExtent(), [
-10585772.743554419,
4712365.161160459,
-10579560.16462974,
4716567.373073828,
])
).to.be(true);
const first = result[0];
expect(first).to.be.a(Feature);
expect(first.get('field_name')).to.be('EUDORA');
expect(first.getId()).to.be(6406);
const firstGeom = first.getGeometry();
expect(firstGeom).to.be.a(Polygon);
expect(
equals(firstGeom.getExtent(), [
-10585772.743554419,
4712365.161160459,
-10579560.16462974,
4716567.373073828,
])
).to.be(true);
const last = result[8];
expect(last).to.be.a(Feature);
expect(last.get('field_name')).to.be('FEAGINS');
expect(last.getId()).to.be(6030);
const lastGeom = last.getGeometry();
expect(lastGeom).to.be.a(Polygon);
expect(
equals(lastGeom.getExtent(), [
-10555714.026858449,
4576511.565880965,
-10553671.199322715,
4578554.9934867555,
])
).to.be(true);
done();
});
const last = result[8];
expect(last).to.be.a(Feature);
expect(last.get('field_name')).to.be('FEAGINS');
expect(last.getId()).to.be(6030);
const lastGeom = last.getGeometry();
expect(lastGeom).to.be.a(Polygon);
expect(
equals(lastGeom.getExtent(), [
-10555714.026858449,
4576511.565880965,
-10553671.199322715,
4578554.9934867555,
])
).to.be(true);
});
});

View File

@@ -1,22 +1,24 @@
import Circle from '../../../../../src/ol/geom/Circle.js';
import Feature from '../../../../../src/ol/Feature.js';
import GeoJSON from '../../../../../src/ol/format/GeoJSON.js';
import GeometryCollection from '../../../../../src/ol/geom/GeometryCollection.js';
import LineString from '../../../../../src/ol/geom/LineString.js';
import LinearRing from '../../../../../src/ol/geom/LinearRing.js';
import MultiPolygon from '../../../../../src/ol/geom/MultiPolygon.js';
import Point from '../../../../../src/ol/geom/Point.js';
import Polygon from '../../../../../src/ol/geom/Polygon.js';
import Circle from '../../../../src/ol/geom/Circle.js';
import Feature from '../../../../src/ol/Feature.js';
import GeoJSON from '../../../../src/ol/format/GeoJSON.js';
import GeometryCollection from '../../../../src/ol/geom/GeometryCollection.js';
import LineString from '../../../../src/ol/geom/LineString.js';
import LinearRing from '../../../../src/ol/geom/LinearRing.js';
import MultiPolygon from '../../../../src/ol/geom/MultiPolygon.js';
import Point from '../../../../src/ol/geom/Point.js';
import Polygon from '../../../../src/ol/geom/Polygon.js';
import expect from '../../expect.js';
import fse from 'fs-extra';
import {
Projection,
fromLonLat,
get as getProjection,
toLonLat,
transform,
} from '../../../../../src/ol/proj.js';
import {equals} from '../../../../../src/ol/extent.js';
} from '../../../../src/ol/proj.js';
import {equals} from '../../../../src/ol/extent.js';
describe('ol.format.GeoJSON', function () {
describe('ol/format/GeoJSON.js', function () {
let format;
beforeEach(function () {
format = new GeoJSON();
@@ -341,45 +343,43 @@ describe('ol.format.GeoJSON', function () {
expect(geometry).to.be.an(Polygon);
});
it('parses countries.geojson', function (done) {
afterLoadText(
'spec/ol/format/geojson/countries.geojson',
function (text) {
const result = format.readFeatures(text);
expect(result.length).to.be(179);
const first = result[0];
expect(first).to.be.a(Feature);
expect(first.get('name')).to.be('Afghanistan');
expect(first.getId()).to.be('AFG');
const firstGeom = first.getGeometry();
expect(firstGeom).to.be.a(Polygon);
expect(
equals(firstGeom.getExtent(), [
60.52843,
29.318572,
75.158028,
38.486282,
])
).to.be(true);
const last = result[178];
expect(last).to.be.a(Feature);
expect(last.get('name')).to.be('Zimbabwe');
expect(last.getId()).to.be('ZWE');
const lastGeom = last.getGeometry();
expect(lastGeom).to.be.a(Polygon);
expect(
equals(lastGeom.getExtent(), [
25.264226,
-22.271612,
32.849861,
-15.507787,
])
).to.be(true);
done();
}
it('parses countries.geojson', async () => {
const text = await fse.readFile(
'test/node/ol/format/GeoJSON/countries.geojson',
{encoding: 'utf8'}
);
const result = format.readFeatures(text);
expect(result.length).to.be(179);
const first = result[0];
expect(first).to.be.a(Feature);
expect(first.get('name')).to.be('Afghanistan');
expect(first.getId()).to.be('AFG');
const firstGeom = first.getGeometry();
expect(firstGeom).to.be.a(Polygon);
expect(
equals(firstGeom.getExtent(), [
60.52843,
29.318572,
75.158028,
38.486282,
])
).to.be(true);
const last = result[178];
expect(last).to.be.a(Feature);
expect(last.get('name')).to.be('Zimbabwe');
expect(last.getId()).to.be('ZWE');
const lastGeom = last.getGeometry();
expect(lastGeom).to.be.a(Polygon);
expect(
equals(lastGeom.getExtent(), [
25.264226,
-22.271612,
32.849861,
-15.507787,
])
).to.be(true);
});
it('generates an array of features for Feature', function () {

View File

@@ -1,9 +1,10 @@
import Feature from '../../../../../src/ol/Feature.js';
import LineString from '../../../../../src/ol/geom/LineString.js';
import Polyline, * as polyline from '../../../../../src/ol/format/Polyline.js';
import {get as getProjection, transform} from '../../../../../src/ol/proj.js';
import Feature from '../../../../src/ol/Feature.js';
import LineString from '../../../../src/ol/geom/LineString.js';
import Polyline, * as polyline from '../../../../src/ol/format/Polyline.js';
import expect from '../../expect.js';
import {get as getProjection, transform} from '../../../../src/ol/proj.js';
describe('ol.format.Polyline', function () {
describe('ol/format/Polyline.js', function () {
let format;
let points;
let flatPoints, encodedFlatPoints, flippedFlatPoints;

View File

@@ -0,0 +1,262 @@
import Feature from '../../../../src/ol/Feature.js';
import FeatureFormat from '../../../../src/ol/format/Feature.js';
import MultiPolygon from '../../../../src/ol/geom/MultiPolygon.js';
import Polygon from '../../../../src/ol/geom/Polygon.js';
import TopoJSON from '../../../../src/ol/format/TopoJSON.js';
import expect from '../../expect.js';
import fse from 'fs-extra';
import {transform} from '../../../../src/ol/proj.js';
const aruba = {
type: 'Topology',
transform: {
scale: [0.036003600360036005, 0.017361589674592462],
translate: [-180, -89.99892578124998],
},
objects: {
aruba: {
type: 'Polygon',
properties: {
prop0: 'value0',
},
arcs: [[0]],
id: 533,
},
},
arcs: [
[
[3058, 5901],
[0, -2],
[-2, 1],
[-1, 3],
[-2, 3],
[0, 3],
[1, 1],
[1, -3],
[2, -5],
[1, -1],
],
],
};
const zeroId = {
type: 'Topology',
objects: {
foobar: {
type: 'Point',
id: 0,
coordinates: [0, 42],
},
},
};
const nullGeometry = {
type: 'Topology',
objects: {
foobar: {
type: null,
properties: {
prop0: 'value0',
},
id: 533,
},
},
};
describe('ol/format/TopoJSON.js', function () {
let format;
before(function () {
format = new TopoJSON();
});
describe('constructor', function () {
it('creates a new format', function () {
expect(format).to.be.a(FeatureFormat);
expect(format).to.be.a(TopoJSON);
});
});
describe('#readFeaturesFromTopology_()', function () {
it('creates an array of features from a topology', function () {
const features = format.readFeaturesFromObject(aruba);
expect(features).to.have.length(1);
const feature = features[0];
expect(feature).to.be.a(Feature);
const geometry = feature.getGeometry();
expect(geometry).to.be.a(Polygon);
// Parses identifier
expect(feature.getId()).to.be(533);
// Parses properties
expect(feature.get('prop0')).to.be('value0');
expect(geometry.getExtent()).to.eql([
-70.08100810081008,
12.417091709170947,
-69.9009900990099,
12.608069195591469,
]);
});
it('can read a feature with id equal to 0', function () {
const features = format.readFeaturesFromObject(zeroId);
expect(features).to.have.length(1);
const feature = features[0];
expect(feature).to.be.a(Feature);
expect(feature.getId()).to.be(0);
});
it('can read a feature with null geometry', function () {
const features = format.readFeaturesFromObject(nullGeometry);
expect(features).to.have.length(1);
const feature = features[0];
expect(feature).to.be.a(Feature);
expect(feature.getGeometry()).to.be(null);
expect(feature.getId()).to.be(533);
expect(feature.get('prop0')).to.be('value0');
});
});
describe('#readFeatures()', function () {
it('parses simple.json', async () => {
const text = await fse.readFile(
'test/node/ol/format/TopoJSON/simple.json',
{encoding: 'utf8'}
);
const features = format.readFeatures(text);
expect(features.length).to.be(3);
const point = features[0].getGeometry();
expect(point.getType()).to.be('Point');
expect(point.getFlatCoordinates()).to.eql([102, 0.5]);
const line = features[1].getGeometry();
expect(line.getType()).to.be('LineString');
expect(line.getFlatCoordinates()).to.eql([
102,
0,
103,
1,
104,
0,
105,
1,
]);
const polygon = features[2].getGeometry();
expect(polygon.getType()).to.be('Polygon');
expect(polygon.getFlatCoordinates()).to.eql([
100,
0,
100,
1,
101,
1,
101,
0,
100,
0,
]);
});
it('parses simple.json and transforms', async () => {
const text = await fse.readFile(
'test/node/ol/format/TopoJSON/simple.json',
{encoding: 'utf8'}
);
const features = format.readFeatures(text, {
featureProjection: 'EPSG:3857',
});
expect(features.length).to.be(3);
const point = features[0].getGeometry();
expect(point.getType()).to.be('Point');
expect(features[0].getGeometry().getCoordinates()).to.eql(
transform([102.0, 0.5], 'EPSG:4326', 'EPSG:3857')
);
const line = features[1].getGeometry();
expect(line.getType()).to.be('LineString');
expect(line.getCoordinates()).to.eql([
transform([102.0, 0.0], 'EPSG:4326', 'EPSG:3857'),
transform([103.0, 1.0], 'EPSG:4326', 'EPSG:3857'),
transform([104.0, 0.0], 'EPSG:4326', 'EPSG:3857'),
transform([105.0, 1.0], 'EPSG:4326', 'EPSG:3857'),
]);
const polygon = features[2].getGeometry();
expect(polygon.getType()).to.be('Polygon');
expect(polygon.getCoordinates()).to.eql([
[
transform([100.0, 0.0], 'EPSG:4326', 'EPSG:3857'),
transform([100.0, 1.0], 'EPSG:4326', 'EPSG:3857'),
transform([101.0, 1.0], 'EPSG:4326', 'EPSG:3857'),
transform([101.0, 0.0], 'EPSG:4326', 'EPSG:3857'),
transform([100.0, 0.0], 'EPSG:4326', 'EPSG:3857'),
],
]);
});
it('parses world-110m.json', async () => {
const text = await fse.readFile(
'test/node/ol/format/TopoJSON/world-110m.json',
{encoding: 'utf8'}
);
const features = format.readFeatures(text);
expect(features.length).to.be(178);
const first = features[0];
expect(first).to.be.a(Feature);
const firstGeom = first.getGeometry();
expect(firstGeom).to.be.a(MultiPolygon);
expect(firstGeom.getExtent()).to.eql([
-180,
-85.60903777459777,
180,
83.64513000000002,
]);
const last = features[177];
expect(last).to.be.a(Feature);
const lastGeom = last.getGeometry();
expect(lastGeom).to.be.a(Polygon);
expect(lastGeom.getExtent()).to.eql([
25.26325263252633,
-22.271802279310577,
32.848528485284874,
-15.50833810039586,
]);
});
it("sets the topology's child names as feature property", async () => {
const text = await fse.readFile(
'test/node/ol/format/TopoJSON/world-110m.json',
{encoding: 'utf8'}
);
const format = new TopoJSON({
layerName: 'layer',
});
const features = format.readFeatures(text);
expect(features[0].get('layer')).to.be('land');
expect(features[177].get('layer')).to.be('countries');
});
it("only parses features from specified topology's children", async () => {
const text = await fse.readFile(
'test/node/ol/format/TopoJSON/world-110m.json',
{encoding: 'utf8'}
);
const format = new TopoJSON({
layers: ['land'],
});
const features = format.readFeatures(text);
expect(features.length).to.be(1);
});
});
});

View File

@@ -1,10 +1,11 @@
import Feature from '../../../../../src/ol/Feature.js';
import GeometryCollection from '../../../../../src/ol/geom/GeometryCollection.js';
import Point from '../../../../../src/ol/geom/Point.js';
import SimpleGeometry from '../../../../../src/ol/geom/SimpleGeometry.js';
import WKB from '../../../../../src/ol/format/WKB.js';
import WKT from '../../../../../src/ol/format/WKT.js';
import {transform} from '../../../../../src/ol/proj.js';
import Feature from '../../../../src/ol/Feature.js';
import GeometryCollection from '../../../../src/ol/geom/GeometryCollection.js';
import Point from '../../../../src/ol/geom/Point.js';
import SimpleGeometry from '../../../../src/ol/geom/SimpleGeometry.js';
import WKB from '../../../../src/ol/format/WKB.js';
import WKT from '../../../../src/ol/format/WKT.js';
import expect from '../../expect.js';
import {transform} from '../../../../src/ol/proj.js';
const patterns = [
[
@@ -957,7 +958,7 @@ const patterns = [
],
];
describe('ol.format.WKB', function () {
describe('ol/format/WKB.js', function () {
const format = new WKB();
describe('#readProjection(string)', function () {

View File

@@ -1,9 +1,10 @@
import Feature from '../../../../../src/ol/Feature.js';
import Point from '../../../../../src/ol/geom/Point.js';
import WKT from '../../../../../src/ol/format/WKT.js';
import {transform} from '../../../../../src/ol/proj.js';
import Feature from '../../../../src/ol/Feature.js';
import Point from '../../../../src/ol/geom/Point.js';
import WKT from '../../../../src/ol/format/WKT.js';
import expect from '../../expect.js';
import {transform} from '../../../../src/ol/proj.js';
describe('ol.format.WKT', function () {
describe('ol/format/WKT.js', function () {
let format = new WKT();
describe('#readProjectionFromText', function () {