Merge pull request #12241 from tschaub/module

Use modules everywhere
This commit is contained in:
Tim Schaub
2021-05-01 10:24:08 -06:00
committed by GitHub
89 changed files with 1532 additions and 622 deletions
+38 -3
View File
@@ -47,8 +47,8 @@ jobs:
- name: Run Tests - name: Run Tests
run: npm run pretest run: npm run pretest
spec: browser:
name: Spec name: Browser
runs-on: ubuntu-latest runs-on: ubuntu-latest
strategy: strategy:
@@ -80,7 +80,42 @@ jobs:
run: npm ci run: npm ci
- name: Run Tests - name: Run Tests
run: npm run test-spec run: npm run test-browser
node:
name: Node
runs-on: ubuntu-latest
strategy:
fail-fast: false
steps:
- name: Clone Repository
uses: actions/checkout@v2
- name: Set Node.js Version
uses: actions/setup-node@v1
with:
node-version: 16
- name: Determine Cache Directory
id: npm-cache
run: |
echo "::set-output name=dir::$(npm config get cache)"
- name: Configure Job Cache
uses: actions/cache@v1
with:
path: ${{ steps.npm-cache.outputs.dir }}
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-
- name: Install Dependencies
run: npm ci
- name: Run Tests
run: npm run test-node
build: build:
name: Build name: Build
+1
View File
@@ -0,0 +1 @@
16
+5 -5
View File
@@ -14,12 +14,12 @@
] ]
}, },
"plugins": [ "plugins": [
"config/jsdoc/api/plugins/markdown", "config/jsdoc/api/plugins/markdown.cjs",
"jsdoc-plugin-typescript", "jsdoc-plugin-typescript",
"config/jsdoc/api/plugins/inline-options", "config/jsdoc/api/plugins/inline-options.cjs",
"config/jsdoc/api/plugins/events", "config/jsdoc/api/plugins/events.cjs",
"config/jsdoc/api/plugins/observable", "config/jsdoc/api/plugins/observable.cjs",
"config/jsdoc/api/plugins/api" "config/jsdoc/api/plugins/api.cjs"
], ],
"typescript": { "typescript": {
"moduleRoot": "src" "moduleRoot": "src"
@@ -1,3 +1,5 @@
/* eslint-disable import/no-commonjs */
/** /**
* Define an @api tag * Define an @api tag
* @param {Object} dictionary The tag dictionary. * @param {Object} dictionary The tag dictionary.
@@ -1,3 +1,5 @@
/* eslint-disable import/no-commonjs */
const events = {}; const events = {};
exports.handlers = { exports.handlers = {
@@ -1,3 +1,5 @@
/* eslint-disable import/no-commonjs */
/** /**
* @fileoverview * @fileoverview
* Inlines option params from typedefs * Inlines option params from typedefs
@@ -1,3 +1,5 @@
/* eslint-disable import/no-commonjs */
/** /**
* Modified from JSDoc's plugins/markdown and lib/jsdoc/util/markdown modules * Modified from JSDoc's plugins/markdown and lib/jsdoc/util/markdown modules
* (see https://github.com/jsdoc3/jsdoc/), which are licensed under the Apache 2 * (see https://github.com/jsdoc3/jsdoc/), which are licensed under the Apache 2
@@ -1,3 +1,5 @@
/* eslint-disable import/no-commonjs */
const classes = {}; const classes = {};
const observables = {}; const observables = {};
+2
View File
@@ -1,3 +1,5 @@
/* eslint-disable import/no-commonjs */
/*global env: true */ /*global env: true */
const hasOwnProp = Object.prototype.hasOwnProperty; const hasOwnProp = Object.prototype.hasOwnProperty;
+2 -2
View File
@@ -11,8 +11,8 @@
}, },
"plugins": [ "plugins": [
"jsdoc-plugin-typescript", "jsdoc-plugin-typescript",
"config/jsdoc/info/define-plugin", "config/jsdoc/info/define-plugin.cjs",
"config/jsdoc/info/virtual-plugin" "config/jsdoc/info/virtual-plugin.cjs"
], ],
"typescript": { "typescript": {
"moduleRoot": "src" "moduleRoot": "src"
@@ -1,3 +1,5 @@
/* eslint-disable import/no-commonjs */
/** /**
* @fileoverview This plugin extracts info from boolean defines. This only * @fileoverview This plugin extracts info from boolean defines. This only
* handles boolean defines with the default value in the description. Default * handles boolean defines with the default value in the description. Default
+2
View File
@@ -1,3 +1,5 @@
/* eslint-disable import/no-commonjs */
/** /**
* @fileoverview Generates JSON output based on exportable symbols. * @fileoverview Generates JSON output based on exportable symbols.
*/ */
@@ -1,3 +1,5 @@
/* eslint-disable import/no-commonjs */
/** /**
* Handle the interface and abstract annotations. * Handle the interface and abstract annotations.
* @param {Object} dictionary The tag dictionary. * @param {Object} dictionary The tag dictionary.
+4
View File
@@ -0,0 +1,4 @@
{
"description": "JSDoc loads publish.js files with require(), so we need to configure everything under this path as a CommonJS module.",
"type": "commonjs"
}
+7 -4
View File
@@ -1,7 +1,10 @@
const path = require('path'); import TerserPlugin from 'terser-webpack-plugin';
const TerserPlugin = require('terser-webpack-plugin'); import path, {dirname} from 'path';
import {fileURLToPath} from 'url';
module.exports = { const baseDir = dirname(fileURLToPath(import.meta.url));
export default {
entry: './build/index.js', entry: './build/index.js',
devtool: 'source-map', devtool: 'source-map',
mode: 'production', mode: 'production',
@@ -18,7 +21,7 @@ module.exports = {
}, },
include: [ include: [
path.join( path.join(
__dirname, baseDir,
'..', '..',
'node_modules', 'node_modules',
'@mapbox', '@mapbox',
-7
View File
@@ -5,12 +5,5 @@
}, },
"parserOptions": { "parserOptions": {
"ecmaVersion": 2017 "ecmaVersion": 2017
},
"rules": {
"space-before-function-paren": ["error", {
"anonymous": "never",
"named": "never",
"asyncArrow": "always"
}]
} }
} }
+18 -15
View File
@@ -1,12 +1,15 @@
const TerserPlugin = require('terser-webpack-plugin'); import CopyPlugin from 'copy-webpack-plugin';
const CopyPlugin = require('copy-webpack-plugin'); import ExampleBuilder from './example-builder.js';
const ExampleBuilder = require('./example-builder'); import TerserPlugin from 'terser-webpack-plugin';
const fs = require('fs'); import fs from 'fs';
const path = require('path'); import path, {dirname} from 'path';
import {fileURLToPath} from 'url';
const src = path.join(__dirname, '..'); const baseDir = dirname(fileURLToPath(import.meta.url));
module.exports = { const src = path.join(baseDir, '..');
export default {
context: src, context: src,
target: ['web', 'es5'], target: ['web', 'es5'],
entry: () => { entry: () => {
@@ -33,10 +36,10 @@ module.exports = {
}, },
}, },
include: [ include: [
path.join(__dirname, '..', '..', 'src'), path.join(baseDir, '..', '..', 'src'),
path.join(__dirname, '..'), path.join(baseDir, '..'),
path.join( path.join(
__dirname, baseDir,
'..', '..',
'..', '..',
'node_modules', 'node_modules',
@@ -48,9 +51,9 @@ module.exports = {
{ {
test: /\.js$/, test: /\.js$/,
use: { use: {
loader: path.join(__dirname, 'worker-loader.js'), loader: path.join(baseDir, 'worker-loader.cjs'),
}, },
include: [path.join(__dirname, '..', '..', 'src', 'ol', 'worker')], include: [path.join(baseDir, '..', '..', 'src', 'ol', 'worker')],
}, },
], ],
}, },
@@ -77,7 +80,7 @@ module.exports = {
}, },
plugins: [ plugins: [
new ExampleBuilder({ new ExampleBuilder({
templates: path.join(__dirname, '..', 'templates'), templates: path.join(baseDir, '..', 'templates'),
common: 'common', common: 'common',
}), }),
new CopyPlugin({ new CopyPlugin({
@@ -93,7 +96,7 @@ module.exports = {
devtool: 'source-map', devtool: 'source-map',
output: { output: {
filename: '[name].js', filename: '[name].js',
path: path.join(__dirname, '..', '..', 'build', 'examples'), path: path.join(baseDir, '..', '..', 'build', 'examples'),
}, },
resolve: { resolve: {
fallback: { fallback: {
@@ -101,7 +104,7 @@ module.exports = {
}, },
alias: { alias: {
// allow imports from 'ol/module' instead of specifiying the source path // allow imports from 'ol/module' instead of specifiying the source path
ol: path.join(__dirname, '..', '..', 'src', 'ol'), ol: path.join(baseDir, '..', '..', 'src', 'ol'),
}, },
}, },
}; };
+36 -20
View File
@@ -1,20 +1,32 @@
const assert = require('assert'); import assert from 'assert';
const frontMatter = require('front-matter'); import frontMatter from 'front-matter';
const fs = require('fs'); import fse from 'fs-extra';
const handlebars = require('handlebars'); import handlebars from 'handlebars';
const marked = require('marked'); import marked from 'marked';
const path = require('path'); import path, {dirname} from 'path';
const pkg = require('../../package.json'); import sources from 'webpack-sources';
const promisify = require('util').promisify; import {fileURLToPath} from 'url';
const RawSource = require('webpack-sources').RawSource;
const RawSource = sources.RawSource;
const baseDir = dirname(fileURLToPath(import.meta.url));
const readFile = promisify(fs.readFile);
const isCssRegEx = /\.css(\?.*)?$/; const isCssRegEx = /\.css(\?.*)?$/;
const isJsRegEx = /\.js(\?.*)?$/; const isJsRegEx = /\.js(\?.*)?$/;
const importRegEx = /^import .* from '(.*)';$/; const importRegEx = /^import .* from '(.*)';$/;
const isTemplateJs = /\/(jquery(-\d+\.\d+\.\d+)?|(bootstrap(\.bundle)?))(\.min)?\.js(\?.*)?$/; const isTemplateJs = /\/(jquery(-\d+\.\d+\.\d+)?|(bootstrap(\.bundle)?))(\.min)?\.js(\?.*)?$/;
const isTemplateCss = /\/bootstrap(\.min)?\.css(\?.*)?$/; const isTemplateCss = /\/bootstrap(\.min)?\.css(\?.*)?$/;
let cachedPackageInfo = null;
async function getPackageInfo() {
if (cachedPackageInfo) {
return cachedPackageInfo;
}
cachedPackageInfo = await fse.readJSON(
path.resolve(baseDir, '../../package.json')
);
return cachedPackageInfo;
}
handlebars.registerHelper( handlebars.registerHelper(
'md', 'md',
(str) => new handlebars.SafeString(marked(str)) (str) => new handlebars.SafeString(marked(str))
@@ -111,9 +123,10 @@ function createWordIndex(exampleData) {
/** /**
* Gets dependencies from the js source. * Gets dependencies from the js source.
* @param {string} jsSource Source. * @param {string} jsSource Source.
* @param {Object} pkg Package info.
* @return {Object<string, string>} dependencies * @return {Object<string, string>} dependencies
*/ */
function getDependencies(jsSource) { function getDependencies(jsSource, pkg) {
const lines = jsSource.split('\n'); const lines = jsSource.split('\n');
const dependencies = { const dependencies = {
ol: pkg.version, ol: pkg.version,
@@ -140,7 +153,7 @@ function getDependencies(jsSource) {
return dependencies; return dependencies;
} }
class ExampleBuilder { export default class ExampleBuilder {
/** /**
* A webpack plugin that builds the html files for our examples. * A webpack plugin that builds the html files for our examples.
* @param {Object} config Plugin configuration. Requires a `templates` property * @param {Object} config Plugin configuration. Requires a `templates` property
@@ -241,16 +254,17 @@ class ExampleBuilder {
async parseExample(dir, name) { async parseExample(dir, name) {
const htmlName = `${name}.html`; const htmlName = `${name}.html`;
const htmlPath = path.join(dir, htmlName); const htmlPath = path.join(dir, htmlName);
const htmlSource = await readFile(htmlPath, {encoding: 'utf8'}); const htmlSource = await fse.readFile(htmlPath, {encoding: 'utf8'});
const jsName = `${name}.js`; const jsName = `${name}.js`;
const jsPath = path.join(dir, jsName); const jsPath = path.join(dir, jsName);
const jsSource = await readFile(jsPath, {encoding: 'utf8'}); const jsSource = await fse.readFile(jsPath, {encoding: 'utf8'});
const {attributes, body} = frontMatter(htmlSource); const {attributes, body} = frontMatter(htmlSource);
assert(!!attributes.layout, `missing layout in ${htmlPath}`); assert(!!attributes.layout, `missing layout in ${htmlPath}`);
const data = Object.assign(attributes, {contents: body}); const data = Object.assign(attributes, {contents: body});
const pkg = await getPackageInfo();
data.olVersion = pkg.version; data.olVersion = pkg.version;
data.filename = htmlName; data.filename = htmlName;
data.dir = dir; data.dir = dir;
@@ -299,7 +313,7 @@ class ExampleBuilder {
const workerPath = path.join(data.dir, workerName); const workerPath = path.join(data.dir, workerName);
let workerSource; let workerSource;
try { try {
workerSource = await readFile(workerPath, readOptions); workerSource = await fse.readFile(workerPath, readOptions);
} catch (err) { } catch (err) {
// pass // pass
} }
@@ -321,11 +335,14 @@ class ExampleBuilder {
assets[workerName] = workerSource; assets[workerName] = workerSource;
} }
const pkg = await getPackageInfo();
data.pkgJson = JSON.stringify( data.pkgJson = JSON.stringify(
{ {
name: data.name, name: data.name,
dependencies: getDependencies( dependencies: getDependencies(
jsSource + (workerSource ? `\n${workerSource}` : '') jsSource + (workerSource ? `\n${workerSource}` : ''),
pkg
), ),
devDependencies: { devDependencies: {
parcel: '^2.0.0-beta.1', parcel: '^2.0.0-beta.1',
@@ -344,7 +361,7 @@ class ExampleBuilder {
const cssPath = path.join(data.dir, cssName); const cssPath = path.join(data.dir, cssName);
let cssSource; let cssSource;
try { try {
cssSource = await readFile(cssPath, readOptions); cssSource = await fse.readFile(cssPath, readOptions);
} catch (err) { } catch (err) {
// pass // pass
} }
@@ -358,6 +375,7 @@ class ExampleBuilder {
// add additional resources // add additional resources
if (data.resources) { if (data.resources) {
const pkg = await getPackageInfo();
const localResources = []; const localResources = [];
const remoteResources = []; const remoteResources = [];
data.resources.forEach((resource) => { data.resources.forEach((resource) => {
@@ -389,11 +407,9 @@ class ExampleBuilder {
} }
const templatePath = path.join(this.templates, data.layout); const templatePath = path.join(this.templates, data.layout);
const templateSource = await readFile(templatePath, readOptions); const templateSource = await fse.readFile(templatePath, readOptions);
assets[data.filename] = handlebars.compile(templateSource)(data); assets[data.filename] = handlebars.compile(templateSource)(data);
return assets; return assets;
} }
} }
module.exports = ExampleBuilder;
@@ -1,6 +1,8 @@
const build = require('../../tasks/serialize-workers').build; /* eslint-disable import/no-commonjs */
function loader() { const build = require('../../tasks/serialize-workers.cjs').build;
module.exports = function loader() {
const callback = this.async(); const callback = this.async();
const minify = this.mode === 'production'; const minify = this.mode === 'production';
@@ -12,6 +14,4 @@ function loader() {
callback(null, chunk.code); callback(null, chunk.code);
}) })
.catch(callback); .catch(callback);
} };
module.exports = loader;
+61 -1
View File
@@ -15,6 +15,7 @@
}, },
"devDependencies": { "devDependencies": {
"@babel/core": "^7.6.4", "@babel/core": "^7.6.4",
"@babel/eslint-parser": "^7.13.14",
"@babel/preset-env": "^7.4.4", "@babel/preset-env": "^7.4.4",
"@openlayers/eslint-plugin": "^4.0.0", "@openlayers/eslint-plugin": "^4.0.0",
"@rollup/plugin-babel": "^5.3.0", "@rollup/plugin-babel": "^5.3.0",
@@ -32,6 +33,7 @@
"copy-webpack-plugin": "^8.0.0", "copy-webpack-plugin": "^8.0.0",
"coverage-istanbul-loader": "^3.0.5", "coverage-istanbul-loader": "^3.0.5",
"coveralls": "3.1.0", "coveralls": "3.1.0",
"es-main": "^1.0.2",
"eslint": "^7.2.0", "eslint": "^7.2.0",
"eslint-config-openlayers": "^15.0.0", "eslint-config-openlayers": "^15.0.0",
"expect.js": "0.3.1", "expect.js": "0.3.1",
@@ -66,7 +68,7 @@
"shx": "^0.3.2", "shx": "^0.3.2",
"sinon": "^10.0.0", "sinon": "^10.0.0",
"terser-webpack-plugin": "^5.1.1", "terser-webpack-plugin": "^5.1.1",
"typescript": "^4.2.3", "typescript": "^4.2.4",
"url-polyfill": "^1.1.5", "url-polyfill": "^1.1.5",
"walk": "^2.3.9", "walk": "^2.3.9",
"webpack": "^5.27.2", "webpack": "^5.27.2",
@@ -127,6 +129,33 @@
"url": "https://opencollective.com/babel" "url": "https://opencollective.com/babel"
} }
}, },
"node_modules/@babel/eslint-parser": {
"version": "7.13.14",
"resolved": "https://registry.npmjs.org/@babel/eslint-parser/-/eslint-parser-7.13.14.tgz",
"integrity": "sha512-I0HweR36D73Ibn/FfrRDMKlMqJHFwidIUgYdMpH+aXYuQC+waq59YaJ6t9e9N36axJ82v1jR041wwqDrDXEwRA==",
"dev": true,
"dependencies": {
"eslint-scope": "^5.1.0",
"eslint-visitor-keys": "^1.3.0",
"semver": "^6.3.0"
},
"engines": {
"node": "^10.13.0 || ^12.13.0 || >=14.0.0"
},
"peerDependencies": {
"@babel/core": ">=7.11.0",
"eslint": ">=7.5.0"
}
},
"node_modules/@babel/eslint-parser/node_modules/eslint-visitor-keys": {
"version": "1.3.0",
"resolved": "https://registry.npmjs.org/eslint-visitor-keys/-/eslint-visitor-keys-1.3.0.tgz",
"integrity": "sha512-6J72N8UNa462wa/KFODt/PJ3IU60SDpC3QXC1Hjc1BXXpfL2C9R5+AU7jhe0F6GREqVMh4Juu+NY7xn+6dipUQ==",
"dev": true,
"engines": {
"node": ">=4"
}
},
"node_modules/@babel/generator": { "node_modules/@babel/generator": {
"version": "7.13.16", "version": "7.13.16",
"resolved": "https://registry.npmjs.org/@babel/generator/-/generator-7.13.16.tgz", "resolved": "https://registry.npmjs.org/@babel/generator/-/generator-7.13.16.tgz",
@@ -3631,6 +3660,12 @@
"url": "https://github.com/sponsors/ljharb" "url": "https://github.com/sponsors/ljharb"
} }
}, },
"node_modules/es-main": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/es-main/-/es-main-1.0.2.tgz",
"integrity": "sha512-LLgW8Cby/FiyQygrI23q2EswulHiDKoyjWlDRgTGXjQ3iRim2R26VfoehpxI5oKRXSNams3L/80KtggoUdxdDQ==",
"dev": true
},
"node_modules/es-module-lexer": { "node_modules/es-module-lexer": {
"version": "0.4.1", "version": "0.4.1",
"resolved": "https://registry.npmjs.org/es-module-lexer/-/es-module-lexer-0.4.1.tgz", "resolved": "https://registry.npmjs.org/es-module-lexer/-/es-module-lexer-0.4.1.tgz",
@@ -11410,6 +11445,25 @@
"source-map": "^0.5.0" "source-map": "^0.5.0"
} }
}, },
"@babel/eslint-parser": {
"version": "7.13.14",
"resolved": "https://registry.npmjs.org/@babel/eslint-parser/-/eslint-parser-7.13.14.tgz",
"integrity": "sha512-I0HweR36D73Ibn/FfrRDMKlMqJHFwidIUgYdMpH+aXYuQC+waq59YaJ6t9e9N36axJ82v1jR041wwqDrDXEwRA==",
"dev": true,
"requires": {
"eslint-scope": "^5.1.0",
"eslint-visitor-keys": "^1.3.0",
"semver": "^6.3.0"
},
"dependencies": {
"eslint-visitor-keys": {
"version": "1.3.0",
"resolved": "https://registry.npmjs.org/eslint-visitor-keys/-/eslint-visitor-keys-1.3.0.tgz",
"integrity": "sha512-6J72N8UNa462wa/KFODt/PJ3IU60SDpC3QXC1Hjc1BXXpfL2C9R5+AU7jhe0F6GREqVMh4Juu+NY7xn+6dipUQ==",
"dev": true
}
}
},
"@babel/generator": { "@babel/generator": {
"version": "7.13.16", "version": "7.13.16",
"resolved": "https://registry.npmjs.org/@babel/generator/-/generator-7.13.16.tgz", "resolved": "https://registry.npmjs.org/@babel/generator/-/generator-7.13.16.tgz",
@@ -14260,6 +14314,12 @@
"unbox-primitive": "^1.0.0" "unbox-primitive": "^1.0.0"
} }
}, },
"es-main": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/es-main/-/es-main-1.0.2.tgz",
"integrity": "sha512-LLgW8Cby/FiyQygrI23q2EswulHiDKoyjWlDRgTGXjQ3iRim2R26VfoehpxI5oKRXSNams3L/80KtggoUdxdDQ==",
"dev": true
},
"es-module-lexer": { "es-module-lexer": {
"version": "0.4.1", "version": "0.4.1",
"resolved": "https://registry.npmjs.org/es-module-lexer/-/es-module-lexer-0.4.1.tgz", "resolved": "https://registry.npmjs.org/es-module-lexer/-/es-module-lexer-0.4.1.tgz",
+16 -8
View File
@@ -13,23 +13,24 @@
"lint": "eslint tasks test src/ol examples config", "lint": "eslint tasks test src/ol examples config",
"pretest": "npm run lint && npm run typecheck", "pretest": "npm run lint && npm run typecheck",
"test-rendering": "node test/rendering/test.js", "test-rendering": "node test/rendering/test.js",
"test-spec": "npm run karma -- --single-run --log-level error", "test-browser": "npm run karma -- --single-run --log-level error",
"test": "npm run test-spec && npm run test-rendering -- --force", "test-node": "mocha --recursive test/node",
"karma": "karma start test/browser/karma.config.js", "test": "npm run test-browser && npm run test-node && npm run test-rendering -- --force",
"karma": "karma start test/browser/karma.config.cjs",
"start": "npm run serve-examples", "start": "npm run serve-examples",
"serve-examples": "webpack serve --config examples/webpack/config.js --mode development", "serve-examples": "webpack serve --config examples/webpack/config.js --mode development",
"build-examples": "webpack --config examples/webpack/config.js --mode production", "build-examples": "webpack --config examples/webpack/config.js --mode production",
"build-package": "npm run transpile && npm run copy-css && node tasks/prepare-package", "build-package": "npm run transpile && npm run copy-css && node tasks/prepare-package.js",
"build-index": "npm run build-package && node tasks/generate-index", "build-index": "npm run build-package && node tasks/generate-index.js",
"build-legacy": "shx rm -rf build && npm run build-index && webpack --config config/webpack-config-legacy-build.js && cleancss --source-map src/ol/ol.css -o build/legacy/ol.css", "build-legacy": "shx rm -rf build && npm run build-index && webpack --config config/webpack-config-legacy-build.js && cleancss --source-map src/ol/ol.css -o build/legacy/ol.css",
"build-site": "npm run build-examples && npm run apidoc && mkdir -p build/site && cp site/index.html build/site && mv build/apidoc build/site/apidoc && mv build/examples build/site/examples", "build-site": "npm run build-examples && npm run apidoc && mkdir -p build/site && cp site/index.html build/site && mv build/apidoc build/site/apidoc && mv build/examples build/site/examples",
"copy-css": "shx cp src/ol/ol.css build/ol/ol.css", "copy-css": "shx cp src/ol/ol.css build/ol/ol.css",
"transpile": "shx rm -rf build/ol && shx mkdir -p build/ol && shx cp -rf src/ol build/ol/src && node tasks/serialize-workers && tsc --project config/tsconfig-build.json", "transpile": "shx rm -rf build/ol && shx mkdir -p build/ol && shx cp -rf src/ol build/ol/src && node tasks/serialize-workers.cjs && tsc --project config/tsconfig-build.json",
"typecheck": "tsc --pretty", "typecheck": "tsc --pretty",
"apidoc-debug": "shx rm -rf build/apidoc && node --inspect-brk=9229 ./node_modules/jsdoc/jsdoc.js -R config/jsdoc/api/index.md -c config/jsdoc/api/conf.json -P package.json -d build/apidoc", "apidoc-debug": "shx rm -rf build/apidoc && node --inspect-brk=9229 ./node_modules/jsdoc/jsdoc.js -R config/jsdoc/api/index.md -c config/jsdoc/api/conf.json -P package.json -d build/apidoc",
"apidoc": "shx rm -rf build/apidoc && jsdoc -R config/jsdoc/api/index.md -c config/jsdoc/api/conf.json -P package.json -d build/apidoc" "apidoc": "shx rm -rf build/apidoc && jsdoc -R config/jsdoc/api/index.md -c config/jsdoc/api/conf.json -P package.json -d build/apidoc"
}, },
"main": "index.js", "type": "module",
"repository": { "repository": {
"type": "git", "type": "git",
"url": "git://github.com/openlayers/openlayers.git" "url": "git://github.com/openlayers/openlayers.git"
@@ -49,6 +50,7 @@
}, },
"devDependencies": { "devDependencies": {
"@babel/core": "^7.6.4", "@babel/core": "^7.6.4",
"@babel/eslint-parser": "^7.13.14",
"@babel/preset-env": "^7.4.4", "@babel/preset-env": "^7.4.4",
"@openlayers/eslint-plugin": "^4.0.0", "@openlayers/eslint-plugin": "^4.0.0",
"@rollup/plugin-babel": "^5.3.0", "@rollup/plugin-babel": "^5.3.0",
@@ -66,6 +68,7 @@
"copy-webpack-plugin": "^8.0.0", "copy-webpack-plugin": "^8.0.0",
"coverage-istanbul-loader": "^3.0.5", "coverage-istanbul-loader": "^3.0.5",
"coveralls": "3.1.0", "coveralls": "3.1.0",
"es-main": "^1.0.2",
"eslint": "^7.2.0", "eslint": "^7.2.0",
"eslint-config-openlayers": "^15.0.0", "eslint-config-openlayers": "^15.0.0",
"expect.js": "0.3.1", "expect.js": "0.3.1",
@@ -100,7 +103,7 @@
"shx": "^0.3.2", "shx": "^0.3.2",
"sinon": "^10.0.0", "sinon": "^10.0.0",
"terser-webpack-plugin": "^5.1.1", "terser-webpack-plugin": "^5.1.1",
"typescript": "^4.2.3", "typescript": "^4.2.4",
"url-polyfill": "^1.1.5", "url-polyfill": "^1.1.5",
"walk": "^2.3.9", "walk": "^2.3.9",
"webpack": "^5.27.2", "webpack": "^5.27.2",
@@ -113,10 +116,15 @@
}, },
"eslintConfig": { "eslintConfig": {
"extends": "openlayers", "extends": "openlayers",
"parser": "@babel/eslint-parser",
"parserOptions": {
"requireConfigFile": false
},
"plugins": [ "plugins": [
"@openlayers" "@openlayers"
], ],
"rules": { "rules": {
"import/no-commonjs": "error",
"@openlayers/no-exclusive-tests": [ "@openlayers/no-exclusive-tests": [
"error", "error",
{ {
+13 -13
View File
@@ -1,6 +1,9 @@
const fse = require('fs-extra'); import esMain from 'es-main';
const path = require('path'); import fse from 'fs-extra';
const generateInfo = require('./generate-info'); import generateInfo from './generate-info.js';
import path from 'path';
import {dirname} from 'path';
import {fileURLToPath} from 'url';
/** /**
* Read the symbols from info file. * Read the symbols from info file.
@@ -21,11 +24,11 @@ function getImport(symbol, member) {
const defaultExport = symbol.name.split('~'); const defaultExport = symbol.name.split('~');
const namedExport = symbol.name.split('.'); const namedExport = symbol.name.split('.');
if (defaultExport.length > 1 && defaultExport[0].indexOf('.') === -1) { if (defaultExport.length > 1 && defaultExport[0].indexOf('.') === -1) {
const from = defaultExport[0].replace(/^module\:/, './'); const from = defaultExport[0].replace(/^module\:/, './') + '.js';
const importName = from.replace(/[.\/]+/g, '$'); const importName = from.replace(/[.\/]+/g, '$');
return `import ${importName} from '${from}';`; return `import ${importName} from '${from}';`;
} else if (namedExport.length > 1 && member) { } else if (namedExport.length > 1 && member) {
const from = namedExport[0].replace(/^module\:/, './'); const from = namedExport[0].replace(/^module\:/, './') + '.js';
const importName = from.replace(/[.\/]+/g, '_'); const importName = from.replace(/[.\/]+/g, '_');
return `import {${member} as ${importName}$${member}} from '${from}';`; return `import {${member} as ${importName}$${member}} from '${from}';`;
} }
@@ -97,7 +100,7 @@ function generateExports(symbols) {
* Generate the exports code. * Generate the exports code.
* @return {Promise<string>} Resolves with the exports code. * @return {Promise<string>} Resolves with the exports code.
*/ */
async function main() { export default async function main() {
const symbols = await getSymbols(); const symbols = await getSymbols();
return generateExports(symbols); return generateExports(symbols);
} }
@@ -106,18 +109,15 @@ async function main() {
* If running this module directly, read the config file, call the main * If running this module directly, read the config file, call the main
* function, and write the output file. * function, and write the output file.
*/ */
if (require.main === module) { if (esMain(import.meta)) {
const baseDir = dirname(fileURLToPath(import.meta.url));
main() main()
.then(async (code) => { .then(async (code) => {
const filepath = path.join(__dirname, '..', 'build', 'index.js'); const filepath = path.join(baseDir, '..', 'build', 'index.js');
await fse.outputFile(filepath, code); await fse.outputFile(filepath, code);
}) })
.catch((err) => { .catch((err) => {
process.stderr.write(`${err.message}\n`, () => process.exit(1)); process.stderr.write(`${err.message}\n`, () => process.exit(1));
}); });
} }
/**
* Export main function.
*/
module.exports = main;
+24 -18
View File
@@ -1,11 +1,16 @@
const fse = require('fs-extra'); import esMain from 'es-main';
const path = require('path'); import fse from 'fs-extra';
const spawn = require('child_process').spawn; import path from 'path';
const walk = require('walk').walk; import {dirname} from 'path';
const isWindows = process.platform.indexOf('win') === 0; import {fileURLToPath} from 'url';
import {spawn} from 'child_process';
import {walk} from 'walk';
const sourceDir = path.join(__dirname, '..', 'src'); const isWindows = process.platform.indexOf('win') === 0;
const infoPath = path.join(__dirname, '..', 'build', 'info.json'); const baseDir = dirname(fileURLToPath(import.meta.url));
const sourceDir = path.join(baseDir, '..', 'src');
const infoPath = path.join(baseDir, '..', 'build', 'info.json');
/** /**
* Get checked path of a binary. * Get checked path of a binary.
@@ -17,9 +22,15 @@ function getBinaryPath(binaryName) {
binaryName += '.cmd'; binaryName += '.cmd';
} }
const jsdocResolved = require.resolve('jsdoc/jsdoc.js'); const jsdocResolved = path.join(
baseDir,
'..',
'node_modules',
'jsdoc',
'jsdoc.js'
);
const expectedPaths = [ const expectedPaths = [
path.join(__dirname, '..', 'node_modules', '.bin', binaryName), path.join(baseDir, '..', 'node_modules', '.bin', binaryName),
path.resolve( path.resolve(
path.join(path.dirname(jsdocResolved), '..', '.bin', binaryName) path.join(path.dirname(jsdocResolved), '..', '.bin', binaryName)
), ),
@@ -40,7 +51,7 @@ function getBinaryPath(binaryName) {
const jsdoc = getBinaryPath('jsdoc'); const jsdoc = getBinaryPath('jsdoc');
const jsdocConfig = path.join( const jsdocConfig = path.join(
__dirname, baseDir,
'..', '..',
'config', 'config',
'jsdoc', 'jsdoc',
@@ -120,7 +131,7 @@ function spawnJSDoc(paths) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let output = ''; let output = '';
let errors = ''; let errors = '';
const cwd = path.join(__dirname, '..'); const cwd = path.join(baseDir, '..');
const child = spawn(jsdoc, ['-c', jsdocConfig].concat(paths), {cwd: cwd}); const child = spawn(jsdoc, ['-c', jsdocConfig].concat(paths), {cwd: cwd});
child.stdout.on('data', (data) => { child.stdout.on('data', (data) => {
@@ -162,7 +173,7 @@ async function write(info) {
* Generate info from the sources. * Generate info from the sources.
* @return {Promise<Error>} Resolves with the info object. * @return {Promise<Error>} Resolves with the info object.
*/ */
async function main() { export default async function main() {
const paths = await getPaths(); const paths = await getPaths();
return await spawnJSDoc(paths); return await spawnJSDoc(paths);
} }
@@ -170,15 +181,10 @@ async function main() {
/** /**
* If running this module directly, generate and write out the info.json file. * If running this module directly, generate and write out the info.json file.
*/ */
if (require.main === module) { if (esMain(import.meta)) {
main() main()
.then(write) .then(write)
.catch((err) => { .catch((err) => {
process.stderr.write(`${err.message}\n`, () => process.exit(1)); process.stderr.write(`${err.message}\n`, () => process.exit(1));
}); });
} }
/**
* Export main function.
*/
module.exports = main;
+16 -5
View File
@@ -1,7 +1,11 @@
const version = require('../package.json').version; import esMain from 'es-main';
const semver = require('semver'); import process from 'process';
import semver from 'semver';
import {promises as fs} from 'fs';
function nextVersion() { async function nextVersion() {
const pkg = await fs.readFile('../package.json', {encoding: 'utf8'});
const version = JSON.parse(pkg).version;
const s = semver.parse(version); const s = semver.parse(version);
if (!s) { if (!s) {
throw new Error(`Invalid version ${version}`); throw new Error(`Invalid version ${version}`);
@@ -9,6 +13,13 @@ function nextVersion() {
return `${s.major}.${s.minor}.${s.patch}-dev.${Date.now()}`; return `${s.major}.${s.minor}.${s.patch}-dev.${Date.now()}`;
} }
if (require.main === module) { if (esMain(import.meta)) {
process.stdout.write(`${nextVersion()}\n`); nextVersion()
.then((version) => {
process.stdout.write(`${version}\n`);
})
.catch((error) => {
process.stderr.write(`${error}\n`);
process.exit(1);
});
} }
+45 -31
View File
@@ -1,35 +1,49 @@
const fs = require('fs'); import esMain from 'es-main';
const path = require('path'); import fse from 'fs-extra';
const pkg = require('../package.json'); import path from 'path';
import {dirname} from 'path';
import {fileURLToPath} from 'url';
const buildDir = path.resolve(__dirname, '../build/ol'); const baseDir = dirname(fileURLToPath(import.meta.url));
const buildDir = path.resolve(baseDir, '../build/ol');
// update the version number in util.js async function main() {
const utilPath = path.join(buildDir, 'util.js'); const pkg = await fse.readJSON(path.resolve(baseDir, '../package.json'));
const versionRegEx = /var VERSION = '(.*)';/g;
const utilSrc = fs
.readFileSync(utilPath, 'utf-8')
.replace(versionRegEx, `var VERSION = '${pkg.version}';`);
fs.writeFileSync(utilPath, utilSrc, 'utf-8');
// write out simplified package.json // update the version number in util.js
delete pkg.scripts; const utilPath = path.join(buildDir, 'util.js');
delete pkg.devDependencies; const versionRegEx = /var VERSION = '(.*)';/g;
delete pkg.style; let utilSrc = await fse.readFile(utilPath, 'utf-8');
delete pkg.eslintConfig; utilSrc = utilSrc.replace(versionRegEx, `var VERSION = '${pkg.version}';`);
delete pkg.private; await fse.writeFile(utilPath, utilSrc, 'utf-8');
fs.writeFileSync(
path.join(buildDir, 'package.json'),
JSON.stringify(pkg, null, 2),
'utf-8'
);
// copy in readme and license files // write out simplified package.json
fs.copyFileSync( pkg.main = 'index.js';
path.resolve(__dirname, '../README.md'), delete pkg.scripts;
path.join(buildDir, 'README.md') delete pkg.devDependencies;
); delete pkg.style;
fs.copyFileSync( delete pkg.eslintConfig;
path.resolve(__dirname, '../LICENSE.md'), delete pkg.private;
path.join(buildDir, 'LICENSE.md') await fse.writeJSON(path.join(buildDir, 'package.json'), pkg, {spaces: 2});
);
// copy in readme and license files
await fse.copyFile(
path.resolve(baseDir, '../README.md'),
path.join(buildDir, 'README.md')
);
await fse.copyFile(
path.resolve(baseDir, '../LICENSE.md'),
path.join(buildDir, 'LICENSE.md')
);
}
/**
* If running this module directly, read the config file, call the main
* function, and write the output file.
*/
if (esMain(import.meta)) {
main().catch((err) => {
process.stderr.write(`${err.message}\n`, () => process.exit(1));
});
}
@@ -1,3 +1,5 @@
/* eslint-disable import/no-commonjs */
const path = require('path'); const path = require('path');
const babel = require('@rollup/plugin-babel').babel; const babel = require('@rollup/plugin-babel').babel;
const resolve = require('@rollup/plugin-node-resolve').nodeResolve; const resolve = require('@rollup/plugin-node-resolve').nodeResolve;
+1 -1
View File
@@ -1,7 +1,7 @@
## Included in this directory ## Included in this directory
- browser - Unit/integration tests run in a browser - browser - Unit/integration tests run in a browser
- node - Unit tests run with Node.js
- rendering - Tests that make assertions about rendered map output - rendering - Tests that make assertions about rendered map output
-5
View File
@@ -1,5 +0,0 @@
// require all modules ending in ".test.js" from the
// current directory and all subdirectories
const testsContext = require.context('./spec', true, /\.test\.js$/);
testsContext.keys().forEach(testsContext);
@@ -1,3 +1,5 @@
/* eslint-disable import/no-commonjs */
const path = require('path'); const path = require('path');
module.exports = function (karma) { module.exports = function (karma) {
@@ -45,7 +47,7 @@ module.exports = function (karma) {
pattern: path.resolve(__dirname, './test-extensions.js'), pattern: path.resolve(__dirname, './test-extensions.js'),
}, },
{ {
pattern: path.resolve(__dirname, './index_test.js'), pattern: 'spec/**/*.test.js',
watched: false, watched: false,
}, },
{ {
@@ -54,7 +56,6 @@ module.exports = function (karma) {
watched: false, watched: false,
}, },
], ],
exclude: ['**/*.test.js'],
proxies: { proxies: {
'/spec/': '/base/spec/', '/spec/': '/base/spec/',
}, },
@@ -99,7 +100,7 @@ module.exports = function (karma) {
use: { use: {
loader: path.join( loader: path.join(
__dirname, __dirname,
'../../examples/webpack/worker-loader.js' '../../examples/webpack/worker-loader.cjs'
), ),
}, },
include: [path.join(__dirname, '../../src/ol/worker')], include: [path.join(__dirname, '../../src/ol/worker')],
-11
View File
@@ -1,11 +0,0 @@
import {assert} from '../../../../src/ol/asserts.js';
describe('ol.asserts', function () {
describe('ol.asserts.assert', function () {
it('throws an exception', function () {
expect(function () {
assert(false, 42);
}).to.throwException();
});
});
});
-22
View File
@@ -1,22 +0,0 @@
import {getUid} from '../../../../src/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));
});
});
-162
View File
@@ -1,162 +0,0 @@
import {
clamp,
cosh,
lerp,
log2,
modulo,
solveLinearSystem,
toDegrees,
toRadians,
} from '../../../../src/ol/math.js';
describe('ol.math.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('ol.math.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('ol.math.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('ol.math.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('ol.math.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('ol.math.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('ol.math.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('ol.math.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);
});
});
-64
View File
@@ -1,64 +0,0 @@
import {assign, clear, getValues, isEmpty} from '../../../../src/ol/obj.js';
describe('ol.obj.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('ol.obj.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('ol.obj.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('ol.obj.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);
});
});
+8
View File
@@ -0,0 +1,8 @@
{
"env": {
"mocha": true
},
"parserOptions": {
"ecmaVersion": 2017
}
}
+36
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;
@@ -1,7 +1,8 @@
import AssertionError from '../../../../src/ol/AssertionError.js'; import AssertionError from '../../../src/ol/AssertionError.js';
import {VERSION} from '../../../../src/ol/util.js'; import expect from '../expect.js';
import {VERSION} from '../../../src/ol/util.js';
describe('ol.AssertionError', function () { describe('ol/AssertionError.js', function () {
it('generates an error', function () { it('generates an error', function () {
const error = new AssertionError(42); const error = new AssertionError(42);
expect(error).to.be.an(Error); expect(error).to.be.an(Error);
@@ -1,8 +1,10 @@
import Collection from '../../../../src/ol/Collection.js'; import Collection from '../../../src/ol/Collection.js';
import CollectionEventType from '../../../../src/ol/CollectionEventType.js'; import CollectionEventType from '../../../src/ol/CollectionEventType.js';
import {listen} from '../../../../src/ol/events.js'; import expect from '../expect.js';
import sinon from 'sinon';
import {listen} from '../../../src/ol/events.js';
describe('ol.collection', function () { describe('ol/Collection.js', function () {
let collection; let collection;
beforeEach(function () { beforeEach(function () {
@@ -1,6 +1,8 @@
import Disposable from '../../../../src/ol/Disposable.js'; import Disposable from '../../../src/ol/Disposable.js';
import expect from '../expect.js';
import sinon from 'sinon';
describe('ol.Disposable', function () { describe('ol/Disposable.js', function () {
describe('constructor', function () { describe('constructor', function () {
it('creates an instance', function () { it('creates an instance', function () {
const disposable = new Disposable(); const disposable = new Disposable();
@@ -1,9 +1,11 @@
import Feature, {createStyleFunction} from '../../../../src/ol/Feature.js'; import Feature, {createStyleFunction} from '../../../src/ol/Feature.js';
import Point from '../../../../src/ol/geom/Point.js'; import Point from '../../../src/ol/geom/Point.js';
import Style from '../../../../src/ol/style/Style.js'; import Style from '../../../src/ol/style/Style.js';
import {isEmpty} from '../../../../src/ol/obj.js'; import expect from '../expect.js';
import sinon from 'sinon';
import {isEmpty} from '../../../src/ol/obj.js';
describe('ol.Feature', function () { describe('ol/Feature.js', function () {
describe('constructor', function () { describe('constructor', function () {
it('creates a new feature', function () { it('creates a new feature', function () {
const feature = new Feature(); const feature = new Feature();
@@ -1,7 +1,9 @@
import BaseObject from '../../../../src/ol/Object.js'; import BaseObject from '../../../src/ol/Object.js';
import {listen} from '../../../../src/ol/events.js'; import expect from '../expect.js';
import sinon from 'sinon';
import {listen} from '../../../src/ol/events.js';
describe('ol.Object', function () { describe('ol/Object.js', function () {
let o; let o;
beforeEach(function () { beforeEach(function () {
o = new BaseObject(); o = new BaseObject();
@@ -1,7 +1,9 @@
import EventTarget from '../../../../src/ol/events/Target.js'; import EventTarget from '../../../src/ol/events/Target.js';
import Observable, {unByKey} from '../../../../src/ol/Observable.js'; import Observable, {unByKey} from '../../../src/ol/Observable.js';
import expect from '../expect.js';
import sinon from 'sinon';
describe('ol.Observable', function () { describe('ol/Observable.js', function () {
describe('constructor', function () { describe('constructor', function () {
it('creates a new observable', function () { it('creates a new observable', function () {
const observable = new Observable(); const observable = new Observable();
+612
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);
});
});
});
+12
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();
});
});
});
@@ -1,5 +1,6 @@
import Circle from '../../../../src/ol/geom/Circle.js'; import Circle from '../../../src/ol/geom/Circle.js';
import Projection from '../../../../src/ol/proj/Projection.js'; import Projection from '../../../src/ol/proj/Projection.js';
import expect from '../expect.js';
import { import {
add as addCoordinate, add as addCoordinate,
closestOnCircle, closestOnCircle,
@@ -13,8 +14,8 @@ import {
toStringHDMS, toStringHDMS,
toStringXY, toStringXY,
wrapX, wrapX,
} from '../../../../src/ol/coordinate.js'; } from '../../../src/ol/coordinate.js';
import {get} from '../../../../src/ol/proj.js'; import {get} from '../../../src/ol/proj.js';
describe('ol.coordinate', function () { describe('ol.coordinate', function () {
describe('#add', function () { describe('#add', function () {
@@ -1,4 +1,5 @@
import {getFontParameters} from '../../../../src/ol/css.js'; import expect from '../expect.js';
import {getFontParameters} from '../../../src/ol/css.js';
describe('ol.css', function () { describe('ol.css', function () {
describe('getFontParameters()', function () { describe('getFontParameters()', function () {
@@ -1,7 +1,9 @@
import EventTarget from '../../../../src/ol/events/Target.js'; import EventTarget from '../../../src/ol/events/Target.js';
import {listen, listenOnce, unlistenByKey} from '../../../../src/ol/events.js'; import expect from '../expect.js';
import sinon from 'sinon';
import {listen, listenOnce, unlistenByKey} from '../../../src/ol/events.js';
describe('ol.events', function () { describe('ol/events.js', function () {
let add, target; let add, target;
beforeEach(function () { beforeEach(function () {
@@ -1,9 +1,10 @@
import Disposable from '../../../../../src/ol/Disposable.js'; import Disposable from '../../../../src/ol/Disposable.js';
import Event from '../../../../../src/ol/events/Event.js'; import Event from '../../../../src/ol/events/Event.js';
import EventTarget from '../../../../../src/ol/events/Target.js'; import EventTarget from '../../../../src/ol/events/Target.js';
import {listen} from '../../../../../src/ol/events.js'; import expect from '../../expect.js';
import {listen} from '../../../../src/ol/events.js';
describe('ol.events.EventTarget', function () { describe('ol/events/Target.js', function () {
let called, events, eventTarget, spy1, spy2, spy3; let called, events, eventTarget, spy1, spy2, spy3;
beforeEach(function () { beforeEach(function () {
@@ -166,7 +167,7 @@ describe('ol.events.EventTarget', function () {
describe('#dispose()', function () { describe('#dispose()', function () {
it('cleans up foreign references', function () { it('cleans up foreign references', function () {
listen(eventTarget, 'foo', spy1, document); listen(eventTarget, 'foo', spy1);
expect(eventTarget.hasListener('foo')).to.be(true); expect(eventTarget.hasListener('foo')).to.be(true);
eventTarget.dispose(); eventTarget.dispose();
expect(eventTarget.hasListener('foo')).to.be(false); expect(eventTarget.hasListener('foo')).to.be(false);
@@ -1,9 +1,11 @@
import Event, { import Event, {
preventDefault, preventDefault,
stopPropagation, stopPropagation,
} from '../../../../../src/ol/events/Event.js'; } from '../../../../src/ol/events/Event.js';
import expect from '../../expect.js';
import sinon from 'sinon';
describe('ol.events.Event', function () { describe('ol/events/Event.js', function () {
describe('constructor', function () { describe('constructor', function () {
it('takes a type as argument', function () { it('takes a type as argument', function () {
const event = new Event('foo'); const event = new Event('foo');
@@ -1,8 +1,11 @@
import * as _ol_extent_ from '../../../../src/ol/extent.js'; import * as _ol_extent_ from '../../../src/ol/extent.js';
import {get, getTransform} from '../../../../src/ol/proj.js'; import expect from '../expect.js';
import {register} from '../../../../src/ol/proj/proj4.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', function () { describe('ol/extent.js', function () {
describe('buffer', function () { describe('buffer', function () {
it('buffers an extent by some value', function () { it('buffers an extent by some value', function () {
const extent = [-10, -20, 10, 20]; const extent = [-10, -20, 10, 20];
@@ -1,6 +1,7 @@
import {memoizeOne} from '../../../../src/ol/functions.js'; import expect from '../expect.js';
import {memoizeOne} from '../../../src/ol/functions.js';
describe('ol/functions', function () { describe('ol/functions.js', function () {
describe('memoizeOne()', function () { describe('memoizeOne()', function () {
it('returns the result from the first call when called a second time with the same args', function () { it('returns the result from the first call when called a second time with the same args', function () {
const arg1 = {}; const arg1 = {};
@@ -1,6 +1,8 @@
import Circle from '../../../../../src/ol/geom/Circle.js'; import Circle from '../../../../src/ol/geom/Circle.js';
import expect from '../../expect.js';
import sinon from 'sinon';
describe('ol.geom.Circle', function () { describe('ol/geom/Circle.js', function () {
describe('with a unit circle', function () { describe('with a unit circle', function () {
let circle; let circle;
beforeEach(function () { beforeEach(function () {
@@ -1,10 +1,12 @@
import Geometry from '../../../../../src/ol/geom/Geometry.js'; import Geometry from '../../../../src/ol/geom/Geometry.js';
import GeometryCollection from '../../../../../src/ol/geom/GeometryCollection.js'; import GeometryCollection from '../../../../src/ol/geom/GeometryCollection.js';
import LineString from '../../../../../src/ol/geom/LineString.js'; import LineString from '../../../../src/ol/geom/LineString.js';
import Point from '../../../../../src/ol/geom/Point.js'; import Point from '../../../../src/ol/geom/Point.js';
import Polygon from '../../../../../src/ol/geom/Polygon.js'; import Polygon from '../../../../src/ol/geom/Polygon.js';
import expect from '../../expect.js';
import sinon from 'sinon';
describe('ol.geom.GeometryCollection', function () { describe('ol/geom/GeometryCollection.js', function () {
const outer = [ const outer = [
[0, 0], [0, 0],
[0, 10], [0, 10],
@@ -1,7 +1,9 @@
import LineString from '../../../../../src/ol/geom/LineString.js'; import LineString from '../../../../src/ol/geom/LineString.js';
import {isEmpty} from '../../../../../src/ol/extent.js'; import expect from '../../expect.js';
import sinon from 'sinon';
import {isEmpty} from '../../../../src/ol/extent.js';
describe('ol.geom.LineString', function () { describe('ol/geom/LineString.js', function () {
it('cannot be constructed with a null geometry', function () { it('cannot be constructed with a null geometry', function () {
expect(function () { expect(function () {
return new LineString(null); return new LineString(null);
@@ -1,8 +1,9 @@
import LineString from '../../../../../src/ol/geom/LineString.js'; import LineString from '../../../../src/ol/geom/LineString.js';
import MultiLineString from '../../../../../src/ol/geom/MultiLineString.js'; import MultiLineString from '../../../../src/ol/geom/MultiLineString.js';
import {isEmpty} from '../../../../../src/ol/extent.js'; import expect from '../../expect.js';
import {isEmpty} from '../../../../src/ol/extent.js';
describe('ol.geom.MultiLineString', function () { describe('ol/geom/MultiLineString.js', function () {
it('cannot be constructed with a null geometry', function () { it('cannot be constructed with a null geometry', function () {
expect(function () { expect(function () {
return new MultiLineString(null); return new MultiLineString(null);
@@ -1,8 +1,10 @@
import MultiPoint from '../../../../../src/ol/geom/MultiPoint.js'; import MultiPoint from '../../../../src/ol/geom/MultiPoint.js';
import Point from '../../../../../src/ol/geom/Point.js'; import Point from '../../../../src/ol/geom/Point.js';
import {isEmpty} from '../../../../../src/ol/extent.js'; import expect from '../../expect.js';
import sinon from 'sinon';
import {isEmpty} from '../../../../src/ol/extent.js';
describe('ol.geom.MultiPoint', function () { describe('ol/geom/MultiPoint.js', function () {
it('cannot be constructed with a null geometry', function () { it('cannot be constructed with a null geometry', function () {
expect(function () { expect(function () {
return new MultiPoint(null); return new MultiPoint(null);
@@ -1,7 +1,8 @@
import MultiPolygon from '../../../../../src/ol/geom/MultiPolygon.js'; import MultiPolygon from '../../../../src/ol/geom/MultiPolygon.js';
import Polygon from '../../../../../src/ol/geom/Polygon.js'; import Polygon from '../../../../src/ol/geom/Polygon.js';
import expect from '../../expect.js';
describe('ol.geom.MultiPolygon', function () { describe('ol/geom/MultiPolygon.js', function () {
it('cannot be constructed with a null geometry', function () { it('cannot be constructed with a null geometry', function () {
expect(function () { expect(function () {
return new MultiPolygon(null); return new MultiPolygon(null);
@@ -1,10 +1,12 @@
import Point from '../../../../../src/ol/geom/Point.js'; import Point from '../../../../src/ol/geom/Point.js';
import expect from '../../expect.js';
import sinon from 'sinon';
import { import {
get as getProjection, get as getProjection,
getTransformFromProjections, getTransformFromProjections,
} from '../../../../../src/ol/proj.js'; } from '../../../../src/ol/proj.js';
describe('ol.geom.Point', function () { describe('ol/geom/Point.js', function () {
it('cannot be constructed with a null geometry', function () { it('cannot be constructed with a null geometry', function () {
expect(function () { expect(function () {
return new Point(null); return new Point(null);
@@ -1,12 +1,13 @@
import Circle from '../../../../../src/ol/geom/Circle.js'; import Circle from '../../../../src/ol/geom/Circle.js';
import LinearRing from '../../../../../src/ol/geom/LinearRing.js'; import LinearRing from '../../../../src/ol/geom/LinearRing.js';
import Polygon, { import Polygon, {
fromCircle, fromCircle,
fromExtent, fromExtent,
} from '../../../../../src/ol/geom/Polygon.js'; } from '../../../../src/ol/geom/Polygon.js';
import {boundingExtent, isEmpty} from '../../../../../src/ol/extent.js'; import expect from '../../expect.js';
import {boundingExtent, isEmpty} from '../../../../src/ol/extent.js';
describe('ol/geom/Polygon', function () { describe('ol/geom/Polygon.js', function () {
it('cannot be constructed with a null geometry', function () { it('cannot be constructed with a null geometry', function () {
expect(function () { expect(function () {
return new Polygon(null); return new Polygon(null);
@@ -1,10 +1,8 @@
import { import expect from '../../../expect.js';
linearRing, import {linearRing, linearRings} from '../../../../../src/ol/geom/flat/area.js';
linearRings,
} from '../../../../../../src/ol/geom/flat/area.js';
describe('ol.geom.flat.area', function () { describe('ol/geom/flat/area.js', function () {
describe('ol.geom.flat.area.linearRing', function () { describe('linearRing', function () {
it('calculates the area of a triangle', function () { it('calculates the area of a triangle', function () {
const area = linearRing([0, 0, 0.5, 1, 1, 0], 0, 6, 2); const area = linearRing([0, 0, 0.5, 1, 1, 0], 0, 6, 2);
expect(area).to.be(0.5); expect(area).to.be(0.5);
@@ -16,7 +14,7 @@ describe('ol.geom.flat.area', function () {
}); });
}); });
describe('ol.geom.flat.area.linearRings', function () { describe('linearRings', function () {
it('calculates the area with holes', function () { it('calculates the area with holes', function () {
const area = linearRings( const area = linearRings(
[0, 0, 0, 3, 3, 3, 3, 0, 1, 1, 2, 1, 2, 2, 1, 2], [0, 0, 0, 3, 3, 3, 3, 0, 1, 1, 2, 1, 2, 2, 1, 2],
@@ -1,8 +1,9 @@
import MultiPolygon from '../../../../../../src/ol/geom/MultiPolygon.js'; import MultiPolygon from '../../../../../src/ol/geom/MultiPolygon.js';
import {linearRingss as linearRingssCenter} from '../../../../../../src/ol/geom/flat/center.js'; import expect from '../../../expect.js';
import {linearRingss as linearRingssCenter} from '../../../../../src/ol/geom/flat/center.js';
describe('ol.geom.flat.center', function () { describe('ol/geom/flat/center.js', function () {
describe('ol.geom.flat.center.linearRingss', function () { describe('linearRingss', function () {
it('calculates the center of a square', function () { it('calculates the center of a square', function () {
const squareMultiPoly = new MultiPolygon([ const squareMultiPoly = new MultiPolygon([
[ [
@@ -1,13 +1,14 @@
import expect from '../../../expect.js';
import { import {
assignClosestPoint, assignClosestPoint,
maxSquaredDelta, maxSquaredDelta,
} from '../../../../../../src/ol/geom/flat/closest.js'; } from '../../../../../src/ol/geom/flat/closest.js';
describe('ol.geom.flat.closest', function () { describe('ol/geom/flat/closest.js', function () {
describe('with simple data', function () { describe('with simple data', function () {
const flatCoordinates = [0, 0, 1, 0, 3, 0, 5, 0, 6, 0, 8, 0, 11, 0]; const flatCoordinates = [0, 0, 1, 0, 3, 0, 5, 0, 6, 0, 8, 0, 11, 0];
describe('ol.geom.flat.closest.maxSquaredDelta', function () { describe('maxSquaredDelta', function () {
it('returns the expected value in simple cases', function () { it('returns the expected value in simple cases', function () {
expect( expect(
maxSquaredDelta(flatCoordinates, 0, flatCoordinates.length, 2, 0) maxSquaredDelta(flatCoordinates, 0, flatCoordinates.length, 2, 0)
@@ -15,7 +16,7 @@ describe('ol.geom.flat.closest', function () {
}); });
}); });
describe('ol.geom.flat.closest.assignClosestPoint', function () { describe('assignClosestPoint', function () {
it('returns the expected value', function () { it('returns the expected value', function () {
const maxDelta = Math.sqrt( const maxDelta = Math.sqrt(
maxSquaredDelta(flatCoordinates, 0, flatCoordinates.length, 2, 0) maxSquaredDelta(flatCoordinates, 0, flatCoordinates.length, 2, 0)
@@ -290,7 +291,7 @@ describe('ol.geom.flat.closest', function () {
480.77, 480.77,
]; ];
describe('ol.geom.closest.maxSquaredDelta', function () { describe('maxSquaredDelta', function () {
it('returns the expected value', function () { it('returns the expected value', function () {
expect( expect(
maxSquaredDelta(flatCoordinates, 0, flatCoordinates.length, 2, 0) maxSquaredDelta(flatCoordinates, 0, flatCoordinates.length, 2, 0)
@@ -298,7 +299,7 @@ describe('ol.geom.flat.closest', function () {
}); });
}); });
describe('ol.geom.flat.closest.assignClosestPoint', function () { describe('assignClosestPoint', function () {
it('returns the expected value', function () { it('returns the expected value', function () {
const maxDelta = Math.sqrt( const maxDelta = Math.sqrt(
maxSquaredDelta(flatCoordinates, 0, flatCoordinates.length, 2, 0) maxSquaredDelta(flatCoordinates, 0, flatCoordinates.length, 2, 0)
@@ -358,7 +359,7 @@ describe('ol.geom.flat.closest', function () {
const flatCoordinates = [0, 0, 10, -10, 2, 2, 30, -20]; const flatCoordinates = [0, 0, 10, -10, 2, 2, 30, -20];
const stride = 4; const stride = 4;
describe('ol.geom.flat.closest.assignClosestPoint', function () { describe('assignClosestPoint', function () {
it('interpolates M coordinates', function () { it('interpolates M coordinates', function () {
const maxDelta = Math.sqrt( const maxDelta = Math.sqrt(
maxSquaredDelta(flatCoordinates, 0, flatCoordinates.length, stride, 0) maxSquaredDelta(flatCoordinates, 0, flatCoordinates.length, stride, 0)
@@ -1,6 +1,7 @@
import {linearRingContainsXY} from '../../../../../../src/ol/geom/flat/contains.js'; import expect from '../../../expect.js';
import {linearRingContainsXY} from '../../../../../src/ol/geom/flat/contains.js';
describe('ol.geom.flat.contains', function () { describe('ol/geom/flat/contains.js', function () {
describe('with simple data', function () { describe('with simple data', function () {
const flatCoordinatesSimple = [0, 0, 1, 0, 1, 1, 0, 1]; const flatCoordinatesSimple = [0, 0, 1, 0, 1, 1, 0, 1];
const flatCoordinatesNonSimple = [ const flatCoordinatesNonSimple = [
@@ -26,7 +27,7 @@ describe('ol.geom.flat.contains', function () {
4, 4,
]; ];
describe('ol.geom.flat.contains.linearRingContainsXY', function () { describe('linearRingContainsXY', function () {
it('returns true for point inside a simple polygon', function () { it('returns true for point inside a simple polygon', function () {
expect( expect(
linearRingContainsXY( linearRingContainsXY(
@@ -1,10 +1,11 @@
import expect from '../../../expect.js';
import { import {
deflateCoordinates, deflateCoordinates,
deflateCoordinatesArray, deflateCoordinatesArray,
} from '../../../../../../src/ol/geom/flat/deflate.js'; } from '../../../../../src/ol/geom/flat/deflate.js';
describe('ol.geom.flat.deflate', function () { describe('ol/geom/flat/deflate.js', function () {
describe('ol.geom.flat.deflate.deflateCoordinates', function () { describe('deflateCoordinates', function () {
let flatCoordinates; let flatCoordinates;
beforeEach(function () { beforeEach(function () {
flatCoordinates = []; flatCoordinates = [];
@@ -25,7 +26,7 @@ describe('ol.geom.flat.deflate', function () {
}); });
}); });
describe('ol.geom.flat.deflate.deflateCoordinatesArray', function () { describe('deflateCoordinatesArray', function () {
let flatCoordinates; let flatCoordinates;
beforeEach(function () { beforeEach(function () {
flatCoordinates = []; flatCoordinates = [];
@@ -1,7 +1,8 @@
import {flipXY} from '../../../../../../src/ol/geom/flat/flip.js'; import expect from '../../../expect.js';
import {flipXY} from '../../../../../src/ol/geom/flat/flip.js';
describe('ol.geom.flat.flip', function () { describe('ol/geom/flat/flip.js', function () {
describe('ol.geom.flat.flip.flipXY', function () { describe('flipXY', function () {
it('can flip XY coordinates', function () { it('can flip XY coordinates', function () {
const flatCoordinates = flipXY([1, 2, 3, 4], 0, 4, 2); const flatCoordinates = flipXY([1, 2, 3, 4], 0, 4, 2);
expect(flatCoordinates).to.eql([2, 1, 4, 3]); expect(flatCoordinates).to.eql([2, 1, 4, 3]);
@@ -1,10 +1,11 @@
import expect from '../../../expect.js';
import { import {
inflateCoordinates, inflateCoordinates,
inflateCoordinatesArray, inflateCoordinatesArray,
} from '../../../../../../src/ol/geom/flat/inflate.js'; } from '../../../../../src/ol/geom/flat/inflate.js';
describe('ol.geom.flat.inflate', function () { describe('ol/geom/flat/inflate.js', function () {
describe('ol.geom.flat.inflate.inflateCoordinates', function () { describe('inflateCoordinates', function () {
it('inflates coordinates', function () { it('inflates coordinates', function () {
const coordinates = inflateCoordinates([1, 2, 3, 4], 0, 4, 2); const coordinates = inflateCoordinates([1, 2, 3, 4], 0, 4, 2);
expect(coordinates).to.eql([ expect(coordinates).to.eql([
@@ -14,7 +15,7 @@ describe('ol.geom.flat.inflate', function () {
}); });
}); });
describe('ol.geom.flat.inflate.inflateCoordinatesArray', function () { describe('inflateCoordinatesArray', function () {
it('inflates arrays of coordinates', function () { it('inflates arrays of coordinates', function () {
const coordinatess = inflateCoordinatesArray( const coordinatess = inflateCoordinatesArray(
[1, 2, 3, 4, 5, 6, 7, 8], [1, 2, 3, 4, 5, 6, 7, 8],
@@ -1,7 +1,8 @@
import {interpolatePoint} from '../../../../../../src/ol/geom/flat/interpolate.js'; import expect from '../../../expect.js';
import {interpolatePoint} from '../../../../../src/ol/geom/flat/interpolate.js';
describe('ol.geom.flat.interpolate', function () { describe('ol/geom/flat/interpolate.js', function () {
describe('ol.geom.flat.interpolate.interpolatePoint', function () { describe('interpolatePoint', function () {
it('returns the expected value for single points', function () { it('returns the expected value for single points', function () {
const flatCoordinates = [0, 1]; const flatCoordinates = [0, 1];
const point = interpolatePoint(flatCoordinates, 0, 2, 2, 0.5); const point = interpolatePoint(flatCoordinates, 0, 2, 2, 0.5);
@@ -1,11 +1,12 @@
import expect from '../../../expect.js';
import { import {
intersectsLineString, intersectsLineString,
intersectsLinearRing, intersectsLinearRing,
intersectsLinearRingArray, intersectsLinearRingArray,
} from '../../../../../../src/ol/geom/flat/intersectsextent.js'; } from '../../../../../src/ol/geom/flat/intersectsextent.js';
describe('ol.geom.flat.intersectsextent', function () { describe('ol/geom/flat/intersectsextent.js', function () {
describe('ol.geom.flat.intersectsextent.intersectsLineString', function () { describe('intersectsLineString', function () {
let flatCoordinates; let flatCoordinates;
beforeEach(function () { beforeEach(function () {
flatCoordinates = [0, 0, 1, 1, 2, 2]; flatCoordinates = [0, 0, 1, 1, 2, 2];
@@ -88,7 +89,7 @@ describe('ol.geom.flat.intersectsextent', function () {
}); });
}); });
describe('ol.geom.flat.intersectsextent.intersectsLinearRing', function () { describe('intersectsLinearRing', function () {
let flatCoordinates; let flatCoordinates;
beforeEach(function () { beforeEach(function () {
flatCoordinates = [0, 0, 1, 1, 2, 0, 1, -1, 0, 0]; flatCoordinates = [0, 0, 1, 1, 2, 0, 1, -1, 0, 0];
@@ -138,7 +139,7 @@ describe('ol.geom.flat.intersectsextent', function () {
}); });
}); });
describe('ol.geom.flat.intersectsextent.intersectsLinearRingArray', function () { describe('intersectsLinearRingArray', function () {
let flatCoordinates; let flatCoordinates;
let ends; let ends;
beforeEach(function () { beforeEach(function () {
@@ -1,10 +1,11 @@
import expect from '../../../expect.js';
import { import {
lineStringLength, lineStringLength,
linearRingLength, linearRingLength,
} from '../../../../../../src/ol/geom/flat/length.js'; } from '../../../../../src/ol/geom/flat/length.js';
describe('ol.geom.flat.length', function () { describe('ol/geom/flat/length.js', function () {
describe('ol.geom.flat.length.lineStringLength', function () { describe('lineStringLength', function () {
describe('stride = 2', function () { describe('stride = 2', function () {
const flatCoords = [0, 0, 1, 0, 1, 1, 0, 1]; const flatCoords = [0, 0, 1, 0, 1, 1, 0, 1];
const stride = 2; const stride = 2;
@@ -64,7 +65,7 @@ describe('ol.geom.flat.length', function () {
}); });
}); });
describe('ol.geom.flat.length.linearRingLength', function () { describe('linearRingLength', function () {
it('calculates the total length of a simple linearRing', function () { it('calculates the total length of a simple linearRing', function () {
const flatCoords = [0, 0, 1, 0, 1, 1, 0, 1]; const flatCoords = [0, 0, 1, 0, 1, 1, 0, 1];
const stride = 2; const stride = 2;
@@ -1,13 +1,14 @@
import expect from '../../../expect.js';
import { import {
linearRingIsClockwise, linearRingIsClockwise,
linearRingsAreOriented, linearRingsAreOriented,
linearRingssAreOriented, linearRingssAreOriented,
orientLinearRings, orientLinearRings,
orientLinearRingsArray, orientLinearRingsArray,
} from '../../../../../../src/ol/geom/flat/orient.js'; } from '../../../../../src/ol/geom/flat/orient.js';
describe('ol.geom.flat.orient', function () { describe('ol/geom/flat/orient.js', function () {
describe('ol.geom.flat.orient.linearRingIsClockwise', function () { describe('linearRingIsClockwise', function () {
it('identifies clockwise rings', function () { it('identifies clockwise rings', function () {
const flatCoordinates = [0, 1, 1, 4, 4, 3, 3, 0]; const flatCoordinates = [0, 1, 1, 4, 4, 3, 3, 0];
const isClockwise = linearRingIsClockwise( const isClockwise = linearRingIsClockwise(
@@ -86,7 +87,7 @@ describe('ol.geom.flat.orient', function () {
}); });
}); });
describe('ol.geom.flat.orient.linearRingsAreOriented', function () { describe('linearRingsAreOriented', function () {
const oriented = linearRingsAreOriented; const oriented = linearRingsAreOriented;
const rightCoords = [ const rightCoords = [
@@ -148,7 +149,7 @@ describe('ol.geom.flat.orient', function () {
}); });
}); });
describe('ol.geom.flat.orient.linearRingssAreOriented', function () { describe('linearRingssAreOriented', function () {
const oriented = linearRingssAreOriented; const oriented = linearRingssAreOriented;
const rightCoords = [ const rightCoords = [
@@ -253,7 +254,7 @@ describe('ol.geom.flat.orient', function () {
}); });
}); });
describe('ol.geom.flat.orient.orientLinearRings', function () { describe('orientLinearRings', function () {
const orient = orientLinearRings; const orient = orientLinearRings;
const rightCoords = [ const rightCoords = [
@@ -325,7 +326,7 @@ describe('ol.geom.flat.orient', function () {
}); });
}); });
describe('ol.geom.flat.orient.orientLinearRingsArray', function () { describe('orientLinearRingsArray', function () {
const orient = orientLinearRingsArray; const orient = orientLinearRingsArray;
const rightCoords = [ const rightCoords = [
@@ -1,7 +1,8 @@
import {coordinates as reverseCoordinates} from '../../../../../../src/ol/geom/flat/reverse.js'; import expect from '../../../expect.js';
import {coordinates as reverseCoordinates} from '../../../../../src/ol/geom/flat/reverse.js';
describe('ol.geom.flat.reverse', function () { describe('ol/geom/flat/reverse.js', function () {
describe('ol.geom.flat.reverse.coordinates', function () { describe('coordinates', function () {
describe('with a stride of 2', function () { describe('with a stride of 2', function () {
it('can reverse empty flat coordinates', function () { it('can reverse empty flat coordinates', function () {
const flatCoordinates = []; const flatCoordinates = [];
@@ -1,7 +1,9 @@
import {forEach as forEachSegment} from '../../../../../../src/ol/geom/flat/segments.js'; 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', function () { describe('ol/geom/flat/segments.js', function () {
describe('ol.geom.flat.segments.forEach', function () { describe('forEach', function () {
let flatCoordinates, offset, end, stride; let flatCoordinates, offset, end, stride;
beforeEach(function () { beforeEach(function () {
flatCoordinates = [0, 0, 1, 1, 2, 2, 3, 3]; flatCoordinates = [0, 0, 1, 1, 2, 2, 3, 3];
@@ -1,11 +1,12 @@
import expect from '../../../expect.js';
import { import {
douglasPeucker, douglasPeucker,
quantize, quantize,
radialDistance, radialDistance,
simplifyLineString, simplifyLineString,
} from '../../../../../../src/ol/geom/flat/simplify.js'; } from '../../../../../src/ol/geom/flat/simplify.js';
describe('ol.geom.flat.simplify', function () { describe('ol/geom/flat/simplify.js', function () {
const flatCoordinates = [ const flatCoordinates = [
224.55, 224.55,
250.15, 250.15,
@@ -532,7 +533,7 @@ describe('ol.geom.flat.simplify', function () {
480.77, 480.77,
]; ];
describe('ol.geom.flat.simplify.simplifyLineString', function () { describe('simplifyLineString', function () {
it('works with empty line strings', function () { it('works with empty line strings', function () {
expect(simplifyLineString([], 0, 0, 2, 1, true)).to.eql([]); expect(simplifyLineString([], 0, 0, 2, 1, true)).to.eql([]);
expect(simplifyLineString([], 0, 0, 2, 1, false)).to.eql([]); expect(simplifyLineString([], 0, 0, 2, 1, false)).to.eql([]);
@@ -570,7 +571,7 @@ describe('ol.geom.flat.simplify', function () {
}); });
}); });
describe('ol.geom.flat.simplify.radialDistance', function () { describe('radialDistance', function () {
let dest; let dest;
beforeEach(function () { beforeEach(function () {
dest = []; dest = [];
@@ -673,7 +674,7 @@ describe('ol.geom.flat.simplify', function () {
}); });
}); });
describe('ol.geom.flat.simplify.douglasPeucker', function () { describe('douglasPeucker', function () {
let dest; let dest;
beforeEach(function () { beforeEach(function () {
dest = []; dest = [];
@@ -809,7 +810,7 @@ describe('ol.geom.flat.simplify', function () {
}); });
}); });
describe('ol.geom.flat.simplify.quantize', function () { describe('quantize', function () {
it('handles empty coordinates', function () { it('handles empty coordinates', function () {
const simplifiedFlatCoordinates = []; const simplifiedFlatCoordinates = [];
expect(quantize([], 0, 0, 2, 2, simplifiedFlatCoordinates, 0)).to.be(0); expect(quantize([], 0, 0, 2, 2, simplifiedFlatCoordinates, 0)).to.be(0);
@@ -1,7 +1,8 @@
import {matchingChunk} from '../../../../../../src/ol/geom/flat/straightchunk.js'; import expect from '../../../expect.js';
import {matchingChunk} from '../../../../../src/ol/geom/flat/straightchunk.js';
describe('ol.geom.flat.straightchunk', function () { describe('ol/geom/flat/straightchunk.js', function () {
describe('ol.geom.flat.straightchunk.matchingChunk', function () { describe('matchingChunk', function () {
describe('single segment with stride == 3', function () { describe('single segment with stride == 3', function () {
const flatCoords = [0, 0, 42, 1, 1, 42]; const flatCoords = [0, 0, 42, 1, 1, 42];
const stride = 3; const stride = 3;
@@ -1,7 +1,8 @@
import {drawTextOnPath} from '../../../../../../src/ol/geom/flat/textpath.js'; import expect from '../../../expect.js';
import {lineStringLength} from '../../../../../../src/ol/geom/flat/length.js'; import {drawTextOnPath} from '../../../../../src/ol/geom/flat/textpath.js';
import {lineStringLength} from '../../../../../src/ol/geom/flat/length.js';
describe('ol.geom.flat.drawTextOnPath', function () { describe('ol/geom/flat/drawTextOnPath.js', function () {
const horizontal = [0, 0, 100, 0]; const horizontal = [0, 0, 100, 0];
const vertical = [0, 0, 0, 100]; const vertical = [0, 0, 0, 100];
const diagonal = [0, 0, 100, 100]; const diagonal = [0, 0, 100, 100];
@@ -1,7 +1,8 @@
import {lineStringIsClosed} from '../../../../../../src/ol/geom/flat/topology.js'; import expect from '../../../expect.js';
import {lineStringIsClosed} from '../../../../../src/ol/geom/flat/topology.js';
describe('ol.geom.flat.topology', function () { describe('ol/geom/flat/topology.js', function () {
describe('ol.geom.flat.topology.lineStringIsClosed', function () { describe('lineStringIsClosed', function () {
it('identifies closed lines aka boundaries', function () { it('identifies closed lines aka boundaries', function () {
const flatCoordinates = [0, 0, 3, 0, 0, 3, 0, 0]; const flatCoordinates = [0, 0, 3, 0, 0, 3, 0, 0];
const isClosed = lineStringIsClosed( const isClosed = lineStringIsClosed(
@@ -1,12 +1,10 @@
import MultiPolygon from '../../../../../../src/ol/geom/MultiPolygon.js'; import MultiPolygon from '../../../../../src/ol/geom/MultiPolygon.js';
import { import expect from '../../../expect.js';
rotate, import {rotate, translate} from '../../../../../src/ol/geom/flat/transform.js';
translate, import {transformGeom2D} from '../../../../../src/ol/geom/SimpleGeometry.js';
} from '../../../../../../src/ol/geom/flat/transform.js';
import {transformGeom2D} from '../../../../../../src/ol/geom/SimpleGeometry.js';
describe('ol.geom.flat.transform', function () { describe('ol/geom/flat/transform.js', function () {
describe('ol.geom.flat.transform.transform2D', function () { describe('transform2D', function () {
it('transforms a Simple Geometry to 2D', function () { it('transforms a Simple Geometry to 2D', function () {
const multiPolygonGeometry = new MultiPolygon([ const multiPolygonGeometry = new MultiPolygon([
[ [
@@ -79,7 +77,7 @@ describe('ol.geom.flat.transform', function () {
}); });
}); });
describe('ol.geom.flat.transform.translate', function () { describe('translate', function () {
it('translates the coordinates array', function () { it('translates the coordinates array', function () {
const multiPolygon = new MultiPolygon([ const multiPolygon = new MultiPolygon([
[ [
@@ -148,7 +146,7 @@ describe('ol.geom.flat.transform', function () {
}); });
}); });
describe('ol.geom.flat.transform.rotate', function () { describe('rotate', function () {
it('rotates the coordinates array', function () { it('rotates the coordinates array', function () {
const multiPolygon = new MultiPolygon([ const multiPolygon = new MultiPolygon([
[ [
+165
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
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);
});
});
});
@@ -1,6 +1,8 @@
import Projection from '../../../../src/ol/proj/Projection.js'; import Projection from '../../../src/ol/proj/Projection.js';
import Units from '../../../../src/ol/proj/Units.js'; import Units from '../../../src/ol/proj/Units.js';
import {HALF_SIZE} from '../../../../src/ol/proj/epsg3857.js'; import expect from '../expect.js';
import proj4 from 'proj4';
import {HALF_SIZE} from '../../../src/ol/proj/epsg3857.js';
import { import {
METERS_PER_UNIT, METERS_PER_UNIT,
addCommon, addCommon,
@@ -22,11 +24,11 @@ import {
transform, transform,
transformExtent, transformExtent,
useGeographic, useGeographic,
} from '../../../../src/ol/proj.js'; } from '../../../src/ol/proj.js';
import {METERS_PER_UNIT as metersPerDegree} from '../../../../src/ol/proj/epsg4326.js'; import {METERS_PER_UNIT as metersPerDegree} from '../../../src/ol/proj/epsg4326.js';
import {register} from '../../../../src/ol/proj/proj4.js'; import {register} from '../../../src/ol/proj/proj4.js';
describe('ol.proj', function () { describe('ol/proj.js', function () {
afterEach(function () { afterEach(function () {
clearAllProjections(); clearAllProjections();
clearUserProjection(); clearUserProjection();
@@ -1,6 +1,7 @@
import LinkedList from '../../../../../src/ol/structs/LinkedList.js'; import LinkedList from '../../../../src/ol/structs/LinkedList.js';
import expect from '../../expect.js';
describe('ol.structs.LinkedList', function () { describe('ol/structs/LinkedList.js', function () {
let ll; let ll;
const item = {}; const item = {};
const item2 = {}; const item2 = {};
@@ -1,6 +1,7 @@
import LRUCache from '../../../../../src/ol/structs/LRUCache.js'; import LRUCache from '../../../../src/ol/structs/LRUCache.js';
import expect from '../../expect.js';
describe('ol.structs.LRUCache', function () { describe('ol/structs/LRUCache.js', function () {
let lruCache; let lruCache;
function fillLRUCache(lruCache) { function fillLRUCache(lruCache) {
@@ -1,8 +1,7 @@
import PriorityQueue, { import PriorityQueue, {DROP} from '../../../../src/ol/structs/PriorityQueue.js';
DROP, import expect from '../../expect.js';
} from '../../../../../src/ol/structs/PriorityQueue.js';
describe('ol.structs.PriorityQueue', function () { describe('ol/structs/PriorityQueue.js', function () {
const identity = function (a) { const identity = function (a) {
return a; return a;
}; };
@@ -1,6 +1,7 @@
import RBush from '../../../../../src/ol/structs/RBush.js'; import RBush from '../../../../src/ol/structs/RBush.js';
import expect from '../../expect.js';
describe('ol.structs.RBush', function () { describe('ol/structs/RBush.js', function () {
let rBush; let rBush;
beforeEach(function () { beforeEach(function () {
rBush = new RBush(); rBush = new RBush();
@@ -1,24 +1,25 @@
import Projection from '../../../../../src/ol/proj/Projection.js'; import Projection from '../../../../src/ol/proj/Projection.js';
import TileGrid from '../../../../../src/ol/tilegrid/TileGrid.js'; import TileGrid from '../../../../src/ol/tilegrid/TileGrid.js';
import TileRange from '../../../../../src/ol/TileRange.js'; import TileRange from '../../../../src/ol/TileRange.js';
import expect from '../../expect.js';
import { import {
DEFAULT_MAX_ZOOM, DEFAULT_MAX_ZOOM,
DEFAULT_TILE_SIZE, DEFAULT_TILE_SIZE,
} from '../../../../../src/ol/tilegrid/common.js'; } from '../../../../src/ol/tilegrid/common.js';
import {HALF_SIZE} from '../../../../../src/ol/proj/epsg3857.js'; import {HALF_SIZE} from '../../../../src/ol/proj/epsg3857.js';
import { import {
METERS_PER_UNIT, METERS_PER_UNIT,
get as getProjection, get as getProjection,
} from '../../../../../src/ol/proj.js'; } from '../../../../src/ol/proj.js';
import { import {
createForExtent, createForExtent,
createForProjection, createForProjection,
createXYZ, createXYZ,
getForProjection as getTileGridForProjection, getForProjection as getTileGridForProjection,
} from '../../../../../src/ol/tilegrid.js'; } from '../../../../src/ol/tilegrid.js';
import {createOrUpdate} from '../../../../../src/ol/extent.js'; import {createOrUpdate} from '../../../../src/ol/extent.js';
describe('ol.tilegrid.TileGrid', function () { describe('ol/tilegrid/TileGrid.js', function () {
let resolutions; let resolutions;
let origin; let origin;
let tileSize; let tileSize;
+25
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));
});
});
});
@@ -1,13 +1,14 @@
import {create, fromTransform} from '../../../../../src/ol/vec/mat4.js'; import expect from '../../expect.js';
import {create, fromTransform} from '../../../../src/ol/vec/mat4.js';
describe('mat4', function () { describe('ol/vec/mat4.js', function () {
describe('mat4.create()', function () { describe('create()', function () {
it('returns the expected matrix', 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]); expect(create()).to.eql([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]);
}); });
}); });
describe('mat4.fromTransform()', function () { describe('fromTransform()', function () {
it('sets the expected transform on the matrix', function () { it('sets the expected transform on the matrix', function () {
const transform = [1, 2, 3, 4, 5, 6]; const transform = [1, 2, 3, 4, 5, 6];
const result = create(); const result = create();
+11
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).
+30 -24
View File
@@ -1,18 +1,24 @@
#! /usr/bin/env node #! /usr/bin/env node
const puppeteer = require('puppeteer'); import config from './webpack.config.js';
const webpack = require('webpack'); import esMain from 'es-main';
const config = require('./webpack.config'); import express from 'express';
const webpackMiddleware = require('webpack-dev-middleware'); import fs from 'fs';
const express = require('express'); import fse from 'fs-extra';
const path = require('path'); import globby from 'globby';
const png = require('pngjs'); import log from 'loglevelnext';
const fs = require('fs'); import path from 'path';
const fse = require('fs-extra'); import pixelmatch from 'pixelmatch';
const pixelmatch = require('pixelmatch'); import png from 'pngjs';
const yargs = require('yargs'); import puppeteer from 'puppeteer';
const log = require('loglevelnext'); import serveStatic from 'serve-static';
const globby = require('globby'); import webpack from 'webpack';
const serveStatic = require('serve-static'); import webpackMiddleware from 'webpack-dev-middleware';
import yargs from 'yargs';
import {dirname} from 'path';
import {fileURLToPath} from 'url';
import {hideBin} from 'yargs/helpers';
const baseDir = dirname(fileURLToPath(import.meta.url));
const compiler = webpack(Object.assign({mode: 'development'}, config)); const compiler = webpack(Object.assign({mode: 'development'}, config));
@@ -20,11 +26,11 @@ function getHref(entry) {
return path.dirname(entry).slice(1) + '/'; return path.dirname(entry).slice(1) + '/';
} }
const staticHandler = serveStatic(__dirname); const staticHandler = serveStatic(baseDir);
const defaultHandler = serveStatic(path.join(__dirname, 'default')); const defaultHandler = serveStatic(path.join(baseDir, 'default'));
const srcHandler = serveStatic(path.join(__dirname, '..', '..', 'src')); const srcHandler = serveStatic(path.join(baseDir, '..', '..', 'src'));
function indexHandler(req, res) { function indexHandler(req, res) {
const items = []; const items = [];
@@ -91,15 +97,15 @@ function serve(options) {
} }
function getActualScreenshotPath(entry) { function getActualScreenshotPath(entry) {
return path.join(__dirname, path.dirname(entry), 'actual.png'); return path.join(baseDir, path.dirname(entry), 'actual.png');
} }
function getExpectedScreenshotPath(entry) { function getExpectedScreenshotPath(entry) {
return path.join(__dirname, path.dirname(entry), 'expected.png'); return path.join(baseDir, path.dirname(entry), 'expected.png');
} }
function getPassFilePath(entry) { function getPassFilePath(entry) {
return path.join(__dirname, path.dirname(entry), 'pass'); return path.join(baseDir, path.dirname(entry), 'pass');
} }
function parsePNG(filepath) { function parsePNG(filepath) {
@@ -284,7 +290,7 @@ async function getLatest(patterns) {
async function getOutdated(entries, options) { async function getOutdated(entries, options) {
const libTime = await getLatest( const libTime = await getLatest(
path.join(__dirname, '..', 'src', 'ol', '**', '*') path.join(baseDir, '..', 'src', 'ol', '**', '*')
); );
options.log.debug('library time', libTime); options.log.debug('library time', libTime);
const outdated = []; const outdated = [];
@@ -298,7 +304,7 @@ async function getOutdated(entries, options) {
} }
const caseTime = await getLatest( const caseTime = await getLatest(
path.join(__dirname, path.dirname(entry), '**', '*') path.join(baseDir, path.dirname(entry), '**', '*')
); );
options.log.debug('case time', entry, caseTime); options.log.debug('case time', entry, caseTime);
if (passTime < caseTime) { if (passTime < caseTime) {
@@ -337,8 +343,8 @@ function sleep(ms) {
}); });
} }
if (require.main === module) { if (esMain(import.meta)) {
const options = yargs const options = yargs(hideBin(process.argv))
.option('fix', { .option('fix', {
describe: 'Accept all screenshots as accepted', describe: 'Accept all screenshots as accepted',
type: 'boolean', type: 'boolean',
+12 -9
View File
@@ -1,7 +1,10 @@
const fs = require('fs'); import fs from 'fs';
const path = require('path'); import path, {dirname} from 'path';
import {fileURLToPath} from 'url';
const cases = path.join(__dirname, 'cases'); const baseDir = dirname(fileURLToPath(import.meta.url));
const cases = path.join(baseDir, 'cases');
const caseDirs = fs.readdirSync(cases).filter((name) => { const caseDirs = fs.readdirSync(cases).filter((name) => {
let exists = true; let exists = true;
@@ -18,8 +21,8 @@ caseDirs.forEach((c) => {
entry[`cases/${c}/main`] = `./cases/${c}/main.js`; entry[`cases/${c}/main`] = `./cases/${c}/main.js`;
}); });
module.exports = { export default {
context: __dirname, context: baseDir,
target: 'web', target: 'web',
entry: entry, entry: entry,
devtool: 'source-map', devtool: 'source-map',
@@ -30,18 +33,18 @@ module.exports = {
test: /\.js$/, test: /\.js$/,
use: { use: {
loader: path.join( loader: path.join(
__dirname, baseDir,
'../../examples/webpack/worker-loader.js' '../../examples/webpack/worker-loader.cjs'
), ),
}, },
include: [path.join(__dirname, '../../src/ol/worker')], include: [path.join(baseDir, '../../src/ol/worker')],
}, },
], ],
}, },
resolve: { resolve: {
alias: { alias: {
// ol-mapbox-style imports ol/style/Style etc // ol-mapbox-style imports ol/style/Style etc
ol: path.join(__dirname, '..', '..', 'src', 'ol'), ol: path.join(baseDir, '..', '..', 'src', 'ol'),
}, },
}, },
}; };