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

View File

@@ -47,8 +47,8 @@ jobs:
- name: Run Tests
run: npm run pretest
spec:
name: Spec
browser:
name: Browser
runs-on: ubuntu-latest
strategy:
@@ -80,7 +80,42 @@ jobs:
run: npm ci
- 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:
name: Build

1
.nvmrc Normal file
View File

@@ -0,0 +1 @@
16

View File

@@ -14,12 +14,12 @@
]
},
"plugins": [
"config/jsdoc/api/plugins/markdown",
"config/jsdoc/api/plugins/markdown.cjs",
"jsdoc-plugin-typescript",
"config/jsdoc/api/plugins/inline-options",
"config/jsdoc/api/plugins/events",
"config/jsdoc/api/plugins/observable",
"config/jsdoc/api/plugins/api"
"config/jsdoc/api/plugins/inline-options.cjs",
"config/jsdoc/api/plugins/events.cjs",
"config/jsdoc/api/plugins/observable.cjs",
"config/jsdoc/api/plugins/api.cjs"
],
"typescript": {
"moduleRoot": "src"

View File

@@ -1,3 +1,5 @@
/* eslint-disable import/no-commonjs */
/**
* Define an @api tag
* @param {Object} dictionary The tag dictionary.

View File

@@ -1,3 +1,5 @@
/* eslint-disable import/no-commonjs */
const events = {};
exports.handlers = {

View File

@@ -1,3 +1,5 @@
/* eslint-disable import/no-commonjs */
/**
* @fileoverview
* Inlines option params from typedefs

View File

@@ -1,3 +1,5 @@
/* eslint-disable import/no-commonjs */
/**
* 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

View File

@@ -1,3 +1,5 @@
/* eslint-disable import/no-commonjs */
const classes = {};
const observables = {};

View File

@@ -1,3 +1,5 @@
/* eslint-disable import/no-commonjs */
/*global env: true */
const hasOwnProp = Object.prototype.hasOwnProperty;

View File

@@ -11,8 +11,8 @@
},
"plugins": [
"jsdoc-plugin-typescript",
"config/jsdoc/info/define-plugin",
"config/jsdoc/info/virtual-plugin"
"config/jsdoc/info/define-plugin.cjs",
"config/jsdoc/info/virtual-plugin.cjs"
],
"typescript": {
"moduleRoot": "src"

View File

@@ -1,3 +1,5 @@
/* eslint-disable import/no-commonjs */
/**
* @fileoverview This plugin extracts info from boolean defines. This only
* handles boolean defines with the default value in the description. Default

View File

@@ -1,3 +1,5 @@
/* eslint-disable import/no-commonjs */
/**
* @fileoverview Generates JSON output based on exportable symbols.
*/

View File

@@ -1,3 +1,5 @@
/* eslint-disable import/no-commonjs */
/**
* Handle the interface and abstract annotations.
* @param {Object} dictionary The tag dictionary.

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"
}

View File

@@ -1,7 +1,10 @@
const path = require('path');
const TerserPlugin = require('terser-webpack-plugin');
import TerserPlugin from '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',
devtool: 'source-map',
mode: 'production',
@@ -18,7 +21,7 @@ module.exports = {
},
include: [
path.join(
__dirname,
baseDir,
'..',
'node_modules',
'@mapbox',

View File

@@ -5,12 +5,5 @@
},
"parserOptions": {
"ecmaVersion": 2017
},
"rules": {
"space-before-function-paren": ["error", {
"anonymous": "never",
"named": "never",
"asyncArrow": "always"
}]
}
}

View File

@@ -1,12 +1,15 @@
const TerserPlugin = require('terser-webpack-plugin');
const CopyPlugin = require('copy-webpack-plugin');
const ExampleBuilder = require('./example-builder');
const fs = require('fs');
const path = require('path');
import CopyPlugin from 'copy-webpack-plugin';
import ExampleBuilder from './example-builder.js';
import TerserPlugin from 'terser-webpack-plugin';
import fs from 'fs';
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,
target: ['web', 'es5'],
entry: () => {
@@ -33,10 +36,10 @@ module.exports = {
},
},
include: [
path.join(__dirname, '..', '..', 'src'),
path.join(__dirname, '..'),
path.join(baseDir, '..', '..', 'src'),
path.join(baseDir, '..'),
path.join(
__dirname,
baseDir,
'..',
'..',
'node_modules',
@@ -48,9 +51,9 @@ module.exports = {
{
test: /\.js$/,
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: [
new ExampleBuilder({
templates: path.join(__dirname, '..', 'templates'),
templates: path.join(baseDir, '..', 'templates'),
common: 'common',
}),
new CopyPlugin({
@@ -93,7 +96,7 @@ module.exports = {
devtool: 'source-map',
output: {
filename: '[name].js',
path: path.join(__dirname, '..', '..', 'build', 'examples'),
path: path.join(baseDir, '..', '..', 'build', 'examples'),
},
resolve: {
fallback: {
@@ -101,7 +104,7 @@ module.exports = {
},
alias: {
// allow imports from 'ol/module' instead of specifiying the source path
ol: path.join(__dirname, '..', '..', 'src', 'ol'),
ol: path.join(baseDir, '..', '..', 'src', 'ol'),
},
},
};

View File

@@ -1,20 +1,32 @@
const assert = require('assert');
const frontMatter = require('front-matter');
const fs = require('fs');
const handlebars = require('handlebars');
const marked = require('marked');
const path = require('path');
const pkg = require('../../package.json');
const promisify = require('util').promisify;
const RawSource = require('webpack-sources').RawSource;
import assert from 'assert';
import frontMatter from 'front-matter';
import fse from 'fs-extra';
import handlebars from 'handlebars';
import marked from 'marked';
import path, {dirname} from 'path';
import sources from 'webpack-sources';
import {fileURLToPath} from 'url';
const RawSource = sources.RawSource;
const baseDir = dirname(fileURLToPath(import.meta.url));
const readFile = promisify(fs.readFile);
const isCssRegEx = /\.css(\?.*)?$/;
const isJsRegEx = /\.js(\?.*)?$/;
const importRegEx = /^import .* from '(.*)';$/;
const isTemplateJs = /\/(jquery(-\d+\.\d+\.\d+)?|(bootstrap(\.bundle)?))(\.min)?\.js(\?.*)?$/;
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(
'md',
(str) => new handlebars.SafeString(marked(str))
@@ -111,9 +123,10 @@ function createWordIndex(exampleData) {
/**
* Gets dependencies from the js source.
* @param {string} jsSource Source.
* @param {Object} pkg Package info.
* @return {Object<string, string>} dependencies
*/
function getDependencies(jsSource) {
function getDependencies(jsSource, pkg) {
const lines = jsSource.split('\n');
const dependencies = {
ol: pkg.version,
@@ -140,7 +153,7 @@ function getDependencies(jsSource) {
return dependencies;
}
class ExampleBuilder {
export default class ExampleBuilder {
/**
* A webpack plugin that builds the html files for our examples.
* @param {Object} config Plugin configuration. Requires a `templates` property
@@ -241,16 +254,17 @@ class ExampleBuilder {
async parseExample(dir, name) {
const htmlName = `${name}.html`;
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 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);
assert(!!attributes.layout, `missing layout in ${htmlPath}`);
const data = Object.assign(attributes, {contents: body});
const pkg = await getPackageInfo();
data.olVersion = pkg.version;
data.filename = htmlName;
data.dir = dir;
@@ -299,7 +313,7 @@ class ExampleBuilder {
const workerPath = path.join(data.dir, workerName);
let workerSource;
try {
workerSource = await readFile(workerPath, readOptions);
workerSource = await fse.readFile(workerPath, readOptions);
} catch (err) {
// pass
}
@@ -321,11 +335,14 @@ class ExampleBuilder {
assets[workerName] = workerSource;
}
const pkg = await getPackageInfo();
data.pkgJson = JSON.stringify(
{
name: data.name,
dependencies: getDependencies(
jsSource + (workerSource ? `\n${workerSource}` : '')
jsSource + (workerSource ? `\n${workerSource}` : ''),
pkg
),
devDependencies: {
parcel: '^2.0.0-beta.1',
@@ -344,7 +361,7 @@ class ExampleBuilder {
const cssPath = path.join(data.dir, cssName);
let cssSource;
try {
cssSource = await readFile(cssPath, readOptions);
cssSource = await fse.readFile(cssPath, readOptions);
} catch (err) {
// pass
}
@@ -358,6 +375,7 @@ class ExampleBuilder {
// add additional resources
if (data.resources) {
const pkg = await getPackageInfo();
const localResources = [];
const remoteResources = [];
data.resources.forEach((resource) => {
@@ -389,11 +407,9 @@ class ExampleBuilder {
}
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);
return assets;
}
}
module.exports = ExampleBuilder;

View File

@@ -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 minify = this.mode === 'production';
@@ -12,6 +14,4 @@ function loader() {
callback(null, chunk.code);
})
.catch(callback);
}
module.exports = loader;
};

62
package-lock.json generated
View File

@@ -15,6 +15,7 @@
},
"devDependencies": {
"@babel/core": "^7.6.4",
"@babel/eslint-parser": "^7.13.14",
"@babel/preset-env": "^7.4.4",
"@openlayers/eslint-plugin": "^4.0.0",
"@rollup/plugin-babel": "^5.3.0",
@@ -32,6 +33,7 @@
"copy-webpack-plugin": "^8.0.0",
"coverage-istanbul-loader": "^3.0.5",
"coveralls": "3.1.0",
"es-main": "^1.0.2",
"eslint": "^7.2.0",
"eslint-config-openlayers": "^15.0.0",
"expect.js": "0.3.1",
@@ -66,7 +68,7 @@
"shx": "^0.3.2",
"sinon": "^10.0.0",
"terser-webpack-plugin": "^5.1.1",
"typescript": "^4.2.3",
"typescript": "^4.2.4",
"url-polyfill": "^1.1.5",
"walk": "^2.3.9",
"webpack": "^5.27.2",
@@ -127,6 +129,33 @@
"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": {
"version": "7.13.16",
"resolved": "https://registry.npmjs.org/@babel/generator/-/generator-7.13.16.tgz",
@@ -3631,6 +3660,12 @@
"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": {
"version": "0.4.1",
"resolved": "https://registry.npmjs.org/es-module-lexer/-/es-module-lexer-0.4.1.tgz",
@@ -11410,6 +11445,25 @@
"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": {
"version": "7.13.16",
"resolved": "https://registry.npmjs.org/@babel/generator/-/generator-7.13.16.tgz",
@@ -14260,6 +14314,12 @@
"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": {
"version": "0.4.1",
"resolved": "https://registry.npmjs.org/es-module-lexer/-/es-module-lexer-0.4.1.tgz",

View File

@@ -13,23 +13,24 @@
"lint": "eslint tasks test src/ol examples config",
"pretest": "npm run lint && npm run typecheck",
"test-rendering": "node test/rendering/test.js",
"test-spec": "npm run karma -- --single-run --log-level error",
"test": "npm run test-spec && npm run test-rendering -- --force",
"karma": "karma start test/browser/karma.config.js",
"test-browser": "npm run karma -- --single-run --log-level error",
"test-node": "mocha --recursive test/node",
"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",
"serve-examples": "webpack serve --config examples/webpack/config.js --mode development",
"build-examples": "webpack --config examples/webpack/config.js --mode production",
"build-package": "npm run transpile && npm run copy-css && node tasks/prepare-package",
"build-index": "npm run build-package && node tasks/generate-index",
"build-package": "npm run transpile && npm run copy-css && node tasks/prepare-package.js",
"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-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",
"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",
"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"
},
"main": "index.js",
"type": "module",
"repository": {
"type": "git",
"url": "git://github.com/openlayers/openlayers.git"
@@ -49,6 +50,7 @@
},
"devDependencies": {
"@babel/core": "^7.6.4",
"@babel/eslint-parser": "^7.13.14",
"@babel/preset-env": "^7.4.4",
"@openlayers/eslint-plugin": "^4.0.0",
"@rollup/plugin-babel": "^5.3.0",
@@ -66,6 +68,7 @@
"copy-webpack-plugin": "^8.0.0",
"coverage-istanbul-loader": "^3.0.5",
"coveralls": "3.1.0",
"es-main": "^1.0.2",
"eslint": "^7.2.0",
"eslint-config-openlayers": "^15.0.0",
"expect.js": "0.3.1",
@@ -100,7 +103,7 @@
"shx": "^0.3.2",
"sinon": "^10.0.0",
"terser-webpack-plugin": "^5.1.1",
"typescript": "^4.2.3",
"typescript": "^4.2.4",
"url-polyfill": "^1.1.5",
"walk": "^2.3.9",
"webpack": "^5.27.2",
@@ -113,10 +116,15 @@
},
"eslintConfig": {
"extends": "openlayers",
"parser": "@babel/eslint-parser",
"parserOptions": {
"requireConfigFile": false
},
"plugins": [
"@openlayers"
],
"rules": {
"import/no-commonjs": "error",
"@openlayers/no-exclusive-tests": [
"error",
{

View File

@@ -1,6 +1,9 @@
const fse = require('fs-extra');
const path = require('path');
const generateInfo = require('./generate-info');
import esMain from 'es-main';
import fse from 'fs-extra';
import generateInfo from './generate-info.js';
import path from 'path';
import {dirname} from 'path';
import {fileURLToPath} from 'url';
/**
* Read the symbols from info file.
@@ -21,11 +24,11 @@ function getImport(symbol, member) {
const defaultExport = symbol.name.split('~');
const namedExport = symbol.name.split('.');
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, '$');
return `import ${importName} from '${from}';`;
} else if (namedExport.length > 1 && member) {
const from = namedExport[0].replace(/^module\:/, './');
const from = namedExport[0].replace(/^module\:/, './') + '.js';
const importName = from.replace(/[.\/]+/g, '_');
return `import {${member} as ${importName}$${member}} from '${from}';`;
}
@@ -97,7 +100,7 @@ function generateExports(symbols) {
* Generate the exports code.
* @return {Promise<string>} Resolves with the exports code.
*/
async function main() {
export default async function main() {
const symbols = await getSymbols();
return generateExports(symbols);
}
@@ -106,18 +109,15 @@ async function main() {
* If running this module directly, read the config file, call the main
* function, and write the output file.
*/
if (require.main === module) {
if (esMain(import.meta)) {
const baseDir = dirname(fileURLToPath(import.meta.url));
main()
.then(async (code) => {
const filepath = path.join(__dirname, '..', 'build', 'index.js');
const filepath = path.join(baseDir, '..', 'build', 'index.js');
await fse.outputFile(filepath, code);
})
.catch((err) => {
process.stderr.write(`${err.message}\n`, () => process.exit(1));
});
}
/**
* Export main function.
*/
module.exports = main;

View File

@@ -1,11 +1,16 @@
const fse = require('fs-extra');
const path = require('path');
const spawn = require('child_process').spawn;
const walk = require('walk').walk;
const isWindows = process.platform.indexOf('win') === 0;
import esMain from 'es-main';
import fse from 'fs-extra';
import path from 'path';
import {dirname} from 'path';
import {fileURLToPath} from 'url';
import {spawn} from 'child_process';
import {walk} from 'walk';
const sourceDir = path.join(__dirname, '..', 'src');
const infoPath = path.join(__dirname, '..', 'build', 'info.json');
const isWindows = process.platform.indexOf('win') === 0;
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.
@@ -17,9 +22,15 @@ function getBinaryPath(binaryName) {
binaryName += '.cmd';
}
const jsdocResolved = require.resolve('jsdoc/jsdoc.js');
const jsdocResolved = path.join(
baseDir,
'..',
'node_modules',
'jsdoc',
'jsdoc.js'
);
const expectedPaths = [
path.join(__dirname, '..', 'node_modules', '.bin', binaryName),
path.join(baseDir, '..', 'node_modules', '.bin', binaryName),
path.resolve(
path.join(path.dirname(jsdocResolved), '..', '.bin', binaryName)
),
@@ -40,7 +51,7 @@ function getBinaryPath(binaryName) {
const jsdoc = getBinaryPath('jsdoc');
const jsdocConfig = path.join(
__dirname,
baseDir,
'..',
'config',
'jsdoc',
@@ -120,7 +131,7 @@ function spawnJSDoc(paths) {
return new Promise((resolve, reject) => {
let output = '';
let errors = '';
const cwd = path.join(__dirname, '..');
const cwd = path.join(baseDir, '..');
const child = spawn(jsdoc, ['-c', jsdocConfig].concat(paths), {cwd: cwd});
child.stdout.on('data', (data) => {
@@ -162,7 +173,7 @@ async function write(info) {
* Generate info from the sources.
* @return {Promise<Error>} Resolves with the info object.
*/
async function main() {
export default async function main() {
const paths = await getPaths();
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 (require.main === module) {
if (esMain(import.meta)) {
main()
.then(write)
.catch((err) => {
process.stderr.write(`${err.message}\n`, () => process.exit(1));
});
}
/**
* Export main function.
*/
module.exports = main;

View File

@@ -1,7 +1,11 @@
const version = require('../package.json').version;
const semver = require('semver');
import esMain from 'es-main';
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);
if (!s) {
throw new Error(`Invalid version ${version}`);
@@ -9,6 +13,13 @@ function nextVersion() {
return `${s.major}.${s.minor}.${s.patch}-dev.${Date.now()}`;
}
if (require.main === module) {
process.stdout.write(`${nextVersion()}\n`);
if (esMain(import.meta)) {
nextVersion()
.then((version) => {
process.stdout.write(`${version}\n`);
})
.catch((error) => {
process.stderr.write(`${error}\n`);
process.exit(1);
});
}

View File

@@ -1,35 +1,49 @@
const fs = require('fs');
const path = require('path');
const pkg = require('../package.json');
import esMain from 'es-main';
import fse from 'fs-extra';
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
const utilPath = path.join(buildDir, 'util.js');
const versionRegEx = /var VERSION = '(.*)';/g;
const utilSrc = fs
.readFileSync(utilPath, 'utf-8')
.replace(versionRegEx, `var VERSION = '${pkg.version}';`);
fs.writeFileSync(utilPath, utilSrc, 'utf-8');
async function main() {
const pkg = await fse.readJSON(path.resolve(baseDir, '../package.json'));
// write out simplified package.json
delete pkg.scripts;
delete pkg.devDependencies;
delete pkg.style;
delete pkg.eslintConfig;
delete pkg.private;
fs.writeFileSync(
path.join(buildDir, 'package.json'),
JSON.stringify(pkg, null, 2),
'utf-8'
);
// update the version number in util.js
const utilPath = path.join(buildDir, 'util.js');
const versionRegEx = /var VERSION = '(.*)';/g;
let utilSrc = await fse.readFile(utilPath, 'utf-8');
utilSrc = utilSrc.replace(versionRegEx, `var VERSION = '${pkg.version}';`);
await fse.writeFile(utilPath, utilSrc, 'utf-8');
// copy in readme and license files
fs.copyFileSync(
path.resolve(__dirname, '../README.md'),
path.join(buildDir, 'README.md')
);
fs.copyFileSync(
path.resolve(__dirname, '../LICENSE.md'),
path.join(buildDir, 'LICENSE.md')
);
// write out simplified package.json
pkg.main = 'index.js';
delete pkg.scripts;
delete pkg.devDependencies;
delete pkg.style;
delete pkg.eslintConfig;
delete pkg.private;
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));
});
}

View File

@@ -1,3 +1,5 @@
/* eslint-disable import/no-commonjs */
const path = require('path');
const babel = require('@rollup/plugin-babel').babel;
const resolve = require('@rollup/plugin-node-resolve').nodeResolve;

View File

@@ -1,7 +1,7 @@
## Included in this directory
- browser - Unit/integration tests run in a browser
- node - Unit tests run with Node.js
- rendering - Tests that make assertions about rendered map output

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

View File

@@ -1,3 +1,5 @@
/* eslint-disable import/no-commonjs */
const path = require('path');
module.exports = function (karma) {
@@ -45,7 +47,7 @@ module.exports = function (karma) {
pattern: path.resolve(__dirname, './test-extensions.js'),
},
{
pattern: path.resolve(__dirname, './index_test.js'),
pattern: 'spec/**/*.test.js',
watched: false,
},
{
@@ -54,7 +56,6 @@ module.exports = function (karma) {
watched: false,
},
],
exclude: ['**/*.test.js'],
proxies: {
'/spec/': '/base/spec/',
},
@@ -99,7 +100,7 @@ module.exports = function (karma) {
use: {
loader: path.join(
__dirname,
'../../examples/webpack/worker-loader.js'
'../../examples/webpack/worker-loader.cjs'
),
},
include: [path.join(__dirname, '../../src/ol/worker')],

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

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

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

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
test/node/.eslintrc Normal file
View File

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

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

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

View File

@@ -1,7 +1,8 @@
import AssertionError from '../../../../src/ol/AssertionError.js';
import {VERSION} from '../../../../src/ol/util.js';
import AssertionError from '../../../src/ol/AssertionError.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 () {
const error = new AssertionError(42);
expect(error).to.be.an(Error);

View File

@@ -1,8 +1,10 @@
import Collection from '../../../../src/ol/Collection.js';
import CollectionEventType from '../../../../src/ol/CollectionEventType.js';
import {listen} from '../../../../src/ol/events.js';
import Collection from '../../../src/ol/Collection.js';
import CollectionEventType from '../../../src/ol/CollectionEventType.js';
import expect from '../expect.js';
import sinon from 'sinon';
import {listen} from '../../../src/ol/events.js';
describe('ol.collection', function () {
describe('ol/Collection.js', function () {
let collection;
beforeEach(function () {

View File

@@ -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 () {
it('creates an instance', function () {
const disposable = new Disposable();

View File

@@ -1,9 +1,11 @@
import Feature, {createStyleFunction} from '../../../../src/ol/Feature.js';
import Point from '../../../../src/ol/geom/Point.js';
import Style from '../../../../src/ol/style/Style.js';
import {isEmpty} from '../../../../src/ol/obj.js';
import Feature, {createStyleFunction} from '../../../src/ol/Feature.js';
import Point from '../../../src/ol/geom/Point.js';
import Style from '../../../src/ol/style/Style.js';
import expect from '../expect.js';
import sinon from 'sinon';
import {isEmpty} from '../../../src/ol/obj.js';
describe('ol.Feature', function () {
describe('ol/Feature.js', function () {
describe('constructor', function () {
it('creates a new feature', function () {
const feature = new Feature();

View File

@@ -1,7 +1,9 @@
import BaseObject from '../../../../src/ol/Object.js';
import {listen} from '../../../../src/ol/events.js';
import BaseObject from '../../../src/ol/Object.js';
import expect from '../expect.js';
import sinon from 'sinon';
import {listen} from '../../../src/ol/events.js';
describe('ol.Object', function () {
describe('ol/Object.js', function () {
let o;
beforeEach(function () {
o = new BaseObject();

View File

@@ -1,7 +1,9 @@
import EventTarget from '../../../../src/ol/events/Target.js';
import Observable, {unByKey} from '../../../../src/ol/Observable.js';
import EventTarget from '../../../src/ol/events/Target.js';
import Observable, {unByKey} from '../../../src/ol/Observable.js';
import expect from '../expect.js';
import sinon from 'sinon';
describe('ol.Observable', function () {
describe('ol/Observable.js', function () {
describe('constructor', function () {
it('creates a new observable', function () {
const observable = new Observable();

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

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

View File

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

View File

@@ -1,5 +1,6 @@
import Circle from '../../../../src/ol/geom/Circle.js';
import Projection from '../../../../src/ol/proj/Projection.js';
import Circle from '../../../src/ol/geom/Circle.js';
import Projection from '../../../src/ol/proj/Projection.js';
import expect from '../expect.js';
import {
add as addCoordinate,
closestOnCircle,
@@ -13,8 +14,8 @@ import {
toStringHDMS,
toStringXY,
wrapX,
} from '../../../../src/ol/coordinate.js';
import {get} from '../../../../src/ol/proj.js';
} from '../../../src/ol/coordinate.js';
import {get} from '../../../src/ol/proj.js';
describe('ol.coordinate', function () {
describe('#add', function () {

View File

@@ -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('getFontParameters()', function () {

View File

@@ -1,7 +1,9 @@
import EventTarget from '../../../../src/ol/events/Target.js';
import {listen, listenOnce, unlistenByKey} from '../../../../src/ol/events.js';
import EventTarget from '../../../src/ol/events/Target.js';
import expect from '../expect.js';
import sinon from 'sinon';
import {listen, listenOnce, unlistenByKey} from '../../../src/ol/events.js';
describe('ol.events', function () {
describe('ol/events.js', function () {
let add, target;
beforeEach(function () {

View File

@@ -1,9 +1,10 @@
import Disposable from '../../../../../src/ol/Disposable.js';
import Event from '../../../../../src/ol/events/Event.js';
import EventTarget from '../../../../../src/ol/events/Target.js';
import {listen} from '../../../../../src/ol/events.js';
import Disposable from '../../../../src/ol/Disposable.js';
import Event from '../../../../src/ol/events/Event.js';
import EventTarget from '../../../../src/ol/events/Target.js';
import expect from '../../expect.js';
import {listen} from '../../../../src/ol/events.js';
describe('ol.events.EventTarget', function () {
describe('ol/events/Target.js', function () {
let called, events, eventTarget, spy1, spy2, spy3;
beforeEach(function () {
@@ -166,7 +167,7 @@ describe('ol.events.EventTarget', function () {
describe('#dispose()', function () {
it('cleans up foreign references', function () {
listen(eventTarget, 'foo', spy1, document);
listen(eventTarget, 'foo', spy1);
expect(eventTarget.hasListener('foo')).to.be(true);
eventTarget.dispose();
expect(eventTarget.hasListener('foo')).to.be(false);

View File

@@ -1,9 +1,11 @@
import Event, {
preventDefault,
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 () {
it('takes a type as argument', function () {
const event = new Event('foo');

View File

@@ -1,8 +1,11 @@
import * as _ol_extent_ from '../../../../src/ol/extent.js';
import {get, getTransform} from '../../../../src/ol/proj.js';
import {register} from '../../../../src/ol/proj/proj4.js';
import * as _ol_extent_ from '../../../src/ol/extent.js';
import expect from '../expect.js';
import proj4 from 'proj4';
import sinon from 'sinon';
import {get, getTransform} from '../../../src/ol/proj.js';
import {register} from '../../../src/ol/proj/proj4.js';
describe('ol.extent', function () {
describe('ol/extent.js', function () {
describe('buffer', function () {
it('buffers an extent by some value', function () {
const extent = [-10, -20, 10, 20];

View File

@@ -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 () {
it('returns the result from the first call when called a second time with the same args', function () {
const arg1 = {};

View File

@@ -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 () {
let circle;
beforeEach(function () {

View File

@@ -1,10 +1,12 @@
import Geometry from '../../../../../src/ol/geom/Geometry.js';
import GeometryCollection from '../../../../../src/ol/geom/GeometryCollection.js';
import LineString from '../../../../../src/ol/geom/LineString.js';
import Point from '../../../../../src/ol/geom/Point.js';
import Polygon from '../../../../../src/ol/geom/Polygon.js';
import Geometry from '../../../../src/ol/geom/Geometry.js';
import GeometryCollection from '../../../../src/ol/geom/GeometryCollection.js';
import LineString from '../../../../src/ol/geom/LineString.js';
import Point from '../../../../src/ol/geom/Point.js';
import Polygon from '../../../../src/ol/geom/Polygon.js';
import expect from '../../expect.js';
import sinon from 'sinon';
describe('ol.geom.GeometryCollection', function () {
describe('ol/geom/GeometryCollection.js', function () {
const outer = [
[0, 0],
[0, 10],

View File

@@ -1,7 +1,9 @@
import LineString from '../../../../../src/ol/geom/LineString.js';
import {isEmpty} from '../../../../../src/ol/extent.js';
import LineString from '../../../../src/ol/geom/LineString.js';
import expect from '../../expect.js';
import sinon from 'sinon';
import {isEmpty} from '../../../../src/ol/extent.js';
describe('ol.geom.LineString', function () {
describe('ol/geom/LineString.js', function () {
it('cannot be constructed with a null geometry', function () {
expect(function () {
return new LineString(null);

View File

@@ -1,8 +1,9 @@
import LineString from '../../../../../src/ol/geom/LineString.js';
import MultiLineString from '../../../../../src/ol/geom/MultiLineString.js';
import {isEmpty} from '../../../../../src/ol/extent.js';
import LineString from '../../../../src/ol/geom/LineString.js';
import MultiLineString from '../../../../src/ol/geom/MultiLineString.js';
import expect from '../../expect.js';
import {isEmpty} from '../../../../src/ol/extent.js';
describe('ol.geom.MultiLineString', function () {
describe('ol/geom/MultiLineString.js', function () {
it('cannot be constructed with a null geometry', function () {
expect(function () {
return new MultiLineString(null);

View File

@@ -1,8 +1,10 @@
import MultiPoint from '../../../../../src/ol/geom/MultiPoint.js';
import Point from '../../../../../src/ol/geom/Point.js';
import {isEmpty} from '../../../../../src/ol/extent.js';
import MultiPoint from '../../../../src/ol/geom/MultiPoint.js';
import Point from '../../../../src/ol/geom/Point.js';
import expect from '../../expect.js';
import sinon from 'sinon';
import {isEmpty} from '../../../../src/ol/extent.js';
describe('ol.geom.MultiPoint', function () {
describe('ol/geom/MultiPoint.js', function () {
it('cannot be constructed with a null geometry', function () {
expect(function () {
return new MultiPoint(null);

View File

@@ -1,7 +1,8 @@
import MultiPolygon from '../../../../../src/ol/geom/MultiPolygon.js';
import Polygon from '../../../../../src/ol/geom/Polygon.js';
import MultiPolygon from '../../../../src/ol/geom/MultiPolygon.js';
import Polygon from '../../../../src/ol/geom/Polygon.js';
import expect from '../../expect.js';
describe('ol.geom.MultiPolygon', function () {
describe('ol/geom/MultiPolygon.js', function () {
it('cannot be constructed with a null geometry', function () {
expect(function () {
return new MultiPolygon(null);

View File

@@ -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 {
get as getProjection,
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 () {
expect(function () {
return new Point(null);

View File

@@ -1,12 +1,13 @@
import Circle from '../../../../../src/ol/geom/Circle.js';
import LinearRing from '../../../../../src/ol/geom/LinearRing.js';
import Circle from '../../../../src/ol/geom/Circle.js';
import LinearRing from '../../../../src/ol/geom/LinearRing.js';
import Polygon, {
fromCircle,
fromExtent,
} from '../../../../../src/ol/geom/Polygon.js';
import {boundingExtent, isEmpty} from '../../../../../src/ol/extent.js';
} from '../../../../src/ol/geom/Polygon.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 () {
expect(function () {
return new Polygon(null);

View File

@@ -1,10 +1,8 @@
import {
linearRing,
linearRings,
} from '../../../../../../src/ol/geom/flat/area.js';
import expect from '../../../expect.js';
import {linearRing, linearRings} from '../../../../../src/ol/geom/flat/area.js';
describe('ol.geom.flat.area', function () {
describe('ol.geom.flat.area.linearRing', function () {
describe('ol/geom/flat/area.js', function () {
describe('linearRing', function () {
it('calculates the area of a triangle', function () {
const area = linearRing([0, 0, 0.5, 1, 1, 0], 0, 6, 2);
expect(area).to.be(0.5);
@@ -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 () {
const area = linearRings(
[0, 0, 0, 3, 3, 3, 3, 0, 1, 1, 2, 1, 2, 2, 1, 2],

View File

@@ -1,8 +1,9 @@
import MultiPolygon from '../../../../../../src/ol/geom/MultiPolygon.js';
import {linearRingss as linearRingssCenter} from '../../../../../../src/ol/geom/flat/center.js';
import MultiPolygon from '../../../../../src/ol/geom/MultiPolygon.js';
import expect from '../../../expect.js';
import {linearRingss as linearRingssCenter} from '../../../../../src/ol/geom/flat/center.js';
describe('ol.geom.flat.center', function () {
describe('ol.geom.flat.center.linearRingss', function () {
describe('ol/geom/flat/center.js', function () {
describe('linearRingss', function () {
it('calculates the center of a square', function () {
const squareMultiPoly = new MultiPolygon([
[

View File

@@ -1,13 +1,14 @@
import expect from '../../../expect.js';
import {
assignClosestPoint,
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 () {
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 () {
expect(
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 () {
const maxDelta = Math.sqrt(
maxSquaredDelta(flatCoordinates, 0, flatCoordinates.length, 2, 0)
@@ -290,7 +291,7 @@ describe('ol.geom.flat.closest', function () {
480.77,
];
describe('ol.geom.closest.maxSquaredDelta', function () {
describe('maxSquaredDelta', function () {
it('returns the expected value', function () {
expect(
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 () {
const maxDelta = Math.sqrt(
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 stride = 4;
describe('ol.geom.flat.closest.assignClosestPoint', function () {
describe('assignClosestPoint', function () {
it('interpolates M coordinates', function () {
const maxDelta = Math.sqrt(
maxSquaredDelta(flatCoordinates, 0, flatCoordinates.length, stride, 0)

View File

@@ -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 () {
const flatCoordinatesSimple = [0, 0, 1, 0, 1, 1, 0, 1];
const flatCoordinatesNonSimple = [
@@ -26,7 +27,7 @@ describe('ol.geom.flat.contains', function () {
4,
];
describe('ol.geom.flat.contains.linearRingContainsXY', function () {
describe('linearRingContainsXY', function () {
it('returns true for point inside a simple polygon', function () {
expect(
linearRingContainsXY(

View File

@@ -1,10 +1,11 @@
import expect from '../../../expect.js';
import {
deflateCoordinates,
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.deflateCoordinates', function () {
describe('ol/geom/flat/deflate.js', function () {
describe('deflateCoordinates', function () {
let flatCoordinates;
beforeEach(function () {
flatCoordinates = [];
@@ -25,7 +26,7 @@ describe('ol.geom.flat.deflate', function () {
});
});
describe('ol.geom.flat.deflate.deflateCoordinatesArray', function () {
describe('deflateCoordinatesArray', function () {
let flatCoordinates;
beforeEach(function () {
flatCoordinates = [];

View File

@@ -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.flipXY', function () {
describe('ol/geom/flat/flip.js', function () {
describe('flipXY', function () {
it('can flip XY coordinates', function () {
const flatCoordinates = flipXY([1, 2, 3, 4], 0, 4, 2);
expect(flatCoordinates).to.eql([2, 1, 4, 3]);

View File

@@ -1,10 +1,11 @@
import expect from '../../../expect.js';
import {
inflateCoordinates,
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.inflateCoordinates', function () {
describe('ol/geom/flat/inflate.js', function () {
describe('inflateCoordinates', function () {
it('inflates coordinates', function () {
const coordinates = inflateCoordinates([1, 2, 3, 4], 0, 4, 2);
expect(coordinates).to.eql([
@@ -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 () {
const coordinatess = inflateCoordinatesArray(
[1, 2, 3, 4, 5, 6, 7, 8],

View File

@@ -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.interpolatePoint', function () {
describe('ol/geom/flat/interpolate.js', function () {
describe('interpolatePoint', function () {
it('returns the expected value for single points', function () {
const flatCoordinates = [0, 1];
const point = interpolatePoint(flatCoordinates, 0, 2, 2, 0.5);

View File

@@ -1,11 +1,12 @@
import expect from '../../../expect.js';
import {
intersectsLineString,
intersectsLinearRing,
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.intersectsLineString', function () {
describe('ol/geom/flat/intersectsextent.js', function () {
describe('intersectsLineString', function () {
let flatCoordinates;
beforeEach(function () {
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;
beforeEach(function () {
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 ends;
beforeEach(function () {

View File

@@ -1,10 +1,11 @@
import expect from '../../../expect.js';
import {
lineStringLength,
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.lineStringLength', function () {
describe('ol/geom/flat/length.js', function () {
describe('lineStringLength', function () {
describe('stride = 2', function () {
const flatCoords = [0, 0, 1, 0, 1, 1, 0, 1];
const stride = 2;
@@ -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 () {
const flatCoords = [0, 0, 1, 0, 1, 1, 0, 1];
const stride = 2;

View File

@@ -1,13 +1,14 @@
import expect from '../../../expect.js';
import {
linearRingIsClockwise,
linearRingsAreOriented,
linearRingssAreOriented,
orientLinearRings,
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.linearRingIsClockwise', function () {
describe('ol/geom/flat/orient.js', function () {
describe('linearRingIsClockwise', function () {
it('identifies clockwise rings', function () {
const flatCoordinates = [0, 1, 1, 4, 4, 3, 3, 0];
const isClockwise = linearRingIsClockwise(
@@ -86,7 +87,7 @@ describe('ol.geom.flat.orient', function () {
});
});
describe('ol.geom.flat.orient.linearRingsAreOriented', function () {
describe('linearRingsAreOriented', function () {
const oriented = linearRingsAreOriented;
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 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 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 rightCoords = [

View File

@@ -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.coordinates', function () {
describe('ol/geom/flat/reverse.js', function () {
describe('coordinates', function () {
describe('with a stride of 2', function () {
it('can reverse empty flat coordinates', function () {
const flatCoordinates = [];

View File

@@ -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.forEach', function () {
describe('ol/geom/flat/segments.js', function () {
describe('forEach', function () {
let flatCoordinates, offset, end, stride;
beforeEach(function () {
flatCoordinates = [0, 0, 1, 1, 2, 2, 3, 3];

View File

@@ -1,11 +1,12 @@
import expect from '../../../expect.js';
import {
douglasPeucker,
quantize,
radialDistance,
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 = [
224.55,
250.15,
@@ -532,7 +533,7 @@ describe('ol.geom.flat.simplify', function () {
480.77,
];
describe('ol.geom.flat.simplify.simplifyLineString', function () {
describe('simplifyLineString', function () {
it('works with empty line strings', function () {
expect(simplifyLineString([], 0, 0, 2, 1, true)).to.eql([]);
expect(simplifyLineString([], 0, 0, 2, 1, false)).to.eql([]);
@@ -570,7 +571,7 @@ describe('ol.geom.flat.simplify', function () {
});
});
describe('ol.geom.flat.simplify.radialDistance', function () {
describe('radialDistance', function () {
let dest;
beforeEach(function () {
dest = [];
@@ -673,7 +674,7 @@ describe('ol.geom.flat.simplify', function () {
});
});
describe('ol.geom.flat.simplify.douglasPeucker', function () {
describe('douglasPeucker', function () {
let dest;
beforeEach(function () {
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 () {
const simplifiedFlatCoordinates = [];
expect(quantize([], 0, 0, 2, 2, simplifiedFlatCoordinates, 0)).to.be(0);

View File

@@ -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.matchingChunk', function () {
describe('ol/geom/flat/straightchunk.js', function () {
describe('matchingChunk', function () {
describe('single segment with stride == 3', function () {
const flatCoords = [0, 0, 42, 1, 1, 42];
const stride = 3;

View File

@@ -1,7 +1,8 @@
import {drawTextOnPath} from '../../../../../../src/ol/geom/flat/textpath.js';
import {lineStringLength} from '../../../../../../src/ol/geom/flat/length.js';
import expect from '../../../expect.js';
import {drawTextOnPath} from '../../../../../src/ol/geom/flat/textpath.js';
import {lineStringLength} from '../../../../../src/ol/geom/flat/length.js';
describe('ol.geom.flat.drawTextOnPath', function () {
describe('ol/geom/flat/drawTextOnPath.js', function () {
const horizontal = [0, 0, 100, 0];
const vertical = [0, 0, 0, 100];
const diagonal = [0, 0, 100, 100];

View File

@@ -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.lineStringIsClosed', function () {
describe('ol/geom/flat/topology.js', function () {
describe('lineStringIsClosed', function () {
it('identifies closed lines aka boundaries', function () {
const flatCoordinates = [0, 0, 3, 0, 0, 3, 0, 0];
const isClosed = lineStringIsClosed(

View File

@@ -1,12 +1,10 @@
import MultiPolygon from '../../../../../../src/ol/geom/MultiPolygon.js';
import {
rotate,
translate,
} from '../../../../../../src/ol/geom/flat/transform.js';
import {transformGeom2D} from '../../../../../../src/ol/geom/SimpleGeometry.js';
import MultiPolygon from '../../../../../src/ol/geom/MultiPolygon.js';
import expect from '../../../expect.js';
import {rotate, translate} from '../../../../../src/ol/geom/flat/transform.js';
import {transformGeom2D} from '../../../../../src/ol/geom/SimpleGeometry.js';
describe('ol.geom.flat.transform', function () {
describe('ol.geom.flat.transform.transform2D', function () {
describe('ol/geom/flat/transform.js', function () {
describe('transform2D', function () {
it('transforms a Simple Geometry to 2D', function () {
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 () {
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 () {
const multiPolygon = new MultiPolygon([
[

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

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

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

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

View File

@@ -1,6 +1,8 @@
import Projection from '../../../../src/ol/proj/Projection.js';
import Units from '../../../../src/ol/proj/Units.js';
import {HALF_SIZE} from '../../../../src/ol/proj/epsg3857.js';
import Projection from '../../../src/ol/proj/Projection.js';
import Units from '../../../src/ol/proj/Units.js';
import expect from '../expect.js';
import proj4 from 'proj4';
import {HALF_SIZE} from '../../../src/ol/proj/epsg3857.js';
import {
METERS_PER_UNIT,
addCommon,
@@ -22,11 +24,11 @@ import {
transform,
transformExtent,
useGeographic,
} from '../../../../src/ol/proj.js';
import {METERS_PER_UNIT as metersPerDegree} from '../../../../src/ol/proj/epsg4326.js';
import {register} from '../../../../src/ol/proj/proj4.js';
} from '../../../src/ol/proj.js';
import {METERS_PER_UNIT as metersPerDegree} from '../../../src/ol/proj/epsg4326.js';
import {register} from '../../../src/ol/proj/proj4.js';
describe('ol.proj', function () {
describe('ol/proj.js', function () {
afterEach(function () {
clearAllProjections();
clearUserProjection();

View File

@@ -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;
const item = {};
const item2 = {};

View File

@@ -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;
function fillLRUCache(lruCache) {

View File

@@ -1,8 +1,7 @@
import PriorityQueue, {
DROP,
} from '../../../../../src/ol/structs/PriorityQueue.js';
import PriorityQueue, {DROP} from '../../../../src/ol/structs/PriorityQueue.js';
import expect from '../../expect.js';
describe('ol.structs.PriorityQueue', function () {
describe('ol/structs/PriorityQueue.js', function () {
const identity = function (a) {
return a;
};

View File

@@ -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;
beforeEach(function () {
rBush = new RBush();

View File

@@ -1,24 +1,25 @@
import Projection from '../../../../../src/ol/proj/Projection.js';
import TileGrid from '../../../../../src/ol/tilegrid/TileGrid.js';
import TileRange from '../../../../../src/ol/TileRange.js';
import Projection from '../../../../src/ol/proj/Projection.js';
import TileGrid from '../../../../src/ol/tilegrid/TileGrid.js';
import TileRange from '../../../../src/ol/TileRange.js';
import expect from '../../expect.js';
import {
DEFAULT_MAX_ZOOM,
DEFAULT_TILE_SIZE,
} from '../../../../../src/ol/tilegrid/common.js';
import {HALF_SIZE} from '../../../../../src/ol/proj/epsg3857.js';
} from '../../../../src/ol/tilegrid/common.js';
import {HALF_SIZE} from '../../../../src/ol/proj/epsg3857.js';
import {
METERS_PER_UNIT,
get as getProjection,
} from '../../../../../src/ol/proj.js';
} from '../../../../src/ol/proj.js';
import {
createForExtent,
createForProjection,
createXYZ,
getForProjection as getTileGridForProjection,
} from '../../../../../src/ol/tilegrid.js';
import {createOrUpdate} from '../../../../../src/ol/extent.js';
} from '../../../../src/ol/tilegrid.js';
import {createOrUpdate} from '../../../../src/ol/extent.js';
describe('ol.tilegrid.TileGrid', function () {
describe('ol/tilegrid/TileGrid.js', function () {
let resolutions;
let origin;
let tileSize;

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

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

View File

@@ -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('mat4.create()', function () {
describe('ol/vec/mat4.js', function () {
describe('create()', function () {
it('returns the expected matrix', function () {
expect(create()).to.eql([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]);
});
});
describe('mat4.fromTransform()', function () {
describe('fromTransform()', function () {
it('sets the expected transform on the matrix', function () {
const transform = [1, 2, 3, 4, 5, 6];
const result = create();

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

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

View File

@@ -1,18 +1,24 @@
#! /usr/bin/env node
const puppeteer = require('puppeteer');
const webpack = require('webpack');
const config = require('./webpack.config');
const webpackMiddleware = require('webpack-dev-middleware');
const express = require('express');
const path = require('path');
const png = require('pngjs');
const fs = require('fs');
const fse = require('fs-extra');
const pixelmatch = require('pixelmatch');
const yargs = require('yargs');
const log = require('loglevelnext');
const globby = require('globby');
const serveStatic = require('serve-static');
import config from './webpack.config.js';
import esMain from 'es-main';
import express from 'express';
import fs from 'fs';
import fse from 'fs-extra';
import globby from 'globby';
import log from 'loglevelnext';
import path from 'path';
import pixelmatch from 'pixelmatch';
import png from 'pngjs';
import puppeteer from 'puppeteer';
import serveStatic from 'serve-static';
import webpack from 'webpack';
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));
@@ -20,11 +26,11 @@ function getHref(entry) {
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) {
const items = [];
@@ -91,15 +97,15 @@ function serve(options) {
}
function getActualScreenshotPath(entry) {
return path.join(__dirname, path.dirname(entry), 'actual.png');
return path.join(baseDir, path.dirname(entry), 'actual.png');
}
function getExpectedScreenshotPath(entry) {
return path.join(__dirname, path.dirname(entry), 'expected.png');
return path.join(baseDir, path.dirname(entry), 'expected.png');
}
function getPassFilePath(entry) {
return path.join(__dirname, path.dirname(entry), 'pass');
return path.join(baseDir, path.dirname(entry), 'pass');
}
function parsePNG(filepath) {
@@ -284,7 +290,7 @@ async function getLatest(patterns) {
async function getOutdated(entries, options) {
const libTime = await getLatest(
path.join(__dirname, '..', 'src', 'ol', '**', '*')
path.join(baseDir, '..', 'src', 'ol', '**', '*')
);
options.log.debug('library time', libTime);
const outdated = [];
@@ -298,7 +304,7 @@ async function getOutdated(entries, options) {
}
const caseTime = await getLatest(
path.join(__dirname, path.dirname(entry), '**', '*')
path.join(baseDir, path.dirname(entry), '**', '*')
);
options.log.debug('case time', entry, caseTime);
if (passTime < caseTime) {
@@ -337,8 +343,8 @@ function sleep(ms) {
});
}
if (require.main === module) {
const options = yargs
if (esMain(import.meta)) {
const options = yargs(hideBin(process.argv))
.option('fix', {
describe: 'Accept all screenshots as accepted',
type: 'boolean',

View File

@@ -1,7 +1,10 @@
const fs = require('fs');
const path = require('path');
import fs from 'fs';
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) => {
let exists = true;
@@ -18,8 +21,8 @@ caseDirs.forEach((c) => {
entry[`cases/${c}/main`] = `./cases/${c}/main.js`;
});
module.exports = {
context: __dirname,
export default {
context: baseDir,
target: 'web',
entry: entry,
devtool: 'source-map',
@@ -30,18 +33,18 @@ module.exports = {
test: /\.js$/,
use: {
loader: path.join(
__dirname,
'../../examples/webpack/worker-loader.js'
baseDir,
'../../examples/webpack/worker-loader.cjs'
),
},
include: [path.join(__dirname, '../../src/ol/worker')],
include: [path.join(baseDir, '../../src/ol/worker')],
},
],
},
resolve: {
alias: {
// ol-mapbox-style imports ol/style/Style etc
ol: path.join(__dirname, '..', '..', 'src', 'ol'),
ol: path.join(baseDir, '..', '..', 'src', 'ol'),
},
},
};