diff --git a/.babelrc b/.babelrc deleted file mode 100644 index 69fe715..0000000 --- a/.babelrc +++ /dev/null @@ -1,3 +0,0 @@ -{ - "presets": ["@babel/preset-env"] -} diff --git a/.circleci/config.yml b/.circleci/config.yml index 9d4cfb0..f8111a6 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -26,11 +26,11 @@ workflows: test-all-node-versions: jobs: - test: - docker_image: circleci/node:12-browsers + docker_image: cimg/node:14.21-browsers - test: - docker_image: circleci/node:13-browsers + docker_image: cimg/node:16.20-browsers - test: - docker_image: circleci/node:14-browsers + docker_image: cimg/node:18.9-browsers - test: - docker_image: circleci/node:16-browsers + docker_image: cimg/node:20.9-browsers - test diff --git a/README.md b/README.md index 91d6606..8b8fb31 100644 --- a/README.md +++ b/README.md @@ -155,7 +155,7 @@ CalendarFactoryAsync({locale: 'ja-JP'}).then(function(cal) { ## License -Copyright © 2019-2023, JEDLSoft +Copyright © 2019-2024, JEDLSoft Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. diff --git a/package.json b/package.json index 8dec3f9..2f95040 100644 --- a/package.json +++ b/package.json @@ -48,18 +48,23 @@ "type": "git", "url": "git@github.com:iLib-js/ilib-es6.git" }, + "engines": { + "node": ">= 14.0.0" + }, "scripts": { "build": "npm run build:prod", "dist": "npm pack", - "test": "LANG=en_US.UTF8 node test/testSuite.js", - "debug": "node --inspect-brk test/testSuite.js", + "test": "npm run test:cli", + "test:cli": "LANG=en_US.UTF8 node --experimental-vm-modules node_modules/.bin/jest --testEnvironment node", + "test:watch": "LANG=en_US.UTF8 node --experimental-vm-modules node_modules/.bin/jest --testEnvironment node --watch", + "debug": "npm run build:dev ; node --experimental-vm-modules --inspect-brk node_modules/.bin/jest --testEnvironment node -i", "clean": "git clean -f -d * ; rm -rf lib" }, "dependencies": { "ilib": "14.19.0" }, "devDependencies": { - "assertextras": "^1.1.0", - "nodeunit": "^0.11.3" + "jest": "^29.7.0", + "npm-run-all": "^4.1.5" } } diff --git a/src/Address.js b/src/Address.js index 2c9f501..a79f4d1 100644 --- a/src/Address.js +++ b/src/Address.js @@ -2,7 +2,7 @@ * Address.js - ES6 wrappers around an ilib class * * @license - * Copyright © 2018, 2022 JEDLSoft + * Copyright © 2018, 2022, 2024 JEDLSoft * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -28,7 +28,7 @@ export default class Address { } static create(address, options = {}) { - return promisifyFunction(function(opts = {}) { + return promisifyFunction((opts = {}) => { const { address } = opts; return new ilibAddress(address, opts); }, Object.assign({}, options, { diff --git a/src/AddressFmt.js b/src/AddressFmt.js index 8fa2859..8ff1b09 100644 --- a/src/AddressFmt.js +++ b/src/AddressFmt.js @@ -2,7 +2,7 @@ * AddressFmt.js - ES6 wrappers around an ilib class * * @license - * Copyright © 2018, 2022 JEDLSoft + * Copyright © 2018, 2022, 2024 JEDLSoft * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,12 +26,12 @@ function wrapGetFormatInfo(formatter) { if (!formatter) return; const oldGetFormatInfo = ilibAddressFmt.prototype.getFormatInfo.bind(formatter); - formatter.getFormatInfo = function(locale, sync, callback) { + formatter.getFormatInfo = (locale, sync, callback) => { if (typeof(sync) === "undefined" || sync) { return oldGetFormatInfo(locale, sync, callback); } - return promisifyFunction(function(options = {}) { + return promisifyFunction((options = {}) => { const {locale, onLoad} = options; return oldGetFormatInfo(locale, false, onLoad); }, { @@ -50,7 +50,7 @@ export default class AddressFmt { } static create(options = {}) { - return new Promise(function(resolve, reject) { + return new Promise((resolve, reject) => { let tempOptions = Object.assign({}, options, { sync: false, onLoad: af => resolve(wrapGetFormatInfo(af)) diff --git a/src/CType.js b/src/CType.js index 9da40b9..fee7509 100644 --- a/src/CType.js +++ b/src/CType.js @@ -2,7 +2,7 @@ * CType.js - ES6 wrappers around an ilib class * * @license - * Copyright © 2018, 2022 JEDLSoft + * Copyright © 2018, 2022, 2024 JEDLSoft * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,12 +26,12 @@ function CType(ch) { return ilibCType(ch); }; -CType._init = function (sync, loadParams, onLoad) { +CType._init = (sync, loadParams, onLoad) => { if (typeof(sync) === "undefined" || sync) { return ilibCType._init(sync, loadParams, onLoad); } - return promisifyFunction(function(options = {}) { + return promisifyFunction((options = {}) => { const { sync, loadParams, onLoad } = options; return ilibCType._init(sync, loadParams, onLoad); }, { @@ -41,12 +41,12 @@ CType._init = function (sync, loadParams, onLoad) { }); }; -CType._load = function (name, sync, loadParams, onLoad) { +CType._load = (name, sync, loadParams, onLoad) => { if (typeof(sync) === "undefined" || sync) { return ilibCType._load(name, sync, loadParams, onLoad); } - return promisifyFunction(function(options = {}) { + return promisifyFunction((options = {}) => { const { name, sync, loadParams, onLoad } = options; return ilibCType._init(name, sync, loadParams, onLoad); }, { diff --git a/src/CalendarFactory.js b/src/CalendarFactory.js index 71d8af1..636e32c 100644 --- a/src/CalendarFactory.js +++ b/src/CalendarFactory.js @@ -2,7 +2,7 @@ * CalendarFactory.js - ES6 wrappers around an ilib class * * @license - * Copyright © 2018, 2022 JEDLSoft + * Copyright © 2018, 2022, 2024 JEDLSoft * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,7 +23,7 @@ import { promisifyFunction } from './promisify.js'; import {default as CalendarFactory } from 'ilib/lib/CalendarFactory.js'; export function CalendarFactoryAsync(options = {}) { - var opts = Object.assign({}, options, {sync: false}); + const opts = Object.assign({}, options, {sync: false}); return promisifyFunction(CalendarFactory, opts); }; diff --git a/src/CharmapFactory.js b/src/CharmapFactory.js index 2abd62e..84e74f4 100644 --- a/src/CharmapFactory.js +++ b/src/CharmapFactory.js @@ -2,7 +2,7 @@ * CharmapFactory.js - ES6 wrappers around an ilib class * * @license - * Copyright © 2018, 2022 JEDLSoft + * Copyright © 2018, 2022, 2024 JEDLSoft * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,7 +23,7 @@ import { promisifyFunction } from './promisify.js'; import { default as CharmapFactory } from 'ilib/lib/CharmapFactory.js'; export function CharmapFactoryAsync(options = {}) { - var opts = Object.assign({}, options); + const opts = Object.assign({}, options); opts.sync = false; return promisifyFunction(CharmapFactory, opts); }; diff --git a/src/DateFactory.js b/src/DateFactory.js index 74a4d58..0a0d710 100644 --- a/src/DateFactory.js +++ b/src/DateFactory.js @@ -2,7 +2,7 @@ * DateFactory.js - ES6 wrappers around an ilib class * * @license - * Copyright © 2018, 2022 JEDLSoft + * Copyright © 2018, 2022, 2024 JEDLSoft * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,7 +23,7 @@ import { promisifyFunction } from './promisify.js'; import { default as DateFactory } from 'ilib/lib/DateFactory.js'; export function DateFactoryAsync(options = {}) { - var opts = Object.assign({}, options, {sync: false}); + const opts = Object.assign({}, options, {sync: false}); return promisifyFunction(DateFactory, opts); }; diff --git a/src/GlyphString.js b/src/GlyphString.js index a1e4113..c4b3891 100644 --- a/src/GlyphString.js +++ b/src/GlyphString.js @@ -2,7 +2,7 @@ * GlyphString.js - ES6 wrappers around an ilib class * * @license - * Copyright © 2018, 2022 JEDLSoft + * Copyright © 2018, 2022, 2024 JEDLSoft * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -28,7 +28,7 @@ export default class GlyphString { } static create(str, options = {}) { - return promisifyFunction(function(opts = {}) { + return promisifyFunction((opts = {}) => { const { str } = opts; return new ilibGlyphString(str, opts); }, Object.assign({}, options, { diff --git a/src/INumber.js b/src/INumber.js index 3f43cac..6d52c8f 100644 --- a/src/INumber.js +++ b/src/INumber.js @@ -2,7 +2,7 @@ * INumber.js - ES6 wrappers around an ilib class * * @license - * Copyright © 2018, 2022 JEDLSoft + * Copyright © 2018, 2022, 2024 JEDLSoft * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -28,7 +28,7 @@ export default class INumber { } static create(str, options = {}) { - return promisifyFunction(function(opts = {}) { + return promisifyFunction((opts = {}) => { const { str } = opts; return new ilibINumber(str, opts); }, Object.assign({}, options, { diff --git a/src/IString.js b/src/IString.js index 643f999..f6665db 100644 --- a/src/IString.js +++ b/src/IString.js @@ -2,7 +2,7 @@ * IString.js - ES6 wrappers around an ilib class * * @license - * Copyright © 2018, 2022 JEDLSoft + * Copyright © 2018, 2022, 2024 JEDLSoft * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,19 +26,19 @@ function wrapSetLocale(str) { if (!str) return; const oldSetLocale = ilibIString.prototype.setLocale.bind(str); - str.setLocale = function(locale, sync, loadParams, onLoad) { + str.setLocale = (locale, sync, loadParams, onLoad) => { if (typeof(sync) === "undefined" || sync) { return oldSetLocale(locale, sync, loadParams, onLoad); } - return promisifyFunction(function(opts = {}) { + return promisifyFunction((opts = {}) => { const { locale, sync, loadParams, onLoad } = opts; return oldSetLocale(locale, sync, loadParams, onLoad); }, { - locale: locale, + locale, sync: false, - loadParams: loadParams, - onLoad: onLoad + loadParams, + onLoad }); }; @@ -50,19 +50,19 @@ export default class IString { return wrapSetLocale(new ilibIString(str)); } - static loadPlurals(sync, locale, loadParams, callback) { + static loadPlurals(sync, locale, loadParams, onLoad) { if (typeof(sync) === "undefined" || sync) { return ilibIString.loadPlurals(sync, locale, loadParams, onLoad); } - return promisifyFunction(function(options = {}) { + return promisifyFunction((options = {}) => { const {locale, loadParams, onLoad} = options; return ilibIString.loadPlurals(false, locale, loadParams, onLoad); }, { - loadParams: loadParams, - locale: locale, + loadParams, + locale, sync: false, - onLoad: callback + onLoad }); } diff --git a/src/LocaleInfo.js b/src/LocaleInfo.js index f030ba9..386b087 100644 --- a/src/LocaleInfo.js +++ b/src/LocaleInfo.js @@ -2,7 +2,7 @@ * LocaleInfo.js - ES6 wrappers around an ilib class * * @license - * Copyright © 2018, 2022 JEDLSoft + * Copyright © 2018, 2022, 2024 JEDLSoft * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -28,7 +28,7 @@ export default class LocaleInfo { } static create(locale, options = {}) { - return promisifyFunction(function(opts = {}) { + return promisifyFunction((opts = {}) => { const { locale } = opts; return new ilibLocaleInfo(locale, opts); }, Object.assign({}, options, { diff --git a/src/Name.js b/src/Name.js index 12fffbd..f115755 100644 --- a/src/Name.js +++ b/src/Name.js @@ -28,7 +28,7 @@ export default class Name { } static create(name, options = {}) { - return promisifyFunction(function(opts = {}) { + return promisifyFunction((opts = {}) => { const { name } = opts; return new ilibName(name, opts); }, Object.assign({}, options, { diff --git a/src/NormString.js b/src/NormString.js index a0045b1..f9b2d62 100644 --- a/src/NormString.js +++ b/src/NormString.js @@ -28,7 +28,7 @@ export default class NormString { } static create(str, options = {}) { - return promisifyFunction(function(opts = {}) { + return promisifyFunction((opts = {}) => { const { str } = opts; return new ilibNormString(str, opts); }, Object.assign({}, options, { diff --git a/src/PhoneFmt.js b/src/PhoneFmt.js index 8650711..3ac4162 100644 --- a/src/PhoneFmt.js +++ b/src/PhoneFmt.js @@ -26,13 +26,13 @@ function wrapFormat(phoneFmt) { if (!phoneFmt) return; const oldFormat = ilibPhoneFmt.prototype.format.bind(phoneFmt); - phoneFmt.format = function(number, options = {}) { + phoneFmt.format = (number, options = {}) => { const { sync } = options; if (typeof(sync) === "undefined" || sync) { return oldFormat(number, options); } - return promisifyFunction(function(opts = {}) { + return promisifyFunction((opts = {}) => { const { number } = opts; return oldFormat(number, opts); }, Object.assign({}, options, { @@ -49,7 +49,7 @@ export default class PhoneFmt { } static create(options = {}) { - return promisifyFunction(function(options = {}) { + return promisifyFunction((options = {}) => { return wrapFormat(new ilibPhoneFmt(options)); }, options); } diff --git a/src/PhoneNumber.js b/src/PhoneNumber.js index 5d65b41..c0c462e 100644 --- a/src/PhoneNumber.js +++ b/src/PhoneNumber.js @@ -26,13 +26,13 @@ function wrapNormalize(phoneNumber) { if (!phoneNumber) return; const oldNormalize = ilibPhoneNumber.prototype.normalize.bind(phoneNumber); - phoneNumber.normalize = function(options = {}) { + phoneNumber.normalize = (options = {}) => { const { sync } = options; if (typeof(sync) === "undefined" || sync) { return oldNormalize(options); } - return promisifyFunction(function(opts = {}) { + return promisifyFunction((opts = {}) => { return oldNormalize(opts); }, options); }; @@ -46,11 +46,22 @@ export default class PhoneNumber { } static create(phoneNumber, options = {}) { - return promisifyFunction(function(opts = {}) { + return promisifyFunction((opts = {}) => { const { phoneNumber } = opts; return wrapNormalize(new ilibPhoneNumber(phoneNumber, opts)); }, Object.assign({}, options, { phoneNumber: phoneNumber })); } + + static parseImsi(imsi, options = {}) { + const { sync } = options; + if (typeof(sync) === "undefined" || sync) { + return ilibPhoneNumber.parseImsi(imsi, options); + } + + return promisifyFunction((opts = {}) => { + return ilibPhoneNumber.parseImsi(imsi, opts); + }, options); + } }; diff --git a/src/ScriptInfo.js b/src/ScriptInfo.js index 7ef221e..8f18038 100644 --- a/src/ScriptInfo.js +++ b/src/ScriptInfo.js @@ -28,7 +28,7 @@ export default class ScriptInfo { } static create(script, options = {}) { - return promisifyFunction(function(opts = {}) { + return promisifyFunction((opts = {}) => { const { script } = opts; return new ilibScriptInfo(script, opts); }, Object.assign({}, options, { @@ -41,7 +41,7 @@ export default class ScriptInfo { return ilibScriptInfo.getAllScripts(sync, loadParams, callback); } - return promisifyFunction(function(options = {}) { + return promisifyFunction((options = {}) => { const {loadParams, onLoad} = options; return ilibScriptInfo.getAllScripts(false, loadParams, onLoad); }, { diff --git a/src/TimeZone.js b/src/TimeZone.js index b079061..a2175fe 100644 --- a/src/TimeZone.js +++ b/src/TimeZone.js @@ -24,12 +24,12 @@ import { default as ilibTimeZone } from 'ilib/lib/TimeZone.js'; const oldGetAvailableIds = ilibTimeZone.getAvailableIds; -ilibTimeZone.getAvailableIds = function(country, sync, callback) { +ilibTimeZone.getAvailableIds = (country, sync, callback) => { if (typeof(sync) === "undefined" || sync) { return oldGetAvailableIds(country, sync, callback); } - return promisifyFunction(function(options = {}) { + return promisifyFunction((options = {}) => { const {country, onLoad} = options; return oldGetAvailableIds(country, false, onLoad); }, { @@ -53,7 +53,7 @@ export default class TimeZone { return oldGetAvailableIds(country, sync, callback); } - return promisifyFunction(function(options = {}) { + return promisifyFunction((options = {}) => { const {country, onLoad} = options; return oldGetAvailableIds(country, false, onLoad); }, { diff --git a/src/isAlnum.js b/src/isAlnum.js index e3874bc..0aa5419 100644 --- a/src/isAlnum.js +++ b/src/isAlnum.js @@ -2,7 +2,7 @@ * isAlnum.js - ES6 wrappers around an ilib class * * @license - * Copyright © 2018, 2022 JEDLSoft + * Copyright © 2018, 2022, 2024 JEDLSoft * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,12 +26,12 @@ function isAlnum(ch) { return ilibisAlnum(ch); }; -isAlnum._init = function (sync, loadParams, onLoad) { +isAlnum._init = (sync, loadParams, onLoad) => { if (typeof(sync) === "undefined" || sync) { - return ilibisAlnum(sync, loadParams, onLoad); + return ilibisAlnum._init(sync, loadParams, onLoad); } - return promisifyFunction(function(options = {}) { + return promisifyFunction((options = {}) => { const { sync, loadParams, onLoad } = options; return ilibisAlnum._init(sync, loadParams, onLoad); }, { diff --git a/src/isAlpha.js b/src/isAlpha.js index 0e675fd..909fc1f 100644 --- a/src/isAlpha.js +++ b/src/isAlpha.js @@ -2,7 +2,7 @@ * isAlpha.js - ES6 wrappers around an ilib class * * @license - * Copyright © 2018, 2022 JEDLSoft + * Copyright © 2018, 2022, 2024 JEDLSoft * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,12 +26,12 @@ function isAlpha(ch) { return ilibisAlpha(ch); }; -isAlpha._init = function (sync, loadParams, onLoad) { +isAlpha._init = (sync, loadParams, onLoad) => { if (typeof(sync) === "undefined" || sync) { - return ilibisAlpha(sync, loadParams, onLoad); + return ilibisAlpha._init(sync, loadParams, onLoad); } - return promisifyFunction(function(options = {}) { + return promisifyFunction((options = {}) => { const { sync, loadParams, onLoad } = options; return ilibisAlpha._init(sync, loadParams, onLoad); }, { diff --git a/src/isAscii.js b/src/isAscii.js index d962cec..afa9bfa 100644 --- a/src/isAscii.js +++ b/src/isAscii.js @@ -2,7 +2,7 @@ * isAscii.js - ES6 wrappers around an ilib class * * @license - * Copyright © 2018, 2022 JEDLSoft + * Copyright © 2018, 2022, 2024 JEDLSoft * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,12 +26,12 @@ function isAscii(ch) { return ilibisAscii(ch); }; -isAscii._init = function (sync, loadParams, onLoad) { +isAscii._init = (sync, loadParams, onLoad) => { if (typeof(sync) === "undefined" || sync) { - return ilibisAscii(sync, loadParams, onLoad); + return ilibisAscii._init(sync, loadParams, onLoad); } - return promisifyFunction(function(options = {}) { + return promisifyFunction((options = {}) => { const { sync, loadParams, onLoad } = options; return ilibisAscii._init(sync, loadParams, onLoad); }, { diff --git a/src/isBlank.js b/src/isBlank.js index f7cd86c..54ad9eb 100644 --- a/src/isBlank.js +++ b/src/isBlank.js @@ -2,7 +2,7 @@ * isBlank.js - ES6 wrappers around an ilib class * * @license - * Copyright © 2018, 2022 JEDLSoft + * Copyright © 2018, 2022, 2024 JEDLSoft * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,12 +26,12 @@ function isBlank(ch) { return ilibisBlank(ch); }; -isBlank._init = function (sync, loadParams, onLoad) { +isBlank._init = (sync, loadParams, onLoad) => { if (typeof(sync) === "undefined" || sync) { - return ilibisBlank(sync, loadParams, onLoad); + return ilibisBlank._init(sync, loadParams, onLoad); } - return promisifyFunction(function(options = {}) { + return promisifyFunction((options = {}) => { const { sync, loadParams, onLoad } = options; return ilibisBlank._init(sync, loadParams, onLoad); }, { diff --git a/src/isCntrl.js b/src/isCntrl.js index b2ebcb8..9b34524 100644 --- a/src/isCntrl.js +++ b/src/isCntrl.js @@ -2,7 +2,7 @@ * isCntrl.js - ES6 wrappers around an ilib class * * @license - * Copyright © 2018, 2022 JEDLSoft + * Copyright © 2018, 2022, 2024 JEDLSoft * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,12 +26,12 @@ function isCntrl(ch) { return ilibisCntrl(ch); }; -isCntrl._init = function (sync, loadParams, onLoad) { +isCntrl._init = (sync, loadParams, onLoad) => { if (typeof(sync) === "undefined" || sync) { - return ilibisCntrl(sync, loadParams, onLoad); + return ilibisCntrl._init(sync, loadParams, onLoad); } - return promisifyFunction(function(options = {}) { + return promisifyFunction((options = {}) => { const { sync, loadParams, onLoad } = options; return ilibisCntrl._init(sync, loadParams, onLoad); }, { diff --git a/src/isDigit.js b/src/isDigit.js index c666c2a..5567394 100644 --- a/src/isDigit.js +++ b/src/isDigit.js @@ -2,7 +2,7 @@ * isDigit.js - ES6 wrappers around an ilib class * * @license - * Copyright © 2018, 2022 JEDLSoft + * Copyright © 2018, 2022, 2024 JEDLSoft * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,12 +26,12 @@ function isDigit(ch) { return ilibisDigit(ch); }; -isDigit._init = function (sync, loadParams, onLoad) { +isDigit._init = (sync, loadParams, onLoad) => { if (typeof(sync) === "undefined" || sync) { - return ilibisDigit(sync, loadParams, onLoad); + return ilibisDigit._init(sync, loadParams, onLoad); } - return promisifyFunction(function(options = {}) { + return promisifyFunction((options = {}) => { const { sync, loadParams, onLoad } = options; return ilibisDigit._init(sync, loadParams, onLoad); }, { diff --git a/src/isGraph.js b/src/isGraph.js index 553bcb9..67459f0 100644 --- a/src/isGraph.js +++ b/src/isGraph.js @@ -2,7 +2,7 @@ * isGraph.js - ES6 wrappers around an ilib class * * @license - * Copyright © 2018, 2022 JEDLSoft + * Copyright © 2018, 2022, 2024 JEDLSoft * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,12 +26,12 @@ function isGraph(ch) { return ilibisGraph(ch); }; -isGraph._init = function (sync, loadParams, onLoad) { +isGraph._init = (sync, loadParams, onLoad) => { if (typeof(sync) === "undefined" || sync) { - return ilibisGraph(sync, loadParams, onLoad); + return ilibisGraph._init(sync, loadParams, onLoad); } - return promisifyFunction(function(options = {}) { + return promisifyFunction((options = {}) => { const { sync, loadParams, onLoad } = options; return ilibisGraph._init(sync, loadParams, onLoad); }, { diff --git a/src/isIdeo.js b/src/isIdeo.js index 324003b..d93ca60 100644 --- a/src/isIdeo.js +++ b/src/isIdeo.js @@ -2,7 +2,7 @@ * isIdeo.js - ES6 wrappers around an ilib class * * @license - * Copyright © 2018, 2022 JEDLSoft + * Copyright © 2018, 2022, 2024 JEDLSoft * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,12 +26,12 @@ function isIdeo(ch) { return ilibisIdeo(ch); }; -isIdeo._init = function (sync, loadParams, onLoad) { +isIdeo._init = (sync, loadParams, onLoad) => { if (typeof(sync) === "undefined" || sync) { - return ilibisIdeo(sync, loadParams, onLoad); + return ilibisIdeo._init(sync, loadParams, onLoad); } - return promisifyFunction(function(options = {}) { + return promisifyFunction((options = {}) => { const { sync, loadParams, onLoad } = options; return ilibisIdeo._init(sync, loadParams, onLoad); }, { diff --git a/src/isLower.js b/src/isLower.js index d06cbdd..b383d8c 100644 --- a/src/isLower.js +++ b/src/isLower.js @@ -2,7 +2,7 @@ * isLower.js - ES6 wrappers around an ilib class * * @license - * Copyright © 2018, 2022 JEDLSoft + * Copyright © 2018, 2022, 2024 JEDLSoft * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,12 +26,12 @@ function isLower(ch) { return ilibisLower(ch); }; -isLower._init = function (sync, loadParams, onLoad) { +isLower._init = (sync, loadParams, onLoad) => { if (typeof(sync) === "undefined" || sync) { - return ilibisLower(sync, loadParams, onLoad); + return ilibisLower._init(sync, loadParams, onLoad); } - return promisifyFunction(function(options = {}) { + return promisifyFunction((options = {}) => { const { sync, loadParams, onLoad } = options; return ilibisLower._init(sync, loadParams, onLoad); }, { diff --git a/src/isPrint.js b/src/isPrint.js index 9414b70..5973357 100644 --- a/src/isPrint.js +++ b/src/isPrint.js @@ -2,7 +2,7 @@ * isPrint.js - ES6 wrappers around an ilib class * * @license - * Copyright © 2018, 2022 JEDLSoft + * Copyright © 2018, 2022, 2024 JEDLSoft * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,12 +26,12 @@ function isPrint(ch) { return ilibisPrint(ch); }; -isPrint._init = function (sync, loadParams, onLoad) { +isPrint._init = (sync, loadParams, onLoad) => { if (typeof(sync) === "undefined" || sync) { - return ilibisPrint(sync, loadParams, onLoad); + return ilibisPrint._init(sync, loadParams, onLoad); } - return promisifyFunction(function(options = {}) { + return promisifyFunction((options = {}) => { const { sync, loadParams, onLoad } = options; return ilibisPrint._init(sync, loadParams, onLoad); }, { diff --git a/src/isPunct.js b/src/isPunct.js index 62301c7..62a27d0 100644 --- a/src/isPunct.js +++ b/src/isPunct.js @@ -26,12 +26,12 @@ function isPunct(ch) { return ilibisPunct(ch); }; -isPunct._init = function (sync, loadParams, onLoad) { +isPunct._init = (sync, loadParams, onLoad) => { if (typeof(sync) === "undefined" || sync) { - return ilibisPunct(sync, loadParams, onLoad); + return ilibisPunct._init(sync, loadParams, onLoad); } - return promisifyFunction(function(options = {}) { + return promisifyFunction((options = {}) => { const { sync, loadParams, onLoad } = options; return ilibisPunct._init(sync, loadParams, onLoad); }, { diff --git a/src/isScript.js b/src/isScript.js index 6d166c5..9544f28 100644 --- a/src/isScript.js +++ b/src/isScript.js @@ -2,7 +2,7 @@ * isScript.js - ES6 wrappers around an ilib class * * @license - * Copyright © 2018, 2022 JEDLSoft + * Copyright © 2018, 2022, 2024 JEDLSoft * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,16 +22,16 @@ import { promisifyFunction } from './promisify.js'; import { default as ilibisScript } from 'ilib/lib/isScript.js'; -function isScript(ch) { - return ilibisScript(ch); +function isScript(ch, scriptName) { + return ilibisScript(ch, scriptName); }; -isScript._init = function (sync, loadParams, onLoad) { +isScript._init = (sync, loadParams, onLoad) => { if (typeof(sync) === "undefined" || sync) { - return ilibisScript(sync, loadParams, onLoad); + return ilibisScript._init(sync, loadParams, onLoad); } - return promisifyFunction(function(options = {}) { + return promisifyFunction((options = {}) => { const { sync, loadParams, onLoad } = options; return ilibisScript._init(sync, loadParams, onLoad); }, { diff --git a/src/isSpace.js b/src/isSpace.js index bf55ab3..001185c 100644 --- a/src/isSpace.js +++ b/src/isSpace.js @@ -2,7 +2,7 @@ * isSpace.js - ES6 wrappers around an ilib class * * @license - * Copyright © 2018, 2022 JEDLSoft + * Copyright © 2018, 2022, 2024 JEDLSoft * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,12 +26,12 @@ function isSpace(ch) { return ilibisSpace(ch); }; -isSpace._init = function (sync, loadParams, onLoad) { +isSpace._init = (sync, loadParams, onLoad) => { if (typeof(sync) === "undefined" || sync) { - return ilibisSpace(sync, loadParams, onLoad); + return ilibisSpace._init(sync, loadParams, onLoad); } - return promisifyFunction(function(options = {}) { + return promisifyFunction((options = {}) => { const { sync, loadParams, onLoad } = options; return ilibisSpace._init(sync, loadParams, onLoad); }, { diff --git a/src/isUpper.js b/src/isUpper.js index 082b6ea..0594c23 100644 --- a/src/isUpper.js +++ b/src/isUpper.js @@ -2,7 +2,7 @@ * isUpper.js - ES6 wrappers around an ilib class * * @license - * Copyright © 2018, 2022 JEDLSoft + * Copyright © 2018, 2022, 2024 JEDLSoft * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,12 +26,12 @@ function isUpper(ch) { return ilibisUpper(ch); }; -isUpper._init = function (sync, loadParams, onLoad) { +isUpper._init = (sync, loadParams, onLoad) => { if (typeof(sync) === "undefined" || sync) { - return ilibisUpper(sync, loadParams, onLoad); + return ilibisUpper._init(sync, loadParams, onLoad); } - return promisifyFunction(function(options = {}) { + return promisifyFunction((options = {}) => { const { sync, loadParams, onLoad } = options; return ilibisUpper._init(sync, loadParams, onLoad); }, { diff --git a/src/isXdigit.js b/src/isXdigit.js index e5fbd35..8ce2e44 100644 --- a/src/isXdigit.js +++ b/src/isXdigit.js @@ -2,7 +2,7 @@ * isXdigit.js - ES6 wrappers around an ilib class * * @license - * Copyright © 2018, 2022 JEDLSoft + * Copyright © 2018, 2022, 2024 JEDLSoft * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,12 +26,12 @@ function isXdigit(ch) { return ilibisXdigit(ch); }; -isXdigit._init = function (sync, loadParams, onLoad) { +isXdigit._init = (sync, loadParams, onLoad) => { if (typeof(sync) === "undefined" || sync) { - return ilibisXdigit(sync, loadParams, onLoad); + return ilibisXdigit._init(sync, loadParams, onLoad); } - return promisifyFunction(function(options = {}) { + return promisifyFunction((options = {}) => { const { sync, loadParams, onLoad } = options; return ilibisXdigit._init(sync, loadParams, onLoad); }, { diff --git a/src/package.json b/src/package.json new file mode 100644 index 0000000..6990891 --- /dev/null +++ b/src/package.json @@ -0,0 +1 @@ +{"type": "module"} diff --git a/src/promisify.js b/src/promisify.js index efaf497..e1d0ba5 100644 --- a/src/promisify.js +++ b/src/promisify.js @@ -21,8 +21,8 @@ export function promisifyFunction(func, options = {}) { const { sync, onLoad } = options; let tempOptions = Object.assign({}, options, {sync: false}); - let promise = new Promise(function(resolve, reject) { - tempOptions.onLoad = function(result) { + let promise = new Promise((resolve, reject) => { + tempOptions.onLoad = (result) => { resolve(result); } func(tempOptions); @@ -30,7 +30,7 @@ export function promisifyFunction(func, options = {}) { if (onLoad) { promise.then(onLoad); } - promise.catch(function(err) { + promise.catch(err => { console.log("Error caught: " + err); if (onLoad) onLoad(undefined); return err; @@ -39,7 +39,7 @@ export function promisifyFunction(func, options = {}) { } export default function promisify(func, options = {}) { - return promisifyFunction(function(opts) { + return promisifyFunction(opts => { return new func(opts); }, options); }; diff --git a/test/addressasync.test.js b/test/addressasync.test.js new file mode 100644 index 0000000..621581b --- /dev/null +++ b/test/addressasync.test.js @@ -0,0 +1,411 @@ +/* + * addressasync.test.js - test the address parsing and formatting routines asynchronously + * + * Copyright © 2018, 2022, 2024 JEDLSoft + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import AddressFmt from "../src/AddressFmt.js"; +import Address from "../src/Address.js"; + +function searchRegions(array, regionCode) { + return array.find(region => { + return region.code === regionCode; + }); +} + +describe("testAddressAsync", () => { + test("ParseAddressAsyncSimple", () => { + expect.assertions(7); + new Address("950 W Maude Ave.\nSunnyvale, CA 94085\nUSA", { + locale: 'en-US', + sync: false, + onLoad: parsedAddress => { + expect(typeof(parsedAddress) !== "undefined").toBeTruthy(); + expect(parsedAddress.streetAddress).toBe("950 W Maude Ave."); + expect(parsedAddress.locality).toBe("Sunnyvale"); + expect(parsedAddress.region).toBe("CA"); + expect(parsedAddress.postalCode).toBe("94085"); + expect(parsedAddress.country).toBe("USA"); + expect(parsedAddress.countryCode).toBe("US"); + } + }); + }); + + // to verify NOV-111026 + test("ParseAddressAsyncSimple2", () => { + expect.assertions(7); + new Address("20 Main St.\nMyTown, NY 11530\nUSA", { + locale: 'en-US', + sync: false, + onLoad: parsedAddress => { + expect(typeof(parsedAddress) !== "undefined").toBeTruthy(); + expect(parsedAddress.streetAddress).toBe("20 Main St."); + expect(parsedAddress.locality).toBe("MyTown"); + expect(parsedAddress.region).toBe("NY"); + expect(parsedAddress.postalCode).toBe("11530"); + expect(parsedAddress.country).toBe("USA"); + expect(parsedAddress.countryCode).toBe("US"); + } + }); + }); + + test("ParseAddressAsyncSimple3", () => { + expect.assertions(7); + const pa = new Address("5-2-1 Ginza, Chuo-ku\nTokyo 170-3293\nJapan", { + locale: 'en-US', + sync: false, + onLoad: parsedAddress => { + expect(typeof(parsedAddress) !== "undefined").toBeTruthy(); + expect(parsedAddress.streetAddress).toBe("5-2-1 Ginza"); + expect(parsedAddress.locality).toBe("Chuo-ku"); + expect(parsedAddress.region).toBe("Tokyo"); + expect(parsedAddress.postalCode).toBe("170-3293"); + expect(parsedAddress.country).toBe("Japan"); + expect(parsedAddress.countryCode).toBe("JP"); + } + }); + }); + + test("ParseAddressAsyncMoreComplex", () => { + expect.assertions(7); + const pa = new Address("950 W 21st Ave, Apt 45\nNY, NY 10234", { + locale: 'en-US', + sync: false, + onLoad: parsedAddress => { + expect(typeof(parsedAddress) !== "undefined").toBeTruthy(); + expect(parsedAddress.streetAddress).toBe("950 W 21st Ave, Apt 45"); + expect(parsedAddress.locality).toBe("NY"); + expect(parsedAddress.region).toBe("NY"); + expect(parsedAddress.postalCode).toBe("10234"); + expect(typeof(parsedAddress.country) === "undefined").toBeTruthy(); + expect(parsedAddress.countryCode).toBe("US"); + } + }); + }); + + test("ParseAddressAsyncSpelledOutState", () => { + expect.assertions(7); + const pa = new Address("20 Main St.\nMyTown, Arizona 11530\nUSA", { + locale: 'en-US', + sync: false, + onLoad: parsedAddress => { + expect(typeof(parsedAddress) !== "undefined").toBeTruthy(); + expect(parsedAddress.streetAddress).toBe("20 Main St."); + expect(parsedAddress.locality).toBe("MyTown"); + expect(parsedAddress.region).toBe("Arizona"); + expect(parsedAddress.postalCode).toBe("11530"); + expect(parsedAddress.country).toBe("USA"); + expect(parsedAddress.countryCode).toBe("US"); + } + }); + }); + + test("ParseAddressAsyncSpelledOutStateWithSpaces", () => { + expect.assertions(7); + const pa = new Address("20 Main St.\nMyTown, New York 11530\nUSA", { + locale: 'en-US', + sync: false, + onLoad: parsedAddress => { + expect(typeof(parsedAddress) !== "undefined").toBeTruthy(); + expect(parsedAddress.streetAddress).toBe("20 Main St."); + expect(parsedAddress.locality).toBe("MyTown"); + expect(parsedAddress.region).toBe("New York"); + expect(parsedAddress.postalCode).toBe("11530"); + expect(parsedAddress.country).toBe("USA"); + expect(parsedAddress.countryCode).toBe("US"); + } + }); + }); + + test("ParseAddressAsyncUnknown", () => { + expect.assertions(7); + + const pa = new Address("123 Main Street, Pretoria 5678, South Africa", { + locale: 'en-US', + sync: false, + onLoad: parsedAddress => { + expect(typeof(parsedAddress) !== "undefined").toBeTruthy(); + expect(parsedAddress.streetAddress).toBe("123 Main Street"); + expect(parsedAddress.locality).toBe("Pretoria"); + expect(typeof(parsedAddress.region) === "undefined").toBeTruthy(); + expect(parsedAddress.postalCode).toBe("5678"); + expect(parsedAddress.country).toBe("South Africa"); + expect(parsedAddress.countryCode).toBe("ZA"); + } + }); + }); + + test("ParseAddressAsyncNonUS", () => { + expect.assertions(7); + const pa = new Address("Achterberglaan 23, 2345 GD Uithoorn, Netherlands", { + locale: 'en-US', + sync: false, + onLoad: parsedAddress => { + expect(typeof(parsedAddress) !== "undefined").toBeTruthy(); + expect(parsedAddress.streetAddress).toBe("Achterberglaan 23"); + expect(parsedAddress.locality).toBe("Uithoorn"); + expect(typeof(parsedAddress.region) === "undefined").toBeTruthy(); + expect(parsedAddress.postalCode).toBe("2345 GD"); + expect(parsedAddress.country).toBe("Netherlands"); + expect(parsedAddress.countryCode).toBe("NL"); + } + }); + }); + + test("FormatAddressAsyncUS", () => { + expect.assertions(2); + const pa = new Address({ + streetAddress: "1234 Any Street", + locality: "Anytown", + region: "CA", + postalCode: "94085", + country: "United States of America", + countryCode: "US" + }, {locale: 'en-US'}); + + const expected = "1234 Any Street\nAnytown CA 94085\nUnited States of America"; + const f = new AddressFmt({ + locale: 'en-US', + sync: false, + onLoad: formatter => { + expect(typeof(formatter) !== "undefined").toBeTruthy(); + expect(formatter.format(pa)).toBe(expected); + } + }); + }); + + test("FormatAddressAsyncUnknownLocaleQQ", () => { + expect.assertions(2); + const pa = new Address({ + streetAddress: "123 mcdonald ave, apt 234", + locality: "Sunnyvale", + region: "CA", + postalCode: "94086", + locale: 'en-QQ' + }); + + // should return the "root" information + const expected = "123 mcdonald ave, apt 234\nSunnyvale CA 94086"; + const f = new AddressFmt({ + locale: 'en-QQ', + style: 'nocountry', + sync: false, + onLoad: formatter => { + expect(typeof(formatter) !== "undefined").toBeTruthy(); + expect(formatter.format(pa)).toBe(expected); + } + }); + }); + + test("ParseAddressAsyncJPAsianNormal1", () => { + expect.assertions(6); + const pa = new Address("〒150-2345 東京都渋谷区本町2丁目4-7サニーマンション203", { + locale: 'ja-JP', + sync: false, + onLoad: parsedAddress => { + expect(typeof(parsedAddress) !== "undefined").toBeTruthy(); + expect(parsedAddress.streetAddress).toBe("本町2丁目4-7サニーマンション203"); + expect(parsedAddress.locality).toBe("渋谷区"); + expect(parsedAddress.region).toBe("東京都"); + expect(parsedAddress.postalCode).toBe("〒150-2345"); + expect(parsedAddress.countryCode).toBe("JP"); + } + }); + }); + + test("FormatAddressAsyncJPAsianNormal", () => { + expect.assertions(2); + const parsedAddress = new Address({ + streetAddress: "本町2丁目4-7サニーマンション203", + locality: "渋谷区", + region: "東京都", + postalCode: "〒150-2345", + countryCode: "JP", + format: "asian" + }, {locale: 'ja-JP'}); + + const expected = "〒150-2345\n東京都渋谷区本町2丁目4-7サニーマンション203"; + + const f = new AddressFmt({ + locale: 'ja-JP', + sync: false, + onLoad: formatter => { + expect(typeof(formatter) !== "undefined").toBeTruthy(); + expect(formatter.format(parsedAddress)).toBe(expected); + } + }); + }); + + + test("ParseAddressAsyncCNAsianNormal", () => { + expect.assertions(7); + const pa = new Address("中国北京市朝阳区建国路112号 中国惠普大厦100022", { + locale: 'zh-CN', + sync: false, + onLoad: parsedAddress => { + expect(typeof(parsedAddress) !== "undefined").toBeTruthy(); + expect(parsedAddress.streetAddress).toBe("建国路112号 中国惠普大厦"); + expect(parsedAddress.locality).toBe("朝阳区"); + expect(parsedAddress.region).toBe("北京市"); + expect(parsedAddress.postalCode).toBe("100022"); + expect(parsedAddress.country).toBe("中国"); + expect(parsedAddress.countryCode).toBe("CN"); + } + }); + }); + + test("ParseAddressAsyncDENormal", () => { + expect.assertions(7); + const pa = new Address("Herrenberger Straße 140, 71034 Böblingen, Deutschland", { + locale: 'de-DE', + sync: false, + onLoad: parsedAddress => { + expect(typeof(parsedAddress) !== "undefined").toBeTruthy(); + expect(parsedAddress.streetAddress).toBe("Herrenberger Straße 140"); + expect(parsedAddress.locality).toBe("Böblingen"); + expect(typeof(parsedAddress.region) === "undefined").toBeTruthy(); + expect(parsedAddress.postalCode).toBe("71034"); + expect(parsedAddress.country).toBe("Deutschland"); + expect(parsedAddress.countryCode).toBe("DE"); + } + }); + }); + + test("ParseAddressAsyncTHNormal", () => { + expect.assertions(7); + const pa = new Address("49 ซอยร่วมฤดี, ถนนเพลินจิต, ลุมพินี\nเขตปทุมวัน กรุงเทพฯ 10330\nประเทศไทย", { + locale: 'th-Th', + sync: false, + onLoad: parsedAddress => { + expect(typeof(parsedAddress) !== "undefined").toBeTruthy(); + expect(parsedAddress.streetAddress).toBe("49 ซอยร่วมฤดี, ถนนเพลินจิต, ลุมพินี"); + expect(parsedAddress.locality).toBe("เขตปทุมวัน"); + expect(parsedAddress.region).toBe("กรุงเทพฯ"); + expect(parsedAddress.postalCode).toBe("10330"); + expect(parsedAddress.country).toBe("ประเทศไทย"); + expect(parsedAddress.countryCode).toBe("TH"); + } + }); + }); + + test("ParseAddressAsyncRUNormal", () => { + expect.assertions(7); + const pa = new Address("Петров Иван Сергеевич ул. Лесная D. 5 поз. Лесной\nАЛЕКСЕЕВСКИЙ R-N\nВоронежская область\nРоссия\n247112", { + locale: 'ru-RU', + sync: false, + onLoad: parsedAddress => { + expect(typeof(parsedAddress) !== "undefined").toBeTruthy(); + expect(parsedAddress.streetAddress).toBe("Петров Иван Сергеевич ул. Лесная D. 5 поз. Лесной"); + expect(parsedAddress.locality).toBe("АЛЕКСЕЕВСКИЙ R-N"); + expect(parsedAddress.region).toBe("Воронежская область"); + expect(parsedAddress.postalCode).toBe("247112"); + expect(parsedAddress.country).toBe("Россия"); + expect(parsedAddress.countryCode).toBe("RU"); + } + }); + }); + + test("ParseAddressAsyncSANormalNative", () => { + expect.assertions(7); + const pa = new Address("السيد عبد الله ناصر\nمكة المكرمة ٢١۴۵۴\nالمملكة العربية السعودية", { + locale: 'ar-SA', + sync: false, + onLoad: parsedAddress => { + expect(typeof(parsedAddress) !== "undefined").toBeTruthy(); + expect(parsedAddress.streetAddress).toBe("السيد عبد الله ناصر"); + expect(parsedAddress.locality).toBe("مكة المكرمة"); + expect(typeof(parsedAddress.region) === "undefined").toBeTruthy(); + expect(parsedAddress.postalCode).toBe("٢١۴۵۴"); + expect(parsedAddress.country).toBe("المملكة العربية السعودية"); + expect(parsedAddress.countryCode).toBe("SA"); + } + }); + }); + + test("ParseAddressAsyncINHINormal", () => { + expect.assertions(7); + const pa = new Address("१२५/१, एजी टावर्स. ३ तल, पार्क स्ट्रीट. सर्कस एवेन्यू\nकोलकाता\nपश्चिम बंगाल\n७०००१७\nभारत", { + locale: 'hi-IN', + sync: false, + onLoad: parsedAddress => { + expect(typeof(parsedAddress) !== "undefined").toBeTruthy(); + expect(parsedAddress.streetAddress).toBe("१२५/१, एजी टावर्स. ३ तल, पार्क स्ट्रीट. सर्कस एवेन्यू"); + expect(parsedAddress.locality).toBe("कोलकाता"); + expect(parsedAddress.region).toBe("पश्चिम बंगाल"); + expect(parsedAddress.postalCode).toBe("७०००१७"); + expect(parsedAddress.country).toBe("भारत"); + expect(parsedAddress.countryCode).toBe("IN"); + } + }); + }); + + test("ParseAddressAsyncINGUNoZip", () => { + expect.assertions(7); + const pa = new Address("125/1, એજી ટાવર્સ. 3 જો માળ, પાર્ક સ્ટ્રીટ. સર્કસ એવન્યુ\nકોલકાતા\nપશ્ચિમ બંગાળ\nભારત", { + locale: 'gu-IN', + sync: false, + onLoad: parsedAddress => { + expect(typeof(parsedAddress) !== "undefined").toBeTruthy(); + expect(parsedAddress.streetAddress).toBe("125/1, એજી ટાવર્સ. 3 જો માળ, પાર્ક સ્ટ્રીટ. સર્કસ એવન્યુ"); + expect(parsedAddress.locality).toBe("કોલકાતા"); + expect(parsedAddress.region).toBe("પશ્ચિમ બંગાળ"); + expect(parsedAddress.country).toBe("ભારત"); + expect(parsedAddress.countryCode).toBe("IN"); + expect(typeof(parsedAddress.postalCode) === "undefined").toBeTruthy(); + } + }); + }); + + test("AddressFmtGetFormatInfoUSRightConstraints", () => { + expect.assertions(19); + new AddressFmt({ + locale: 'en-US', + sync: false, + onLoad: formatter => { + formatter.getFormatInfo(undefined, false, info => { + expect(info).toBeTruthy(); + + expect(info[1][2].component).toBe("postalCode"); + expect(info[1][2].constraint).toBe("[0-9]{5}(-[0-9]{4})?"); + + expect(info[1][1].component).toBe("region"); + expect(info[1][1].constraint).toBeTruthy(); + let r = searchRegions(info[1][1].constraint, "AZ"); + expect(r.code).toBe("AZ"); + expect(r.name).toBe("Arizona"); + r = searchRegions(info[1][1].constraint, "MS"); + expect(r.code).toBe("MS"); + expect(r.name).toBe("Mississippi"); + r = searchRegions(info[1][1].constraint, "NY"); + expect(r.code).toBe("NY"); + expect(r.name).toBe("New York"); + + expect(info[2][0].component).toBe("country"); + expect(info[2][0].constraint).toBeTruthy(); + r = searchRegions(info[2][0].constraint, "JP"); + expect(r.code).toBe("JP"); + expect(r.name).toBe("Japan"); + r = searchRegions(info[2][0].constraint, "CR"); + expect(r.code).toBe("CR"); + expect(r.name).toBe("Costa Rica"); + r = searchRegions(info[2][0].constraint, "ZA"); + expect(r.code).toBe("ZA"); + expect(r.name).toBe("South Africa"); + }); + } + }); + }); +}); diff --git a/test/addresspromise.test.js b/test/addresspromise.test.js new file mode 100644 index 0000000..47773ab --- /dev/null +++ b/test/addresspromise.test.js @@ -0,0 +1,374 @@ +/* + * addressasync.test.js - test the address parsing and formatting routines asynchronously + * + * Copyright © 2018, 2022, 2024 JEDLSoft + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import AddressFmt from "../src/AddressFmt.js"; +import Address from "../src/Address.js"; + +function searchRegions(array, regionCode) { + return array.find(region => { + return region.code === regionCode; + }); +} + +describe("testaddresspromise", () => { + test("PromiseParseAddressAsyncSimple", () => { + expect.assertions(7); + return Address.create("950 W Maude Ave.\nSunnyvale, CA 94085\nUSA", { + locale: 'en-US' + }).then(parsedAddress => { + expect(typeof(parsedAddress) !== "undefined").toBeTruthy(); + expect(parsedAddress.streetAddress).toBe("950 W Maude Ave."); + expect(parsedAddress.locality).toBe("Sunnyvale"); + expect(parsedAddress.region).toBe("CA"); + expect(parsedAddress.postalCode).toBe("94085"); + expect(parsedAddress.country).toBe("USA"); + expect(parsedAddress.countryCode).toBe("US"); + }); + }); + + // to verify NOV-111026 + test("PromiseParseAddressAsyncSimple2", () => { + expect.assertions(7); + return Address.create("20 Main St.\nMyTown, NY 11530\nUSA", { + locale: 'en-US' + }).then(parsedAddress => { + expect(typeof(parsedAddress) !== "undefined").toBeTruthy(); + expect(parsedAddress.streetAddress).toBe("20 Main St."); + expect(parsedAddress.locality).toBe("MyTown"); + expect(parsedAddress.region).toBe("NY"); + expect(parsedAddress.postalCode).toBe("11530"); + expect(parsedAddress.country).toBe("USA"); + expect(parsedAddress.countryCode).toBe("US"); + }); + }); + + test("PromiseParseAddressAsyncSimple3", () => { + expect.assertions(7); + return Address.create("5-2-1 Ginza, Chuo-ku\nTokyo 170-3293\nJapan", { + locale: 'en-US' + }).then(parsedAddress => { + expect(typeof(parsedAddress) !== "undefined").toBeTruthy(); + expect(parsedAddress.streetAddress).toBe("5-2-1 Ginza"); + expect(parsedAddress.locality).toBe("Chuo-ku"); + expect(parsedAddress.region).toBe("Tokyo"); + expect(parsedAddress.postalCode).toBe("170-3293"); + expect(parsedAddress.country).toBe("Japan"); + expect(parsedAddress.countryCode).toBe("JP"); + }); + }); + + test("PromiseParseAddressAsyncMoreComplex", () => { + expect.assertions(7); + return Address.create("950 W 21st Ave, Apt 45\nNY, NY 10234", { + locale: 'en-US' + }).then(parsedAddress => { + expect(typeof(parsedAddress) !== "undefined").toBeTruthy(); + expect(parsedAddress.streetAddress).toBe("950 W 21st Ave, Apt 45"); + expect(parsedAddress.locality).toBe("NY"); + expect(parsedAddress.region).toBe("NY"); + expect(parsedAddress.postalCode).toBe("10234"); + expect(typeof(parsedAddress.country) === "undefined").toBeTruthy(); + expect(parsedAddress.countryCode).toBe("US"); + }); + }); + + test("PromiseParseAddressAsyncSpelledOutState", () => { + expect.assertions(7); + return Address.create("20 Main St.\nMyTown, Arizona 11530\nUSA", { + locale: 'en-US' + }).then(parsedAddress => { + expect(typeof(parsedAddress) !== "undefined").toBeTruthy(); + expect(parsedAddress.streetAddress).toBe("20 Main St."); + expect(parsedAddress.locality).toBe("MyTown"); + expect(parsedAddress.region).toBe("Arizona"); + expect(parsedAddress.postalCode).toBe("11530"); + expect(parsedAddress.country).toBe("USA"); + expect(parsedAddress.countryCode).toBe("US"); + }); + }); + + test("PromiseParseAddressAsyncSpelledOutStateWithSpaces", () => { + expect.assertions(7); + return Address.create("20 Main St.\nMyTown, New York 11530\nUSA", { + locale: 'en-US' + }).then(parsedAddress => { + expect(typeof(parsedAddress) !== "undefined").toBeTruthy(); + expect(parsedAddress.streetAddress).toBe("20 Main St."); + expect(parsedAddress.locality).toBe("MyTown"); + expect(parsedAddress.region).toBe("New York"); + expect(parsedAddress.postalCode).toBe("11530"); + expect(parsedAddress.country).toBe("USA"); + expect(parsedAddress.countryCode).toBe("US"); + }); + }); + + test("PromiseParseAddressAsyncUnknown", () => { + expect.assertions(7); + + return Address.create("123 Main Street, Pretoria 5678, South Africa", { + locale: 'en-US' + }).then(parsedAddress => { + expect(typeof(parsedAddress) !== "undefined").toBeTruthy(); + expect(parsedAddress.streetAddress).toBe("123 Main Street"); + expect(parsedAddress.locality).toBe("Pretoria"); + expect(typeof(parsedAddress.region) === "undefined").toBeTruthy(); + expect(parsedAddress.postalCode).toBe("5678"); + expect(parsedAddress.country).toBe("South Africa"); + expect(parsedAddress.countryCode).toBe("ZA"); + }); + }); + + test("PromiseParseAddressAsyncNonUS", () => { + expect.assertions(7); + return Address.create("Achterberglaan 23, 2345 GD Uithoorn, Netherlands", { + locale: 'en-US' + }).then(parsedAddress => { + expect(typeof(parsedAddress) !== "undefined").toBeTruthy(); + expect(parsedAddress.streetAddress).toBe("Achterberglaan 23"); + expect(parsedAddress.locality).toBe("Uithoorn"); + expect(typeof(parsedAddress.region) === "undefined").toBeTruthy(); + expect(parsedAddress.postalCode).toBe("2345 GD"); + expect(parsedAddress.country).toBe("Netherlands"); + expect(parsedAddress.countryCode).toBe("NL"); + }); + }); + + test("PromiseFormatAddressAsyncUS", () => { + expect.assertions(2); + const pa = new Address({ + streetAddress: "1234 Any Street", + locality: "Anytown", + region: "CA", + postalCode: "94085", + country: "United States of America", + countryCode: "US" + }, {locale: 'en-US'}); + + const expected = "1234 Any Street\nAnytown CA 94085\nUnited States of America"; + return AddressFmt.create({ + locale: 'en-US' + }).then(formatter => { + expect(typeof(formatter) !== "undefined").toBeTruthy(); + expect(formatter.format(pa)).toBe(expected); + }); + }); + + test("PromiseFormatAddressAsyncUnknownLocaleQQ", () => { + expect.assertions(2); + const pa = new Address({ + streetAddress: "123 mcdonald ave, apt 234", + locality: "Sunnyvale", + region: "CA", + postalCode: "94086", + locale: 'en-QQ' + }); + + // should return the "root" information + const expected = "123 mcdonald ave, apt 234\nSunnyvale CA 94086"; + return AddressFmt.create({ + locale: 'en-QQ', + style: 'nocountry' + }).then(formatter => { + expect(typeof(formatter) !== "undefined").toBeTruthy(); + expect(formatter.format(pa)).toBe(expected); + }); + }); + + test("PromiseParseAddressAsyncJPAsianNormal1", () => { + expect.assertions(6); + return Address.create("〒150-2345 東京都渋谷区本町2丁目4-7サニーマンション203", { + locale: 'ja-JP' + }).then(parsedAddress => { + expect(typeof(parsedAddress) !== "undefined").toBeTruthy(); + expect(parsedAddress.streetAddress).toBe("本町2丁目4-7サニーマンション203"); + expect(parsedAddress.locality).toBe("渋谷区"); + expect(parsedAddress.region).toBe("東京都"); + expect(parsedAddress.postalCode).toBe("〒150-2345"); + expect(parsedAddress.countryCode).toBe("JP"); + }); + }); + + test("PromiseFormatAddressAsyncJPAsianNormal", () => { + expect.assertions(2); + const parsedAddress = new Address({ + streetAddress: "本町2丁目4-7サニーマンション203", + locality: "渋谷区", + region: "東京都", + postalCode: "〒150-2345", + countryCode: "JP", + format: "asian" + }, {locale: 'ja-JP'}); + + const expected = "〒150-2345\n東京都渋谷区本町2丁目4-7サニーマンション203"; + + return AddressFmt.create({ + locale: 'ja-JP' + }).then(formatter => { + expect(typeof(formatter) !== "undefined").toBeTruthy(); + expect(formatter.format(parsedAddress)).toBe(expected); + }); + }); + + + test("PromiseParseAddressAsyncCNAsianNormal", () => { + expect.assertions(7); + return Address.create("中国北京市朝阳区建国路112号 中国惠普大厦100022", { + locale: 'zh-CN' + }).then(parsedAddress => { + expect(typeof(parsedAddress) !== "undefined").toBeTruthy(); + expect(parsedAddress.streetAddress).toBe("建国路112号 中国惠普大厦"); + expect(parsedAddress.locality).toBe("朝阳区"); + expect(parsedAddress.region).toBe("北京市"); + expect(parsedAddress.postalCode).toBe("100022"); + expect(parsedAddress.country).toBe("中国"); + expect(parsedAddress.countryCode).toBe("CN"); + }); + }); + + test("PromiseParseAddressAsyncDENormal", () => { + expect.assertions(7); + return Address.create("Herrenberger Straße 140, 71034 Böblingen, Deutschland", { + locale: 'de-DE' + }).then(parsedAddress => { + expect(typeof(parsedAddress) !== "undefined").toBeTruthy(); + expect(parsedAddress.streetAddress).toBe("Herrenberger Straße 140"); + expect(parsedAddress.locality).toBe("Böblingen"); + expect(typeof(parsedAddress.region) === "undefined").toBeTruthy(); + expect(parsedAddress.postalCode).toBe("71034"); + expect(parsedAddress.country).toBe("Deutschland"); + expect(parsedAddress.countryCode).toBe("DE"); + }); + }); + + test("PromiseParseAddressAsyncTHNormal", () => { + expect.assertions(7); + return Address.create("49 ซอยร่วมฤดี, ถนนเพลินจิต, ลุมพินี\nเขตปทุมวัน กรุงเทพฯ 10330\nประเทศไทย", { + locale: 'th-Th' + }).then(parsedAddress => { + expect(typeof(parsedAddress) !== "undefined").toBeTruthy(); + expect(parsedAddress.streetAddress).toBe("49 ซอยร่วมฤดี, ถนนเพลินจิต, ลุมพินี"); + expect(parsedAddress.locality).toBe("เขตปทุมวัน"); + expect(parsedAddress.region).toBe("กรุงเทพฯ"); + expect(parsedAddress.postalCode).toBe("10330"); + expect(parsedAddress.country).toBe("ประเทศไทย"); + expect(parsedAddress.countryCode).toBe("TH"); + }); + }); + + test("PromiseParseAddressAsyncRUNormal", () => { + expect.assertions(7); + return Address.create("Петров Иван Сергеевич ул. Лесная D. 5 поз. Лесной\nАЛЕКСЕЕВСКИЙ R-N\nВоронежская область\nРоссия\n247112", { + locale: 'ru-RU' + }).then(parsedAddress => { + expect(typeof(parsedAddress) !== "undefined").toBeTruthy(); + expect(parsedAddress.streetAddress).toBe("Петров Иван Сергеевич ул. Лесная D. 5 поз. Лесной"); + expect(parsedAddress.locality).toBe("АЛЕКСЕЕВСКИЙ R-N"); + expect(parsedAddress.region).toBe("Воронежская область"); + expect(parsedAddress.postalCode).toBe("247112"); + expect(parsedAddress.country).toBe("Россия"); + expect(parsedAddress.countryCode).toBe("RU"); + }); + }); + + test("PromiseParseAddressAsyncSANormalNative", () => { + expect.assertions(7); + return Address.create("السيد عبد الله ناصر\nمكة المكرمة ٢١۴۵۴\nالمملكة العربية السعودية", { + locale: 'ar-SA' + }).then(parsedAddress => { + expect(typeof(parsedAddress) !== "undefined").toBeTruthy(); + expect(parsedAddress.streetAddress).toBe("السيد عبد الله ناصر"); + expect(parsedAddress.locality).toBe("مكة المكرمة"); + expect(typeof(parsedAddress.region) === "undefined").toBeTruthy(); + expect(parsedAddress.postalCode).toBe("٢١۴۵۴"); + expect(parsedAddress.country).toBe("المملكة العربية السعودية"); + expect(parsedAddress.countryCode).toBe("SA"); + }); + }); + + test("PromiseParseAddressAsyncINHINormal", () => { + expect.assertions(7); + return Address.create("१२५/१, एजी टावर्स. ३ तल, पार्क स्ट्रीट. सर्कस एवेन्यू\nकोलकाता\nपश्चिम बंगाल\n७०००१७\nभारत", { + locale: 'hi-IN' + }).then(parsedAddress => { + expect(typeof(parsedAddress) !== "undefined").toBeTruthy(); + expect(parsedAddress.streetAddress).toBe("१२५/१, एजी टावर्स. ३ तल, पार्क स्ट्रीट. सर्कस एवेन्यू"); + expect(parsedAddress.locality).toBe("कोलकाता"); + expect(parsedAddress.region).toBe("पश्चिम बंगाल"); + expect(parsedAddress.postalCode).toBe("७०००१७"); + expect(parsedAddress.country).toBe("भारत"); + expect(parsedAddress.countryCode).toBe("IN"); + }); + }); + + test("PromiseParseAddressAsyncINGUNoZip", () => { + expect.assertions(7); + return Address.create("125/1, એજી ટાવર્સ. 3 જો માળ, પાર્ક સ્ટ્રીટ. સર્કસ એવન્યુ\nકોલકાતા\nપશ્ચિમ બંગાળ\nભારત", { + locale: 'gu-IN' + }).then(parsedAddress => { + expect(typeof(parsedAddress) !== "undefined").toBeTruthy(); + expect(parsedAddress.streetAddress).toBe("125/1, એજી ટાવર્સ. 3 જો માળ, પાર્ક સ્ટ્રીટ. સર્કસ એવન્યુ"); + expect(parsedAddress.locality).toBe("કોલકાતા"); + expect(parsedAddress.region).toBe("પશ્ચિમ બંગાળ"); + expect(parsedAddress.country).toBe("ભારત"); + expect(parsedAddress.countryCode).toBe("IN"); + expect(typeof(parsedAddress.postalCode) === "undefined").toBeTruthy(); + }); + }); + + test("PromiseAddressFmtGetFormatInfoUSRightConstraints", () => { + expect.assertions(22); + return AddressFmt.create({ + locale: 'en-US' + }).then(formatter => { + return formatter.getFormatInfo("de-DE", false); // get labels in German + }).then(info => { + expect(info).toBeTruthy(); + + expect(info[1][2].component).toBe("postalCode"); + expect(info[1][2].label).toBe("Postleitzahl"); + expect(info[1][2].constraint).toBe("[0-9]{5}(-[0-9]{4})?"); + + expect(info[1][1].component).toBe("region"); + expect(info[1][1].label).toBe("Bundesland"); + expect(info[1][1].constraint).toBeTruthy(); + let r = searchRegions(info[1][1].constraint, "AZ"); + expect(r.code).toBe("AZ"); + expect(r.name).toBe("Arizona"); + r = searchRegions(info[1][1].constraint, "MS"); + expect(r.code).toBe("MS"); + expect(r.name).toBe("Mississippi"); + r = searchRegions(info[1][1].constraint, "NY"); + expect(r.code).toBe("NY"); + expect(r.name).toBe("New York"); + + expect(info[2][0].component).toBe("country"); + expect(info[2][0].label).toBe("Land"); + expect(info[2][0].constraint).toBeTruthy(); + r = searchRegions(info[2][0].constraint, "JP"); + expect(r.code).toBe("JP"); + expect(r.name).toBe("Japan"); + r = searchRegions(info[2][0].constraint, "CR"); + expect(r.code).toBe("CR"); + expect(r.name).toBe("Costa Rica"); + r = searchRegions(info[2][0].constraint, "ZA"); + expect(r.code).toBe("ZA"); + expect(r.name).toBe("Südafrika"); + }); + }); +}); diff --git a/test/testalphaindexasync.js b/test/alphaindexasync.test.js similarity index 72% rename from test/testalphaindexasync.js rename to test/alphaindexasync.test.js index d73a798..4e919fa 100644 --- a/test/testalphaindexasync.js +++ b/test/alphaindexasync.test.js @@ -1,7 +1,7 @@ /* - * testalphaindexasync.js - test the Alphabetic Index class asynchronously + * alphaindexasync.test.js - test the Alphabetic Index class asynchronously * - * Copyright © 2018, 2022 JEDLSoft + * Copyright © 2018, 2022, 2024 JEDLSoft * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,98 +19,90 @@ import AlphabeticIndex from "../src/AlphabeticIndex.js"; -export const testalphaindexasync = { - testAlphaIndexAsyncConstructor: function(test) { - test.expect(1); +describe("testalphaindexasync", () => { + test("AlphaIndexAsyncConstructor", () => { + expect.assertions(1); new AlphabeticIndex({ sync: false, - onLoad: function(ai) { - test.ok(ai); - test.done(); + onLoad: ai => { + expect(ai).toBeTruthy(); } }); - }, + }); - testAlphaIndexAsyncConstructorWithParams: function(test) { - test.expect(1); + test("AlphaIndexAsyncConstructorWithParams", () => { + expect.assertions(1); new AlphabeticIndex({ locale: "en-US", sync: false, - onLoad: function(ai) { - test.ok(ai); - test.done(); + onLoad: ai => { + expect(ai).toBeTruthy(); } }); - }, + }); - testAlphaIndexAsyncgetDefaultIndexStyle: function(test) { - test.expect(2); + test("AlphaIndexAsyncgetDefaultIndexStyle", () => { + expect.assertions(2); new AlphabeticIndex({ locale: "en-US", sync: false, - onLoad: function(ai) { - test.ok(ai); - test.equal("latin", ai.getIndexStyle()); - test.done(); + onLoad: ai => { + expect(ai).toBeTruthy(); + expect("latin").toBe(ai.getIndexStyle()); } }); - }, + }); - testAlphaIndexAsyncConstructorWithUnknownLocale: function(test) { - test.expect(1); + test("AlphaIndexAsyncConstructorWithUnknownLocale", () => { + expect.assertions(1); new AlphabeticIndex({ locale: "qq-QQ", sync: false, - onLoad: function(ai) { - test.ok(ai); - test.done(); + onLoad: ai => { + expect(ai).toBeTruthy(); } }); - }, + }); - testAlphaIndexAsyncENUSGetBucket: function(test) { - test.expect(3); + test("AlphaIndexAsyncENUSGetBucket", () => { + expect.assertions(3); new AlphabeticIndex({ locale: "en-US", sync: false, - onLoad: function(ai) { - test.ok(ai); - test.equal("B", ai.getBucket("belarus")); - test.equal("B", ai.getBucket("Belarus")); - - test.done(); + onLoad: ai => { + expect(ai).toBeTruthy(); + expect("B").toBe(ai.getBucket("belarus")); + expect("B").toBe(ai.getBucket("Belarus")); } }); - }, + }); - testAlphaIndexAsyncENUSGetBucket2: function(test) { - test.expect(3); + test("AlphaIndexAsyncENUSGetBucket2", () => { + expect.assertions(3); new AlphabeticIndex({ locale: "en-US", sync: false, - onLoad: function(ai) { - test.ok(ai); - - test.equal("E", ai.getBucket("Élan")); - test.equal("E", ai.getBucket("ëieasdf")); + onLoad: ai => { + expect(ai).toBeTruthy(); - test.done(); + expect("E").toBe(ai.getBucket("Élan")); + expect("E").toBe(ai.getBucket("ëieasdf")); } }); - }, + }); - testAlphaIndexAsyncENUSGetAllBuckets: function(test) { - test.expect(2); + test("AlphaIndexAsyncENUSGetAllBuckets", () => { + expect.assertions(2); new AlphabeticIndex({ locale: "en-US", sync: false, - onLoad: function(ai) { - test.ok(ai); + onLoad: ai => { + expect(ai).toBeTruthy(); - var items = [ + const items = [ "omicron", "beta", "epsilon", @@ -127,11 +119,11 @@ export const testalphaindexasync = { "gamma" ]; - items.forEach(function(item) { + items.forEach(item => { ai.addElement(item); }); - var expected = [ + const expected = [ { label: "A", elements: ["alpha"] @@ -174,22 +166,21 @@ export const testalphaindexasync = { } ] - test.deepEqual(ai.getAllBuckets(), expected); - test.done(); + expect(ai.getAllBuckets()).toStrictEqual(expected); } }); - }, + }); - testAlphaIndexAsyncENUSGetBucketLabels: function(test) { - test.expect(2); + test("AlphaIndexAsyncENUSGetBucketLabels", () => { + expect.assertions(2); new AlphabeticIndex({ locale: "en-US", sync: false, - onLoad: function(ai) { - test.ok(ai); + onLoad: ai => { + expect(ai).toBeTruthy(); - var items = [ + const items = [ "omicron", "beta", "echo", @@ -212,11 +203,11 @@ export const testalphaindexasync = { "gamma" ]; - items.forEach(function(item) { + items.forEach(item => { ai.addElement(item); }); - var expected = [ + const expected = [ "A", "B", "C", @@ -234,23 +225,22 @@ export const testalphaindexasync = { "Z" ]; - test.deepEqual(ai.getBucketLabels(), expected); - test.done(); + expect(ai.getBucketLabels()).toStrictEqual(expected); } }); - }, + }); - testAlphaIndexAsyncUnknowLocaleActsLikeEnglish: function(test) { - test.expect(2); + test("AlphaIndexAsyncUnknowLocaleActsLikeEnglish", () => { + expect.assertions(2); new AlphabeticIndex({ locale: "qq-QQ", sync: false, - onLoad: function(ai) { - test.ok(ai); + onLoad: ai => { + expect(ai).toBeTruthy(); - var items = [ + const items = [ "omicron", "beta", "echo", @@ -268,11 +258,11 @@ export const testalphaindexasync = { "ürgen" ]; - items.forEach(function(item) { + items.forEach(item => { ai.addElement(item); }); - var expected = [ + const expected = [ { label: "A", elements: ["alpha"] @@ -319,72 +309,67 @@ export const testalphaindexasync = { } ] - test.deepEqual(ai.getAllBuckets(), expected); - test.done(); + expect(ai.getAllBuckets()).toStrictEqual(expected); } }); - }, + }); - testAlphaIndexAsyncConstructorAsync_deDE: function(test) { - test.expect(1); - var ai = new AlphabeticIndex({ + test("AlphaIndexAsyncConstructorAsync_deDE", () => { + expect.assertions(1); + const ai = new AlphabeticIndex({ locale: "de-DE", sync: false, - onLoad: function(ai) { - test.ok(ai); - test.done(); + onLoad: ai => { + expect(ai).toBeTruthy(); } }); - }, + }); - testAlphaIndexAsyncGetBucket_deDE2: function(test) { - test.expect(7); + test("AlphaIndexAsyncGetBucket_deDE2", () => { + expect.assertions(7); - var ai = new AlphabeticIndex({ + const ai = new AlphabeticIndex({ locale: "de-DE", sync: false, - onLoad: function(ai) { - test.ok(ai); - - test.equal("U", ai.getBucket("über")); - test.equal("A", ai.getBucket("änderen")); - test.equal("O", ai.getBucket("öffenen")); + onLoad: ai => { + expect(ai).toBeTruthy(); - test.equal("U", ai.getBucket("Über")); - test.equal("A", ai.getBucket("Änderen")); - test.equal("O", ai.getBucket("Öffenen")); + expect("U").toBe(ai.getBucket("über")); + expect("A").toBe(ai.getBucket("änderen")); + expect("O").toBe(ai.getBucket("öffenen")); - test.done(); + expect("U").toBe(ai.getBucket("Über")); + expect("A").toBe(ai.getBucket("Änderen")); + expect("O").toBe(ai.getBucket("Öffenen")); } }); - }, + }); - testAlphaIndexAsyncGetBucketAccentInsensitiveSZ_deDE: function(test) { - test.expect(2); + test("AlphaIndexAsyncGetBucketAccentInsensitiveSZ_deDE", () => { + expect.assertions(2); - var ai = new AlphabeticIndex({ + const ai = new AlphabeticIndex({ locale: "de-DE", sync: false, - onLoad: function(ai) { - test.ok(ai); + onLoad: ai => { + expect(ai).toBeTruthy(); // no words start with sz, but handle this just in case - test.equal("S", ai.getBucket("ß")); - test.done(); + expect("S").toBe(ai.getBucket("ß")); } }); - }, + }); - testAlphaIndexAsyncDEDEPhonebookStyle: function(test) { - test.expect(2); + test("AlphaIndexAsyncDEDEPhonebookStyle", () => { + expect.assertions(2); - var ai = new AlphabeticIndex({ + const ai = new AlphabeticIndex({ locale: "de-DE", style: "phonebook", sync: false, - onLoad: function(ai) { - test.ok(ai); + onLoad: ai => { + expect(ai).toBeTruthy(); - var items = [ + const items = [ "Jürgen", "Georg", "Matthias", @@ -405,11 +390,11 @@ export const testalphaindexasync = { "Judrich" ]; - items.forEach(function(item) { + items.forEach(item => { ai.addElement(item); }); - var expected = [ + const expected = [ { label: "F", elements: ["Fritz"] @@ -440,24 +425,23 @@ export const testalphaindexasync = { } ]; - test.deepEqual(ai.getAllBuckets(), expected); - test.done(); + expect(ai.getAllBuckets()).toStrictEqual(expected); } }); - }, + }); - testAlphaIndexAsyncDEDEDictionaryStyle: function(test) { - test.expect(2); + test("AlphaIndexAsyncDEDEDictionaryStyle", () => { + expect.assertions(2); - var ai = new AlphabeticIndex({ + const ai = new AlphabeticIndex({ locale: "de-DE", style: "dictionary", sync: false, - onLoad: function(ai) { - test.ok(ai); + onLoad: ai => { + expect(ai).toBeTruthy(); - var items = [ + const items = [ "Jürgen", "Georg", "Matthias", @@ -478,11 +462,11 @@ export const testalphaindexasync = { "Judrich" ]; - items.forEach(function(item) { + items.forEach(item => { ai.addElement(item); }); - var expected = [ + const expected = [ { label: "F", elements: ["Fritz"] @@ -513,34 +497,32 @@ export const testalphaindexasync = { } ]; - test.deepEqual(ai.getAllBuckets(), expected); - test.done(); + expect(ai.getAllBuckets()).toStrictEqual(expected); } }); - }, + }); - testAlphaIndexAsyncConstructorAsync: function(test) { - test.expect(1); - var ai = new AlphabeticIndex({ + test("AlphaIndexAsyncConstructorAsync", () => { + expect.assertions(1); + const ai = new AlphabeticIndex({ locale: "ko-KR", sync: false, - onLoad: function(ai) { - test.ok(ai); - test.done(); + onLoad: ai => { + expect(ai).toBeTruthy(); } }); - }, + }); - testAlphaIndexAsyncKOKRMixedScriptTest1: function(test) { - test.expect(2); + test("AlphaIndexAsyncKOKRMixedScriptTest1", () => { + expect.assertions(2); - var ai = new AlphabeticIndex({ + const ai = new AlphabeticIndex({ locale: "ko-KR", sync: false, - onLoad: function(ai) { - test.ok(ai); + onLoad: ai => { + expect(ai).toBeTruthy(); - var items = [ + const items = [ "Apple", "Banana", "김철수", @@ -565,11 +547,11 @@ export const testalphaindexasync = { "박세진" ]; - items.forEach(function(item) { + items.forEach(item => { ai.addElement(item); }); - var expected = [ + const expected = [ { label: "ㄱ", elements: ["강성진","김영희","김철수"] @@ -612,23 +594,22 @@ export const testalphaindexasync = { } ] - test.deepEqual(ai.getAllBuckets(), expected); - test.done(); + expect(ai.getAllBuckets()).toStrictEqual(expected); } }); - }, + }); - testAlphaIndexAsyncKOKRMixedScriptTest2: function(test) { - test.expect(2); + test("AlphaIndexAsyncKOKRMixedScriptTest2", () => { + expect.assertions(2); - var ai = new AlphabeticIndex({ + const ai = new AlphabeticIndex({ locale: "ko-KR", sync: false, - onLoad: function(ai) { - test.ok(ai); + onLoad: ai => { + expect(ai).toBeTruthy(); - var items = [ + const items = [ "Apple", "Banana", "김철수", @@ -646,11 +627,11 @@ export const testalphaindexasync = { "박세진" ]; - items.forEach(function(item) { + items.forEach(item => { ai.addElement(item); }); - var expected = [ + const expected = [ { label: "ㄱ", elements: ["강성진", "김철수"] @@ -697,9 +678,8 @@ export const testalphaindexasync = { } ]; - test.deepEqual(ai.getAllBuckets(), expected); - test.done(); + expect(ai.getAllBuckets()).toStrictEqual(expected); } }); - } -}; \ No newline at end of file + }); +}); \ No newline at end of file diff --git a/test/testalphaindexpromise.js b/test/alphaindexpromise.test.js similarity index 69% rename from test/testalphaindexpromise.js rename to test/alphaindexpromise.test.js index a1e22a6..89dff1d 100644 --- a/test/testalphaindexpromise.js +++ b/test/alphaindexpromise.test.js @@ -1,7 +1,7 @@ /* - * testalphaindexpromise.js - test the Alphabetic Index class asynchronously + * alphaindexpromise.test.js - test the Alphabetic Index class asynchronously * - * Copyright © 2018, 2022 JEDLSoft + * Copyright © 2018, 2022, 2024 JEDLSoft * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,92 +19,84 @@ import AlphabeticIndex from "../src/AlphabeticIndex.js"; -export const testalphaindexpromise = { - testAlphaIndexAsyncConstructor: function(test) { - test.expect(1); - AlphabeticIndex.create({ +describe("testalphaindexpromise", () => { + test("AlphaIndexAsyncConstructor", () => { + expect.assertions(1); + return AlphabeticIndex.create({ sync: false - }).then(function(ai) { - test.ok(ai); - test.done(); + }).then(ai => { + expect(ai).toBeTruthy(); }); - }, + }); - testAlphaIndexAsyncConstructorWithParams: function(test) { - test.expect(1); - AlphabeticIndex.create({ + test("AlphaIndexAsyncConstructorWithParams", () => { + expect.assertions(1); + return AlphabeticIndex.create({ locale: "en-US", sync: false - }).then(function(ai) { - test.ok(ai); - test.done(); + }).then(ai => { + expect(ai).toBeTruthy(); }); - }, + }); - testAlphaIndexAsyncgetDefaultIndexStyle: function(test) { - test.expect(2); - AlphabeticIndex.create({ + test("AlphaIndexAsyncgetDefaultIndexStyle", () => { + expect.assertions(2); + return AlphabeticIndex.create({ locale: "en-US", sync: false - }).then(function(ai) { - test.ok(ai); - test.equal("latin", ai.getIndexStyle()); - test.done(); + }).then(ai => { + expect(ai).toBeTruthy(); + expect("latin").toBe(ai.getIndexStyle()); }); - }, + }); - testAlphaIndexAsyncConstructorWithUnknownLocale: function(test) { - test.expect(1); - AlphabeticIndex.create({ + test("AlphaIndexAsyncConstructorWithUnknownLocale", () => { + expect.assertions(1); + return AlphabeticIndex.create({ locale: "qq-QQ", sync: false - }).then(function(ai) { - test.ok(ai); - test.done(); + }).then(ai => { + expect(ai).toBeTruthy(); }); - }, + }); - testAlphaIndexAsyncENUSGetBucket: function(test) { - test.expect(3); + test("AlphaIndexAsyncENUSGetBucket", () => { + expect.assertions(3); - AlphabeticIndex.create({ + return AlphabeticIndex.create({ locale: "en-US", sync: false - }).then(function(ai) { - test.ok(ai); - test.equal("B", ai.getBucket("belarus")); - test.equal("B", ai.getBucket("Belarus")); - - test.done(); + }).then(ai => { + expect(ai).toBeTruthy(); + expect("B").toBe(ai.getBucket("belarus")); + expect("B").toBe(ai.getBucket("Belarus")); }); - }, + }); - testAlphaIndexAsyncENUSGetBucket2: function(test) { - test.expect(3); + test("AlphaIndexAsyncENUSGetBucket2", () => { + expect.assertions(3); - AlphabeticIndex.create({ + return AlphabeticIndex.create({ locale: "en-US", sync: false - }).then(function(ai) { - test.ok(ai); - - test.equal("E", ai.getBucket("Élan")); - test.equal("E", ai.getBucket("ëieasdf")); + }).then(ai => { + expect(ai).toBeTruthy(); - test.done(); + expect("E").toBe(ai.getBucket("Élan")); + expect("E").toBe(ai.getBucket("ëieasdf")); }); - }, + }); - testAlphaIndexAsyncENUSGetAllBuckets: function(test) { - test.expect(2); + test("AlphaIndexAsyncENUSGetAllBuckets", () => { + expect.assertions(2); - AlphabeticIndex.create({ + return AlphabeticIndex.create({ locale: "en-US", sync: false - }).then(function(ai) { - test.ok(ai); + }).then(ai => { + expect(ai).toBeTruthy(); - var items = [ + const items = [ "omicron", "beta", "epsilon", @@ -121,11 +113,11 @@ export const testalphaindexpromise = { "gamma" ]; - items.forEach(function(item) { + items.forEach(item => { ai.addElement(item); }); - var expected = [ + const expected = [ { label: "A", elements: ["alpha"] @@ -168,21 +160,20 @@ export const testalphaindexpromise = { } ] - test.deepEqual(ai.getAllBuckets(), expected); - test.done(); + expect(ai.getAllBuckets()).toStrictEqual(expected); }); - }, + }); - testAlphaIndexAsyncENUSGetBucketLabels: function(test) { - test.expect(2); + test("AlphaIndexAsyncENUSGetBucketLabels", () => { + expect.assertions(2); - AlphabeticIndex.create({ + return AlphabeticIndex.create({ locale: "en-US", sync: false - }).then(function(ai) { - test.ok(ai); + }).then(ai => { + expect(ai).toBeTruthy(); - var items = [ + const items = [ "omicron", "beta", "echo", @@ -205,11 +196,11 @@ export const testalphaindexpromise = { "gamma" ]; - items.forEach(function(item) { + items.forEach(item => { ai.addElement(item); }); - var expected = [ + const expected = [ "A", "B", "C", @@ -227,22 +218,21 @@ export const testalphaindexpromise = { "Z" ]; - test.deepEqual(ai.getBucketLabels(), expected); - test.done(); + expect(ai.getBucketLabels()).toStrictEqual(expected); }); - }, + }); - testAlphaIndexAsyncUnknowLocaleActsLikeEnglish: function(test) { - test.expect(2); + test("AlphaIndexAsyncUnknowLocaleActsLikeEnglish", () => { + expect.assertions(2); - AlphabeticIndex.create({ + return AlphabeticIndex.create({ locale: "qq-QQ", sync: false - }).then(function(ai) { - test.ok(ai); + }).then(ai => { + expect(ai).toBeTruthy(); - var items = [ + const items = [ "omicron", "beta", "echo", @@ -260,11 +250,11 @@ export const testalphaindexpromise = { "ürgen" ]; - items.forEach(function(item) { + items.forEach(item => { ai.addElement(item); }); - var expected = [ + const expected = [ { label: "A", elements: ["alpha"] @@ -311,68 +301,63 @@ export const testalphaindexpromise = { } ] - test.deepEqual(ai.getAllBuckets(), expected); - test.done(); + expect(ai.getAllBuckets()).toStrictEqual(expected); }); - }, + }); - testAlphaIndexAsyncConstructorAsync_deDE: function(test) { - test.expect(1); - var ai = AlphabeticIndex.create({ + test("AlphaIndexAsyncConstructorAsync_deDE", () => { + expect.assertions(1); + return AlphabeticIndex.create({ locale: "de-DE", sync: false - }).then(function(ai) { - test.ok(ai); - test.done(); + }).then(ai => { + expect(ai).toBeTruthy(); }); - }, + }); - testAlphaIndexAsyncGetBucket_deDE2: function(test) { - test.expect(7); + test("AlphaIndexAsyncGetBucket_deDE2", () => { + expect.assertions(7); - var ai = AlphabeticIndex.create({ + return AlphabeticIndex.create({ locale: "de-DE", sync: false - }).then(function(ai) { - test.ok(ai); - - test.equal("U", ai.getBucket("über")); - test.equal("A", ai.getBucket("änderen")); - test.equal("O", ai.getBucket("öffenen")); + }).then(ai => { + expect(ai).toBeTruthy(); - test.equal("U", ai.getBucket("Über")); - test.equal("A", ai.getBucket("Änderen")); - test.equal("O", ai.getBucket("Öffenen")); + expect("U").toBe(ai.getBucket("über")); + expect("A").toBe(ai.getBucket("änderen")); + expect("O").toBe(ai.getBucket("öffenen")); - test.done(); + expect("U").toBe(ai.getBucket("Über")); + expect("A").toBe(ai.getBucket("Änderen")); + expect("O").toBe(ai.getBucket("Öffenen")); }); - }, + }); - testAlphaIndexAsyncGetBucketAccentInsensitiveSZ_deDE: function(test) { - test.expect(2); + test("AlphaIndexAsyncGetBucketAccentInsensitiveSZ_deDE", () => { + expect.assertions(2); - var ai = AlphabeticIndex.create({ + return AlphabeticIndex.create({ locale: "de-DE", sync: false - }).then(function(ai) { - test.ok(ai); + }).then(ai => { + expect(ai).toBeTruthy(); // no words start with sz, but handle this just in case - test.equal("S", ai.getBucket("ß")); - test.done(); + expect("S").toBe(ai.getBucket("ß")); }); - }, + }); - testAlphaIndexAsyncDEDEPhonebookStyle: function(test) { - test.expect(2); + test("AlphaIndexAsyncDEDEPhonebookStyle", () => { + expect.assertions(2); - var ai = AlphabeticIndex.create({ + return AlphabeticIndex.create({ locale: "de-DE", style: "phonebook", sync: false - }).then(function(ai) { - test.ok(ai); + }).then(ai => { + expect(ai).toBeTruthy(); - var items = [ + const items = [ "Jürgen", "Georg", "Matthias", @@ -393,11 +378,11 @@ export const testalphaindexpromise = { "Judrich" ]; - items.forEach(function(item) { + items.forEach(item => { ai.addElement(item); }); - var expected = [ + const expected = [ { label: "F", elements: ["Fritz"] @@ -428,23 +413,22 @@ export const testalphaindexpromise = { } ]; - test.deepEqual(ai.getAllBuckets(), expected); - test.done(); + expect(ai.getAllBuckets()).toStrictEqual(expected); }); - }, + }); - testAlphaIndexAsyncDEDEDictionaryStyle: function(test) { - test.expect(2); + test("AlphaIndexAsyncDEDEDictionaryStyle", () => { + expect.assertions(2); - var ai = AlphabeticIndex.create({ + return AlphabeticIndex.create({ locale: "de-DE", style: "dictionary", sync: false - }).then(function(ai) { - test.ok(ai); + }).then(ai => { + expect(ai).toBeTruthy(); - var items = [ + const items = [ "Jürgen", "Georg", "Matthias", @@ -465,11 +449,11 @@ export const testalphaindexpromise = { "Judrich" ]; - items.forEach(function(item) { + items.forEach(item => { ai.addElement(item); }); - var expected = [ + const expected = [ { label: "F", elements: ["Fritz"] @@ -500,32 +484,30 @@ export const testalphaindexpromise = { } ]; - test.deepEqual(ai.getAllBuckets(), expected); - test.done(); + expect(ai.getAllBuckets()).toStrictEqual(expected); }); - }, + }); - testAlphaIndexAsyncConstructorAsync: function(test) { - test.expect(1); - var ai = AlphabeticIndex.create({ + test("AlphaIndexAsyncConstructorAsync", () => { + expect.assertions(1); + return AlphabeticIndex.create({ locale: "ko-KR", sync: false - }).then(function(ai) { - test.ok(ai); - test.done(); + }).then(ai => { + expect(ai).toBeTruthy(); }); - }, + }); - testAlphaIndexAsyncKOKRMixedScriptTest1: function(test) { - test.expect(2); + test("AlphaIndexAsyncKOKRMixedScriptTest1", () => { + expect.assertions(2); - var ai = AlphabeticIndex.create({ + return AlphabeticIndex.create({ locale: "ko-KR", sync: false - }).then(function(ai) { - test.ok(ai); + }).then(ai => { + expect(ai).toBeTruthy(); - var items = [ + const items = [ "Apple", "Banana", "김철수", @@ -550,11 +532,11 @@ export const testalphaindexpromise = { "박세진" ]; - items.forEach(function(item) { + items.forEach(item => { ai.addElement(item); }); - var expected = [ + const expected = [ { label: "ㄱ", elements: ["강성진","김영희","김철수"] @@ -597,22 +579,21 @@ export const testalphaindexpromise = { } ] - test.deepEqual(ai.getAllBuckets(), expected); - test.done(); + expect(ai.getAllBuckets()).toStrictEqual(expected); }); - }, + }); - testAlphaIndexAsyncKOKRMixedScriptTest2: function(test) { - test.expect(2); + test("AlphaIndexAsyncKOKRMixedScriptTest2", () => { + expect.assertions(2); - var ai = AlphabeticIndex.create({ + return AlphabeticIndex.create({ locale: "ko-KR", sync: false - }).then(function(ai) { - test.ok(ai); + }).then(ai => { + expect(ai).toBeTruthy(); - var items = [ + const items = [ "Apple", "Banana", "김철수", @@ -630,11 +611,11 @@ export const testalphaindexpromise = { "박세진" ]; - items.forEach(function(item) { + items.forEach(item => { ai.addElement(item); }); - var expected = [ + const expected = [ { label: "ㄱ", elements: ["강성진", "김철수"] @@ -681,8 +662,7 @@ export const testalphaindexpromise = { } ]; - test.deepEqual(ai.getAllBuckets(), expected); - test.done(); + expect(ai.getAllBuckets()).toStrictEqual(expected); }); - } -}; \ No newline at end of file + }); +}); \ No newline at end of file diff --git a/test/calasync.test.js b/test/calasync.test.js new file mode 100644 index 0000000..c7e97f5 --- /dev/null +++ b/test/calasync.test.js @@ -0,0 +1,390 @@ +/* + * calasync.test.js - test the calendar objects asynchronously + * + * Copyright © 2018, 2022, 2024 JEDLSoft + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import CalendarFactory, {CalendarFactoryAsync} from "../src/CalendarFactory.js"; +import CopticDate from "../src/CopticDate.js"; +import EthiopicDate from "../src/EthiopicDate.js"; +import GregorianDate from "../src/GregorianDate.js"; +import HanDate from "../src/HanDate.js"; +import HebrewDate from "../src/HebrewDate.js"; +import IslamicDate from "../src/IslamicDate.js"; +import JulianDate from "../src/JulianDate.js"; +import PersianAlgoDate from "../src/PersianAlgoDate.js"; +import PersianDate from "../src/PersianDate.js"; +import ThaiSolarDate from "../src/ThaiSolarDate.js"; + +describe("testcalasync", () => { + test("CalendarFactoryAsyncDefault", () => { + expect.assertions(1); + CalendarFactory({ + sync: false, + onLoad: cal => { + expect(typeof(cal) !== "undefined").toBeTruthy(); + } + }); + }); + + test("CalendarFactoryAsyncSpecific", () => { + expect.assertions(2); + const cal = CalendarFactory({ + type: "julian", + sync: false, + onLoad: cal => { + expect(typeof(cal) !== "undefined").toBeTruthy(); + + expect(cal.getType()).toBe("julian"); + } + }); + }); + + test("CalendarFactoryAsyncUnknown", () => { + expect.assertions(1); + try { + CalendarFactory({ + type: "asdf", + sync: false, + onLoad: cal => { + expect(typeof(cal) === "undefined").toBeTruthy(); + } + }); + } catch (e) { + console.log("caught: " + e); + } + }); + + test("CalendarFactoryAsyncDefaultForLocale", () => { + expect.assertions(2); + CalendarFactory({ + locale: "fa-IR", + sync: false, + onLoad: cal => { + expect(typeof(cal) !== "undefined").toBeTruthy(); + + expect(cal.getType()).toBe("persian"); + } + }); + }); + + test("CalendarFactoryAsyncDefaultForLocaleOther", () => { + expect.assertions(2); + const cal = CalendarFactory({ + locale: "th-TH", + sync: false, + onLoad: cal => { + expect(typeof(cal) !== "undefined").toBeTruthy(); + + expect(cal.getType()).toBe("thaisolar"); + } + }); + }); + + test("CalendarFactoryAsyncOverrideLocale", () => { + expect.assertions(2); + const cal = CalendarFactory({ + locale: "fa-IR", + type: "gregorian", + sync: false, + onLoad: cal => { + expect(typeof(cal) !== "undefined").toBeTruthy(); + + expect(cal.getType()).toBe("gregorian"); + } + }); + }); + + test("CopticDateAsyncConstructorFull", () => { + expect.assertions(8); + new CopticDate({ + year: 1735, + month: 9, + day: 23, + hour: 16, + minute: 7, + second: 12, + millisecond: 123, + timezone: "Etc/UTC", + sync: false, + onLoad: cd => { + expect(cd !== null).toBeTruthy(); + + expect(cd.getYears()).toBe(1735); + expect(cd.getMonths()).toBe(9); + expect(cd.getDays()).toBe(23); + expect(cd.getHours()).toBe(16); + expect(cd.getMinutes()).toBe(7); + expect(cd.getSeconds()).toBe(12); + expect(cd.getMilliseconds()).toBe(123); + } + }); + }); + + test("EthiopicDateAsyncConstructorFull", () => { + expect.assertions(8); + new EthiopicDate({ + year: 2011, + month: 9, + day: 23, + hour: 16, + minute: 7, + second: 12, + millisecond: 123, + timezone: "Etc/UTC", + sync: false, + onLoad: ed => { + expect(ed !== null).toBeTruthy(); + + expect(ed.getYears()).toBe(2011); + expect(ed.getMonths()).toBe(9); + expect(ed.getDays()).toBe(23); + expect(ed.getHours()).toBe(16); + expect(ed.getMinutes()).toBe(7); + expect(ed.getSeconds()).toBe(12); + expect(ed.getMilliseconds()).toBe(123); + } + }); + }); + + test("GregDateAsyncConstructorFull", () => { + expect.assertions(8); + new GregorianDate({ + year: 2011, + month: 9, + day: 23, + hour: 16, + minute: 7, + second: 12, + millisecond: 123, + sync: false, + onLoad: gd => { + expect(gd !== null).toBeTruthy(); + + expect(gd.getYears()).toBe(2011); + expect(gd.getMonths()).toBe(9); + expect(gd.getDays()).toBe(23); + expect(gd.getHours()).toBe(16); + expect(gd.getMinutes()).toBe(7); + expect(gd.getSeconds()).toBe(12); + expect(gd.getMilliseconds()).toBe(123); + } + }); + + }); + + test("HanDateAsyncConstructorFull", () => { + expect.assertions(10); + new HanDate({ + year: 4711, + month: 9, + day: 23, + hour: 16, + minute: 7, + second: 12, + millisecond: 123, + timezone: "Etc/UTC", + sync: false, + onLoad: hd => { + expect(hd !== null).toBeTruthy(); + + expect(hd.getYears()).toBe(4711); + expect(hd.getMonths()).toBe(9); + expect(hd.getDays()).toBe(23); + expect(hd.getHours()).toBe(16); + expect(hd.getMinutes()).toBe(7); + expect(hd.getSeconds()).toBe(12); + expect(hd.getMilliseconds()).toBe(123); + expect(hd.getCycles()).toBe(78); + expect(hd.getCycleYears()).toBe(31); + } + }); + + }); + + test("HebrewDateAsyncConstructorFull", () => { + expect.assertions(8); + new HebrewDate({ + year: 2011, + month: 9, + day: 23, + hour: 16, + minute: 7, + second: 12, + millisecond: 123, + timezone: "Etc/UTC", + sync: false, + onLoad: hd => { + expect(hd !== null).toBeTruthy(); + + expect(hd.getYears()).toBe(2011); + expect(hd.getMonths()).toBe(9); + expect(hd.getDays()).toBe(23); + expect(hd.getHours()).toBe(16); + expect(hd.getMinutes()).toBe(7); + expect(hd.getSeconds()).toBe(12); + expect(hd.getMilliseconds()).toBe(123); + } + }); + + }); + + test("IslamicDateAsyncConstructorFull", () => { + expect.assertions(8); + new IslamicDate({ + year: 2011, + month: 9, + day: 23, + hour: 16, + minute: 7, + second: 12, + millisecond: 123, + timezone: "Etc/UTC", + sync: false, + onLoad: id => { + expect(id !== null).toBeTruthy(); + + expect(id.getYears()).toBe(2011); + expect(id.getMonths()).toBe(9); + expect(id.getDays()).toBe(23); + expect(id.getHours()).toBe(16); + expect(id.getMinutes()).toBe(7); + expect(id.getSeconds()).toBe(12); + expect(id.getMilliseconds()).toBe(123); + } + }); + + }); + + test("JulDateAsyncConstructorFull", () => { + expect.assertions(8); + new JulianDate({ + year: 2011, + month: 9, + day: 23, + hour: 16, + minute: 7, + second: 12, + millisecond: 123, + timezone: "Etc/UTC", + sync: false, + onLoad: jul => { + expect(jul !== null).toBeTruthy(); + + expect(jul.getYears()).toBe(2011); + expect(jul.getMonths()).toBe(9); + expect(jul.getDays()).toBe(23); + expect(jul.getHours()).toBe(16); + expect(jul.getMinutes()).toBe(7); + expect(jul.getSeconds()).toBe(12); + expect(jul.getMilliseconds()).toBe(123); + } + }); + + }); + + test("PersAlgoDateAsyncConstructorFull", () => { + expect.assertions(8); + new PersianAlgoDate({ + year: 1392, + month: 9, + day: 23, + hour: 16, + minute: 7, + second: 12, + millisecond: 123, + timezone: "Etc/UTC", + sync: false, + onLoad: pd => { + expect(pd !== null).toBeTruthy(); + + expect(pd.getYears()).toBe(1392); + expect(pd.getMonths()).toBe(9); + expect(pd.getDays()).toBe(23); + expect(pd.getHours()).toBe(16); + expect(pd.getMinutes()).toBe(7); + expect(pd.getSeconds()).toBe(12); + expect(pd.getMilliseconds()).toBe(123); + } + }); + + }); + + test("PersDateAstroAsyncConstructorFull", () => { + expect.assertions(8); + new PersianDate({ + year: 1392, + month: 9, + day: 23, + hour: 16, + minute: 7, + second: 12, + millisecond: 123, + timezone: "Etc/UTC", + sync: false, + onLoad: pd => { + expect(pd !== null).toBeTruthy(); + + expect(pd.getYears()).toBe(1392); + expect(pd.getMonths()).toBe(9); + expect(pd.getDays()).toBe(23); + expect(pd.getHours()).toBe(16); + expect(pd.getMinutes()).toBe(7); + expect(pd.getSeconds()).toBe(12); + expect(pd.getMilliseconds()).toBe(123); + } + }); + + }); + + test("ThaiSolarDateAsyncConstructorFull", () => { + expect.assertions(8); + new ThaiSolarDate({ + year: 2553, + month: 9, + day: 23, + hour: 16, + minute: 7, + second: 12, + millisecond: 123, + timezone: "Etc/UTC", + sync: false, + onLoad: td => { + expect(td !== null).toBeTruthy(); + + expect(td.getYears()).toBe(2553); + expect(td.getMonths()).toBe(9); + expect(td.getDays()).toBe(23); + expect(td.getHours()).toBe(16); + expect(td.getMinutes()).toBe(7); + expect(td.getSeconds()).toBe(12); + expect(td.getMilliseconds()).toBe(123); + } + }); + }); + + test("CalendarFactoryAsync", () => { + expect.assertions(2); + const cal = CalendarFactoryAsync({ + locale: "th-TH", + onLoad: cal => { + expect(typeof(cal) !== "undefined").toBeTruthy(); + + expect(cal.getType()).toBe("thaisolar"); + } + }); + }); +}); diff --git a/test/calpromise.test.js b/test/calpromise.test.js new file mode 100644 index 0000000..0f70764 --- /dev/null +++ b/test/calpromise.test.js @@ -0,0 +1,372 @@ +/* + * calpromise.test.js - test the calendar objects asynchronously with promises + * + * Copyright © 2018, 2022, 2024 JEDLSoft + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import CalendarFactory, {CalendarFactoryAsync} from "../src/CalendarFactory.js"; +import CopticDate from "../src/CopticDate.js"; +import EthiopicDate from "../src/EthiopicDate.js"; +import GregorianDate from "../src/GregorianDate.js"; +import HanDate from "../src/HanDate.js"; +import HebrewDate from "../src/HebrewDate.js"; +import IslamicDate from "../src/IslamicDate.js"; +import JulianDate from "../src/JulianDate.js"; +import PersianAlgoDate from "../src/PersianAlgoDate.js"; +import PersianDate from "../src/PersianDate.js"; +import ThaiSolarDate from "../src/ThaiSolarDate.js"; + +describe("testcalpromise", () => { + test("CalendarPromiseFactoryAsyncDefault", () => { + expect.assertions(1); + return CalendarFactoryAsync().then(cal => { + expect(typeof(cal) !== "undefined").toBeTruthy(); + }); + }); + + test("CalendarPromiseFactoryAsyncSpecific", () => { + expect.assertions(2); + return CalendarFactoryAsync({ + type: "julian" + }).then(cal => { + expect(typeof(cal) !== "undefined").toBeTruthy(); + + expect(cal.getType()).toBe("julian"); + }); + }); + + test("CalendarPromiseFactoryAsyncUnknown", () => { + expect.assertions(1); + return CalendarFactoryAsync({ + type: "asdf" + }).then(cal => { + expect(typeof(cal) === "undefined").toBeTruthy(); + }).catch(e => { + test.fail(); + console.log("caught: " + e); + }); + }); + + test("CalendarPromiseFactoryAsyncDefaultForLocale", () => { + expect.assertions(2); + return CalendarFactoryAsync({ + locale: "fa-IR" + }).then(cal => { + expect(typeof(cal) !== "undefined").toBeTruthy(); + + expect(cal.getType()).toBe("persian"); + }); + }); + + test("CalendarPromiseFactoryAsyncDefaultForLocaleOther", () => { + expect.assertions(2); + return CalendarFactoryAsync({ + locale: "th-TH" + }).then(cal => { + expect(typeof(cal) !== "undefined").toBeTruthy(); + + expect(cal.getType()).toBe("thaisolar"); + }); + }); + + test("CalendarPromiseFactoryAsyncOverrideLocale", () => { + expect.assertions(2); + return CalendarFactoryAsync({ + locale: "fa-IR", + type: "gregorian" + }).then(cal => { + expect(typeof(cal) !== "undefined").toBeTruthy(); + + expect(cal.getType()).toBe("gregorian"); + }); + }); + + test("CopticDatePromiseConstructorFull", () => { + expect.assertions(8); + try { + return CopticDate.create({ + year: 1735, + month: 9, + day: 23, + hour: 16, + minute: 7, + second: 12, + millisecond: 123, + timezone: "Etc/UTC", + sync: false + }).then(cd => { + expect(cd !== null).toBeTruthy(); + + expect(cd.getYears()).toBe(1735); + expect(cd.getMonths()).toBe(9); + expect(cd.getDays()).toBe(23); + expect(cd.getHours()).toBe(16); + expect(cd.getMinutes()).toBe(7); + expect(cd.getSeconds()).toBe(12); + expect(cd.getMilliseconds()).toBe(123); + }).catch(err => { + console.log("Error: " + err); + test.fail(); + }); + } catch (e) { + console.log("Got exception: " + e); + } + }); + + test("EthiopicDatePromiseConstructorFull", () => { + expect.assertions(8); + return EthiopicDate.create({ + year: 2011, + month: 9, + day: 23, + hour: 16, + minute: 7, + second: 12, + millisecond: 123, + timezone: "Etc/UTC", + sync: false + }).then(ed => { + expect(ed !== null).toBeTruthy(); + + expect(ed.getYears()).toBe(2011); + expect(ed.getMonths()).toBe(9); + expect(ed.getDays()).toBe(23); + expect(ed.getHours()).toBe(16); + expect(ed.getMinutes()).toBe(7); + expect(ed.getSeconds()).toBe(12); + expect(ed.getMilliseconds()).toBe(123); + }); + }); + + test("GregDatePromiseConstructorFull", () => { + expect.assertions(8); + return GregorianDate.create({ + year: 2011, + month: 9, + day: 23, + hour: 16, + minute: 7, + second: 12, + millisecond: 123, + sync: false + }).then(gd => { + expect(gd !== null).toBeTruthy(); + + expect(gd.getYears()).toBe(2011); + expect(gd.getMonths()).toBe(9); + expect(gd.getDays()).toBe(23); + expect(gd.getHours()).toBe(16); + expect(gd.getMinutes()).toBe(7); + expect(gd.getSeconds()).toBe(12); + expect(gd.getMilliseconds()).toBe(123); + }); + + }); + + test("HanDatePromiseConstructorFull", () => { + expect.assertions(10); + return HanDate.create({ + year: 4711, + month: 9, + day: 23, + hour: 16, + minute: 7, + second: 12, + millisecond: 123, + timezone: "Etc/UTC", + sync: false + }).then(hd => { + expect(hd !== null).toBeTruthy(); + + expect(hd.getYears()).toBe(4711); + expect(hd.getMonths()).toBe(9); + expect(hd.getDays()).toBe(23); + expect(hd.getHours()).toBe(16); + expect(hd.getMinutes()).toBe(7); + expect(hd.getSeconds()).toBe(12); + expect(hd.getMilliseconds()).toBe(123); + expect(hd.getCycles()).toBe(78); + expect(hd.getCycleYears()).toBe(31); + }); + + }); + + test("HebrewDatePromiseConstructorFull", () => { + expect.assertions(8); + return HebrewDate.create({ + year: 2011, + month: 9, + day: 23, + hour: 16, + minute: 7, + second: 12, + millisecond: 123, + timezone: "Etc/UTC", + sync: false + }).then(hd => { + expect(hd !== null).toBeTruthy(); + + expect(hd.getYears()).toBe(2011); + expect(hd.getMonths()).toBe(9); + expect(hd.getDays()).toBe(23); + expect(hd.getHours()).toBe(16); + expect(hd.getMinutes()).toBe(7); + expect(hd.getSeconds()).toBe(12); + expect(hd.getMilliseconds()).toBe(123); + }); + + }); + + test("IslamicDatePromiseConstructorFull", () => { + expect.assertions(8); + return IslamicDate.create({ + year: 2011, + month: 9, + day: 23, + hour: 16, + minute: 7, + second: 12, + millisecond: 123, + timezone: "Etc/UTC", + sync: false + }).then(id => { + expect(id !== null).toBeTruthy(); + + expect(id.getYears()).toBe(2011); + expect(id.getMonths()).toBe(9); + expect(id.getDays()).toBe(23); + expect(id.getHours()).toBe(16); + expect(id.getMinutes()).toBe(7); + expect(id.getSeconds()).toBe(12); + expect(id.getMilliseconds()).toBe(123); + }); + + }); + + test("JulDatePromiseConstructorFull", () => { + expect.assertions(8); + return JulianDate.create({ + year: 2011, + month: 9, + day: 23, + hour: 16, + minute: 7, + second: 12, + millisecond: 123, + timezone: "Etc/UTC", + sync: false + }).then(jul => { + expect(jul !== null).toBeTruthy(); + + expect(jul.getYears()).toBe(2011); + expect(jul.getMonths()).toBe(9); + expect(jul.getDays()).toBe(23); + expect(jul.getHours()).toBe(16); + expect(jul.getMinutes()).toBe(7); + expect(jul.getSeconds()).toBe(12); + expect(jul.getMilliseconds()).toBe(123); + }); + + }); + + test("PersAlgoDatePromiseConstructorFull", () => { + expect.assertions(8); + return PersianAlgoDate.create({ + year: 1392, + month: 9, + day: 23, + hour: 16, + minute: 7, + second: 12, + millisecond: 123, + timezone: "Etc/UTC", + sync: false + }).then(pd => { + expect(pd !== null).toBeTruthy(); + + expect(pd.getYears()).toBe(1392); + expect(pd.getMonths()).toBe(9); + expect(pd.getDays()).toBe(23); + expect(pd.getHours()).toBe(16); + expect(pd.getMinutes()).toBe(7); + expect(pd.getSeconds()).toBe(12); + expect(pd.getMilliseconds()).toBe(123); + }); + + }); + + test("PersDateAstroAsyncConstructorFull", () => { + expect.assertions(8); + return PersianDate.create({ + year: 1392, + month: 9, + day: 23, + hour: 16, + minute: 7, + second: 12, + millisecond: 123, + timezone: "Etc/UTC", + sync: false + }).then(pd => { + expect(pd !== null).toBeTruthy(); + + expect(pd.getYears()).toBe(1392); + expect(pd.getMonths()).toBe(9); + expect(pd.getDays()).toBe(23); + expect(pd.getHours()).toBe(16); + expect(pd.getMinutes()).toBe(7); + expect(pd.getSeconds()).toBe(12); + expect(pd.getMilliseconds()).toBe(123); + }); + + }); + + test("ThaiSolarDatePromiseConstructorFull", () => { + expect.assertions(8); + return ThaiSolarDate.create({ + year: 2553, + month: 9, + day: 23, + hour: 16, + minute: 7, + second: 12, + millisecond: 123, + timezone: "Etc/UTC", + sync: false + }).then(td => { + expect(td !== null).toBeTruthy(); + + expect(td.getYears()).toBe(2553); + expect(td.getMonths()).toBe(9); + expect(td.getDays()).toBe(23); + expect(td.getHours()).toBe(16); + expect(td.getMinutes()).toBe(7); + expect(td.getSeconds()).toBe(12); + expect(td.getMilliseconds()).toBe(123); + }); + }); + + test("CalendarPromiseFactoryAsync", () => { + expect.assertions(2); + return CalendarFactoryAsync({ + locale: "th-TH" + }).then(cal => { + expect(typeof(cal) !== "undefined").toBeTruthy(); + + expect(cal.getType()).toBe("thaisolar"); + }); + }); +}); diff --git a/test/charmapasync.test.js b/test/charmapasync.test.js new file mode 100644 index 0000000..d77835a --- /dev/null +++ b/test/charmapasync.test.js @@ -0,0 +1,130 @@ +/* + * charmapasync.test.js - Test the charset mapping routines + * + * Copyright © 2018, 2022, 2024 JEDLSoft + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import CharmapFactory from "../src/CharmapFactory.js"; + +describe("testcharmapasync", () => { + test("CharmapAsyncConstructor", () => { + expect.assertions(1); + CharmapFactory({ + sync: false, + onLoad: cm => { + expect(typeof(cm) !== "undefined").toBeTruthy(); + } + }); + }); + + test("CharmapAsyncLoadMap", () => { + expect.assertions(3); + const cm = CharmapFactory({ + name: "ISO-8859-15", + sync: false, + onLoad: cm => { + expect(typeof(cm) !== "undefined").toBeTruthy(); + expect(typeof(cm.map) !== "undefined").toBeTruthy(); + expect(cm.getName()).toBe("ISO-8859-15"); + } + }); + }); + + test("CharmapAsyncAlias", () => { + expect.assertions(3); + const cm = CharmapFactory({ + name: "ISO-Latin-9", + sync: false, + onLoad: cm => { + expect(typeof(cm) !== "undefined").toBeTruthy(); + expect(typeof(cm.map) !== "undefined").toBeTruthy(); + expect(cm.getName()).toBe("ISO-8859-15"); + } + }); + }); + + test("CharmapAsyncLoadAlgorithmic", () => { + expect.assertions(3); + const cm = CharmapFactory({ + name: "UTF-8", + sync: false, + onLoad: cm => { + expect(typeof(cm) !== "undefined").toBeTruthy(); + expect(typeof(cm.map) === "undefined").toBeTruthy(); // no map because it's algorithmic + expect(cm.getName()).toBe("UTF-8"); + } + }); + }); + + test("CharmapAsyncUTF8MapToUnicodeUint8ArrayExtended3", () => { + expect.assertions(2); + const cm = CharmapFactory({ + name: "UTF-8", + sync: false, + onLoad: cm => { + expect(typeof(cm) !== "undefined").toBeTruthy(); + const input = new Uint8Array([ + 0xe4, 0xb8, 0x80, + 0xe4, 0xb8, 0x81, + 0xe4, 0xb8, 0x82, + 0xe5, 0x9c, 0x81, + 0xe5, 0x9c, 0x82, + 0xe5, 0x9c, 0x83 + ]); + + expect(cm.mapToUnicode(input)).toBe("一丁丂圁圂圃"); + } + }); + }); + + test("CharmapAsyncCNMapToUnicode", () => { + const big5source = [ + 0xa4, 0xb1, // 仃 + 0xa4, 0x48, // 人 + 0xae, 0x49, // 埋 + 0xaa, 0x42, // 朋 + 0xae, 0x54 // 娛 + ]; + + CharmapFactory({ + name: "Big5", + sync: false, + onLoad: cm => { + expect(typeof(cm) !== "undefined").toBeTruthy(); + expect(cm.mapToUnicode(big5source)).toStrictEqual("仃人埋朋娛"); + } + }); + }); + + test("CharmapAsyncCNMapToNative", () => { + const big5source = new Uint8Array([ + 0xa4, 0xb1, // 仃 + 0xa4, 0x48, // 人 + 0xae, 0x49, // 埋 + 0xaa, 0x42, // 朋 + 0xae, 0x54 // 娛 + ]); + + CharmapFactory({ + name: "Big5", + sync: false, + onLoad: cm => { + expect(typeof(cm) !== "undefined").toBeTruthy(); + expect(cm.mapToNative("仃人埋朋娛")).toStrictEqual(big5source); + } + }); + }); +}); diff --git a/test/charmappromise.test.js b/test/charmappromise.test.js new file mode 100644 index 0000000..119ff8a --- /dev/null +++ b/test/charmappromise.test.js @@ -0,0 +1,115 @@ +/* + * charmapasync.test.js - Test the charset mapping routines + * + * Copyright © 2018, 2022, 2024 JEDLSoft + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import CharmapFactory, {CharmapFactoryAsync} from "../src/CharmapFactory.js"; + +describe("testcharmappromise", () => { + test("CharmapAsyncConstructor", () => { + expect.assertions(1); + return CharmapFactoryAsync().then(cm => { + expect(typeof(cm) !== "undefined").toBeTruthy(); + }); + }); + + test("CharmapAsyncLoadMap", () => { + expect.assertions(3); + return CharmapFactoryAsync({ + name: "ISO-8859-15" + }).then(cm => { + expect(typeof(cm) !== "undefined").toBeTruthy(); + expect(typeof(cm.map) !== "undefined").toBeTruthy(); + expect(cm.getName()).toBe("ISO-8859-15"); + }); + }); + + test("CharmapAsyncAlias", () => { + expect.assertions(3); + return CharmapFactoryAsync({ + name: "ISO-Latin-9" + }).then(cm => { + expect(typeof(cm) !== "undefined").toBeTruthy(); + expect(typeof(cm.map) !== "undefined").toBeTruthy(); + expect(cm.getName()).toBe("ISO-8859-15"); + }); + }); + + test("CharmapAsyncLoadAlgorithmic", () => { + expect.assertions(3); + return CharmapFactoryAsync({ + name: "UTF-8" + }).then(cm => { + expect(typeof(cm) !== "undefined").toBeTruthy(); + expect(typeof(cm.map) === "undefined").toBeTruthy(); // no map because it's algorithmic + expect(cm.getName()).toBe("UTF-8"); + }); + }); + + test("CharmapAsyncUTF8MapToUnicodeUint8ArrayExtended3", () => { + expect.assertions(2); + return CharmapFactoryAsync({ + name: "UTF-8" + }).then(cm => { + expect(typeof(cm) !== "undefined").toBeTruthy(); + const input = new Uint8Array([ + 0xe4, 0xb8, 0x80, + 0xe4, 0xb8, 0x81, + 0xe4, 0xb8, 0x82, + 0xe5, 0x9c, 0x81, + 0xe5, 0x9c, 0x82, + 0xe5, 0x9c, 0x83 + ]); + + expect(cm.mapToUnicode(input)).toBe("一丁丂圁圂圃"); + }); + }); + + test("CharmapAsyncCNMapToUnicode", () => { + const big5source = [ + 0xa4, 0xb1, // 仃 + 0xa4, 0x48, // 人 + 0xae, 0x49, // 埋 + 0xaa, 0x42, // 朋 + 0xae, 0x54 // 娛 + ]; + + return CharmapFactoryAsync({ + name: "Big5" + }).then(cm => { + expect(typeof(cm) !== "undefined").toBeTruthy(); + expect(cm.mapToUnicode(big5source)).toStrictEqual("仃人埋朋娛"); + }); + }); + + test("CharmapAsyncCNMapToNative", () => { + const big5source = new Uint8Array([ + 0xa4, 0xb1, // 仃 + 0xa4, 0x48, // 人 + 0xae, 0x49, // 埋 + 0xaa, 0x42, // 朋 + 0xae, 0x54 // 娛 + ]); + + return CharmapFactoryAsync({ + name: "Big5" + }).then(cm => { + expect(typeof(cm) !== "undefined").toBeTruthy(); + expect(cm.mapToNative("仃人埋朋娛")).toStrictEqual(big5source); + }); + }); +}); diff --git a/test/charsetasync.test.js b/test/charsetasync.test.js new file mode 100644 index 0000000..d231d29 --- /dev/null +++ b/test/charsetasync.test.js @@ -0,0 +1,164 @@ +/* + * charsetasync.test.js - test the charset info object + * + * Copyright © 2018, 2022, 2024 JEDLSoft + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import Charset from "../src/Charset.js"; + +describe("testcharsetasync", () => { + test("CharsetAsyncConstructor", () => { + expect.assertions(1); + new Charset({ + sync: false, + onLoad: cs => { + expect(cs !== null).toBeTruthy(); + } + }); + }); + + test("CharsetAsyncGetStandardNameIdentity", () => { + expect.assertions(2); + new Charset({ + name: "UTF-8", + sync: false, + onLoad: cs => { + expect(cs.getName()).toBe("UTF-8"); + new Charset({ + name: "KOI8-R", + sync: false, + onLoad: cs => { + expect(cs.getName()).toBe("KOI8-R"); + } + }); + } + }) + }); + + test("CharsetAsyncGetStandardNameUndefined", () => { + expect.assertions(1); + new Charset({ + sync: false, + onLoad: cs => { + expect(typeof(cs) !== "undefined").toBeTruthy(); + } + }); + }); + + test("CharsetAsyncGetStandardNameIdentityUnknown", () => { + expect.assertions(1); + new Charset({ + name: "foobarfoo", + sync: false, + onLoad: cs => { + expect(cs.getName()).toBe("foobarfoo"); + } + }); + }); + + test("CharsetAsyncGetStandardNameUTF8", () => { + expect.assertions(1); + new Charset({ + name: "UTF8", + sync: false, + onLoad: cs => { + expect(cs.getName()).toBe("UTF-8"); + } + }); + }); + + test("CharsetAsyncGetStandardNameISOLatin1", () => { + expect.assertions(3); + new Charset({ + name: "Latin1", + sync: false, + onLoad: cs => { + expect(cs.getName()).toBe("ISO-8859-1"); + new Charset({ + name: "ISO-8859-1", + sync: false, + onLoad: cs => { + expect(cs.getName()).toBe("ISO-8859-1"); + new Charset({ + name: "ISO-Latin-1", + sync: false, + onLoad: cs => { + expect(cs.getName()).toBe("ISO-8859-1"); + } + }); + } + }); + } + }) + }); + + test("CharsetAsyncGetOriginalNameUnknown", () => { + expect.assertions(1); + new Charset({ + name: "foobarfoo", + sync: false, + onLoad: cs => { + expect(cs.getOriginalName()).toBe("foobarfoo"); + } + }) + }); + + test("CharsetAsyncMinCharWidth1", () => { + expect.assertions(1); + new Charset({ + name: "Latin1", + sync: false, + onLoad: cs => { + expect(cs.getMinCharWidth()).toBe(1); + } + }); + }); + + test("CharsetAsyncMinCharWidth2", () => { + expect.assertions(1); + // built-in + new Charset({ + name: "UCS-2", + sync: false, + onLoad: cs => { + expect(cs.getMinCharWidth()).toBe(2); + } + }); + }); + + test("CharsetAsyncIsMultibyteTrue", () => { + expect.assertions(1); + new Charset({ + name: "Shift_JIS", + sync: false, + onLoad: cs => { + expect(cs.isMultibyte()).toBeTruthy(); + } + }); + }); + + + test("CharsetAsyncIsBigEndianUTF16", () => { + expect.assertions(1); + new Charset({ + name: "UTF-16", + sync: false, + onLoad: cs => { + expect(cs.isBigEndian()).toBeTruthy(); + } + }); + }); +}); diff --git a/test/charsetpromise.test.js b/test/charsetpromise.test.js new file mode 100644 index 0000000..557de83 --- /dev/null +++ b/test/charsetpromise.test.js @@ -0,0 +1,150 @@ +/* + * charsetasync.test.js - test the charset info object + * + * Copyright © 2018, 2022, 2024 JEDLSoft + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import Charset from "../src/Charset.js"; + +describe("testcharsetpromise", () => { + test("CharsetAsyncConstructor", () => { + expect.assertions(1); + return Charset.create({ + sync: false + }).then(cs => { + expect(cs !== null).toBeTruthy(); + }); + }); + + test("CharsetAsyncGetStandardNameIdentity", () => { + expect.assertions(2); + return Charset.create({ + name: "UTF-8", + sync: false + }).then(cs => { + expect(cs.getName()).toBe("UTF-8"); + return Charset.create({ + name: "KOI8-R", + sync: false + }); + }).then(cs => { + expect(cs.getName()).toBe("KOI8-R"); + }); + }); + + test("CharsetAsyncGetStandardNameUndefined", () => { + expect.assertions(1); + return Charset.create({ + sync: false + }).then(cs => { + expect(typeof(cs) !== "undefined").toBeTruthy(); + }); + }); + + test("CharsetAsyncGetStandardNameIdentityUnknown", () => { + expect.assertions(1); + return Charset.create({ + name: "foobarfoo", + sync: false + }).then(cs => { + expect(cs.getName()).toBe("foobarfoo"); + }); + }); + + test("CharsetAsyncGetStandardNameUTF8", () => { + expect.assertions(1); + return Charset.create({ + name: "UTF8", + sync: false + }).then(cs => { + expect(cs.getName()).toBe("UTF-8"); + }); + }); + + test("CharsetAsyncGetStandardNameISOLatin1", () => { + expect.assertions(3); + return Charset.create({ + name: "Latin1", + sync: false + }).then(cs => { + expect(cs.getName()).toBe("ISO-8859-1"); + return Charset.create({ + name: "ISO-8859-1", + sync: false + }); + }).then(cs => { + expect(cs.getName()).toBe("ISO-8859-1"); + return Charset.create({ + name: "ISO-Latin-1", + sync: false + }); + }).then(cs => { + expect(cs.getName()).toBe("ISO-8859-1"); + }); + }); + + test("CharsetAsyncGetOriginalNameUnknown", () => { + expect.assertions(1); + return Charset.create({ + name: "foobarfoo", + sync: false + }).then(cs => { + expect(cs.getOriginalName()).toBe("foobarfoo"); + }); + }); + + test("CharsetAsyncMinCharWidth1", () => { + expect.assertions(1); + return Charset.create({ + name: "Latin1", + sync: false + }).then(cs => { + expect(cs.getMinCharWidth()).toBe(1); + }); + }); + + test("CharsetAsyncMinCharWidth2", () => { + expect.assertions(1); + // built-in + return Charset.create({ + name: "UCS-2", + sync: false + }).then(cs => { + expect(cs.getMinCharWidth()).toBe(2); + }); + }); + + test("CharsetAsyncIsMultibyteTrue", () => { + expect.assertions(1); + return Charset.create({ + name: "Shift_JIS", + sync: false + }).then(cs => { + expect(cs.isMultibyte()).toBeTruthy(); + }); + }); + + + test("CharsetAsyncIsBigEndianUTF16", () => { + expect.assertions(1); + return Charset.create({ + name: "UTF-16", + sync: false + }).then(cs => { + expect(cs.isBigEndian()).toBeTruthy(); + }); + }); +}); diff --git a/test/collationasync.test.js b/test/collationasync.test.js new file mode 100644 index 0000000..6f0e753 --- /dev/null +++ b/test/collationasync.test.js @@ -0,0 +1,469 @@ +/* + * collationasync.test.js - test the Collator object asynchronously + * + * Copyright © 2018, 2022, 2024 JEDLSoft + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import ilib from "../src/ilib.js"; +import Collator from "../src/Collator.js"; + +describe("testcollationasync", () => { + test("CollatorAsyncConstructorNative", () => { + expect.assertions(1); + const col = new Collator({ + sync: false, + onLoad: col => { + expect(typeof(col) !== "undefined").toBeTruthy(); + } + }); + }); + + test("CollatorAsyncDefaultNative", () => { + expect.assertions(5); + const col = new Collator({ + sync: false, + onLoad: col => { + expect(typeof(col) !== "undefined").toBeTruthy(); + + // should compare in English + expect(col.compare("string", "string")).toBe(0); + expect(col.compare("a", "b") < 0).toBeTruthy(); + expect(col.compare("b", "c") < 0).toBeTruthy(); + expect(col.compare("c", "z") < 0).toBeTruthy(); + } + }); + + }); + + test("CollatorAsyncDefaultCase", () => { + expect.assertions(5); + const col = new Collator({ + sync: false, + onLoad: col => { + expect(typeof(col) !== "undefined").toBeTruthy(); + // netscape and ie do not work properly on some platforms + const browser = ilib._getBrowser(); + if ((browser === "firefox" && + navigator && + navigator.userAgent && + navigator.userAgent.indexOf("Android")) || + browser === "ie" || browser === "Edge" || browser === "iOS") { + // should compare lower-case first within a base character + expect(col.compare("a", "A") < 0).toBeTruthy(); + expect(col.compare("b", "B") < 0).toBeTruthy(); + expect(col.compare("a", "Z") < 0).toBeTruthy(); + expect(col.compare("a", "Á") < 0).toBeTruthy(); + } else { + // should compare upper-case first within a base character + expect(col.compare("A", "a") < 0).toBeTruthy(); + expect(col.compare("B", "b") < 0).toBeTruthy(); + expect(col.compare("a", "Z") < 0).toBeTruthy(); + expect(col.compare("A", "a") < 0).toBeTruthy(); // accent is more important than case + } + } + }); + }); + + test("CollatorAsyncGetComparator", () => { + expect.assertions(3); + const col = new Collator({ + sync: false, + onLoad: col => { + expect(typeof(col) !== "undefined").toBeTruthy(); + + // should compare in English + const func = col.getComparator(); + expect(typeof(func) !== "undefined").toBeTruthy(); + expect(typeof(func)).toBe("function"); + } + }); + }); + + + test("CollatorAsyncConstructorJS", () => { + expect.assertions(1); + const col = new Collator({ + useNative: false, + sync: false, + onLoad: col => { + expect(typeof(col) !== "undefined").toBeTruthy(); + } + }); + + }); + + test("CollatorAsyncDefaultJS", () => { + expect.assertions(5); + const col = new Collator({ + useNative: false, + sync: false, + onLoad: col => { + expect(typeof(col) !== "undefined").toBeTruthy(); + + // should compare in English + expect(col.compare("string", "string")).toBe(0); + expect(col.compare("a", "b") < 0).toBeTruthy(); + expect(col.compare("b", "c") < 0).toBeTruthy(); + expect(col.compare("c", "z") < 0).toBeTruthy(); + } + }); + + }); + + + test("CollatorAsyncGetComparatorWorksWithCaseJS", () => { + expect.assertions(6); + const col = new Collator({ + useNative: false, + sync: false, + onLoad: col => { + expect(typeof(col) !== "undefined").toBeTruthy(); + + const func = col.getComparator(); + expect(typeof(func) !== "undefined").toBeTruthy(); + + // should compare upper-case first + expect(func("A", "a") < 0).toBeTruthy(); + expect(func("B", "b") < 0).toBeTruthy(); + expect(func("a", "Z") < 0).toBeTruthy(); + expect(func("A", "a") < 0).toBeTruthy(); + } + }); + + }); + + + test("CollatorAsyncGetSortKeyNative", () => { + if (typeof(Intl) === 'undefined' && Intl) { + return; + } + const col = new Collator({ + sync: false, + onLoad: col => { + expect.assertions(2); + expect(typeof(col) !== "undefined").toBeTruthy(); + + // no sort key available when using native... + expect(col.sortKey("string")).toBe("string"); + } + }); + + }); + + test("CollatorAsyncGetSortKeySimpleUpper", () => { + expect.assertions(2); + const col = new Collator({ + useNative: false, + sync: false, + onLoad: col => { + expect(typeof(col) !== "undefined").toBeTruthy(); + + expect(col.sortKey("ABCDEF")).toBe("4204404604804a04c0"); + } + }); + + }); + + test("CollatorAsyncGetSortKeyMixed", () => { + expect.assertions(2); + const col = new Collator({ + useNative: false, + sync: false, + onLoad: col => { + expect(typeof(col) !== "undefined").toBeTruthy(); + + expect(col.sortKey("String")).toBe("6606826425225c24e2"); + } + }); + + }); + + test("CollatorAsyncWithSort", () => { + expect.assertions(2); + const col = new Collator({ + sync: false, + onLoad: col => { + expect(typeof(col) !== "undefined").toBeTruthy(); + + const input = ["q", "w", "e", "r", "t", "y", "u", "i", "o", "p"]; + + input.sort(col.getComparator()); + + const expected = ["e", "i", "o", "p", "q", "r", "t", "u", "w", "y"]; + + expect(input).toStrictEqual(expected); + } + }); + }); + + test("CollatorAsyncWithSortJS", () => { + expect.assertions(2); + const col = new Collator({ + useNative: false, + sync: false, + onLoad: col => { + expect(typeof(col) !== "undefined").toBeTruthy(); + + const input = ["q", "w", "e", "r", "t", "y", "u", "i", "o", "p"]; + + input.sort(col.getComparator()); + + const expected = ["e", "i", "o", "p", "q", "r", "t", "u", "w", "y"]; + + expect(input).toStrictEqual(expected); + } + }); + }); + + test("CollatorAsyncWithSortUpperFirstJS", () => { + expect.assertions(2); + const col = new Collator({ + upperFirst: true, + useNative: false, + sync: false, + onLoad: col => { + expect(typeof(col) !== "undefined").toBeTruthy(); + + const input = ["q", "I", "e", "r", "T", "U", "i", "E", "o", "p"]; + + input.sort(col.getComparator()); + + const expected = ["E", "e", "I", "i", "o", "p", "q", "r", "T", "U"]; + + expect(input).toStrictEqual(expected); + } + }); + }); + + test("CollatorAsyncGetAvailableScripts", () => { + expect.assertions(1); + expect(Collator.getAvailableScripts()).toStrictEqual(["Latn"]); + }); + + test("CollatorAsyncGetAvailableStyles", () => { + expect.assertions(1); + expect(Collator.getAvailableStyles()).toStrictEqual(["standard"]); + }); + + test("CollatorAsyncPhonebookQuatAE_de", () => { + expect.assertions(5); + const col = new Collator({ + locale: "de-DE", + useNative: false, + sensitivity: "quaternary", + style: "phonebook", + usage: "search", + sync: false, + onLoad: col => { + expect(typeof(col) !== "undefined").toBeTruthy(); + + // aa ae äa äz af + expect(col.compare("aa", "ae") < 0).toBeTruthy(); + expect(col.compare("ae", "äa") < 0).toBeTruthy(); + expect(col.compare("äa", "äz") < 0).toBeTruthy(); + expect(col.compare("äz", "af") < 0).toBeTruthy(); + } + }); + + }); + + test("CollatorAsyncQuat_el", () => { + expect.assertions(91); + const col = new Collator({ + locale: "el-GR", + useNative: false, + sensitivity: "quaternary", + usage: "search", + sync: false, + onLoad: col => { + expect(typeof(col) !== "undefined").toBeTruthy(); + + // Α α Ά Ά ά ά Β β Γ γ Δ δ Ε ε Έ Έ έ έ Ζ ζ Η η Ή Ή ή ή Θ θ Ι ι Ί Ί ί ί Ϊ Ϊ ϊ ϊ ΐ ΐ ί̈ Κ κ Λ λ Μ μ Ν ν Ξ ξ Ο ο Ό Ό ό ό Π π Ρ ρ Σ σ ς Τ τ Υ υ Ύ Ύ ύ ύ Ϋ Ϋ ϋ ϋ ΰ ΰ ύ̈ Φ φ Χ χ Ψ ψ Ω ω Ώ Ώ ώ ώ + + expect(col.compare("Α", "α") < 0).toBeTruthy(); + expect(col.compare("α", "Ά") < 0).toBeTruthy(); + expect(col.compare("Ά", "Ά") < 0).toBeTruthy(); + expect(col.compare("Ά", "ά") < 0).toBeTruthy(); + expect(col.compare("ά", "ά") < 0).toBeTruthy(); + expect(col.compare("ά", "Β") < 0).toBeTruthy(); + expect(col.compare("Β", "β") < 0).toBeTruthy(); + expect(col.compare("β", "Γ") < 0).toBeTruthy(); + expect(col.compare("Γ", "γ") < 0).toBeTruthy(); + expect(col.compare("γ", "Δ") < 0).toBeTruthy(); + expect(col.compare("Δ", "δ") < 0).toBeTruthy(); + expect(col.compare("δ", "Ε") < 0).toBeTruthy(); + expect(col.compare("Ε", "ε") < 0).toBeTruthy(); + expect(col.compare("ε", "Έ") < 0).toBeTruthy(); + expect(col.compare("Έ", "Έ") < 0).toBeTruthy(); + expect(col.compare("Έ", "έ") < 0).toBeTruthy(); + expect(col.compare("έ", "έ") < 0).toBeTruthy(); + expect(col.compare("έ", "Ζ") < 0).toBeTruthy(); + expect(col.compare("Ζ", "ζ") < 0).toBeTruthy(); + expect(col.compare("ζ", "Η") < 0).toBeTruthy(); + expect(col.compare("Η", "η") < 0).toBeTruthy(); + expect(col.compare("η", "Ή") < 0).toBeTruthy(); + expect(col.compare("Ή", "Ή") < 0).toBeTruthy(); + expect(col.compare("Ή", "ή") < 0).toBeTruthy(); + expect(col.compare("ή", "ή") < 0).toBeTruthy(); + expect(col.compare("ή", "Θ") < 0).toBeTruthy(); + expect(col.compare("Θ", "θ") < 0).toBeTruthy(); + expect(col.compare("θ", "Ι") < 0).toBeTruthy(); + expect(col.compare("Ι", "ι") < 0).toBeTruthy(); + expect(col.compare("ι", "Ί") < 0).toBeTruthy(); + expect(col.compare("Ί", "Ί") < 0).toBeTruthy(); + expect(col.compare("Ί", "ί") < 0).toBeTruthy(); + expect(col.compare("ί", "ί") < 0).toBeTruthy(); + expect(col.compare("ί", "Ϊ") < 0).toBeTruthy(); + expect(col.compare("Ϊ", "Ϊ") < 0).toBeTruthy(); + expect(col.compare("Ϊ", "ϊ") < 0).toBeTruthy(); + expect(col.compare("ϊ", "ϊ") < 0).toBeTruthy(); + expect(col.compare("ϊ", "ΐ") < 0).toBeTruthy(); + expect(col.compare("ΐ", "ΐ") < 0).toBeTruthy(); + expect(col.compare("ΐ", "ί̈") < 0).toBeTruthy(); + expect(col.compare("ί̈", "Κ") < 0).toBeTruthy(); + expect(col.compare("Κ", "κ") < 0).toBeTruthy(); + expect(col.compare("κ", "Λ") < 0).toBeTruthy(); + expect(col.compare("Λ", "λ") < 0).toBeTruthy(); + expect(col.compare("λ", "Μ") < 0).toBeTruthy(); + expect(col.compare("Μ", "μ") < 0).toBeTruthy(); + expect(col.compare("μ", "Ν") < 0).toBeTruthy(); + expect(col.compare("Ν", "ν") < 0).toBeTruthy(); + expect(col.compare("ν", "Ξ") < 0).toBeTruthy(); + expect(col.compare("Ξ", "ξ") < 0).toBeTruthy(); + expect(col.compare("ξ", "Ο") < 0).toBeTruthy(); + expect(col.compare("Ο", "ο") < 0).toBeTruthy(); + expect(col.compare("ο", "Ό") < 0).toBeTruthy(); + expect(col.compare("Ό", "Ό") < 0).toBeTruthy(); + expect(col.compare("Ό", "ό") < 0).toBeTruthy(); + expect(col.compare("ό", "ό") < 0).toBeTruthy(); + expect(col.compare("ό", "Π") < 0).toBeTruthy(); + expect(col.compare("Π", "π") < 0).toBeTruthy(); + expect(col.compare("π", "Ρ") < 0).toBeTruthy(); + expect(col.compare("Ρ", "ρ") < 0).toBeTruthy(); + expect(col.compare("ρ", "Σ") < 0).toBeTruthy(); + expect(col.compare("Σ", "σ") < 0).toBeTruthy(); + expect(col.compare("σ", "ς") < 0).toBeTruthy(); + expect(col.compare("ς", "Τ") < 0).toBeTruthy(); + expect(col.compare("Τ", "τ") < 0).toBeTruthy(); + expect(col.compare("τ", "Υ") < 0).toBeTruthy(); + expect(col.compare("Υ", "υ") < 0).toBeTruthy(); + expect(col.compare("υ", "Ύ") < 0).toBeTruthy(); + expect(col.compare("Ύ", "Ύ") < 0).toBeTruthy(); + expect(col.compare("Ύ", "ύ") < 0).toBeTruthy(); + expect(col.compare("ύ", "ύ") < 0).toBeTruthy(); + expect(col.compare("ύ", "Ϋ") < 0).toBeTruthy(); + expect(col.compare("Ϋ", "Ϋ") < 0).toBeTruthy(); + expect(col.compare("Ϋ", "ϋ") < 0).toBeTruthy(); + expect(col.compare("ϋ", "ϋ") < 0).toBeTruthy(); + expect(col.compare("ϋ", "ΰ") < 0).toBeTruthy(); + expect(col.compare("ΰ", "ΰ") < 0).toBeTruthy(); + expect(col.compare("ΰ", "ύ̈") < 0).toBeTruthy(); + expect(col.compare("ύ̈", "Φ") < 0).toBeTruthy(); + expect(col.compare("Φ", "φ") < 0).toBeTruthy(); + expect(col.compare("φ", "Χ") < 0).toBeTruthy(); + expect(col.compare("Χ", "χ") < 0).toBeTruthy(); + expect(col.compare("χ", "Ψ") < 0).toBeTruthy(); + expect(col.compare("Ψ", "ψ") < 0).toBeTruthy(); + expect(col.compare("ψ", "Ω") < 0).toBeTruthy(); + expect(col.compare("Ω", "ω") < 0).toBeTruthy(); + expect(col.compare("ω", "Ώ") < 0).toBeTruthy(); + expect(col.compare("Ώ", "Ώ") < 0).toBeTruthy(); + expect(col.compare("Ώ", "ώ") < 0).toBeTruthy(); + expect(col.compare("ώ", "ώ") < 0).toBeTruthy(); + } + }); + + }); + + test("CollatorAsyncPriL_ko", () => { + expect.assertions(5); + const col = new Collator({ + locale: "ko-KR", + useNative: false, + sensitivity: "primary", + sync: false, + onLoad: col => { + expect(typeof(col) !== "undefined").toBeTruthy(); + + // 가까나다따 + expect(col.compare("가", "까") < 0).toBeTruthy(); + expect(col.compare("까", "나") < 0).toBeTruthy(); + expect(col.compare("나", "다") < 0).toBeTruthy(); + expect(col.compare("다", "따") < 0).toBeTruthy(); + } + }); + + }); + + test("CollatorAsyncQuatHanzi_zh_Hans", () => { + expect.assertions(21); + const col = new Collator({ + locale: "zh-Hans-CN", + useNative: false, + sensitivity: "quaternary", + usage: "search", + sync: false, + onLoad: col => { + // Hanzi are all primary differences from each other + + expect(typeof(col) !== "undefined").toBeTruthy(); + + expect(col.compare("阿", "拜") < 0).toBeTruthy(); + expect(col.compare("拜", "𩑻") < 0).toBeTruthy(); + expect(col.compare("𩑻", "䯋") < 0).toBeTruthy(); + expect(col.compare("䯋", "𧀱") < 0).toBeTruthy(); + expect(col.compare("𧀱", "捶") < 0).toBeTruthy(); + expect(col.compare("捶", "峒") < 0).toBeTruthy(); + expect(col.compare("峒", "㶥") < 0).toBeTruthy(); + expect(col.compare("㶥", "㜳") < 0).toBeTruthy(); + expect(col.compare("㜳", "䌸") < 0).toBeTruthy(); + expect(col.compare("䌸", "䢧") < 0).toBeTruthy(); + expect(col.compare("䢧", "苜") < 0).toBeTruthy(); + expect(col.compare("苜", "肶") < 0).toBeTruthy(); + expect(col.compare("肶", "埁") < 0).toBeTruthy(); + expect(col.compare("埁", "泩") < 0).toBeTruthy(); + expect(col.compare("泩", "窱") < 0).toBeTruthy(); + expect(col.compare("窱", "扤") < 0).toBeTruthy(); + expect(col.compare("扤", "辥") < 0).toBeTruthy(); + expect(col.compare("辥", "䓰") < 0).toBeTruthy(); + expect(col.compare("䓰", "赵") < 0).toBeTruthy(); + expect(col.compare("赵", "蓙") < 0).toBeTruthy(); + } + }); + + }); + + test("CollatorAsyncTraditionalQuatCH_es", () => { + expect.assertions(6); + const col = new Collator({ + locale: "es-ES", + useNative: false, + sensitivity: "quaternary", + style: "traditional", + sync: false, + onLoad: col => { + expect(typeof(col) !== "undefined").toBeTruthy(); + + // a b c ch d + expect(col.compare("a", "b") < 0).toBeTruthy(); + expect(col.compare("b", "c") < 0).toBeTruthy(); + expect(col.compare("ca", "ch") < 0).toBeTruthy(); + expect(col.compare("cz", "ch") < 0).toBeTruthy(); + expect(col.compare("ch", "d") < 0).toBeTruthy(); + } + }); + }); +}); \ No newline at end of file diff --git a/test/collationpromise.test.js b/test/collationpromise.test.js new file mode 100644 index 0000000..50406fc --- /dev/null +++ b/test/collationpromise.test.js @@ -0,0 +1,452 @@ +/* + * collationpromise.test.js - test the Collator object asynchronously + * + * Copyright © 2018, 2022, 2024 JEDLSoft + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import ilib from "../src/ilib.js"; +import Collator from "../src/Collator.js"; + +describe("testcollationpromise", () => { + test("CollatorAsyncConstructorNative", () => { + expect.assertions(1); + return Collator.create({ + sync: false + }).then(col => { + expect(typeof(col) !== "undefined").toBeTruthy(); + }); + + }); + + test("CollatorAsyncDefaultNative", () => { + expect.assertions(5); + return Collator.create({ + sync: false + }).then(col => { + expect(typeof(col) !== "undefined").toBeTruthy(); + + // should compare in English + expect(col.compare("string", "string")).toBe(0); + expect(col.compare("a", "b") < 0).toBeTruthy(); + expect(col.compare("b", "c") < 0).toBeTruthy(); + expect(col.compare("c", "z") < 0).toBeTruthy(); + }); + + }); + + test("CollatorAsyncDefaultCase", () => { + expect.assertions(5); + return Collator.create({ + sync: false + }).then(col => { + expect(typeof(col) !== "undefined").toBeTruthy(); + // netscape and ie do not work properly on some platforms + const browser = ilib._getBrowser(); + if ((browser === "firefox" && + navigator && + navigator.userAgent && + navigator.userAgent.indexOf("Android")) || + browser === "ie" || browser === "Edge" || browser === "iOS") { + // should compare lower-case first within a base character + expect(col.compare("a", "A") < 0).toBeTruthy(); + expect(col.compare("b", "B") < 0).toBeTruthy(); + expect(col.compare("a", "Z") < 0).toBeTruthy(); + expect(col.compare("a", "Á") < 0).toBeTruthy(); + } else { + // should compare upper-case first within a base character + expect(col.compare("A", "a") < 0).toBeTruthy(); + expect(col.compare("B", "b") < 0).toBeTruthy(); + expect(col.compare("a", "Z") < 0).toBeTruthy(); + expect(col.compare("A", "a") < 0).toBeTruthy(); // accent is more important than case + } + }); + }); + + test("CollatorAsyncGetComparator", () => { + expect.assertions(3); + return Collator.create({ + sync: false + }).then(col => { + expect(typeof(col) !== "undefined").toBeTruthy(); + + // should compare in English + const func = col.getComparator(); + expect(typeof(func) !== "undefined").toBeTruthy(); + expect(typeof(func)).toBe("function"); + }); + }); + + + test("CollatorAsyncConstructorJS", () => { + expect.assertions(1); + return Collator.create({ + useNative: false, + sync: false + }).then(col => { + expect(typeof(col) !== "undefined").toBeTruthy(); + }); + + }); + + test("CollatorAsyncDefaultJS", () => { + expect.assertions(5); + return Collator.create({ + useNative: false, + sync: false + }).then(col => { + expect(typeof(col) !== "undefined").toBeTruthy(); + + // should compare in English + expect(col.compare("string", "string")).toBe(0); + expect(col.compare("a", "b") < 0).toBeTruthy(); + expect(col.compare("b", "c") < 0).toBeTruthy(); + expect(col.compare("c", "z") < 0).toBeTruthy(); + }); + + }); + + + test("CollatorAsyncGetComparatorWorksWithCaseJS", () => { + expect.assertions(6); + return Collator.create({ + useNative: false, + sync: false + }).then(col => { + expect(typeof(col) !== "undefined").toBeTruthy(); + + const func = col.getComparator(); + expect(typeof(func) !== "undefined").toBeTruthy(); + + // should compare upper-case first + expect(func("A", "a") < 0).toBeTruthy(); + expect(func("B", "b") < 0).toBeTruthy(); + expect(func("a", "Z") < 0).toBeTruthy(); + expect(func("A", "a") < 0).toBeTruthy(); + }); + + }); + + + test("CollatorAsyncGetSortKeyNative", () => { + if (typeof(Intl) === 'undefined' && Intl) { + return; + } + return Collator.create({ + sync: false + }).then(col => { + expect.assertions(2); + expect(typeof(col) !== "undefined").toBeTruthy(); + + // no sort key available when using native... + expect(col.sortKey("string")).toBe("string"); + }); + + }); + + test("CollatorAsyncGetSortKeySimpleUpper", () => { + expect.assertions(2); + return Collator.create({ + useNative: false, + sync: false + }).then(col => { + expect(typeof(col) !== "undefined").toBeTruthy(); + + expect(col.sortKey("ABCDEF")).toBe("4204404604804a04c0"); + }); + + }); + + test("CollatorAsyncGetSortKeyMixed", () => { + expect.assertions(2); + return Collator.create({ + useNative: false, + sync: false + }).then(col => { + expect(typeof(col) !== "undefined").toBeTruthy(); + + expect(col.sortKey("String")).toBe("6606826425225c24e2"); + }); + + }); + + test("CollatorAsyncWithSort", () => { + expect.assertions(2); + return Collator.create({ + sync: false + }).then(col => { + expect(typeof(col) !== "undefined").toBeTruthy(); + + const input = ["q", "w", "e", "r", "t", "y", "u", "i", "o", "p"]; + + input.sort(col.getComparator()); + + const expected = ["e", "i", "o", "p", "q", "r", "t", "u", "w", "y"]; + + expect(input).toStrictEqual(expected); + }); + }); + + test("CollatorAsyncWithSortJS", () => { + expect.assertions(2); + return Collator.create({ + useNative: false, + sync: false + }).then(col => { + expect(typeof(col) !== "undefined").toBeTruthy(); + + const input = ["q", "w", "e", "r", "t", "y", "u", "i", "o", "p"]; + + input.sort(col.getComparator()); + + const expected = ["e", "i", "o", "p", "q", "r", "t", "u", "w", "y"]; + + expect(input).toStrictEqual(expected); + }); + }); + + test("CollatorAsyncWithSortUpperFirstJS", () => { + expect.assertions(2); + return Collator.create({ + upperFirst: true, + useNative: false, + sync: false + }).then(col => { + expect(typeof(col) !== "undefined").toBeTruthy(); + + const input = ["q", "I", "e", "r", "T", "U", "i", "E", "o", "p"]; + + input.sort(col.getComparator()); + + const expected = ["E", "e", "I", "i", "o", "p", "q", "r", "T", "U"]; + + expect(input).toStrictEqual(expected); + }); + }); + + test("CollatorAsyncGetAvailableScripts", () => { + expect.assertions(1); + expect(Collator.getAvailableScripts()).toStrictEqual(["Latn"]); + }); + + test("CollatorAsyncGetAvailableStyles", () => { + expect.assertions(1); + expect(Collator.getAvailableStyles()).toStrictEqual(["standard"]); + }); + + test("CollatorAsyncPhonebookQuatAE_de", () => { + expect.assertions(5); + return Collator.create({ + locale: "de-DE", + useNative: false, + sensitivity: "quaternary", + style: "phonebook", + usage: "search", + sync: false + }).then(col => { + expect(typeof(col) !== "undefined").toBeTruthy(); + + // aa ae äa äz af + expect(col.compare("aa", "ae") < 0).toBeTruthy(); + expect(col.compare("ae", "äa") < 0).toBeTruthy(); + expect(col.compare("äa", "äz") < 0).toBeTruthy(); + expect(col.compare("äz", "af") < 0).toBeTruthy(); + }); + + }); + + test("CollatorAsyncQuat_el", () => { + expect.assertions(91); + return Collator.create({ + locale: "el-GR", + useNative: false, + sensitivity: "quaternary", + usage: "search", + sync: false + }).then(col => { + expect(typeof(col) !== "undefined").toBeTruthy(); + + // Α α Ά Ά ά ά Β β Γ γ Δ δ Ε ε Έ Έ έ έ Ζ ζ Η η Ή Ή ή ή Θ θ Ι ι Ί Ί ί ί Ϊ Ϊ ϊ ϊ ΐ ΐ ί̈ Κ κ Λ λ Μ μ Ν ν Ξ ξ Ο ο Ό Ό ό ό Π π Ρ ρ Σ σ ς Τ τ Υ υ Ύ Ύ ύ ύ Ϋ Ϋ ϋ ϋ ΰ ΰ ύ̈ Φ φ Χ χ Ψ ψ Ω ω Ώ Ώ ώ ώ + + expect(col.compare("Α", "α") < 0).toBeTruthy(); + expect(col.compare("α", "Ά") < 0).toBeTruthy(); + expect(col.compare("Ά", "Ά") < 0).toBeTruthy(); + expect(col.compare("Ά", "ά") < 0).toBeTruthy(); + expect(col.compare("ά", "ά") < 0).toBeTruthy(); + expect(col.compare("ά", "Β") < 0).toBeTruthy(); + expect(col.compare("Β", "β") < 0).toBeTruthy(); + expect(col.compare("β", "Γ") < 0).toBeTruthy(); + expect(col.compare("Γ", "γ") < 0).toBeTruthy(); + expect(col.compare("γ", "Δ") < 0).toBeTruthy(); + expect(col.compare("Δ", "δ") < 0).toBeTruthy(); + expect(col.compare("δ", "Ε") < 0).toBeTruthy(); + expect(col.compare("Ε", "ε") < 0).toBeTruthy(); + expect(col.compare("ε", "Έ") < 0).toBeTruthy(); + expect(col.compare("Έ", "Έ") < 0).toBeTruthy(); + expect(col.compare("Έ", "έ") < 0).toBeTruthy(); + expect(col.compare("έ", "έ") < 0).toBeTruthy(); + expect(col.compare("έ", "Ζ") < 0).toBeTruthy(); + expect(col.compare("Ζ", "ζ") < 0).toBeTruthy(); + expect(col.compare("ζ", "Η") < 0).toBeTruthy(); + expect(col.compare("Η", "η") < 0).toBeTruthy(); + expect(col.compare("η", "Ή") < 0).toBeTruthy(); + expect(col.compare("Ή", "Ή") < 0).toBeTruthy(); + expect(col.compare("Ή", "ή") < 0).toBeTruthy(); + expect(col.compare("ή", "ή") < 0).toBeTruthy(); + expect(col.compare("ή", "Θ") < 0).toBeTruthy(); + expect(col.compare("Θ", "θ") < 0).toBeTruthy(); + expect(col.compare("θ", "Ι") < 0).toBeTruthy(); + expect(col.compare("Ι", "ι") < 0).toBeTruthy(); + expect(col.compare("ι", "Ί") < 0).toBeTruthy(); + expect(col.compare("Ί", "Ί") < 0).toBeTruthy(); + expect(col.compare("Ί", "ί") < 0).toBeTruthy(); + expect(col.compare("ί", "ί") < 0).toBeTruthy(); + expect(col.compare("ί", "Ϊ") < 0).toBeTruthy(); + expect(col.compare("Ϊ", "Ϊ") < 0).toBeTruthy(); + expect(col.compare("Ϊ", "ϊ") < 0).toBeTruthy(); + expect(col.compare("ϊ", "ϊ") < 0).toBeTruthy(); + expect(col.compare("ϊ", "ΐ") < 0).toBeTruthy(); + expect(col.compare("ΐ", "ΐ") < 0).toBeTruthy(); + expect(col.compare("ΐ", "ί̈") < 0).toBeTruthy(); + expect(col.compare("ί̈", "Κ") < 0).toBeTruthy(); + expect(col.compare("Κ", "κ") < 0).toBeTruthy(); + expect(col.compare("κ", "Λ") < 0).toBeTruthy(); + expect(col.compare("Λ", "λ") < 0).toBeTruthy(); + expect(col.compare("λ", "Μ") < 0).toBeTruthy(); + expect(col.compare("Μ", "μ") < 0).toBeTruthy(); + expect(col.compare("μ", "Ν") < 0).toBeTruthy(); + expect(col.compare("Ν", "ν") < 0).toBeTruthy(); + expect(col.compare("ν", "Ξ") < 0).toBeTruthy(); + expect(col.compare("Ξ", "ξ") < 0).toBeTruthy(); + expect(col.compare("ξ", "Ο") < 0).toBeTruthy(); + expect(col.compare("Ο", "ο") < 0).toBeTruthy(); + expect(col.compare("ο", "Ό") < 0).toBeTruthy(); + expect(col.compare("Ό", "Ό") < 0).toBeTruthy(); + expect(col.compare("Ό", "ό") < 0).toBeTruthy(); + expect(col.compare("ό", "ό") < 0).toBeTruthy(); + expect(col.compare("ό", "Π") < 0).toBeTruthy(); + expect(col.compare("Π", "π") < 0).toBeTruthy(); + expect(col.compare("π", "Ρ") < 0).toBeTruthy(); + expect(col.compare("Ρ", "ρ") < 0).toBeTruthy(); + expect(col.compare("ρ", "Σ") < 0).toBeTruthy(); + expect(col.compare("Σ", "σ") < 0).toBeTruthy(); + expect(col.compare("σ", "ς") < 0).toBeTruthy(); + expect(col.compare("ς", "Τ") < 0).toBeTruthy(); + expect(col.compare("Τ", "τ") < 0).toBeTruthy(); + expect(col.compare("τ", "Υ") < 0).toBeTruthy(); + expect(col.compare("Υ", "υ") < 0).toBeTruthy(); + expect(col.compare("υ", "Ύ") < 0).toBeTruthy(); + expect(col.compare("Ύ", "Ύ") < 0).toBeTruthy(); + expect(col.compare("Ύ", "ύ") < 0).toBeTruthy(); + expect(col.compare("ύ", "ύ") < 0).toBeTruthy(); + expect(col.compare("ύ", "Ϋ") < 0).toBeTruthy(); + expect(col.compare("Ϋ", "Ϋ") < 0).toBeTruthy(); + expect(col.compare("Ϋ", "ϋ") < 0).toBeTruthy(); + expect(col.compare("ϋ", "ϋ") < 0).toBeTruthy(); + expect(col.compare("ϋ", "ΰ") < 0).toBeTruthy(); + expect(col.compare("ΰ", "ΰ") < 0).toBeTruthy(); + expect(col.compare("ΰ", "ύ̈") < 0).toBeTruthy(); + expect(col.compare("ύ̈", "Φ") < 0).toBeTruthy(); + expect(col.compare("Φ", "φ") < 0).toBeTruthy(); + expect(col.compare("φ", "Χ") < 0).toBeTruthy(); + expect(col.compare("Χ", "χ") < 0).toBeTruthy(); + expect(col.compare("χ", "Ψ") < 0).toBeTruthy(); + expect(col.compare("Ψ", "ψ") < 0).toBeTruthy(); + expect(col.compare("ψ", "Ω") < 0).toBeTruthy(); + expect(col.compare("Ω", "ω") < 0).toBeTruthy(); + expect(col.compare("ω", "Ώ") < 0).toBeTruthy(); + expect(col.compare("Ώ", "Ώ") < 0).toBeTruthy(); + expect(col.compare("Ώ", "ώ") < 0).toBeTruthy(); + expect(col.compare("ώ", "ώ") < 0).toBeTruthy(); + }); + + }); + + test("CollatorAsyncPriL_ko", () => { + expect.assertions(5); + return Collator.create({ + locale: "ko-KR", + useNative: false, + sensitivity: "primary", + sync: false + }).then(col => { + expect(typeof(col) !== "undefined").toBeTruthy(); + + // 가까나다따 + expect(col.compare("가", "까") < 0).toBeTruthy(); + expect(col.compare("까", "나") < 0).toBeTruthy(); + expect(col.compare("나", "다") < 0).toBeTruthy(); + expect(col.compare("다", "따") < 0).toBeTruthy(); + }); + + }); + + test("CollatorAsyncQuatHanzi_zh_Hans", () => { + expect.assertions(21); + return Collator.create({ + locale: "zh-Hans-CN", + useNative: false, + sensitivity: "quaternary", + usage: "search", + sync: false + }).then(col => { + // Hanzi are all primary differences from each other + + expect(typeof(col) !== "undefined").toBeTruthy(); + + expect(col.compare("阿", "拜") < 0).toBeTruthy(); + expect(col.compare("拜", "𩑻") < 0).toBeTruthy(); + expect(col.compare("𩑻", "䯋") < 0).toBeTruthy(); + expect(col.compare("䯋", "𧀱") < 0).toBeTruthy(); + expect(col.compare("𧀱", "捶") < 0).toBeTruthy(); + expect(col.compare("捶", "峒") < 0).toBeTruthy(); + expect(col.compare("峒", "㶥") < 0).toBeTruthy(); + expect(col.compare("㶥", "㜳") < 0).toBeTruthy(); + expect(col.compare("㜳", "䌸") < 0).toBeTruthy(); + expect(col.compare("䌸", "䢧") < 0).toBeTruthy(); + expect(col.compare("䢧", "苜") < 0).toBeTruthy(); + expect(col.compare("苜", "肶") < 0).toBeTruthy(); + expect(col.compare("肶", "埁") < 0).toBeTruthy(); + expect(col.compare("埁", "泩") < 0).toBeTruthy(); + expect(col.compare("泩", "窱") < 0).toBeTruthy(); + expect(col.compare("窱", "扤") < 0).toBeTruthy(); + expect(col.compare("扤", "辥") < 0).toBeTruthy(); + expect(col.compare("辥", "䓰") < 0).toBeTruthy(); + expect(col.compare("䓰", "赵") < 0).toBeTruthy(); + expect(col.compare("赵", "蓙") < 0).toBeTruthy(); + }); + + }); + + test("CollatorAsyncTraditionalQuatCH_es", () => { + expect.assertions(6); + return Collator.create({ + locale: "es-ES", + useNative: false, + sensitivity: "quaternary", + style: "traditional", + sync: false + }).then(col => { + expect(typeof(col) !== "undefined").toBeTruthy(); + + // a b c ch d + expect(col.compare("a", "b") < 0).toBeTruthy(); + expect(col.compare("b", "c") < 0).toBeTruthy(); + expect(col.compare("ca", "ch") < 0).toBeTruthy(); + expect(col.compare("cz", "ch") < 0).toBeTruthy(); + expect(col.compare("ch", "d") < 0).toBeTruthy(); + }); + }); +}); \ No newline at end of file diff --git a/test/countryasync.test.js b/test/countryasync.test.js new file mode 100644 index 0000000..5c02b06 --- /dev/null +++ b/test/countryasync.test.js @@ -0,0 +1,95 @@ +/* + * countryasync.test.js - test the country routines + * + * Copyright © 2018, 2024 LGE + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import Country from "../src/Country.js"; + +describe("testcountryasync", () => { + test("CountryAsyncConstructorEmpty", () => { + expect.assertions(1); + new Country({ + sync: false, + onLoad: ctry => { + expect(ctry !== null).toBeTruthy(); + } + }); + }); + + test("CountryAsyncLocale1", () => { + expect.assertions(4); + new Country({ + locale: "ko-KR", + sync: false, + onLoad: ctry => { + expect(ctry !== null).toBeTruthy(); + + expect(ctry.getName("CD")).toBe("콩고-킨샤사"); + expect(ctry.getCode("콩고-킨샤사")).toBe("CD"); + const locale = ctry.getLocale(); + expect(locale.toString()).toBe("ko-KR"); + } + }); + }); + + test("CountryAsyncLocale2", () => { + expect.assertions(4); + new Country({ + locale: "en-US", + sync: false, + onLoad: ctry => { + expect(ctry !== null).toBeTruthy(); + + expect(ctry.getName("CD")).toBe("Congo - Kinshasa"); + expect(ctry.getCode("Congo - Kinshasa")).toBe("CD"); + const locale = ctry.getLocale(); + expect(locale.toString()).toBe("en-US"); + } + }); + }); + + test("CountryAsyncGetByCodeUnknown", () => { + new Country({ + locale: "en-US", + sync: false, + onLoad: ctry => { + try { + ctry.getName('xxx'); + test.fail(); + } catch (e) { + expect(e).toBe("Country xxx is unknown"); + } + } + }); + }); + + test("CountryAsyncJP", () => { + expect.assertions(4); + new Country({ + locale: "ja-JP", + sync: false, + onLoad: ctry => { + expect(ctry !== null).toBeTruthy(); + + expect(ctry.getName("JP")).toBe("日本"); + expect(ctry.getCode("日本")).toBe("JP"); + const locale = ctry.getLocale(); + expect(locale.toString()).toBe("ja-JP"); + } + }); + }); +}); diff --git a/test/countrypromise.test.js b/test/countrypromise.test.js new file mode 100644 index 0000000..58d1d86 --- /dev/null +++ b/test/countrypromise.test.js @@ -0,0 +1,90 @@ +/* + * countryasync.test.js - test the country routines + * + * Copyright © 2018, 2024 LGE + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import Country from "../src/Country.js"; + +describe("testcountrypromise", () => { + test("CountryAsyncConstructorEmpty", () => { + expect.assertions(1); + return Country.create({ + sync: false + }).then(ctry => { + expect(ctry !== null).toBeTruthy(); + }); + }); + + test("CountryAsyncLocale1", () => { + expect.assertions(4); + return Country.create({ + locale: "ko-KR", + sync: false + }).then(ctry => { + expect(ctry !== null).toBeTruthy(); + + expect(ctry.getName("CD")).toBe("콩고-킨샤사"); + expect(ctry.getCode("콩고-킨샤사")).toBe("CD"); + const locale = ctry.getLocale(); + expect(locale.toString()).toBe("ko-KR"); + }); + }); + + test("CountryAsyncLocale2", () => { + expect.assertions(4); + return Country.create({ + locale: "en-US", + sync: false + }).then(ctry => { + expect(ctry !== null).toBeTruthy(); + + expect(ctry.getName("CD")).toBe("Congo - Kinshasa"); + expect(ctry.getCode("Congo - Kinshasa")).toBe("CD"); + const locale = ctry.getLocale(); + expect(locale.toString()).toBe("en-US"); + }); + }); + + test("CountryAsyncGetByCodeUnknown", () => { + return Country.create({ + locale: "en-US", + sync: false + }).then(ctry => { + try { + ctry.getName('xxx'); + test.fail(); + } catch (e) { + expect(e).toBe("Country xxx is unknown"); + } + }); + }); + + test("CountryAsyncJP", () => { + expect.assertions(4); + return Country.create({ + locale: "ja-JP", + sync: false + }).then(ctry => { + expect(ctry !== null).toBeTruthy(); + + expect(ctry.getName("JP")).toBe("日本"); + expect(ctry.getCode("日本")).toBe("JP"); + const locale = ctry.getLocale(); + expect(locale.toString()).toBe("ja-JP"); + }); + }); +}); diff --git a/test/ctype.test.js b/test/ctype.test.js new file mode 100644 index 0000000..a94a6b7 --- /dev/null +++ b/test/ctype.test.js @@ -0,0 +1,334 @@ +/* + * ctype.test.js - test the character type information functions + * + * Copyright © 2024 JEDLSoft + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import ilib from '../src/ilib.js'; +import isXdigit from "../src/isXdigit.js"; +import isUpper from "../src/isUpper.js"; +import isSpace from "../src/isSpace.js"; +import isScript from "../src/isScript.js"; +import isPunct from "../src/isPunct.js"; +import isPrint from "../src/isPrint.js"; +import isLower from "../src/isLower.js"; +import isIdeo from "../src/isIdeo.js"; +import isGraph from "../src/isGraph.js"; +import isDigit from "../src/isDigit.js"; +import isCntrl from "../src/isCntrl.js"; +import isBlank from "../src/isBlank.js"; +import isAscii from "../src/isAscii.js"; +import isAlpha from "../src/isAlpha.js"; +import isAlnum from "../src/isAlnum.js"; +import CType from "../src/CType.js"; + +describe("test ctype synchronously", () => { + beforeEach(() => { + ilib.clearCache(); + ilib.data.ctype = null; + ilib.data.ctype_c = null; + ilib.data.ctype_l = null; + ilib.data.ctype_m = null; + ilib.data.ctype_p = null; + ilib.data.ctype_z = null; + ilib.data.scriptToRange = null; + }); + + test("CType functions do not work if not initialized first", () => { + expect.assertions(2); + + expect(isIdeo('碗')).toBeFalsy(); + expect(isIdeo('人')).toBeFalsy(); + }); + + test("IsAlphaTrue", () => { + expect.assertions(5); + isAlpha._init(); + expect(isAlpha('a')).toBeTruthy(); + expect(isAlpha('m')).toBeTruthy(); + expect(isAlpha('z')).toBeTruthy(); + expect(isAlpha('A')).toBeTruthy(); + expect(isAlpha('Q')).toBeTruthy(); + }); + + test("IsLowerTrue", () => { + expect.assertions(11); + + isLower._init(); + expect(isLower('a')).toBeTruthy(); + expect(isLower('m')).toBeTruthy(); + expect(isLower('щ')).toBeTruthy(); + expect(isLower('λ')).toBeTruthy(); + expect(!isLower(' ')).toBeTruthy(); + expect(!isLower('$')).toBeTruthy(); + expect(!isLower('A')).toBeTruthy(); + expect(!isLower('M')).toBeTruthy(); + expect(!isLower('0')).toBeTruthy(); + expect(!isLower('Щ')).toBeTruthy(); + expect(!isLower('Λ')).toBeTruthy(); + }); + + test("IsUpperTrue", () => { + expect.assertions(11); + isUpper._init(); + expect(isUpper('A')).toBeTruthy(); + expect(isUpper('M')).toBeTruthy(); + expect(isUpper('Щ')).toBeTruthy(); + expect(isUpper('Λ')).toBeTruthy(); + expect(!isUpper(' ')).toBeTruthy(); + expect(!isUpper('$')).toBeTruthy(); + expect(!isUpper('a')).toBeTruthy(); + expect(!isUpper('m')).toBeTruthy(); + expect(!isUpper('щ')).toBeTruthy(); + expect(!isUpper('λ')).toBeTruthy(); + expect(!isUpper('0')).toBeTruthy(); + }); + + test("IsPrintTrue", () => { + expect.assertions(7); + isPrint._init(); + expect(isPrint(' ')).toBeTruthy(); + expect(isPrint('A')).toBeTruthy(); + expect(isPrint('M')).toBeTruthy(); + expect(isPrint('Щ')).toBeTruthy(); + expect(isPrint('Λ')).toBeTruthy(); + expect(!isPrint('\u0001')).toBeTruthy(); + expect(!isPrint('\u0085')).toBeTruthy(); + }); + + test("IsAsciiTrue", () => { + expect.assertions(13); + isAscii._init(); + expect(isAscii('a')).toBeTruthy(); + expect(isAscii('m')).toBeTruthy(); + expect(isAscii('z')).toBeTruthy(); + expect(isAscii('A')).toBeTruthy(); + expect(isAscii('Q')).toBeTruthy(); + expect(isAscii(' ')).toBeTruthy(); + expect(isAscii('$')).toBeTruthy(); + expect(isAscii('0')).toBeTruthy(); + expect(isAscii('1')).toBeTruthy(); + expect(isAscii('8')).toBeTruthy(); + expect(!isAscii('ü')).toBeTruthy(); + expect(!isAscii('ó')).toBeTruthy(); + expect(!isAscii('Д')).toBeTruthy(); + }); + + test("IsBlankTrue", () => { + expect.assertions(13); + isBlank._init(); + expect(isBlank(' ')).toBeTruthy(); + expect(!isBlank('a')).toBeTruthy(); + expect(!isBlank('m')).toBeTruthy(); + expect(!isBlank('z')).toBeTruthy(); + expect(!isBlank('A')).toBeTruthy(); + expect(!isBlank('Q')).toBeTruthy(); + expect(!isBlank('$')).toBeTruthy(); + expect(!isBlank('0')).toBeTruthy(); + expect(!isBlank('1')).toBeTruthy(); + expect(!isBlank('8')).toBeTruthy(); + expect(!isBlank('ü')).toBeTruthy(); + expect(!isBlank('ó')).toBeTruthy(); + expect(!isBlank('Д')).toBeTruthy(); + }); + + test("IsSpaceTrue", () => { + expect.assertions(11); + isSpace._init(); + expect(isSpace(' ')).toBeTruthy(); + expect(isSpace('\t')).toBeTruthy(); + expect(isSpace('\n')).toBeTruthy(); + expect(isSpace('\u2000')).toBeTruthy(); + expect(!isSpace('a')).toBeTruthy(); + expect(!isSpace('A')).toBeTruthy(); + expect(!isSpace('$')).toBeTruthy(); + expect(!isSpace('0')).toBeTruthy(); + expect(!isSpace('ü')).toBeTruthy(); + expect(!isSpace('ó')).toBeTruthy(); + expect(!isSpace('Д')).toBeTruthy(); + }); + + test("IsPunctTrue", () => { + expect.assertions(17); + isPunct._init(); + expect(isPunct('?')).toBeTruthy(); + expect(isPunct('.')).toBeTruthy(); + expect(isPunct('\u2010')).toBeTruthy(); // hyphen + expect(isPunct('\u037E')).toBeTruthy(); // Greek question mark + expect(isPunct('\u3001')).toBeTruthy(); // ideographic comma + expect(!isPunct('a')).toBeTruthy(); + expect(!isPunct('m')).toBeTruthy(); + expect(!isPunct('z')).toBeTruthy(); + expect(!isPunct('A')).toBeTruthy(); + expect(!isPunct('Q')).toBeTruthy(); + expect(!isPunct(' ')).toBeTruthy(); + expect(!isPunct('0')).toBeTruthy(); + expect(!isPunct('1')).toBeTruthy(); + expect(!isPunct('8')).toBeTruthy(); + expect(!isPunct('ü')).toBeTruthy(); + expect(!isPunct('ó')).toBeTruthy(); + expect(!isPunct('Д')).toBeTruthy(); + }); + + test("IsIdeoTrue", () => { + expect.assertions(10); + + isIdeo._init(); + expect(isIdeo('碗')).toBeTruthy(); + expect(isIdeo('人')).toBeTruthy(); + expect(!isIdeo(' ')).toBeTruthy(); + expect(!isIdeo('$')).toBeTruthy(); + expect(!isIdeo('a')).toBeTruthy(); + expect(!isIdeo('m')).toBeTruthy(); + expect(!isIdeo('z')).toBeTruthy(); + expect(!isIdeo('0')).toBeTruthy(); + expect(!isIdeo('1')).toBeTruthy(); + expect(!isIdeo('8')).toBeTruthy(); + }); + + test("IsCntrlTrue", () => { + expect.assertions(10); + isCntrl._init(); + expect(isCntrl('\u0001')).toBeTruthy(); + expect(isCntrl('\u0085')).toBeTruthy(); + expect(!isCntrl(' ')).toBeTruthy(); + expect(!isCntrl('$')).toBeTruthy(); + expect(!isCntrl('a')).toBeTruthy(); + expect(!isCntrl('m')).toBeTruthy(); + expect(!isCntrl('z')).toBeTruthy(); + expect(!isCntrl('0')).toBeTruthy(); + expect(!isCntrl('1')).toBeTruthy(); + expect(!isCntrl('8')).toBeTruthy(); + }); + + test("IsDigitTrue", () => { + expect.assertions(20); + isDigit._init(); + expect(isDigit('0')).toBeTruthy(); + expect(isDigit('1')).toBeTruthy(); + expect(isDigit('2')).toBeTruthy(); + expect(isDigit('3')).toBeTruthy(); + expect(isDigit('4')).toBeTruthy(); + expect(isDigit('5')).toBeTruthy(); + expect(isDigit('6')).toBeTruthy(); + expect(isDigit('7')).toBeTruthy(); + expect(isDigit('8')).toBeTruthy(); + expect(isDigit('9')).toBeTruthy(); + expect(!isDigit(' ')).toBeTruthy(); + expect(!isDigit('a')).toBeTruthy(); + expect(!isDigit('m')).toBeTruthy(); + expect(!isDigit('z')).toBeTruthy(); + expect(!isDigit('A')).toBeTruthy(); + expect(!isDigit('Q')).toBeTruthy(); + expect(!isDigit('$')).toBeTruthy(); + expect(!isDigit('ü')).toBeTruthy(); + expect(!isDigit('ó')).toBeTruthy(); + expect(!isDigit('Д')).toBeTruthy(); + }); + test("IsXdigitTrue", () => { + expect.assertions(32); + isXdigit._init(); + expect(isXdigit('0')).toBeTruthy(); + expect(isXdigit('1')).toBeTruthy(); + expect(isXdigit('2')).toBeTruthy(); + expect(isXdigit('3')).toBeTruthy(); + expect(isXdigit('4')).toBeTruthy(); + expect(isXdigit('5')).toBeTruthy(); + expect(isXdigit('6')).toBeTruthy(); + expect(isXdigit('7')).toBeTruthy(); + expect(isXdigit('8')).toBeTruthy(); + expect(isXdigit('9')).toBeTruthy(); + expect(isXdigit('A')).toBeTruthy(); + expect(isXdigit('B')).toBeTruthy(); + expect(isXdigit('C')).toBeTruthy(); + expect(isXdigit('D')).toBeTruthy(); + expect(isXdigit('E')).toBeTruthy(); + expect(isXdigit('F')).toBeTruthy(); + expect(isXdigit('a')).toBeTruthy(); + expect(isXdigit('b')).toBeTruthy(); + expect(isXdigit('c')).toBeTruthy(); + expect(isXdigit('d')).toBeTruthy(); + expect(isXdigit('e')).toBeTruthy(); + expect(isXdigit('f')).toBeTruthy(); + expect(!isXdigit('G')).toBeTruthy(); + expect(!isXdigit('g')).toBeTruthy(); + expect(!isXdigit(' ')).toBeTruthy(); + expect(!isXdigit('m')).toBeTruthy(); + expect(!isXdigit('z')).toBeTruthy(); + expect(!isXdigit('Q')).toBeTruthy(); + expect(!isXdigit('$')).toBeTruthy(); + expect(!isXdigit('ü')).toBeTruthy(); + expect(!isXdigit('ó')).toBeTruthy(); + expect(!isXdigit('Д')).toBeTruthy(); + }); + + test("WithinRangeTrue", () => { + expect.assertions(5); + CType._init(); + expect(CType.withinRange('a', 'ascii')).toBeTruthy(); + expect(!CType.withinRange('\u2000a', 'ascii')).toBeTruthy(); + expect(CType.withinRange('a', 'ASCII')).toBeTruthy(); + expect(!CType.withinRange('G', 'arabic')).toBeTruthy(); + expect(CType.withinRange('a', 'latin')).toBeTruthy(); + }); + + test("IsScriptTrue", () => { + expect.assertions(16); + isScript._init(); + expect(isScript("a", "Latn")).toBeTruthy(); + expect(isScript("Д", "Cyrl")).toBeTruthy(); + expect(isScript("ώ", "Grek")).toBeTruthy(); + expect(isScript("귋", "Hang")).toBeTruthy(); + expect(isScript("㜴", "Hani")).toBeTruthy(); + expect(isScript("ש", "Hebr")).toBeTruthy(); + expect(isScript("ش", "Arab")).toBeTruthy(); + expect(isScript("झ", "Deva")).toBeTruthy(); + expect(!isScript("a", "Cyrl")).toBeTruthy(); + expect(!isScript("Д", "Grek")).toBeTruthy(); + expect(!isScript("ώ", "Hang")).toBeTruthy(); + expect(!isScript("귋", "Hani")).toBeTruthy(); + expect(!isScript("㜴", "Hebr")).toBeTruthy(); + expect(!isScript("ש", "Arab")).toBeTruthy(); + expect(!isScript("ش", "Deva")).toBeTruthy(); + expect(!isScript("झ", "Latn")).toBeTruthy(); + }); + + test("IsAlnum", () => { + expect.assertions(11); + isAlnum._init(); + expect(isAlnum('a')).toBeTruthy(); + expect(isAlnum('m')).toBeTruthy(); + expect(isAlnum('z')).toBeTruthy(); + expect(isAlnum('A')).toBeTruthy(); + expect(isAlnum('Q')).toBeTruthy(); + expect(isAlnum('0')).toBeTruthy(); + expect(isAlnum('1')).toBeTruthy(); + expect(isAlnum('8')).toBeTruthy(); + expect(isAlnum('Ꞛ')).toBeTruthy(); + expect(!isAlnum(' ')).toBeTruthy(); + expect(!isAlnum('$')).toBeTruthy(); + }); + + test("IsGraphTrue", () => { + expect.assertions(5); + isGraph._init(); + expect(isGraph('A')).toBeTruthy(); + expect(isGraph('Q')).toBeTruthy(); + expect(isGraph('碗')).toBeTruthy(); + expect(!isGraph(' ')).toBeTruthy(); + expect(!isGraph('\u0002')).toBeTruthy(); + }); +}); diff --git a/test/ctypeasync.test.js b/test/ctypeasync.test.js new file mode 100644 index 0000000..51f6998 --- /dev/null +++ b/test/ctypeasync.test.js @@ -0,0 +1,341 @@ +/* + * ctypeasync.test.js - test the character type information functions + * + * Copyright © 2018, 2022, 2024 JEDLSoft + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import ilib from '../src/ilib.js'; +import isXdigit from "../src/isXdigit.js"; +import isUpper from "../src/isUpper.js"; +import isSpace from "../src/isSpace.js"; +import isScript from "../src/isScript.js"; +import isPunct from "../src/isPunct.js"; +import isPrint from "../src/isPrint.js"; +import isLower from "../src/isLower.js"; +import isIdeo from "../src/isIdeo.js"; +import isGraph from "../src/isGraph.js"; +import isDigit from "../src/isDigit.js"; +import isCntrl from "../src/isCntrl.js"; +import isBlank from "../src/isBlank.js"; +import isAscii from "../src/isAscii.js"; +import isAlpha from "../src/isAlpha.js"; +import isAlnum from "../src/isAlnum.js"; +import CType from "../src/CType.js"; + +describe("testctypeasync", () => { + beforeEach(() => { + ilib.clearCache(); + ilib.data.ctype = null; + ilib.data.ctype_c = null; + ilib.data.ctype_l = null; + ilib.data.ctype_m = null; + ilib.data.ctype_p = null; + ilib.data.ctype_z = null; + ilib.data.scriptToRange = null; + }); + + test("IsAlphaTrue", () => { + expect.assertions(5); + isAlpha._init(false, undefined,() => { + expect(isAlpha('a')).toBeTruthy(); + expect(isAlpha('m')).toBeTruthy(); + expect(isAlpha('z')).toBeTruthy(); + expect(isAlpha('A')).toBeTruthy(); + expect(isAlpha('Q')).toBeTruthy(); + }); + }); + + test("IsLowerTrue", () => { + expect.assertions(11); + isLower._init(false, undefined,() => { + expect(isLower('a')).toBeTruthy(); + expect(isLower('m')).toBeTruthy(); + expect(isLower('щ')).toBeTruthy(); + expect(isLower('λ')).toBeTruthy(); + expect(!isLower(' ')).toBeTruthy(); + expect(!isLower('$')).toBeTruthy(); + expect(!isLower('A')).toBeTruthy(); + expect(!isLower('M')).toBeTruthy(); + expect(!isLower('0')).toBeTruthy(); + expect(!isLower('Щ')).toBeTruthy(); + expect(!isLower('Λ')).toBeTruthy(); + }); + }); + + test("IsUpperTrue", () => { + expect.assertions(11); + isUpper._init(false, undefined,() => { + expect(isUpper('A')).toBeTruthy(); + expect(isUpper('M')).toBeTruthy(); + expect(isUpper('Щ')).toBeTruthy(); + expect(isUpper('Λ')).toBeTruthy(); + expect(!isUpper(' ')).toBeTruthy(); + expect(!isUpper('$')).toBeTruthy(); + expect(!isUpper('a')).toBeTruthy(); + expect(!isUpper('m')).toBeTruthy(); + expect(!isUpper('щ')).toBeTruthy(); + expect(!isUpper('λ')).toBeTruthy(); + expect(!isUpper('0')).toBeTruthy(); + }); + }); + + test("IsPrintTrue", () => { + expect.assertions(7); + isPrint._init(false, undefined,() => { + expect(isPrint(' ')).toBeTruthy(); + expect(isPrint('A')).toBeTruthy(); + expect(isPrint('M')).toBeTruthy(); + expect(isPrint('Щ')).toBeTruthy(); + expect(isPrint('Λ')).toBeTruthy(); + expect(!isPrint('\u0001')).toBeTruthy(); + expect(!isPrint('\u0085')).toBeTruthy(); + }); + }); + + test("IsAsciiTrue", () => { + expect.assertions(13); + isAscii._init(false, undefined,() => { + expect(isAscii('a')).toBeTruthy(); + expect(isAscii('m')).toBeTruthy(); + expect(isAscii('z')).toBeTruthy(); + expect(isAscii('A')).toBeTruthy(); + expect(isAscii('Q')).toBeTruthy(); + expect(isAscii(' ')).toBeTruthy(); + expect(isAscii('$')).toBeTruthy(); + expect(isAscii('0')).toBeTruthy(); + expect(isAscii('1')).toBeTruthy(); + expect(isAscii('8')).toBeTruthy(); + expect(!isAscii('ü')).toBeTruthy(); + expect(!isAscii('ó')).toBeTruthy(); + expect(!isAscii('Д')).toBeTruthy(); + }); + }); + + test("IsBlankTrue", () => { + expect.assertions(13); + isBlank._init(false, undefined,() => { + expect(isBlank(' ')).toBeTruthy(); + expect(!isBlank('a')).toBeTruthy(); + expect(!isBlank('m')).toBeTruthy(); + expect(!isBlank('z')).toBeTruthy(); + expect(!isBlank('A')).toBeTruthy(); + expect(!isBlank('Q')).toBeTruthy(); + expect(!isBlank('$')).toBeTruthy(); + expect(!isBlank('0')).toBeTruthy(); + expect(!isBlank('1')).toBeTruthy(); + expect(!isBlank('8')).toBeTruthy(); + expect(!isBlank('ü')).toBeTruthy(); + expect(!isBlank('ó')).toBeTruthy(); + expect(!isBlank('Д')).toBeTruthy(); + }); + }); + + test("IsSpaceTrue", () => { + expect.assertions(11); + isSpace._init(false, undefined,() => { + expect(isSpace(' ')).toBeTruthy(); + expect(isSpace('\t')).toBeTruthy(); + expect(isSpace('\n')).toBeTruthy(); + expect(isSpace('\u2000')).toBeTruthy(); + expect(!isSpace('a')).toBeTruthy(); + expect(!isSpace('A')).toBeTruthy(); + expect(!isSpace('$')).toBeTruthy(); + expect(!isSpace('0')).toBeTruthy(); + expect(!isSpace('ü')).toBeTruthy(); + expect(!isSpace('ó')).toBeTruthy(); + expect(!isSpace('Д')).toBeTruthy(); + }); + }); + + test("IsPunctTrue", () => { + expect.assertions(17); + isPunct._init(false, undefined,() => { + expect(isPunct('?')).toBeTruthy(); + expect(isPunct('.')).toBeTruthy(); + expect(isPunct('\u2010')).toBeTruthy(); // hyphen + expect(isPunct('\u037E')).toBeTruthy(); // Greek question mark + expect(isPunct('\u3001')).toBeTruthy(); // ideographic comma + expect(!isPunct('a')).toBeTruthy(); + expect(!isPunct('m')).toBeTruthy(); + expect(!isPunct('z')).toBeTruthy(); + expect(!isPunct('A')).toBeTruthy(); + expect(!isPunct('Q')).toBeTruthy(); + expect(!isPunct(' ')).toBeTruthy(); + expect(!isPunct('0')).toBeTruthy(); + expect(!isPunct('1')).toBeTruthy(); + expect(!isPunct('8')).toBeTruthy(); + expect(!isPunct('ü')).toBeTruthy(); + expect(!isPunct('ó')).toBeTruthy(); + expect(!isPunct('Д')).toBeTruthy(); + }); + }); + + test("IsIdeoTrue", () => { + expect.assertions(10); + isIdeo._init(false, undefined,() => { + expect(isIdeo('碗')).toBeTruthy(); + expect(isIdeo('人')).toBeTruthy(); + expect(!isIdeo(' ')).toBeTruthy(); + expect(!isIdeo('$')).toBeTruthy(); + expect(!isIdeo('a')).toBeTruthy(); + expect(!isIdeo('m')).toBeTruthy(); + expect(!isIdeo('z')).toBeTruthy(); + expect(!isIdeo('0')).toBeTruthy(); + expect(!isIdeo('1')).toBeTruthy(); + expect(!isIdeo('8')).toBeTruthy(); + }); + }); + + test("IsCntrlTrue", () => { + expect.assertions(10); + isCntrl._init(false, undefined,() => { + expect(isCntrl('\u0001')).toBeTruthy(); + expect(isCntrl('\u0085')).toBeTruthy(); + expect(!isCntrl(' ')).toBeTruthy(); + expect(!isCntrl('$')).toBeTruthy(); + expect(!isCntrl('a')).toBeTruthy(); + expect(!isCntrl('m')).toBeTruthy(); + expect(!isCntrl('z')).toBeTruthy(); + expect(!isCntrl('0')).toBeTruthy(); + expect(!isCntrl('1')).toBeTruthy(); + expect(!isCntrl('8')).toBeTruthy(); + }); + }); + + test("IsDigitTrue", () => { + expect.assertions(20); + isDigit._init(false, undefined,() => { + expect(isDigit('0')).toBeTruthy(); + expect(isDigit('1')).toBeTruthy(); + expect(isDigit('2')).toBeTruthy(); + expect(isDigit('3')).toBeTruthy(); + expect(isDigit('4')).toBeTruthy(); + expect(isDigit('5')).toBeTruthy(); + expect(isDigit('6')).toBeTruthy(); + expect(isDigit('7')).toBeTruthy(); + expect(isDigit('8')).toBeTruthy(); + expect(isDigit('9')).toBeTruthy(); + expect(!isDigit(' ')).toBeTruthy(); + expect(!isDigit('a')).toBeTruthy(); + expect(!isDigit('m')).toBeTruthy(); + expect(!isDigit('z')).toBeTruthy(); + expect(!isDigit('A')).toBeTruthy(); + expect(!isDigit('Q')).toBeTruthy(); + expect(!isDigit('$')).toBeTruthy(); + expect(!isDigit('ü')).toBeTruthy(); + expect(!isDigit('ó')).toBeTruthy(); + expect(!isDigit('Д')).toBeTruthy(); + }); + }); + test("IsXdigitTrue", () => { + expect.assertions(32); + isXdigit._init(false, undefined,() => { + expect(isXdigit('0')).toBeTruthy(); + expect(isXdigit('1')).toBeTruthy(); + expect(isXdigit('2')).toBeTruthy(); + expect(isXdigit('3')).toBeTruthy(); + expect(isXdigit('4')).toBeTruthy(); + expect(isXdigit('5')).toBeTruthy(); + expect(isXdigit('6')).toBeTruthy(); + expect(isXdigit('7')).toBeTruthy(); + expect(isXdigit('8')).toBeTruthy(); + expect(isXdigit('9')).toBeTruthy(); + expect(isXdigit('A')).toBeTruthy(); + expect(isXdigit('B')).toBeTruthy(); + expect(isXdigit('C')).toBeTruthy(); + expect(isXdigit('D')).toBeTruthy(); + expect(isXdigit('E')).toBeTruthy(); + expect(isXdigit('F')).toBeTruthy(); + expect(isXdigit('a')).toBeTruthy(); + expect(isXdigit('b')).toBeTruthy(); + expect(isXdigit('c')).toBeTruthy(); + expect(isXdigit('d')).toBeTruthy(); + expect(isXdigit('e')).toBeTruthy(); + expect(isXdigit('f')).toBeTruthy(); + expect(!isXdigit('G')).toBeTruthy(); + expect(!isXdigit('g')).toBeTruthy(); + expect(!isXdigit(' ')).toBeTruthy(); + expect(!isXdigit('m')).toBeTruthy(); + expect(!isXdigit('z')).toBeTruthy(); + expect(!isXdigit('Q')).toBeTruthy(); + expect(!isXdigit('$')).toBeTruthy(); + expect(!isXdigit('ü')).toBeTruthy(); + expect(!isXdigit('ó')).toBeTruthy(); + expect(!isXdigit('Д')).toBeTruthy(); + }); + }); + + test("WithinRangeTrue", () => { + expect.assertions(5); + CType._init(false, undefined,() => { + expect(CType.withinRange('a', 'ascii')).toBeTruthy(); + expect(!CType.withinRange('\u2000a', 'ascii')).toBeTruthy(); + expect(CType.withinRange('a', 'ASCII')).toBeTruthy(); + expect(!CType.withinRange('G', 'arabic')).toBeTruthy(); + expect(CType.withinRange('a', 'latin')).toBeTruthy(); + }); + }); + + test("IsScriptTrue", () => { + expect.assertions(16); + isScript._init(false, undefined,() => { + expect(isScript("a", "Latn")).toBeTruthy(); + expect(isScript("Д", "Cyrl")).toBeTruthy(); + expect(isScript("ώ", "Grek")).toBeTruthy(); + expect(isScript("귋", "Hang")).toBeTruthy(); + expect(isScript("㜴", "Hani")).toBeTruthy(); + expect(isScript("ש", "Hebr")).toBeTruthy(); + expect(isScript("ش", "Arab")).toBeTruthy(); + expect(isScript("झ", "Deva")).toBeTruthy(); + expect(!isScript("a", "Cyrl")).toBeTruthy(); + expect(!isScript("Д", "Grek")).toBeTruthy(); + expect(!isScript("ώ", "Hang")).toBeTruthy(); + expect(!isScript("귋", "Hani")).toBeTruthy(); + expect(!isScript("㜴", "Hebr")).toBeTruthy(); + expect(!isScript("ש", "Arab")).toBeTruthy(); + expect(!isScript("ش", "Deva")).toBeTruthy(); + expect(!isScript("झ", "Latn")).toBeTruthy(); + }); + }); + + test("IsAlnum", () => { + expect.assertions(11); + isAlnum._init(false, undefined,() => { + expect(isAlnum('a')).toBeTruthy(); + expect(isAlnum('m')).toBeTruthy(); + expect(isAlnum('z')).toBeTruthy(); + expect(isAlnum('A')).toBeTruthy(); + expect(isAlnum('Q')).toBeTruthy(); + expect(isAlnum('0')).toBeTruthy(); + expect(isAlnum('1')).toBeTruthy(); + expect(isAlnum('8')).toBeTruthy(); + expect(isAlnum('Ꞛ')).toBeTruthy(); + expect(!isAlnum(' ')).toBeTruthy(); + expect(!isAlnum('$')).toBeTruthy(); + }); + }); + + test("IsGraphTrue", () => { + expect.assertions(5); + isGraph._init(false, undefined,() => { + expect(isGraph('A')).toBeTruthy(); + expect(isGraph('Q')).toBeTruthy(); + expect(isGraph('碗')).toBeTruthy(); + expect(!isGraph(' ')).toBeTruthy(); + expect(!isGraph('\u0002')).toBeTruthy(); + }); + }); +}); diff --git a/test/ctypepromise.test.js b/test/ctypepromise.test.js new file mode 100644 index 0000000..d2c3e06 --- /dev/null +++ b/test/ctypepromise.test.js @@ -0,0 +1,341 @@ +/* + * ctypepromise.test.js - test the character type information functions + * + * Copyright © 2018, 2022, 2024 JEDLSoft + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import ilib from '../src/ilib.js'; +import isXdigit from "../src/isXdigit.js"; +import isUpper from "../src/isUpper.js"; +import isSpace from "../src/isSpace.js"; +import isScript from "../src/isScript.js"; +import isPunct from "../src/isPunct.js"; +import isPrint from "../src/isPrint.js"; +import isLower from "../src/isLower.js"; +import isIdeo from "../src/isIdeo.js"; +import isGraph from "../src/isGraph.js"; +import isDigit from "../src/isDigit.js"; +import isCntrl from "../src/isCntrl.js"; +import isBlank from "../src/isBlank.js"; +import isAscii from "../src/isAscii.js"; +import isAlpha from "../src/isAlpha.js"; +import isAlnum from "../src/isAlnum.js"; +import CType from "../src/CType.js"; + +describe("testctypepromise", () => { + beforeEach(() => { + ilib.clearCache(); + ilib.data.ctype = null; + ilib.data.ctype_c = null; + ilib.data.ctype_l = null; + ilib.data.ctype_m = null; + ilib.data.ctype_p = null; + ilib.data.ctype_z = null; + ilib.data.scriptToRange = null; + }); + + test("IsAlphaTrue", () => { + expect.assertions(5); + return isAlpha._init(false).then(() => { + expect(isAlpha('a')).toBeTruthy(); + expect(isAlpha('m')).toBeTruthy(); + expect(isAlpha('z')).toBeTruthy(); + expect(isAlpha('A')).toBeTruthy(); + expect(isAlpha('Q')).toBeTruthy(); + }); + }); + + test("IsLowerTrue", () => { + expect.assertions(11); + return isLower._init(false).then(() => { + expect(isLower('a')).toBeTruthy(); + expect(isLower('m')).toBeTruthy(); + expect(isLower('щ')).toBeTruthy(); + expect(isLower('λ')).toBeTruthy(); + expect(!isLower(' ')).toBeTruthy(); + expect(!isLower('$')).toBeTruthy(); + expect(!isLower('A')).toBeTruthy(); + expect(!isLower('M')).toBeTruthy(); + expect(!isLower('0')).toBeTruthy(); + expect(!isLower('Щ')).toBeTruthy(); + expect(!isLower('Λ')).toBeTruthy(); + }); + }); + + test("IsUpperTrue", () => { + expect.assertions(11); + return isUpper._init(false).then(() => { + expect(isUpper('A')).toBeTruthy(); + expect(isUpper('M')).toBeTruthy(); + expect(isUpper('Щ')).toBeTruthy(); + expect(isUpper('Λ')).toBeTruthy(); + expect(!isUpper(' ')).toBeTruthy(); + expect(!isUpper('$')).toBeTruthy(); + expect(!isUpper('a')).toBeTruthy(); + expect(!isUpper('m')).toBeTruthy(); + expect(!isUpper('щ')).toBeTruthy(); + expect(!isUpper('λ')).toBeTruthy(); + expect(!isUpper('0')).toBeTruthy(); + }); + }); + + test("IsPrintTrue", () => { + expect.assertions(7); + return isPrint._init(false).then(() => { + expect(isPrint(' ')).toBeTruthy(); + expect(isPrint('A')).toBeTruthy(); + expect(isPrint('M')).toBeTruthy(); + expect(isPrint('Щ')).toBeTruthy(); + expect(isPrint('Λ')).toBeTruthy(); + expect(!isPrint('\u0001')).toBeTruthy(); + expect(!isPrint('\u0085')).toBeTruthy(); + }); + }); + + test("IsAsciiTrue", () => { + expect.assertions(13); + return isAscii._init(false).then(() => { + expect(isAscii('a')).toBeTruthy(); + expect(isAscii('m')).toBeTruthy(); + expect(isAscii('z')).toBeTruthy(); + expect(isAscii('A')).toBeTruthy(); + expect(isAscii('Q')).toBeTruthy(); + expect(isAscii(' ')).toBeTruthy(); + expect(isAscii('$')).toBeTruthy(); + expect(isAscii('0')).toBeTruthy(); + expect(isAscii('1')).toBeTruthy(); + expect(isAscii('8')).toBeTruthy(); + expect(!isAscii('ü')).toBeTruthy(); + expect(!isAscii('ó')).toBeTruthy(); + expect(!isAscii('Д')).toBeTruthy(); + }); + }); + + test("IsBlankTrue", () => { + expect.assertions(13); + return isBlank._init(false).then(() => { + expect(isBlank(' ')).toBeTruthy(); + expect(!isBlank('a')).toBeTruthy(); + expect(!isBlank('m')).toBeTruthy(); + expect(!isBlank('z')).toBeTruthy(); + expect(!isBlank('A')).toBeTruthy(); + expect(!isBlank('Q')).toBeTruthy(); + expect(!isBlank('$')).toBeTruthy(); + expect(!isBlank('0')).toBeTruthy(); + expect(!isBlank('1')).toBeTruthy(); + expect(!isBlank('8')).toBeTruthy(); + expect(!isBlank('ü')).toBeTruthy(); + expect(!isBlank('ó')).toBeTruthy(); + expect(!isBlank('Д')).toBeTruthy(); + }); + }); + + test("IsSpaceTrue", () => { + expect.assertions(11); + return isSpace._init(false).then(() => { + expect(isSpace(' ')).toBeTruthy(); + expect(isSpace('\t')).toBeTruthy(); + expect(isSpace('\n')).toBeTruthy(); + expect(isSpace('\u2000')).toBeTruthy(); + expect(!isSpace('a')).toBeTruthy(); + expect(!isSpace('A')).toBeTruthy(); + expect(!isSpace('$')).toBeTruthy(); + expect(!isSpace('0')).toBeTruthy(); + expect(!isSpace('ü')).toBeTruthy(); + expect(!isSpace('ó')).toBeTruthy(); + expect(!isSpace('Д')).toBeTruthy(); + }); + }); + + test("IsPunctTrue", () => { + expect.assertions(17); + return isPunct._init(false).then(() => { + expect(isPunct('?')).toBeTruthy(); + expect(isPunct('.')).toBeTruthy(); + expect(isPunct('\u2010')).toBeTruthy(); // hyphen + expect(isPunct('\u037E')).toBeTruthy(); // Greek question mark + expect(isPunct('\u3001')).toBeTruthy(); // ideographic comma + expect(!isPunct('a')).toBeTruthy(); + expect(!isPunct('m')).toBeTruthy(); + expect(!isPunct('z')).toBeTruthy(); + expect(!isPunct('A')).toBeTruthy(); + expect(!isPunct('Q')).toBeTruthy(); + expect(!isPunct(' ')).toBeTruthy(); + expect(!isPunct('0')).toBeTruthy(); + expect(!isPunct('1')).toBeTruthy(); + expect(!isPunct('8')).toBeTruthy(); + expect(!isPunct('ü')).toBeTruthy(); + expect(!isPunct('ó')).toBeTruthy(); + expect(!isPunct('Д')).toBeTruthy(); + }); + }); + + test("IsIdeoTrue", () => { + expect.assertions(10); + return isIdeo._init(false).then(() => { + expect(isIdeo('碗')).toBeTruthy(); + expect(isIdeo('人')).toBeTruthy(); + expect(!isIdeo(' ')).toBeTruthy(); + expect(!isIdeo('$')).toBeTruthy(); + expect(!isIdeo('a')).toBeTruthy(); + expect(!isIdeo('m')).toBeTruthy(); + expect(!isIdeo('z')).toBeTruthy(); + expect(!isIdeo('0')).toBeTruthy(); + expect(!isIdeo('1')).toBeTruthy(); + expect(!isIdeo('8')).toBeTruthy(); + }); + }); + + test("IsCntrlTrue", () => { + expect.assertions(10); + return isCntrl._init(false).then(() => { + expect(isCntrl('\u0001')).toBeTruthy(); + expect(isCntrl('\u0085')).toBeTruthy(); + expect(!isCntrl(' ')).toBeTruthy(); + expect(!isCntrl('$')).toBeTruthy(); + expect(!isCntrl('a')).toBeTruthy(); + expect(!isCntrl('m')).toBeTruthy(); + expect(!isCntrl('z')).toBeTruthy(); + expect(!isCntrl('0')).toBeTruthy(); + expect(!isCntrl('1')).toBeTruthy(); + expect(!isCntrl('8')).toBeTruthy(); + }); + }); + + test("IsDigitTrue", () => { + expect.assertions(20); + return isDigit._init(false).then(() => { + expect(isDigit('0')).toBeTruthy(); + expect(isDigit('1')).toBeTruthy(); + expect(isDigit('2')).toBeTruthy(); + expect(isDigit('3')).toBeTruthy(); + expect(isDigit('4')).toBeTruthy(); + expect(isDigit('5')).toBeTruthy(); + expect(isDigit('6')).toBeTruthy(); + expect(isDigit('7')).toBeTruthy(); + expect(isDigit('8')).toBeTruthy(); + expect(isDigit('9')).toBeTruthy(); + expect(!isDigit(' ')).toBeTruthy(); + expect(!isDigit('a')).toBeTruthy(); + expect(!isDigit('m')).toBeTruthy(); + expect(!isDigit('z')).toBeTruthy(); + expect(!isDigit('A')).toBeTruthy(); + expect(!isDigit('Q')).toBeTruthy(); + expect(!isDigit('$')).toBeTruthy(); + expect(!isDigit('ü')).toBeTruthy(); + expect(!isDigit('ó')).toBeTruthy(); + expect(!isDigit('Д')).toBeTruthy(); + }); + }); + test("IsXdigitTrue", () => { + expect.assertions(32); + return isXdigit._init(false).then(() => { + expect(isXdigit('0')).toBeTruthy(); + expect(isXdigit('1')).toBeTruthy(); + expect(isXdigit('2')).toBeTruthy(); + expect(isXdigit('3')).toBeTruthy(); + expect(isXdigit('4')).toBeTruthy(); + expect(isXdigit('5')).toBeTruthy(); + expect(isXdigit('6')).toBeTruthy(); + expect(isXdigit('7')).toBeTruthy(); + expect(isXdigit('8')).toBeTruthy(); + expect(isXdigit('9')).toBeTruthy(); + expect(isXdigit('A')).toBeTruthy(); + expect(isXdigit('B')).toBeTruthy(); + expect(isXdigit('C')).toBeTruthy(); + expect(isXdigit('D')).toBeTruthy(); + expect(isXdigit('E')).toBeTruthy(); + expect(isXdigit('F')).toBeTruthy(); + expect(isXdigit('a')).toBeTruthy(); + expect(isXdigit('b')).toBeTruthy(); + expect(isXdigit('c')).toBeTruthy(); + expect(isXdigit('d')).toBeTruthy(); + expect(isXdigit('e')).toBeTruthy(); + expect(isXdigit('f')).toBeTruthy(); + expect(!isXdigit('G')).toBeTruthy(); + expect(!isXdigit('g')).toBeTruthy(); + expect(!isXdigit(' ')).toBeTruthy(); + expect(!isXdigit('m')).toBeTruthy(); + expect(!isXdigit('z')).toBeTruthy(); + expect(!isXdigit('Q')).toBeTruthy(); + expect(!isXdigit('$')).toBeTruthy(); + expect(!isXdigit('ü')).toBeTruthy(); + expect(!isXdigit('ó')).toBeTruthy(); + expect(!isXdigit('Д')).toBeTruthy(); + }); + }); + + test("WithinRangeTrue", () => { + expect.assertions(5); + return CType._init(false).then(() => { + expect(CType.withinRange('a', 'ascii')).toBeTruthy(); + expect(!CType.withinRange('\u2000a', 'ascii')).toBeTruthy(); + expect(CType.withinRange('a', 'ASCII')).toBeTruthy(); + expect(!CType.withinRange('G', 'arabic')).toBeTruthy(); + expect(CType.withinRange('a', 'latin')).toBeTruthy(); + }); + }); + + test("IsScriptTrue", () => { + expect.assertions(16); + return isScript._init(false).then(() => { + expect(isScript("a", "Latn")).toBeTruthy(); + expect(isScript("Д", "Cyrl")).toBeTruthy(); + expect(isScript("ώ", "Grek")).toBeTruthy(); + expect(isScript("귋", "Hang")).toBeTruthy(); + expect(isScript("㜴", "Hani")).toBeTruthy(); + expect(isScript("ש", "Hebr")).toBeTruthy(); + expect(isScript("ش", "Arab")).toBeTruthy(); + expect(isScript("झ", "Deva")).toBeTruthy(); + expect(!isScript("a", "Cyrl")).toBeTruthy(); + expect(!isScript("Д", "Grek")).toBeTruthy(); + expect(!isScript("ώ", "Hang")).toBeTruthy(); + expect(!isScript("귋", "Hani")).toBeTruthy(); + expect(!isScript("㜴", "Hebr")).toBeTruthy(); + expect(!isScript("ש", "Arab")).toBeTruthy(); + expect(!isScript("ش", "Deva")).toBeTruthy(); + expect(!isScript("झ", "Latn")).toBeTruthy(); + }); + }); + + test("IsAlnum", () => { + expect.assertions(11); + return isAlnum._init(false).then(() => { + expect(isAlnum('a')).toBeTruthy(); + expect(isAlnum('m')).toBeTruthy(); + expect(isAlnum('z')).toBeTruthy(); + expect(isAlnum('A')).toBeTruthy(); + expect(isAlnum('Q')).toBeTruthy(); + expect(isAlnum('0')).toBeTruthy(); + expect(isAlnum('1')).toBeTruthy(); + expect(isAlnum('8')).toBeTruthy(); + expect(isAlnum('Ꞛ')).toBeTruthy(); + expect(!isAlnum(' ')).toBeTruthy(); + expect(!isAlnum('$')).toBeTruthy(); + }); + }); + + test("IsGraphTrue", () => { + expect.assertions(5); + return isGraph._init(false).then(() => { + expect(isGraph('A')).toBeTruthy(); + expect(isGraph('Q')).toBeTruthy(); + expect(isGraph('碗')).toBeTruthy(); + expect(!isGraph(' ')).toBeTruthy(); + expect(!isGraph('\u0002')).toBeTruthy(); + }); + }); +}); diff --git a/test/currencyasync.test.js b/test/currencyasync.test.js new file mode 100644 index 0000000..cf7e5d8 --- /dev/null +++ b/test/currencyasync.test.js @@ -0,0 +1,105 @@ +/* + * currencyasync.test.js - test the currency routines + * + * Copyright © 2018, 2022, 2024 JEDLSoft + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import Currency from "../src/Currency.js"; + +describe("testcurrencyasync", () => { + test("CurrencyAsyncDefaults", () => { + expect.assertions(7); + new Currency({ + sync: false, + onLoad: cur => { + expect(cur !== null).toBeTruthy(); + + expect(cur.getCode()).toBe("USD"); + expect(cur.getFractionDigits()).toBe(2); + expect(cur.getSign()).toBe("$"); + expect(cur.getName()).toBe("US Dollar"); + const locale = cur.getLocale(); + expect(typeof(locale) !== "undefined").toBeTruthy(); + expect(locale.toString()).toBe("en-US"); + } + }); + }); + + test("CurrencyAsyncGetByCode1", () => { + expect.assertions(6); + new Currency({ + code: "EUR", + sync: false, + onLoad: cur => { + expect(cur !== null).toBeTruthy(); + + expect(cur.getCode()).toBe("EUR"); + expect(cur.getFractionDigits()).toBe(2); + expect(cur.getSign()).toBe("€"); + expect(cur.getName()).toBe("Euro"); + const locale = cur.getLocale(); + expect(locale.toString()).toBe("en-US"); + } + }); + }); + + test("CurrencyAsyncGetByCodeUnknown", () => { + new Currency({ + code: "xxx", + sync: false, + onLoad: cur => { + expect(!cur).toBeTruthy(); + } + }); + }); + + test("CurrencyAsyncGetBySignUnambiguous", () => { + expect.assertions(6); + new Currency({ + sign: "€", + sync: false, + onLoad: cur => { + expect(cur !== null).toBeTruthy(); + + expect(cur.getCode()).toBe("EUR"); + expect(cur.getFractionDigits()).toBe(2); + expect(cur.getSign()).toBe("€"); + expect(cur.getName()).toBe("Euro"); + const locale = cur.getLocale(); + expect(locale.toString()).toBe("en-US"); + } + }); + }); + + test("CurrencyAsync", () => { + expect.assertions(6); + new Currency({ + locale: "en-GB", + sign: "$", + sync: false, + onLoad: cur => { + expect(cur !== null).toBeTruthy(); + + expect(cur.getCode()).toBe("USD"); + expect(cur.getFractionDigits()).toBe(2); + expect(cur.getSign()).toBe("$"); + expect(cur.getName()).toBe("US Dollar"); + const locale = cur.getLocale(); + expect(locale.toString()).toBe("en-GB"); + } + }); + }); +}); diff --git a/test/currencypromise.test.js b/test/currencypromise.test.js new file mode 100644 index 0000000..a72b59e --- /dev/null +++ b/test/currencypromise.test.js @@ -0,0 +1,100 @@ +/* + * currencyasync.test.js - test the currency routines + * + * Copyright © 2018, 2022, 2024 JEDLSoft + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import Currency from "../src/Currency.js"; + +describe("testcurrencypromise", () => { + test("CurrencyAsyncDefaults", () => { + expect.assertions(7); + return Currency.create({ + sync: false + }).then(cur => { + expect(cur !== null).toBeTruthy(); + + expect(cur.getCode()).toBe("USD"); + expect(cur.getFractionDigits()).toBe(2); + expect(cur.getSign()).toBe("$"); + expect(cur.getName()).toBe("US Dollar"); + const locale = cur.getLocale(); + expect(typeof(locale) !== "undefined").toBeTruthy(); + expect(locale.toString()).toBe("en-US"); + }); + }); + + test("CurrencyAsyncGetByCode1", () => { + expect.assertions(6); + return Currency.create({ + code: "EUR", + sync: false + }).then(cur => { + expect(cur !== null).toBeTruthy(); + + expect(cur.getCode()).toBe("EUR"); + expect(cur.getFractionDigits()).toBe(2); + expect(cur.getSign()).toBe("€"); + expect(cur.getName()).toBe("Euro"); + const locale = cur.getLocale(); + expect(locale.toString()).toBe("en-US"); + }); + }); + + test("CurrencyAsyncGetByCodeUnknown", () => { + return Currency.create({ + code: "xxx", + sync: false + }).then(cur => { + expect(!cur).toBeTruthy(); + }); + }); + + test("CurrencyAsyncGetBySignUnambiguous", () => { + expect.assertions(6); + return Currency.create({ + sign: "€", + sync: false + }).then(cur => { + expect(cur !== null).toBeTruthy(); + + expect(cur.getCode()).toBe("EUR"); + expect(cur.getFractionDigits()).toBe(2); + expect(cur.getSign()).toBe("€"); + expect(cur.getName()).toBe("Euro"); + const locale = cur.getLocale(); + expect(locale.toString()).toBe("en-US"); + }); + }); + + test("CurrencyAsync", () => { + expect.assertions(6); + return Currency.create({ + locale: "en-GB", + sign: "$", + sync: false + }).then(cur => { + expect(cur !== null).toBeTruthy(); + + expect(cur.getCode()).toBe("USD"); + expect(cur.getFractionDigits()).toBe(2); + expect(cur.getSign()).toBe("$"); + expect(cur.getName()).toBe("US Dollar"); + const locale = cur.getLocale(); + expect(locale.toString()).toBe("en-GB"); + }); + }); +}); diff --git a/test/testdateasync.js b/test/dateasync.test.js similarity index 50% rename from test/testdateasync.js rename to test/dateasync.test.js index 544b751..376603a 100644 --- a/test/testdateasync.js +++ b/test/dateasync.test.js @@ -1,7 +1,7 @@ /* - * testdateasync.js - test the date object asynchronously + * dateasync.test.js - test the date object asynchronously * - * Copyright © 2018, 2022 JEDLSoft + * Copyright © 2018, 2022, 2024 JEDLSoft * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,20 +21,19 @@ import GregorianDate from "../src/GregorianDate.js"; import DateFmt from "../src/DateFmt.js"; import DateFactory from "../src/DateFactory.js"; -export const testdateasync = { - testDateConstructor: function(test) { - test.expect(1); +describe("testdateasync", () => { + test("DateConstructor", () => { + expect.assertions(1); DateFactory({ sync: false, - onLoad: function(gd) { - test.ok(gd !== null); - test.done(); + onLoad: gd => { + expect(gd !== null).toBeTruthy(); } }); - }, + }); - testDateConstructorFull: function(test) { - test.expect(8); + test("DateConstructorFull", () => { + expect.assertions(8); DateFactory({ year: 2011, month: 9, @@ -44,75 +43,70 @@ export const testdateasync = { second: 12, millisecond: 123, sync: false, - onLoad: function(gd) { - test.ok(gd !== null); + onLoad: gd => { + expect(gd !== null).toBeTruthy(); - test.equal(gd.getYears(), 2011); - test.equal(gd.getMonths(), 9); - test.equal(gd.getDays(), 23); - test.equal(gd.getHours(), 16); - test.equal(gd.getMinutes(), 7); - test.equal(gd.getSeconds(), 12); - test.equal(gd.getMilliseconds(), 123); - test.done(); + expect(gd.getYears()).toBe(2011); + expect(gd.getMonths()).toBe(9); + expect(gd.getDays()).toBe(23); + expect(gd.getHours()).toBe(16); + expect(gd.getMinutes()).toBe(7); + expect(gd.getSeconds()).toBe(12); + expect(gd.getMilliseconds()).toBe(123); } }); - }, + }); - testDateFactoryRightType: function(test) { - test.expect(2); + test("DateFactoryRightType", () => { + expect.assertions(2); DateFactory({ type: "gregorian", sync: false, - onLoad: function(gd) { - test.ok(gd !== null); - test.equal(gd.getCalendar(), "gregorian"); - test.done(); + onLoad: gd => { + expect(gd !== null).toBeTruthy(); + expect(gd.getCalendar()).toBe("gregorian"); } }); - }, + }); - testDateFactoryNonGregorian: function(test) { - test.expect(2); + test("DateFactoryNonGregorian", () => { + expect.assertions(2); DateFactory({ type: "hebrew", sync: false, - onLoad: function(hd) { - test.ok(hd !== null); - test.equal(hd.getCalendar(), "hebrew"); - test.done(); + onLoad: hd => { + expect(hd !== null).toBeTruthy(); + expect(hd.getCalendar()).toBe("hebrew"); } }); - }, + }); - testDateFactoryNonGregorianWithCalendar: function(test) { - test.expect(2); + test("DateFactoryNonGregorianWithCalendar", () => { + expect.assertions(2); DateFactory({ calendar: "hebrew", sync: false, - onLoad: function(hd) { - test.ok(hd !== null); - test.equal(hd.getCalendar(), "hebrew"); - test.done(); + onLoad: hd => { + expect(hd !== null).toBeTruthy(); + expect(hd.getCalendar()).toBe("hebrew"); } }); - }, + }); - testDateFactoryBogus: function(test) { - test.expect(1); + test("DateFactoryBogus", () => { + expect.assertions(1); DateFactory({ type: "asdf", sync: false, - onLoad: function(gd) { - test.ok(typeof(gd) === "undefined"); - test.done(); + onLoad: gd => { + expect(typeof(gd) === "undefined").toBeTruthy(); } }); - }, + }); - testDateToIlibDate: function(test) { - test.expect(1); - var datMyBday = new Date("Fri Aug 13 1982 13:37:35 GMT-0700"); + test("DateToIlibDate", () => { + expect.assertions(1); + const datMyBday = new Date("Fri Aug 13 1982 13:37:35 GMT-0700"); DateFactory({ year: 1982, month: 8, @@ -122,21 +116,20 @@ export const testdateasync = { second: 35, timezone: "America/Los_Angeles", sync: false, - onLoad: function(ildMyBday) { + onLoad: ildMyBday => { new DateFmt({ length: "full", sync: false, - onLoad: function(fmt) { - test.equal(fmt.format(DateFactory._dateToIlib(datMyBday)), fmt.format(ildMyBday)); - test.done(); + onLoad: fmt => { + expect(fmt.format(DateFactory._dateToIlib(datMyBday))).toBe(fmt.format(ildMyBday)); } }); } }); - }, + }); - testDstStartBoundary_Azores: function(test) { - test.expect(1); + test("DstStartBoundary_Azores", () => { + expect.assertions(1); DateFactory({ year: 2019, month: 3, @@ -146,18 +139,17 @@ export const testdateasync = { second: 0, timezone: "Atlantic/Azores", sync: false, - onLoad: function(boundaryiLib) { + onLoad: boundaryiLib => { // we can't set time zone to Date object, so compare with constant value // 1553994000000: new Date(2019, 2, 31, 0, 0, 0).getTime() with Azores local time - test.equal(boundaryiLib.getTimeExtended(), 1553994000000); - test.done(); + expect(boundaryiLib.getTimeExtended()).toBe(1553994000000); } }); - }, + }); - testDstEndBoundary_Azores: function(test) { - test.expect(1); - var boundaryiLib = DateFactory({ + test("DstEndBoundary_Azores", () => { + expect.assertions(1); + const boundaryiLib = DateFactory({ year: 2019, month: 10, day: 27, @@ -166,10 +158,9 @@ export const testdateasync = { second: 0, timezone: "Atlantic/Azores" }); - var boundaryEs = new Date(2019, 9, 27, 1, 0, 0); + const boundaryEs = new Date(2019, 9, 27, 1, 0, 0); // we can't set time zone to Date object, so compare with constant value // 1572141600000: new Date(2019, 9, 27, 1, 0, 0).getTime() with Azores local time - test.equal(boundaryiLib.getTimeExtended(), 1572141600000); - test.done(); - } -}; + expect(boundaryiLib.getTimeExtended()).toBe(1572141600000); + }); +}); diff --git a/test/datefmt.test.js b/test/datefmt.test.js new file mode 100644 index 0000000..dd06fd2 --- /dev/null +++ b/test/datefmt.test.js @@ -0,0 +1,54 @@ +/* + * datefmt.test.js - test the date formatter static functions + * + * Copyright © 2024 JEDLSoft + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import DateFmt from "../src/DateFmt.js"; + +describe("testdatefmt", () => { + test("DateFmtGetMeridiemsRangeName_with_am_ET_locale", () => { + expect.assertions(2); + const fmt = DateFmt.getMeridiemsRange({ locale: "am-ET"}); + expect(fmt !== null).toBeTruthy(); + + expect(fmt[0].name).toBe("ጥዋት"); + }); + + test("DateFmtGetMeridiemsRangeName_with_am_ET_locale_gregorian_meridiems", () => { + expect.assertions(2); + const fmt = DateFmt.getMeridiemsRange({ locale: "am-ET", meridiems: "gregorian"}); + expect(fmt !== null).toBeTruthy(); + + expect(fmt[0].name).toBe("ጥዋት"); + }); + + test("DateFmtGetMeridiemsRangeName_with_zh_CN_locale", () => { + expect.assertions(2); + const fmt = DateFmt.getMeridiemsRange({ locale: "zh-CN"}); + expect(fmt !== null).toBeTruthy(); + + expect(fmt[0].name).toBe("上午"); + }); + + test("DateFmtGetMeridiemsRangeName_with_zh_CN_locale_chinese_meridiems", () => { + expect.assertions(2); + const fmt = DateFmt.getMeridiemsRange({ locale: "zh-CN", meridiems: "chinese"}); + expect(fmt !== null).toBeTruthy(); + + expect(fmt[0].name).toBe("凌晨"); + }); +}); diff --git a/test/datefmtasync.test.js b/test/datefmtasync.test.js new file mode 100644 index 0000000..64e3032 --- /dev/null +++ b/test/datefmtasync.test.js @@ -0,0 +1,327 @@ +/* + * datefmtasync.test.js - test the date formatter object asynchronously + * + * Copyright © 2018, 2022, 2024 JEDLSoft + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import ilib from "../src/ilib.js"; +import ThaiSolarDate from "../src/ThaiSolarDate.js"; +import PersianDate from "../src/PersianDate.js"; +import IslamicDate from "../src/IslamicDate.js"; +import HebrewDate from "../src/HebrewDate.js"; +import GregorianDate from "../src/GregorianDate.js"; +import JulianDate from "../src/JulianDate.js"; +import DateFmt from "../src/DateFmt.js"; +import DateFactory from "../src/DateFactory.js"; + +describe("testdatefmtasync", () => { + test("DateFmtConstructorEmpty", () => { + expect.assertions(1); + new DateFmt({ + sync: false, + onLoad: fmt => { + expect(fmt !== null).toBeTruthy(); + } + }); + }); + + test("DateFmtGetCalendarExplicit", () => { + expect.assertions(3); + new DateFmt({ + calendar: "julian", + sync: false, + onLoad: fmt => { + expect(fmt).toBeTruthy(); + const cal = fmt.getCalendar(); + expect(cal).toBeTruthy(); + + expect(cal).toBe("julian"); + } + }); + }); + + test("DateFmtGetCalendarNotInThisLocale", () => { + new DateFmt({ + calendar: "arabic", + locale: 'en-US', + sync: false, + onLoad: fmt => { + // "No formats available for calendar arabic in locale en-US" + expect(!fmt).toBeTruthy(); + } + }); + }); + + test("DateFmtGetTimeZoneDefault", () => { + expect.assertions(2); + ilib.tz = undefined; // just in case + if (ilib._getPlatform() === "nodejs") { + process.env.TZ = ""; + } + + new DateFmt({ + sync: false, + onLoad: fmt => { + expect(fmt !== null).toBeTruthy(); + + expect(fmt.getTimeZone().getId()).toBe("local"); + } + }); + }); + + test("DateFmtGetTimeZone", () => { + expect.assertions(2); + new DateFmt({ + timezone: "Europe/Paris", + sync: false, + onLoad: fmt => { + expect(fmt !== null).toBeTruthy(); + + expect(fmt.getTimeZone().getId()).toBe("Europe/Paris"); + } + }); + }); + + test("DateFmtUseTemplateNonEmptyCalendar", () => { + expect.assertions(2); + new DateFmt({ + calendar: 'julian', + template: "EEE 'the' DD 'of' MM, yyyy G", + sync: false, + onLoad: fmt => { + expect(fmt !== null).toBeTruthy(); + + expect(fmt.getCalendar()).toBe("julian"); + } + }); + }); + + test("DateFmtUseTemplateNonEmptyLocale", () => { + expect.assertions(2); + const fmt = new DateFmt({ + locale: 'de-DE', + template: "EEE 'the' DD 'of' MM, yyyy G", + sync: false, + onLoad: fmt => { + expect(fmt !== null).toBeTruthy(); + + expect(fmt.getLocale().toString()).toBe("de-DE"); + } + }); + }); + + test("DateFmtFormatJSDate1", () => { + expect.assertions(2); + new DateFmt({ + type: "time", + length: "short", + timezone: "America/Los_Angeles", + sync: false, + onLoad: fmt => { + expect(fmt !== null).toBeTruthy(); + + // test formatting a javascript date. It should be converted to + // an ilib date object automatically and then formatted + const datMyBday = new Date("Fri Aug 13 1982 13:37:35 GMT-0700"); + expect(fmt.format(datMyBday)).toBe("1:37 PM"); + } + }); + }); + + test("DateFmtFormatJSDateRightTimeZone1", () => { + expect.assertions(2); + const fmt = new DateFmt({ + type: "date", + length: "full", + date: "w", + timezone: "America/Los_Angeles", + sync: false, + onLoad: fmt => { + expect(fmt !== null).toBeTruthy(); + + // test formatting a javascript date. It should be converted to + // an ilib date object automatically and then formatted + const datMyBday = new Date("Wed May 14 2014 23:37:35 GMT-0700"); + expect(fmt.format(datMyBday)).toBe("Wednesday"); + } + }); + }); + + test("DateFmtGetMonthsOfYearThai", () => { + expect.assertions(2); + // uses ThaiSolar calendar + const fmt = new DateFmt({ + locale: "th-TH", + sync: false, + onLoad: fmt => { + expect(fmt !== null).toBeTruthy(); + + const arrMonths = fmt.getMonthsOfYear({length: "long"}); + + const expected = [undefined, "ม.ค.", "ก.พ.", "มี.ค.", "เม.ย.", "พ.ค.", "มิ.ย.", "ก.ค.", "ส.ค.", "ก.ย.", "ต.ค.", "พ.ย.", "ธ.ค."]; + expect(arrMonths).toStrictEqual(expected); + } + }); + }); + + test("DateFmtFormatRelativeWithinMinuteAfter", () => { + expect.assertions(1); + new DateFmt({ + length: "full", + sync: false, + onLoad: fmt => { + new GregorianDate({ + year: 2011, + month: 11, + day: 20, + hour: 13, + minute: 45, + second: 0, + millisecond: 0, + sync: false, + onLoad: reference => { + new GregorianDate({ + year: 2011, + month: 11, + day: 20, + hour: 13, + minute: 45, + second: 30, + millisecond: 0, + sync: false, + onLoad: date => { + expect(fmt.formatRelative(reference, date)).toBe("in 30 seconds"); + } + }); + } + }); + } + }); + }); + + test("DateFmtConvertToGMT", () => { + expect.assertions(2); + const fmt = new DateFmt({ + length: "short", + type: "datetime", + timezone: "Europe/London", + locale: "en-GB", + time: "hmaz", + sync: false, + onLoad: fmt => { + expect(fmt !== null).toBeTruthy(); + + new GregorianDate({ + year: 2011, + month: 9, + day: 20, + hour: 13, + minute: 45, + second: 0, + millisecond: 0, + timezone: "America/Los_Angeles", + locale: "en-US", + sync: false, + onLoad: date => { + expect(fmt.format(date)).toBe("20/09/2011, 21:45 GMT/BST"); + } + }); + } + }); + }); + + test("DateFmtConvertToOtherTimeZone", () => { + expect.assertions(2); + new DateFmt({ + length: "short", + type: "datetime", + timezone: "Australia/Sydney", + locale: "en-AU", + time: "hmaz", + sync: false, + onLoad: fmt => { + expect(fmt !== null).toBeTruthy(); + + new GregorianDate({ + year: 2011, + month: 9, + day: 20, + hour: 13, + minute: 45, + second: 0, + millisecond: 0, + timezone: "America/Los_Angeles", + locale: "en-US", + sync: false, + onLoad: date => { + expect(fmt.format(date)).toBe("21/9/11, 6:45 am AEST"); + } + }); + + } + }); + }); + + test("DateFmtGetMeridiemsRangeName_with_am_ET_locale", () => { + expect.assertions(2); + DateFmt.getMeridiemsRange({ + locale: "am-ET", + sync: false, + onLoad: fmt => { + expect(fmt !== null).toBeTruthy(); + expect(fmt[0].name).toBe("ጥዋት"); + } + }); + }); + + test("DateFmtGetMeridiemsRangeName_with_am_ET_locale_gregorian_meridiems", () => { + expect.assertions(2); + DateFmt.getMeridiemsRange({ + locale: "am-ET", + meridiems: "gregorian", + sync: false, + onLoad: fmt => { + expect(fmt !== null).toBeTruthy(); + expect(fmt[0].name).toBe("ጥዋት"); + } + }); + }); + + test("DateFmtGetMeridiemsRangeName_with_zh_CN_locale", () => { + expect.assertions(2); + DateFmt.getMeridiemsRange({ + locale: "zh-CN", + sync: false, + onLoad: fmt => { + expect(fmt !== null).toBeTruthy(); + expect(fmt[0].name).toBe("上午"); + } + }); + }); + + test("DateFmtGetMeridiemsRangeName_with_zh_CN_locale_chinese_meridiems", () => { + expect.assertions(2); + DateFmt.getMeridiemsRange({ + locale: "zh-CN", + meridiems: "chinese", + sync: false, + onLoad: fmt => { + expect(fmt !== null).toBeTruthy(); + expect(fmt[0].name).toBe("凌晨"); + } + }); + }); +}); \ No newline at end of file diff --git a/test/datefmtpromise.test.js b/test/datefmtpromise.test.js new file mode 100644 index 0000000..0983abf --- /dev/null +++ b/test/datefmtpromise.test.js @@ -0,0 +1,305 @@ +/* + * datefmtpromise.test.js - test the date formatter object asynchronously + * + * Copyright © 2018, 2022, 2024 JEDLSoft + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import ilib from "../src/ilib.js"; +import ThaiSolarDate from "../src/ThaiSolarDate.js"; +import PersianDate from "../src/PersianDate.js"; +import IslamicDate from "../src/IslamicDate.js"; +import HebrewDate from "../src/HebrewDate.js"; +import GregorianDate from "../src/GregorianDate.js"; +import JulianDate from "../src/JulianDate.js"; +import DateFmt from "../src/DateFmt.js"; +import DateFactory from "../src/DateFactory.js"; + +describe("testdatefmtpromise", () => { + test("DateFmtConstructorEmpty", () => { + expect.assertions(1); + return DateFmt.create({ + sync: false + }).then(fmt => { + expect(fmt !== null).toBeTruthy(); + }); + }); + + test("DateFmtGetCalendarExplicit", () => { + expect.assertions(3); + return DateFmt.create({ + calendar: "julian", + sync: false + }).then(fmt => { + expect(fmt).toBeTruthy(); + const cal = fmt.getCalendar(); + expect(cal).toBeTruthy(); + + expect(cal).toBe("julian"); + }); + }); + + test("DateFmtGetCalendarNotInThisLocale", () => { + return DateFmt.create({ + calendar: "arabic", + locale: 'en-US', + sync: false + }).then(fmt => { + // "No formats available for calendar arabic in locale en-US" + expect(!fmt).toBeTruthy(); + }); + }); + + test("DateFmtGetTimeZoneDefault", () => { + expect.assertions(2); + ilib.tz = undefined; // just in case + if (ilib._getPlatform() === "nodejs") { + process.env.TZ = ""; + } + + return DateFmt.create({ + sync: false + }).then(fmt => { + expect(fmt !== null).toBeTruthy(); + + expect(fmt.getTimeZone().getId()).toBe("local"); + }); + }); + + test("DateFmtGetTimeZone", () => { + expect.assertions(2); + return DateFmt.create({ + timezone: "Europe/Paris", + sync: false + }).then(fmt => { + expect(fmt !== null).toBeTruthy(); + + expect(fmt.getTimeZone().getId()).toBe("Europe/Paris"); + }); + }); + + test("DateFmtUseTemplateNonEmptyCalendar", () => { + expect.assertions(2); + return DateFmt.create({ + calendar: 'julian', + template: "EEE 'the' DD 'of' MM, yyyy G", + sync: false + }).then(fmt => { + expect(fmt !== null).toBeTruthy(); + + expect(fmt.getCalendar()).toBe("julian"); + }); + }); + + test("DateFmtUseTemplateNonEmptyLocale", () => { + expect.assertions(2); + return DateFmt.create({ + locale: 'de-DE', + template: "EEE 'the' DD 'of' MM, yyyy G", + sync: false + }).then(fmt => { + expect(fmt !== null).toBeTruthy(); + + expect(fmt.getLocale().toString()).toBe("de-DE"); + }); + }); + + test("DateFmtFormatJSDate1", () => { + expect.assertions(2); + return DateFmt.create({ + type: "time", + length: "short", + timezone: "America/Los_Angeles", + sync: false + }).then(fmt => { + expect(fmt !== null).toBeTruthy(); + + // test formatting a javascript date. It should be converted to + // an ilib date object automatically and then formatted + const datMyBday = new Date("Fri Aug 13 1982 13:37:35 GMT-0700"); + expect(fmt.format(datMyBday)).toBe("1:37 PM"); + }); + }); + + test("DateFmtFormatJSDateRightTimeZone1", () => { + expect.assertions(2); + return DateFmt.create({ + type: "date", + length: "full", + date: "w", + timezone: "America/Los_Angeles", + sync: false + }).then(fmt => { + expect(fmt !== null).toBeTruthy(); + + // test formatting a javascript date. It should be converted to + // an ilib date object automatically and then formatted + const datMyBday = new Date("Wed May 14 2014 23:37:35 GMT-0700"); + expect(fmt.format(datMyBday)).toBe("Wednesday"); + }); + }); + + test("DateFmtGetMonthsOfYearThai", () => { + expect.assertions(2); + // uses ThaiSolar calendar + return DateFmt.create({ + locale: "th-TH", + sync: false + }).then(fmt => { + expect(fmt !== null).toBeTruthy(); + + const arrMonths = fmt.getMonthsOfYear({length: "long"}); + + const expected = [undefined, "ม.ค.", "ก.พ.", "มี.ค.", "เม.ย.", "พ.ค.", "มิ.ย.", "ก.ค.", "ส.ค.", "ก.ย.", "ต.ค.", "พ.ย.", "ธ.ค."]; + expect(arrMonths).toStrictEqual(expected); + }); + }); + + test("DateFmtFormatRelativeWithinMinuteAfter", () => { + expect.assertions(1); + return DateFmt.create({ + length: "full", + sync: false + }).then(fmt => { + GregorianDate.create({ + year: 2011, + month: 11, + day: 20, + hour: 13, + minute: 45, + second: 0, + millisecond: 0, + sync: false + }).then(reference => { + GregorianDate.create({ + year: 2011, + month: 11, + day: 20, + hour: 13, + minute: 45, + second: 30, + millisecond: 0, + sync: false + }).then(date => { + expect(fmt.formatRelative(reference, date)).toBe("in 30 seconds"); + }); + }); + }); + }); + + test("DateFmtConvertToGMT", () => { + expect.assertions(2); + return DateFmt.create({ + length: "short", + type: "datetime", + timezone: "Europe/London", + locale: "en-GB", + time: "hmaz", + sync: false + }).then(fmt => { + expect(fmt !== null).toBeTruthy(); + + GregorianDate.create({ + year: 2011, + month: 9, + day: 20, + hour: 13, + minute: 45, + second: 0, + millisecond: 0, + timezone: "America/Los_Angeles", + locale: "en-US", + sync: false + }).then(date => { + expect(fmt.format(date)).toBe("20/09/2011, 21:45 GMT/BST"); + }); + }); + }); + + test("DateFmtConvertToOtherTimeZone", () => { + expect.assertions(2); + return DateFmt.create({ + length: "short", + type: "datetime", + timezone: "Australia/Sydney", + locale: "en-AU", + time: "hmaz", + sync: false + }).then(fmt => { + expect(fmt !== null).toBeTruthy(); + + GregorianDate.create({ + year: 2011, + month: 9, + day: 20, + hour: 13, + minute: 45, + second: 0, + millisecond: 0, + timezone: "America/Los_Angeles", + locale: "en-US", + sync: false + }).then(date => { + expect(fmt.format(date)).toBe("21/9/11, 6:45 am AEST"); + }); + }); + }); + + test("DateFmtGetMeridiemsRangeName_with_am_ET_locale", () => { + expect.assertions(2); + return DateFmt.getMeridiemsRange({ + locale: "am-ET", + sync: false + }).then(fmt => { + expect(fmt !== null).toBeTruthy(); + expect(fmt[0].name).toBe("ጥዋት"); + }); + }); + + test("DateFmtGetMeridiemsRangeName_with_am_ET_locale_gregorian_meridiems", () => { + expect.assertions(2); + return DateFmt.getMeridiemsRange({ + locale: "am-ET", + meridiems: "gregorian", + sync: false + }).then(fmt => { + expect(fmt !== null).toBeTruthy(); + expect(fmt[0].name).toBe("ጥዋት"); + }); + }); + + test("DateFmtGetMeridiemsRangeName_with_zh_CN_locale", () => { + expect.assertions(2); + return DateFmt.getMeridiemsRange({ + locale: "zh-CN", + sync: false + }).then(fmt => { + expect(fmt !== null).toBeTruthy(); + expect(fmt[0].name).toBe("上午"); + }); + }); + + test("DateFmtGetMeridiemsRangeName_with_zh_CN_locale_chinese_meridiems", () => { + expect.assertions(2); + return DateFmt.getMeridiemsRange({ + locale: "zh-CN", + meridiems: "chinese", + sync: false, + }).then(fmt => { + expect(fmt !== null).toBeTruthy(); + expect(fmt[0].name).toBe("凌晨"); + }); + }); +}); \ No newline at end of file diff --git a/test/testdatefmtrangeasync.js b/test/datefmtrangeasync.test.js similarity index 50% rename from test/testdatefmtrangeasync.js rename to test/datefmtrangeasync.test.js index 1b50594..24bd5db 100644 --- a/test/testdatefmtrangeasync.js +++ b/test/datefmtrangeasync.test.js @@ -1,7 +1,7 @@ /* - * testdatefmtrangeasync.js - test the date range formatter object + * datefmtrangeasync.test.js - test the date range formatter object * - * Copyright © 2018-2019, JEDLSoft + * Copyright © 2018-2019, 2024 JEDLSoft * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,101 +22,95 @@ import GregorianDate from "../src/GregorianDate.js"; import DateRngFmt from "../src/DateRngFmt.js"; import DateFmt from "../src/DateFmt.js"; -export const testdatefmtrangeasync = { - testDateRngFmtConstructorEmpty: function(test) { - test.expect(1); +describe("testdatefmtrangeasync", () => { + test("DateRngFmtConstructorEmpty", () => { + expect.assertions(1); new DateRngFmt({ sync: false, - onLoad: function(fmt) { - test.ok(fmt !== null); - test.done(); + onLoad: fmt => { + expect(fmt !== null).toBeTruthy(); } }); - }, + }); - testDateRngFmtGetCalendarExplicit: function(test) { - test.expect(3); + test("DateRngFmtGetCalendarExplicit", () => { + expect.assertions(3); new DateRngFmt({ calendar: "julian", sync: false, - onLoad: function(fmt) { - test.ok(fmt !== null); - var cal = fmt.getCalendar(); - test.ok(cal !== null); + onLoad: fmt => { + expect(fmt !== null).toBeTruthy(); + const cal = fmt.getCalendar(); + expect(cal !== null).toBeTruthy(); - test.equal(cal, "julian"); - test.done(); + expect(cal).toBe("julian"); } }); - }, + }); - testDateRngFmtGetCalendarNotInThisLocale: function(test) { + test("DateRngFmtGetCalendarNotInThisLocale", () => { new DateRngFmt({ calendar: "arabic", locale: 'en-US', sync: false, - onLoad: function(fmt) { + onLoad: fmt => { // "No formats available for calendar arabic in locale en-US" - test.ok(!fmt); - test.done(); + expect(!fmt).toBeTruthy(); } }); - }, + }); - testDateRngFmtGetLocale: function(test) { - test.expect(2); - var fmt = new DateRngFmt({ + test("DateRngFmtGetLocale", () => { + expect.assertions(2); + const fmt = new DateRngFmt({ locale: "de-DE", sync: false, - onLoad: function(fmt) { - test.ok(fmt !== null); + onLoad: fmt => { + expect(fmt !== null).toBeTruthy(); - test.equal(fmt.getLocale().toString(), "de-DE"); - test.done(); + expect(fmt.getLocale().toString()).toBe("de-DE"); } }); - }, + }); - testDateRngFmtGetTimeZone: function(test) { - test.expect(2); - var fmt = new DateRngFmt({ + test("DateRngFmtGetTimeZone", () => { + expect.assertions(2); + const fmt = new DateRngFmt({ timezone: "Europe/Paris", sync: false, - onLoad: function(fmt) { - test.ok(fmt !== null); + onLoad: fmt => { + expect(fmt !== null).toBeTruthy(); - var tz = fmt.getTimeZone(); - test.equal(tz.getId(), "Europe/Paris"); - test.done(); + const tz = fmt.getTimeZone(); + expect(tz.getId()).toBe("Europe/Paris"); } }); - }, + }); - testDateRngFmtGetDefaultLocale: function(test) { - test.expect(2); - var fmt = new DateRngFmt({ + test("DateRngFmtGetDefaultLocale", () => { + expect.assertions(2); + const fmt = new DateRngFmt({ locale: "yy-YY", sync: false, - onLoad: function(fmt) { - test.ok(fmt !== null); + onLoad: fmt => { + expect(fmt !== null).toBeTruthy(); - test.equal(fmt.getLocale().toString(), "yy-YY"); - test.done(); + expect(fmt.getLocale().toString()).toBe("yy-YY"); } }); - }, + }); //Miscellaneous UnitTest - testDateRngFmtesPAMultiDayMedium: function(test) { - test.expect(2); - var fmt = new DateRngFmt({ + test("DateRngFmtesPAMultiDayMedium", () => { + expect.assertions(2); + const fmt = new DateRngFmt({ locale: "es-PA", length: "medium", sync: false, - onLoad: function(fmt) { - test.ok(fmt !== null); + onLoad: fmt => { + expect(fmt !== null).toBeTruthy(); new GregorianDate({ year: 2011, @@ -127,7 +121,7 @@ export const testdatefmtrangeasync = { second: 0, millisecond: 0, sync: false, - onLoad: function(start) { + onLoad: start => { new GregorianDate({ year: 2011, month: 6, @@ -137,25 +131,24 @@ export const testdatefmtrangeasync = { second: 30, millisecond: 0, sync: false, - onLoad: function(end) { - test.equal(fmt.format(start, end), "06/20 – 28/2011"); - test.done(); + onLoad: end => { + expect(fmt.format(start, end)).toBe("06/20 – 28/2011"); } }); } }); } }); - }, + }); - testDateRngFmtesPRNextMonthMedium: function(test) { - test.expect(2); - var fmt = new DateRngFmt({ + test("DateRngFmtesPRNextMonthMedium", () => { + expect.assertions(2); + const fmt = new DateRngFmt({ locale: "es-PR", length: "medium", sync: false, - onLoad: function(fmt) { - test.ok(fmt !== null); + onLoad: fmt => { + expect(fmt !== null).toBeTruthy(); new GregorianDate({ year: 2011, @@ -166,7 +159,7 @@ export const testdatefmtrangeasync = { second: 0, millisecond: 0, sync: false, - onLoad: function(start) { + onLoad: start => { new GregorianDate({ year: 2011, month: 11, @@ -176,48 +169,45 @@ export const testdatefmtrangeasync = { second: 0, millisecond: 0, sync: false, - onLoad: function(end) { - test.equal(fmt.format(start, end), "06/20 – 11/28/2011"); - test.done(); + onLoad: end => { + expect(fmt.format(start, end)).toBe("06/20 – 11/28/2011"); } }); } }); } }); - }, + }); - testDateRngFmtAcceptJSIntrisicDates: function(test) { - test.expect(2); - var fmt = new DateRngFmt({ + test("DateRngFmtAcceptJSIntrisicDates", () => { + expect.assertions(2); + const fmt = new DateRngFmt({ locale: "en-US", length: "short", sync: false, - onLoad: function(fmt) { - test.ok(fmt !== null); + onLoad: fmt => { + expect(fmt !== null).toBeTruthy(); - var start = new Date(2011, 5, 20, 13, 45, 0); - var end = new Date(2012, 4, 26, 16, 30, 0); - test.equal(fmt.format(start, end), "6/20/11 – 5/26/12"); - test.done(); + const start = new Date(2011, 5, 20, 13, 45, 0); + const end = new Date(2012, 4, 26, 16, 30, 0); + expect(fmt.format(start, end)).toBe("6/20/11 – 5/26/12"); } }); - }, + }); - testDateRngFmtAcceptUnixTimes: function(test) { - test.expect(2); - var fmt = new DateRngFmt({ + test("DateRngFmtAcceptUnixTimes", () => { + expect.assertions(2); + const fmt = new DateRngFmt({ locale: "en-US", length: "short", sync: false, - onLoad: function(fmt) { - test.ok(fmt !== null); + onLoad: fmt => { + expect(fmt !== null).toBeTruthy(); - var start = 1308602700000; - var end = 1338075000000; - test.equal(fmt.format(start, end), "6/20/11 – 5/26/12"); - test.done(); + const start = 1308602700000; + const end = 1338075000000; + expect(fmt.format(start, end)).toBe("6/20/11 – 5/26/12"); } }); - } -}; + }); +}); diff --git a/test/datefmtrangepromise.test.js b/test/datefmtrangepromise.test.js new file mode 100644 index 0000000..29e1d88 --- /dev/null +++ b/test/datefmtrangepromise.test.js @@ -0,0 +1,199 @@ +/* + * datefmtrangepromise.test.js - test the date range formatter object + * + * Copyright © 2018-2019, 2024 JEDLSoft + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import LocaleInfo from "../src/LocaleInfo.js"; +import GregorianDate from "../src/GregorianDate.js"; +import DateRngFmt from "../src/DateRngFmt.js"; +import DateFmt from "../src/DateFmt.js"; + +describe("testdatefmtrangepromise", () => { + test("DateRngFmtConstructorEmpty", () => { + expect.assertions(1); + return DateRngFmt.create({ + sync: false + }).then(fmt => { + expect(fmt !== null).toBeTruthy(); + }); + + }); + + test("DateRngFmtGetCalendarExplicit", () => { + expect.assertions(3); + return DateRngFmt.create({ + calendar: "julian", + sync: false + }).then(fmt => { + expect(fmt !== null).toBeTruthy(); + const cal = fmt.getCalendar(); + expect(cal !== null).toBeTruthy(); + + expect(cal).toBe("julian"); + }); + }); + + test("DateRngFmtGetCalendarNotInThisLocale", () => { + return DateRngFmt.create({ + calendar: "arabic", + locale: 'en-US', + sync: false + }).then(fmt => { + // "No formats available for calendar arabic in locale en-US" + expect(!fmt).toBeTruthy(); + }); + }); + + test("DateRngFmtGetLocale", () => { + expect.assertions(2); + return DateRngFmt.create({ + locale: "de-DE", + sync: false + }).then(fmt => { + expect(fmt !== null).toBeTruthy(); + + expect(fmt.getLocale().toString()).toBe("de-DE"); + }); + }); + + test("DateRngFmtGetTimeZone", () => { + expect.assertions(2); + return DateRngFmt.create({ + timezone: "Europe/Paris", + sync: false + }).then(fmt => { + expect(fmt !== null).toBeTruthy(); + + const tz = fmt.getTimeZone(); + expect(tz.getId()).toBe("Europe/Paris"); + }); + }); + + test("DateRngFmtGetDefaultLocale", () => { + expect.assertions(2); + return DateRngFmt.create({ + locale: "yy-YY", + sync: false + }).then(fmt => { + expect(fmt !== null).toBeTruthy(); + + expect(fmt.getLocale().toString()).toBe("yy-YY"); + }); + }); + + //Miscellaneous UnitTest + + test("DateRngFmtesPAMultiDayMedium", () => { + expect.assertions(2); + return DateRngFmt.create({ + locale: "es-PA", + length: "medium", + sync: false + }).then(fmt => { + expect(fmt !== null).toBeTruthy(); + + GregorianDate.create({ + year: 2011, + month: 6, + day: 20, + hour: 13, + minute: 45, + second: 0, + millisecond: 0, + sync: false + }).then(start => { + GregorianDate.create({ + year: 2011, + month: 6, + day: 28, + hour: 14, + minute: 5, + second: 30, + millisecond: 0, + sync: false + }).then(end => { + expect(fmt.format(start, end)).toBe("06/20 – 28/2011"); + }); + }); + }); + }); + + test("DateRngFmtesPRNextMonthMedium", () => { + expect.assertions(2); + return DateRngFmt.create({ + locale: "es-PR", + length: "medium", + sync: false + }).then(fmt => { + expect(fmt !== null).toBeTruthy(); + + GregorianDate.create({ + year: 2011, + month: 6, + day: 20, + hour: 13, + minute: 45, + second: 0, + millisecond: 0, + sync: false + }).then(start => { + GregorianDate.create({ + year: 2011, + month: 11, + day: 28, + hour: 14, + minute: 30, + second: 0, + millisecond: 0, + sync: false + }).then(end => { + expect(fmt.format(start, end)).toBe("06/20 – 11/28/2011"); + }); + }); + }); + }); + + test("DateRngFmtAcceptJSIntrisicDates", () => { + expect.assertions(2); + return DateRngFmt.create({ + locale: "en-US", + length: "short", + sync: false + }).then(fmt => { + expect(fmt !== null).toBeTruthy(); + + const start = new Date(2011, 5, 20, 13, 45, 0); + const end = new Date(2012, 4, 26, 16, 30, 0); + expect(fmt.format(start, end)).toBe("6/20/11 – 5/26/12"); + }); + }); + + test("DateRngFmtAcceptUnixTimes", () => { + expect.assertions(2); + return DateRngFmt.create({ + locale: "en-US", + length: "short", + sync: false + }).then(fmt => { + expect(fmt !== null).toBeTruthy(); + + const start = 1308602700000; + const end = 1338075000000; + expect(fmt.format(start, end)).toBe("6/20/11 – 5/26/12"); + }); + }); +}); diff --git a/test/datepromise.test.js b/test/datepromise.test.js new file mode 100644 index 0000000..0d32d56 --- /dev/null +++ b/test/datepromise.test.js @@ -0,0 +1,148 @@ +/* + * datepromise.test.js - test the date object asynchronously + * + * Copyright © 2018, 2022, 2024 JEDLSoft + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import GregorianDate from "../src/GregorianDate.js"; +import DateFmt from "../src/DateFmt.js"; +import DateFactory, {DateFactoryAsync} from "../src/DateFactory.js"; + +describe("testdatepromise", () => { + test("DateConstructor", () => { + expect.assertions(1); + return DateFactoryAsync().then(gd => { + expect(gd !== null).toBeTruthy(); + }); + }); + + test("DateConstructorFull", () => { + expect.assertions(8); + return DateFactoryAsync({ + year: 2011, + month: 9, + day: 23, + hour: 16, + minute: 7, + second: 12, + millisecond: 123 + }).then(gd => { + expect(gd !== null).toBeTruthy(); + + expect(gd.getYears()).toBe(2011); + expect(gd.getMonths()).toBe(9); + expect(gd.getDays()).toBe(23); + expect(gd.getHours()).toBe(16); + expect(gd.getMinutes()).toBe(7); + expect(gd.getSeconds()).toBe(12); + expect(gd.getMilliseconds()).toBe(123); + }); + }); + + test("DateFactoryRightType", () => { + expect.assertions(2); + return DateFactoryAsync({ + type: "gregorian" + }).then(gd => { + expect(gd !== null).toBeTruthy(); + expect(gd.getCalendar()).toBe("gregorian"); + }); + }); + + test("DateFactoryNonGregorian", () => { + expect.assertions(2); + return DateFactoryAsync({ + type: "hebrew" + }).then(hd => { + expect(hd !== null).toBeTruthy(); + expect(hd.getCalendar()).toBe("hebrew"); + }); + }); + + test("DateFactoryNonGregorianWithCalendar", () => { + expect.assertions(2); + return DateFactoryAsync({ + calendar: "hebrew" + }).then(hd => { + expect(hd !== null).toBeTruthy(); + expect(hd.getCalendar()).toBe("hebrew"); + }); + }); + + test("DateFactoryBogus", () => { + expect.assertions(1); + return DateFactoryAsync({ + type: "asdf" + }).then(gd => { + expect(typeof(gd) === "undefined").toBeTruthy(); + }); + }); + + test("DateToIlibDate", () => { + expect.assertions(1); + const datMyBday = new Date("Fri Aug 13 1982 13:37:35 GMT-0700"); + return DateFactoryAsync({ + year: 1982, + month: 8, + day: 13, + hour: 13, + minute: 37, + second: 35, + timezone: "America/Los_Angeles" + }).then(ildMyBday => { + return DateFmt.create({ + length: "full", + sync: false + }).then(fmt => { + expect(fmt.format(DateFactory._dateToIlib(datMyBday))).toBe(fmt.format(ildMyBday)); + }); + }); + }); + + test("DstStartBoundary_Azores", () => { + expect.assertions(1); + return DateFactoryAsync({ + year: 2019, + month: 3, + day: 31, + hour: 0, + minute: 0, + second: 0, + timezone: "Atlantic/Azores" + }).then(boundaryiLib => { + // we can't set time zone to Date object, so compare with constant value + // 1553994000000: new Date(2019, 2, 31, 0, 0, 0).getTime() with Azores local time + expect(boundaryiLib.getTimeExtended()).toBe(1553994000000); + }); + }); + + test("DstEndBoundary_Azores", () => { + expect.assertions(1); + return DateFactoryAsync({ + year: 2019, + month: 10, + day: 27, + hour: 1, + minute: 0, + second: 0, + timezone: "Atlantic/Azores" + }).then(boundaryiLib => { + // we can't set time zone to Date object, so compare with constant value + // 1572141600000: new Date(2019, 9, 27, 1, 0, 0).getTime() with Azores local time + expect(boundaryiLib.getTimeExtended()).toBe(1572141600000); + }); + }); +}); diff --git a/test/testdurfmtasync.js b/test/durfmtasync.test.js similarity index 53% rename from test/testdurfmtasync.js rename to test/durfmtasync.test.js index 0d4db78..fea527f 100644 --- a/test/testdurfmtasync.js +++ b/test/durfmtasync.test.js @@ -1,7 +1,7 @@ /* - * testdurfmtasync.js - test the duration formatter object + * durfmtasync.test.js - test the duration formatter object * - * Copyright © 2018, 2022 JEDLSoft + * Copyright © 2018, 2022, 2024 JEDLSoft * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,56 +19,53 @@ import DurationFmt from "../src/DurationFmt.js"; -export const testdurfmtasync = { - testDurFmtAsyncConstructorEmpty: function(test) { - test.expect(1); +describe("testdurfmtasync", () => { + test("DurFmtAsyncConstructorEmpty", () => { + expect.assertions(1); new DurationFmt({ sync: false, - onLoad: function(fmt) { - test.ok(fmt !== null); - test.done(); + onLoad: fmt => { + expect(fmt !== null).toBeTruthy(); } }); - }, + }); - testDurFmtAsyncGetLocale: function(test) { - test.expect(2); + test("DurFmtAsyncGetLocale", () => { + expect.assertions(2); new DurationFmt({ locale: "de-DE", sync: false, - onLoad: function(fmt) { - test.ok(fmt !== null); + onLoad: fmt => { + expect(fmt !== null).toBeTruthy(); - test.equal(fmt.getLocale().toString(), "de-DE"); - test.done(); + expect(fmt.getLocale().toString()).toBe("de-DE"); } }); - }, + }); - testDurFmtAsyncGetLocaleBogus: function(test) { - test.expect(2); + test("DurFmtAsyncGetLocaleBogus", () => { + expect.assertions(2); new DurationFmt({ locale: "zyy-XX", sync: false, - onLoad: function(fmt) { - test.ok(fmt !== null); + onLoad: fmt => { + expect(fmt !== null).toBeTruthy(); - test.equal(fmt.getLocale().toString(), "zyy-XX"); - test.done(); + expect(fmt.getLocale().toString()).toBe("zyy-XX"); } }); - }, + }); - testDurFmtAsyncFormatShortText: function(test) { - test.expect(2); + test("DurFmtAsyncFormatShortText", () => { + expect.assertions(2); new DurationFmt({ length: "short", sync: false, - onLoad: function(fmt) { - test.ok(fmt !== null); + onLoad: fmt => { + expect(fmt !== null).toBeTruthy(); - var duration = fmt.format({ + const duration = fmt.format({ year: 1, month: 1, week: 1, @@ -78,21 +75,20 @@ export const testdurfmtasync = { second: 1, millisecond: 1 }); - test.equal(duration.toString(), "1y 1m 1w 1d 1h 1m 1s 1ms"); - test.done(); + expect(duration.toString()).toBe("1y 1m 1w 1d 1h 1m 1s 1ms"); } }); - }, + }); - testDurFmtAsyncFormatLongSingle: function(test) { - test.expect(2); + test("DurFmtAsyncFormatLongSingle", () => { + expect.assertions(2); new DurationFmt({ length: "long", sync: false, - onLoad: function(fmt) { - test.ok(fmt !== null); + onLoad: fmt => { + expect(fmt !== null).toBeTruthy(); - var duration = fmt.format({ + const duration = fmt.format({ year: 1, month: 1, week: 1, @@ -102,21 +98,20 @@ export const testdurfmtasync = { second: 1, millisecond: 1 }); - test.equal(duration.toString(), "1 yr, 1 mth, 1 wk, 1 day, 1 hr, 1 min, 1 sec, 1 ms"); - test.done(); + expect(duration.toString()).toBe("1 yr, 1 mth, 1 wk, 1 day, 1 hr, 1 min, 1 sec, 1 ms"); } }); - }, + }); - testDurFmtAsyncFormatFullPlural: function(test) { - test.expect(2); + test("DurFmtAsyncFormatFullPlural", () => { + expect.assertions(2); new DurationFmt({ length: "full", sync: false, - onLoad: function(fmt) { - test.ok(fmt !== null); + onLoad: fmt => { + expect(fmt !== null).toBeTruthy(); - var duration = fmt.format({ + const duration = fmt.format({ year: 2, month: 2, week: 2, @@ -126,21 +121,20 @@ export const testdurfmtasync = { second: 2, millisecond: 2 }); - test.equal(duration.toString(), "2 years, 2 months, 2 weeks, 2 days, 2 hours, 2 minutes, 2 seconds, 2 milliseconds"); - test.done(); + expect(duration.toString()).toBe("2 years, 2 months, 2 weeks, 2 days, 2 hours, 2 minutes, 2 seconds, 2 milliseconds"); } }); - }, + }); - testDurFmtAsyncFormatShortDEDefaultStyle: function(test) { - test.expect(2); + test("DurFmtAsyncFormatShortDEDefaultStyle", () => { + expect.assertions(2); new DurationFmt({ locale: "de-DE", length: "short", sync: false, - onLoad: function(fmt) { - test.ok(fmt !== null); - var duration = fmt.format({ + onLoad: fmt => { + expect(fmt !== null).toBeTruthy(); + const duration = fmt.format({ year: 1, month: 1, week: 1, @@ -150,22 +144,21 @@ export const testdurfmtasync = { second: 1, millisecond: 1 }); - test.equal(duration.toString(), "1 J, 1 M, 1 W, 1 T, 1 Std., 1 Min., 1 Sek., 1 ms"); - test.done(); + expect(duration.toString()).toBe("1 J, 1 M, 1 W, 1 T, 1 Std., 1 Min., 1 Sek., 1 ms"); } }); - }, + }); - testDurFmtAsyncFormatFullDESingle: function(test) { - test.expect(2); + test("DurFmtAsyncFormatFullDESingle", () => { + expect.assertions(2); new DurationFmt({ locale: "de-DE", length: "full", sync: false, - onLoad: function(fmt) { - test.ok(fmt !== null); + onLoad: fmt => { + expect(fmt !== null).toBeTruthy(); - var duration = fmt.format({ + const duration = fmt.format({ year: 1, month: 1, week: 1, @@ -175,23 +168,22 @@ export const testdurfmtasync = { second: 1, millisecond: 1 }); - test.equal(duration.toString(), "1 Jahr, 1 Monat, 1 Woche, 1 Tag, 1 Stunde, 1 Minute, 1 Sekunde und 1 Millisekunde"); - test.done(); + expect(duration.toString()).toBe("1 Jahr, 1 Monat, 1 Woche, 1 Tag, 1 Stunde, 1 Minute, 1 Sekunde und 1 Millisekunde"); } }); - }, + }); - testDurFmtAsyncFormatShortZHText: function(test) { - test.expect(2); + test("DurFmtAsyncFormatShortZHText", () => { + expect.assertions(2); new DurationFmt({ locale: "zh-Hans-CN", length: "short", style: "text", sync: false, - onLoad: function(fmt) { - test.ok(fmt !== null); + onLoad: fmt => { + expect(fmt !== null).toBeTruthy(); - var duration = fmt.format({ + const duration = fmt.format({ year: 1, month: 1, week: 1, @@ -201,23 +193,22 @@ export const testdurfmtasync = { second: 1, millisecond: 1 }); - test.equal(duration.toString(), "1年1个月1周1天1小时1分钟1秒1毫秒"); - test.done(); + expect(duration.toString()).toBe("1年1个月1周1天1小时1分钟1秒1毫秒"); } }); - }, + }); - testDurFmtAsyncFormatShortZHClock: function(test) { - test.expect(2); + test("DurFmtAsyncFormatShortZHClock", () => { + expect.assertions(2); new DurationFmt({ locale: "zh-Hans-CN", length: "short", style: "clock", sync: false, - onLoad: function(fmt) { - test.ok(fmt !== null); + onLoad: fmt => { + expect(fmt !== null).toBeTruthy(); - var duration = fmt.format({ + const duration = fmt.format({ year: 1, month: 1, week: 1, @@ -227,22 +218,21 @@ export const testdurfmtasync = { second: 1, millisecond: 1 }); - test.equal(duration.toString(), "1年1个月1周1天01:01:01"); - test.done(); + expect(duration.toString()).toBe("1年1个月1周1天01:01:01"); } }); - }, + }); - testDurFmtAsyncFormatFullZH: function(test) { - test.expect(2); + test("DurFmtAsyncFormatFullZH", () => { + expect.assertions(2); new DurationFmt({ locale: "zh-Hans-CN", length: "full", sync: false, - onLoad: function(fmt) { - test.ok(fmt !== null); + onLoad: fmt => { + expect(fmt !== null).toBeTruthy(); - var duration = fmt.format({ + const duration = fmt.format({ year: 1, month: 1, week: 1, @@ -252,9 +242,8 @@ export const testdurfmtasync = { second: 1, millisecond: 1 }); - test.equal(duration.toString(), "1年1个月1周1天1小时1分钟1秒钟1毫秒"); - test.done(); + expect(duration.toString()).toBe("1年1个月1周1天1小时1分钟1秒钟1毫秒"); } }); - } -}; + }); +}); diff --git a/test/durfmtpromise.test.js b/test/durfmtpromise.test.js new file mode 100644 index 0000000..7613213 --- /dev/null +++ b/test/durfmtpromise.test.js @@ -0,0 +1,238 @@ +/* + * durfmtasync.test.js - test the duration formatter object + * + * Copyright © 2018, 2022, 2024 JEDLSoft + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import DurationFmt from "../src/DurationFmt.js"; + +describe("testdurfmtpromise", () => { + test("DurFmtAsyncConstructorEmpty", () => { + expect.assertions(1); + return DurationFmt.create({ + sync: false + }).then(fmt => { + expect(fmt !== null).toBeTruthy(); + }); + + }); + + test("DurFmtAsyncGetLocale", () => { + expect.assertions(2); + return DurationFmt.create({ + locale: "de-DE", + sync: false + }).then(fmt => { + expect(fmt !== null).toBeTruthy(); + + expect(fmt.getLocale().toString()).toBe("de-DE"); + }); + }); + + test("DurFmtAsyncGetLocaleBogus", () => { + expect.assertions(2); + return DurationFmt.create({ + locale: "zyy-XX", + sync: false + }).then(fmt => { + expect(fmt !== null).toBeTruthy(); + + expect(fmt.getLocale().toString()).toBe("zyy-XX"); + }); + }); + + test("DurFmtAsyncFormatShortText", () => { + expect.assertions(2); + return DurationFmt.create({ + length: "short", + sync: false + }).then(fmt => { + expect(fmt !== null).toBeTruthy(); + + const duration = fmt.format({ + year: 1, + month: 1, + week: 1, + day: 1, + hour: 1, + minute: 1, + second: 1, + millisecond: 1 + }); + expect(duration.toString()).toBe("1y 1m 1w 1d 1h 1m 1s 1ms"); + }); + }); + + test("DurFmtAsyncFormatLongSingle", () => { + expect.assertions(2); + return DurationFmt.create({ + length: "long", + sync: false + }).then(fmt => { + expect(fmt !== null).toBeTruthy(); + + const duration = fmt.format({ + year: 1, + month: 1, + week: 1, + day: 1, + hour: 1, + minute: 1, + second: 1, + millisecond: 1 + }); + expect(duration.toString()).toBe("1 yr, 1 mth, 1 wk, 1 day, 1 hr, 1 min, 1 sec, 1 ms"); + }); + }); + + test("DurFmtAsyncFormatFullPlural", () => { + expect.assertions(2); + return DurationFmt.create({ + length: "full", + sync: false + }).then(fmt => { + expect(fmt !== null).toBeTruthy(); + + const duration = fmt.format({ + year: 2, + month: 2, + week: 2, + day: 2, + hour: 2, + minute: 2, + second: 2, + millisecond: 2 + }); + expect(duration.toString()).toBe("2 years, 2 months, 2 weeks, 2 days, 2 hours, 2 minutes, 2 seconds, 2 milliseconds"); + }); + }); + + test("DurFmtAsyncFormatShortDEDefaultStyle", () => { + expect.assertions(2); + return DurationFmt.create({ + locale: "de-DE", + length: "short", + sync: false + }).then(fmt => { + expect(fmt !== null).toBeTruthy(); + const duration = fmt.format({ + year: 1, + month: 1, + week: 1, + day: 1, + hour: 1, + minute: 1, + second: 1, + millisecond: 1 + }); + expect(duration.toString()).toBe("1 J, 1 M, 1 W, 1 T, 1 Std., 1 Min., 1 Sek., 1 ms"); + }); + }); + + test("DurFmtAsyncFormatFullDESingle", () => { + expect.assertions(2); + return DurationFmt.create({ + locale: "de-DE", + length: "full", + sync: false + }).then(fmt => { + expect(fmt !== null).toBeTruthy(); + + const duration = fmt.format({ + year: 1, + month: 1, + week: 1, + day: 1, + hour: 1, + minute: 1, + second: 1, + millisecond: 1 + }); + expect(duration.toString()).toBe("1 Jahr, 1 Monat, 1 Woche, 1 Tag, 1 Stunde, 1 Minute, 1 Sekunde und 1 Millisekunde"); + }); + }); + + test("DurFmtAsyncFormatShortZHText", () => { + expect.assertions(2); + return DurationFmt.create({ + locale: "zh-Hans-CN", + length: "short", + style: "text", + sync: false + }).then(fmt => { + expect(fmt !== null).toBeTruthy(); + + const duration = fmt.format({ + year: 1, + month: 1, + week: 1, + day: 1, + hour: 1, + minute: 1, + second: 1, + millisecond: 1 + }); + expect(duration.toString()).toBe("1年1个月1周1天1小时1分钟1秒1毫秒"); + }); + }); + + test("DurFmtAsyncFormatShortZHClock", () => { + expect.assertions(2); + return DurationFmt.create({ + locale: "zh-Hans-CN", + length: "short", + style: "clock", + sync: false + }).then(fmt => { + expect(fmt !== null).toBeTruthy(); + + const duration = fmt.format({ + year: 1, + month: 1, + week: 1, + day: 1, + hour: 1, + minute: 1, + second: 1, + millisecond: 1 + }); + expect(duration.toString()).toBe("1年1个月1周1天01:01:01"); + }); + }); + + test("DurFmtAsyncFormatFullZH", () => { + expect.assertions(2); + return DurationFmt.create({ + locale: "zh-Hans-CN", + length: "full", + sync: false + }).then(fmt => { + expect(fmt !== null).toBeTruthy(); + + const duration = fmt.format({ + year: 1, + month: 1, + week: 1, + day: 1, + hour: 1, + minute: 1, + second: 1, + millisecond: 1 + }); + expect(duration.toString()).toBe("1年1个月1周1天1小时1分钟1秒钟1毫秒"); + }); + }); +}); diff --git a/test/global.test.js b/test/global.test.js new file mode 100644 index 0000000..927d820 --- /dev/null +++ b/test/global.test.js @@ -0,0 +1,575 @@ +/* + * global.test.js - test the ilib static routines + * + * Copyright © 2012-2015, 2017-2024 JEDLSoft + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import ilib from "../src/ilib.js"; +import Locale from "../src/Locale.js"; + +describe("testglobal", () => { + beforeAll(() => { + ilib.clearCache(); + }); + + test("GetLocaleDefault", () => { + expect.assertions(1); + expect(ilib.getLocale()).toBe("en-US"); + }); + + test("SetLocale", () => { + expect.assertions(2); + expect(ilib.getLocale()).toBe("en-US"); + + ilib.setLocale("it-IT"); + + expect(ilib.getLocale()).toBe("it-IT"); + delete ilib.locale; // clean up + }); + + test("SetLocaleObject", () => { + expect.assertions(2); + expect(ilib.getLocale()).toBe("en-US"); + + ilib.setLocale(new Locale("it-IT")); + + // do not change the locale if the arg is not a string + expect(ilib.getLocale()).toBe("en-US"); + }); + + test("SetLocaleEmpty", () => { + expect.assertions(2); + expect(ilib.getLocale()).toBe("en-US"); + + ilib.setLocale(); + + expect(ilib.getLocale()).toBe("en-US"); + }); + + test("GetVersion", () => { + expect.assertions(1); + expect(ilib.getVersion().substring(0,5)).toBe("14.19"); + }); + + test("GetTimeZoneDefault", () => { + // use a different test when the Intl object is available + ilib._platform = undefined; + if (ilib._global("Intl")) { + return; + } + + expect.assertions(1); + ilib._platform = undefined; + ilib.tz = undefined; + + if (ilib._getPlatform() === "nodejs") { + process.env.TZ = ""; + } + + if (ilib._getPlatform() === "browser") { + navigator.timezone = undefined; + } + expect(ilib.getTimeZone()).toBe("local"); + }); + + /* + uncomment again when ilib is fixed + test("GetTimeZoneDefaultWithIntl", () => { + // only test when the Intl object is available + if (!ilib._global("Intl")) { + return; + } + + ilib._platform = undefined; + ilib.tz = undefined; // clear this first + const ro = new Intl.DateTimeFormat().resolvedOptions(); + const expected = ro && ro.timeZone; + if (expected) { + expect.assertions(1); + expect(ilib.getTimeZone()).toBe(expected); + } + }); + */ + + test("SetTimeZone", () => { + // use a different test when the Intl object is available + if (ilib._global("Intl")) { + return; + } + + expect.assertions(2); + ilib._platform = undefined; + ilib.tz = undefined; + expect(ilib.getTimeZone()).toBe("local"); + + ilib.setTimeZone("America/Los_Angeles"); + + expect(ilib.getTimeZone()).toBe("America/Los_Angeles"); + delete ilib.tz; // clean up + }); + + test("GetTimeZoneBrowser", () => { + if (ilib._getPlatform() !== "browser" || ilib._global("Intl")) { + // only testable on a browser without the Intl object available + return; + } + + ilib._platform = undefined; + ilib.tz = undefined; + navigator.timezone = "America/New_York"; + + expect.assertions(1); + expect(ilib.getTimeZone()).toBe("America/New_York"); + navigator.timezone = undefined; + }); + + test("SetTimeZoneEmpty", () => { + // use a different test when the Intl object is available + if (ilib._global("Intl")) { + return; + } + + expect.assertions(2); + ilib._platform = undefined; + ilib.tz = undefined; + if (ilib._getPlatform() === "browser") { + navigator.timezone = undefined; + } + + expect(ilib.getTimeZone()).toBe("local"); + + ilib.setTimeZone(); + + expect(ilib.getTimeZone()).toBe("local"); + }); + + test("GetTimeZoneNodejs", () => { + // only test on older nodejs where the Intl object is not available + if (ilib._getPlatform() === "nodejs" && !ilib._global("Intl")) { + expect.assertions(1); + ilib._platform = undefined; + ilib.tz = undefined; + if (typeof(process) === 'undefined') { + process = { + env: {} + }; + } + if (!process.env) process.env = {}; + + const tmp = process.env.TZ; + process.env.TZ = "America/Phoenix"; + + expect(ilib.getTimeZone()).toBe("America/Phoenix"); + + process.env.TZ = tmp; + } + }); + + test("GetTimeZoneRhino", () => { + if (ilib._getPlatform() !== "rhino" || ilib._global("Intl")) { + // only test this in rhino + return; + } + ilib.tz = undefined; + + if (typeof(process) === 'undefined') { + // under plain rhino + environment.user.timezone = "America/New_York"; + } else { + // under trireme on rhino emulating nodejs + process.env.TZ = "America/New_York"; + } + + expect.assertions(1); + expect(ilib.getTimeZone()).toBe("America/New_York"); + }); + + test("GetTimeZoneWebOS", () => { + if (ilib._getPlatform() !== "webos" || ilib._global("Intl")) { + // only test this in webos + return; + } + ilib.tz = undefined; + PalmSystem.timezone = "Europe/London"; + + expect.assertions(1); + expect(ilib.getTimeZone()).toBe("Europe/London"); + }); + + test("GetLocaleNodejs1", () => { + if (ilib._getPlatform() !== "nodejs") { + // only test this in node + return; + } + + ilib.locale = undefined; + if (!process.env) process.env = {}; + + process.env.LANG = "th-TH"; + + expect.assertions(1); + expect(ilib.getLocale()).toBe("th-TH"); + + process.env.LANG = ""; + ilib.locale = undefined; + }); + + test("GetLocaleNodejs2", () => { + if (ilib._getPlatform() !== "nodejs") { + // only test this in node + return; + } + + ilib.locale = undefined; + + process.env.LC_ALL = "th-TH"; + + expect.assertions(1); + expect(ilib.getLocale()).toBe("th-TH"); + + process.env.LC_ALL = ""; + ilib.locale = undefined; + }); + + test("GetLocaleRhino", () => { + if (ilib._getPlatform() !== "rhino") { + // only test this in node + return; + } + + ilib.locale = undefined; + + if (typeof(process) === 'undefined') { + // under plain rhino + environment.user.language = "de"; + environment.user.country = "AT"; + } else { + // under trireme on rhino emulating nodejs + process.env.LANG = "de_AT.UTF8"; + } + + expect.assertions(1); + expect(ilib.getLocale()).toBe("de-AT"); + + if (typeof(process) === 'undefined') { + // under plain rhino + environment.user.language = undefined; + environment.user.country = undefined; + } else { + process.env.LANG = "en_US.UTF8"; + } + }); + + test("GetLocaleWebOS", () => { + if (ilib._getPlatform() !== "webos") { + // only test this in node + return; + } + + ilib.locale = undefined; + + PalmSystem.locale = "ru-RU"; + + expect.assertions(1); + expect(ilib.getLocale()).toBe("ru-RU"); + + PalmSystem.locale = undefined; + }); + + test("GetLocaleNotString", () => { + if (ilib.isDynCode()) { + // can't test this with dynamically loaded code because the global context + // is different for each module and we cannot set global variables, so we + // cannot simulate the conditions where this code would work + return; + } + ilib._platform = undefined; + ilib.locale = new Locale("it-IT"); + + // should remove the locale object and make it into a string + expect.assertions(1); + expect(ilib.getLocale()).toBe("en-US"); + }); + + test("GetLocaleBrowser", () => { + if (ilib._getPlatform() !== "browser") { + // only test this in a real browser + return; + } + ilib.locale = undefined; + + const loc = ""; + + if (navigator.language.length > 5) { + const l = navigator.language; + loc = l.substring(0,3) + l.charAt(3).toUpperCase() + l.substring(4,8).toLowerCase() + l.substring(8).toUpperCase(); + } else if (navigator.language.length > 2) { + loc = navigator.language.substring(0,3) + navigator.language.substring(3).toUpperCase(); + } else { + loc = navigator.language; + } + if (loc === "en") { + loc = "en-US"; + } + expect.assertions(1); + expect(ilib.getLocale()).toBe(loc); + }); + + test("IsArrayNewArrayObj", () => { + expect.assertions(1); + const a = new Array(); + expect(ilib.isArray(a)).toBeTruthy(); + }); + + test("IsArrayNewArrayBrackets", () => { + expect.assertions(1); + const a = []; + expect(ilib.isArray(a)).toBeTruthy(); + }); + + test("IsArrayObject", () => { + expect.assertions(1); + const a = {foo:234}; + expect(!ilib.isArray(a)).toBeTruthy(); + }); + + test("IsArrayObjectWithNumericProperties", () => { + expect.assertions(1); + const a = {"0": "d", "1": "c"}; + expect(!ilib.isArray(a)).toBeTruthy(); + }); + + test("IsArrayNumber", () => { + expect.assertions(1); + const a = 234; + expect(!ilib.isArray(a)).toBeTruthy(); + }); + + test("IsArrayString", () => { + expect.assertions(1); + const a = "asdf"; + expect(!ilib.isArray(a)).toBeTruthy(); + }); + + test("IsArrayNull", () => { + expect.assertions(1); + const a = null; + expect(!ilib.isArray(a)).toBeTruthy(); + }); + + test("IsArrayUndefined", () => { + expect.assertions(1); + const a = undefined; + expect(!ilib.isArray(a)).toBeTruthy(); + }); + + test("ExtendSimple", () => { + expect.assertions(1); + const object1 = {"a": "A", "b": "B"}, + object2 = {"c": "C", "d": "D"}; + + ilib.extend(object1, object2); + expect(object1).toStrictEqual({"a": "A", "b": "B", "c": "C", "d": "D"}); + }); + + test("ExtendReturnObject1", () => { + expect.assertions(1); + const object1 = {"a": "A", "b": "B"}, + object2 = {"c": "C", "d": "D"}; + + const x = ilib.extend(object1, object2); + expect(x).toBe(object1); + }); + + test("ExtendArrays", () => { + expect.assertions(1); + const object1 = {"a": ["b", "c"]}, + object2 = {"a": ["d"]}; + + ilib.extend(object1, object2); + expect(object1).toStrictEqual({"a": ["b", "c", "d"]}); + }); + + test("ExtendArraysDups", () => { + expect.assertions(1); + const object1 = {"a": ["b", "c"]}, + object2 = {"a": ["c", "d"]}; + + ilib.extend(object1, object2); + expect(object1).toStrictEqual({"a": ["b", "c", "c", "d"]}); + }); + + test("ExtendArraysEmptySource", () => { + expect.assertions(1); + const object1 = {"a": []}, + object2 = {"a": ["d"]}; + + ilib.extend(object1, object2); + expect(object1).toStrictEqual({"a": ["d"]}); + }); + + test("ExtendArraysEmptyTarget", () => { + expect.assertions(1); + const object1 = {"a": ["b", "c"]}, + object2 = {"a": []}; + + ilib.extend(object1, object2); + expect(object1).toStrictEqual({"a": ["b", "c"]}); + }); + + test("ExtendArraysIncongruentTypes1", () => { + expect.assertions(1); + const object1 = {"a": ["b", "c"]}, + object2 = {"a": "d"}; + + ilib.extend(object1, object2); + expect(object1).toStrictEqual({"a": "d"}); + }); + + test("ExtendArraysIncongruentTypes2", () => { + expect.assertions(1); + const object1 = {"a": "b"}, + object2 = {"a": ["d"]}; + + ilib.extend(object1, object2); + expect(object1).toStrictEqual({"a": ["d"]}); + }); + + test("ExtendSimpleProperty", () => { + expect.assertions(1); + const object1 = {"a": "A", "b": "B"}, + object2 = {"b": "X"}; + + ilib.extend(object1, object2); + expect(object1).toStrictEqual({"a": "A", "b": "X"}); + }); + + test("ExtendComplexProperty", () => { + expect.assertions(1); + const object1 = {"a": "A", "b": {"x": "B"}}, + object2 = {"b": "X"}; + + ilib.extend(object1, object2); + expect(object1).toStrictEqual({"a": "A", "b": "X"}); + }); + + test("ExtendSubobjects", () => { + expect.assertions(1); + const object1 = {"b": {"x": "X", "y": "Y"}}, + object2 = {"b": {"x": "M", "y": "N"}}; + + ilib.extend(object1, object2); + expect(object1).toStrictEqual({"b": {"x": "M", "y": "N"}}); + }); + + test("ExtendSubobjectsLeaveObj1PropsUntouched", () => { + expect.assertions(1); + const object1 = {"a": "A", "b": {"x": "X", "y": "Y", "z": "Z"}}, + object2 = {"b": {"x": "M", "y": "N"}}; + + ilib.extend(object1, object2); + expect(object1).toStrictEqual({"a": "A", "b": {"x": "M", "y": "N", "z": "Z"}}); + }); + + test("ExtendSubobjectsAddProps", () => { + expect.assertions(1); + const object1 = {"a": "A", "b": {"x": "X", "y": "Y"}}, + object2 = {"b": {"x": "M", "y": "N", "z": "Z"}}; + + ilib.extend(object1, object2); + expect(object1).toStrictEqual({"a": "A", "b": {"x": "M", "y": "N", "z": "Z"}}); + }); + + test("ExtendSubobjectsAddProps", () => { + expect.assertions(1); + const object1 = {"a": "A", "b": {"x": "X", "y": "Y"}}, + object2 = {"b": {"x": "M", "y": "N", "z": "Z"}}; + + ilib.extend(object1, object2); + expect(object1).toStrictEqual({"a": "A", "b": {"x": "M", "y": "N", "z": "Z"}}); + }); + + test("ExtendBooleans", () => { + expect.assertions(1); + const object1 = {"a": true, "b": true}, + object2 = {"b": false}; + + ilib.extend(object1, object2); + expect(object1).toStrictEqual({"a": true, "b": false}); + }); + + test("ExtendAddBooleans", () => { + expect.assertions(1); + const object1 = {"a": true, "b": true}, + object2 = {"c": false}; + + ilib.extend(object1, object2); + expect(object1).toStrictEqual({"a": true, "b": true, "c": false}); + }); + + test("ExtendNumbers", () => { + expect.assertions(1); + const object1 = {"a": 1, "b": 2}, + object2 = {"b": 3}; + + ilib.extend(object1, object2); + expect(object1).toStrictEqual({"a": 1, "b": 3}); + }); + + test("ExtendNumbersWithZero", () => { + expect.assertions(1); + const object1 = {"a": 1, "b": 2}, + object2 = {"b": 0}; + + ilib.extend(object1, object2); + expect(object1).toStrictEqual({"a": 1, "b": 0}); + }); + + test("ExtendNumbersAddZero", () => { + expect.assertions(1); + const object1 = {"a": 1, "b": 2}, + object2 = {"c": 0}; + + ilib.extend(object1, object2); + expect(object1).toStrictEqual({"a": 1, "b": 2, "c": 0}); + }); + + + /* + const testGlobalNumber = 42; + + test("IsGlobal", () => { + expect.assertions(1); + expect(ilib._isGlobal("testGlobalNumber")).toBeTruthy(); + }); + + test("IsGlobalNot", () => { + expect.assertions(1); + expect(!ilib._isGlobal("asdfasdfasdf")).toBeTruthy(); + }); + + test("Global", () => { + expect.assertions(1); + expect(ilib._global("testGlobalNumber")).toBe(42); + }); + + test("GlobalUndefined", () => { + expect.assertions(1); + expect(typeof(ilib._global("testGlobalNumber2")) === "undefined").toBeTruthy(); + }); + */ + +}); diff --git a/test/glyphstrasync.test.js b/test/glyphstrasync.test.js new file mode 100644 index 0000000..4c8ebc4 --- /dev/null +++ b/test/glyphstrasync.test.js @@ -0,0 +1,128 @@ +/* + * glyphstrasync.test.js - test the glyph iteration routines + * + * Copyright © 2018, 2022, 2024 JEDLSoft + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import GlyphString from "../src/GlyphString.js"; + +describe("testglyphstrasync", () => { + test("GlyphStrAsyncCharIteratorNormal", () => { + expect.assertions(8); + new GlyphString("aÄa", { // the A umlaut is a decomposed char + sync: false, + onLoad: s => { + const it = s.charIterator(); + + expect(it.hasNext()).toBeTruthy(); + expect(it.next()).toBe("a"); + expect(it.hasNext()).toBeTruthy(); + expect(it.next()).toBe("Ä"); + expect(it.hasNext()).toBeTruthy(); + expect(it.next()).toBe("a"); + expect(!it.hasNext()).toBeTruthy(); + expect(it.next()).toBe(undefined); + } + }); + }); + + test("GlyphStrAsyncCharIteratorEmpty", () => { + expect.assertions(2); + const s = new GlyphString("", { + sync: false, + onLoad: s => { + const it = s.charIterator(); + + expect(!it.hasNext()).toBeTruthy(); + expect(it.next()).toBe(undefined); + } + }); + }); + + test("GlyphStrAsyncCharIteratorMultipleDecomposed", () => { + expect.assertions(8); + const s = new GlyphString("aẬa", { // the accented A is a decomposed char with 2 accents + sync: false, + onLoad: s => { + const it = s.charIterator(); + + expect(it.hasNext()).toBeTruthy(); + expect(it.next()).toBe("a"); + expect(it.hasNext()).toBeTruthy(); + expect(it.next()).toBe("Ậ"); + expect(it.hasNext()).toBeTruthy(); + expect(it.next()).toBe("a"); + expect(!it.hasNext()).toBeTruthy(); + expect(it.next()).toBe(undefined); + } + }); + }); + + test("GlyphStrAsyncTruncateWithCombiningAccentsWholeGlyphs", () => { + expect.assertions(1); + const s = new GlyphString("aẬbẬcẬdẬe", { // the accented A is a decomposed char with 2 accents + sync: false, + onLoad: s => { + expect(s.truncate(4)).toBe("aẬbẬ"); + } + }); + }); + + test("GlyphStrAsyncTruncateThai", () => { + expect.assertions(1); + const s = new GlyphString("สวัุสดีคุณเป็นอย่างไรบ้าง", { + sync: false, + onLoad: s => { + // this tests non-spacing marks that are also non-combining + + expect(s.truncate(4)).toBe("สวัุสดี"); + } + }); + }); + + test("GlyphStrAsyncTruncateDevanagari1", () => { + expect.assertions(1); + const s = new GlyphString("हैलो, आप कैसे हैं?", { + sync: false, + onLoad: s => { + // if the 2nd base character has combining spacing accents on it, + // then it will not fit in the two spaces available, so the base + // and all its combining spacing accents have to be removed. + expect(s.truncate(2)).toBe("है"); + } + }); + }); + + test("GlyphStrAsyncEllipsizeDevanagari2", () => { + expect.assertions(1); + const s = new GlyphString("हैलो, आप कैसे हैं?", { + sync: false, + onLoad: s => { + expect(s.ellipsize(8)).toBe("हैलो, आप …"); + } + }); + }); + + test("GlyphStrAsyncEllipsizeJapanese", () => { + expect.assertions(1); + const s = new GlyphString("ェドイン", { + sync: false, + onLoad: s => { + expect(s.ellipsize(3)).toBe("ェド…"); + } + }); + }); +}); diff --git a/test/glyphstrpromise.test.js b/test/glyphstrpromise.test.js new file mode 100644 index 0000000..76d7b29 --- /dev/null +++ b/test/glyphstrpromise.test.js @@ -0,0 +1,120 @@ +/* + * glyphstrasync.test.js - test the glyph iteration routines + * + * Copyright © 2018, 2022, 2024 JEDLSoft + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import GlyphString from "../src/GlyphString.js"; + +describe("testglyphstrpromise", () => { + test("GlyphStrAsyncCharIteratorNormal", () => { + expect.assertions(8); + return GlyphString.create("aÄa", { // the A umlaut is a decomposed char + sync: false + }).then(s => { + const it = s.charIterator(); + + expect(it.hasNext()).toBeTruthy(); + expect(it.next()).toBe("a"); + expect(it.hasNext()).toBeTruthy(); + expect(it.next()).toBe("Ä"); + expect(it.hasNext()).toBeTruthy(); + expect(it.next()).toBe("a"); + expect(!it.hasNext()).toBeTruthy(); + expect(it.next()).toBe(undefined); + }); + }); + + test("GlyphStrAsyncCharIteratorEmpty", () => { + expect.assertions(2); + return GlyphString.create("", { + sync: false + }).then(s => { + const it = s.charIterator(); + + expect(!it.hasNext()).toBeTruthy(); + expect(it.next()).toBe(undefined); + }); + }); + + test("GlyphStrAsyncCharIteratorMultipleDecomposed", () => { + expect.assertions(8); + return GlyphString.create("aẬa", { // the accented A is a decomposed char with 2 accents + sync: false + }).then(s => { + const it = s.charIterator(); + + expect(it.hasNext()).toBeTruthy(); + expect(it.next()).toBe("a"); + expect(it.hasNext()).toBeTruthy(); + expect(it.next()).toBe("Ậ"); + expect(it.hasNext()).toBeTruthy(); + expect(it.next()).toBe("a"); + expect(!it.hasNext()).toBeTruthy(); + expect(it.next()).toBe(undefined); + }); + }); + + test("GlyphStrAsyncTruncateWithCombiningAccentsWholeGlyphs", () => { + expect.assertions(1); + return GlyphString.create("aẬbẬcẬdẬe", { // the accented A is a decomposed char with 2 accents + sync: false + }).then(s => { + expect(s.truncate(4)).toBe("aẬbẬ"); + }); + }); + + test("GlyphStrAsyncTruncateThai", () => { + expect.assertions(1); + return GlyphString.create("สวัุสดีคุณเป็นอย่างไรบ้าง", { + sync: false + }).then(s => { + // this tests non-spacing marks that are also non-combining + + expect(s.truncate(4)).toBe("สวัุสดี"); + }); + }); + + test("GlyphStrAsyncTruncateDevanagari1", () => { + expect.assertions(1); + return GlyphString.create("हैलो, आप कैसे हैं?", { + sync: false + }).then(s => { + // if the 2nd base character has combining spacing accents on it, + // then it will not fit in the two spaces available, so the base + // and all its combining spacing accents have to be removed. + expect(s.truncate(2)).toBe("है"); + }); + }); + + test("GlyphStrAsyncEllipsizeDevanagari2", () => { + expect.assertions(1); + return GlyphString.create("हैलो, आप कैसे हैं?", { + sync: false + }).then(s => { + expect(s.ellipsize(8)).toBe("हैलो, आप …"); + }); + }); + + test("GlyphStrAsyncEllipsizeJapanese", () => { + expect.assertions(1); + return GlyphString.create("ェドイン", { + sync: false + }).then(s => { + expect(s.ellipsize(3)).toBe("ェド…"); + }); + }); +}); diff --git a/test/imsi.test.js b/test/imsi.test.js new file mode 100644 index 0000000..aea43ab --- /dev/null +++ b/test/imsi.test.js @@ -0,0 +1,89 @@ +/* + * imsi.test.js - Test the parseImsi() function synchronously. + * + * Copyright © 2024 JEDLSoft + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import PhoneNumber from "../src/PhoneNumber.js"; + +describe("imsi synchronously", () => { + test("RegularImsi3DigitMNC", () => { + expect.assertions(1); + const imsi = "31003014084567890" + const expected = { + mcc: "310", + mnc: "030", + msin: "14084567890" + }; + + const actual = PhoneNumber.parseImsi(imsi); + expect(actual).toStrictEqual(expected); + }); + + test("SpecialImsi1", () => { + expect.assertions(1); + const imsi = "31000201234567" + const expected = { + mcc: "310", + mnc: "00", + msin: "201234567" + }; + + const actual = PhoneNumber.parseImsi(imsi); + expect(actual).toStrictEqual(expected); + }); + + test("BrokenMCC", () => { + expect.assertions(1); + const imsi = "32000414084567890" + const expected = { + mcc: "320", + mnc: "004", + msin: "14084567890" + }; + + // should default to a 3 digit mnc + const actual = PhoneNumber.parseImsi(imsi); + expect(actual).toStrictEqual(expected); + }); + + test("BrokenMNC", () => { + expect.assertions(1); + const imsi = "31014114084567890" + const expected = { + mcc: "310", + mnc: "141", + msin: "14084567890" + }; + + // should default to a 3 digit mnc + const actual = PhoneNumber.parseImsi(imsi); + expect(actual).toStrictEqual(expected); + }); + + test("TooShort", () => { + expect.assertions(1); + const imsi = "31"; + const actual = PhoneNumber.parseImsi(imsi); + expect(actual).toBeFalsy(); + }); + + test("Undefined", () => { + expect.assertions(1); + const actual = PhoneNumber.parseImsi(undefined); + expect(actual).toBeFalsy(); + }); +}); diff --git a/test/testimsiasync.js b/test/imsiasync.test.js similarity index 52% rename from test/testimsiasync.js rename to test/imsiasync.test.js index 9cddd22..0c4e35c 100644 --- a/test/testimsiasync.js +++ b/test/imsiasync.test.js @@ -1,7 +1,7 @@ /* - * testimsiasync.js - Test the parseImsi() function. + * imsiasync.test.js - Test the parseImsi() function. * - * Copyright © 2018, 2022 JEDLSoft + * Copyright © 2018, 2022, 2024 JEDLSoft * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,11 +19,11 @@ import PhoneNumber from "../src/PhoneNumber.js"; -module.exports.imsiasync = { - testRegularImsi3DigitMNC: function(test) { - test.expect(1); - var imsi = "31003014084567890" - var expected = { +describe("imsiasync", () => { + test("RegularImsi3DigitMNC", () => { + expect.assertions(1); + const imsi = "31003014084567890" + const expected = { mcc: "310", mnc: "030", msin: "14084567890" @@ -31,17 +31,16 @@ module.exports.imsiasync = { PhoneNumber.parseImsi(imsi, { sync: false, - onLoad: function(actual) { - test.deepEqual(actual, expected); - test.done(); + onLoad: actual => { + expect(actual).toStrictEqual(expected); } }) - }, + }); - testSpecialImsi1: function(test) { - test.expect(1); - var imsi = "31000201234567" - var expected = { + test("SpecialImsi1", () => { + expect.assertions(1); + const imsi = "31000201234567" + const expected = { mcc: "310", mnc: "00", msin: "201234567" @@ -49,17 +48,16 @@ module.exports.imsiasync = { PhoneNumber.parseImsi(imsi, { sync: false, - onLoad: function(actual) { - test.deepEqual(actual, expected); - test.done(); + onLoad: actual => { + expect(actual).toStrictEqual(expected); } }); - }, + }); - testBrokenMCC: function(test) { - test.expect(1); - var imsi = "32000414084567890" - var expected = { + test("BrokenMCC", () => { + expect.assertions(1); + const imsi = "32000414084567890" + const expected = { mcc: "320", mnc: "004", msin: "14084567890" @@ -68,17 +66,16 @@ module.exports.imsiasync = { // should default to a 3 digit mnc PhoneNumber.parseImsi(imsi, { sync: false, - onLoad: function(actual) { - test.deepEqual(actual, expected); - test.done(); + onLoad: actual => { + expect(actual).toStrictEqual(expected); } }); - }, + }); - testBrokenMNC: function(test) { - test.expect(1); - var imsi = "31014114084567890" - var expected = { + test("BrokenMNC", () => { + expect.assertions(1); + const imsi = "31014114084567890" + const expected = { mcc: "310", mnc: "141", msin: "14084567890" @@ -87,33 +84,30 @@ module.exports.imsiasync = { // should default to a 3 digit mnc PhoneNumber.parseImsi(imsi, { sync: false, - onLoad: function(actual) { - test.deepEqual(actual, expected); - test.done(); + onLoad: actual => { + expect(actual).toStrictEqual(expected); } }); - }, + }); - testTooShort: function(test) { - test.expect(1); - var imsi = "31"; + test("TooShort", () => { + expect.assertions(1); + const imsi = "31"; PhoneNumber.parseImsi(imsi, { sync: false, - onLoad: function(actual) { - test.ok(!actual); - test.done(); + onLoad: actual => { + expect(actual).toBeFalsy(); } }); - }, + }); - testUndefined: function(test) { - test.expect(1); + test("Undefined", () => { + expect.assertions(1); PhoneNumber.parseImsi(undefined, { sync: false, - onLoad: function(actual) { - test.ok(!actual); - test.done(); + onLoad: actual => { + expect(actual).toBeFalsy(); } }); - } -}; + }); +}); diff --git a/test/imsipromise.test.js b/test/imsipromise.test.js new file mode 100644 index 0000000..30304ba --- /dev/null +++ b/test/imsipromise.test.js @@ -0,0 +1,95 @@ +/* + * imsiapromise.test.js - Test the parseImsi() function with promises. + * + * Copyright © 2024 JEDLSoft + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import PhoneNumber from "../src/PhoneNumber.js"; + +describe("imsi with promises", () => { + test("RegularImsi3DigitMNC", () => { + expect.assertions(1); + const imsi = "31003014084567890" + const expected = { + mcc: "310", + mnc: "030", + msin: "14084567890" + }; + + return PhoneNumber.parseImsi(imsi, {sync: false}).then(actual => { + expect(actual).toStrictEqual(expected); + }); + }); + + test("SpecialImsi1", () => { + expect.assertions(1); + const imsi = "31000201234567" + const expected = { + mcc: "310", + mnc: "00", + msin: "201234567" + }; + + return PhoneNumber.parseImsi(imsi, {sync: false}).then(actual => { + expect(actual).toStrictEqual(expected); + }); + }); + + test("BrokenMCC", () => { + expect.assertions(1); + const imsi = "32000414084567890" + const expected = { + mcc: "320", + mnc: "004", + msin: "14084567890" + }; + + // should default to a 3 digit mnc + return PhoneNumber.parseImsi(imsi, {sync: false}).then(actual => { + expect(actual).toStrictEqual(expected); + }); + }); + + test("BrokenMNC", () => { + expect.assertions(1); + const imsi = "31014114084567890" + const expected = { + mcc: "310", + mnc: "141", + msin: "14084567890" + }; + + // should default to a 3 digit mnc + return PhoneNumber.parseImsi(imsi, {sync: false}).then(actual => { + expect(actual).toStrictEqual(expected); + }); + }); + + test("TooShort", () => { + expect.assertions(1); + const imsi = "31"; + return PhoneNumber.parseImsi(imsi, {sync: false}).then(actual => { + expect(actual).toBeFalsy(); + }); + }); + + test("Undefined", () => { + expect.assertions(1); + return PhoneNumber.parseImsi(undefined, {sync: false}).then(actual => { + expect(actual).toBeFalsy(); + }); + }); +}); diff --git a/test/listfmtasync.test.js b/test/listfmtasync.test.js new file mode 100644 index 0000000..a17f7f5 --- /dev/null +++ b/test/listfmtasync.test.js @@ -0,0 +1,67 @@ +/* + * listfmtasync.test.js - test the list formatter object + * + * Copyright © 2018, 2022, 2024 JEDLSoft + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSe-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import ListFmt from "../src/ListFmt.js"; + +describe("testlistfmtasync", () => { + test("ListFmtAsyncConstructorEmpty", () => { + new ListFmt({ + sync: false, + onLoad: fmt => { + expect.assertions(1); + expect(fmt !== null).toBeTruthy(); + } + }); + }); + + test("ListFmtAsyncTestPropertyTwo", () => { + new ListFmt({ + sync: false, + onLoad: fmt => { + expect.assertions(2); + expect(fmt !== null).toBeTruthy(); + expect(fmt.format(["true", "false"])).toBe("true & false"); + } + }); + }); + + test("ListFmtAsyncbnINNumberFormatFour", () => { + new ListFmt({ + locale: "bn-IN", + sync: false, + onLoad: fmt => { + expect.assertions(2); + expect(fmt !== null).toBeTruthy(); + expect(fmt.format(["এক", "দুই", "তিন", "চার"])).toBe("এক, দুই, তিন এবং চার"); + } + }); + }); + + test("ListFmtAsynckoKRNumberFormatThree", () => { + new ListFmt({ + locale: "ko-KR", + sync: false, + onLoad: fmt => { + expect.assertions(2); + expect(fmt !== null).toBeTruthy(); + expect(fmt.format(["하나", "둘", "셋"])).toBe("하나, 둘 및 셋"); + } + }); + }); +}); diff --git a/test/listfmtpromise.test.js b/test/listfmtpromise.test.js new file mode 100644 index 0000000..f292661 --- /dev/null +++ b/test/listfmtpromise.test.js @@ -0,0 +1,63 @@ +/* + * listfmtasync.test.js - test the list formatter object + * + * Copyright © 2018, 2022, 2024 JEDLSoft + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSe-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import ListFmt from "../src/ListFmt.js"; + +describe("testlistfmtpromise", () => { + test("ListFmtAsyncConstructorEmpty", () => { + return ListFmt.create({ + sync: false + }).then(fmt => { + expect.assertions(1); + expect(fmt !== null).toBeTruthy(); + }); + }); + + test("ListFmtAsyncTestPropertyTwo", () => { + return ListFmt.create({ + sync: false + }).then(fmt => { + expect.assertions(2); + expect(fmt !== null).toBeTruthy(); + expect(fmt.format(["true", "false"])).toBe("true & false"); + }); + }); + + test("ListFmtAsyncbnINNumberFormatFour", () => { + return ListFmt.create({ + locale: "bn-IN", + sync: false + }).then(fmt => { + expect.assertions(2); + expect(fmt !== null).toBeTruthy(); + expect(fmt.format(["এক", "দুই", "তিন", "চার"])).toBe("এক, দুই, তিন এবং চার"); + }); + }); + + test("ListFmtAsynckoKRNumberFormatThree", () => { + return ListFmt.create({ + locale: "ko-KR", + sync: false + }).then(fmt => { + expect.assertions(2); + expect(fmt !== null).toBeTruthy(); + expect(fmt.format(["하나", "둘", "셋"])).toBe("하나, 둘 및 셋"); + }); + }); +}); diff --git a/test/locale.test.js b/test/locale.test.js new file mode 100644 index 0000000..60bee89 --- /dev/null +++ b/test/locale.test.js @@ -0,0 +1,714 @@ +/* + * locale.test.js - test the locale object + * + * Copyright © 2018-2019, 2024 JEDLSoft + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import ilib from "../src/ilib.js"; +import Locale from "../src/Locale.js"; + +describe("testlocale", () => { + beforeAll(() => { + ilib.clearCache(); + }); + + test("LocaleConstructor", () => { + expect.assertions(1); + const loc = new Locale(); + + expect(loc !== null).toBeTruthy(); + }); + + test("LocaleConstructorCurrent", () => { + expect.assertions(4); + if (ilib._getPlatform() === "browser") { + // make sure it picks it up from the right place + navigator.language = "en-US"; + } + + const loc = new Locale(); // gives locale of the host JS engine + + expect(loc !== null).toBeTruthy(); + + expect(loc.getLanguage()).toBe("en"); + expect(loc.getRegion()).toBe("US"); + expect(typeof(loc.getVariant()) === "undefined").toBeTruthy(); + }); + + test("LocaleConstructorDefault", () => { + expect.assertions(4); + ilib.setLocale("it-IT"); + + const loc = new Locale(); + + expect(loc !== null).toBeTruthy(); + + expect(loc.getLanguage()).toBe("it"); + expect(loc.getRegion()).toBe("IT"); + expect(typeof(loc.getVariant()) === "undefined").toBeTruthy(); + + delete ilib.locale; + }); + + test("LocaleCopyConstructor", () => { + expect.assertions(4); + const loc2 = new Locale("de", "DE"); + const loc = new Locale(loc2); + + expect(loc !== null).toBeTruthy(); + + expect(loc.getLanguage()).toBe("de"); + expect(loc.getRegion()).toBe("DE"); + expect(typeof(loc.getVariant()) === "undefined").toBeTruthy(); + }); + + test("LocaleConstructorFull", () => { + expect.assertions(4); + const loc = new Locale("en", "US", "Midwest"); + + expect(loc !== null).toBeTruthy(); + + expect(loc.getLanguage()).toBe("en"); + expect(loc.getRegion()).toBe("US"); + expect(loc.getVariant()).toBe("Midwest"); + }); + + test("LocaleConstructorSpecWithVariant", () => { + expect.assertions(5); + const loc = new Locale("en-US-Midwest"); + + expect(loc !== null).toBeTruthy(); + + expect(loc.getLanguage()).toBe("en"); + expect(loc.getRegion()).toBe("US"); + expect(loc.getVariant()).toBe("Midwest"); + expect(typeof(loc.getScript()) === "undefined").toBeTruthy(); + }); + + test("LocaleConstructorSpecWithScript", () => { + expect.assertions(5); + const loc = new Locale("en-US-Latn"); + + expect(loc !== null).toBeTruthy(); + + expect(loc.getLanguage()).toBe("en"); + expect(loc.getRegion()).toBe("US"); + expect(loc.getScript()).toBe("Latn"); + expect(typeof(loc.getVariant()) === "undefined").toBeTruthy(); + }); + + test("LocaleConstructorPartial", () => { + expect.assertions(4); + const loc = new Locale("en", "US"); + + expect(loc !== null).toBeTruthy(); + + expect(loc.getLanguage()).toBe("en"); + expect(loc.getRegion()).toBe("US"); + expect(typeof(loc.getVariant()) === "undefined").toBeTruthy(); + }); + + test("LocaleConstructorSpecPartial", () => { + expect.assertions(4); + const loc = new Locale("en-US"); + + expect(loc !== null).toBeTruthy(); + + expect(loc.getLanguage()).toBe("en"); + expect(loc.getRegion()).toBe("US"); + expect(typeof(loc.getVariant()) === "undefined").toBeTruthy(); + }); + + test("LocaleConstructorShort", () => { + expect.assertions(4); + const loc = new Locale("en"); + + expect(loc !== null).toBeTruthy(); + + expect(loc.getLanguage()).toBe("en"); + expect(typeof(loc.getRegion()) === "undefined").toBeTruthy(); + expect(typeof(loc.getVariant()) === "undefined").toBeTruthy(); + }); + + test("LocaleConstructorUpperCaseLanguage", () => { + expect.assertions(4); + const loc = new Locale("EN", "US"); + + expect(loc !== null).toBeTruthy(); + + expect(loc.getLanguage()).toBe("en"); + expect(loc.getRegion()).toBe("US"); + expect(typeof(loc.getVariant()) === "undefined").toBeTruthy(); + }); + + test("LocaleConstructorLowerCaseRegion", () => { + expect.assertions(4); + const loc = new Locale("en", "us"); + + expect(loc !== null).toBeTruthy(); + + expect(loc.getLanguage()).toBe("en"); + expect(loc.getRegion()).toBe("US"); + expect(typeof(loc.getVariant()) === "undefined").toBeTruthy(); + }); + + test("LocaleConstructorSpecMissingRegion", () => { + expect.assertions(5); + const loc = new Locale("en--Midwest"); + + expect(loc !== null).toBeTruthy(); + + expect(loc.getLanguage()).toBe("en"); + expect(typeof(loc.getRegion()) === "undefined").toBeTruthy(); + expect(loc.getVariant()).toBe("Midwest"); + expect(typeof(loc.getScript()) === "undefined").toBeTruthy(); + }); + + test("LocaleConstructorSpecMissingLanguage", () => { + expect.assertions(5); + const loc = new Locale("-US-Midwest"); + + expect(loc !== null).toBeTruthy(); + + expect(typeof(loc.getLanguage()) === "undefined").toBeTruthy(); + expect(loc.getRegion()).toBe("US"); + expect(loc.getVariant()).toBe("Midwest"); + expect(typeof(loc.getScript()) === "undefined").toBeTruthy(); + }); + + test("LocaleConstructorSpecMissingLanguageAndVariant", () => { + expect.assertions(5); + const loc = new Locale("-US"); + + expect(loc !== null).toBeTruthy(); + + expect(typeof(loc.getLanguage()) === "undefined").toBeTruthy(); + expect(loc.getRegion()).toBe("US"); + expect(typeof(loc.getVariant()) === "undefined").toBeTruthy(); + expect(typeof(loc.getScript()) === "undefined").toBeTruthy(); + }); + + test("LocaleEqualsTrue", () => { + expect.assertions(3); + const loc1 = new Locale("en-US"), + loc2 = new Locale("en", "US"); + + expect(loc1 !== null).toBeTruthy(); + expect(loc2 !== null).toBeTruthy(); + + expect(loc1.equals(loc2)).toBeTruthy(); + }); + + test("LocaleEqualsFalse", () => { + expect.assertions(3); + const loc1 = new Locale("en-US"), + loc2 = new Locale("en", "CA"); + + expect(loc1 !== null).toBeTruthy(); + expect(loc2 !== null).toBeTruthy(); + + expect(!loc1.equals(loc2)).toBeTruthy(); + }); + + test("LocaleEqualsMissing", () => { + expect.assertions(3); + const loc1 = new Locale("en-US"), + loc2 = new Locale("en", "US", "govt"); + + expect(loc1 !== null).toBeTruthy(); + expect(loc2 !== null).toBeTruthy(); + + expect(!loc1.equals(loc2)).toBeTruthy(); + }); + + test("LocaleEqualsTrueFull", () => { + expect.assertions(3); + const loc1 = new Locale("en-US-govt"), + loc2 = new Locale("en", "US", "govt"); + + expect(loc1 !== null).toBeTruthy(); + expect(loc2 !== null).toBeTruthy(); + + expect(loc1.equals(loc2)).toBeTruthy(); + }); + + test("LocaleEqualsTrueShort", () => { + expect.assertions(3); + const loc1 = new Locale("en"), + loc2 = new Locale("en"); + + expect(loc1 !== null).toBeTruthy(); + expect(loc2 !== null).toBeTruthy(); + + expect(loc1.equals(loc2)).toBeTruthy(); + }); + + test("LocaleIsPseudoTrue", () => { + expect.assertions(2); + const loc = new Locale("zxx-XX"); + + expect(loc !== null).toBeTruthy(); + + expect(loc.isPseudo(loc)).toBeTruthy(); + }); + + test("LocaleIsPseudoFalse", () => { + expect.assertions(2); + const loc = new Locale("en-US"); + + expect(loc !== null).toBeTruthy(); + + expect(!loc.isPseudo(loc)).toBeTruthy(); + }); + + test("LocaleIsPseudoFalseButClosLang", () => { + expect.assertions(2); + const loc = new Locale("zxx-US"); + + expect(loc !== null).toBeTruthy(); + + expect(!loc.isPseudo(loc)).toBeTruthy(); + }); + + test("LocaleIsPseudoFalse", () => { + expect.assertions(2); + const loc = new Locale("en-XX"); + + expect(loc !== null).toBeTruthy(); + + expect(!loc.isPseudo(loc)).toBeTruthy(); + }); + + + test("LocaleGetSpecLangOnly", () => { + expect.assertions(2); + const loc = new Locale("en"); + + expect(loc !== null).toBeTruthy(); + + expect(loc.getSpec()).toBe("en"); + }); + + test("LocaleGetSpecRegionOnly", () => { + expect.assertions(2); + const loc = new Locale("CA"); + + expect(loc !== null).toBeTruthy(); + + expect(loc.getSpec()).toBe("CA"); + }); + + test("LocaleGetSpecScriptOnly", () => { + expect.assertions(2); + const loc = new Locale("Latn"); + + expect(loc !== null).toBeTruthy(); + + expect(loc.getSpec()).toBe("Latn"); + }); + + test("LocaleGetSpecVariantOnly", () => { + expect.assertions(2); + const loc = new Locale("asdfasdf"); + + expect(loc !== null).toBeTruthy(); + + expect(loc.getSpec()).toBe("asdfasdf"); + }); + + test("LocaleGetSpecLangAndScript", () => { + expect.assertions(2); + const loc = new Locale("Latn-en"); + + expect(loc !== null).toBeTruthy(); + + expect(loc.getSpec()).toBe("en-Latn"); + }); + + test("LocaleGetSpecLangAndRegion", () => { + expect.assertions(2); + const loc = new Locale("CA-en"); + + expect(loc !== null).toBeTruthy(); + + expect(loc.getSpec()).toBe("en-CA"); + }); + + test("LocaleGetSpecLangAndVariant", () => { + expect.assertions(2); + const loc = new Locale("asdf-en"); + + expect(loc !== null).toBeTruthy(); + + expect(loc.getSpec()).toBe("en-asdf"); + }); + + test("LocaleGetSpecScriptAndRegion", () => { + expect.assertions(2); + const loc = new Locale("CA-Latn"); + + expect(loc !== null).toBeTruthy(); + + expect(loc.getSpec()).toBe("Latn-CA"); + }); + + test("LocaleGetSpecScriptAndVariant", () => { + expect.assertions(2); + const loc = new Locale("asdf-Latn"); + + expect(loc !== null).toBeTruthy(); + + expect(loc.getSpec()).toBe("Latn-asdf"); + }); + + test("LocaleGetSpecRegionAndVariant", () => { + expect.assertions(2); + const loc = new Locale("asdf-CA"); + + expect(loc !== null).toBeTruthy(); + + expect(loc.getSpec()).toBe("CA-asdf"); + }); + + test("LocaleGetSpecLangScriptRegion", () => { + expect.assertions(2); + const loc = new Locale("CA-en-Latn"); + + expect(loc !== null).toBeTruthy(); + + expect(loc.getSpec()).toBe("en-Latn-CA"); + }); + + test("LocaleGetSpecScriptRegionVariant", () => { + expect.assertions(2); + const loc = new Locale("CA-asdf-Latn"); + + expect(loc !== null).toBeTruthy(); + + expect(loc.getSpec()).toBe("Latn-CA-asdf"); + }); + + test("LocaleGetSpecLangScriptVariant", () => { + expect.assertions(2); + const loc = new Locale("asdf-Latn-en"); + + expect(loc !== null).toBeTruthy(); + + expect(loc.getSpec()).toBe("en-Latn-asdf"); + }); + + test("LocaleGetSpecLangRegionVariant", () => { + expect.assertions(2); + const loc = new Locale("asdf-CA-en"); + + expect(loc !== null).toBeTruthy(); + + expect(loc.getSpec()).toBe("en-CA-asdf"); + }); + + test("LocaleGetSpecAll", () => { + expect.assertions(2); + const loc = new Locale("asdf-CA-Latn-en"); + + expect(loc !== null).toBeTruthy(); + + expect(loc.getSpec()).toBe("en-Latn-CA-asdf"); + }); + + test("LocaleM49RegionCodeGetParts", () => { + expect.assertions(4); + const loc = new Locale("en-001"); + + expect(loc !== null).toBeTruthy(); + + expect(loc.getLanguage()).toBe("en"); + expect(loc.getRegion()).toBe("001"); + expect(typeof(loc.getVariant()) === "undefined").toBeTruthy(); + }); + + test("LocaleM49RegionCodeGetParts2", () => { + expect.assertions(4); + const loc = new Locale("en-150"); + + expect(loc !== null).toBeTruthy(); + + expect(loc.getLanguage()).toBe("en"); + expect(loc.getRegion()).toBe("150"); + expect(typeof(loc.getVariant()) === "undefined").toBeTruthy(); + }); + + test("LocaleM49RegionCodeGetSpec", () => { + expect.assertions(2); + const loc = new Locale("en-001"); + + expect(loc !== null).toBeTruthy(); + + expect(loc.getSpec()).toBe("en-001"); + }); + + test("LocaleNoLocale", () => { + expect.assertions(6); + const loc = new Locale("-"); + + expect(loc !== null).toBeTruthy(); + + expect(loc.getSpec()).toBe(""); + expect(typeof(loc.getLanguage()) === "undefined").toBeTruthy(); + expect(typeof(loc.getRegion()) === "undefined").toBeTruthy(); + expect(typeof(loc.getScript()) === "undefined").toBeTruthy(); + expect(typeof(loc.getVariant()) === "undefined").toBeTruthy(); + }); + + + test("LocaleRegionMap1", () => { + expect.assertions(1); + expect(Locale.regionAlpha2ToAlpha3("SG")).toBe("SGP"); + }); + + test("LocaleRegionMap2", () => { + expect.assertions(1); + expect(Locale.regionAlpha2ToAlpha3("VN")).toBe("VNM"); + }); + + test("LocaleRegionMap3", () => { + expect.assertions(1); + expect(Locale.regionAlpha2ToAlpha3("KR")).toBe("KOR"); + }); + + test("LocaleRegionMapEmpty", () => { + expect.assertions(1); + expect(Locale.regionAlpha2ToAlpha3("")).toBe(""); + }); + + test("LocaleRegionMapUnknown", () => { + expect.assertions(1); + expect(Locale.regionAlpha2ToAlpha3("QQ")).toBe("QQ"); + }); + + test("LocaleRegionMapWrongCase", () => { + expect.assertions(1); + expect(Locale.regionAlpha2ToAlpha3("sg")).toBe("sg"); + }); + + test("LocaleRegionMapUndefined", () => { + expect.assertions(1); + expect(typeof(Locale.regionAlpha2ToAlpha3(undefined)) === "undefined").toBeTruthy(); + }); + + test("LocaleLanguageMap1", () => { + expect.assertions(1); + expect(Locale.languageAlpha1ToAlpha3("ko")).toBe("kor"); + }); + + test("LocaleLanguageMap2", () => { + expect.assertions(1); + expect(Locale.languageAlpha1ToAlpha3("th")).toBe("tha"); + }); + + test("LocaleLanguageMap3", () => { + expect.assertions(1); + expect(Locale.languageAlpha1ToAlpha3("hr")).toBe("hrv"); + }); + + test("LocaleLanguageMapEmpty", () => { + expect.assertions(1); + expect(Locale.languageAlpha1ToAlpha3("")).toBe(""); + }); + + test("LocaleLanguageMapUnknown", () => { + expect.assertions(1); + expect(Locale.languageAlpha1ToAlpha3("qq")).toBe("qq"); + }); + + test("LocaleLanguageMapWrongCase", () => { + expect.assertions(1); + expect(Locale.languageAlpha1ToAlpha3("EN")).toBe("EN"); + }); + + test("LocaleLanguageMapUndefined", () => { + expect.assertions(1); + expect(typeof(Locale.languageAlpha1ToAlpha3(undefined)) === "undefined").toBeTruthy(); + }); + + test("LocaleGetLanguageAlpha3_1", () => { + expect.assertions(2); + const loc = new Locale("en-US"); + + expect(loc !== null).toBeTruthy(); + + expect(loc.getLanguageAlpha3()).toBe("eng"); + }); + + test("LocaleGetLanguageAlpha3_2", () => { + expect.assertions(2); + const loc = new Locale("ru-RU"); + + expect(loc !== null).toBeTruthy(); + + expect(loc.getLanguageAlpha3()).toBe("rus"); + }); + + test("LocaleGetLanguageAlpha3_3", () => { + expect.assertions(2); + const loc = new Locale("gv-GB"); + + expect(loc !== null).toBeTruthy(); + + expect(loc.getLanguageAlpha3()).toBe("glv"); + }); + + test("LocaleGetLanguageAlpha3NoLanguage", () => { + expect.assertions(2); + const loc = new Locale("GB"); + + expect(loc !== null).toBeTruthy(); + + expect(typeof(loc.getLanguageAlpha3()) === "undefined").toBeTruthy(); + }); + + test("LocaleGetRegionAlpha3_1", () => { + expect.assertions(2); + const loc = new Locale("en-US"); + + expect(loc !== null).toBeTruthy(); + + expect(loc.getRegionAlpha3()).toBe("USA"); + }); + + test("LocaleGetRegionAlpha3_2", () => { + expect.assertions(2); + const loc = new Locale("ru-RU"); + + expect(loc !== null).toBeTruthy(); + + expect(loc.getRegionAlpha3()).toBe("RUS"); + }); + + test("LocaleGetRegionAlpha3_3", () => { + expect.assertions(2); + const loc = new Locale("gv-GB"); + + expect(loc !== null).toBeTruthy(); + + expect(loc.getRegionAlpha3()).toBe("GBR"); + }); + + test("LocaleGetRegionAlpha3NoRegion", () => { + expect.assertions(2); + const loc = new Locale("en"); + + expect(loc !== null).toBeTruthy(); + + expect(typeof(loc.getRegionAlpha3()) === "undefined").toBeTruthy(); + }); + + test("LocaleGetAvailableLocalesDefault", () => { + expect.assertions(2); + const locales = Locale.getAvailableLocales(); + expect(typeof(locales) !== "undefined").toBeTruthy(); + expect(locales.length > 0).toBeTruthy(); + }); + + test("LocaleGetAvailableLocalesCallback", () => { + expect.assertions(2); + Locale.getAvailableLocales(true, locales => { + expect(typeof(locales) !== "undefined").toBeTruthy(); + expect(locales.length > 0).toBeTruthy(); + }); + }); + + test("LocaleGetLanguageSpecSimple", () => { + expect.assertions(2); + + const loc = new Locale("en"); + expect(loc !== null).toBeTruthy(); + + expect(loc.getLangSpec()).toBe("en"); + }); + + test("LocaleGetLanguageSpecLeaveOutRegionAndVariant", () => { + expect.assertions(2); + + const loc = new Locale("en-US-MILITARY"); + expect(loc !== null).toBeTruthy(); + + expect(loc.getLangSpec()).toBe("en"); + }); + + test("LocaleGetLanguageSpecIncludeScript", () => { + expect.assertions(2); + + const loc = new Locale("zh-Hans"); + expect(loc !== null).toBeTruthy(); + + expect(loc.getLangSpec()).toBe("zh-Hans"); + }); + + test("LocaleGetLanguageSpecIncludeScriptButNotOthers", () => { + expect.assertions(2); + + const loc = new Locale("zh-Hans-CN-GOVT"); + expect(loc !== null).toBeTruthy(); + + expect(loc.getLangSpec()).toBe("zh-Hans"); + }); + + test("LocaleGetLanguageSpecLanguageAndScriptMissing", () => { + expect.assertions(2); + + const loc = new Locale("CN"); + expect(loc !== null).toBeTruthy(); + + expect(loc.getLangSpec()).toBe(""); + }); + + test("LocaleGetLanguageSpecNoScriptWithoutLanguage", () => { + expect.assertions(2); + + const loc = new Locale("Hans-CN"); + expect(loc !== null).toBeTruthy(); + + expect(loc.getLangSpec()).toBe(""); + }); + + test("LocaleConstructorCalledWithNonStrings", () => { + expect.assertions(8); + + function a(a) { return a; } + + try { + let loc = new Locale(true, true, false, true); + expect(loc.getLangSpec()).toBe(""); + loc = new Locale(a, a, a, a); + expect(loc.getSpec()).toBe(""); + loc = new Locale(4, 4, 4, 4); + expect(loc.getSpec()).toBe(""); + loc = new Locale({}, {}, {}, {}); + expect(loc.getSpec()).toBe(""); + + loc = new Locale(true); + expect(loc.getSpec()).toBe(""); + loc = new Locale(a); + expect(loc.getSpec()).toBe(""); + loc = new Locale(4); + expect(loc.getSpec()).toBe(""); + loc = new Locale({}); + expect(loc.getSpec()).toBe(""); + } catch (e) { + test.fail(); + } + }); +}); \ No newline at end of file diff --git a/test/localeinfoasync.test.js b/test/localeinfoasync.test.js new file mode 100644 index 0000000..00fcffc --- /dev/null +++ b/test/localeinfoasync.test.js @@ -0,0 +1,122 @@ +/* + * localeinfoasync.test.js - test the locale info object + * + * Copyright © 2018-2019, 2024 JEDLSoft + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import LocaleInfo from "../src/LocaleInfo.js"; + +describe("testlocaleinfoasync", () => { + test("LocaleInfoAsyncConstructor", () => { + expect.assertions(5); + new LocaleInfo(undefined, { + sync: false, + onLoad: info => { + expect(info !== null).toBeTruthy(); + + const loc = info.getLocale(); + expect(loc !== null).toBeTruthy(); + + expect(loc.getLanguage()).toBe("en"); + expect(loc.getRegion()).toBe("US"); + expect(typeof(loc.getVariant()) === "undefined").toBeTruthy(); + } + }); + }); + + test("LocaleInfoAsyncConstructorGivenLocale", () => { + expect.assertions(4); + new LocaleInfo("de-DE", { + sync: false, + onLoad: info => { + expect(info !== null).toBeTruthy(); + + const loc = info.getLocale(); + + expect(loc.getLanguage()).toBe("de"); + expect(loc.getRegion()).toBe("DE"); + expect(typeof(loc.getVariant()) === "undefined").toBeTruthy(); + } + }); + }); + + test("LocaleInfoAsyncGetTimeZoneDefault", () => { + expect.assertions(2); + new LocaleInfo("zz-ZZ", { + sync: false, + onLoad: info => { + expect(info !== null).toBeTruthy(); + + expect(info.getTimeZone()).toBe("Etc/UTC"); + } + }); + }); + + test("LocaleInfoAsyncGetCurrencyUnknown", () => { + expect.assertions(2); + new LocaleInfo("zxx-XX", { + sync: false, + onLoad: info => { + expect(info !== null).toBeTruthy(); + + expect(info.getCurrency()).toBe("USD"); + } + }); + }); + + test("LocaleInfoAsyncGetDecimalSeparatorfor_ko_KR", () => { + expect.assertions(5); + new LocaleInfo("ko-KR", { + sync: false, + onLoad: info => { + expect(info !== null).toBeTruthy(); + expect(info.getDecimalSeparator()).toBe("."); + expect(info.getGroupingSeparator()).toBe(","); + expect(info.getPercentageFormat()).toBe("{n}%"); + expect(info.getCurrencyFormats().common).toBe("{s}{n}"); + } + }); + }); + + test("LocaleInfoAsyncGetDecimalSeparatorfor_fr_FR", () => { + expect.assertions(5); + new LocaleInfo("fr-FR", { + sync: false, + onLoad: info => { + expect(info !== null).toBeTruthy(); + expect(info.getDecimalSeparator()).toBe(","); + expect(info.getGroupingSeparator()).toBe(" "); + expect(info.getPercentageFormat()).toBe("{n} %"); + expect(info.getCurrencyFormats().common).toBe("{n} {s}"); + } + }); + }); + + test("LocaleInfoAsyncGetDecimalSeparatorfor_zh_Hant_US", () => { + expect.assertions(5); + // test mixing locale parts for a non-standard locale + new LocaleInfo("zh-Hant-US", { + sync: false, + onLoad: info => { + expect(info !== null).toBeTruthy(); + expect(info.getDecimalSeparator()).toBe("."); + expect(info.getGroupingSeparator()).toBe(","); + expect(info.getRoundingMode()).toBe("halfdown"); + expect(info.getCurrency()).toBe("USD"); + } + }); + }); +}); diff --git a/test/localeinfopromise.test.js b/test/localeinfopromise.test.js new file mode 100644 index 0000000..3f52e55 --- /dev/null +++ b/test/localeinfopromise.test.js @@ -0,0 +1,115 @@ +/* + * localeinfoasync.test.js - test the locale info object + * + * Copyright © 2018-2019, 2024 JEDLSoft + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import LocaleInfo from "../src/LocaleInfo.js"; + +describe("testlocaleinfopromise", () => { + test("LocaleInfoAsyncConstructor", () => { + expect.assertions(5); + return LocaleInfo.create(undefined, { + sync: false + }).then(info => { + expect(info !== null).toBeTruthy(); + + const loc = info.getLocale(); + expect(loc !== null).toBeTruthy(); + + expect(loc.getLanguage()).toBe("en"); + expect(loc.getRegion()).toBe("US"); + expect(typeof(loc.getVariant()) === "undefined").toBeTruthy(); + }); + }); + + test("LocaleInfoAsyncConstructorGivenLocale", () => { + expect.assertions(4); + return LocaleInfo.create("de-DE", { + sync: false + }).then(info => { + expect(info !== null).toBeTruthy(); + + const loc = info.getLocale(); + + expect(loc.getLanguage()).toBe("de"); + expect(loc.getRegion()).toBe("DE"); + expect(typeof(loc.getVariant()) === "undefined").toBeTruthy(); + }); + }); + + test("LocaleInfoAsyncGetTimeZoneDefault", () => { + expect.assertions(2); + return LocaleInfo.create("zz-ZZ", { + sync: false + }).then(info => { + expect(info !== null).toBeTruthy(); + + expect(info.getTimeZone()).toBe("Etc/UTC"); + }); + }); + + test("LocaleInfoAsyncGetCurrencyUnknown", () => { + expect.assertions(2); + return LocaleInfo.create("zxx-XX", { + sync: false + }).then(info => { + expect(info !== null).toBeTruthy(); + + expect(info.getCurrency()).toBe("USD"); + }); + }); + + test("LocaleInfoAsyncGetDecimalSeparatorfor_ko_KR", () => { + expect.assertions(5); + return LocaleInfo.create("ko-KR", { + sync: false + }).then(info => { + expect(info !== null).toBeTruthy(); + expect(info.getDecimalSeparator()).toBe("."); + expect(info.getGroupingSeparator()).toBe(","); + expect(info.getPercentageFormat()).toBe("{n}%"); + expect(info.getCurrencyFormats().common).toBe("{s}{n}"); + }); + }); + + test("LocaleInfoAsyncGetDecimalSeparatorfor_fr_FR", () => { + expect.assertions(5); + return LocaleInfo.create("fr-FR", { + sync: false + }).then(info => { + expect(info !== null).toBeTruthy(); + expect(info.getDecimalSeparator()).toBe(","); + expect(info.getGroupingSeparator()).toBe(" "); + expect(info.getPercentageFormat()).toBe("{n} %"); + expect(info.getCurrencyFormats().common).toBe("{n} {s}"); + }); + }); + + test("LocaleInfoAsyncGetDecimalSeparatorfor_zh_Hant_US", () => { + expect.assertions(5); + // test mixing locale parts for a non-standard locale + return LocaleInfo.create("zh-Hant-US", { + sync: false + }).then(info => { + expect(info !== null).toBeTruthy(); + expect(info.getDecimalSeparator()).toBe("."); + expect(info.getGroupingSeparator()).toBe(","); + expect(info.getRoundingMode()).toBe("halfdown"); + expect(info.getCurrency()).toBe("USD"); + }); + }); +}); diff --git a/test/localematchasync.test.js b/test/localematchasync.test.js new file mode 100644 index 0000000..bbd0862 --- /dev/null +++ b/test/localematchasync.test.js @@ -0,0 +1,74 @@ +/* + * localematchasync.test.js - test the locale matcher object + * + * Copyright © 2018, 2022, 2024 JEDLSoft + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import LocaleMatcher from "../src/LocaleMatcher.js"; + +describe("testlocalematchasync", () => { + test("LocaleMatcherAsyncConstructor", () => { + expect.assertions(1); + new LocaleMatcher({ + sync: false, + onLoad: lm => { + expect(lm !== null).toBeTruthy(); + } + }); + }); + + test("LocaleMatcherAsyncGetLikelyLocaleByLanguage1", () => { + expect.assertions(3); + new LocaleMatcher({ + locale: "uz", + sync: false, + onLoad: lm => { + expect(typeof(lm) !== "undefined").toBeTruthy(); + const locale = lm.getLikelyLocale(); + expect(typeof(locale) !== "undefined").toBeTruthy(); + expect(locale.getSpec()).toBe("uz-Latn-UZ"); + } + }); + }); + + test("LocaleMatcherAsyncGetLikelyLocaleByRegion", () => { + expect.assertions(3); + new LocaleMatcher({ + locale: "UZ", + sync: false, + onLoad: lm => { + expect(typeof(lm) !== "undefined").toBeTruthy(); + const locale = lm.getLikelyLocale(); + expect(typeof(locale) !== "undefined").toBeTruthy(); + expect(locale.getSpec()).toBe("uz-Latn-UZ"); + } + }); + }); + + test("LocaleMatcherAsyncGetLikelyLocaleByScript", () => { + expect.assertions(3); + new LocaleMatcher({ + locale: "Arab", + sync: false, + onLoad: lm => { + expect(typeof(lm) !== "undefined").toBeTruthy(); + const locale = lm.getLikelyLocale(); + expect(typeof(locale) !== "undefined").toBeTruthy(); + expect(locale.getSpec()).toBe("ar-Arab-EG"); + } + }); + }); +}); diff --git a/test/localematchpromise.test.js b/test/localematchpromise.test.js new file mode 100644 index 0000000..77aeec6 --- /dev/null +++ b/test/localematchpromise.test.js @@ -0,0 +1,70 @@ +/* + * localematchasync.test.js - test the locale matcher object + * + * Copyright © 2018, 2022, 2024 JEDLSoft + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import LocaleMatcher from "../src/LocaleMatcher.js"; + +describe("testlocalematchpromise", () => { + test("LocaleMatcherAsyncConstructor", () => { + expect.assertions(1); + return LocaleMatcher.create({ + sync: false + }).then(lm => { + expect(lm !== null).toBeTruthy(); + }); + }); + + test("LocaleMatcherAsyncGetLikelyLocaleByLanguage1", () => { + expect.assertions(3); + return LocaleMatcher.create({ + locale: "uz", + sync: false + }).then(lm => { + expect(typeof(lm) !== "undefined").toBeTruthy(); + const locale = lm.getLikelyLocale(); + expect(typeof(locale) !== "undefined").toBeTruthy(); + expect(locale.getSpec()).toBe("uz-Latn-UZ"); + }); + }); + + test("LocaleMatcherAsyncGetLikelyLocaleByRegion", () => { + expect.assertions(3); + return LocaleMatcher.create({ + locale: "UZ", + sync: false + }).then(lm => { + expect(typeof(lm) !== "undefined").toBeTruthy(); + const locale = lm.getLikelyLocale(); + expect(typeof(locale) !== "undefined").toBeTruthy(); + expect(locale.getSpec()).toBe("uz-Latn-UZ"); + }); + }); + + test("LocaleMatcherAsyncGetLikelyLocaleByScript", () => { + expect.assertions(3); + return LocaleMatcher.create({ + locale: "Arab", + sync: false + }).then(lm => { + expect(typeof(lm) !== "undefined").toBeTruthy(); + const locale = lm.getLikelyLocale(); + expect(typeof(locale) !== "undefined").toBeTruthy(); + expect(locale.getSpec()).toBe("ar-Arab-EG"); + }); + }); +}); diff --git a/test/nameasync.test.js b/test/nameasync.test.js new file mode 100644 index 0000000..9c5d829 --- /dev/null +++ b/test/nameasync.test.js @@ -0,0 +1,110 @@ +/* + * nameasync.test.js - test the name object + * + * Copyright © 2018, 2022, 2024 JEDLSoft + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import Name from "../src/Name.js"; + +describe("testnameasync", () => { + test("NameAsyncEmptyConstructor", () => { + expect.assertions(1); + new Name(undefined, { + sync: false, + onLoad: name => { + expect(typeof(name) === "undefined").toBeTruthy(); + } + }); + }); + + test("NameAsyncCopyConstructor", () => { + expect.assertions(2); + new Name({ + prefix: "a", + givenName: "b", + middleName: "c", + familyName: "d", + suffix: "e", + honorific: "x" + }, { + sync: false, + onLoad: name => { + expect(typeof(name) !== "undefined").toBeTruthy(); + + expect(name).toEqual(expect.objectContaining({ + prefix: "a", + givenName: "b", + middleName: "c", + familyName: "d", + suffix: "e", + honorific: "x" + })); + } + }); + }); + + test("NameAsyncDEWithMultiplePrefixes", () => { + expect.assertions(2); + new Name("Herr Dr. Josef Hans Jürgen Herzheim", { + locale: "de-DE", + sync: false, + onLoad: name => { + expect(typeof(name) !== "undefined").toBeTruthy(); + + expect(name).toEqual(expect.objectContaining({ + prefix: "Herr Dr.", + givenName: "Josef", + middleName: "Hans Jürgen", + familyName: "Herzheim" + })); + } + }); + }); + + test("NameAsyncESFull", () => { + expect.assertions(2); + new Name("Juan Carlos Maria León Arroyo", { + locale: "es-ES", + sync: false, + onLoad: name => { + expect(typeof(name) !== "undefined").toBeTruthy(); + + expect(name).toEqual(expect.objectContaining({ + givenName: "Juan", + middleName: "Carlos Maria", + familyName: "León Arroyo" + })); + } + }); + }); + + test("NameAsyncZHHonorific", () => { + expect.assertions(2); + new Name("堂哥胡锦涛", { + locale: "zh-CN", + sync: false, + onLoad: name => { + expect(typeof(name) !== "undefined").toBeTruthy(); + + expect(name).toEqual(expect.objectContaining({ + prefix: "堂哥", + givenName: "锦涛", + familyName: "胡" + })); + } + }); + }); +}); diff --git a/test/testnamefmtasync.js b/test/namefmtasync.test.js similarity index 59% rename from test/testnamefmtasync.js rename to test/namefmtasync.test.js index f2a7c0a..36ca3fd 100644 --- a/test/testnamefmtasync.js +++ b/test/namefmtasync.test.js @@ -1,7 +1,7 @@ /* - * testnamefmtasync.js - test the name formatter object + * namefmtasync.test.js - test the name formatter object * - * Copyright © 2018, 2022 JEDLSoft + * Copyright © 2018, 2022, 2024 JEDLSoft * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,33 +20,31 @@ import NameFmt from "../src/NameFmt.js"; import Name from "../src/Name.js"; -export const testnamefmtasync = { - testNameFmtAsyncConstructor: function(test) { - test.expect(1); +describe("testnamefmtasync", () => { + test("NameFmtAsyncConstructor", () => { + expect.assertions(1); new NameFmt({ sync: false, - onLoad: function(fmt) { - test.ok(typeof(fmt) !== "undefined"); - test.done(); + onLoad: fmt => { + expect(typeof(fmt) !== "undefined").toBeTruthy(); } }); - }, + }); - testNameFmtAsyncGetBogus: function(test) { - test.expect(1); + test("NameFmtAsyncGetBogus", () => { + expect.assertions(1); new NameFmt({ locale: "ii-II", sync: false, - onLoad: function(fmt) { - test.equal(fmt.getLocale().getSpec(), "ii-II"); - test.done(); + onLoad: fmt => { + expect(fmt.getLocale().getSpec()).toBe("ii-II"); } }); - }, + }); - testNameFmtAsyncENFull: function(test) { - test.expect(1); + test("NameFmtAsyncENFull", () => { + expect.assertions(1); new Name({ prefix: "Mr.", givenName: "John", @@ -55,22 +53,21 @@ export const testnamefmtasync = { suffix: "Phd." }, { sync: false, - onLoad: function(name) { + onLoad: name => { new NameFmt({ style: "full", sync: false, - onLoad: function(fmt) { - test.equal(fmt.format(name), "Mr. John Kevin Smith Phd."); - test.done(); + onLoad: fmt => { + expect(fmt.format(name)).toBe("Mr. John Kevin Smith Phd."); } }); } }); - }, + }); - testNameFmtAsyncDEFull: function(test) { - test.expect(1); + test("NameFmtAsyncDEFull", () => { + expect.assertions(1); new Name({ prefix: "Hr.", givenName: "Andreas", @@ -80,22 +77,21 @@ export const testnamefmtasync = { }, { locale: "de-DE", sync: false, - onLoad: function(name) { + onLoad: name => { new NameFmt({ style: "full", locale: "de-DE", sync: false, - onLoad: function(fmt) { - test.equal(fmt.format(name), "Hr. Andreas Helmut Schmidt MdB"); - test.done(); + onLoad: fmt => { + expect(fmt.format(name)).toBe("Hr. Andreas Helmut Schmidt MdB"); } }); } }); - }, + }); - testNameFmtAsyncZHFormalLong: function(test) { - test.expect(1); + test("NameFmtAsyncZHFormalLong", () => { + expect.assertions(1); new Name({ honorific: "医生", givenName: "芳", @@ -103,23 +99,22 @@ export const testnamefmtasync = { }, { locale: "zh-Hans-CN", sync: false, - onLoad: function(name) { + onLoad: name => { new NameFmt({ style: "formal_long", locale: "zh-Hans-CN", sync: false, - onLoad: function(fmt) { - test.equal(fmt.format(name), "李芳医生"); - test.done(); + onLoad: fmt => { + expect(fmt.format(name)).toBe("李芳医生"); } }); } }); - }, + }); - testNameFmtAsyncZHFormalLong: function(test) { - test.expect(1); + test("NameFmtAsyncZHFormalLong", () => { + expect.assertions(1); new Name({ honorific: "닥터", givenName: "은성", @@ -127,18 +122,17 @@ export const testnamefmtasync = { }, { locale: "ko-KR", sync: false, - onLoad: function(name) { + onLoad: name => { new NameFmt({ style: "formal_long", locale: "ko-KR", sync: false, - onLoad: function(fmt) { - test.equal(fmt.format(name), "닥터 박은성"); - test.done(); + onLoad: fmt => { + expect(fmt.format(name)).toBe("닥터 박은성"); } }); } }); - } -}; + }); +}); diff --git a/test/testnamefmtpromise.js b/test/namefmtpromise.test.js similarity index 53% rename from test/testnamefmtpromise.js rename to test/namefmtpromise.test.js index 6001f96..6fc66f9 100644 --- a/test/testnamefmtpromise.js +++ b/test/namefmtpromise.test.js @@ -1,7 +1,7 @@ /* - * testnamefmtasync.js - test the name formatter object + * namefmtasync.test.js - test the name formatter object * - * Copyright © 2018, 2022 JEDLSoft + * Copyright © 2018, 2022, 2024 JEDLSoft * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,32 +20,29 @@ import NameFmt from "../src/NameFmt.js"; import Name from "../src/Name.js"; -export const testnamefmtpromise = { - testNameFmtAsyncConstructor: function(test) { - test.expect(1); - NameFmt.create({ +describe("testnamefmtpromise", () => { + test("NameFmtAsyncConstructor", () => { + expect.assertions(1); + return NameFmt.create({ sync: false - }).then(function(fmt) { - test.ok(typeof(fmt) !== "undefined"); - test.done(); + }).then(fmt => { + expect(typeof(fmt) !== "undefined").toBeTruthy(); }); - }, + }); - testNameFmtAsyncGetBogus: function(test) { - test.expect(1); - NameFmt.create({ + test("NameFmtAsyncGetBogus", () => { + expect.assertions(1); + return NameFmt.create({ locale: "ii-II", sync: false - }).then(function(fmt) { - test.equal(fmt.getLocale().getSpec(), "ii-II"); - test.done(); + }).then(fmt => { + expect(fmt.getLocale().getSpec()).toBe("ii-II"); }); + }); - }, - - testNameFmtAsyncENFull: function(test) { - test.expect(1); - Name.create({ + test("NameFmtAsyncENFull", () => { + expect.assertions(1); + return Name.create({ prefix: "Mr.", givenName: "John", middleName: "Kevin", @@ -53,20 +50,19 @@ export const testnamefmtpromise = { suffix: "Phd." }, { sync: false - }).then(function(name) { + }).then(name => { return NameFmt.create({ style: "full", sync: false - }).then(function(fmt) { - test.equal(fmt.format(name), "Mr. John Kevin Smith Phd."); - test.done(); + }).then(fmt => { + expect(fmt.format(name)).toBe("Mr. John Kevin Smith Phd."); }); }); - }, + }); - testNameFmtAsyncDEFull: function(test) { - test.expect(1); - Name.create({ + test("NameFmtAsyncDEFull", () => { + expect.assertions(1); + return Name.create({ prefix: "Hr.", givenName: "Andreas", middleName: "Helmut", @@ -75,57 +71,54 @@ export const testnamefmtpromise = { }, { locale: "de-DE", sync: false - }).then(function(name) { - NameFmt.create({ + }).then(name => { + return NameFmt.create({ style: "full", locale: "de-DE", sync: false - }).then(function(fmt) { - test.equal(fmt.format(name), "Hr. Andreas Helmut Schmidt MdB"); - test.done(); + }).then(fmt => { + expect(fmt.format(name)).toBe("Hr. Andreas Helmut Schmidt MdB"); }); }); - }, + }); - testNameFmtAsyncZHFormalLong: function(test) { - test.expect(1); - Name.create({ + test("NameFmtAsyncZHFormalLong", () => { + expect.assertions(1); + return Name.create({ honorific: "医生", givenName: "芳", familyName: "李" }, { locale: "zh-Hans-CN", sync: false - }).then(function(name) { - NameFmt.create({ + }).then(name => { + return NameFmt.create({ style: "formal_long", locale: "zh-Hans-CN", sync: false - }).then(function(fmt) { - test.equal(fmt.format(name), "李芳医生"); - test.done(); + }).then(fmt => { + expect(fmt.format(name)).toBe("李芳医生"); }); }); - }, + }); - testNameFmtAsyncZHFormalLong: function(test) { - test.expect(1); - Name.create({ + test("NameFmtAsyncZHFormalLong", () => { + expect.assertions(1); + return Name.create({ honorific: "닥터", givenName: "은성", familyName: "박" }, { locale: "ko-KR", sync: false - }).then(function(name) { - NameFmt.create({ + }).then(name => { + return NameFmt.create({ style: "formal_long", locale: "ko-KR", sync: false - }).then(function(fmt) { - test.equal(fmt.format(name), "닥터 박은성"); - test.done(); + }).then(fmt => { + expect(fmt.format(name)).toBe("닥터 박은성"); }); }); - } -}; + }); +}); diff --git a/test/namepromise.test.js b/test/namepromise.test.js new file mode 100644 index 0000000..184b80e --- /dev/null +++ b/test/namepromise.test.js @@ -0,0 +1,105 @@ +/* + * nameasync.test.js - test the name object + * + * Copyright © 2018, 2022, 2024 JEDLSoft + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import Name from "../src/Name.js"; + +describe("testnamepromise", () => { + test("NameAsyncEmptyConstructor", () => { + expect.assertions(1); + return Name.create(undefined, { + sync: false + }).then(name => { + expect(typeof(name) === "undefined").toBeTruthy(); + }); + }); + + test("NameAsyncCopyConstructor", () => { + expect.assertions(2); + return Name.create({ + prefix: "a", + givenName: "b", + middleName: "c", + familyName: "d", + suffix: "e", + honorific: "x" + }, { + sync: false + }).then(name => { + expect(typeof(name) !== "undefined").toBeTruthy(); + + expect(name).toEqual(expect.objectContaining({ + prefix: "a", + givenName: "b", + middleName: "c", + familyName: "d", + suffix: "e", + honorific: "x" + })); + }); + }); + + test("NameAsyncDEWithMultiplePrefixes", () => { + expect.assertions(2); + return Name.create("Herr Dr. Josef Hans Jürgen Herzheim", { + locale: "de-DE", + sync: false + }).then(name => { + expect(typeof(name) !== "undefined").toBeTruthy(); + + expect(name).toEqual(expect.objectContaining({ + prefix: "Herr Dr.", + givenName: "Josef", + middleName: "Hans Jürgen", + familyName: "Herzheim" + })); + }); + }); + + test("NameAsyncESFull", () => { + expect.assertions(2); + return Name.create("Juan Carlos Maria León Arroyo", { + locale: "es-ES", + sync: false + }).then(name => { + expect(typeof(name) !== "undefined").toBeTruthy(); + + expect(name).toEqual(expect.objectContaining({ + givenName: "Juan", + middleName: "Carlos Maria", + familyName: "León Arroyo" + })); + }); + }); + + test("NameAsyncZHHonorific", () => { + expect.assertions(2); + return Name.create("堂哥胡锦涛", { + locale: "zh-CN", + sync: false + }).then(name => { + expect(typeof(name) !== "undefined").toBeTruthy(); + + expect(name).toEqual(expect.objectContaining({ + prefix: "堂哥", + givenName: "锦涛", + familyName: "胡" + })); + }); + }); +}); diff --git a/test/testnormalizeasync.js b/test/normalizeasync.test.js similarity index 56% rename from test/testnormalizeasync.js rename to test/normalizeasync.test.js index 65bcd94..d29e4c1 100644 --- a/test/testnormalizeasync.js +++ b/test/normalizeasync.test.js @@ -1,7 +1,7 @@ /* - * testnormalizeasync.js - test phonenumber normalize function() + * normalizeasync.test.js - test phonenumber normalize function() * - * Copyright © 2018, 2022 JEDLSoft + * Copyright © 2018, 2022, 2024 JEDLSoft * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,64 +19,61 @@ import PhoneNumber from "../src/PhoneNumber.js"; -export const testnormalizeasync = { - testNormalizeAsyncIDDPrefix: function(test) { - test.expect(1); +describe("testnormalizeasync", () => { + test("NormalizeAsyncIDDPrefix", () => { + expect.assertions(1); new PhoneNumber("011 31 456 3453434", { locale: 'en-US', sync: false, - onLoad: function(parsed) { + onLoad: parsed => { parsed.normalize({ locale: 'en-US', sync: false, - onLoad: function(normalized) { - var expected = "+314563453434"; - test.equal(normalized, expected); - test.done(); + onLoad: normalized => { + const expected = "+314563453434"; + expect(normalized).toBe(expected); } }); } }); - }, + }); - testNormalizeAsyncLDNumberUsingDEMCC: function(test) { - test.expect(1); + test("NormalizeAsyncLDNumberUsingDEMCC", () => { + expect.assertions(1); new PhoneNumber("02302 654321", { locale: 'de-DE', sync: false, - onLoad: function(parsed) { + onLoad: parsed => { parsed.normalize({ mcc: "262", locale: 'en-US', sync: false, - onLoad: function(normalized) { - var expected = "+492302654321"; + onLoad: normalized => { + const expected = "+492302654321"; - test.equal(normalized, expected); // 'de-DE' - test.done(); + expect(normalized).toBe(expected); // 'de-DE' } }); } }); - }, + }); - testNormalizeAsyncLDNumberUsingUSSpanishLocale: function(test) { - test.expect(1); + test("NormalizeAsyncLDNumberUsingUSSpanishLocale", () => { + expect.assertions(1); new PhoneNumber("650 7654321", { locale: 'es-US', sync: false, - onLoad: function(parsed) { + onLoad: parsed => { parsed.normalize({ locale: 'en-US', sync: false, - onLoad: function(normalized) { - var expected = "+16507654321"; + onLoad: normalized => { + const expected = "+16507654321"; - test.equal(normalized, expected); // 'es-US' - test.done(); + expect(normalized).toBe(expected); // 'es-US' } }); } }); - } -}; + }); +}); diff --git a/test/normalizepromise.test.js b/test/normalizepromise.test.js new file mode 100644 index 0000000..df5aea5 --- /dev/null +++ b/test/normalizepromise.test.js @@ -0,0 +1,73 @@ +/* + * normalizeasync.test.js - test phonenumber normalize function() + * + * Copyright © 2018, 2022, 2024 JEDLSoft + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import PhoneNumber from "../src/PhoneNumber.js"; + +describe("testnormalizepromise", () => { + test("NormalizeAsyncIDDPrefix", () => { + expect.assertions(1); + return PhoneNumber.create("011 31 456 3453434", { + locale: 'en-US', + sync: false + }).then(parsed => { + parsed.normalize({ + locale: 'en-US', + sync: false + }).then(normalized => { + const expected = "+314563453434"; + expect(normalized).toBe(expected); + }); + }); + }); + + test("NormalizeAsyncLDNumberUsingDEMCC", () => { + expect.assertions(1); + return PhoneNumber.create("02302 654321", { + locale: 'de-DE', + sync: false + }).then(parsed => { + parsed.normalize({ + mcc: "262", + locale: 'en-US', + sync: false + }).then(normalized => { + const expected = "+492302654321"; + + expect(normalized).toBe(expected); // 'de-DE' + }); + }); + }); + + test("NormalizeAsyncLDNumberUsingUSSpanishLocale", () => { + expect.assertions(1); + return PhoneNumber.create("650 7654321", { + locale: 'es-US', + sync: false + }).then(parsed => { + parsed.normalize({ + locale: 'en-US', + sync: false + }).then(normalized => { + const expected = "+16507654321"; + + expect(normalized).toBe(expected); // 'es-US' + }); + }); + }); +}); diff --git a/test/normasync.test.js b/test/normasync.test.js new file mode 100644 index 0000000..5640ed6 --- /dev/null +++ b/test/normasync.test.js @@ -0,0 +1,99 @@ +/* + * normasync.test.js - test the Unicode Normalization Algorithm routines + * + * Copyright © 2018, 2022, 2024 JEDLSoft + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import NormString from "../src/NormString.js"; + +function toHexString(string) { + let i, result = ""; + + if (!string) { + return ""; + } + for (i = 0; i < string.length; i++) { + const ch = string.charCodeAt(i).toString(16); + result += "0000".substring(0, 4-ch.length) + ch; + if (i < string.length - 1) { + result += " "; + } + } + return result.toUpperCase(); +} + + +describe("testnormasync", () => { + test("NormStringAsyncNormalizeNFD", () => { + expect.assertions(1); + + new NormString("ᄀ각ᆨ", { + sync: false, + onLoad: str => { + expect(str.normalize("nfd").toString()).toBe("ᄀ각ᆨ"); + } + }); + }); + + test("NormStringAsyncNormalizeNFKD", () => { + expect.assertions(1); + new NormString("ᄀ각ᆨ", { + sync: false, + onLoad: str => { + expect(str.normalize("nfkd").toString()).toBe("ᄀ각ᆨ"); + } + }); + }); + + test("NormStringAsyncNormalizeNFC", () => { + expect.assertions(1); + new NormString("ᄀ각ᆨ", { + sync: false, + onLoad: str => { + expect(str.normalize("nfc").toString()).toBe("ᄀ각ᆨ"); + } + }); + }); + + test("NormStringAsyncNormalizeNFKC", () => { + expect.assertions(1); + new NormString("ᄀ각ᆨ", { + sync: false, + onLoad: str => { + expect(str.normalize("nfkc").toString()).toBe("ᄀ각ᆨ"); + } + }); + }); + + test("NormStringAsyncCharIteratorDecomposed", () => { + expect.assertions(8); + const s = new NormString("aÄa", { // the A umlaut is a decomposed char + sync: false, + onLoad: str => { + const it = str.charIterator(); + + expect(it.hasNext()).toBeTruthy(); + expect(it.next()).toBe("a"); + expect(it.hasNext()).toBeTruthy(); + expect(it.next()).toBe("Ä"); + expect(it.hasNext()).toBeTruthy(); + expect(it.next()).toBe("a"); + expect(!it.hasNext()).toBeTruthy(); + expect(it.next()).toBe(undefined); + } + }); + }); +}); diff --git a/test/normpromise.test.js b/test/normpromise.test.js new file mode 100644 index 0000000..0992465 --- /dev/null +++ b/test/normpromise.test.js @@ -0,0 +1,94 @@ +/* + * normasync.test.js - test the Unicode Normalization Algorithm routines + * + * Copyright © 2018, 2022, 2024 JEDLSoft + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import NormString from "../src/NormString.js"; + +function toHexString(string) { + let i, result = ""; + + if (!string) { + return ""; + } + for (i = 0; i < string.length; i++) { + const ch = string.charCodeAt(i).toString(16); + result += "0000".substring(0, 4-ch.length) + ch; + if (i < string.length - 1) { + result += " "; + } + } + return result.toUpperCase(); +} + + +describe("testnormpromise", () => { + test("NormStringAsyncNormalizeNFD", () => { + expect.assertions(1); + + return NormString.create("ᄀ각ᆨ", { + sync: false + }).then(str => { + expect(str.normalize("nfd").toString()).toBe("ᄀ각ᆨ"); + }); + }); + + test("NormStringAsyncNormalizeNFKD", () => { + expect.assertions(1); + return NormString.create("ᄀ각ᆨ", { + sync: false + }).then(str => { + expect(str.normalize("nfkd").toString()).toBe("ᄀ각ᆨ"); + }); + }); + + test("NormStringAsyncNormalizeNFC", () => { + expect.assertions(1); + return NormString.create("ᄀ각ᆨ", { + sync: false + }).then(str => { + expect(str.normalize("nfc").toString()).toBe("ᄀ각ᆨ"); + }); + }); + + test("NormStringAsyncNormalizeNFKC", () => { + expect.assertions(1); + return NormString.create("ᄀ각ᆨ", { + sync: false + }).then(str => { + expect(str.normalize("nfkc").toString()).toBe("ᄀ각ᆨ"); + }); + }); + + test("NormStringAsyncCharIteratorDecomposed", () => { + expect.assertions(8); + return NormString.create("aÄa", { // the A umlaut is a decomposed char + sync: false + }).then(str => { + const it = str.charIterator(); + + expect(it.hasNext()).toBeTruthy(); + expect(it.next()).toBe("a"); + expect(it.hasNext()).toBeTruthy(); + expect(it.next()).toBe("Ä"); + expect(it.hasNext()).toBeTruthy(); + expect(it.next()).toBe("a"); + expect(!it.hasNext()).toBeTruthy(); + expect(it.next()).toBe(undefined); + }); + }); +}); diff --git a/test/numfmtasync.test.js b/test/numfmtasync.test.js new file mode 100644 index 0000000..9520420 --- /dev/null +++ b/test/numfmtasync.test.js @@ -0,0 +1,123 @@ +/* + * numfmtasync.test.js - test the number formatter object + * + * Copyright © 2018, 2022, 2024 JEDLSoft + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSe-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import NumFmt from "../src/NumFmt.js"; + +describe("testnumfmtasync", () => { + test("NumFmtAsyncDefaults", () => { + expect.assertions(7); + new NumFmt({ + sync: false, + onLoad: fmt => { + expect(fmt !== null).toBeTruthy(); + + expect(fmt.getType()).toBe("number"); + expect(fmt.getMaxFractionDigits()).toBe(-1); + expect(fmt.getMinFractionDigits()).toBe(-1); + expect(fmt.isGroupingUsed()).toBeTruthy(); + expect(fmt.getRoundingMode()).toBe("halfdown"); + expect(typeof(fmt.getCurrency()) === "undefined").toBeTruthy(); + } + }); + + }); + + test("NumFmtAsyncNumberFormatSimple", () => { + expect.assertions(2); + new NumFmt({ + sync: false, + onLoad: fmt => { + expect(fmt !== null).toBeTruthy(); + + expect(fmt.format(1.745)).toBe("1.745"); + } + }); + }); + + test("NumFmtAsyncNumberStyleStringArgument", () => { + expect.assertions(2); + new NumFmt({ + style: "standard", + sync: false, + onLoad: fmt => { + expect(fmt !== null).toBeTruthy(); + + // should work with string arguments + expect(fmt.format("2345678901234567.0")).toBe("2,345,678,901,234,567"); + } + }); + }); + + test("NumFmtAsyncFormatNativeDefaultTrue", () => { + expect.assertions(2); + new NumFmt({ + locale: "bn-IN", + sync: false, + onLoad: fmt => { + expect(fmt !== null).toBeTruthy(); + + expect(fmt.format(123.456)).toBe("১২৩.৪৫৬"); + } + }); + }); + + test("NumFmtAsyncCurrencyFormatUS", () => { + expect.assertions(2); + new NumFmt({ + type: "currency", + currency: "USD", + sync: false, + onLoad: fmt => { + expect(fmt !== null).toBeTruthy(); + + expect(fmt.format(57.3467)).toBe("$57.35"); + } + }); + }); + + test("NumFmtAsyncCurrencyGetFractionDigitsOtherCurrency", () => { + expect.assertions(3); + new NumFmt({ + type: "currency", + currency: "JPY", // Japanese yen + sync: false, + onLoad: fmt => { + expect(fmt !== null).toBeTruthy(); + + expect(fmt.getMaxFractionDigits()).toBe(0); + expect(fmt.getMinFractionDigits()).toBe(0); + } + }); + }); + + test("NumFmtAsyncPercentageFormatRegular_kn_IN", () => { + expect.assertions(2); + new NumFmt({ + locale: "kn-IN", + useNative: true, + type: "percentage", + sync: false, + onLoad: fmt => { + expect(fmt !== null).toBeTruthy(); + + expect(fmt.format(57.8)).toBe("೫೭.೮%"); + } + }); + }); +}); diff --git a/test/numfmtpromise.test.js b/test/numfmtpromise.test.js new file mode 100644 index 0000000..4e26da0 --- /dev/null +++ b/test/numfmtpromise.test.js @@ -0,0 +1,116 @@ +/* + * numfmtasync.test.js - test the number formatter object + * + * Copyright © 2018, 2022, 2024 JEDLSoft + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSe-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import NumFmt from "../src/NumFmt.js"; + +describe("testnumfmtpromise", () => { + test("NumFmtAsyncDefaults", () => { + expect.assertions(7); + return NumFmt.create({ + sync: false + }).then(fmt => { + expect(fmt !== null).toBeTruthy(); + + expect(fmt.getType()).toBe("number"); + expect(fmt.getMaxFractionDigits()).toBe(-1); + expect(fmt.getMinFractionDigits()).toBe(-1); + expect(fmt.isGroupingUsed()).toBeTruthy(); + expect(fmt.getRoundingMode()).toBe("halfdown"); + expect(typeof(fmt.getCurrency()) === "undefined").toBeTruthy(); + }); + + }); + + test("NumFmtAsyncNumberFormatSimple", () => { + expect.assertions(2); + return NumFmt.create({ + sync: false + }).then(fmt => { + expect(fmt !== null).toBeTruthy(); + + expect(fmt.format(1.745)).toBe("1.745"); + }); + }); + + test("NumFmtAsyncNumberStyleStringArgument", () => { + expect.assertions(2); + return NumFmt.create({ + style: "standard", + sync: false + }).then(fmt => { + expect(fmt !== null).toBeTruthy(); + + // should work with string arguments + expect(fmt.format("2345678901234567.0")).toBe("2,345,678,901,234,567"); + }); + }); + + test("NumFmtAsyncFormatNativeDefaultTrue", () => { + expect.assertions(2); + return NumFmt.create({ + locale: "bn-IN", + sync: false + }).then(fmt => { + expect(fmt !== null).toBeTruthy(); + + expect(fmt.format(123.456)).toBe("১২৩.৪৫৬"); + }); + }); + + test("NumFmtAsyncCurrencyFormatUS", () => { + expect.assertions(2); + return NumFmt.create({ + type: "currency", + currency: "USD", + sync: false + }).then(fmt => { + expect(fmt !== null).toBeTruthy(); + + expect(fmt.format(57.3467)).toBe("$57.35"); + }); + }); + + test("NumFmtAsyncCurrencyGetFractionDigitsOtherCurrency", () => { + expect.assertions(3); + return NumFmt.create({ + type: "currency", + currency: "JPY", // Japanese yen + sync: false + }).then(fmt => { + expect(fmt !== null).toBeTruthy(); + + expect(fmt.getMaxFractionDigits()).toBe(0); + expect(fmt.getMinFractionDigits()).toBe(0); + }); + }); + + test("NumFmtAsyncPercentageFormatRegular_kn_IN", () => { + expect.assertions(2); + return NumFmt.create({ + locale: "kn-IN", + useNative: true, + type: "percentage", + sync: false + }).then(fmt => { + expect(fmt !== null).toBeTruthy(); + + expect(fmt.format(57.8)).toBe("೫೭.೮%"); + }); + }); +}); diff --git a/test/numplanasync.test.js b/test/numplanasync.test.js new file mode 100644 index 0000000..ef3659e --- /dev/null +++ b/test/numplanasync.test.js @@ -0,0 +1,70 @@ +/* + * numplanasync.test.js - Test the phone numbering plan. + * + * Copyright © 2018, 2022, 2024 JEDLSoft + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import NumberingPlan from "../src/NumberingPlan.js"; + +describe("testnumplanasync", () => { + test("NumberingPlanAsync1", () => { + expect.assertions(2); + new NumberingPlan({ + locale: "en-US", + sync: false, + onLoad: plan => { + expect(typeof(plan) !== "undefined").toBeTruthy(); + expect(plan.getName()).toBe("US"); + } + }); + }); + + test("NumberingPlanAsync2", () => { + expect.assertions(2); + new NumberingPlan({ + locale: "de-DE", + sync: false, + onLoad: plan => { + expect(typeof(plan) !== "undefined").toBeTruthy(); + expect(plan.getName()).toBe("DE"); + } + }); + }); + + test("NumberingPlanAsyncUnknown", () => { + expect.assertions(2); + new NumberingPlan({ + locale: "unknown-unknown", + sync: false, + onLoad: plan => { + expect(typeof(plan) !== "undefined").toBeTruthy(); + expect(plan.getName()).toBe("XX"); + } + }); + }); + + test("NumberingPlanAsyncUnrecognized", () => { + expect.assertions(2); + new NumberingPlan({ + locale: "zu-ZZ", + sync: false, + onLoad: plan => { + expect(typeof(plan) !== "undefined").toBeTruthy(); + expect(plan.getName()).toBe("XX"); + } + }); + }); +}); diff --git a/test/numplanpromise.test.js b/test/numplanpromise.test.js new file mode 100644 index 0000000..d2e659f --- /dev/null +++ b/test/numplanpromise.test.js @@ -0,0 +1,66 @@ +/* + * numplanasync.test.js - Test the phone numbering plan. + * + * Copyright © 2018, 2022, 2024 JEDLSoft + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import NumberingPlan from "../src/NumberingPlan.js"; + +describe("testnumplanpromise", () => { + test("NumberingPlanAsync1", () => { + expect.assertions(2); + return NumberingPlan.create({ + locale: "en-US", + sync: false + }).then(plan => { + expect(typeof(plan) !== "undefined").toBeTruthy(); + expect(plan.getName()).toBe("US"); + }); + }); + + test("NumberingPlanAsync2", () => { + expect.assertions(2); + return NumberingPlan.create({ + locale: "de-DE", + sync: false + }).then(plan => { + expect(typeof(plan) !== "undefined").toBeTruthy(); + expect(plan.getName()).toBe("DE"); + }); + }); + + test("NumberingPlanAsyncUnknown", () => { + expect.assertions(2); + return NumberingPlan.create({ + locale: "unknown-unknown", + sync: false + }).then(plan => { + expect(typeof(plan) !== "undefined").toBeTruthy(); + expect(plan.getName()).toBe("XX"); + }); + }); + + test("NumberingPlanAsyncUnrecognized", () => { + expect.assertions(2); + return NumberingPlan.create({ + locale: "zu-ZZ", + sync: false + }).then(plan => { + expect(typeof(plan) !== "undefined").toBeTruthy(); + expect(plan.getName()).toBe("XX"); + }); + }); +}); diff --git a/test/numprsasync.test.js b/test/numprsasync.test.js new file mode 100644 index 0000000..2b29ded --- /dev/null +++ b/test/numprsasync.test.js @@ -0,0 +1,132 @@ +/* + * numprsasync.test.js - test the number parsing routines + * + * Copyright © 2012-2018, 2022, 2024 JEDLSoft + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import INumber from "../src/INumber.js"; + +describe("testnumprsasync", () => { + test("NumberAsyncConstructorDefault", () => { + expect.assertions(2); + const num = new INumber("3.4", { + sync: false, + onLoad: num => { + expect(num !== null).toBeTruthy(); + expect(num.valueOf()).toBe(3.4); + } + }); + }); + + test("NumberAsyncConstructorWithIlibNumber", () => { + expect.assertions(2); + new INumber("3.4", { + sync: false, + onLoad: num => { + new INumber(num, { + sync: false, + onLoad: num2 => { + expect(num2 !== null).toBeTruthy(); + expect(num2.valueOf()).toBe(3.4); + } + }); + } + }) + }); + + test("NumberAsyncGetLocaleOther", () => { + expect.assertions(3); + const num = new INumber("3,4", { + locale: "de-DE", + sync: false, + onLoad: num => { + expect(num !== null).toBeTruthy(); + + expect(num.getLocale().getSpec()).toBe("de-DE"); + expect(num.valueOf()).toBe(3.4); + } + }); + }); + + test("NumberAsyncPercentage", () => { + expect.assertions(2); + const num = new INumber("58.3%", { + type: "percentage", + sync: false, + onLoad: num => { + expect(num !== null).toBeTruthy(); + + expect(num.valueOf()).toBe(0.583); + } + }); + }); + + test("NumberAsyncCurrencyValue", () => { + expect.assertions(2); + const num = new INumber("$5.80", { + type: "currency", + sync: false, + onLoad: num => { + expect(num !== null).toBeTruthy(); + + expect(num.valueOf()).toBe(5.80); + } + }); + }); + + test("NumberAsyncCurrencyForLocale", () => { + expect.assertions(3); + const num = new INumber("£5.80", { + type: "currency", + sync: false, + onLoad: num => { + expect(num !== null).toBeTruthy(); + + const cur = num.getCurrency(); + expect(typeof(cur) !== "undefined").toBeTruthy(); + expect(cur.getCode()).toBe("GBP"); + } + }); + }); + + test("NumberAsyncParseNonLatinDigits", () => { + expect.assertions(2); + + // tests that the CType isDigit data is loaded + new INumber("১২৩.৪৫৬", { + locale: "bn-IN", + sync: false, + onLoad: num => { + expect(num !== null).toBeTruthy(); + expect(num.valueOf()).toBe(123.456); + } + }); + }); + + test("NumberAsyncParseWithSpaces", () => { + expect.assertions(2); + + // tests that CType isSpace data is loaded + new INumber("1 234 567,745", { + locale: "fr-FR", + sync: false, + onLoad: num => { + expect(num !== null).toBeTruthy(); + expect(num.valueOf()).toBe(1234567.745); + } + }); + }); +}); diff --git a/test/numprspromise.test.js b/test/numprspromise.test.js new file mode 100644 index 0000000..d807571 --- /dev/null +++ b/test/numprspromise.test.js @@ -0,0 +1,123 @@ +/* + * numprsasync.test.js - test the number parsing routines + * + * Copyright © 2012-2018, 2022, 2024 JEDLSoft + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import INumber from "../src/INumber.js"; + +describe("testnumprspromise", () => { + test("NumberAsyncConstructorDefault", () => { + expect.assertions(2); + return INumber.create("3.4", { + sync: false + }).then(num => { + expect(num !== null).toBeTruthy(); + expect(num.valueOf()).toBe(3.4); + }); + }); + + test("NumberAsyncConstructorWithIlibNumber", () => { + expect.assertions(2); + return INumber.create("3.4", { + sync: false + }).then(num => { + return INumber.create(num, { + sync: false + }).then(num2 => { + expect(num2 !== null).toBeTruthy(); + expect(num2.valueOf()).toBe(3.4); + }); + }); + }); + + test("NumberAsyncGetLocaleOther", () => { + expect.assertions(3); + return INumber.create("3,4", { + locale: "de-DE", + sync: false + }).then(num => { + expect(num !== null).toBeTruthy(); + + expect(num.getLocale().getSpec()).toBe("de-DE"); + expect(num.valueOf()).toBe(3.4); + }); + }); + + test("NumberAsyncPercentage", () => { + expect.assertions(2); + return INumber.create("58.3%", { + type: "percentage", + sync: false + }).then(num => { + expect(num !== null).toBeTruthy(); + + expect(num.valueOf()).toBe(0.583); + }); + }); + + test("NumberAsyncCurrencyValue", () => { + expect.assertions(2); + return INumber.create("$5.80", { + type: "currency", + sync: false + }).then(num => { + expect(num !== null).toBeTruthy(); + + expect(num.valueOf()).toBe(5.80); + }); + }); + + test("NumberAsyncCurrencyForLocale", () => { + expect.assertions(3); + return INumber.create("£5.80", { + type: "currency", + sync: false + }).then(num => { + expect(num !== null).toBeTruthy(); + + const cur = num.getCurrency(); + expect(typeof(cur) !== "undefined").toBeTruthy(); + expect(cur.getCode()).toBe("GBP"); + }); + }); + + test("NumberAsyncParseNonLatinDigits", () => { + expect.assertions(2); + + // tests that the CType isDigit data is loaded + return INumber.create("১২৩.৪৫৬", { + locale: "bn-IN", + sync: false + }).then(num => { + expect(num !== null).toBeTruthy(); + expect(num.valueOf()).toBe(123.456); + }); + }); + + test("NumberAsyncParseWithSpaces", () => { + expect.assertions(2); + + // tests that CType isSpace data is loaded + return INumber.create("1 234 567,745", { + locale: "fr-FR", + sync: false + }).then(num => { + expect(num !== null).toBeTruthy(); + expect(num.valueOf()).toBe(1234567.745); + }); + }); +}); diff --git a/test/package.json b/test/package.json new file mode 100644 index 0000000..6990891 --- /dev/null +++ b/test/package.json @@ -0,0 +1 @@ +{"type": "module"} diff --git a/test/testphonefmtasync.js b/test/phonefmtasync.test.js similarity index 58% rename from test/testphonefmtasync.js rename to test/phonefmtasync.test.js index ce9640b..0331dcc 100644 --- a/test/testphonefmtasync.js +++ b/test/phonefmtasync.test.js @@ -1,7 +1,7 @@ /* - * testphonefmtasync.js - Test the phonefmt + * phonefmtasync.test.js - Test the phonefmt * - * Copyright © 2018, 2022 JEDLSoft + * Copyright © 2018, 2022, 2024 JEDLSoft * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,130 +20,126 @@ import PhoneNumber from "../src/PhoneNumber.js"; import PhoneFmt from "../src/PhoneFmt.js"; -export const testphonefmtasync = { - testFormatAsyncUSNoLocale: function(test) { - test.expect(1); - var formatted; +describe("testphonefmtasync", () => { + test("FormatAsyncUSNoLocale", () => { + expect.assertions(1); + let formatted; new PhoneNumber({ areaCode: "456", subscriberNumber: "3453434" }, { sync: false, - onLoad: function(parsed) { + onLoad: parsed => { // default to US format new PhoneFmt({ style: "default", sync: false, - onLoad: function(fmt) { - var expected = "(456) 345-3434"; + onLoad: fmt => { + const expected = "(456) 345-3434"; fmt.format(parsed, { sync: false, - onLoad: function(formatted) { - test.equal(formatted, expected); - test.done(); + onLoad: formatted => { + expect(formatted).toBe(expected); } }); } }); } }); - }, + }); - testFormatAsyncUSPlusIDDtoUnknownCountry: function(test) { - test.expect(1); - var formatted; + test("FormatAsyncUSPlusIDDtoUnknownCountry", () => { + expect.assertions(1); + let formatted; new PhoneNumber({ iddPrefix: "+", countryCode: "506", // costa rica subscriberNumber: "87654321" }, { sync: false, - onLoad: function(parsed) { + onLoad: parsed => { // default to US format new PhoneFmt({ locale: "en-US", style: "dashes", sync: false, - onLoad: function(fmt) { - var expected = "+506 87654321"; // use last resort rule for subscriber number + onLoad: fmt => { + const expected = "+506 87654321"; // use last resort rule for subscriber number fmt.format(parsed, { sync: false, - onLoad: function(formatted) { - test.equal(formatted, expected); - test.done(); + onLoad: formatted => { + expect(formatted).toBe(expected); } }); } }); } }); - }, + }); - testFormatAsyncUSStyle0Emergency: function(test) { - test.expect(1); - var formatted; + test("FormatAsyncUSStyle0Emergency", () => { + expect.assertions(1); + let formatted; new PhoneNumber({ emergency: "911" }, { sync: false, - onLoad: function(parsed) { + onLoad: parsed => { // default to US format new PhoneFmt({ locale: "en-US", style: "default", sync: false, - onLoad: function(fmt) { - var expected = "911 "; + onLoad: fmt => { + const expected = "911 "; fmt.format(parsed, { sync: false, - onLoad: function(formatted) { - test.equal(formatted, expected); - test.done(); + onLoad: formatted => { + expect(formatted).toBe(expected); } }); } }); } }); - }, + }); - testFormatAsyncUSNumberWithFRMCC: function(test) { - test.expect(1); - var formatted; + test("FormatAsyncUSNumberWithFRMCC", () => { + expect.assertions(1); + let formatted; new PhoneNumber({ trunkAccess: "0", areaCode: "6", subscriberNumber: "15987654" }, { sync: false, - onLoad: function(parsed) { + onLoad: parsed => { // default to US format new PhoneFmt({ locale: "en-US", style: "default", mcc: "208", sync: false, - onLoad: function(fmt) { - var expected = "06 15 98 76 54"; + onLoad: fmt => { + const expected = "06 15 98 76 54"; fmt.format(parsed, { sync: false, - onLoad: function(formatted) { - test.equal(formatted, expected); - test.done(); + onLoad: formatted => { + expect(formatted).toBe(expected); } }); } }); } }); - }, + }); - testFormatAsyncWithParamsFormatUSInternational: function(test) { - test.expect(1); - var formatted; + test("FormatAsyncWithParamsFormatUSInternational", () => { + expect.assertions(1); + let formatted; new PhoneNumber({ iddPrefix: "+", countryCode: "33", @@ -152,29 +148,28 @@ export const testphonefmtasync = { }, { locale: "en-US", sync: false, - onLoad: function(parsed) { + onLoad: parsed => { new PhoneFmt({ locale: "en-US", sync: false, - onLoad: function(fmt) { - var expected = "+33 1 12 34 56 78"; + onLoad: fmt => { + const expected = "+33 1 12 34 56 78"; fmt.format(parsed, { sync: false, - onLoad: function(formatted) { - test.equal(formatted, expected); - test.done(); + onLoad: formatted => { + expect(formatted).toBe(expected); } }); } }); } }); - }, + }); - testFormatAsyncGBLongAreaCode: function(test) { - test.expect(1); - var formatted; + test("FormatAsyncGBLongAreaCode", () => { + expect.assertions(1); + let formatted; new PhoneNumber({ trunkAccess: "0", areaCode: "17684", @@ -182,19 +177,18 @@ export const testphonefmtasync = { }, { locale: "en-GB", sync: false, - onLoad: function(parsed) { + onLoad: parsed => { new PhoneFmt({ locale: "en-GB", style: "default", sync: false, - onLoad: function(fmt) { - var expected = "(0176 84) 12345"; + onLoad: fmt => { + const expected = "(0176 84) 12345"; fmt.format(parsed, { sync: false, - onLoad: function(formatted) { - test.equal(formatted, expected); - test.done(); + onLoad: formatted => { + expect(formatted).toBe(expected); } }); } @@ -203,11 +197,11 @@ export const testphonefmtasync = { }); - }, + }); - testFormatAsyncDEStyle1: function(test) { - test.expect(1); - var formatted; + test("FormatAsyncDEStyle1", () => { + expect.assertions(1); + let formatted; new PhoneNumber({ trunkAccess: "0", areaCode: "6224", @@ -215,46 +209,44 @@ export const testphonefmtasync = { }, { locale: "de-DE", sync: false, - onLoad: function(parsed) { + onLoad: parsed => { new PhoneFmt({ locale: "de-DE", style: "alten", sync: false, - onLoad: function(fmt) { - var expected = "06224/1 23 45 67"; + onLoad: fmt => { + const expected = "06224/1 23 45 67"; fmt.format(parsed, { sync: false, - onLoad: function(formatted) { - test.equal(formatted, expected); - test.done(); + onLoad: formatted => { + expect(formatted).toBe(expected); } }); } }); } }); - }, + }); - testFormatAsyncJPStyle1: function(test) { - test.expect(1); - var formatted; + test("FormatAsyncJPStyle1", () => { + expect.assertions(1); + let formatted; new PhoneNumber("0668795111", { locale: "ja-JP", sync: false, - onLoad: function(parsed) { + onLoad: parsed => { new PhoneFmt({ locale: "ja-JP", style: "default", sync: false, - onLoad: function(fmt) { - var expected = "06-6879-5111"; + onLoad: fmt => { + const expected = "06-6879-5111"; fmt.format(parsed, { sync: false, - onLoad: function(formatted) { - test.equal(formatted, expected); - test.done(); + onLoad: formatted => { + expect(formatted).toBe(expected); } }); @@ -262,5 +254,5 @@ export const testphonefmtasync = { }); } }); - } -}; + }); +}); diff --git a/test/testphonefmtpromise.js b/test/phonefmtpromise.test.js similarity index 53% rename from test/testphonefmtpromise.js rename to test/phonefmtpromise.test.js index 6021439..ec47c0b 100644 --- a/test/testphonefmtpromise.js +++ b/test/phonefmtpromise.test.js @@ -1,7 +1,7 @@ /* - * testphonefmtasync.js - Test the phonefmt + * phonefmtasync.test.js - Test the phonefmt * - * Copyright © 2018, 2022 JEDLSoft + * Copyright © 2018, 2022, 2024 JEDLSoft * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,119 +20,115 @@ import PhoneNumber from "../src/PhoneNumber.js"; import PhoneFmt from "../src/PhoneFmt.js"; -export const testphonefmtpromise = { - testFormatAsyncUSNoLocale: function(test) { - test.expect(1); - var formatted; - PhoneNumber.create({ +describe("testphonefmtpromise", () => { + test("FormatAsyncUSNoLocale", () => { + expect.assertions(1); + let formatted; + return PhoneNumber.create({ areaCode: "456", subscriberNumber: "3453434" }, { sync: false - }).then(function(parsed) { + }).then(parsed => { // default to US format return PhoneFmt.create({ style: "default", sync: false - }).then(function(fmt) { - var expected = "(456) 345-3434"; + }).then(fmt => { + const expected = "(456) 345-3434"; fmt.format(parsed, { sync: false - }).then(function(formatted) { - test.equal(formatted, expected); - test.done(); + }).then(formatted => { + expect(formatted).toBe(expected); }); }); }); - }, + }); - testFormatAsyncUSPlusIDDtoUnknownCountry: function(test) { - test.expect(1); - var formatted; - PhoneNumber.create({ + test("FormatAsyncUSPlusIDDtoUnknownCountry", () => { + expect.assertions(1); + let formatted; + return PhoneNumber.create({ iddPrefix: "+", countryCode: "506", // costa rica subscriberNumber: "87654321" }, { sync: false - }).then(function(parsed) { + }).then(parsed => { // default to US format return PhoneFmt.create({ locale: "en-US", style: "dashes", sync: false - }).then(function(fmt) { - var expected = "+506 87654321"; // use last resort rule for subscriber number + }).then(fmt => { + const expected = "+506 87654321"; // use last resort rule for subscriber number fmt.format(parsed, { sync: false - }).then(function(formatted) { - test.equal(formatted, expected); - test.done(); + }).then(formatted => { + expect(formatted).toBe(expected); }); }); }); - }, + }); - testFormatAsyncUSStyle0Emergency: function(test) { - test.expect(1); - var formatted; - PhoneNumber.create({ + test("FormatAsyncUSStyle0Emergency", () => { + expect.assertions(1); + let formatted; + return PhoneNumber.create({ emergency: "911" }, { sync: false - }).then(function(parsed) { + }).then(parsed => { // default to US format return PhoneFmt.create({ locale: "en-US", style: "default", sync: false - }).then(function(fmt) { - var expected = "911 "; + }).then(fmt => { + const expected = "911 "; fmt.format(parsed, { sync: false - }).then(function(formatted) { - test.equal(formatted, expected); - test.done(); + }).then(formatted => { + expect(formatted).toBe(expected); }); }); }); - }, + }); - testFormatAsyncUSNumberWithFRMCC: function(test) { - test.expect(1); - var formatted; - PhoneNumber.create({ + test("FormatAsyncUSNumberWithFRMCC", () => { + expect.assertions(1); + let formatted; + return PhoneNumber.create({ trunkAccess: "0", areaCode: "6", subscriberNumber: "15987654" }, { sync: false - }).then(function(parsed) { + }).then(parsed => { // default to US format return PhoneFmt.create({ locale: "en-US", style: "default", mcc: "208", sync: false - }).then(function(fmt) { - var expected = "06 15 98 76 54"; + }).then(fmt => { + const expected = "06 15 98 76 54"; fmt.format(parsed, { sync: false - }).then(function(formatted) { - test.equal(formatted, expected); - test.done(); + }).then(formatted => { + expect(formatted).toBe(expected); }); }); }); - }, + }); - testFormatAsyncWithParamsFormatUSInternational: function(test) { - test.expect(1); - var formatted; - PhoneNumber.create({ + test("FormatAsyncWithParamsFormatUSInternational", () => { + expect.assertions(1); + let formatted; + return PhoneNumber.create({ iddPrefix: "+", countryCode: "33", areaCode: "1", @@ -140,102 +136,98 @@ export const testphonefmtpromise = { }, { locale: "en-US", sync: false - }).then(function(parsed) { + }).then(parsed => { return PhoneFmt.create({ locale: "en-US", sync: false - }).then(function(fmt) { - var expected = "+33 1 12 34 56 78"; + }).then(fmt => { + const expected = "+33 1 12 34 56 78"; fmt.format(parsed, { sync: false - }).then(function(formatted) { - test.equal(formatted, expected); - test.done(); + }).then(formatted => { + expect(formatted).toBe(expected); }); }); }); - }, + }); - testFormatAsyncGBLongAreaCode: function(test) { - test.expect(1); - var formatted; - PhoneNumber.create({ + test("FormatAsyncGBLongAreaCode", () => { + expect.assertions(1); + let formatted; + return PhoneNumber.create({ trunkAccess: "0", areaCode: "17684", subscriberNumber: "12345" }, { locale: "en-GB", sync: false - }).then(function(parsed) { + }).then(parsed => { return PhoneFmt.create({ locale: "en-GB", style: "default", sync: false - }).then(function(fmt) { - var expected = "(0176 84) 12345"; + }).then(fmt => { + const expected = "(0176 84) 12345"; fmt.format(parsed, { sync: false - }).then(function(formatted) { - test.equal(formatted, expected); - test.done(); + }).then(formatted => { + expect(formatted).toBe(expected); }); }); }); - }, + }); - testFormatAsyncDEStyle1: function(test) { - test.expect(1); - var formatted; - PhoneNumber.create({ + test("FormatAsyncDEStyle1", () => { + expect.assertions(1); + let formatted; + return PhoneNumber.create({ trunkAccess: "0", areaCode: "6224", subscriberNumber: "1234567" }, { locale: "de-DE", sync: false - }).then(function(parsed) { + }).then(parsed => { return PhoneFmt.create({ locale: "de-DE", style: "alten", sync: false - }).then(function(fmt) { - var expected = "06224/1 23 45 67"; + }).then(fmt => { + const expected = "06224/1 23 45 67"; fmt.format(parsed, { sync: false - }).then(function(formatted) { - test.equal(formatted, expected); - test.done(); + }).then(formatted => { + expect(formatted).toBe(expected); }); }); }); - }, + }); - testFormatAsyncJPStyle1: function(test) { - test.expect(1); - var formatted; - PhoneNumber.create("0668795111", { + test("FormatAsyncJPStyle1", () => { + expect.assertions(1); + let formatted; + return PhoneNumber.create("0668795111", { locale: "ja-JP", sync: false - }).then(function(parsed) { + }).then(parsed => { return PhoneFmt.create({ locale: "ja-JP", style: "default", sync: false - }).then(function(fmt) { - var expected = "06-6879-5111"; + }).then(fmt => { + const expected = "06-6879-5111"; fmt.format(parsed, { sync: false - }).then(function(formatted) { - test.equal(formatted, expected); - test.done(); + }).then(formatted => { + expect(formatted).toBe(expected); }); }); }); - } -}; + }); +}); diff --git a/test/testphonegeoasync.js b/test/phonegeoasync.test.js similarity index 52% rename from test/testphonegeoasync.js rename to test/phonegeoasync.test.js index 6f5b4c5..ccfe798 100644 --- a/test/testphonegeoasync.js +++ b/test/phonegeoasync.test.js @@ -1,7 +1,7 @@ /* - * testphonegeoasync.js - Test the GeoLocator Object. + * phonegeoasync.test.js - Test the GeoLocator Object. * - * Copyright © 2018, 2022 JEDLSoft + * Copyright © 2018, 2022, 2024 JEDLSoft * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,16 +20,16 @@ import PhoneNumber from "../src/PhoneNumber.js"; import PhoneGeoLocator from "../src/PhoneGeoLocator.js"; -export const testphonegeoasync = { - testPhoneGeoAsyncNANP: function(test) { - test.expect(6); +describe("testphonegeoasync", () => { + test("PhoneGeoAsyncNANP", () => { + expect.assertions(6); new PhoneNumber("+1 650 654 3210", { sync: false, - onLoad: function(parsed) { + onLoad: parsed => { new PhoneGeoLocator({ sync: false, - onLoad: function(locator) { - var expected = { + onLoad: locator => { + const expected = { country: { sn: "North America", ln: "North America and the Caribbean Islands", @@ -40,32 +40,31 @@ export const testphonegeoasync = { ln: "Central California: San Mateo, Palo Alto, Redwood City, Menlo Park, Mountain View, southern San Francisco suburbs" } }; - test.ok(typeof(locator) !== "undefined"); + expect(typeof(locator) !== "undefined").toBeTruthy(); - var geoInfo = locator.locate(parsed); + const geoInfo = locator.locate(parsed); - test.equal(geoInfo.country.code, expected.country.code); - test.equal(geoInfo.country.sn, expected.country.sn); - test.equal(geoInfo.country.ln, expected.country.ln); - test.equal(geoInfo.area.sn, expected.area.sn); - test.equal(geoInfo.area.ln, expected.area.ln); - test.done(); + expect(geoInfo.country.code).toBe(expected.country.code); + expect(geoInfo.country.sn).toBe(expected.country.sn); + expect(geoInfo.country.ln).toBe(expected.country.ln); + expect(geoInfo.area.sn).toBe(expected.area.sn); + expect(geoInfo.area.ln).toBe(expected.area.ln); } }); } }); - }, + }); - testPhoneGeoAsyncNANPOtherLocale: function(test) { - test.expect(6); + test("PhoneGeoAsyncNANPOtherLocale", () => { + expect.assertions(6); new PhoneNumber("+1 650 654 3210", { sync: false, - onLoad: function(parsed) { + onLoad: parsed => { new PhoneGeoLocator({ locale: 'fr-FR', sync: false, - onLoad: function(locator) { - var expected = { + onLoad: locator => { + const expected = { country: { sn: "Amérique du Nord", ln: "Amérique du Nord et Îles Caraïbes", @@ -77,32 +76,31 @@ export const testphonegeoasync = { } }; - test.ok(typeof(locator) !== "undefined"); - var geoInfo = locator.locate(parsed); + expect(typeof(locator) !== "undefined").toBeTruthy(); + const geoInfo = locator.locate(parsed); - test.equal(geoInfo.country.code, expected.country.code); - test.equal(geoInfo.country.sn, expected.country.sn); - test.equal(geoInfo.country.ln, expected.country.ln); - test.equal(geoInfo.area.sn, expected.area.sn); - test.equal(geoInfo.area.ln, expected.area.ln); - test.done(); + expect(geoInfo.country.code).toBe(expected.country.code); + expect(geoInfo.country.sn).toBe(expected.country.sn); + expect(geoInfo.country.ln).toBe(expected.country.ln); + expect(geoInfo.area.sn).toBe(expected.area.sn); + expect(geoInfo.area.ln).toBe(expected.area.ln); } }); } }); - }, + }); //for bug NOV-118981 - testPhoneGeoAsyncNANPInvalidNumber: function(test) { - test.expect(6); + test("PhoneGeoAsyncNANPInvalidNumber", () => { + expect.assertions(6); new PhoneNumber("1 234", { sync: false, - onLoad: function(parsed) { + onLoad: parsed => { new PhoneGeoLocator({ locale: 'en-US', sync: false, - onLoad: function(locator) { - var expected = { + onLoad: locator => { + const expected = { country: { sn: "North America", ln: "North America and the Caribbean Islands", @@ -114,33 +112,32 @@ export const testphonegeoasync = { } }; - test.ok(typeof(locator) !== "undefined"); - var geoInfo = locator.locate(parsed); + expect(typeof(locator) !== "undefined").toBeTruthy(); + const geoInfo = locator.locate(parsed); - test.equal(geoInfo.country.code, expected.country.code); - test.equal(geoInfo.country.sn, expected.country.sn); - test.equal(geoInfo.country.ln, expected.country.ln); - test.equal(geoInfo.area.sn, expected.area.sn); - test.equal(geoInfo.area.ln, expected.area.ln); - test.done(); + expect(geoInfo.country.code).toBe(expected.country.code); + expect(geoInfo.country.sn).toBe(expected.country.sn); + expect(geoInfo.country.ln).toBe(expected.country.ln); + expect(geoInfo.area.sn).toBe(expected.area.sn); + expect(geoInfo.area.ln).toBe(expected.area.ln); } }); } }); - }, + }); - testPhoneGeoAsyncDefaultDE: function(test) { - test.expect(6); + test("PhoneGeoAsyncDefaultDE", () => { + expect.assertions(6); new PhoneNumber("06224 123456", { locale: "de-DE", sync: false, - onLoad: function(parsed) { + onLoad: parsed => { new PhoneGeoLocator({ locale: 'de-DE', sync: false, - onLoad: function(locator) { - var expected = { + onLoad: locator => { + const expected = { country: { sn: "Deutschland", ln: "Deutschland", @@ -152,32 +149,31 @@ export const testphonegeoasync = { } }; - test.ok(typeof(locator) !== "undefined"); - var geoInfo = locator.locate(parsed); + expect(typeof(locator) !== "undefined").toBeTruthy(); + const geoInfo = locator.locate(parsed); - test.equal(geoInfo.country.code, expected.country.code); - test.equal(geoInfo.country.sn, expected.country.sn); - test.equal(geoInfo.country.ln, expected.country.ln); - test.equal(geoInfo.area.sn, expected.area.sn); - test.equal(geoInfo.area.ln, expected.area.ln); - test.done(); + expect(geoInfo.country.code).toBe(expected.country.code); + expect(geoInfo.country.sn).toBe(expected.country.sn); + expect(geoInfo.country.ln).toBe(expected.country.ln); + expect(geoInfo.area.sn).toBe(expected.area.sn); + expect(geoInfo.area.ln).toBe(expected.area.ln); } }); } }); - }, + }); - testPhoneGeoAsyncDEMobileNumber: function(test) { - test.expect(6); + test("PhoneGeoAsyncDEMobileNumber", () => { + expect.assertions(6); new PhoneNumber("017 12345678", { locale: "de-DE", sync: false, - onLoad: function(parsed) { + onLoad: parsed => { new PhoneGeoLocator({ locale: 'de-DE', sync: false, - onLoad: function(locator) { - var expected = { + onLoad: locator => { + const expected = { country: { sn: "Deutschland", ln: "Deutschland", @@ -189,33 +185,32 @@ export const testphonegeoasync = { } }; - test.ok(typeof(locator) !== "undefined"); - var geoInfo = locator.locate(parsed); + expect(typeof(locator) !== "undefined").toBeTruthy(); + const geoInfo = locator.locate(parsed); - test.equal(geoInfo.country.code, expected.country.code); - test.equal(geoInfo.country.sn, expected.country.sn); - test.equal(geoInfo.country.ln, expected.country.ln); - test.equal(geoInfo.area.sn, expected.area.sn); - test.equal(geoInfo.area.ln, expected.area.ln); - test.done(); + expect(geoInfo.country.code).toBe(expected.country.code); + expect(geoInfo.country.sn).toBe(expected.country.sn); + expect(geoInfo.country.ln).toBe(expected.country.ln); + expect(geoInfo.area.sn).toBe(expected.area.sn); + expect(geoInfo.area.ln).toBe(expected.area.ln); } }); } }); - }, + }); - testPhoneGeoAsyncDefaultHK: function(test) { - test.expect(6); + test("PhoneGeoAsyncDefaultHK", () => { + expect.assertions(6); new PhoneNumber("0663 12345678", { locale: 'en-CN', sync: false, - onLoad: function(parsed) { + onLoad: parsed => { new PhoneGeoLocator({ locale: 'zh-HK', mcc: "460", sync: false, - onLoad: function(locator) { - var expected = { + onLoad: locator => { + const expected = { country: { sn: "中国", ln: "中华人民共和国", @@ -228,18 +223,17 @@ export const testphonegeoasync = { }; // give the prc mcc number so that this gives the right geo location - test.ok(typeof(locator) !== "undefined"); - var geoInfo = locator.locate(parsed); - - test.equal(geoInfo.country.code, expected.country.code); - test.equal(geoInfo.country.sn, expected.country.sn); - test.equal(geoInfo.country.ln, expected.country.ln); - test.equal(geoInfo.area.sn, expected.area.sn); - test.equal(geoInfo.area.ln, expected.area.ln); - test.done(); + expect(typeof(locator) !== "undefined").toBeTruthy(); + const geoInfo = locator.locate(parsed); + + expect(geoInfo.country.code).toBe(expected.country.code); + expect(geoInfo.country.sn).toBe(expected.country.sn); + expect(geoInfo.country.ln).toBe(expected.country.ln); + expect(geoInfo.area.sn).toBe(expected.area.sn); + expect(geoInfo.area.ln).toBe(expected.area.ln); } }); } }); - } -}; + }); +}); diff --git a/test/phonegeopromise.test.js b/test/phonegeopromise.test.js new file mode 100644 index 0000000..09ead15 --- /dev/null +++ b/test/phonegeopromise.test.js @@ -0,0 +1,227 @@ +/* + * phonegeoasync.test.js - Test the GeoLocator Object. + * + * Copyright © 2018, 2022, 2024 JEDLSoft + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import PhoneNumber from "../src/PhoneNumber.js"; +import PhoneGeoLocator from "../src/PhoneGeoLocator.js"; + +describe("testphonegeopromise", () => { + test("PhoneGeoAsyncNANP", () => { + expect.assertions(6); + return PhoneNumber.create("+1 650 654 3210", { + sync: false + }).then(parsed => { + return PhoneGeoLocator.create({ + sync: false + }).then(locator => { + const expected = { + country: { + sn: "North America", + ln: "North America and the Caribbean Islands", + code: "US" + }, + area: { + sn: "California", + ln: "Central California: San Mateo, Palo Alto, Redwood City, Menlo Park, Mountain View, southern San Francisco suburbs" + } + }; + expect(typeof(locator) !== "undefined").toBeTruthy(); + + const geoInfo = locator.locate(parsed); + + expect(geoInfo.country.code).toBe(expected.country.code); + expect(geoInfo.country.sn).toBe(expected.country.sn); + expect(geoInfo.country.ln).toBe(expected.country.ln); + expect(geoInfo.area.sn).toBe(expected.area.sn); + expect(geoInfo.area.ln).toBe(expected.area.ln); + }); + }); + }); + + test("PhoneGeoAsyncNANPOtherLocale", () => { + expect.assertions(6); + return PhoneNumber.create("+1 650 654 3210", { + sync: false + }).then(parsed => { + return PhoneGeoLocator.create({ + locale: 'fr-FR', + sync: false + }).then(locator => { + const expected = { + country: { + sn: "Amérique du Nord", + ln: "Amérique du Nord et Îles Caraïbes", + code: "US" + }, + area: { + sn: "Californie", + ln: "Californie centrale : San Mateo, Palo Alto, Redwood City, Menlo Park, Mountain View, banlieues du sud de San Francisco" + } + }; + + expect(typeof(locator) !== "undefined").toBeTruthy(); + const geoInfo = locator.locate(parsed); + + expect(geoInfo.country.code).toBe(expected.country.code); + expect(geoInfo.country.sn).toBe(expected.country.sn); + expect(geoInfo.country.ln).toBe(expected.country.ln); + expect(geoInfo.area.sn).toBe(expected.area.sn); + expect(geoInfo.area.ln).toBe(expected.area.ln); + }); + }); + }); + +// for bug NOV-118981 + test("PhoneGeoAsyncNANPInvalidNumber", () => { + expect.assertions(6); + return PhoneNumber.create("1 234", { + sync: false + }).then(parsed => { + return PhoneGeoLocator.create({ + locale: 'en-US', + sync: false + }).then(locator => { + const expected = { + country: { + sn: "North America", + ln: "North America and the Caribbean Islands", + code: "US" + }, + area: { + sn: "Ohio", + ln: "Ohio" + } + }; + + expect(typeof(locator) !== "undefined").toBeTruthy(); + const geoInfo = locator.locate(parsed); + + expect(geoInfo.country.code).toBe(expected.country.code); + expect(geoInfo.country.sn).toBe(expected.country.sn); + expect(geoInfo.country.ln).toBe(expected.country.ln); + expect(geoInfo.area.sn).toBe(expected.area.sn); + expect(geoInfo.area.ln).toBe(expected.area.ln); + }); + }); + + }); + + test("PhoneGeoAsyncDefaultDE", () => { + expect.assertions(6); + return PhoneNumber.create("06224 123456", { + locale: "de-DE", + sync: false + }).then(parsed => { + return PhoneGeoLocator.create({ + locale: 'de-DE', + sync: false + }).then(locator => { + const expected = { + country: { + sn: "Deutschland", + ln: "Deutschland", + code: "DE" + }, + area: { + sn: "Leimen", + ln: "Leimen, Nußloch, Sandhausen" + } + }; + + expect(typeof(locator) !== "undefined").toBeTruthy(); + const geoInfo = locator.locate(parsed); + + expect(geoInfo.country.code).toBe(expected.country.code); + expect(geoInfo.country.sn).toBe(expected.country.sn); + expect(geoInfo.country.ln).toBe(expected.country.ln); + expect(geoInfo.area.sn).toBe(expected.area.sn); + expect(geoInfo.area.ln).toBe(expected.area.ln); + }); + }); + }); + + test("PhoneGeoAsyncDEMobileNumber", () => { + expect.assertions(6); + return PhoneNumber.create("017 12345678", { + locale: "de-DE", + sync: false + }).then(parsed => { + return PhoneGeoLocator.create({ + locale: 'de-DE', + sync: false + }).then(locator => { + const expected = { + country: { + sn: "Deutschland", + ln: "Deutschland", + code: "DE" + }, + area: { + sn: "Handynummer", + ln: "Handynummer" + } + }; + + expect(typeof(locator) !== "undefined").toBeTruthy(); + const geoInfo = locator.locate(parsed); + + expect(geoInfo.country.code).toBe(expected.country.code); + expect(geoInfo.country.sn).toBe(expected.country.sn); + expect(geoInfo.country.ln).toBe(expected.country.ln); + expect(geoInfo.area.sn).toBe(expected.area.sn); + expect(geoInfo.area.ln).toBe(expected.area.ln); + }); + }); + }); + + test("PhoneGeoAsyncDefaultHK", () => { + expect.assertions(6); + return PhoneNumber.create("0663 12345678", { + locale: 'en-CN', + sync: false + }).then(parsed => { + return PhoneGeoLocator.create({ + locale: 'zh-HK', + mcc: "460", + sync: false + }).then(locator => { + const expected = { + country: { + sn: "中国", + ln: "中华人民共和国", + code: "CN" + }, + area: { + sn: "揭阳市", + ln: "揭阳市" + } + }; + + // give the prc mcc number so that this gives the right geo location + expect(typeof(locator) !== "undefined").toBeTruthy(); + const geoInfo = locator.locate(parsed); + + expect(geoInfo.country.code).toBe(expected.country.code); + expect(geoInfo.country.sn).toBe(expected.country.sn); + expect(geoInfo.country.ln).toBe(expected.country.ln); + expect(geoInfo.area.sn).toBe(expected.area.sn); + expect(geoInfo.area.ln).toBe(expected.area.ln); + }); + }); + }); +}); diff --git a/test/testphonenumasync.js b/test/phonenumasync.test.js similarity index 57% rename from test/testphonenumasync.js rename to test/phonenumasync.test.js index 4941eab..db3b21f 100644 --- a/test/testphonenumasync.js +++ b/test/phonenumasync.test.js @@ -1,7 +1,7 @@ /* - * testphonenumasync.js - test phonenumber class + * phonenumasync.test.js - test phonenumber class * - * Copyright © 2018, 2022 JEDLSoft + * Copyright © 2018, 2022, 2024 JEDLSoft * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,14 +19,14 @@ import PhoneNumber from "../src/PhoneNumber.js"; -export const testphonenumasync = { - testPhoneParseAsyncAUFull: function(test) { - test.expect(2); +describe("testphonenumasync", () => { + test("PhoneParseAsyncAUFull", () => { + expect.assertions(2); new PhoneNumber("(08) 1234 5678", { locale: "en-AU", sync: false, - onLoad: function(parsed) { - test.ok(typeof(parsed) !== "undefined"); + onLoad: parsed => { + expect(typeof(parsed) !== "undefined").toBeTruthy(); new PhoneNumber({ trunkAccess: "0", @@ -35,22 +35,21 @@ export const testphonenumasync = { }, { locale: "en-AU", sync: false, - onLoad: function(expected) { - test.ok(parsed.equals(expected)); - test.done(); + onLoad: expected => { + expect(parsed.equals(expected)).toBeTruthy(); } }); } }); - }, + }); - testPhoneParseAsyncHKFromIntl: function(test) { - test.expect(2); + test("PhoneParseAsyncHKFromIntl", () => { + expect.assertions(2); new PhoneNumber("+85223897077", { locale: "en-HK", sync: false, - onLoad: function(parsed) { - test.ok(typeof(parsed) !== "undefined"); + onLoad: parsed => { + expect(typeof(parsed) !== "undefined").toBeTruthy(); new PhoneNumber({ iddPrefix: "+", @@ -59,22 +58,21 @@ export const testphonenumasync = { }, { locale: "en-HK", sync: false, - onLoad: function(expected) { - test.ok(parsed.equals(expected)); - test.done(); + onLoad: expected => { + expect(parsed.equals(expected)).toBeTruthy(); } }); } }); - }, + }); - testPhoneParseAsyncUSFull: function(test) { - test.expect(2); + test("PhoneParseAsyncUSFull", () => { + expect.assertions(2); new PhoneNumber("(456) 345-3434", { locale: "en-US", sync: false, - onLoad: function(parsed) { - test.ok(typeof(parsed) !== "undefined"); + onLoad: parsed => { + expect(typeof(parsed) !== "undefined").toBeTruthy(); new PhoneNumber({ areaCode: "456", @@ -82,22 +80,21 @@ export const testphonenumasync = { }, { locale: "en-US", sync: false, - onLoad: function(expected) { - test.ok(parsed.equals(expected)); - test.done(); + onLoad: expected => { + expect(parsed.equals(expected)).toBeTruthy(); } }); } }); - }, + }); - testPhoneParseAsyncKRFullLongAreaCode: function(test) { - test.expect(2); + test("PhoneParseAsyncKRFullLongAreaCode", () => { + expect.assertions(2); new PhoneNumber("033-9467-2345", { locale: "ko-KR", sync: false, - onLoad: function(parsed) { - test.ok(typeof(parsed) !== "undefined"); + onLoad: parsed => { + expect(typeof(parsed) !== "undefined").toBeTruthy(); new PhoneNumber({ trunkAccess: "0", @@ -106,22 +103,21 @@ export const testphonenumasync = { }, { locale: "ko-KR", sync: false, - onLoad: function(expected) { - test.ok(parsed.equals(expected)); - test.done(); + onLoad: expected => { + expect(parsed.equals(expected)).toBeTruthy(); } }); } }); - }, + }); - testPhoneParseAsyncMXFull: function(test) { - test.expect(2); + test("PhoneParseAsyncMXFull", () => { + expect.assertions(2); new PhoneNumber("6241234567", { locale: "es-MX", sync: false, - onLoad: function(parsed) { - test.ok(typeof(parsed) !== "undefined"); + onLoad: parsed => { + expect(typeof(parsed) !== "undefined").toBeTruthy(); new PhoneNumber({ areaCode: "624", @@ -129,38 +125,33 @@ export const testphonenumasync = { }, { locale: "es-MX", sync: false, - onLoad: function(expected) { - test.ok(parsed.equals(expected)); - test.done(); + onLoad: expected => { + expect(parsed.equals(expected)).toBeTruthy(); } }); } }); - }, + }); - testPhoneParseAsyncUndefined: function(test) { - test.expect(1); + test("PhoneParseAsyncUndefined", () => { + expect.assertions(1); new PhoneNumber(undefined, { locale: "en-AU", sync: false, - onLoad: function(parsed) { - test.ok(typeof(parsed) === "undefined"); - - test.done(); + onLoad: parsed => { + expect(typeof(parsed) === "undefined").toBeTruthy(); } }); - }, + }); - testPhoneParseAsyncEmpty: function(test) { - test.expect(1); + test("PhoneParseAsyncEmpty", () => { + expect.assertions(1); new PhoneNumber("", { locale: "en-AU", sync: false, - onLoad: function(parsed) { - test.ok(typeof(parsed) === "undefined"); - - test.done(); + onLoad: parsed => { + expect(typeof(parsed) === "undefined").toBeTruthy(); } }); - } -}; \ No newline at end of file + }); +}); \ No newline at end of file diff --git a/test/phonenumpromise.test.js b/test/phonenumpromise.test.js new file mode 100644 index 0000000..44f3e10 --- /dev/null +++ b/test/phonenumpromise.test.js @@ -0,0 +1,145 @@ +/* + * phonenumasync.test.js - test phonenumber class + * + * Copyright © 2018, 2022, 2024 JEDLSoft + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import PhoneNumber from "../src/PhoneNumber.js"; + +describe("testphonenumpromise", () => { + test("PhoneParseAsyncAUFull", () => { + expect.assertions(2); + return PhoneNumber.create("(08) 1234 5678", { + locale: "en-AU", + sync: false + }).then(parsed => { + expect(typeof(parsed) !== "undefined").toBeTruthy(); + + return PhoneNumber.create({ + trunkAccess: "0", + areaCode: "8", + subscriberNumber: "12345678" + }, { + locale: "en-AU", + sync: false + }).then(expected => { + expect(parsed.equals(expected)).toBeTruthy(); + }); + }); + }); + + test("PhoneParseAsyncHKFromIntl", () => { + expect.assertions(2); + return PhoneNumber.create("+85223897077", { + locale: "en-HK", + sync: false + }).then(parsed => { + expect(typeof(parsed) !== "undefined").toBeTruthy(); + + return PhoneNumber.create({ + iddPrefix: "+", + countryCode: "852", + subscriberNumber: "23897077" + }, { + locale: "en-HK", + sync: false + }).then(expected => { + expect(parsed.equals(expected)).toBeTruthy(); + }); + }); + }); + + test("PhoneParseAsyncUSFull", () => { + expect.assertions(2); + return PhoneNumber.create("(456) 345-3434", { + locale: "en-US", + sync: false + }).then(parsed => { + expect(typeof(parsed) !== "undefined").toBeTruthy(); + + return PhoneNumber.create({ + areaCode: "456", + subscriberNumber: "3453434" + }, { + locale: "en-US", + sync: false + }).then(expected => { + expect(parsed.equals(expected)).toBeTruthy(); + }); + }); + }); + + test("PhoneParseAsyncKRFullLongAreaCode", () => { + expect.assertions(2); + return PhoneNumber.create("033-9467-2345", { + locale: "ko-KR", + sync: false + }).then(parsed => { + expect(typeof(parsed) !== "undefined").toBeTruthy(); + + return PhoneNumber.create({ + trunkAccess: "0", + areaCode: "33", + subscriberNumber: "94672345" + }, { + locale: "ko-KR", + sync: false + }).then(expected => { + expect(parsed.equals(expected)).toBeTruthy(); + }); + }); + }); + + test("PhoneParseAsyncMXFull", () => { + expect.assertions(2); + return PhoneNumber.create("6241234567", { + locale: "es-MX", + sync: false + }).then(parsed => { + expect(typeof(parsed) !== "undefined").toBeTruthy(); + + return PhoneNumber.create({ + areaCode: "624", + subscriberNumber: "1234567" + }, { + locale: "es-MX", + sync: false + }).then(expected => { + expect(parsed.equals(expected)).toBeTruthy(); + }); + }); + }); + + test("PhoneParseAsyncUndefined", () => { + expect.assertions(1); + return PhoneNumber.create(undefined, { + locale: "en-AU", + sync: false + }).then(parsed => { + expect(typeof(parsed) === "undefined").toBeTruthy(); + }); + }); + + test("PhoneParseAsyncEmpty", () => { + expect.assertions(1); + return PhoneNumber.create("", { + locale: "en-AU", + sync: false + }).then(parsed => { + expect(typeof(parsed) === "undefined").toBeTruthy(); + }); + }); +}); \ No newline at end of file diff --git a/test/resourcesasync.test.js b/test/resourcesasync.test.js new file mode 100644 index 0000000..757e1b9 --- /dev/null +++ b/test/resourcesasync.test.js @@ -0,0 +1,225 @@ +/* + * resourcesasync.test.js - test the Resources object + * + * Copyright © 2018, 2022-2024 JEDLSoft + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import ilib from "../src/ilib.js"; + +import ResBundle from "../src/ResBundle.js"; +import Locale from "../src/Locale.js"; + +import path from 'path'; + +import * as url from 'url'; +const __dirname = url.fileURLToPath(new URL('.', import.meta.url)); + +describe("testresourcesasync", () => { + test("ResBundleAsyncConstructorEmpty", () => { + expect.assertions(2); + ilib.clearPseudoLocales(); + + new ResBundle({ + sync: false, + onLoad: rb => { + expect(rb !== null).toBeTruthy(); + + expect(rb.getName()).toBe("strings"); + } + }); + }); + + test("ResBundleAsyncConstructorOtherLocale", () => { + expect.assertions(2); + new ResBundle({ + locale: "de-DE", + sync: false, + onLoad: rb => { + expect(rb !== null).toBeTruthy(); + + const loc = rb.getLocale(); + + expect(loc.toString()).toBe("de-DE"); + } + }); + }); + + test("ResBundleAsyncGetStringOtherBundleesMX", () => { + if (ilib._getPlatform() !== "nodejs" || !ilib._dyndata || !ilib._dyncode) { + return; + } + + expect.assertions(4); + + // clear this to be sure it is actually loading something + ilib.data.strings = undefined; + ilib.data.strings_es = undefined; + ilib.data.strings_und_MX = undefined; + ilib.data.strings_es_MX = undefined; + ilib.clearCache(); + + const base = path.relative(process.cwd(), path.resolve(__dirname, "./resources")); + + new ResBundle({ + locale: "es-MX", + sync: false, + loadParams: { + base: base + }, + onLoad: rb => { + expect(rb !== null).toBeTruthy(); + + expect(rb.getString("Hello from {country}").toString()).toBe("Que tal de {country}"); + expect(rb.getString("Hello from {city}").toString()).toBe("Que tal de {city}"); + expect(rb.getString("Greetings from {city} in {country}").toString()).toBe("Hola de {city} en {country}"); + } + }); + + }); + + test("ResBundleAsyncGetStringOtherBundlePsuedoRaw", () => { + expect.assertions(4); + new ResBundle({ + name: "tester", + locale: "zxx-XX", + type: "raw", + sync: false, + onLoad: rb => { + expect(rb !== null).toBeTruthy(); + + // should not pseudo-ize the replacement parameter names + expect(rb.getString("Hello from {country}").toString()).toBe("[Ħëľľõ fŕõm {çõüñţŕÿ}]"); + expect(rb.getString("Hello from {city}").toString()).toBe("[Ħëľľõ fŕõm {çíţÿ}]"); + expect(rb.getString("Greetings from {city} in {country}").toString()).toBe("[Ĝŕëëţíñğš fŕõm {çíţÿ} íñ {çõüñţŕÿ}]"); + } + }); + }); + + test("ResBundleAsyncGetStringNonExistantTranslations", () => { + expect.assertions(2); + new ResBundle({ + name: "tester", + locale: "zh-CN", + sync: false, + onLoad: rb => { + expect(rb !== null).toBeTruthy(); + + // should return source + expect(rb.getString("foobar").toString()).toBe("foobar"); + } + }); + }); + + test("ResBundleAsyncGetStringNoResourcesReturnSource", () => { + expect.assertions(2); + new ResBundle({ + name: "tester", + locale: "zz-ZZ", + sync: false, + onLoad: rb => { + expect(rb !== null).toBeTruthy(); + + expect(rb.getString("This is a test.").toString()).toBe("This is a test."); + } + }); + }); + + test("ResBundleAsyncGetStringCyrlPsuedoRaw", () => { + expect.assertions(4); + new ResBundle({ + name: "tester", + locale: "zxx-Cyrl-XX", + type: "raw", + sync: false, + onLoad: rb => { + expect(rb !== null).toBeTruthy(); + + // should pseudo-ize the replacement parameter names + expect(rb.getString("Hello from {country}").toString()).toBe("[Хэлло фром {чоунтря}]"); + expect(rb.getString("Hello from {city}").toString()).toBe("[Хэлло фром {читя}]"); + expect(rb.getString("Greetings from {city} in {country}").toString()).toBe("[Грээтингс фром {читя} ин {чоунтря}]"); + } + }); + + }); + + test("ResBundleAsyncGetStringHansPsuedoText", () => { + expect.assertions(4); + new ResBundle({ + name: "tester", + locale: "zxx-Hans-XX", + type: "text", + sync: false, + onLoad: rb => { + expect(rb !== null).toBeTruthy(); + + // should not pseudo-ize the replacement parameter names + // for Chinese scripts, remove the spaces to the simulate Chinese writing style + expect(rb.getString("Hello from {country}").toString()).toBe("[和俄了了夥凡熱夥们{country}]"); + expect(rb.getString("Hello from {city}").toString()).toBe("[和俄了了夥凡熱夥们{city}]"); + expect(rb.getString("Greetings from {city} in {country}").toString()).toBe("[个熱俄俄推意尼个思凡熱夥们{city}意尼{country}]"); + } + }); + + }); + + test("ResBundleAsyncGetStringHebrPsuedoText", () => { + expect.assertions(4); + new ResBundle({ + name: "tester", + locale: "zxx-Hebr-XX", + type: "text", + sync: false, + onLoad: rb => { + expect(rb !== null).toBeTruthy(); + + // should not pseudo-ize the replacement parameter names + expect(rb.getString("Hello from {country}").toString()).toBe("[הֶללֹ פרֹמ {country}]"); + expect(rb.getString("Hello from {city}").toString()).toBe("[הֶללֹ פרֹמ {city}]"); + expect(rb.getString("Greetings from {city} in {country}").toString()).toBe("[גרֶֶטִנגס פרֹמ {city} ִנ {country}]"); + } + }); + + }); + + test("ResBundleAsyncPseudo_euES", () => { + expect.assertions(1); + ilib.clearPseudoLocales(); + ilib.setAsPseudoLocale("eu-ES"); + new ResBundle({ + locale:'eu-ES', + sync: false, + onLoad: rb => { + expect(rb.getString("This is psuedo string test").toString()).toBe("[Ťĥíš íš þšüëðõ šţŕíñğ ţëšţ]"); + ilib.clearPseudoLocales(); + } + }); + }); + + test("ResBundleAsyncPseudo_psAF", () => { + expect.assertions(1); + ilib.clearPseudoLocales(); + ilib.setAsPseudoLocale("ps-AF"); + new ResBundle({ + locale:'ps-AF', + sync: false, + onLoad: rb => { + expect(rb.getString("This is psuedo string test").toString()).toBe("[טהִס ִס פסֶֻדֹ סטרִנג טֶסט]"); + ilib.clearPseudoLocales(); + } + }); + }); +}); diff --git a/test/resourcespromise.test.js b/test/resourcespromise.test.js new file mode 100644 index 0000000..3ad37ad --- /dev/null +++ b/test/resourcespromise.test.js @@ -0,0 +1,214 @@ +/* + * resourcesasync.test.js - test the Resources object + * + * Copyright © 2018, 2022-2024 JEDLSoft + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import ilib from "../src/ilib.js"; + +import ResBundle from "../src/ResBundle.js"; +import Locale from "../src/Locale.js"; + +import path from 'path'; + +import * as url from 'url'; +const __dirname = url.fileURLToPath(new URL('.', import.meta.url)); + +describe("testresourcespromise", () => { + test("ResBundleAsyncConstructorEmpty", () => { + expect.assertions(2); + ilib.clearPseudoLocales(); + + return ResBundle.create({ + sync: false + }).then(rb => { + expect(rb !== null).toBeTruthy(); + + expect(rb.getName()).toBe("strings"); + }); + }); + + test("ResBundleAsyncConstructorOtherLocale", () => { + expect.assertions(2); + return ResBundle.create({ + locale: "de-DE", + sync: false + }).then(rb => { + expect(rb !== null).toBeTruthy(); + + const loc = rb.getLocale(); + + expect(loc.toString()).toBe("de-DE"); + }); + }); + + test("ResBundleAsyncGetStringOtherBundleesMX", () => { + if (ilib._getPlatform() !== "nodejs" || !ilib._dyndata || !ilib._dyncode) { + return; + } + + expect.assertions(4); + + // clear this to be sure it is actually loading something + ilib.data.strings = undefined; + ilib.data.strings_es = undefined; + ilib.data.strings_und_MX = undefined; + ilib.data.strings_es_MX = undefined; + ilib.clearCache(); + + const base = path.relative(process.cwd(), path.resolve(__dirname, "./resources")); + + return ResBundle.create({ + locale: "es-MX", + sync: false, + loadParams: { + base: base + } + }).then(rb => { + expect(rb !== null).toBeTruthy(); + + expect(rb.getStringJS("Hello from {country}")).toBe("Que tal de {country}"); + expect(rb.getStringJS("Hello from {city}")).toBe("Que tal de {city}"); + expect(rb.getStringJS("Greetings from {city} in {country}")).toBe("Hola de {city} en {country}"); + }); + + }); + + test("ResBundleAsyncGetStringOtherBundlePsuedoRaw", () => { + expect.assertions(4); + return ResBundle.create({ + name: "tester", + locale: "zxx-XX", + type: "raw", + sync: false + }).then(rb => { + expect(rb !== null).toBeTruthy(); + + // should not pseudo-ize the replacement parameter names + expect(rb.getString("Hello from {country}").toString()).toBe("[Ħëľľõ fŕõm {çõüñţŕÿ}]"); + expect(rb.getString("Hello from {city}").toString()).toBe("[Ħëľľõ fŕõm {çíţÿ}]"); + expect(rb.getString("Greetings from {city} in {country}").toString()).toBe("[Ĝŕëëţíñğš fŕõm {çíţÿ} íñ {çõüñţŕÿ}]"); + }); + }); + + test("ResBundleAsyncGetStringNonExistantTranslations", () => { + expect.assertions(2); + return ResBundle.create({ + name: "tester", + locale: "zh-CN", + sync: false + }).then(rb => { + expect(rb !== null).toBeTruthy(); + + // should return source + expect(rb.getString("foobar").toString()).toBe("foobar"); + }); + }); + + test("ResBundleAsyncGetStringNoResourcesReturnSource", () => { + expect.assertions(2); + return ResBundle.create({ + name: "tester", + locale: "zz-ZZ", + sync: false + }).then(rb => { + expect(rb !== null).toBeTruthy(); + + expect(rb.getString("This is a test.").toString()).toBe("This is a test."); + }); + }); + + test("ResBundleAsyncGetStringCyrlPsuedoRaw", () => { + expect.assertions(4); + return ResBundle.create({ + name: "tester", + locale: "zxx-Cyrl-XX", + type: "raw", + sync: false + }).then(rb => { + expect(rb !== null).toBeTruthy(); + + // should pseudo-ize the replacement parameter names + expect(rb.getString("Hello from {country}").toString()).toBe("[Хэлло фром {чоунтря}]"); + expect(rb.getString("Hello from {city}").toString()).toBe("[Хэлло фром {читя}]"); + expect(rb.getString("Greetings from {city} in {country}").toString()).toBe("[Грээтингс фром {читя} ин {чоунтря}]"); + }); + + }); + + test("ResBundleAsyncGetStringHansPsuedoText", () => { + expect.assertions(4); + return ResBundle.create({ + name: "tester", + locale: "zxx-Hans-XX", + type: "text", + sync: false + }).then(rb => { + expect(rb !== null).toBeTruthy(); + + // should not pseudo-ize the replacement parameter names + // for Chinese scripts, remove the spaces to the simulate Chinese writing style + expect(rb.getString("Hello from {country}").toString()).toBe("[和俄了了夥凡熱夥们{country}]"); + expect(rb.getString("Hello from {city}").toString()).toBe("[和俄了了夥凡熱夥们{city}]"); + expect(rb.getString("Greetings from {city} in {country}").toString()).toBe("[个熱俄俄推意尼个思凡熱夥们{city}意尼{country}]"); + }); + + }); + + test("ResBundleAsyncGetStringHebrPsuedoText", () => { + expect.assertions(4); + return ResBundle.create({ + name: "tester", + locale: "zxx-Hebr-XX", + type: "text", + sync: false + }).then(rb => { + expect(rb !== null).toBeTruthy(); + + // should not pseudo-ize the replacement parameter names + expect(rb.getString("Hello from {country}").toString()).toBe("[הֶללֹ פרֹמ {country}]"); + expect(rb.getString("Hello from {city}").toString()).toBe("[הֶללֹ פרֹמ {city}]"); + expect(rb.getString("Greetings from {city} in {country}").toString()).toBe("[גרֶֶטִנגס פרֹמ {city} ִנ {country}]"); + }); + + }); + + test("ResBundleAsyncPseudo_euES", () => { + expect.assertions(1); + ilib.clearPseudoLocales(); + ilib.setAsPseudoLocale("eu-ES"); + return ResBundle.create({ + locale:'eu-ES', + sync: false + }).then(rb => { + expect(rb.getString("This is psuedo string test").toString()).toBe("[Ťĥíš íš þšüëðõ šţŕíñğ ţëšţ]"); + ilib.clearPseudoLocales(); + }); + }); + + test("ResBundleAsyncPseudo_psAF", () => { + expect.assertions(1); + ilib.clearPseudoLocales(); + ilib.setAsPseudoLocale("ps-AF"); + return ResBundle.create({ + locale:'ps-AF', + sync: false + }).then(rb => { + expect(rb.getString("This is psuedo string test").toString()).toBe("[טהִס ִס פסֶֻדֹ סטרִנג טֶסט]"); + ilib.clearPseudoLocales(); + }); + }); +}); diff --git a/test/scriptinfo.test.js b/test/scriptinfo.test.js new file mode 100644 index 0000000..29525d0 --- /dev/null +++ b/test/scriptinfo.test.js @@ -0,0 +1,41 @@ +/* + * scriptinfo.test.js - test the script info static methods synchronously + * + * Copyright © 2024 JEDLSoft + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import ilib from '../src/ilib.js'; +import ScriptInfo from "../src/ScriptInfo.js"; + +describe("testscriptinfo synchronously", () => { + beforeEach(() => { + ilib.clearCache(); + }); + + test("ScriptInfoAsyncGetAllScripts", () => { + expect.assertions(7); + const scripts = ScriptInfo.getAllScripts(); + expect(scripts !== null).toBeTruthy(); + + expect(scripts.length).toBe(213); + + expect(scripts[0]).toBe("Adlm"); + expect(scripts[1]).toBe("Afak"); + expect(scripts[2]).toBe("Aghb"); + expect(scripts[4]).toBe("Arab"); + expect(scripts[scripts.length-1]).toBe("Zzzz"); + }); +}); diff --git a/test/scriptinfoasync.test.js b/test/scriptinfoasync.test.js new file mode 100644 index 0000000..f0bc646 --- /dev/null +++ b/test/scriptinfoasync.test.js @@ -0,0 +1,130 @@ +/* + * scriptinfoasync.test.js - test the script info object + * + * Copyright © 2018, 2022, 2024 JEDLSoft + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import ilib from '../src/ilib.js'; +import ScriptInfo from "../src/ScriptInfo.js"; + +describe("testscriptinfoasync", () => { + beforeEach(() => { + ilib.clearCache(); + }); + + test("ScriptInfoAsyncConstructor", () => { + expect.assertions(1); + const si = new ScriptInfo(undefined, { + sync: false, + onLoad: si => { + expect(si !== null).toBeTruthy(); + } + }); + }); + + test("ScriptInfoAsyncGet1", () => { + expect.assertions(8); + const si = new ScriptInfo("Latn", { + sync: false, + onLoad: si => { + expect(si !== null).toBeTruthy(); + + expect(si.getCode()).toBe("Latn"); + expect(si.getCodeNumber()).toBe(215); + expect(si.getName()).toBe("Latin"); + expect(si.getLongCode()).toBe("Latin"); + expect(si.getScriptDirection()).toBe("ltr"); + expect(!si.getNeedsIME()).toBeTruthy(); + expect(si.getCasing()).toBeTruthy(); + } + }); + }); + + test("ScriptInfoAsyncGet4", () => { + expect.assertions(8); + const si = new ScriptInfo("Hans", { + sync: false, + onLoad: si => { + expect(si !== null).toBeTruthy(); + + expect(si.getCode()).toBe("Hans"); + expect(si.getCodeNumber()).toBe(501); + expect(si.getName()).toBe("Han (Simplified variant)"); + expect(si.getLongCode()).toBe("Han_(Simplified_variant)"); + expect(si.getScriptDirection()).toBe("ltr"); + expect(si.getNeedsIME()).toBeTruthy(); + expect(!si.getCasing()).toBeTruthy(); + } + }); + }); + + test("ScriptInfoAsyncGetDefaultLongCodeArab", () => { + expect.assertions(8); + const si = new ScriptInfo("Arab", { + sync: false, + onLoad: si => { + expect(si !== null).toBeTruthy(); + + expect(si.getCode()).toBe("Arab"); + expect(si.getCodeNumber()).toBe(160); + expect(si.getName()).toBe("Arabic"); + expect(si.getLongCode()).toBe("Arabic"); + expect(si.getScriptDirection()).toBe("rtl"); + expect(!si.getNeedsIME()).toBeTruthy(); + expect(!si.getCasing()).toBeTruthy(); + } + }); + }); + test("ScriptInfoAsyncGetUnknown", () => { + expect.assertions(5); + const si = new ScriptInfo("Fooo", { + sync: false, + onLoad: si => { + expect(si !== null).toBeTruthy(); + + expect(si.getCode()).toBe(undefined); + expect(si.getCodeNumber()).toBe(0); + expect(si.getName()).toBe(undefined); + expect(si.getLongCode()).toBe(undefined); + } + }); + }); + + test("ScriptInfoAsyncGetAllScripts", () => { + expect.assertions(11); + ScriptInfo.getAllScripts(false, undefined, scripts => { + expect(scripts !== null).toBeTruthy(); + + expect(scripts.length).toBe(213); + + expect(scripts[0]).toBe("Adlm"); + expect(scripts[1]).toBe("Afak"); + expect(scripts[2]).toBe("Aghb"); + expect(scripts[4]).toBe("Arab"); + expect(scripts[scripts.length-1]).toBe("Zzzz"); + + // make sure the callback is called after the 2nd call + ScriptInfo.getAllScripts(false, undefined, scripts => { + expect(scripts !== null).toBeTruthy(); + + expect(scripts.length).toBe(213); + + expect(scripts[0]).toBe("Adlm"); + expect(scripts[scripts.length-1]).toBe("Zzzz"); + }); + }); + }); +}); diff --git a/test/scriptinfopromise.test.js b/test/scriptinfopromise.test.js new file mode 100644 index 0000000..89f69b6 --- /dev/null +++ b/test/scriptinfopromise.test.js @@ -0,0 +1,125 @@ +/* + * scriptinfoasync.test.js - test the script info object + * + * Copyright © 2018, 2022, 2024 JEDLSoft + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import ilib from '../src/ilib.js'; +import ScriptInfo from "../src/ScriptInfo.js"; + +describe("testscriptinfopromise", () => { + beforeEach(() => { + ilib.clearCache(); + }); + + test("ScriptInfoAsyncConstructor", () => { + expect.assertions(1); + return ScriptInfo.create(undefined, { + sync: false + }).then(si => { + expect(si !== null).toBeTruthy(); + }); + }); + + test("ScriptInfoAsyncGet1", () => { + expect.assertions(8); + return ScriptInfo.create("Latn", { + sync: false + }).then(si => { + expect(si !== null).toBeTruthy(); + + expect(si.getCode()).toBe("Latn"); + expect(si.getCodeNumber()).toBe(215); + expect(si.getName()).toBe("Latin"); + expect(si.getLongCode()).toBe("Latin"); + expect(si.getScriptDirection()).toBe("ltr"); + expect(!si.getNeedsIME()).toBeTruthy(); + expect(si.getCasing()).toBeTruthy(); + }); + }); + + test("ScriptInfoAsyncGet4", () => { + expect.assertions(8); + return ScriptInfo.create("Hans", { + sync: false + }).then(si => { + expect(si !== null).toBeTruthy(); + + expect(si.getCode()).toBe("Hans"); + expect(si.getCodeNumber()).toBe(501); + expect(si.getName()).toBe("Han (Simplified variant)"); + expect(si.getLongCode()).toBe("Han_(Simplified_variant)"); + expect(si.getScriptDirection()).toBe("ltr"); + expect(si.getNeedsIME()).toBeTruthy(); + expect(!si.getCasing()).toBeTruthy(); + }); + }); + + test("ScriptInfoAsyncGetDefaultLongCodeArab", () => { + expect.assertions(8); + return ScriptInfo.create("Arab", { + sync: false + }).then(si => { + expect(si !== null).toBeTruthy(); + + expect(si.getCode()).toBe("Arab"); + expect(si.getCodeNumber()).toBe(160); + expect(si.getName()).toBe("Arabic"); + expect(si.getLongCode()).toBe("Arabic"); + expect(si.getScriptDirection()).toBe("rtl"); + expect(!si.getNeedsIME()).toBeTruthy(); + expect(!si.getCasing()).toBeTruthy(); + }); + }); + test("ScriptInfoAsyncGetUnknown", () => { + expect.assertions(5); + return ScriptInfo.create("Fooo", { + sync: false + }).then(si => { + expect(si !== null).toBeTruthy(); + + expect(si.getCode()).toBe(undefined); + expect(si.getCodeNumber()).toBe(0); + expect(si.getName()).toBe(undefined); + expect(si.getLongCode()).toBe(undefined); + }); + }); + + test("ScriptInfoAsyncGetAllScripts", () => { + expect.assertions(11); + ScriptInfo.getAllScripts(false, undefined, scripts => { + expect(scripts !== null).toBeTruthy(); + + expect(scripts.length).toBe(213); + + expect(scripts[0]).toBe("Adlm"); + expect(scripts[1]).toBe("Afak"); + expect(scripts[2]).toBe("Aghb"); + expect(scripts[4]).toBe("Arab"); + expect(scripts[scripts.length-1]).toBe("Zzzz"); + + // make sure the callback is called after the 2nd call + ScriptInfo.getAllScripts(false, undefined, scripts => { + expect(scripts !== null).toBeTruthy(); + + expect(scripts.length).toBe(213); + + expect(scripts[0]).toBe("Adlm"); + expect(scripts[scripts.length-1]).toBe("Zzzz"); + }); + }); + }); +}); diff --git a/test/strings.test.js b/test/strings.test.js new file mode 100644 index 0000000..7858db6 --- /dev/null +++ b/test/strings.test.js @@ -0,0 +1,66 @@ +/* + * strings.test.js - test the IString static methods + * + * Copyright © 2024 JEDLSoft + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import ilib from '../src/ilib.js'; +import NormString from "../src/NormString.js"; +import Locale from "../src/Locale.js"; +import IString from "../src/IString.js"; + +describe("test IString synchronously", () => { + beforeEach(() => { + ilib.clearCache(); + }); + + test("StringLoadPlurals", () => { + expect.assertions(2); + IString.loadPlurals(true, "ru-RU"); + const str = new IString("asdf"); + expect(str !== null).toBeTruthy(); + + // plurals should already be loaded + str.setLocale("ru-RU", true); + expect(str.getLocale()).toBe("ru-RU"); + }); + + test("StringFormatChoiceSimpleRussian", () => { + expect.assertions(3); + const str = new IString("1#first string|few#second string|many#third string"); + str.setLocale("ru-RU", true); + expect(str !== null).toBeTruthy(); + + expect(str.formatChoice(2)).toBe("second string"); + expect(str.formatChoice(5)).toBe("third string"); + }); + + test("StringFormatChoiceSimpleRussianTwice", () => { + expect.assertions(4); + let str = new IString("1#one|few#few|many#many"); + str.setLocale("ru-RU", true); + expect(str !== null).toBeTruthy(); + + expect(str.formatChoice(3)).toBe("few"); + str = new IString("1#single|few#double|many#multiple"); + + // Russian rules should already be loaded. Need to make sure + // the callback is still called anyways + str.setLocale("ru-RU", true); + expect(str !== null).toBeTruthy(); + expect(str.formatChoice(5)).toBe("multiple"); + }); +}); diff --git a/test/stringsasync.test.js b/test/stringsasync.test.js new file mode 100644 index 0000000..4db053d --- /dev/null +++ b/test/stringsasync.test.js @@ -0,0 +1,91 @@ +/* + * stringsasync.test.js - test the String object + * + * Copyright © 2018, 2022, 2024 JEDLSoft + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import ilib from '../src/ilib.js'; +import NormString from "../src/NormString.js"; +import Locale from "../src/Locale.js"; +import IString from "../src/IString.js"; + +describe("teststringsasync", () => { + beforeEach(() => { + ilib.clearCache(); + }); + + test("StringLoadPlurals", () => { + expect.assertions(2); + IString.loadPlurals(false, undefined, undefined,() => { + const str = new IString("asdf"); + expect(str !== null).toBeTruthy(); + + // plurals should already be loaded + str.setLocale("ru-RU", true); + expect(str.getLocale()).toBe("ru-RU"); + }) + }); + + test("StringSetLocale", () => { + expect.assertions(2); + const str = new IString("1#first string|2#second string"); + str.setLocale("de-DE", false, undefined,() => { + expect(str !== null).toBeTruthy(); + + expect(str.toString()).toBe("1#first string|2#second string"); + }) + }); + + test("StringFormatChoiceSimpleNoPlurals", () => { + expect.assertions(2); + const str = new IString("1#first string|2#second string"); + + expect(str !== null).toBeTruthy(); + + // should default to English rules + expect(str.formatChoice(1)).toBe("first string"); + }); + + test("StringFormatChoiceSimpleRussian", () => { + expect.assertions(3); + const str = new IString("1#first string|few#second string|many#third string"); + str.setLocale("ru-RU", false, undefined,() => { + expect(str !== null).toBeTruthy(); + + expect(str.formatChoice(2)).toBe("second string"); + expect(str.formatChoice(5)).toBe("third string"); + }); + }); + + test("StringFormatChoiceSimpleRussianTwice", () => { + expect.assertions(4); + let str = new IString("1#one|few#few|many#many"); + str.setLocale("ru-RU", false, undefined,() => { + expect(str !== null).toBeTruthy(); + + expect(str.formatChoice(3)).toBe("few"); + str = new IString("1#single|few#double|many#multiple"); + + // Russian rules should already be loaded. Need to make sure + // the callback is still called anyways + str.setLocale("ru-RU", false, undefined,() => { + expect(str !== null).toBeTruthy(); + expect(str.formatChoice(5)).toBe("multiple"); + }); + }); + }); + +}); diff --git a/test/stringspromise.test.js b/test/stringspromise.test.js new file mode 100644 index 0000000..4a041c7 --- /dev/null +++ b/test/stringspromise.test.js @@ -0,0 +1,91 @@ +/* + * stringsasync.test.js - test the String object + * + * Copyright © 2018, 2022, 2024 JEDLSoft + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import ilib from '../src/ilib.js'; +import NormString from "../src/NormString.js"; +import Locale from "../src/Locale.js"; +import IString from "../src/IString.js"; + +describe("teststringspromise", () => { + beforeEach(() => { + ilib.clearCache(); + }); + + test("StringLoadPlurals", () => { + expect.assertions(2); + return IString.loadPlurals(false, undefined).then(() => { + const str = new IString("asdf"); + expect(str !== null).toBeTruthy(); + + // plurals should already be loaded + str.setLocale("ru-RU"); + expect(str.getLocale()).toBe("ru-RU"); + }); + }); + + test("StringSetLocale", () => { + expect.assertions(2); + const str = new IString("1#first string|2#second string"); + return str.setLocale("de-DE", false).then(() => { + expect(str !== null).toBeTruthy(); + + expect(str.toString()).toBe("1#first string|2#second string"); + }); + }); + + test("StringFormatChoiceSimpleNoPlurals", () => { + expect.assertions(2); + const str = new IString("1#first string|2#second string"); + + expect(str !== null).toBeTruthy(); + + // should default to English rules + expect(str.formatChoice(1)).toBe("first string"); + }); + + test("StringFormatChoiceSimpleRussian", () => { + expect.assertions(3); + const str = new IString("1#first string|few#second string|many#third string"); + return str.setLocale("ru-RU", false).then(() => { + expect(str !== null).toBeTruthy(); + + expect(str.formatChoice(2)).toBe("second string"); + expect(str.formatChoice(5)).toBe("third string"); + }); + }); + + test("StringFormatChoiceSimpleRussianTwice", () => { + expect.assertions(4); + let str = new IString("1#one|few#few|many#many"); + return str.setLocale("ru-RU", false).then(() => { + expect(str !== null).toBeTruthy(); + + expect(str.formatChoice(3)).toBe("few"); + str = new IString("1#single|few#double|many#multiple"); + + // Russian rules should already be loaded. Need to make sure + // the callback is still called anyways + return str.setLocale("ru-RU", false); + }).then(() => { + expect(str !== null).toBeTruthy(); + expect(str.formatChoice(5)).toBe("multiple"); + }); + }); + +}); diff --git a/test/testSuite.js b/test/testSuite.js deleted file mode 100644 index 7f2740f..0000000 --- a/test/testSuite.js +++ /dev/null @@ -1,46 +0,0 @@ -/* - * testSuite.js - test suite for this directory - * - * Copyright © 2018-2019, 2022 JEDLSoft - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import nodeunit from 'nodeunit'; -import assert from 'nodeunit/lib/assert.js'; -import assertextras from 'assertextras'; -import { files } from './testSuiteFiles.js'; - -assertextras(assert); - -const reporter = nodeunit.reporters.minimal; -let modules = {}; - -let promise = Promise.resolve(true); - -files.forEach(path => { - promise = promise.then(() => { - return import("./" + path).then(test => { - for (let suite in test) { - modules[suite] = test[suite]; - } - }); - }); -}); - -promise.then(() => { - reporter.run(modules, undefined, err => { - process.exit(err ? 1 : 0); - }); -}); diff --git a/test/testSuiteFiles.js b/test/testSuiteFiles.js deleted file mode 100644 index 797b070..0000000 --- a/test/testSuiteFiles.js +++ /dev/null @@ -1,87 +0,0 @@ -/* - * testSuiteFiles.js - list the test files in this directory - * - * Copyright © 2018, 2022 JEDLSoft - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -export const files = [ - "testaddressasync.js", - "testaddresspromise.js", - "testalphaindexasync.js", - "testalphaindexpromise.js", - "testcalasync.js", - "testcalpromise.js", - "testcharmapasync.js", - "testcharmappromise.js", - "testcharsetasync.js", - "testcharsetpromise.js", - "testcollationasync.js", - "testcollationpromise.js", - "testcountryasync.js", - "testcountrypromise.js", - "testctypeasync.js", - "testctypepromise.js", - "testcurrencyasync.js", - "testcurrencypromise.js", - "testdateasync.js", - "testdatepromise.js", - "testdatefmtasync.js", - "testdatefmtpromise.js", - "testdatefmtrangeasync.js", - "testdatefmtrangepromise.js", - "testdurfmtasync.js", - "testdurfmtpromise.js", - "testglyphstrasync.js", - "testglyphstrpromise.js", - "testlistfmtasync.js", - "testlistfmtpromise.js", - "testlocaleinfoasync.js", - "testlocaleinfopromise.js", - "testlocalematchasync.js", - "testlocalematchpromise.js", - "testnameasync.js", - "testnamepromise.js", - "testnamefmtasync.js", - "testnamefmtpromise.js", - "testnormalizeasync.js", - "testnormalizepromise.js", - "testnormasync.js", - "testnormpromise.js", - "testnumfmtasync.js", - "testnumfmtpromise.js", - "testnumplanasync.js", - "testnumplanpromise.js", - "testnumprsasync.js", - "testnumprspromise.js", - "testphonefmtasync.js", - "testphonefmtpromise.js", - "testphonegeoasync.js", - "testphonegeopromise.js", - "testphonenumasync.js", - "testphonenumpromise.js", - "testresourcesasync.js", - "testresourcespromise.js", - "testscriptinfoasync.js", - "testscriptinfopromise.js", - "teststringsasync.js", - "teststringspromise.js", - "testtimezoneasync.js", - "testtimezonepromise.js", - "testunitfmtasync.js", - "testunitfmtpromise.js", - "testlocale.js", - "testglobal.js" -]; diff --git a/test/testaddressasync.js b/test/testaddressasync.js deleted file mode 100644 index 3114095..0000000 --- a/test/testaddressasync.js +++ /dev/null @@ -1,432 +0,0 @@ -/* - * testaddressasync.js - test the address parsing and formatting routines asynchronously - * - * Copyright © 2018, 2022 JEDLSoft - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import AddressFmt from "../src/AddressFmt.js"; -import Address from "../src/Address.js"; - -function searchRegions(array, regionCode) { - return array.find(function(region) { - return region.code === regionCode; - }); -} - -export const testAddressAsync = { - testParseAddressAsyncSimple: function(test) { - test.expect(7); - new Address("950 W Maude Ave.\nSunnyvale, CA 94085\nUSA", { - locale: 'en-US', - sync: false, - onLoad: function(parsedAddress) { - test.ok(typeof(parsedAddress) !== "undefined"); - test.equal(parsedAddress.streetAddress, "950 W Maude Ave."); - test.equal(parsedAddress.locality, "Sunnyvale"); - test.equal(parsedAddress.region, "CA"); - test.equal(parsedAddress.postalCode, "94085"); - test.equal(parsedAddress.country, "USA"); - test.equal(parsedAddress.countryCode, "US"); - test.done(); - } - }); - }, - - // to verify NOV-111026 - testParseAddressAsyncSimple2: function(test) { - test.expect(7); - new Address("20 Main St.\nMyTown, NY 11530\nUSA", { - locale: 'en-US', - sync: false, - onLoad: function(parsedAddress) { - test.ok(typeof(parsedAddress) !== "undefined"); - test.equal(parsedAddress.streetAddress, "20 Main St."); - test.equal(parsedAddress.locality, "MyTown"); - test.equal(parsedAddress.region, "NY"); - test.equal(parsedAddress.postalCode, "11530"); - test.equal(parsedAddress.country, "USA"); - test.equal(parsedAddress.countryCode, "US"); - test.done(); - } - }); - }, - - testParseAddressAsyncSimple3: function(test) { - test.expect(7); - var pa = new Address("5-2-1 Ginza, Chuo-ku\nTokyo 170-3293\nJapan", { - locale: 'en-US', - sync: false, - onLoad: function(parsedAddress) { - test.ok(typeof(parsedAddress) !== "undefined"); - test.equal(parsedAddress.streetAddress, "5-2-1 Ginza"); - test.equal(parsedAddress.locality, "Chuo-ku"); - test.equal(parsedAddress.region, "Tokyo"); - test.equal(parsedAddress.postalCode, "170-3293"); - test.equal(parsedAddress.country, "Japan"); - test.equal(parsedAddress.countryCode, "JP"); - test.done(); - } - }); - }, - - testParseAddressAsyncMoreComplex: function(test) { - test.expect(7); - var pa = new Address("950 W 21st Ave, Apt 45\nNY, NY 10234", { - locale: 'en-US', - sync: false, - onLoad: function(parsedAddress) { - test.ok(typeof(parsedAddress) !== "undefined"); - test.equal(parsedAddress.streetAddress, "950 W 21st Ave, Apt 45"); - test.equal(parsedAddress.locality, "NY"); - test.equal(parsedAddress.region, "NY"); - test.equal(parsedAddress.postalCode, "10234"); - test.ok(typeof(parsedAddress.country) === "undefined"); - test.equal(parsedAddress.countryCode, "US"); - test.done(); - } - }); - }, - - testParseAddressAsyncSpelledOutState: function(test) { - test.expect(7); - var pa = new Address("20 Main St.\nMyTown, Arizona 11530\nUSA", { - locale: 'en-US', - sync: false, - onLoad: function(parsedAddress) { - test.ok(typeof(parsedAddress) !== "undefined"); - test.equal(parsedAddress.streetAddress, "20 Main St."); - test.equal(parsedAddress.locality, "MyTown"); - test.equal(parsedAddress.region, "Arizona"); - test.equal(parsedAddress.postalCode, "11530"); - test.equal(parsedAddress.country, "USA"); - test.equal(parsedAddress.countryCode, "US"); - test.done(); - } - }); - }, - - testParseAddressAsyncSpelledOutStateWithSpaces: function(test) { - test.expect(7); - var pa = new Address("20 Main St.\nMyTown, New York 11530\nUSA", { - locale: 'en-US', - sync: false, - onLoad: function(parsedAddress) { - test.ok(typeof(parsedAddress) !== "undefined"); - test.equal(parsedAddress.streetAddress, "20 Main St."); - test.equal(parsedAddress.locality, "MyTown"); - test.equal(parsedAddress.region, "New York"); - test.equal(parsedAddress.postalCode, "11530"); - test.equal(parsedAddress.country, "USA"); - test.equal(parsedAddress.countryCode, "US"); - test.done(); - } - }); - }, - - testParseAddressAsyncUnknown: function(test) { - test.expect(7); - - var pa = new Address("123 Main Street, Pretoria 5678, South Africa", { - locale: 'en-US', - sync: false, - onLoad: function(parsedAddress) { - test.ok(typeof(parsedAddress) !== "undefined"); - test.equal(parsedAddress.streetAddress, "123 Main Street"); - test.equal(parsedAddress.locality, "Pretoria"); - test.ok(typeof(parsedAddress.region) === "undefined"); - test.equal(parsedAddress.postalCode, "5678"); - test.equal(parsedAddress.country, "South Africa"); - test.equal(parsedAddress.countryCode, "ZA"); - test.done(); - } - }); - }, - - testParseAddressAsyncNonUS: function(test) { - test.expect(7); - var pa = new Address("Achterberglaan 23, 2345 GD Uithoorn, Netherlands", { - locale: 'en-US', - sync: false, - onLoad: function(parsedAddress) { - test.ok(typeof(parsedAddress) !== "undefined"); - test.equal(parsedAddress.streetAddress, "Achterberglaan 23"); - test.equal(parsedAddress.locality, "Uithoorn"); - test.ok(typeof(parsedAddress.region) === "undefined"); - test.equal(parsedAddress.postalCode, "2345 GD"); - test.equal(parsedAddress.country, "Netherlands"); - test.equal(parsedAddress.countryCode, "NL"); - test.done(); - } - }); - }, - - testFormatAddressAsyncUS: function(test) { - test.expect(2); - var pa = new Address({ - streetAddress: "1234 Any Street", - locality: "Anytown", - region: "CA", - postalCode: "94085", - country: "United States of America", - countryCode: "US" - }, {locale: 'en-US'}); - - var expected = "1234 Any Street\nAnytown CA 94085\nUnited States of America"; - var f = new AddressFmt({ - locale: 'en-US', - sync: false, - onLoad: function(formatter) { - test.ok(typeof(formatter) !== "undefined"); - test.equal(formatter.format(pa), expected); - test.done(); - } - }); - }, - - testFormatAddressAsyncUnknownLocaleQQ: function(test) { - test.expect(2); - var pa = new Address({ - streetAddress: "123 mcdonald ave, apt 234", - locality: "Sunnyvale", - region: "CA", - postalCode: "94086", - locale: 'en-QQ' - }); - - // should return the "root" information - var expected = "123 mcdonald ave, apt 234\nSunnyvale CA 94086"; - var f = new AddressFmt({ - locale: 'en-QQ', - style: 'nocountry', - sync: false, - onLoad: function(formatter) { - test.ok(typeof(formatter) !== "undefined"); - test.equal(formatter.format(pa), expected); - test.done(); - } - }); - }, - - testParseAddressAsyncJPAsianNormal1: function(test) { - test.expect(6); - var pa = new Address("〒150-2345 東京都渋谷区本町2丁目4-7サニーマンション203", { - locale: 'ja-JP', - sync: false, - onLoad: function(parsedAddress) { - test.ok(typeof(parsedAddress) !== "undefined"); - test.equal(parsedAddress.streetAddress, "本町2丁目4-7サニーマンション203"); - test.equal(parsedAddress.locality, "渋谷区"); - test.equal(parsedAddress.region, "東京都"); - test.equal(parsedAddress.postalCode, "〒150-2345"); - test.equal(parsedAddress.countryCode, "JP"); - test.done(); - } - }); - }, - - testFormatAddressAsyncJPAsianNormal: function(test) { - test.expect(2); - var parsedAddress = new Address({ - streetAddress: "本町2丁目4-7サニーマンション203", - locality: "渋谷区", - region: "東京都", - postalCode: "〒150-2345", - countryCode: "JP", - format: "asian" - }, {locale: 'ja-JP'}); - - var expected = "〒150-2345\n東京都渋谷区本町2丁目4-7サニーマンション203"; - - var f = new AddressFmt({ - locale: 'ja-JP', - sync: false, - onLoad: function(formatter) { - test.ok(typeof(formatter) !== "undefined"); - test.equal(formatter.format(parsedAddress), expected); - test.done(); - } - }); - }, - - - testParseAddressAsyncCNAsianNormal: function(test) { - test.expect(7); - var pa = new Address("中国北京市朝阳区建国路112号 中国惠普大厦100022", { - locale: 'zh-CN', - sync: false, - onLoad: function(parsedAddress) { - test.ok(typeof(parsedAddress) !== "undefined"); - test.equal(parsedAddress.streetAddress, "建国路112号 中国惠普大厦"); - test.equal(parsedAddress.locality, "朝阳区"); - test.equal(parsedAddress.region, "北京市"); - test.equal(parsedAddress.postalCode, "100022"); - test.equal(parsedAddress.country, "中国"); - test.equal(parsedAddress.countryCode, "CN"); - test.done(); - } - }); - }, - - testParseAddressAsyncDENormal: function(test) { - test.expect(7); - var pa = new Address("Herrenberger Straße 140, 71034 Böblingen, Deutschland", { - locale: 'de-DE', - sync: false, - onLoad: function(parsedAddress) { - test.ok(typeof(parsedAddress) !== "undefined"); - test.equal(parsedAddress.streetAddress, "Herrenberger Straße 140"); - test.equal(parsedAddress.locality, "Böblingen"); - test.ok(typeof(parsedAddress.region) === "undefined"); - test.equal(parsedAddress.postalCode, "71034"); - test.equal(parsedAddress.country, "Deutschland"); - test.equal(parsedAddress.countryCode, "DE"); - test.done(); - } - }); - }, - - testParseAddressAsyncTHNormal: function(test) { - test.expect(7); - var pa = new Address("49 ซอยร่วมฤดี, ถนนเพลินจิต, ลุมพินี\nเขตปทุมวัน กรุงเทพฯ 10330\nประเทศไทย", { - locale: 'th-Th', - sync: false, - onLoad: function(parsedAddress) { - test.ok(typeof(parsedAddress) !== "undefined"); - test.equal(parsedAddress.streetAddress, "49 ซอยร่วมฤดี, ถนนเพลินจิต, ลุมพินี"); - test.equal(parsedAddress.locality, "เขตปทุมวัน"); - test.equal(parsedAddress.region, "กรุงเทพฯ"); - test.equal(parsedAddress.postalCode, "10330"); - test.equal(parsedAddress.country, "ประเทศไทย"); - test.equal(parsedAddress.countryCode, "TH"); - test.done(); - } - }); - }, - - testParseAddressAsyncRUNormal: function(test) { - test.expect(7); - var pa = new Address("Петров Иван Сергеевич ул. Лесная D. 5 поз. Лесной\nАЛЕКСЕЕВСКИЙ R-N\nВоронежская область\nРоссия\n247112", { - locale: 'ru-RU', - sync: false, - onLoad: function(parsedAddress) { - test.ok(typeof(parsedAddress) !== "undefined"); - test.equal(parsedAddress.streetAddress, "Петров Иван Сергеевич ул. Лесная D. 5 поз. Лесной"); - test.equal(parsedAddress.locality, "АЛЕКСЕЕВСКИЙ R-N"); - test.equal(parsedAddress.region, "Воронежская область"); - test.equal(parsedAddress.postalCode, "247112"); - test.equal(parsedAddress.country, "Россия"); - test.equal(parsedAddress.countryCode, "RU"); - test.done(); - } - }); - }, - - testParseAddressAsyncSANormalNative: function(test) { - test.expect(7); - var pa = new Address("السيد عبد الله ناصر\nمكة المكرمة ٢١۴۵۴\nالمملكة العربية السعودية", { - locale: 'ar-SA', - sync: false, - onLoad: function(parsedAddress) { - test.ok(typeof(parsedAddress) !== "undefined"); - test.equal(parsedAddress.streetAddress, "السيد عبد الله ناصر"); - test.equal(parsedAddress.locality, "مكة المكرمة"); - test.ok(typeof(parsedAddress.region) === "undefined"); - test.equal(parsedAddress.postalCode, "٢١۴۵۴"); - test.equal(parsedAddress.country, "المملكة العربية السعودية"); - test.equal(parsedAddress.countryCode, "SA"); - test.done(); - } - }); - }, - - testParseAddressAsyncINHINormal: function(test) { - test.expect(7); - var pa = new Address("१२५/१, एजी टावर्स. ३ तल, पार्क स्ट्रीट. सर्कस एवेन्यू\nकोलकाता\nपश्चिम बंगाल\n७०००१७\nभारत", { - locale: 'hi-IN', - sync: false, - onLoad: function(parsedAddress) { - test.ok(typeof(parsedAddress) !== "undefined"); - test.equal(parsedAddress.streetAddress, "१२५/१, एजी टावर्स. ३ तल, पार्क स्ट्रीट. सर्कस एवेन्यू"); - test.equal(parsedAddress.locality, "कोलकाता"); - test.equal(parsedAddress.region, "पश्चिम बंगाल"); - test.equal(parsedAddress.postalCode, "७०००१७"); - test.equal(parsedAddress.country, "भारत"); - test.equal(parsedAddress.countryCode, "IN"); - test.done(); - } - }); - }, - - testParseAddressAsyncINGUNoZip: function(test) { - test.expect(7); - var pa = new Address("125/1, એજી ટાવર્સ. 3 જો માળ, પાર્ક સ્ટ્રીટ. સર્કસ એવન્યુ\nકોલકાતા\nપશ્ચિમ બંગાળ\nભારત", { - locale: 'gu-IN', - sync: false, - onLoad: function(parsedAddress) { - test.ok(typeof(parsedAddress) !== "undefined"); - test.equal(parsedAddress.streetAddress, "125/1, એજી ટાવર્સ. 3 જો માળ, પાર્ક સ્ટ્રીટ. સર્કસ એવન્યુ"); - test.equal(parsedAddress.locality, "કોલકાતા"); - test.equal(parsedAddress.region, "પશ્ચિમ બંગાળ"); - test.equal(parsedAddress.country, "ભારત"); - test.equal(parsedAddress.countryCode, "IN"); - test.ok(typeof(parsedAddress.postalCode) === "undefined"); - test.done(); - } - }); - }, - - testAddressFmtGetFormatInfoUSRightConstraints: function(test) { - test.expect(19); - new AddressFmt({ - locale: 'en-US', - sync: false, - onLoad: function(formatter) { - formatter.getFormatInfo(undefined, false, function(info) { - test.ok(info); - - test.equal(info[1][2].component, "postalCode"); - test.equal(info[1][2].constraint, "[0-9]{5}(-[0-9]{4})?"); - - test.equal(info[1][1].component, "region"); - test.ok(info[1][1].constraint); - var r = searchRegions(info[1][1].constraint, "AZ"); - test.equal(r.code, "AZ"); - test.equal(r.name, "Arizona"); - r = searchRegions(info[1][1].constraint, "MS"); - test.equal(r.code, "MS"); - test.equal(r.name, "Mississippi"); - r = searchRegions(info[1][1].constraint, "NY"); - test.equal(r.code, "NY"); - test.equal(r.name, "New York"); - - test.equal(info[2][0].component, "country"); - test.ok(info[2][0].constraint); - var r = searchRegions(info[2][0].constraint, "JP"); - test.equal(r.code, "JP"); - test.equal(r.name, "Japan"); - r = searchRegions(info[2][0].constraint, "CR"); - test.equal(r.code, "CR"); - test.equal(r.name, "Costa Rica"); - r = searchRegions(info[2][0].constraint, "ZA"); - test.equal(r.code, "ZA"); - test.equal(r.name, "South Africa"); - - test.done(); - }); - } - }); - } -}; diff --git a/test/testaddresspromise.js b/test/testaddresspromise.js deleted file mode 100644 index feb5b8f..0000000 --- a/test/testaddresspromise.js +++ /dev/null @@ -1,395 +0,0 @@ -/* - * testaddressasync.js - test the address parsing and formatting routines asynchronously - * - * Copyright © 2018, 2022 JEDLSoft - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import AddressFmt from "../src/AddressFmt.js"; -import Address from "../src/Address.js"; - -function searchRegions(array, regionCode) { - return array.find(function(region) { - return region.code === regionCode; - }); -} - -export const testaddresspromise = { - testPromiseParseAddressAsyncSimple: function(test) { - test.expect(7); - Address.create("950 W Maude Ave.\nSunnyvale, CA 94085\nUSA", { - locale: 'en-US' - }).then(function(parsedAddress) { - test.ok(typeof(parsedAddress) !== "undefined"); - test.equal(parsedAddress.streetAddress, "950 W Maude Ave."); - test.equal(parsedAddress.locality, "Sunnyvale"); - test.equal(parsedAddress.region, "CA"); - test.equal(parsedAddress.postalCode, "94085"); - test.equal(parsedAddress.country, "USA"); - test.equal(parsedAddress.countryCode, "US"); - test.done(); - }); - }, - - // to verify NOV-111026 - testPromiseParseAddressAsyncSimple2: function(test) { - test.expect(7); - Address.create("20 Main St.\nMyTown, NY 11530\nUSA", { - locale: 'en-US' - }).then(function(parsedAddress) { - test.ok(typeof(parsedAddress) !== "undefined"); - test.equal(parsedAddress.streetAddress, "20 Main St."); - test.equal(parsedAddress.locality, "MyTown"); - test.equal(parsedAddress.region, "NY"); - test.equal(parsedAddress.postalCode, "11530"); - test.equal(parsedAddress.country, "USA"); - test.equal(parsedAddress.countryCode, "US"); - test.done(); - }); - }, - - testPromiseParseAddressAsyncSimple3: function(test) { - test.expect(7); - var pa = Address.create("5-2-1 Ginza, Chuo-ku\nTokyo 170-3293\nJapan", { - locale: 'en-US' - }).then(function(parsedAddress) { - test.ok(typeof(parsedAddress) !== "undefined"); - test.equal(parsedAddress.streetAddress, "5-2-1 Ginza"); - test.equal(parsedAddress.locality, "Chuo-ku"); - test.equal(parsedAddress.region, "Tokyo"); - test.equal(parsedAddress.postalCode, "170-3293"); - test.equal(parsedAddress.country, "Japan"); - test.equal(parsedAddress.countryCode, "JP"); - test.done(); - }); - }, - - testPromiseParseAddressAsyncMoreComplex: function(test) { - test.expect(7); - var pa = Address.create("950 W 21st Ave, Apt 45\nNY, NY 10234", { - locale: 'en-US' - }).then(function(parsedAddress) { - test.ok(typeof(parsedAddress) !== "undefined"); - test.equal(parsedAddress.streetAddress, "950 W 21st Ave, Apt 45"); - test.equal(parsedAddress.locality, "NY"); - test.equal(parsedAddress.region, "NY"); - test.equal(parsedAddress.postalCode, "10234"); - test.ok(typeof(parsedAddress.country) === "undefined"); - test.equal(parsedAddress.countryCode, "US"); - test.done(); - }); - }, - - testPromiseParseAddressAsyncSpelledOutState: function(test) { - test.expect(7); - var pa = Address.create("20 Main St.\nMyTown, Arizona 11530\nUSA", { - locale: 'en-US' - }).then(function(parsedAddress) { - test.ok(typeof(parsedAddress) !== "undefined"); - test.equal(parsedAddress.streetAddress, "20 Main St."); - test.equal(parsedAddress.locality, "MyTown"); - test.equal(parsedAddress.region, "Arizona"); - test.equal(parsedAddress.postalCode, "11530"); - test.equal(parsedAddress.country, "USA"); - test.equal(parsedAddress.countryCode, "US"); - test.done(); - }); - }, - - testPromiseParseAddressAsyncSpelledOutStateWithSpaces: function(test) { - test.expect(7); - var pa = Address.create("20 Main St.\nMyTown, New York 11530\nUSA", { - locale: 'en-US' - }).then(function(parsedAddress) { - test.ok(typeof(parsedAddress) !== "undefined"); - test.equal(parsedAddress.streetAddress, "20 Main St."); - test.equal(parsedAddress.locality, "MyTown"); - test.equal(parsedAddress.region, "New York"); - test.equal(parsedAddress.postalCode, "11530"); - test.equal(parsedAddress.country, "USA"); - test.equal(parsedAddress.countryCode, "US"); - test.done(); - }); - }, - - testPromiseParseAddressAsyncUnknown: function(test) { - test.expect(7); - - var pa = Address.create("123 Main Street, Pretoria 5678, South Africa", { - locale: 'en-US' - }).then(function(parsedAddress) { - test.ok(typeof(parsedAddress) !== "undefined"); - test.equal(parsedAddress.streetAddress, "123 Main Street"); - test.equal(parsedAddress.locality, "Pretoria"); - test.ok(typeof(parsedAddress.region) === "undefined"); - test.equal(parsedAddress.postalCode, "5678"); - test.equal(parsedAddress.country, "South Africa"); - test.equal(parsedAddress.countryCode, "ZA"); - test.done(); - }); - }, - - testPromiseParseAddressAsyncNonUS: function(test) { - test.expect(7); - var pa = Address.create("Achterberglaan 23, 2345 GD Uithoorn, Netherlands", { - locale: 'en-US' - }).then(function(parsedAddress) { - test.ok(typeof(parsedAddress) !== "undefined"); - test.equal(parsedAddress.streetAddress, "Achterberglaan 23"); - test.equal(parsedAddress.locality, "Uithoorn"); - test.ok(typeof(parsedAddress.region) === "undefined"); - test.equal(parsedAddress.postalCode, "2345 GD"); - test.equal(parsedAddress.country, "Netherlands"); - test.equal(parsedAddress.countryCode, "NL"); - test.done(); - }); - }, - - testPromiseFormatAddressAsyncUS: function(test) { - test.expect(2); - var pa = new Address({ - streetAddress: "1234 Any Street", - locality: "Anytown", - region: "CA", - postalCode: "94085", - country: "United States of America", - countryCode: "US" - }, {locale: 'en-US'}); - - var expected = "1234 Any Street\nAnytown CA 94085\nUnited States of America"; - var f = AddressFmt.create({ - locale: 'en-US' - }).then(function(formatter) { - test.ok(typeof(formatter) !== "undefined"); - test.equal(formatter.format(pa), expected); - test.done(); - }); - }, - - testPromiseFormatAddressAsyncUnknownLocaleQQ: function(test) { - test.expect(2); - var pa = new Address({ - streetAddress: "123 mcdonald ave, apt 234", - locality: "Sunnyvale", - region: "CA", - postalCode: "94086", - locale: 'en-QQ' - }); - - // should return the "root" information - var expected = "123 mcdonald ave, apt 234\nSunnyvale CA 94086"; - var f = AddressFmt.create({ - locale: 'en-QQ', - style: 'nocountry' - }).then(function(formatter) { - test.ok(typeof(formatter) !== "undefined"); - test.equal(formatter.format(pa), expected); - test.done(); - }); - }, - - testPromiseParseAddressAsyncJPAsianNormal1: function(test) { - test.expect(6); - var pa = Address.create("〒150-2345 東京都渋谷区本町2丁目4-7サニーマンション203", { - locale: 'ja-JP' - }).then(function(parsedAddress) { - test.ok(typeof(parsedAddress) !== "undefined"); - test.equal(parsedAddress.streetAddress, "本町2丁目4-7サニーマンション203"); - test.equal(parsedAddress.locality, "渋谷区"); - test.equal(parsedAddress.region, "東京都"); - test.equal(parsedAddress.postalCode, "〒150-2345"); - test.equal(parsedAddress.countryCode, "JP"); - test.done(); - }); - }, - - testPromiseFormatAddressAsyncJPAsianNormal: function(test) { - test.expect(2); - var parsedAddress = new Address({ - streetAddress: "本町2丁目4-7サニーマンション203", - locality: "渋谷区", - region: "東京都", - postalCode: "〒150-2345", - countryCode: "JP", - format: "asian" - }, {locale: 'ja-JP'}); - - var expected = "〒150-2345\n東京都渋谷区本町2丁目4-7サニーマンション203"; - - var f = AddressFmt.create({ - locale: 'ja-JP' - }).then(function(formatter) { - test.ok(typeof(formatter) !== "undefined"); - test.equal(formatter.format(parsedAddress), expected); - test.done(); - }); - }, - - - testPromiseParseAddressAsyncCNAsianNormal: function(test) { - test.expect(7); - var pa = Address.create("中国北京市朝阳区建国路112号 中国惠普大厦100022", { - locale: 'zh-CN' - }).then(function(parsedAddress) { - test.ok(typeof(parsedAddress) !== "undefined"); - test.equal(parsedAddress.streetAddress, "建国路112号 中国惠普大厦"); - test.equal(parsedAddress.locality, "朝阳区"); - test.equal(parsedAddress.region, "北京市"); - test.equal(parsedAddress.postalCode, "100022"); - test.equal(parsedAddress.country, "中国"); - test.equal(parsedAddress.countryCode, "CN"); - test.done(); - }); - }, - - testPromiseParseAddressAsyncDENormal: function(test) { - test.expect(7); - var pa = Address.create("Herrenberger Straße 140, 71034 Böblingen, Deutschland", { - locale: 'de-DE' - }).then(function(parsedAddress) { - test.ok(typeof(parsedAddress) !== "undefined"); - test.equal(parsedAddress.streetAddress, "Herrenberger Straße 140"); - test.equal(parsedAddress.locality, "Böblingen"); - test.ok(typeof(parsedAddress.region) === "undefined"); - test.equal(parsedAddress.postalCode, "71034"); - test.equal(parsedAddress.country, "Deutschland"); - test.equal(parsedAddress.countryCode, "DE"); - test.done(); - }); - }, - - testPromiseParseAddressAsyncTHNormal: function(test) { - test.expect(7); - var pa = Address.create("49 ซอยร่วมฤดี, ถนนเพลินจิต, ลุมพินี\nเขตปทุมวัน กรุงเทพฯ 10330\nประเทศไทย", { - locale: 'th-Th' - }).then(function(parsedAddress) { - test.ok(typeof(parsedAddress) !== "undefined"); - test.equal(parsedAddress.streetAddress, "49 ซอยร่วมฤดี, ถนนเพลินจิต, ลุมพินี"); - test.equal(parsedAddress.locality, "เขตปทุมวัน"); - test.equal(parsedAddress.region, "กรุงเทพฯ"); - test.equal(parsedAddress.postalCode, "10330"); - test.equal(parsedAddress.country, "ประเทศไทย"); - test.equal(parsedAddress.countryCode, "TH"); - test.done(); - }); - }, - - testPromiseParseAddressAsyncRUNormal: function(test) { - test.expect(7); - var pa = Address.create("Петров Иван Сергеевич ул. Лесная D. 5 поз. Лесной\nАЛЕКСЕЕВСКИЙ R-N\nВоронежская область\nРоссия\n247112", { - locale: 'ru-RU' - }).then(function(parsedAddress) { - test.ok(typeof(parsedAddress) !== "undefined"); - test.equal(parsedAddress.streetAddress, "Петров Иван Сергеевич ул. Лесная D. 5 поз. Лесной"); - test.equal(parsedAddress.locality, "АЛЕКСЕЕВСКИЙ R-N"); - test.equal(parsedAddress.region, "Воронежская область"); - test.equal(parsedAddress.postalCode, "247112"); - test.equal(parsedAddress.country, "Россия"); - test.equal(parsedAddress.countryCode, "RU"); - test.done(); - }); - }, - - testPromiseParseAddressAsyncSANormalNative: function(test) { - test.expect(7); - var pa = Address.create("السيد عبد الله ناصر\nمكة المكرمة ٢١۴۵۴\nالمملكة العربية السعودية", { - locale: 'ar-SA' - }).then(function(parsedAddress) { - test.ok(typeof(parsedAddress) !== "undefined"); - test.equal(parsedAddress.streetAddress, "السيد عبد الله ناصر"); - test.equal(parsedAddress.locality, "مكة المكرمة"); - test.ok(typeof(parsedAddress.region) === "undefined"); - test.equal(parsedAddress.postalCode, "٢١۴۵۴"); - test.equal(parsedAddress.country, "المملكة العربية السعودية"); - test.equal(parsedAddress.countryCode, "SA"); - test.done(); - }); - }, - - testPromiseParseAddressAsyncINHINormal: function(test) { - test.expect(7); - var pa = Address.create("१२५/१, एजी टावर्स. ३ तल, पार्क स्ट्रीट. सर्कस एवेन्यू\nकोलकाता\nपश्चिम बंगाल\n७०००१७\nभारत", { - locale: 'hi-IN' - }).then(function(parsedAddress) { - test.ok(typeof(parsedAddress) !== "undefined"); - test.equal(parsedAddress.streetAddress, "१२५/१, एजी टावर्स. ३ तल, पार्क स्ट्रीट. सर्कस एवेन्यू"); - test.equal(parsedAddress.locality, "कोलकाता"); - test.equal(parsedAddress.region, "पश्चिम बंगाल"); - test.equal(parsedAddress.postalCode, "७०००१७"); - test.equal(parsedAddress.country, "भारत"); - test.equal(parsedAddress.countryCode, "IN"); - test.done(); - }); - }, - - testPromiseParseAddressAsyncINGUNoZip: function(test) { - test.expect(7); - var pa = Address.create("125/1, એજી ટાવર્સ. 3 જો માળ, પાર્ક સ્ટ્રીટ. સર્કસ એવન્યુ\nકોલકાતા\nપશ્ચિમ બંગાળ\nભારત", { - locale: 'gu-IN' - }).then(function(parsedAddress) { - test.ok(typeof(parsedAddress) !== "undefined"); - test.equal(parsedAddress.streetAddress, "125/1, એજી ટાવર્સ. 3 જો માળ, પાર્ક સ્ટ્રીટ. સર્કસ એવન્યુ"); - test.equal(parsedAddress.locality, "કોલકાતા"); - test.equal(parsedAddress.region, "પશ્ચિમ બંગાળ"); - test.equal(parsedAddress.country, "ભારત"); - test.equal(parsedAddress.countryCode, "IN"); - test.ok(typeof(parsedAddress.postalCode) === "undefined"); - test.done(); - }); - }, - - testPromiseAddressFmtGetFormatInfoUSRightConstraints: function(test) { - test.expect(22); - AddressFmt.create({ - locale: 'en-US' - }).then(function(formatter) { - return formatter.getFormatInfo("de-DE", false); // get labels in German - }).then(function(info) { - test.ok(info); - - test.equal(info[1][2].component, "postalCode"); - test.equal(info[1][2].label, "Postleitzahl"); - test.equal(info[1][2].constraint, "[0-9]{5}(-[0-9]{4})?"); - - test.equal(info[1][1].component, "region"); - test.equal(info[1][1].label, "Bundesland"); - test.ok(info[1][1].constraint); - var r = searchRegions(info[1][1].constraint, "AZ"); - test.equal(r.code, "AZ"); - test.equal(r.name, "Arizona"); - r = searchRegions(info[1][1].constraint, "MS"); - test.equal(r.code, "MS"); - test.equal(r.name, "Mississippi"); - r = searchRegions(info[1][1].constraint, "NY"); - test.equal(r.code, "NY"); - test.equal(r.name, "New York"); - - test.equal(info[2][0].component, "country"); - test.equal(info[2][0].label, "Land"); - test.ok(info[2][0].constraint); - var r = searchRegions(info[2][0].constraint, "JP"); - test.equal(r.code, "JP"); - test.equal(r.name, "Japan"); - r = searchRegions(info[2][0].constraint, "CR"); - test.equal(r.code, "CR"); - test.equal(r.name, "Costa Rica"); - r = searchRegions(info[2][0].constraint, "ZA"); - test.equal(r.code, "ZA"); - test.equal(r.name, "Südafrika"); - - test.done(); - }); - } -}; diff --git a/test/testcalasync.js b/test/testcalasync.js deleted file mode 100644 index 7c24e47..0000000 --- a/test/testcalasync.js +++ /dev/null @@ -1,407 +0,0 @@ -/* - * testcalasync.js - test the calendar objects asynchronously - * - * Copyright © 2018, 2022 JEDLSoft - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import CalendarFactory, {CalendarFactoryAsync} from "../src/CalendarFactory.js"; -import CopticDate from "../src/CopticDate.js"; -import EthiopicDate from "../src/EthiopicDate.js"; -import GregorianDate from "../src/GregorianDate.js"; -import HanDate from "../src/HanDate.js"; -import HebrewDate from "../src/HebrewDate.js"; -import IslamicDate from "../src/IslamicDate.js"; -import JulianDate from "../src/JulianDate.js"; -import PersianAlgoDate from "../src/PersianAlgoDate.js"; -import PersianDate from "../src/PersianDate.js"; -import ThaiSolarDate from "../src/ThaiSolarDate.js"; - -export const testcalasync = { - testCalendarFactoryAsyncDefault: function(test) { - test.expect(1); - CalendarFactory({ - sync: false, - onLoad: function(cal) { - test.ok(typeof(cal) !== "undefined"); - test.done(); - } - }); - }, - - testCalendarFactoryAsyncSpecific: function(test) { - test.expect(2); - var cal = CalendarFactory({ - type: "julian", - sync: false, - onLoad: function(cal) { - test.ok(typeof(cal) !== "undefined"); - - test.equal(cal.getType(), "julian"); - test.done(); - } - }); - }, - - testCalendarFactoryAsyncUnknown: function(test) { - test.expect(1); - try { - CalendarFactory({ - type: "asdf", - sync: false, - onLoad: function(cal) { - test.ok(typeof(cal) === "undefined"); - test.done(); - } - }); - } catch (e) { - console.log("caught: " + e); - } - }, - - testCalendarFactoryAsyncDefaultForLocale: function(test) { - test.expect(2); - CalendarFactory({ - locale: "fa-IR", - sync: false, - onLoad: function(cal) { - test.ok(typeof(cal) !== "undefined"); - - test.equal(cal.getType(), "persian"); - test.done(); - } - }); - }, - - testCalendarFactoryAsyncDefaultForLocaleOther: function(test) { - test.expect(2); - var cal = CalendarFactory({ - locale: "th-TH", - sync: false, - onLoad: function(cal) { - test.ok(typeof(cal) !== "undefined"); - - test.equal(cal.getType(), "thaisolar"); - test.done(); - } - }); - }, - - testCalendarFactoryAsyncOverrideLocale: function(test) { - test.expect(2); - var cal = CalendarFactory({ - locale: "fa-IR", - type: "gregorian", - sync: false, - onLoad: function(cal) { - test.ok(typeof(cal) !== "undefined"); - - test.equal(cal.getType(), "gregorian"); - test.done(); - } - }); - }, - - testCopticDateAsyncConstructorFull: function(test) { - test.expect(8); - new CopticDate({ - year: 1735, - month: 9, - day: 23, - hour: 16, - minute: 7, - second: 12, - millisecond: 123, - timezone: "Etc/UTC", - sync: false, - onLoad: function(cd) { - test.ok(cd !== null); - - test.equal(cd.getYears(), 1735); - test.equal(cd.getMonths(), 9); - test.equal(cd.getDays(), 23); - test.equal(cd.getHours(), 16); - test.equal(cd.getMinutes(), 7); - test.equal(cd.getSeconds(), 12); - test.equal(cd.getMilliseconds(), 123); - test.done(); - } - }); - }, - - testEthiopicDateAsyncConstructorFull: function(test) { - test.expect(8); - new EthiopicDate({ - year: 2011, - month: 9, - day: 23, - hour: 16, - minute: 7, - second: 12, - millisecond: 123, - timezone: "Etc/UTC", - sync: false, - onLoad: function(ed) { - test.ok(ed !== null); - - test.equal(ed.getYears(), 2011); - test.equal(ed.getMonths(), 9); - test.equal(ed.getDays(), 23); - test.equal(ed.getHours(), 16); - test.equal(ed.getMinutes(), 7); - test.equal(ed.getSeconds(), 12); - test.equal(ed.getMilliseconds(), 123); - test.done(); - } - }); - }, - - testGregDateAsyncConstructorFull: function(test) { - test.expect(8); - new GregorianDate({ - year: 2011, - month: 9, - day: 23, - hour: 16, - minute: 7, - second: 12, - millisecond: 123, - sync: false, - onLoad: function(gd) { - test.ok(gd !== null); - - test.equal(gd.getYears(), 2011); - test.equal(gd.getMonths(), 9); - test.equal(gd.getDays(), 23); - test.equal(gd.getHours(), 16); - test.equal(gd.getMinutes(), 7); - test.equal(gd.getSeconds(), 12); - test.equal(gd.getMilliseconds(), 123); - test.done(); - } - }); - - }, - - testHanDateAsyncConstructorFull: function(test) { - test.expect(10); - new HanDate({ - year: 4711, - month: 9, - day: 23, - hour: 16, - minute: 7, - second: 12, - millisecond: 123, - timezone: "Etc/UTC", - sync: false, - onLoad: function(hd) { - test.ok(hd !== null); - - test.equal(hd.getYears(), 4711); - test.equal(hd.getMonths(), 9); - test.equal(hd.getDays(), 23); - test.equal(hd.getHours(), 16); - test.equal(hd.getMinutes(), 7); - test.equal(hd.getSeconds(), 12); - test.equal(hd.getMilliseconds(), 123); - test.equal(hd.getCycles(), 78); - test.equal(hd.getCycleYears(), 31); - test.done(); - } - }); - - }, - - testHebrewDateAsyncConstructorFull: function(test) { - test.expect(8); - new HebrewDate({ - year: 2011, - month: 9, - day: 23, - hour: 16, - minute: 7, - second: 12, - millisecond: 123, - timezone: "Etc/UTC", - sync: false, - onLoad: function(hd) { - test.ok(hd !== null); - - test.equal(hd.getYears(), 2011); - test.equal(hd.getMonths(), 9); - test.equal(hd.getDays(), 23); - test.equal(hd.getHours(), 16); - test.equal(hd.getMinutes(), 7); - test.equal(hd.getSeconds(), 12); - test.equal(hd.getMilliseconds(), 123); - test.done(); - } - }); - - }, - - testIslamicDateAsyncConstructorFull: function(test) { - test.expect(8); - new IslamicDate({ - year: 2011, - month: 9, - day: 23, - hour: 16, - minute: 7, - second: 12, - millisecond: 123, - timezone: "Etc/UTC", - sync: false, - onLoad: function(id) { - test.ok(id !== null); - - test.equal(id.getYears(), 2011); - test.equal(id.getMonths(), 9); - test.equal(id.getDays(), 23); - test.equal(id.getHours(), 16); - test.equal(id.getMinutes(), 7); - test.equal(id.getSeconds(), 12); - test.equal(id.getMilliseconds(), 123); - test.done(); - } - }); - - }, - - testJulDateAsyncConstructorFull: function(test) { - test.expect(8); - new JulianDate({ - year: 2011, - month: 9, - day: 23, - hour: 16, - minute: 7, - second: 12, - millisecond: 123, - timezone: "Etc/UTC", - sync: false, - onLoad: function(jul) { - test.ok(jul !== null); - - test.equal(jul.getYears(), 2011); - test.equal(jul.getMonths(), 9); - test.equal(jul.getDays(), 23); - test.equal(jul.getHours(), 16); - test.equal(jul.getMinutes(), 7); - test.equal(jul.getSeconds(), 12); - test.equal(jul.getMilliseconds(), 123); - test.done(); - } - }); - - }, - - testPersAlgoDateAsyncConstructorFull: function(test) { - test.expect(8); - new PersianAlgoDate({ - year: 1392, - month: 9, - day: 23, - hour: 16, - minute: 7, - second: 12, - millisecond: 123, - timezone: "Etc/UTC", - sync: false, - onLoad: function(pd) { - test.ok(pd !== null); - - test.equal(pd.getYears(), 1392); - test.equal(pd.getMonths(), 9); - test.equal(pd.getDays(), 23); - test.equal(pd.getHours(), 16); - test.equal(pd.getMinutes(), 7); - test.equal(pd.getSeconds(), 12); - test.equal(pd.getMilliseconds(), 123); - test.done(); - } - }); - - }, - - testPersDateAstroAsyncConstructorFull: function(test) { - test.expect(8); - new PersianDate({ - year: 1392, - month: 9, - day: 23, - hour: 16, - minute: 7, - second: 12, - millisecond: 123, - timezone: "Etc/UTC", - sync: false, - onLoad: function(pd) { - test.ok(pd !== null); - - test.equal(pd.getYears(), 1392); - test.equal(pd.getMonths(), 9); - test.equal(pd.getDays(), 23); - test.equal(pd.getHours(), 16); - test.equal(pd.getMinutes(), 7); - test.equal(pd.getSeconds(), 12); - test.equal(pd.getMilliseconds(), 123); - test.done(); - } - }); - - }, - - testThaiSolarDateAsyncConstructorFull: function(test) { - test.expect(8); - new ThaiSolarDate({ - year: 2553, - month: 9, - day: 23, - hour: 16, - minute: 7, - second: 12, - millisecond: 123, - timezone: "Etc/UTC", - sync: false, - onLoad: function(td) { - test.ok(td !== null); - - test.equal(td.getYears(), 2553); - test.equal(td.getMonths(), 9); - test.equal(td.getDays(), 23); - test.equal(td.getHours(), 16); - test.equal(td.getMinutes(), 7); - test.equal(td.getSeconds(), 12); - test.equal(td.getMilliseconds(), 123); - test.done(); - } - }); - }, - - testCalendarFactoryAsync: function(test) { - test.expect(2); - var cal = CalendarFactoryAsync({ - locale: "th-TH", - onLoad: function(cal) { - test.ok(typeof(cal) !== "undefined"); - - test.equal(cal.getType(), "thaisolar"); - test.done(); - } - }); - } -}; diff --git a/test/testcalpromise.js b/test/testcalpromise.js deleted file mode 100644 index 011ea2f..0000000 --- a/test/testcalpromise.js +++ /dev/null @@ -1,393 +0,0 @@ -/* - * testcalpromise.js - test the calendar objects asynchronously with promises - * - * Copyright © 2018, 2022 JEDLSoft - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import CalendarFactory, {CalendarFactoryAsync} from "../src/CalendarFactory.js"; -import CopticDate from "../src/CopticDate.js"; -import EthiopicDate from "../src/EthiopicDate.js"; -import GregorianDate from "../src/GregorianDate.js"; -import HanDate from "../src/HanDate.js"; -import HebrewDate from "../src/HebrewDate.js"; -import IslamicDate from "../src/IslamicDate.js"; -import JulianDate from "../src/JulianDate.js"; -import PersianAlgoDate from "../src/PersianAlgoDate.js"; -import PersianDate from "../src/PersianDate.js"; -import ThaiSolarDate from "../src/ThaiSolarDate.js"; - -export const testcalpromise = { - testCalendarPromiseFactoryAsyncDefault: function(test) { - test.expect(1); - CalendarFactoryAsync().then(function(cal) { - test.ok(typeof(cal) !== "undefined"); - test.done(); - }); - }, - - testCalendarPromiseFactoryAsyncSpecific: function(test) { - test.expect(2); - var cal = CalendarFactoryAsync({ - type: "julian" - }).then(function(cal) { - test.ok(typeof(cal) !== "undefined"); - - test.equal(cal.getType(), "julian"); - test.done(); - }); - }, - - testCalendarPromiseFactoryAsyncUnknown: function(test) { - test.expect(1); - CalendarFactoryAsync({ - type: "asdf" - }).then(function(cal) { - test.ok(typeof(cal) === "undefined"); - test.done(); - }).catch(function(e) { - test.fail(); - console.log("caught: " + e); - test.done(); - }); - }, - - testCalendarPromiseFactoryAsyncDefaultForLocale: function(test) { - test.expect(2); - CalendarFactoryAsync({ - locale: "fa-IR" - }).then(function(cal) { - test.ok(typeof(cal) !== "undefined"); - - test.equal(cal.getType(), "persian"); - test.done(); - }); - }, - - testCalendarPromiseFactoryAsyncDefaultForLocaleOther: function(test) { - test.expect(2); - var cal = CalendarFactoryAsync({ - locale: "th-TH" - }).then(function(cal) { - test.ok(typeof(cal) !== "undefined"); - - test.equal(cal.getType(), "thaisolar"); - test.done(); - }); - }, - - testCalendarPromiseFactoryAsyncOverrideLocale: function(test) { - test.expect(2); - var cal = CalendarFactoryAsync({ - locale: "fa-IR", - type: "gregorian" - }).then(function(cal) { - test.ok(typeof(cal) !== "undefined"); - - test.equal(cal.getType(), "gregorian"); - test.done(); - }); - }, - - testCopticDatePromiseConstructorFull: function(test) { - test.expect(8); - try { - CopticDate.create({ - year: 1735, - month: 9, - day: 23, - hour: 16, - minute: 7, - second: 12, - millisecond: 123, - timezone: "Etc/UTC", - sync: false - }).then(function(cd) { - test.ok(cd !== null); - - test.equal(cd.getYears(), 1735); - test.equal(cd.getMonths(), 9); - test.equal(cd.getDays(), 23); - test.equal(cd.getHours(), 16); - test.equal(cd.getMinutes(), 7); - test.equal(cd.getSeconds(), 12); - test.equal(cd.getMilliseconds(), 123); - test.done(); - }).catch(function(err) { - console.log("Error: " + err); - test.fail(); - test.done(); - }); - } catch (e) { - console.log("Got exception: " + e); - test.fail(); - test.done(); - } - }, - - testEthiopicDatePromiseConstructorFull: function(test) { - test.expect(8); - EthiopicDate.create({ - year: 2011, - month: 9, - day: 23, - hour: 16, - minute: 7, - second: 12, - millisecond: 123, - timezone: "Etc/UTC", - sync: false - }).then(function(ed) { - test.ok(ed !== null); - - test.equal(ed.getYears(), 2011); - test.equal(ed.getMonths(), 9); - test.equal(ed.getDays(), 23); - test.equal(ed.getHours(), 16); - test.equal(ed.getMinutes(), 7); - test.equal(ed.getSeconds(), 12); - test.equal(ed.getMilliseconds(), 123); - test.done(); - }); - }, - - testGregDatePromiseConstructorFull: function(test) { - test.expect(8); - GregorianDate.create({ - year: 2011, - month: 9, - day: 23, - hour: 16, - minute: 7, - second: 12, - millisecond: 123, - sync: false - }).then(function(gd) { - test.ok(gd !== null); - - test.equal(gd.getYears(), 2011); - test.equal(gd.getMonths(), 9); - test.equal(gd.getDays(), 23); - test.equal(gd.getHours(), 16); - test.equal(gd.getMinutes(), 7); - test.equal(gd.getSeconds(), 12); - test.equal(gd.getMilliseconds(), 123); - test.done(); - }); - - }, - - testHanDatePromiseConstructorFull: function(test) { - test.expect(10); - HanDate.create({ - year: 4711, - month: 9, - day: 23, - hour: 16, - minute: 7, - second: 12, - millisecond: 123, - timezone: "Etc/UTC", - sync: false - }).then(function(hd) { - test.ok(hd !== null); - - test.equal(hd.getYears(), 4711); - test.equal(hd.getMonths(), 9); - test.equal(hd.getDays(), 23); - test.equal(hd.getHours(), 16); - test.equal(hd.getMinutes(), 7); - test.equal(hd.getSeconds(), 12); - test.equal(hd.getMilliseconds(), 123); - test.equal(hd.getCycles(), 78); - test.equal(hd.getCycleYears(), 31); - test.done(); - }); - - }, - - testHebrewDatePromiseConstructorFull: function(test) { - test.expect(8); - HebrewDate.create({ - year: 2011, - month: 9, - day: 23, - hour: 16, - minute: 7, - second: 12, - millisecond: 123, - timezone: "Etc/UTC", - sync: false - }).then(function(hd) { - test.ok(hd !== null); - - test.equal(hd.getYears(), 2011); - test.equal(hd.getMonths(), 9); - test.equal(hd.getDays(), 23); - test.equal(hd.getHours(), 16); - test.equal(hd.getMinutes(), 7); - test.equal(hd.getSeconds(), 12); - test.equal(hd.getMilliseconds(), 123); - test.done(); - }); - - }, - - testIslamicDatePromiseConstructorFull: function(test) { - test.expect(8); - IslamicDate.create({ - year: 2011, - month: 9, - day: 23, - hour: 16, - minute: 7, - second: 12, - millisecond: 123, - timezone: "Etc/UTC", - sync: false - }).then(function(id) { - test.ok(id !== null); - - test.equal(id.getYears(), 2011); - test.equal(id.getMonths(), 9); - test.equal(id.getDays(), 23); - test.equal(id.getHours(), 16); - test.equal(id.getMinutes(), 7); - test.equal(id.getSeconds(), 12); - test.equal(id.getMilliseconds(), 123); - test.done(); - }); - - }, - - testJulDatePromiseConstructorFull: function(test) { - test.expect(8); - JulianDate.create({ - year: 2011, - month: 9, - day: 23, - hour: 16, - minute: 7, - second: 12, - millisecond: 123, - timezone: "Etc/UTC", - sync: false - }).then(function(jul) { - test.ok(jul !== null); - - test.equal(jul.getYears(), 2011); - test.equal(jul.getMonths(), 9); - test.equal(jul.getDays(), 23); - test.equal(jul.getHours(), 16); - test.equal(jul.getMinutes(), 7); - test.equal(jul.getSeconds(), 12); - test.equal(jul.getMilliseconds(), 123); - test.done(); - }); - - }, - - testPersAlgoDatePromiseConstructorFull: function(test) { - test.expect(8); - PersianAlgoDate.create({ - year: 1392, - month: 9, - day: 23, - hour: 16, - minute: 7, - second: 12, - millisecond: 123, - timezone: "Etc/UTC", - sync: false - }).then(function(pd) { - test.ok(pd !== null); - - test.equal(pd.getYears(), 1392); - test.equal(pd.getMonths(), 9); - test.equal(pd.getDays(), 23); - test.equal(pd.getHours(), 16); - test.equal(pd.getMinutes(), 7); - test.equal(pd.getSeconds(), 12); - test.equal(pd.getMilliseconds(), 123); - test.done(); - }); - - }, - - testPersDateAstroAsyncConstructorFull: function(test) { - test.expect(8); - PersianDate.create({ - year: 1392, - month: 9, - day: 23, - hour: 16, - minute: 7, - second: 12, - millisecond: 123, - timezone: "Etc/UTC", - sync: false - }).then(function(pd) { - test.ok(pd !== null); - - test.equal(pd.getYears(), 1392); - test.equal(pd.getMonths(), 9); - test.equal(pd.getDays(), 23); - test.equal(pd.getHours(), 16); - test.equal(pd.getMinutes(), 7); - test.equal(pd.getSeconds(), 12); - test.equal(pd.getMilliseconds(), 123); - test.done(); - }); - - }, - - testThaiSolarDatePromiseConstructorFull: function(test) { - test.expect(8); - ThaiSolarDate.create({ - year: 2553, - month: 9, - day: 23, - hour: 16, - minute: 7, - second: 12, - millisecond: 123, - timezone: "Etc/UTC", - sync: false - }).then(function(td) { - test.ok(td !== null); - - test.equal(td.getYears(), 2553); - test.equal(td.getMonths(), 9); - test.equal(td.getDays(), 23); - test.equal(td.getHours(), 16); - test.equal(td.getMinutes(), 7); - test.equal(td.getSeconds(), 12); - test.equal(td.getMilliseconds(), 123); - test.done(); - }); - }, - - testCalendarPromiseFactoryAsync: function(test) { - test.expect(2); - var cal = CalendarFactoryAsync({ - locale: "th-TH" - }).then(function(cal) { - test.ok(typeof(cal) !== "undefined"); - - test.equal(cal.getType(), "thaisolar"); - test.done(); - }); - } -}; diff --git a/test/testcharmapasync.js b/test/testcharmapasync.js deleted file mode 100644 index 4411e61..0000000 --- a/test/testcharmapasync.js +++ /dev/null @@ -1,137 +0,0 @@ -/* - * testcharmapasync.js - Test the charset mapping routines - * - * Copyright © 2018, 2022 JEDLSoft - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import CharmapFactory from "../src/CharmapFactory.js"; - -export const testcharmapasync = { - testCharmapAsyncConstructor: function(test) { - test.expect(1); - CharmapFactory({ - sync: false, - onLoad: function(cm) { - test.ok(typeof(cm) !== "undefined"); - test.done(); - } - }); - }, - - testCharmapAsyncLoadMap: function(test) { - test.expect(3); - var cm = CharmapFactory({ - name: "ISO-8859-15", - sync: false, - onLoad: function(cm) { - test.ok(typeof(cm) !== "undefined"); - test.ok(typeof(cm.map) !== "undefined"); - test.equal(cm.getName(), "ISO-8859-15"); - test.done(); - } - }); - }, - - testCharmapAsyncAlias: function(test) { - test.expect(3); - var cm = CharmapFactory({ - name: "ISO-Latin-9", - sync: false, - onLoad: function(cm) { - test.ok(typeof(cm) !== "undefined"); - test.ok(typeof(cm.map) !== "undefined"); - test.equal(cm.getName(), "ISO-8859-15"); - test.done(); - } - }); - }, - - testCharmapAsyncLoadAlgorithmic: function(test) { - test.expect(3); - var cm = CharmapFactory({ - name: "UTF-8", - sync: false, - onLoad: function(cm) { - test.ok(typeof(cm) !== "undefined"); - test.ok(typeof(cm.map) === "undefined"); // no map because it's algorithmic - test.equal(cm.getName(), "UTF-8"); - test.done(); - } - }); - }, - - testCharmapAsyncUTF8MapToUnicodeUint8ArrayExtended3: function(test) { - test.expect(2); - var cm = CharmapFactory({ - name: "UTF-8", - sync: false, - onLoad: function(cm) { - test.ok(typeof(cm) !== "undefined"); - var input = new Uint8Array([ - 0xe4, 0xb8, 0x80, - 0xe4, 0xb8, 0x81, - 0xe4, 0xb8, 0x82, - 0xe5, 0x9c, 0x81, - 0xe5, 0x9c, 0x82, - 0xe5, 0x9c, 0x83 - ]); - - test.equal(cm.mapToUnicode(input), "一丁丂圁圂圃"); - test.done(); - } - }); - }, - - testCharmapAsyncCNMapToUnicode: function(test) { - var big5source = [ - 0xa4, 0xb1, // 仃 - 0xa4, 0x48, // 人 - 0xae, 0x49, // 埋 - 0xaa, 0x42, // 朋 - 0xae, 0x54 // 娛 - ]; - - CharmapFactory({ - name: "Big5", - sync: false, - onLoad: function(cm) { - test.ok(typeof(cm) !== "undefined"); - test.deepEqual("仃人埋朋娛", cm.mapToUnicode(big5source)); - test.done(); - } - }); - }, - - testCharmapAsyncCNMapToNative: function(test) { - var big5source = [ - 0xa4, 0xb1, // 仃 - 0xa4, 0x48, // 人 - 0xae, 0x49, // 埋 - 0xaa, 0x42, // 朋 - 0xae, 0x54 // 娛 - ]; - - CharmapFactory({ - name: "Big5", - sync: false, - onLoad: function(cm) { - test.ok(typeof(cm) !== "undefined"); - test.deepEqual(big5source, cm.mapToNative("仃人埋朋娛")); - test.done(); - } - }); - } -}; diff --git a/test/testcharmappromise.js b/test/testcharmappromise.js deleted file mode 100644 index 3a9fb70..0000000 --- a/test/testcharmappromise.js +++ /dev/null @@ -1,122 +0,0 @@ -/* - * testcharmapasync.js - Test the charset mapping routines - * - * Copyright © 2018, 2022 JEDLSoft - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import CharmapFactory, {CharmapFactoryAsync} from "../src/CharmapFactory.js"; - -export const testcharmappromise = { - testCharmapAsyncConstructor: function(test) { - test.expect(1); - CharmapFactoryAsync().then(function(cm) { - test.ok(typeof(cm) !== "undefined"); - test.done(); - }); - }, - - testCharmapAsyncLoadMap: function(test) { - test.expect(3); - var cm = CharmapFactoryAsync({ - name: "ISO-8859-15" - }).then(function(cm) { - test.ok(typeof(cm) !== "undefined"); - test.ok(typeof(cm.map) !== "undefined"); - test.equal(cm.getName(), "ISO-8859-15"); - test.done(); - }); - }, - - testCharmapAsyncAlias: function(test) { - test.expect(3); - var cm = CharmapFactoryAsync({ - name: "ISO-Latin-9" - }).then(function(cm) { - test.ok(typeof(cm) !== "undefined"); - test.ok(typeof(cm.map) !== "undefined"); - test.equal(cm.getName(), "ISO-8859-15"); - test.done(); - }); - }, - - testCharmapAsyncLoadAlgorithmic: function(test) { - test.expect(3); - var cm = CharmapFactoryAsync({ - name: "UTF-8" - }).then(function(cm) { - test.ok(typeof(cm) !== "undefined"); - test.ok(typeof(cm.map) === "undefined"); // no map because it's algorithmic - test.equal(cm.getName(), "UTF-8"); - test.done(); - }); - }, - - testCharmapAsyncUTF8MapToUnicodeUint8ArrayExtended3: function(test) { - test.expect(2); - var cm = CharmapFactoryAsync({ - name: "UTF-8" - }).then(function(cm) { - test.ok(typeof(cm) !== "undefined"); - var input = new Uint8Array([ - 0xe4, 0xb8, 0x80, - 0xe4, 0xb8, 0x81, - 0xe4, 0xb8, 0x82, - 0xe5, 0x9c, 0x81, - 0xe5, 0x9c, 0x82, - 0xe5, 0x9c, 0x83 - ]); - - test.equal(cm.mapToUnicode(input), "一丁丂圁圂圃"); - test.done(); - }); - }, - - testCharmapAsyncCNMapToUnicode: function(test) { - var big5source = [ - 0xa4, 0xb1, // 仃 - 0xa4, 0x48, // 人 - 0xae, 0x49, // 埋 - 0xaa, 0x42, // 朋 - 0xae, 0x54 // 娛 - ]; - - CharmapFactoryAsync({ - name: "Big5" - }).then(function(cm) { - test.ok(typeof(cm) !== "undefined"); - test.deepEqual("仃人埋朋娛", cm.mapToUnicode(big5source)); - test.done(); - }); - }, - - testCharmapAsyncCNMapToNative: function(test) { - var big5source = [ - 0xa4, 0xb1, // 仃 - 0xa4, 0x48, // 人 - 0xae, 0x49, // 埋 - 0xaa, 0x42, // 朋 - 0xae, 0x54 // 娛 - ]; - - CharmapFactoryAsync({ - name: "Big5" - }).then(function(cm) { - test.ok(typeof(cm) !== "undefined"); - test.deepEqual(big5source, cm.mapToNative("仃人埋朋娛")); - test.done(); - }); - } -}; diff --git a/test/testcharsetasync.js b/test/testcharsetasync.js deleted file mode 100644 index 7875552..0000000 --- a/test/testcharsetasync.js +++ /dev/null @@ -1,175 +0,0 @@ -/* - * testcharsetasync.js - test the charset info object - * - * Copyright © 2018, 2022 JEDLSoft - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import Charset from "../src/Charset.js"; - -export const testcharsetasync = { - testCharsetAsyncConstructor: function(test) { - test.expect(1); - new Charset({ - sync: false, - onLoad: function(cs) { - test.ok(cs !== null); - test.done(); - } - }); - }, - - testCharsetAsyncGetStandardNameIdentity: function(test) { - test.expect(2); - new Charset({ - name: "UTF-8", - sync: false, - onLoad: function(cs) { - test.equal(cs.getName(), "UTF-8"); - new Charset({ - name: "KOI8-R", - sync: false, - onLoad: function(cs) { - test.equal(cs.getName(), "KOI8-R"); - test.done(); - } - }); - } - }) - }, - - testCharsetAsyncGetStandardNameUndefined: function(test) { - test.expect(1); - new Charset({ - sync: false, - onLoad: function(cs) { - test.ok(typeof(cs) !== "undefined"); - test.done(); - } - }); - }, - - testCharsetAsyncGetStandardNameIdentityUnknown: function(test) { - test.expect(1); - new Charset({ - name: "foobarfoo", - sync: false, - onLoad: function(cs) { - test.equal(cs.getName(), "foobarfoo"); - test.done(); - } - }); - }, - - testCharsetAsyncGetStandardNameUTF8: function(test) { - test.expect(1); - new Charset({ - name: "UTF8", - sync: false, - onLoad: function(cs) { - test.equal(cs.getName(), "UTF-8"); - test.done(); - } - }); - }, - - testCharsetAsyncGetStandardNameISOLatin1: function(test) { - test.expect(3); - new Charset({ - name: "Latin1", - sync: false, - onLoad: function(cs) { - test.equal(cs.getName(), "ISO-8859-1"); - new Charset({ - name: "ISO-8859-1", - sync: false, - onLoad: function(cs) { - test.equal(cs.getName(), "ISO-8859-1"); - new Charset({ - name: "ISO-Latin-1", - sync: false, - onLoad: function(cs) { - test.equal(cs.getName(), "ISO-8859-1"); - test.done(); - } - }); - } - }); - } - }) - }, - - testCharsetAsyncGetOriginalNameUnknown: function(test) { - test.expect(1); - new Charset({ - name: "foobarfoo", - sync: false, - onLoad: function(cs) { - test.equal(cs.getOriginalName(), "foobarfoo"); - test.done(); - } - }) - }, - - testCharsetAsyncMinCharWidth1: function(test) { - test.expect(1); - new Charset({ - name: "Latin1", - sync: false, - onLoad: function(cs) { - test.equal(cs.getMinCharWidth(), 1); - test.done(); - } - }); - }, - - testCharsetAsyncMinCharWidth2: function(test) { - test.expect(1); - // built-in - new Charset({ - name: "UCS-2", - sync: false, - onLoad: function(cs) { - test.equal(cs.getMinCharWidth(), 2); - test.done(); - } - }); - }, - - testCharsetAsyncIsMultibyteTrue: function(test) { - test.expect(1); - new Charset({ - name: "Shift_JIS", - sync: false, - onLoad: function(cs) { - test.ok(cs.isMultibyte()); - test.done(); - } - }); - }, - - - testCharsetAsyncIsBigEndianUTF16: function(test) { - test.expect(1); - new Charset({ - name: "UTF-16", - sync: false, - onLoad: function(cs) { - test.ok(cs.isBigEndian()); - test.done(); - } - }); - } -}; diff --git a/test/testcharsetpromise.js b/test/testcharsetpromise.js deleted file mode 100644 index 34c4889..0000000 --- a/test/testcharsetpromise.js +++ /dev/null @@ -1,161 +0,0 @@ -/* - * testcharsetasync.js - test the charset info object - * - * Copyright © 2018, 2022 JEDLSoft - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import Charset from "../src/Charset.js"; - -export const testcharsetpromise = { - testCharsetAsyncConstructor: function(test) { - test.expect(1); - Charset.create({ - sync: false - }).then(function(cs) { - test.ok(cs !== null); - test.done(); - }); - }, - - testCharsetAsyncGetStandardNameIdentity: function(test) { - test.expect(2); - Charset.create({ - name: "UTF-8", - sync: false - }).then(function(cs) { - test.equal(cs.getName(), "UTF-8"); - return Charset.create({ - name: "KOI8-R", - sync: false - }); - }).then(function(cs) { - test.equal(cs.getName(), "KOI8-R"); - test.done(); - }); - }, - - testCharsetAsyncGetStandardNameUndefined: function(test) { - test.expect(1); - Charset.create({ - sync: false - }).then(function(cs) { - test.ok(typeof(cs) !== "undefined"); - test.done(); - }); - }, - - testCharsetAsyncGetStandardNameIdentityUnknown: function(test) { - test.expect(1); - Charset.create({ - name: "foobarfoo", - sync: false - }).then(function(cs) { - test.equal(cs.getName(), "foobarfoo"); - test.done(); - }); - }, - - testCharsetAsyncGetStandardNameUTF8: function(test) { - test.expect(1); - Charset.create({ - name: "UTF8", - sync: false - }).then(function(cs) { - test.equal(cs.getName(), "UTF-8"); - test.done(); - }); - }, - - testCharsetAsyncGetStandardNameISOLatin1: function(test) { - test.expect(3); - Charset.create({ - name: "Latin1", - sync: false - }).then(function(cs) { - test.equal(cs.getName(), "ISO-8859-1"); - return Charset.create({ - name: "ISO-8859-1", - sync: false - }); - }).then(function(cs) { - test.equal(cs.getName(), "ISO-8859-1"); - return Charset.create({ - name: "ISO-Latin-1", - sync: false - }); - }).then(function(cs) { - test.equal(cs.getName(), "ISO-8859-1"); - test.done(); - }); - }, - - testCharsetAsyncGetOriginalNameUnknown: function(test) { - test.expect(1); - Charset.create({ - name: "foobarfoo", - sync: false - }).then(function(cs) { - test.equal(cs.getOriginalName(), "foobarfoo"); - test.done(); - }); - }, - - testCharsetAsyncMinCharWidth1: function(test) { - test.expect(1); - Charset.create({ - name: "Latin1", - sync: false - }).then(function(cs) { - test.equal(cs.getMinCharWidth(), 1); - test.done(); - }); - }, - - testCharsetAsyncMinCharWidth2: function(test) { - test.expect(1); - // built-in - Charset.create({ - name: "UCS-2", - sync: false - }).then(function(cs) { - test.equal(cs.getMinCharWidth(), 2); - test.done(); - }); - }, - - testCharsetAsyncIsMultibyteTrue: function(test) { - test.expect(1); - Charset.create({ - name: "Shift_JIS", - sync: false - }).then(function(cs) { - test.ok(cs.isMultibyte()); - test.done(); - }); - }, - - - testCharsetAsyncIsBigEndianUTF16: function(test) { - test.expect(1); - Charset.create({ - name: "UTF-16", - sync: false - }).then(function(cs) { - test.ok(cs.isBigEndian()); - test.done(); - }); - } -}; diff --git a/test/testcollationasync.js b/test/testcollationasync.js deleted file mode 100644 index 90b0d06..0000000 --- a/test/testcollationasync.js +++ /dev/null @@ -1,490 +0,0 @@ -/* - * testcollationasync.js - test the Collator object asynchronously - * - * Copyright © 2018, 2022 JEDLSoft - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import ilib from "../src/ilib.js"; -import Collator from "../src/Collator.js"; - -export const testcollationasync = { - testCollatorAsyncConstructorNative: function(test) { - test.expect(1); - var col = new Collator({ - sync: false, - onLoad: function(col) { - test.ok(typeof(col) !== "undefined"); - test.done(); - } - }); - }, - - testCollatorAsyncDefaultNative: function(test) { - test.expect(5); - var col = new Collator({ - sync: false, - onLoad: function(col) { - test.ok(typeof(col) !== "undefined"); - - // should compare in English - test.equal(0, col.compare("string", "string"), "equality"); - test.ok("a < b", col.compare("a", "b") < 0); - test.ok("b < c", col.compare("b", "c") < 0); - test.ok("c < z", col.compare("c", "z") < 0); - test.done(); - } - }); - - }, - - testCollatorAsyncDefaultCase: function(test) { - test.expect(5); - var col = new Collator({ - sync: false, - onLoad: function(col) { - test.ok(typeof(col) !== "undefined"); - // netscape and ie do not work properly on some platforms - var browser = ilib._getBrowser(); - if ((browser === "firefox" && - navigator && - navigator.userAgent && - navigator.userAgent.indexOf("Android")) || - browser === "ie" || browser === "Edge" || browser === "iOS") { - // should compare lower-case first within a base character - test.ok("a < A", col.compare("a", "A") < 0); - test.ok("b < B", col.compare("b", "B") < 0); - test.ok("a < Z", col.compare("a", "Z") < 0); - test.ok("a < Á", col.compare("a", "Á") < 0); - } else { - // should compare upper-case first within a base character - test.ok("A < a", col.compare("A", "a") < 0); - test.ok("B < b", col.compare("B", "b") < 0); - test.ok("a < Z", col.compare("a", "Z") < 0); - test.ok("Á < a", col.compare("A", "a") < 0); // accent is more important than case - } - test.done(); - } - }); - }, - - testCollatorAsyncGetComparator: function(test) { - test.expect(3); - var col = new Collator({ - sync: false, - onLoad: function(col) { - test.ok(typeof(col) !== "undefined"); - - // should compare in English - var func = col.getComparator(); - test.ok(typeof(func) !== "undefined"); - test.equal(typeof(func), "function"); - test.done(); - } - }); - }, - - - testCollatorAsyncConstructorJS: function(test) { - test.expect(1); - var col = new Collator({ - useNative: false, - sync: false, - onLoad: function(col) { - test.ok(typeof(col) !== "undefined"); - test.done(); - } - }); - - }, - - testCollatorAsyncDefaultJS: function(test) { - test.expect(5); - var col = new Collator({ - useNative: false, - sync: false, - onLoad: function(col) { - test.ok(typeof(col) !== "undefined"); - - // should compare in English - test.equal(0, col.compare("string", "string"), "equality"); - test.ok("a < b", col.compare("a", "b") < 0); - test.ok("b < c", col.compare("b", "c") < 0); - test.ok("c < z", col.compare("c", "z") < 0); - test.done(); - } - }); - - }, - - - testCollatorAsyncGetComparatorWorksWithCaseJS: function(test) { - test.expect(6); - var col = new Collator({ - useNative: false, - sync: false, - onLoad: function(col) { - test.ok(typeof(col) !== "undefined"); - - var func = col.getComparator(); - test.ok(typeof(func) !== "undefined"); - - // should compare upper-case first - test.ok("A < a", func("A", "a") < 0); - test.ok("B < b", func("B", "b") < 0); - test.ok("a < Z", func("a", "Z") < 0); - test.ok("Á < a", func("A", "a") < 0); - test.done(); - } - }); - - }, - - - testCollatorAsyncGetSortKeyNative: function(test) { - if (typeof(Intl) === 'undefined' && Intl) { - test.done(); - return; - } - var col = new Collator({ - sync: false, - onLoad: function(col) { - test.expect(2); - test.ok(typeof(col) !== "undefined"); - - // no sort key available when using native... - test.equal(col.sortKey("string"), "string"); - test.done(); - } - }); - - }, - - testCollatorAsyncGetSortKeySimpleUpper: function(test) { - test.expect(2); - var col = new Collator({ - useNative: false, - sync: false, - onLoad: function(col) { - test.ok(typeof(col) !== "undefined"); - - test.equal(col.sortKey("ABCDEF"), "4204404604804a04c0"); - test.done(); - } - }); - - }, - - testCollatorAsyncGetSortKeyMixed: function(test) { - test.expect(2); - var col = new Collator({ - useNative: false, - sync: false, - onLoad: function(col) { - test.ok(typeof(col) !== "undefined"); - - test.equal(col.sortKey("String"), "6606826425225c24e2"); - test.done(); - } - }); - - }, - - testCollatorAsyncWithSort: function(test) { - test.expect(2); - var col = new Collator({ - sync: false, - onLoad: function(col) { - test.ok(typeof(col) !== "undefined"); - - var input = ["q", "w", "e", "r", "t", "y", "u", "i", "o", "p"]; - - input.sort(col.getComparator()); - - var expected = ["e", "i", "o", "p", "q", "r", "t", "u", "w", "y"]; - - test.deepEqual(input, expected); - test.done(); - } - }); - }, - - testCollatorAsyncWithSortJS: function(test) { - test.expect(2); - var col = new Collator({ - useNative: false, - sync: false, - onLoad: function(col) { - test.ok(typeof(col) !== "undefined"); - - var input = ["q", "w", "e", "r", "t", "y", "u", "i", "o", "p"]; - - input.sort(col.getComparator()); - - var expected = ["e", "i", "o", "p", "q", "r", "t", "u", "w", "y"]; - - test.deepEqual(input, expected); - test.done(); - } - }); - }, - - testCollatorAsyncWithSortUpperFirstJS: function(test) { - test.expect(2); - var col = new Collator({ - upperFirst: true, - useNative: false, - sync: false, - onLoad: function(col) { - test.ok(typeof(col) !== "undefined"); - - var input = ["q", "I", "e", "r", "T", "U", "i", "E", "o", "p"]; - - input.sort(col.getComparator()); - - var expected = ["E", "e", "I", "i", "o", "p", "q", "r", "T", "U"]; - - test.deepEqual(input, expected); - test.done(); - } - }); - }, - - testCollatorAsyncGetAvailableScripts: function(test) { - test.expect(1); - test.deepEqual(Collator.getAvailableScripts(), ["Latn"]); - test.done(); - }, - - testCollatorAsyncGetAvailableStyles: function(test) { - test.expect(1); - test.deepEqual(Collator.getAvailableStyles(), ["standard"]); - test.done(); - }, - - testCollatorAsyncPhonebookQuatAE_de: function(test) { - test.expect(5); - var col = new Collator({ - locale: "de-DE", - useNative: false, - sensitivity: "quaternary", - style: "phonebook", - usage: "search", - sync: false, - onLoad: function(col) { - test.ok(typeof(col) !== "undefined"); - - // aa ae äa äz af - test.ok("aa < ae", col.compare("aa", "ae") < 0); - test.ok("ae < äa", col.compare("ae", "äa") < 0); - test.ok("äa < äz", col.compare("äa", "äz") < 0); - test.ok("äz < af", col.compare("äz", "af") < 0); - test.done(); - } - }); - - }, - - testCollatorAsyncQuat_el: function(test) { - test.expect(91); - var col = new Collator({ - locale: "el-GR", - useNative: false, - sensitivity: "quaternary", - usage: "search", - sync: false, - onLoad: function(col) { - test.ok(typeof(col) !== "undefined"); - - // Α α Ά Ά ά ά Β β Γ γ Δ δ Ε ε Έ Έ έ έ Ζ ζ Η η Ή Ή ή ή Θ θ Ι ι Ί Ί ί ί Ϊ Ϊ ϊ ϊ ΐ ΐ ί̈ Κ κ Λ λ Μ μ Ν ν Ξ ξ Ο ο Ό Ό ό ό Π π Ρ ρ Σ σ ς Τ τ Υ υ Ύ Ύ ύ ύ Ϋ Ϋ ϋ ϋ ΰ ΰ ύ̈ Φ φ Χ χ Ψ ψ Ω ω Ώ Ώ ώ ώ - - test.ok("Α < α", col.compare("Α", "α") < 0); - test.ok("α < Ά", col.compare("α", "Ά") < 0); - test.ok("Ά < Ά", col.compare("Ά", "Ά") < 0); - test.ok("Ά < ά", col.compare("Ά", "ά") < 0); - test.ok("ά < ά", col.compare("ά", "ά") < 0); - test.ok("ά < Β", col.compare("ά", "Β") < 0); - test.ok("Β < β", col.compare("Β", "β") < 0); - test.ok("β < Γ", col.compare("β", "Γ") < 0); - test.ok("Γ < γ", col.compare("Γ", "γ") < 0); - test.ok("γ < Δ", col.compare("γ", "Δ") < 0); - test.ok("Δ < δ", col.compare("Δ", "δ") < 0); - test.ok("δ < Ε", col.compare("δ", "Ε") < 0); - test.ok("Ε < ε", col.compare("Ε", "ε") < 0); - test.ok("ε < Έ", col.compare("ε", "Έ") < 0); - test.ok("Έ < Έ", col.compare("Έ", "Έ") < 0); - test.ok("Έ < έ", col.compare("Έ", "έ") < 0); - test.ok("έ < έ", col.compare("έ", "έ") < 0); - test.ok("έ < Ζ", col.compare("έ", "Ζ") < 0); - test.ok("Ζ < ζ", col.compare("Ζ", "ζ") < 0); - test.ok("ζ < Η", col.compare("ζ", "Η") < 0); - test.ok("Η < η", col.compare("Η", "η") < 0); - test.ok("η < Ή", col.compare("η", "Ή") < 0); - test.ok("Ή < Ή", col.compare("Ή", "Ή") < 0); - test.ok("Ή < ή", col.compare("Ή", "ή") < 0); - test.ok("ή < ή", col.compare("ή", "ή") < 0); - test.ok("ή < Θ", col.compare("ή", "Θ") < 0); - test.ok("Θ < θ", col.compare("Θ", "θ") < 0); - test.ok("θ < Ι", col.compare("θ", "Ι") < 0); - test.ok("Ι < ι", col.compare("Ι", "ι") < 0); - test.ok("ι < Ί", col.compare("ι", "Ί") < 0); - test.ok("Ί < Ί", col.compare("Ί", "Ί") < 0); - test.ok("Ί < ί", col.compare("Ί", "ί") < 0); - test.ok("ί < ί", col.compare("ί", "ί") < 0); - test.ok("ί < Ϊ", col.compare("ί", "Ϊ") < 0); - test.ok("Ϊ < Ϊ", col.compare("Ϊ", "Ϊ") < 0); - test.ok("Ϊ < ϊ", col.compare("Ϊ", "ϊ") < 0); - test.ok("ϊ < ϊ", col.compare("ϊ", "ϊ") < 0); - test.ok("ϊ < ΐ", col.compare("ϊ", "ΐ") < 0); - test.ok("ΐ < ΐ", col.compare("ΐ", "ΐ") < 0); - test.ok("ΐ < ί̈", col.compare("ΐ", "ί̈") < 0); - test.ok("ί̈ < Κ", col.compare("ί̈", "Κ") < 0); - test.ok("Κ < κ", col.compare("Κ", "κ") < 0); - test.ok("κ < Λ", col.compare("κ", "Λ") < 0); - test.ok("Λ < λ", col.compare("Λ", "λ") < 0); - test.ok("λ < Μ", col.compare("λ", "Μ") < 0); - test.ok("Μ < μ", col.compare("Μ", "μ") < 0); - test.ok("μ < Ν", col.compare("μ", "Ν") < 0); - test.ok("Ν < ν", col.compare("Ν", "ν") < 0); - test.ok("ν < Ξ", col.compare("ν", "Ξ") < 0); - test.ok("Ξ < ξ", col.compare("Ξ", "ξ") < 0); - test.ok("ξ < Ο", col.compare("ξ", "Ο") < 0); - test.ok("Ο < ο", col.compare("Ο", "ο") < 0); - test.ok("ο < Ό", col.compare("ο", "Ό") < 0); - test.ok("Ό < Ό", col.compare("Ό", "Ό") < 0); - test.ok("Ό < ό", col.compare("Ό", "ό") < 0); - test.ok("ό < ό", col.compare("ό", "ό") < 0); - test.ok("ό < Π", col.compare("ό", "Π") < 0); - test.ok("Π < π", col.compare("Π", "π") < 0); - test.ok("π < Ρ", col.compare("π", "Ρ") < 0); - test.ok("Ρ < ρ", col.compare("Ρ", "ρ") < 0); - test.ok("ρ < Σ", col.compare("ρ", "Σ") < 0); - test.ok("Σ < σ", col.compare("Σ", "σ") < 0); - test.ok("σ < ς", col.compare("σ", "ς") < 0); - test.ok("ς < Τ", col.compare("ς", "Τ") < 0); - test.ok("Τ < τ", col.compare("Τ", "τ") < 0); - test.ok("τ < Υ", col.compare("τ", "Υ") < 0); - test.ok("Υ < υ", col.compare("Υ", "υ") < 0); - test.ok("υ < Ύ", col.compare("υ", "Ύ") < 0); - test.ok("Ύ < Ύ", col.compare("Ύ", "Ύ") < 0); - test.ok("Ύ < ύ", col.compare("Ύ", "ύ") < 0); - test.ok("ύ < ύ", col.compare("ύ", "ύ") < 0); - test.ok("ύ < Ϋ", col.compare("ύ", "Ϋ") < 0); - test.ok("Ϋ < Ϋ", col.compare("Ϋ", "Ϋ") < 0); - test.ok("Ϋ < ϋ", col.compare("Ϋ", "ϋ") < 0); - test.ok("ϋ < ϋ", col.compare("ϋ", "ϋ") < 0); - test.ok("ϋ < ΰ", col.compare("ϋ", "ΰ") < 0); - test.ok("ΰ < ΰ", col.compare("ΰ", "ΰ") < 0); - test.ok("ΰ < ύ̈", col.compare("ΰ", "ύ̈") < 0); - test.ok("ύ̈ < Φ", col.compare("ύ̈", "Φ") < 0); - test.ok("Φ < φ", col.compare("Φ", "φ") < 0); - test.ok("φ < Χ", col.compare("φ", "Χ") < 0); - test.ok("Χ < χ", col.compare("Χ", "χ") < 0); - test.ok("χ < Ψ", col.compare("χ", "Ψ") < 0); - test.ok("Ψ < ψ", col.compare("Ψ", "ψ") < 0); - test.ok("ψ < Ω", col.compare("ψ", "Ω") < 0); - test.ok("Ω < ω", col.compare("Ω", "ω") < 0); - test.ok("ω < Ώ", col.compare("ω", "Ώ") < 0); - test.ok("Ώ < Ώ", col.compare("Ώ", "Ώ") < 0); - test.ok("Ώ < ώ", col.compare("Ώ", "ώ") < 0); - test.ok("ώ < ώ", col.compare("ώ", "ώ") < 0); - test.done(); - } - }); - - }, - - testCollatorAsyncPriL_ko: function(test) { - test.expect(5); - var col = new Collator({ - locale: "ko-KR", - useNative: false, - sensitivity: "primary", - sync: false, - onLoad: function(col) { - test.ok(typeof(col) !== "undefined"); - - // 가까나다따 - test.ok("가 < 까", col.compare("가", "까") < 0); - test.ok("까 < 나", col.compare("까", "나") < 0); - test.ok("나 < 다", col.compare("나", "다") < 0); - test.ok("다 < 따", col.compare("다", "따") < 0); - test.done(); - } - }); - - }, - - testCollatorAsyncQuatHanzi_zh_Hans: function(test) { - test.expect(21); - var col = new Collator({ - locale: "zh-Hans-CN", - useNative: false, - sensitivity: "quaternary", - usage: "search", - sync: false, - onLoad: function(col) { - // Hanzi are all primary differences from each other - - test.ok(typeof(col) !== "undefined"); - - test.ok("阿 < 拜", col.compare("阿", "拜") < 0); - test.ok("拜 < 𩑻", col.compare("拜", "𩑻") < 0); - test.ok("𩑻 < 䯋", col.compare("𩑻", "䯋") < 0); - test.ok("䯋 < 𧀱", col.compare("䯋", "𧀱") < 0); - test.ok("𧀱 < 捶", col.compare("𧀱", "捶") < 0); - test.ok("捶 < 峒", col.compare("捶", "峒") < 0); - test.ok("峒 < 㶥", col.compare("峒", "㶥") < 0); - test.ok("㶥 < 㜳", col.compare("㶥", "㜳") < 0); - test.ok("㜳 < 䌸", col.compare("㜳", "䌸") < 0); - test.ok("䌸 < 䢧", col.compare("䌸", "䢧") < 0); - test.ok("䢧 < 苜", col.compare("䢧", "苜") < 0); - test.ok("苜 < 肶", col.compare("苜", "肶") < 0); - test.ok("肶 < 埁", col.compare("肶", "埁") < 0); - test.ok("埁 < 泩", col.compare("埁", "泩") < 0); - test.ok("泩 < 窱", col.compare("泩", "窱") < 0); - test.ok("窱 < 扤", col.compare("窱", "扤") < 0); - test.ok("扤 < 辥", col.compare("扤", "辥") < 0); - test.ok("辥 < 䓰", col.compare("辥", "䓰") < 0); - test.ok("䓰 < 赵", col.compare("䓰", "赵") < 0); - test.ok("赵 < 蓙", col.compare("赵", "蓙") < 0); - test.done(); - } - }); - - }, - - testCollatorAsyncTraditionalQuatCH_es: function(test) { - test.expect(6); - var col = new Collator({ - locale: "es-ES", - useNative: false, - sensitivity: "quaternary", - style: "traditional", - sync: false, - onLoad: function(col) { - test.ok(typeof(col) !== "undefined"); - - // a b c ch d - test.ok("a < b", col.compare("a", "b") < 0); - test.ok("b < c", col.compare("b", "c") < 0); - test.ok("ca < ch", col.compare("ca", "ch") < 0); - test.ok("cz < ch", col.compare("cz", "ch") < 0); - test.ok("ch < d", col.compare("ch", "d") < 0); - test.done(); - } - }); - } -}; \ No newline at end of file diff --git a/test/testcollationpromise.js b/test/testcollationpromise.js deleted file mode 100644 index 8aeff3e..0000000 --- a/test/testcollationpromise.js +++ /dev/null @@ -1,473 +0,0 @@ -/* - * testcollationpromise.js - test the Collator object asynchronously - * - * Copyright © 2018, 2022 JEDLSoft - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import ilib from "../src/ilib.js"; -import Collator from "../src/Collator.js"; - -export const testcollationpromise = { - testCollatorAsyncConstructorNative: function(test) { - test.expect(1); - var col = Collator.create({ - sync: false - }).then(function(col) { - test.ok(typeof(col) !== "undefined"); - test.done(); - }); - - }, - - testCollatorAsyncDefaultNative: function(test) { - test.expect(5); - var col = Collator.create({ - sync: false - }).then(function(col) { - test.ok(typeof(col) !== "undefined"); - - // should compare in English - test.equal(0, col.compare("string", "string"), "equality"); - test.ok("a < b", col.compare("a", "b") < 0); - test.ok("b < c", col.compare("b", "c") < 0); - test.ok("c < z", col.compare("c", "z") < 0); - test.done(); - }); - - }, - - testCollatorAsyncDefaultCase: function(test) { - test.expect(5); - var col = Collator.create({ - sync: false - }).then(function(col) { - test.ok(typeof(col) !== "undefined"); - // netscape and ie do not work properly on some platforms - var browser = ilib._getBrowser(); - if ((browser === "firefox" && - navigator && - navigator.userAgent && - navigator.userAgent.indexOf("Android")) || - browser === "ie" || browser === "Edge" || browser === "iOS") { - // should compare lower-case first within a base character - test.ok("a < A", col.compare("a", "A") < 0); - test.ok("b < B", col.compare("b", "B") < 0); - test.ok("a < Z", col.compare("a", "Z") < 0); - test.ok("a < Á", col.compare("a", "Á") < 0); - } else { - // should compare upper-case first within a base character - test.ok("A < a", col.compare("A", "a") < 0); - test.ok("B < b", col.compare("B", "b") < 0); - test.ok("a < Z", col.compare("a", "Z") < 0); - test.ok("Á < a", col.compare("A", "a") < 0); // accent is more important than case - } - test.done(); - }); - }, - - testCollatorAsyncGetComparator: function(test) { - test.expect(3); - var col = Collator.create({ - sync: false - }).then(function(col) { - test.ok(typeof(col) !== "undefined"); - - // should compare in English - var func = col.getComparator(); - test.ok(typeof(func) !== "undefined"); - test.equal(typeof(func), "function"); - test.done(); - }); - }, - - - testCollatorAsyncConstructorJS: function(test) { - test.expect(1); - var col = Collator.create({ - useNative: false, - sync: false - }).then(function(col) { - test.ok(typeof(col) !== "undefined"); - test.done(); - }); - - }, - - testCollatorAsyncDefaultJS: function(test) { - test.expect(5); - var col = Collator.create({ - useNative: false, - sync: false - }).then(function(col) { - test.ok(typeof(col) !== "undefined"); - - // should compare in English - test.equal(0, col.compare("string", "string"), "equality"); - test.ok("a < b", col.compare("a", "b") < 0); - test.ok("b < c", col.compare("b", "c") < 0); - test.ok("c < z", col.compare("c", "z") < 0); - test.done(); - }); - - }, - - - testCollatorAsyncGetComparatorWorksWithCaseJS: function(test) { - test.expect(6); - var col = Collator.create({ - useNative: false, - sync: false - }).then(function(col) { - test.ok(typeof(col) !== "undefined"); - - var func = col.getComparator(); - test.ok(typeof(func) !== "undefined"); - - // should compare upper-case first - test.ok("A < a", func("A", "a") < 0); - test.ok("B < b", func("B", "b") < 0); - test.ok("a < Z", func("a", "Z") < 0); - test.ok("Á < a", func("A", "a") < 0); - test.done(); - }); - - }, - - - testCollatorAsyncGetSortKeyNative: function(test) { - if (typeof(Intl) === 'undefined' && Intl) { - test.done(); - return; - } - var col = Collator.create({ - sync: false - }).then(function(col) { - test.expect(2); - test.ok(typeof(col) !== "undefined"); - - // no sort key available when using native... - test.equal(col.sortKey("string"), "string"); - test.done(); - }); - - }, - - testCollatorAsyncGetSortKeySimpleUpper: function(test) { - test.expect(2); - var col = Collator.create({ - useNative: false, - sync: false - }).then(function(col) { - test.ok(typeof(col) !== "undefined"); - - test.equal(col.sortKey("ABCDEF"), "4204404604804a04c0"); - test.done(); - }); - - }, - - testCollatorAsyncGetSortKeyMixed: function(test) { - test.expect(2); - var col = Collator.create({ - useNative: false, - sync: false - }).then(function(col) { - test.ok(typeof(col) !== "undefined"); - - test.equal(col.sortKey("String"), "6606826425225c24e2"); - test.done(); - }); - - }, - - testCollatorAsyncWithSort: function(test) { - test.expect(2); - var col = Collator.create({ - sync: false - }).then(function(col) { - test.ok(typeof(col) !== "undefined"); - - var input = ["q", "w", "e", "r", "t", "y", "u", "i", "o", "p"]; - - input.sort(col.getComparator()); - - var expected = ["e", "i", "o", "p", "q", "r", "t", "u", "w", "y"]; - - test.deepEqual(input, expected); - test.done(); - }); - }, - - testCollatorAsyncWithSortJS: function(test) { - test.expect(2); - var col = Collator.create({ - useNative: false, - sync: false - }).then(function(col) { - test.ok(typeof(col) !== "undefined"); - - var input = ["q", "w", "e", "r", "t", "y", "u", "i", "o", "p"]; - - input.sort(col.getComparator()); - - var expected = ["e", "i", "o", "p", "q", "r", "t", "u", "w", "y"]; - - test.deepEqual(input, expected); - test.done(); - }); - }, - - testCollatorAsyncWithSortUpperFirstJS: function(test) { - test.expect(2); - var col = Collator.create({ - upperFirst: true, - useNative: false, - sync: false - }).then(function(col) { - test.ok(typeof(col) !== "undefined"); - - var input = ["q", "I", "e", "r", "T", "U", "i", "E", "o", "p"]; - - input.sort(col.getComparator()); - - var expected = ["E", "e", "I", "i", "o", "p", "q", "r", "T", "U"]; - - test.deepEqual(input, expected); - test.done(); - }); - }, - - testCollatorAsyncGetAvailableScripts: function(test) { - test.expect(1); - test.deepEqual(Collator.getAvailableScripts(), ["Latn"]); - test.done(); - }, - - testCollatorAsyncGetAvailableStyles: function(test) { - test.expect(1); - test.deepEqual(Collator.getAvailableStyles(), ["standard"]); - test.done(); - }, - - testCollatorAsyncPhonebookQuatAE_de: function(test) { - test.expect(5); - var col = Collator.create({ - locale: "de-DE", - useNative: false, - sensitivity: "quaternary", - style: "phonebook", - usage: "search", - sync: false - }).then(function(col) { - test.ok(typeof(col) !== "undefined"); - - // aa ae äa äz af - test.ok("aa < ae", col.compare("aa", "ae") < 0); - test.ok("ae < äa", col.compare("ae", "äa") < 0); - test.ok("äa < äz", col.compare("äa", "äz") < 0); - test.ok("äz < af", col.compare("äz", "af") < 0); - test.done(); - }); - - }, - - testCollatorAsyncQuat_el: function(test) { - test.expect(91); - var col = Collator.create({ - locale: "el-GR", - useNative: false, - sensitivity: "quaternary", - usage: "search", - sync: false - }).then(function(col) { - test.ok(typeof(col) !== "undefined"); - - // Α α Ά Ά ά ά Β β Γ γ Δ δ Ε ε Έ Έ έ έ Ζ ζ Η η Ή Ή ή ή Θ θ Ι ι Ί Ί ί ί Ϊ Ϊ ϊ ϊ ΐ ΐ ί̈ Κ κ Λ λ Μ μ Ν ν Ξ ξ Ο ο Ό Ό ό ό Π π Ρ ρ Σ σ ς Τ τ Υ υ Ύ Ύ ύ ύ Ϋ Ϋ ϋ ϋ ΰ ΰ ύ̈ Φ φ Χ χ Ψ ψ Ω ω Ώ Ώ ώ ώ - - test.ok("Α < α", col.compare("Α", "α") < 0); - test.ok("α < Ά", col.compare("α", "Ά") < 0); - test.ok("Ά < Ά", col.compare("Ά", "Ά") < 0); - test.ok("Ά < ά", col.compare("Ά", "ά") < 0); - test.ok("ά < ά", col.compare("ά", "ά") < 0); - test.ok("ά < Β", col.compare("ά", "Β") < 0); - test.ok("Β < β", col.compare("Β", "β") < 0); - test.ok("β < Γ", col.compare("β", "Γ") < 0); - test.ok("Γ < γ", col.compare("Γ", "γ") < 0); - test.ok("γ < Δ", col.compare("γ", "Δ") < 0); - test.ok("Δ < δ", col.compare("Δ", "δ") < 0); - test.ok("δ < Ε", col.compare("δ", "Ε") < 0); - test.ok("Ε < ε", col.compare("Ε", "ε") < 0); - test.ok("ε < Έ", col.compare("ε", "Έ") < 0); - test.ok("Έ < Έ", col.compare("Έ", "Έ") < 0); - test.ok("Έ < έ", col.compare("Έ", "έ") < 0); - test.ok("έ < έ", col.compare("έ", "έ") < 0); - test.ok("έ < Ζ", col.compare("έ", "Ζ") < 0); - test.ok("Ζ < ζ", col.compare("Ζ", "ζ") < 0); - test.ok("ζ < Η", col.compare("ζ", "Η") < 0); - test.ok("Η < η", col.compare("Η", "η") < 0); - test.ok("η < Ή", col.compare("η", "Ή") < 0); - test.ok("Ή < Ή", col.compare("Ή", "Ή") < 0); - test.ok("Ή < ή", col.compare("Ή", "ή") < 0); - test.ok("ή < ή", col.compare("ή", "ή") < 0); - test.ok("ή < Θ", col.compare("ή", "Θ") < 0); - test.ok("Θ < θ", col.compare("Θ", "θ") < 0); - test.ok("θ < Ι", col.compare("θ", "Ι") < 0); - test.ok("Ι < ι", col.compare("Ι", "ι") < 0); - test.ok("ι < Ί", col.compare("ι", "Ί") < 0); - test.ok("Ί < Ί", col.compare("Ί", "Ί") < 0); - test.ok("Ί < ί", col.compare("Ί", "ί") < 0); - test.ok("ί < ί", col.compare("ί", "ί") < 0); - test.ok("ί < Ϊ", col.compare("ί", "Ϊ") < 0); - test.ok("Ϊ < Ϊ", col.compare("Ϊ", "Ϊ") < 0); - test.ok("Ϊ < ϊ", col.compare("Ϊ", "ϊ") < 0); - test.ok("ϊ < ϊ", col.compare("ϊ", "ϊ") < 0); - test.ok("ϊ < ΐ", col.compare("ϊ", "ΐ") < 0); - test.ok("ΐ < ΐ", col.compare("ΐ", "ΐ") < 0); - test.ok("ΐ < ί̈", col.compare("ΐ", "ί̈") < 0); - test.ok("ί̈ < Κ", col.compare("ί̈", "Κ") < 0); - test.ok("Κ < κ", col.compare("Κ", "κ") < 0); - test.ok("κ < Λ", col.compare("κ", "Λ") < 0); - test.ok("Λ < λ", col.compare("Λ", "λ") < 0); - test.ok("λ < Μ", col.compare("λ", "Μ") < 0); - test.ok("Μ < μ", col.compare("Μ", "μ") < 0); - test.ok("μ < Ν", col.compare("μ", "Ν") < 0); - test.ok("Ν < ν", col.compare("Ν", "ν") < 0); - test.ok("ν < Ξ", col.compare("ν", "Ξ") < 0); - test.ok("Ξ < ξ", col.compare("Ξ", "ξ") < 0); - test.ok("ξ < Ο", col.compare("ξ", "Ο") < 0); - test.ok("Ο < ο", col.compare("Ο", "ο") < 0); - test.ok("ο < Ό", col.compare("ο", "Ό") < 0); - test.ok("Ό < Ό", col.compare("Ό", "Ό") < 0); - test.ok("Ό < ό", col.compare("Ό", "ό") < 0); - test.ok("ό < ό", col.compare("ό", "ό") < 0); - test.ok("ό < Π", col.compare("ό", "Π") < 0); - test.ok("Π < π", col.compare("Π", "π") < 0); - test.ok("π < Ρ", col.compare("π", "Ρ") < 0); - test.ok("Ρ < ρ", col.compare("Ρ", "ρ") < 0); - test.ok("ρ < Σ", col.compare("ρ", "Σ") < 0); - test.ok("Σ < σ", col.compare("Σ", "σ") < 0); - test.ok("σ < ς", col.compare("σ", "ς") < 0); - test.ok("ς < Τ", col.compare("ς", "Τ") < 0); - test.ok("Τ < τ", col.compare("Τ", "τ") < 0); - test.ok("τ < Υ", col.compare("τ", "Υ") < 0); - test.ok("Υ < υ", col.compare("Υ", "υ") < 0); - test.ok("υ < Ύ", col.compare("υ", "Ύ") < 0); - test.ok("Ύ < Ύ", col.compare("Ύ", "Ύ") < 0); - test.ok("Ύ < ύ", col.compare("Ύ", "ύ") < 0); - test.ok("ύ < ύ", col.compare("ύ", "ύ") < 0); - test.ok("ύ < Ϋ", col.compare("ύ", "Ϋ") < 0); - test.ok("Ϋ < Ϋ", col.compare("Ϋ", "Ϋ") < 0); - test.ok("Ϋ < ϋ", col.compare("Ϋ", "ϋ") < 0); - test.ok("ϋ < ϋ", col.compare("ϋ", "ϋ") < 0); - test.ok("ϋ < ΰ", col.compare("ϋ", "ΰ") < 0); - test.ok("ΰ < ΰ", col.compare("ΰ", "ΰ") < 0); - test.ok("ΰ < ύ̈", col.compare("ΰ", "ύ̈") < 0); - test.ok("ύ̈ < Φ", col.compare("ύ̈", "Φ") < 0); - test.ok("Φ < φ", col.compare("Φ", "φ") < 0); - test.ok("φ < Χ", col.compare("φ", "Χ") < 0); - test.ok("Χ < χ", col.compare("Χ", "χ") < 0); - test.ok("χ < Ψ", col.compare("χ", "Ψ") < 0); - test.ok("Ψ < ψ", col.compare("Ψ", "ψ") < 0); - test.ok("ψ < Ω", col.compare("ψ", "Ω") < 0); - test.ok("Ω < ω", col.compare("Ω", "ω") < 0); - test.ok("ω < Ώ", col.compare("ω", "Ώ") < 0); - test.ok("Ώ < Ώ", col.compare("Ώ", "Ώ") < 0); - test.ok("Ώ < ώ", col.compare("Ώ", "ώ") < 0); - test.ok("ώ < ώ", col.compare("ώ", "ώ") < 0); - test.done(); - }); - - }, - - testCollatorAsyncPriL_ko: function(test) { - test.expect(5); - var col = Collator.create({ - locale: "ko-KR", - useNative: false, - sensitivity: "primary", - sync: false - }).then(function(col) { - test.ok(typeof(col) !== "undefined"); - - // 가까나다따 - test.ok("가 < 까", col.compare("가", "까") < 0); - test.ok("까 < 나", col.compare("까", "나") < 0); - test.ok("나 < 다", col.compare("나", "다") < 0); - test.ok("다 < 따", col.compare("다", "따") < 0); - test.done(); - }); - - }, - - testCollatorAsyncQuatHanzi_zh_Hans: function(test) { - test.expect(21); - var col = Collator.create({ - locale: "zh-Hans-CN", - useNative: false, - sensitivity: "quaternary", - usage: "search", - sync: false - }).then(function(col) { - // Hanzi are all primary differences from each other - - test.ok(typeof(col) !== "undefined"); - - test.ok("阿 < 拜", col.compare("阿", "拜") < 0); - test.ok("拜 < 𩑻", col.compare("拜", "𩑻") < 0); - test.ok("𩑻 < 䯋", col.compare("𩑻", "䯋") < 0); - test.ok("䯋 < 𧀱", col.compare("䯋", "𧀱") < 0); - test.ok("𧀱 < 捶", col.compare("𧀱", "捶") < 0); - test.ok("捶 < 峒", col.compare("捶", "峒") < 0); - test.ok("峒 < 㶥", col.compare("峒", "㶥") < 0); - test.ok("㶥 < 㜳", col.compare("㶥", "㜳") < 0); - test.ok("㜳 < 䌸", col.compare("㜳", "䌸") < 0); - test.ok("䌸 < 䢧", col.compare("䌸", "䢧") < 0); - test.ok("䢧 < 苜", col.compare("䢧", "苜") < 0); - test.ok("苜 < 肶", col.compare("苜", "肶") < 0); - test.ok("肶 < 埁", col.compare("肶", "埁") < 0); - test.ok("埁 < 泩", col.compare("埁", "泩") < 0); - test.ok("泩 < 窱", col.compare("泩", "窱") < 0); - test.ok("窱 < 扤", col.compare("窱", "扤") < 0); - test.ok("扤 < 辥", col.compare("扤", "辥") < 0); - test.ok("辥 < 䓰", col.compare("辥", "䓰") < 0); - test.ok("䓰 < 赵", col.compare("䓰", "赵") < 0); - test.ok("赵 < 蓙", col.compare("赵", "蓙") < 0); - test.done(); - }); - - }, - - testCollatorAsyncTraditionalQuatCH_es: function(test) { - test.expect(6); - var col = Collator.create({ - locale: "es-ES", - useNative: false, - sensitivity: "quaternary", - style: "traditional", - sync: false - }).then(function(col) { - test.ok(typeof(col) !== "undefined"); - - // a b c ch d - test.ok("a < b", col.compare("a", "b") < 0); - test.ok("b < c", col.compare("b", "c") < 0); - test.ok("ca < ch", col.compare("ca", "ch") < 0); - test.ok("cz < ch", col.compare("cz", "ch") < 0); - test.ok("ch < d", col.compare("ch", "d") < 0); - test.done(); - }); - } -}; \ No newline at end of file diff --git a/test/testcountryasync.js b/test/testcountryasync.js deleted file mode 100644 index 86b7514..0000000 --- a/test/testcountryasync.js +++ /dev/null @@ -1,100 +0,0 @@ -/* - * testcountryasync.js - test the country routines - * - * Copyright © 2018, LGE - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import Country from "../src/Country.js"; - -export const testcountryasync = { - testCountryAsyncConstructorEmpty: function(test) { - test.expect(1); - new Country({ - sync: false, - onLoad: function(ctry) { - test.ok(ctry !== null); - test.done(); - } - }); - }, - - testCountryAsyncLocale1: function(test) { - test.expect(4); - new Country({ - locale: "ko-KR", - sync: false, - onLoad: function(ctry) { - test.ok(ctry !== null); - - test.equal(ctry.getName("CD"), "콩고-킨샤사"); - test.equal(ctry.getCode("콩고-킨샤사"), "CD"); - var locale = ctry.getLocale(); - test.equal(locale.toString(), "ko-KR"); - test.done(); - } - }); - }, - - testCountryAsyncLocale2: function(test) { - test.expect(4); - new Country({ - locale: "en-US", - sync: false, - onLoad: function(ctry) { - test.ok(ctry !== null); - - test.equal(ctry.getName("CD"), "Congo - Kinshasa"); - test.equal(ctry.getCode("Congo - Kinshasa"), "CD"); - var locale = ctry.getLocale(); - test.equal(locale.toString(), "en-US"); - test.done(); - } - }); - }, - - testCountryAsyncGetByCodeUnknown: function(test) { - new Country({ - locale: "en-US", - sync: false, - onLoad: function(ctry) { - try { - ctry.getName('xxx'); - test.fail(); - } catch (e) { - test.equal(e, "Country xxx is unknown"); - } - test.done(); - } - }); - }, - - testCountryAsyncJP: function(test) { - test.expect(4); - new Country({ - locale: "ja-JP", - sync: false, - onLoad: function (ctry) { - test.ok(ctry !== null); - - test.equal(ctry.getName("JP"), "日本"); - test.equal(ctry.getCode("日本"), "JP"); - var locale = ctry.getLocale(); - test.equal(locale.toString(), "ja-JP"); - test.done(); - } - }); - } -}; diff --git a/test/testcountrypromise.js b/test/testcountrypromise.js deleted file mode 100644 index 4f8f970..0000000 --- a/test/testcountrypromise.js +++ /dev/null @@ -1,95 +0,0 @@ -/* - * testcountryasync.js - test the country routines - * - * Copyright © 2018, LGE - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import Country from "../src/Country.js"; - -export const testcountrypromise = { - testCountryAsyncConstructorEmpty: function(test) { - test.expect(1); - Country.create({ - sync: false - }).then(function(ctry) { - test.ok(ctry !== null); - test.done(); - }); - }, - - testCountryAsyncLocale1: function(test) { - test.expect(4); - Country.create({ - locale: "ko-KR", - sync: false - }).then(function(ctry) { - test.ok(ctry !== null); - - test.equal(ctry.getName("CD"), "콩고-킨샤사"); - test.equal(ctry.getCode("콩고-킨샤사"), "CD"); - var locale = ctry.getLocale(); - test.equal(locale.toString(), "ko-KR"); - test.done(); - }); - }, - - testCountryAsyncLocale2: function(test) { - test.expect(4); - Country.create({ - locale: "en-US", - sync: false - }).then(function(ctry) { - test.ok(ctry !== null); - - test.equal(ctry.getName("CD"), "Congo - Kinshasa"); - test.equal(ctry.getCode("Congo - Kinshasa"), "CD"); - var locale = ctry.getLocale(); - test.equal(locale.toString(), "en-US"); - test.done(); - }); - }, - - testCountryAsyncGetByCodeUnknown: function(test) { - Country.create({ - locale: "en-US", - sync: false - }).then(function(ctry) { - try { - ctry.getName('xxx'); - test.fail(); - } catch (e) { - test.equal(e, "Country xxx is unknown"); - } - test.done(); - }); - }, - - testCountryAsyncJP: function(test) { - test.expect(4); - Country.create({ - locale: "ja-JP", - sync: false - }).then(function (ctry) { - test.ok(ctry !== null); - - test.equal(ctry.getName("JP"), "日本"); - test.equal(ctry.getCode("日本"), "JP"); - var locale = ctry.getLocale(); - test.equal(locale.toString(), "ja-JP"); - test.done(); - }); - } -}; diff --git a/test/testctypeasync.js b/test/testctypeasync.js deleted file mode 100644 index e2066fa..0000000 --- a/test/testctypeasync.js +++ /dev/null @@ -1,345 +0,0 @@ -/* - * testctypeasync.js - test the character type information functions - * - * Copyright © 2018, 2022 JEDLSoft - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import isXdigit from "../src/isXdigit.js"; -import isUpper from "../src/isUpper.js"; -import isSpace from "../src/isSpace.js"; -import isScript from "../src/isScript.js"; -import isPunct from "../src/isPunct.js"; -import isPrint from "../src/isPrint.js"; -import isLower from "../src/isLower.js"; -import isIdeo from "../src/isIdeo.js"; -import isGraph from "../src/isGraph.js"; -import isDigit from "../src/isDigit.js"; -import isCntrl from "../src/isCntrl.js"; -import isBlank from "../src/isBlank.js"; -import isAscii from "../src/isAscii.js"; -import isAlpha from "../src/isAlpha.js"; -import isAlnum from "../src/isAlnum.js"; -import CType from "../src/CType.js"; - -export const testctypeasync = { - testIsAlphaTrue: function(test) { - test.expect(5); - isAlpha._init(false, undefined, function() { - test.ok(isAlpha('a')); - test.ok(isAlpha('m')); - test.ok(isAlpha('z')); - test.ok(isAlpha('A')); - test.ok(isAlpha('Q')); - test.done(); - }); - }, - - testIsLowerTrue: function(test) { - test.expect(11); - isLower._init(false, undefined, function() { - test.ok(isLower('a')); - test.ok(isLower('m')); - test.ok(isLower('щ')); - test.ok(isLower('λ')); - test.ok(!isLower(' ')); - test.ok(!isLower('$')); - test.ok(!isLower('A')); - test.ok(!isLower('M')); - test.ok(!isLower('0')); - test.ok(!isLower('Щ')); - test.ok(!isLower('Λ')); - test.done(); - }); - }, - - testIsUpperTrue: function(test) { - test.expect(11); - isUpper._init(false, undefined, function() { - test.ok(isUpper('A')); - test.ok(isUpper('M')); - test.ok(isUpper('Щ')); - test.ok(isUpper('Λ')); - test.ok(!isUpper(' ')); - test.ok(!isUpper('$')); - test.ok(!isUpper('a')); - test.ok(!isUpper('m')); - test.ok(!isUpper('щ')); - test.ok(!isUpper('λ')); - test.ok(!isUpper('0')); - test.done(); - }); - }, - - testIsPrintTrue: function(test) { - test.expect(7); - isPrint._init(false, undefined, function() { - test.ok(isPrint(' ')); - test.ok(isPrint('A')); - test.ok(isPrint('M')); - test.ok(isPrint('Щ')); - test.ok(isPrint('Λ')); - test.ok(!isPrint('\u0001')); - test.ok(!isPrint('\u0085')); - test.done(); - }); - }, - - testIsAsciiTrue: function(test) { - test.expect(13); - isAscii._init(false, undefined, function() { - test.ok(isAscii('a')); - test.ok(isAscii('m')); - test.ok(isAscii('z')); - test.ok(isAscii('A')); - test.ok(isAscii('Q')); - test.ok(isAscii(' ')); - test.ok(isAscii('$')); - test.ok(isAscii('0')); - test.ok(isAscii('1')); - test.ok(isAscii('8')); - test.ok(!isAscii('ü')); - test.ok(!isAscii('ó')); - test.ok(!isAscii('Д')); - test.done(); - }); - }, - - testIsBlankTrue: function(test) { - test.expect(13); - isBlank._init(false, undefined, function() { - test.ok(isBlank(' ')); - test.ok(!isBlank('a')); - test.ok(!isBlank('m')); - test.ok(!isBlank('z')); - test.ok(!isBlank('A')); - test.ok(!isBlank('Q')); - test.ok(!isBlank('$')); - test.ok(!isBlank('0')); - test.ok(!isBlank('1')); - test.ok(!isBlank('8')); - test.ok(!isBlank('ü')); - test.ok(!isBlank('ó')); - test.ok(!isBlank('Д')); - test.done(); - }); - }, - - testIsSpaceTrue: function(test) { - test.expect(11); - isSpace._init(false, undefined, function() { - test.ok(isSpace(' ')); - test.ok(isSpace('\t')); - test.ok(isSpace('\n')); - test.ok(isSpace('\u2000')); - test.ok(!isSpace('a')); - test.ok(!isSpace('A')); - test.ok(!isSpace('$')); - test.ok(!isSpace('0')); - test.ok(!isSpace('ü')); - test.ok(!isSpace('ó')); - test.ok(!isSpace('Д')); - test.done(); - }); - }, - - testIsPunctTrue: function(test) { - test.expect(17); - isPunct._init(false, undefined, function() { - test.ok(isPunct('?')); - test.ok(isPunct('.')); - test.ok(isPunct('\u2010')); // hyphen - test.ok(isPunct('\u037E')); // Greek question mark - test.ok(isPunct('\u3001')); // ideographic comma - test.ok(!isPunct('a')); - test.ok(!isPunct('m')); - test.ok(!isPunct('z')); - test.ok(!isPunct('A')); - test.ok(!isPunct('Q')); - test.ok(!isPunct(' ')); - test.ok(!isPunct('0')); - test.ok(!isPunct('1')); - test.ok(!isPunct('8')); - test.ok(!isPunct('ü')); - test.ok(!isPunct('ó')); - test.ok(!isPunct('Д')); - test.done(); - }); - }, - - testIsIdeoTrue: function(test) { - test.expect(10); - isIdeo._init(false, undefined, function() { - test.ok(isIdeo('碗')); - test.ok(isIdeo('人')); - test.ok(!isIdeo(' ')); - test.ok(!isIdeo('$')); - test.ok(!isIdeo('a')); - test.ok(!isIdeo('m')); - test.ok(!isIdeo('z')); - test.ok(!isIdeo('0')); - test.ok(!isIdeo('1')); - test.ok(!isIdeo('8')); - test.done(); - }); - }, - - testIsCntrlTrue: function(test) { - test.expect(10); - isCntrl._init(false, undefined, function() { - test.ok(isCntrl('\u0001')); - test.ok(isCntrl('\u0085')); - test.ok(!isCntrl(' ')); - test.ok(!isCntrl('$')); - test.ok(!isCntrl('a')); - test.ok(!isCntrl('m')); - test.ok(!isCntrl('z')); - test.ok(!isCntrl('0')); - test.ok(!isCntrl('1')); - test.ok(!isCntrl('8')); - test.done(); - }); - }, - - testIsDigitTrue: function(test) { - test.expect(20); - isDigit._init(false, undefined, function() { - test.ok(isDigit('0')); - test.ok(isDigit('1')); - test.ok(isDigit('2')); - test.ok(isDigit('3')); - test.ok(isDigit('4')); - test.ok(isDigit('5')); - test.ok(isDigit('6')); - test.ok(isDigit('7')); - test.ok(isDigit('8')); - test.ok(isDigit('9')); - test.ok(!isDigit(' ')); - test.ok(!isDigit('a')); - test.ok(!isDigit('m')); - test.ok(!isDigit('z')); - test.ok(!isDigit('A')); - test.ok(!isDigit('Q')); - test.ok(!isDigit('$')); - test.ok(!isDigit('ü')); - test.ok(!isDigit('ó')); - test.ok(!isDigit('Д')); - test.done(); - }); - }, - testIsXdigitTrue: function(test) { - test.expect(32); - isXdigit._init(false, undefined, function() { - test.ok(isXdigit('0')); - test.ok(isXdigit('1')); - test.ok(isXdigit('2')); - test.ok(isXdigit('3')); - test.ok(isXdigit('4')); - test.ok(isXdigit('5')); - test.ok(isXdigit('6')); - test.ok(isXdigit('7')); - test.ok(isXdigit('8')); - test.ok(isXdigit('9')); - test.ok(isXdigit('A')); - test.ok(isXdigit('B')); - test.ok(isXdigit('C')); - test.ok(isXdigit('D')); - test.ok(isXdigit('E')); - test.ok(isXdigit('F')); - test.ok(isXdigit('a')); - test.ok(isXdigit('b')); - test.ok(isXdigit('c')); - test.ok(isXdigit('d')); - test.ok(isXdigit('e')); - test.ok(isXdigit('f')); - test.ok(!isXdigit('G')); - test.ok(!isXdigit('g')); - test.ok(!isXdigit(' ')); - test.ok(!isXdigit('m')); - test.ok(!isXdigit('z')); - test.ok(!isXdigit('Q')); - test.ok(!isXdigit('$')); - test.ok(!isXdigit('ü')); - test.ok(!isXdigit('ó')); - test.ok(!isXdigit('Д')); - test.done(); - }); - }, - - testWithinRangeTrue: function(test) { - test.expect(5); - CType._init(false, undefined, function() { - test.ok(CType.withinRange('a', 'ascii')); - test.ok(!CType.withinRange('\u2000a', 'ascii')); - test.ok(CType.withinRange('a', 'ASCII')); - test.ok(!CType.withinRange('G', 'arabic')); - test.ok(CType.withinRange('a', 'latin')); - test.done(); - }); - }, - - testIsScriptTrue: function(test) { - test.expect(16); - isScript._init(false, undefined, function() { - test.ok("testing Latn", isScript("a", "Latn")); - test.ok("testing Cyrl", isScript("Д", "Cyrl")); - test.ok("testing Grek", isScript("ώ", "Grek")); - test.ok("testing Hang", isScript("귋", "Hang")); - test.ok("testing Hani", isScript("㜴", "Hani")); - test.ok("testing Hebr", isScript("ש", "Hebr")); - test.ok("testing Arab", isScript("ش", "Arab")); - test.ok("testing Deva", isScript("झ", "Deva")); - test.ok(!isScript("a", "Cyrl")); - test.ok(!isScript("Д", "Grek")); - test.ok(!isScript("ώ", "Hang")); - test.ok(!isScript("귋", "Hani")); - test.ok(!isScript("㜴", "Hebr")); - test.ok(!isScript("ש", "Arab")); - test.ok(!isScript("ش", "Deva")); - test.ok(!isScript("झ", "Latn")); - test.done(); - }); - }, - - testIsAlnum: function(test) { - test.expect(11); - isAlnum._init(false, undefined, function() { - test.ok(isAlnum('a')); - test.ok(isAlnum('m')); - test.ok(isAlnum('z')); - test.ok(isAlnum('A')); - test.ok(isAlnum('Q')); - test.ok(isAlnum('0')); - test.ok(isAlnum('1')); - test.ok(isAlnum('8')); - test.ok(isAlnum('Ꞛ')); - test.ok(!isAlnum(' ')); - test.ok(!isAlnum('$')); - test.done(); - }); - }, - - testIsGraphTrue: function(test) { - test.expect(5); - isGraph._init(false, undefined, function() { - test.ok(isGraph('A')); - test.ok(isGraph('Q')); - test.ok(isGraph('碗')); - test.ok(!isGraph(' ')); - test.ok(!isGraph('\u0002')); - test.done(); - }); - } -}; diff --git a/test/testctypepromise.js b/test/testctypepromise.js deleted file mode 100644 index 0f378fb..0000000 --- a/test/testctypepromise.js +++ /dev/null @@ -1,345 +0,0 @@ -/* - * testctypepromise.js - test the character type information functions - * - * Copyright © 2018, 2022 JEDLSoft - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import isXdigit from "../src/isXdigit.js"; -import isUpper from "../src/isUpper.js"; -import isSpace from "../src/isSpace.js"; -import isScript from "../src/isScript.js"; -import isPunct from "../src/isPunct.js"; -import isPrint from "../src/isPrint.js"; -import isLower from "../src/isLower.js"; -import isIdeo from "../src/isIdeo.js"; -import isGraph from "../src/isGraph.js"; -import isDigit from "../src/isDigit.js"; -import isCntrl from "../src/isCntrl.js"; -import isBlank from "../src/isBlank.js"; -import isAscii from "../src/isAscii.js"; -import isAlpha from "../src/isAlpha.js"; -import isAlnum from "../src/isAlnum.js"; -import CType from "../src/CType.js"; - -export const testctypepromise = { - testIsAlphaTrue: function(test) { - test.expect(5); - isAlpha._init(false).then(function() { - test.ok(isAlpha('a')); - test.ok(isAlpha('m')); - test.ok(isAlpha('z')); - test.ok(isAlpha('A')); - test.ok(isAlpha('Q')); - test.done(); - }); - }, - - testIsLowerTrue: function(test) { - test.expect(11); - isLower._init(false).then(function() { - test.ok(isLower('a')); - test.ok(isLower('m')); - test.ok(isLower('щ')); - test.ok(isLower('λ')); - test.ok(!isLower(' ')); - test.ok(!isLower('$')); - test.ok(!isLower('A')); - test.ok(!isLower('M')); - test.ok(!isLower('0')); - test.ok(!isLower('Щ')); - test.ok(!isLower('Λ')); - test.done(); - }); - }, - - testIsUpperTrue: function(test) { - test.expect(11); - isUpper._init(false).then(function() { - test.ok(isUpper('A')); - test.ok(isUpper('M')); - test.ok(isUpper('Щ')); - test.ok(isUpper('Λ')); - test.ok(!isUpper(' ')); - test.ok(!isUpper('$')); - test.ok(!isUpper('a')); - test.ok(!isUpper('m')); - test.ok(!isUpper('щ')); - test.ok(!isUpper('λ')); - test.ok(!isUpper('0')); - test.done(); - }); - }, - - testIsPrintTrue: function(test) { - test.expect(7); - isPrint._init(false).then(function() { - test.ok(isPrint(' ')); - test.ok(isPrint('A')); - test.ok(isPrint('M')); - test.ok(isPrint('Щ')); - test.ok(isPrint('Λ')); - test.ok(!isPrint('\u0001')); - test.ok(!isPrint('\u0085')); - test.done(); - }); - }, - - testIsAsciiTrue: function(test) { - test.expect(13); - isAscii._init(false).then(function() { - test.ok(isAscii('a')); - test.ok(isAscii('m')); - test.ok(isAscii('z')); - test.ok(isAscii('A')); - test.ok(isAscii('Q')); - test.ok(isAscii(' ')); - test.ok(isAscii('$')); - test.ok(isAscii('0')); - test.ok(isAscii('1')); - test.ok(isAscii('8')); - test.ok(!isAscii('ü')); - test.ok(!isAscii('ó')); - test.ok(!isAscii('Д')); - test.done(); - }); - }, - - testIsBlankTrue: function(test) { - test.expect(13); - isBlank._init(false).then(function() { - test.ok(isBlank(' ')); - test.ok(!isBlank('a')); - test.ok(!isBlank('m')); - test.ok(!isBlank('z')); - test.ok(!isBlank('A')); - test.ok(!isBlank('Q')); - test.ok(!isBlank('$')); - test.ok(!isBlank('0')); - test.ok(!isBlank('1')); - test.ok(!isBlank('8')); - test.ok(!isBlank('ü')); - test.ok(!isBlank('ó')); - test.ok(!isBlank('Д')); - test.done(); - }); - }, - - testIsSpaceTrue: function(test) { - test.expect(11); - isSpace._init(false).then(function() { - test.ok(isSpace(' ')); - test.ok(isSpace('\t')); - test.ok(isSpace('\n')); - test.ok(isSpace('\u2000')); - test.ok(!isSpace('a')); - test.ok(!isSpace('A')); - test.ok(!isSpace('$')); - test.ok(!isSpace('0')); - test.ok(!isSpace('ü')); - test.ok(!isSpace('ó')); - test.ok(!isSpace('Д')); - test.done(); - }); - }, - - testIsPunctTrue: function(test) { - test.expect(17); - isPunct._init(false).then(function() { - test.ok(isPunct('?')); - test.ok(isPunct('.')); - test.ok(isPunct('\u2010')); // hyphen - test.ok(isPunct('\u037E')); // Greek question mark - test.ok(isPunct('\u3001')); // ideographic comma - test.ok(!isPunct('a')); - test.ok(!isPunct('m')); - test.ok(!isPunct('z')); - test.ok(!isPunct('A')); - test.ok(!isPunct('Q')); - test.ok(!isPunct(' ')); - test.ok(!isPunct('0')); - test.ok(!isPunct('1')); - test.ok(!isPunct('8')); - test.ok(!isPunct('ü')); - test.ok(!isPunct('ó')); - test.ok(!isPunct('Д')); - test.done(); - }); - }, - - testIsIdeoTrue: function(test) { - test.expect(10); - isIdeo._init(false).then(function() { - test.ok(isIdeo('碗')); - test.ok(isIdeo('人')); - test.ok(!isIdeo(' ')); - test.ok(!isIdeo('$')); - test.ok(!isIdeo('a')); - test.ok(!isIdeo('m')); - test.ok(!isIdeo('z')); - test.ok(!isIdeo('0')); - test.ok(!isIdeo('1')); - test.ok(!isIdeo('8')); - test.done(); - }); - }, - - testIsCntrlTrue: function(test) { - test.expect(10); - isCntrl._init(false).then(function() { - test.ok(isCntrl('\u0001')); - test.ok(isCntrl('\u0085')); - test.ok(!isCntrl(' ')); - test.ok(!isCntrl('$')); - test.ok(!isCntrl('a')); - test.ok(!isCntrl('m')); - test.ok(!isCntrl('z')); - test.ok(!isCntrl('0')); - test.ok(!isCntrl('1')); - test.ok(!isCntrl('8')); - test.done(); - }); - }, - - testIsDigitTrue: function(test) { - test.expect(20); - isDigit._init(false).then(function() { - test.ok(isDigit('0')); - test.ok(isDigit('1')); - test.ok(isDigit('2')); - test.ok(isDigit('3')); - test.ok(isDigit('4')); - test.ok(isDigit('5')); - test.ok(isDigit('6')); - test.ok(isDigit('7')); - test.ok(isDigit('8')); - test.ok(isDigit('9')); - test.ok(!isDigit(' ')); - test.ok(!isDigit('a')); - test.ok(!isDigit('m')); - test.ok(!isDigit('z')); - test.ok(!isDigit('A')); - test.ok(!isDigit('Q')); - test.ok(!isDigit('$')); - test.ok(!isDigit('ü')); - test.ok(!isDigit('ó')); - test.ok(!isDigit('Д')); - test.done(); - }); - }, - testIsXdigitTrue: function(test) { - test.expect(32); - isXdigit._init(false).then(function() { - test.ok(isXdigit('0')); - test.ok(isXdigit('1')); - test.ok(isXdigit('2')); - test.ok(isXdigit('3')); - test.ok(isXdigit('4')); - test.ok(isXdigit('5')); - test.ok(isXdigit('6')); - test.ok(isXdigit('7')); - test.ok(isXdigit('8')); - test.ok(isXdigit('9')); - test.ok(isXdigit('A')); - test.ok(isXdigit('B')); - test.ok(isXdigit('C')); - test.ok(isXdigit('D')); - test.ok(isXdigit('E')); - test.ok(isXdigit('F')); - test.ok(isXdigit('a')); - test.ok(isXdigit('b')); - test.ok(isXdigit('c')); - test.ok(isXdigit('d')); - test.ok(isXdigit('e')); - test.ok(isXdigit('f')); - test.ok(!isXdigit('G')); - test.ok(!isXdigit('g')); - test.ok(!isXdigit(' ')); - test.ok(!isXdigit('m')); - test.ok(!isXdigit('z')); - test.ok(!isXdigit('Q')); - test.ok(!isXdigit('$')); - test.ok(!isXdigit('ü')); - test.ok(!isXdigit('ó')); - test.ok(!isXdigit('Д')); - test.done(); - }); - }, - - testWithinRangeTrue: function(test) { - test.expect(5); - CType._init(false).then(function() { - test.ok(CType.withinRange('a', 'ascii')); - test.ok(!CType.withinRange('\u2000a', 'ascii')); - test.ok(CType.withinRange('a', 'ASCII')); - test.ok(!CType.withinRange('G', 'arabic')); - test.ok(CType.withinRange('a', 'latin')); - test.done(); - }); - }, - - testIsScriptTrue: function(test) { - test.expect(16); - isScript._init(false).then(function() { - test.ok("testing Latn", isScript("a", "Latn")); - test.ok("testing Cyrl", isScript("Д", "Cyrl")); - test.ok("testing Grek", isScript("ώ", "Grek")); - test.ok("testing Hang", isScript("귋", "Hang")); - test.ok("testing Hani", isScript("㜴", "Hani")); - test.ok("testing Hebr", isScript("ש", "Hebr")); - test.ok("testing Arab", isScript("ش", "Arab")); - test.ok("testing Deva", isScript("झ", "Deva")); - test.ok(!isScript("a", "Cyrl")); - test.ok(!isScript("Д", "Grek")); - test.ok(!isScript("ώ", "Hang")); - test.ok(!isScript("귋", "Hani")); - test.ok(!isScript("㜴", "Hebr")); - test.ok(!isScript("ש", "Arab")); - test.ok(!isScript("ش", "Deva")); - test.ok(!isScript("झ", "Latn")); - test.done(); - }); - }, - - testIsAlnum: function(test) { - test.expect(11); - isAlnum._init(false).then(function() { - test.ok(isAlnum('a')); - test.ok(isAlnum('m')); - test.ok(isAlnum('z')); - test.ok(isAlnum('A')); - test.ok(isAlnum('Q')); - test.ok(isAlnum('0')); - test.ok(isAlnum('1')); - test.ok(isAlnum('8')); - test.ok(isAlnum('Ꞛ')); - test.ok(!isAlnum(' ')); - test.ok(!isAlnum('$')); - test.done(); - }); - }, - - testIsGraphTrue: function(test) { - test.expect(5); - isGraph._init(false).then(function() { - test.ok(isGraph('A')); - test.ok(isGraph('Q')); - test.ok(isGraph('碗')); - test.ok(!isGraph(' ')); - test.ok(!isGraph('\u0002')); - test.done(); - }); - } -}; diff --git a/test/testcurrencyasync.js b/test/testcurrencyasync.js deleted file mode 100644 index 2d54831..0000000 --- a/test/testcurrencyasync.js +++ /dev/null @@ -1,110 +0,0 @@ -/* - * testcurrencyasync.js - test the currency routines - * - * Copyright © 2018, 2022 JEDLSoft - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import Currency from "../src/Currency.js"; - -export const testcurrencyasync = { - testCurrencyAsyncDefaults: function(test) { - test.expect(7); - new Currency({ - sync: false, - onLoad: function(cur) { - test.ok(cur !== null); - - test.equal(cur.getCode(), "USD"); - test.equal(cur.getFractionDigits(), 2); - test.equal(cur.getSign(), "$"); - test.equal(cur.getName(), "US Dollar"); - var locale = cur.getLocale(); - test.ok(typeof(locale) !== "undefined"); - test.equal(locale.toString(), "en-US"); - test.done(); - } - }); - }, - - testCurrencyAsyncGetByCode1: function(test) { - test.expect(6); - new Currency({ - code: "EUR", - sync: false, - onLoad: function(cur) { - test.ok(cur !== null); - - test.equal(cur.getCode(), "EUR"); - test.equal(cur.getFractionDigits(), 2); - test.equal(cur.getSign(), "€"); - test.equal(cur.getName(), "Euro"); - var locale = cur.getLocale(); - test.equal(locale.toString(), "en-US"); - test.done(); - } - }); - }, - - testCurrencyAsyncGetByCodeUnknown: function(test) { - new Currency({ - code: "xxx", - sync: false, - onLoad: function(cur) { - test.ok(!cur); - test.done(); - } - }); - }, - - testCurrencyAsyncGetBySignUnambiguous: function(test) { - test.expect(6); - new Currency({ - sign: "€", - sync: false, - onLoad: function(cur) { - test.ok(cur !== null); - - test.equal(cur.getCode(), "EUR"); - test.equal(cur.getFractionDigits(), 2); - test.equal(cur.getSign(), "€"); - test.equal(cur.getName(), "Euro"); - var locale = cur.getLocale(); - test.equal(locale.toString(), "en-US"); - test.done(); - } - }); - }, - - testCurrencyAsync: function(test) { - test.expect(6); - new Currency({ - locale: "en-GB", - sign: "$", - sync: false, - onLoad: function (cur) { - test.ok(cur !== null); - - test.equal(cur.getCode(), "USD"); - test.equal(cur.getFractionDigits(), 2); - test.equal(cur.getSign(), "$"); - test.equal(cur.getName(), "US Dollar"); - var locale = cur.getLocale(); - test.equal(locale.toString(), "en-GB"); - test.done(); - } - }); - } -}; diff --git a/test/testcurrencypromise.js b/test/testcurrencypromise.js deleted file mode 100644 index faf2c53..0000000 --- a/test/testcurrencypromise.js +++ /dev/null @@ -1,105 +0,0 @@ -/* - * testcurrencyasync.js - test the currency routines - * - * Copyright © 2018, 2022 JEDLSoft - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import Currency from "../src/Currency.js"; - -export const testcurrencypromise = { - testCurrencyAsyncDefaults: function(test) { - test.expect(7); - Currency.create({ - sync: false - }).then(function(cur) { - test.ok(cur !== null); - - test.equal(cur.getCode(), "USD"); - test.equal(cur.getFractionDigits(), 2); - test.equal(cur.getSign(), "$"); - test.equal(cur.getName(), "US Dollar"); - var locale = cur.getLocale(); - test.ok(typeof(locale) !== "undefined"); - test.equal(locale.toString(), "en-US"); - test.done(); - }); - }, - - testCurrencyAsyncGetByCode1: function(test) { - test.expect(6); - Currency.create({ - code: "EUR", - sync: false - }).then(function(cur) { - test.ok(cur !== null); - - test.equal(cur.getCode(), "EUR"); - test.equal(cur.getFractionDigits(), 2); - test.equal(cur.getSign(), "€"); - test.equal(cur.getName(), "Euro"); - var locale = cur.getLocale(); - test.equal(locale.toString(), "en-US"); - test.done(); - }); - }, - - testCurrencyAsyncGetByCodeUnknown: function(test) { - Currency.create({ - code: "xxx", - sync: false - }).then(function(cur) { - test.ok(!cur); - test.done(); - }); - }, - - testCurrencyAsyncGetBySignUnambiguous: function(test) { - test.expect(6); - Currency.create({ - sign: "€", - sync: false - }).then(function(cur) { - test.ok(cur !== null); - - test.equal(cur.getCode(), "EUR"); - test.equal(cur.getFractionDigits(), 2); - test.equal(cur.getSign(), "€"); - test.equal(cur.getName(), "Euro"); - var locale = cur.getLocale(); - test.equal(locale.toString(), "en-US"); - test.done(); - }); - }, - - testCurrencyAsync: function(test) { - test.expect(6); - Currency.create({ - locale: "en-GB", - sign: "$", - sync: false - }).then(function (cur) { - test.ok(cur !== null); - - test.equal(cur.getCode(), "USD"); - test.equal(cur.getFractionDigits(), 2); - test.equal(cur.getSign(), "$"); - test.equal(cur.getName(), "US Dollar"); - var locale = cur.getLocale(); - test.equal(locale.toString(), "en-GB"); - test.done(); - }); - } -}; diff --git a/test/testdatefmtasync.js b/test/testdatefmtasync.js deleted file mode 100644 index d8deef8..0000000 --- a/test/testdatefmtasync.js +++ /dev/null @@ -1,290 +0,0 @@ -/* - * testdatefmtasync.js - test the date formatter object asynchronously - * - * Copyright © 2018, 2022 JEDLSoft - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import ilib from "../src/ilib.js"; -import ThaiSolarDate from "../src/ThaiSolarDate.js"; -import PersianDate from "../src/PersianDate.js"; -import IslamicDate from "../src/IslamicDate.js"; -import HebrewDate from "../src/HebrewDate.js"; -import GregorianDate from "../src/GregorianDate.js"; -import JulianDate from "../src/JulianDate.js"; -import DateFmt from "../src/DateFmt.js"; -import DateFactory from "../src/DateFactory.js"; - -export const testdatefmtasync = { - testDateFmtConstructorEmpty: function(test) { - test.expect(1); - new DateFmt({ - sync: false, - onLoad: function(fmt) { - test.ok(fmt !== null); - test.done(); - } - }); - }, - - testDateFmtGetCalendarExplicit: function(test) { - test.expect(3); - new DateFmt({ - calendar: "julian", - sync: false, - onLoad: function(fmt) { - test.ok(fmt); - var cal = fmt.getCalendar(); - test.ok(cal); - - test.equal(cal, "julian"); - test.done(); - } - }); - }, - - testDateFmtGetCalendarNotInThisLocale: function(test) { - new DateFmt({ - calendar: "arabic", - locale: 'en-US', - sync: false, - onLoad: function(fmt) { - // "No formats available for calendar arabic in locale en-US" - test.ok(!fmt); - test.done(); - } - }); - }, - - testDateFmtGetTimeZoneDefault: function(test) { - test.expect(2); - ilib.tz = undefined; // just in case - if (ilib._getPlatform() === "nodejs") { - process.env.TZ = ""; - } - - new DateFmt({ - sync: false, - onLoad: function(fmt) { - test.ok(fmt !== null); - - test.equal(fmt.getTimeZone().getId(), "local"); - test.done(); - } - }); - }, - - testDateFmtGetTimeZone: function(test) { - test.expect(2); - new DateFmt({ - timezone: "Europe/Paris", - sync: false, - onLoad: function(fmt) { - test.ok(fmt !== null); - - test.equal(fmt.getTimeZone().getId(), "Europe/Paris"); - test.done(); - } - }); - }, - - testDateFmtUseTemplateNonEmptyCalendar: function(test) { - test.expect(2); - new DateFmt({ - calendar: 'julian', - template: "EEE 'the' DD 'of' MM, yyyy G", - sync: false, - onLoad: function(fmt) { - test.ok(fmt !== null); - - test.equal(fmt.getCalendar(), "julian"); - test.done(); - } - }); - }, - - testDateFmtUseTemplateNonEmptyLocale: function(test) { - test.expect(2); - var fmt = new DateFmt({ - locale: 'de-DE', - template: "EEE 'the' DD 'of' MM, yyyy G", - sync: false, - onLoad: function(fmt) { - test.ok(fmt !== null); - - test.equal(fmt.getLocale().toString(), "de-DE"); - test.done(); - } - }); - }, - - testDateFmtFormatJSDate1: function(test) { - test.expect(2); - new DateFmt({ - type: "time", - length: "short", - timezone: "America/Los_Angeles", - sync: false, - onLoad: function(fmt) { - test.ok(fmt !== null); - - // test formatting a javascript date. It should be converted to - // an ilib date object automatically and then formatted - var datMyBday = new Date("Fri Aug 13 1982 13:37:35 GMT-0700"); - test.equal(fmt.format(datMyBday), "1:37 PM"); - test.done(); - } - }); - }, - - testDateFmtFormatJSDateRightTimeZone1: function(test) { - test.expect(2); - var fmt = new DateFmt({ - type: "date", - length: "full", - date: "w", - timezone: "America/Los_Angeles", - sync: false, - onLoad: function(fmt) { - test.ok(fmt !== null); - - // test formatting a javascript date. It should be converted to - // an ilib date object automatically and then formatted - var datMyBday = new Date("Wed May 14 2014 23:37:35 GMT-0700"); - test.equal(fmt.format(datMyBday), "Wednesday"); - test.done(); - } - }); - }, - - testDateFmtGetMonthsOfYearThai: function(test) { - test.expect(2); - // uses ThaiSolar calendar - var fmt = new DateFmt({ - locale: "th-TH", - sync: false, - onLoad: function(fmt) { - test.ok(fmt !== null); - - var arrMonths = fmt.getMonthsOfYear({length: "long"}); - - var expected = [undefined, "ม.ค.", "ก.พ.", "มี.ค.", "เม.ย.", "พ.ค.", "มิ.ย.", "ก.ค.", "ส.ค.", "ก.ย.", "ต.ค.", "พ.ย.", "ธ.ค."]; - test.deepEqual(arrMonths, expected); - test.done(); - } - }); - }, - - testDateFmtFormatRelativeWithinMinuteAfter: function(test) { - test.expect(1); - new DateFmt({ - length: "full", - sync: false, - onLoad: function(fmt) { - new GregorianDate({ - year: 2011, - month: 11, - day: 20, - hour: 13, - minute: 45, - second: 0, - millisecond: 0, - sync: false, - onLoad: function(reference) { - new GregorianDate({ - year: 2011, - month: 11, - day: 20, - hour: 13, - minute: 45, - second: 30, - millisecond: 0, - sync: false, - onLoad: function(date) { - test.equal(fmt.formatRelative(reference, date), "in 30 seconds"); - test.done(); - } - }); - } - }); - } - }); - }, - - testDateFmtConvertToGMT: function(test) { - test.expect(2); - var fmt = new DateFmt({ - length: "short", - type: "datetime", - timezone: "Europe/London", - locale: "en-GB", - time: "hmaz", - sync: false, - onLoad: function(fmt) { - test.ok(fmt !== null); - - new GregorianDate({ - year: 2011, - month: 9, - day: 20, - hour: 13, - minute: 45, - second: 0, - millisecond: 0, - timezone: "America/Los_Angeles", - locale: "en-US", - sync: false, - onLoad: function(date) { - test.equal(fmt.format(date), "20/09/2011, 21:45 GMT/BST"); - test.done(); - } - }); - } - }); - }, - - testDateFmtConvertToOtherTimeZone: function(test) { - test.expect(2); - new DateFmt({ - length: "short", - type: "datetime", - timezone: "Australia/Sydney", - locale: "en-AU", - time: "hmaz", - sync: false, - onLoad: function(fmt) { - test.ok(fmt !== null); - - new GregorianDate({ - year: 2011, - month: 9, - day: 20, - hour: 13, - minute: 45, - second: 0, - millisecond: 0, - timezone: "America/Los_Angeles", - locale: "en-US", - sync: false, - onLoad: function(date) { - test.equal(fmt.format(date), "21/9/11, 6:45 am AEST"); - test.done(); - } - }); - - } - }); - } -}; \ No newline at end of file diff --git a/test/testdatefmtpromise.js b/test/testdatefmtpromise.js deleted file mode 100644 index 46598d7..0000000 --- a/test/testdatefmtpromise.js +++ /dev/null @@ -1,272 +0,0 @@ -/* - * testdatefmtpromise.js - test the date formatter object asynchronously - * - * Copyright © 2018, 2022 JEDLSoft - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import ilib from "../src/ilib.js"; -import ThaiSolarDate from "../src/ThaiSolarDate.js"; -import PersianDate from "../src/PersianDate.js"; -import IslamicDate from "../src/IslamicDate.js"; -import HebrewDate from "../src/HebrewDate.js"; -import GregorianDate from "../src/GregorianDate.js"; -import JulianDate from "../src/JulianDate.js"; -import DateFmt from "../src/DateFmt.js"; -import DateFactory from "../src/DateFactory.js"; - -export const testdatefmtpromise = { - testDateFmtConstructorEmpty: function(test) { - test.expect(1); - DateFmt.create({ - sync: false - }).then(function(fmt) { - test.ok(fmt !== null); - test.done(); - }); - }, - - testDateFmtGetCalendarExplicit: function(test) { - test.expect(3); - DateFmt.create({ - calendar: "julian", - sync: false - }).then(function(fmt) { - test.ok(fmt); - var cal = fmt.getCalendar(); - test.ok(cal); - - test.equal(cal, "julian"); - test.done(); - }); - }, - - testDateFmtGetCalendarNotInThisLocale: function(test) { - DateFmt.create({ - calendar: "arabic", - locale: 'en-US', - sync: false - }).then(function(fmt) { - // "No formats available for calendar arabic in locale en-US" - test.ok(!fmt); - test.done(); - }); - }, - - testDateFmtGetTimeZoneDefault: function(test) { - test.expect(2); - ilib.tz = undefined; // just in case - if (ilib._getPlatform() === "nodejs") { - process.env.TZ = ""; - } - - DateFmt.create({ - sync: false - }).then(function(fmt) { - test.ok(fmt !== null); - - test.equal(fmt.getTimeZone().getId(), "local"); - test.done(); - }); - }, - - testDateFmtGetTimeZone: function(test) { - test.expect(2); - DateFmt.create({ - timezone: "Europe/Paris", - sync: false - }).then(function(fmt) { - test.ok(fmt !== null); - - test.equal(fmt.getTimeZone().getId(), "Europe/Paris"); - test.done(); - }); - }, - - testDateFmtUseTemplateNonEmptyCalendar: function(test) { - test.expect(2); - DateFmt.create({ - calendar: 'julian', - template: "EEE 'the' DD 'of' MM, yyyy G", - sync: false - }).then(function(fmt) { - test.ok(fmt !== null); - - test.equal(fmt.getCalendar(), "julian"); - test.done(); - }); - }, - - testDateFmtUseTemplateNonEmptyLocale: function(test) { - test.expect(2); - var fmt = DateFmt.create({ - locale: 'de-DE', - template: "EEE 'the' DD 'of' MM, yyyy G", - sync: false - }).then(function(fmt) { - test.ok(fmt !== null); - - test.equal(fmt.getLocale().toString(), "de-DE"); - test.done(); - }); - }, - - testDateFmtFormatJSDate1: function(test) { - test.expect(2); - DateFmt.create({ - type: "time", - length: "short", - timezone: "America/Los_Angeles", - sync: false - }).then(function(fmt) { - test.ok(fmt !== null); - - // test formatting a javascript date. It should be converted to - // an ilib date object automatically and then formatted - var datMyBday = new Date("Fri Aug 13 1982 13:37:35 GMT-0700"); - test.equal(fmt.format(datMyBday), "1:37 PM"); - test.done(); - }); - }, - - testDateFmtFormatJSDateRightTimeZone1: function(test) { - test.expect(2); - var fmt = DateFmt.create({ - type: "date", - length: "full", - date: "w", - timezone: "America/Los_Angeles", - sync: false - }).then(function(fmt) { - test.ok(fmt !== null); - - // test formatting a javascript date. It should be converted to - // an ilib date object automatically and then formatted - var datMyBday = new Date("Wed May 14 2014 23:37:35 GMT-0700"); - test.equal(fmt.format(datMyBday), "Wednesday"); - test.done(); - }); - }, - - testDateFmtGetMonthsOfYearThai: function(test) { - test.expect(2); - // uses ThaiSolar calendar - var fmt = DateFmt.create({ - locale: "th-TH", - sync: false - }).then(function(fmt) { - test.ok(fmt !== null); - - var arrMonths = fmt.getMonthsOfYear({length: "long"}); - - var expected = [undefined, "ม.ค.", "ก.พ.", "มี.ค.", "เม.ย.", "พ.ค.", "มิ.ย.", "ก.ค.", "ส.ค.", "ก.ย.", "ต.ค.", "พ.ย.", "ธ.ค."]; - test.deepEqual(arrMonths, expected); - test.done(); - }); - }, - - testDateFmtFormatRelativeWithinMinuteAfter: function(test) { - test.expect(1); - DateFmt.create({ - length: "full", - sync: false - }).then(function(fmt) { - GregorianDate.create({ - year: 2011, - month: 11, - day: 20, - hour: 13, - minute: 45, - second: 0, - millisecond: 0, - sync: false - }).then(function(reference) { - GregorianDate.create({ - year: 2011, - month: 11, - day: 20, - hour: 13, - minute: 45, - second: 30, - millisecond: 0, - sync: false - }).then(function(date) { - test.equal(fmt.formatRelative(reference, date), "in 30 seconds"); - test.done(); - }); - }); - }); - }, - - testDateFmtConvertToGMT: function(test) { - test.expect(2); - var fmt = DateFmt.create({ - length: "short", - type: "datetime", - timezone: "Europe/London", - locale: "en-GB", - time: "hmaz", - sync: false - }).then(function(fmt) { - test.ok(fmt !== null); - - GregorianDate.create({ - year: 2011, - month: 9, - day: 20, - hour: 13, - minute: 45, - second: 0, - millisecond: 0, - timezone: "America/Los_Angeles", - locale: "en-US", - sync: false - }).then(function(date) { - test.equal(fmt.format(date), "20/09/2011, 21:45 GMT/BST"); - test.done(); - }); - }); - }, - - testDateFmtConvertToOtherTimeZone: function(test) { - test.expect(2); - DateFmt.create({ - length: "short", - type: "datetime", - timezone: "Australia/Sydney", - locale: "en-AU", - time: "hmaz", - sync: false - }).then(function(fmt) { - test.ok(fmt !== null); - - GregorianDate.create({ - year: 2011, - month: 9, - day: 20, - hour: 13, - minute: 45, - second: 0, - millisecond: 0, - timezone: "America/Los_Angeles", - locale: "en-US", - sync: false - }).then(function(date) { - test.equal(fmt.format(date), "21/9/11, 6:45 am AEST"); - test.done(); - }); - }); - } -}; \ No newline at end of file diff --git a/test/testdatefmtrangepromise.js b/test/testdatefmtrangepromise.js deleted file mode 100644 index 8d7857b..0000000 --- a/test/testdatefmtrangepromise.js +++ /dev/null @@ -1,209 +0,0 @@ -/* - * testdatefmtrangepromise.js - test the date range formatter object - * - * Copyright © 2018-2019, JEDLSoft - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import LocaleInfo from "../src/LocaleInfo.js"; -import GregorianDate from "../src/GregorianDate.js"; -import DateRngFmt from "../src/DateRngFmt.js"; -import DateFmt from "../src/DateFmt.js"; - -export const testdatefmtrangepromise = { - testDateRngFmtConstructorEmpty: function(test) { - test.expect(1); - DateRngFmt.create({ - sync: false - }).then(function(fmt) { - test.ok(fmt !== null); - test.done(); - }); - - }, - - testDateRngFmtGetCalendarExplicit: function(test) { - test.expect(3); - DateRngFmt.create({ - calendar: "julian", - sync: false - }).then(function(fmt) { - test.ok(fmt !== null); - var cal = fmt.getCalendar(); - test.ok(cal !== null); - - test.equal(cal, "julian"); - test.done(); - }); - }, - - testDateRngFmtGetCalendarNotInThisLocale: function(test) { - DateRngFmt.create({ - calendar: "arabic", - locale: 'en-US', - sync: false - }).then(function(fmt) { - // "No formats available for calendar arabic in locale en-US" - test.ok(!fmt); - test.done(); - }); - }, - - testDateRngFmtGetLocale: function(test) { - test.expect(2); - var fmt = DateRngFmt.create({ - locale: "de-DE", - sync: false - }).then(function(fmt) { - test.ok(fmt !== null); - - test.equal(fmt.getLocale().toString(), "de-DE"); - test.done(); - }); - }, - - testDateRngFmtGetTimeZone: function(test) { - test.expect(2); - var fmt = DateRngFmt.create({ - timezone: "Europe/Paris", - sync: false - }).then(function(fmt) { - test.ok(fmt !== null); - - var tz = fmt.getTimeZone(); - test.equal(tz.getId(), "Europe/Paris"); - test.done(); - }); - }, - - testDateRngFmtGetDefaultLocale: function(test) { - test.expect(2); - var fmt = DateRngFmt.create({ - locale: "yy-YY", - sync: false - }).then(function(fmt) { - test.ok(fmt !== null); - - test.equal(fmt.getLocale().toString(), "yy-YY"); - test.done(); - }); - }, - - //Miscellaneous UnitTest - - testDateRngFmtesPAMultiDayMedium: function(test) { - test.expect(2); - var fmt = DateRngFmt.create({ - locale: "es-PA", - length: "medium", - sync: false - }).then(function(fmt) { - test.ok(fmt !== null); - - GregorianDate.create({ - year: 2011, - month: 6, - day: 20, - hour: 13, - minute: 45, - second: 0, - millisecond: 0, - sync: false - }).then(function(start) { - GregorianDate.create({ - year: 2011, - month: 6, - day: 28, - hour: 14, - minute: 5, - second: 30, - millisecond: 0, - sync: false - }).then(function(end) { - test.equal(fmt.format(start, end), "06/20 – 28/2011"); - test.done(); - }); - }); - }); - }, - - testDateRngFmtesPRNextMonthMedium: function(test) { - test.expect(2); - var fmt = DateRngFmt.create({ - locale: "es-PR", - length: "medium", - sync: false - }).then(function(fmt) { - test.ok(fmt !== null); - - GregorianDate.create({ - year: 2011, - month: 6, - day: 20, - hour: 13, - minute: 45, - second: 0, - millisecond: 0, - sync: false - }).then(function(start) { - GregorianDate.create({ - year: 2011, - month: 11, - day: 28, - hour: 14, - minute: 30, - second: 0, - millisecond: 0, - sync: false - }).then(function(end) { - test.equal(fmt.format(start, end), "06/20 – 11/28/2011"); - test.done(); - }); - }); - }); - }, - - testDateRngFmtAcceptJSIntrisicDates: function(test) { - test.expect(2); - var fmt = DateRngFmt.create({ - locale: "en-US", - length: "short", - sync: false - }).then(function(fmt) { - test.ok(fmt !== null); - - var start = new Date(2011, 5, 20, 13, 45, 0); - var end = new Date(2012, 4, 26, 16, 30, 0); - test.equal(fmt.format(start, end), "6/20/11 – 5/26/12"); - test.done(); - }); - }, - - testDateRngFmtAcceptUnixTimes: function(test) { - test.expect(2); - var fmt = DateRngFmt.create({ - locale: "en-US", - length: "short", - sync: false - }).then(function(fmt) { - test.ok(fmt !== null); - - var start = 1308602700000; - var end = 1338075000000; - test.equal(fmt.format(start, end), "6/20/11 – 5/26/12"); - test.done(); - }); - } -}; diff --git a/test/testdatepromise.js b/test/testdatepromise.js deleted file mode 100644 index f84ea51..0000000 --- a/test/testdatepromise.js +++ /dev/null @@ -1,157 +0,0 @@ -/* - * testdatepromise.js - test the date object asynchronously - * - * Copyright © 2018, 2022 JEDLSoft - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import GregorianDate from "../src/GregorianDate.js"; -import DateFmt from "../src/DateFmt.js"; -import DateFactory, {DateFactoryAsync} from "../src/DateFactory.js"; - -export const testdatepromise = { - testDateConstructor: function(test) { - test.expect(1); - DateFactoryAsync().then(function(gd) { - test.ok(gd !== null); - test.done(); - }); - }, - - testDateConstructorFull: function(test) { - test.expect(8); - DateFactoryAsync({ - year: 2011, - month: 9, - day: 23, - hour: 16, - minute: 7, - second: 12, - millisecond: 123 - }).then(function(gd) { - test.ok(gd !== null); - - test.equal(gd.getYears(), 2011); - test.equal(gd.getMonths(), 9); - test.equal(gd.getDays(), 23); - test.equal(gd.getHours(), 16); - test.equal(gd.getMinutes(), 7); - test.equal(gd.getSeconds(), 12); - test.equal(gd.getMilliseconds(), 123); - test.done(); - }); - }, - - testDateFactoryRightType: function(test) { - test.expect(2); - DateFactoryAsync({ - type: "gregorian" - }).then(function(gd) { - test.ok(gd !== null); - test.equal(gd.getCalendar(), "gregorian"); - test.done(); - }); - }, - - testDateFactoryNonGregorian: function(test) { - test.expect(2); - DateFactoryAsync({ - type: "hebrew" - }).then(function(hd) { - test.ok(hd !== null); - test.equal(hd.getCalendar(), "hebrew"); - test.done(); - }); - }, - - testDateFactoryNonGregorianWithCalendar: function(test) { - test.expect(2); - DateFactoryAsync({ - calendar: "hebrew" - }).then(function(hd) { - test.ok(hd !== null); - test.equal(hd.getCalendar(), "hebrew"); - test.done(); - }); - }, - - testDateFactoryBogus: function(test) { - test.expect(1); - DateFactoryAsync({ - type: "asdf" - }).then(function(gd) { - test.ok(typeof(gd) === "undefined"); - test.done(); - }); - }, - - testDateToIlibDate: function(test) { - test.expect(1); - var datMyBday = new Date("Fri Aug 13 1982 13:37:35 GMT-0700"); - DateFactoryAsync({ - year: 1982, - month: 8, - day: 13, - hour: 13, - minute: 37, - second: 35, - timezone: "America/Los_Angeles" - }).then(function(ildMyBday) { - DateFmt.create({ - length: "full", - sync: false - }).then(function(fmt) { - test.equal(fmt.format(DateFactory._dateToIlib(datMyBday)), fmt.format(ildMyBday)); - test.done(); - }); - }); - }, - - testDstStartBoundary_Azores: function(test) { - test.expect(1); - DateFactoryAsync({ - year: 2019, - month: 3, - day: 31, - hour: 0, - minute: 0, - second: 0, - timezone: "Atlantic/Azores" - }).then(function(boundaryiLib) { - // we can't set time zone to Date object, so compare with constant value - // 1553994000000: new Date(2019, 2, 31, 0, 0, 0).getTime() with Azores local time - test.equal(boundaryiLib.getTimeExtended(), 1553994000000); - test.done(); - }); - }, - - testDstEndBoundary_Azores: function(test) { - test.expect(1); - var boundaryiLib = DateFactory({ - year: 2019, - month: 10, - day: 27, - hour: 1, - minute: 0, - second: 0, - timezone: "Atlantic/Azores" - }); - var boundaryEs = new Date(2019, 9, 27, 1, 0, 0); - // we can't set time zone to Date object, so compare with constant value - // 1572141600000: new Date(2019, 9, 27, 1, 0, 0).getTime() with Azores local time - test.equal(boundaryiLib.getTimeExtended(), 1572141600000); - test.done(); - } -}; diff --git a/test/testdurfmtpromise.js b/test/testdurfmtpromise.js deleted file mode 100644 index e58720d..0000000 --- a/test/testdurfmtpromise.js +++ /dev/null @@ -1,249 +0,0 @@ -/* - * testdurfmtasync.js - test the duration formatter object - * - * Copyright © 2018, 2022 JEDLSoft - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import DurationFmt from "../src/DurationFmt.js"; - -export const testdurfmtpromise = { - testDurFmtAsyncConstructorEmpty: function(test) { - test.expect(1); - DurationFmt.create({ - sync: false - }).then(function(fmt) { - test.ok(fmt !== null); - test.done(); - }); - - }, - - testDurFmtAsyncGetLocale: function(test) { - test.expect(2); - DurationFmt.create({ - locale: "de-DE", - sync: false - }).then(function(fmt) { - test.ok(fmt !== null); - - test.equal(fmt.getLocale().toString(), "de-DE"); - test.done(); - }); - }, - - testDurFmtAsyncGetLocaleBogus: function(test) { - test.expect(2); - DurationFmt.create({ - locale: "zyy-XX", - sync: false - }).then(function(fmt) { - test.ok(fmt !== null); - - test.equal(fmt.getLocale().toString(), "zyy-XX"); - test.done(); - }); - }, - - testDurFmtAsyncFormatShortText: function(test) { - test.expect(2); - DurationFmt.create({ - length: "short", - sync: false - }).then(function(fmt) { - test.ok(fmt !== null); - - var duration = fmt.format({ - year: 1, - month: 1, - week: 1, - day: 1, - hour: 1, - minute: 1, - second: 1, - millisecond: 1 - }); - test.equal(duration.toString(), "1y 1m 1w 1d 1h 1m 1s 1ms"); - test.done(); - }); - }, - - testDurFmtAsyncFormatLongSingle: function(test) { - test.expect(2); - DurationFmt.create({ - length: "long", - sync: false - }).then(function(fmt) { - test.ok(fmt !== null); - - var duration = fmt.format({ - year: 1, - month: 1, - week: 1, - day: 1, - hour: 1, - minute: 1, - second: 1, - millisecond: 1 - }); - test.equal(duration.toString(), "1 yr, 1 mth, 1 wk, 1 day, 1 hr, 1 min, 1 sec, 1 ms"); - test.done(); - }); - }, - - testDurFmtAsyncFormatFullPlural: function(test) { - test.expect(2); - DurationFmt.create({ - length: "full", - sync: false - }).then(function(fmt) { - test.ok(fmt !== null); - - var duration = fmt.format({ - year: 2, - month: 2, - week: 2, - day: 2, - hour: 2, - minute: 2, - second: 2, - millisecond: 2 - }); - test.equal(duration.toString(), "2 years, 2 months, 2 weeks, 2 days, 2 hours, 2 minutes, 2 seconds, 2 milliseconds"); - test.done(); - }); - }, - - testDurFmtAsyncFormatShortDEDefaultStyle: function(test) { - test.expect(2); - DurationFmt.create({ - locale: "de-DE", - length: "short", - sync: false - }).then(function(fmt) { - test.ok(fmt !== null); - var duration = fmt.format({ - year: 1, - month: 1, - week: 1, - day: 1, - hour: 1, - minute: 1, - second: 1, - millisecond: 1 - }); - test.equal(duration.toString(), "1 J, 1 M, 1 W, 1 T, 1 Std., 1 Min., 1 Sek., 1 ms"); - test.done(); - }); - }, - - testDurFmtAsyncFormatFullDESingle: function(test) { - test.expect(2); - DurationFmt.create({ - locale: "de-DE", - length: "full", - sync: false - }).then(function(fmt) { - test.ok(fmt !== null); - - var duration = fmt.format({ - year: 1, - month: 1, - week: 1, - day: 1, - hour: 1, - minute: 1, - second: 1, - millisecond: 1 - }); - test.equal(duration.toString(), "1 Jahr, 1 Monat, 1 Woche, 1 Tag, 1 Stunde, 1 Minute, 1 Sekunde und 1 Millisekunde"); - test.done(); - }); - }, - - testDurFmtAsyncFormatShortZHText: function(test) { - test.expect(2); - DurationFmt.create({ - locale: "zh-Hans-CN", - length: "short", - style: "text", - sync: false - }).then(function(fmt) { - test.ok(fmt !== null); - - var duration = fmt.format({ - year: 1, - month: 1, - week: 1, - day: 1, - hour: 1, - minute: 1, - second: 1, - millisecond: 1 - }); - test.equal(duration.toString(), "1年1个月1周1天1小时1分钟1秒1毫秒"); - test.done(); - }); - }, - - testDurFmtAsyncFormatShortZHClock: function(test) { - test.expect(2); - DurationFmt.create({ - locale: "zh-Hans-CN", - length: "short", - style: "clock", - sync: false - }).then(function(fmt) { - test.ok(fmt !== null); - - var duration = fmt.format({ - year: 1, - month: 1, - week: 1, - day: 1, - hour: 1, - minute: 1, - second: 1, - millisecond: 1 - }); - test.equal(duration.toString(), "1年1个月1周1天01:01:01"); - test.done(); - }); - }, - - testDurFmtAsyncFormatFullZH: function(test) { - test.expect(2); - DurationFmt.create({ - locale: "zh-Hans-CN", - length: "full", - sync: false - }).then(function(fmt) { - test.ok(fmt !== null); - - var duration = fmt.format({ - year: 1, - month: 1, - week: 1, - day: 1, - hour: 1, - minute: 1, - second: 1, - millisecond: 1 - }); - test.equal(duration.toString(), "1年1个月1周1天1小时1分钟1秒钟1毫秒"); - test.done(); - }); - } -}; diff --git a/test/testglobal.js b/test/testglobal.js deleted file mode 100644 index 4d15e8a..0000000 --- a/test/testglobal.js +++ /dev/null @@ -1,639 +0,0 @@ -/* - * testglobal.js - test the ilib static routines - * - * Copyright © 2012-2015, 2017-2023, JEDLSoft - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import ilib from "../src/ilib.js"; -import Locale from "../src/Locale.js"; - -export const testglobal = { - setUp: function(callback) { - ilib.clearCache(); - callback(); - }, - - testGetLocaleDefault: function(test) { - test.expect(1); - test.equal(ilib.getLocale(), "en-US"); - test.done(); - }, - - testSetLocale: function(test) { - test.expect(2); - test.equal(ilib.getLocale(), "en-US"); - - ilib.setLocale("it-IT"); - - test.equal(ilib.getLocale(), "it-IT"); - test.done(); - delete ilib.locale; // clean up - }, - - testSetLocaleObject: function(test) { - test.expect(2); - test.equal(ilib.getLocale(), "en-US"); - - ilib.setLocale(new Locale("it-IT")); - - // do not change the locale if the arg is not a string - test.equal(ilib.getLocale(), "en-US"); - test.done(); - }, - - testSetLocaleEmpty: function(test) { - test.expect(2); - test.equal(ilib.getLocale(), "en-US"); - - ilib.setLocale(); - - test.equal(ilib.getLocale(), "en-US"); - test.done(); - }, - - testGetVersion: function(test) { - test.expect(1); - test.equal(ilib.getVersion().substring(0,5), "14.19"); - test.done(); - }, - - testGetTimeZoneDefault: function(test) { - // use a different test when the Intl object is available - ilib._platform = undefined; - if (ilib._global("Intl")) { - test.done(); - return; - } - - test.expect(1); - ilib._platform = undefined; - ilib.tz = undefined; - - if (ilib._getPlatform() === "nodejs") { - process.env.TZ = ""; - } - - if (ilib._getPlatform() === "browser") { - navigator.timezone = undefined; - } - test.equal(ilib.getTimeZone(), "local"); - test.done(); - }, - - /* - uncomment again when ilib is fixed - testGetTimeZoneDefaultWithIntl: function(test) { - // only test when the Intl object is available - if (!ilib._global("Intl")) { - test.done(); - return; - } - - ilib._platform = undefined; - ilib.tz = undefined; // clear this first - var ro = new Intl.DateTimeFormat().resolvedOptions(); - var expected = ro && ro.timeZone; - if (expected) { - test.expect(1); - test.equal(ilib.getTimeZone(), expected); - } - test.done(); - }, - */ - - testSetTimeZone: function(test) { - // use a different test when the Intl object is available - if (ilib._global("Intl")) { - test.done(); - return; - } - - test.expect(2); - ilib._platform = undefined; - ilib.tz = undefined; - test.equal(ilib.getTimeZone(), "local"); - - ilib.setTimeZone("America/Los_Angeles"); - - test.equal(ilib.getTimeZone(), "America/Los_Angeles"); - test.done(); - delete ilib.tz; // clean up - }, - - testGetTimeZoneBrowser: function(test) { - if (ilib._getPlatform() !== "browser" || ilib._global("Intl")) { - // only testable on a browser without the Intl object available - test.done(); - return; - } - - ilib._platform = undefined; - ilib.tz = undefined; - navigator.timezone = "America/New_York"; - - test.expect(1); - test.equal(ilib.getTimeZone(), "America/New_York"); - test.done(); - navigator.timezone = undefined; - }, - - testSetTimeZoneEmpty: function(test) { - // use a different test when the Intl object is available - if (ilib._global("Intl")) { - test.done(); - return; - } - - test.expect(2); - ilib._platform = undefined; - ilib.tz = undefined; - if (ilib._getPlatform() === "browser") { - navigator.timezone = undefined; - } - - test.equal(ilib.getTimeZone(), "local"); - - ilib.setTimeZone(); - - test.equal(ilib.getTimeZone(), "local"); - test.done(); - }, - - testGetTimeZoneNodejs: function(test) { - // only test on older nodejs where the Intl object is not available - if (ilib._getPlatform() === "nodejs" && !ilib._global("Intl")) { - test.expect(1); - ilib._platform = undefined; - ilib.tz = undefined; - if (typeof(process) === 'undefined') { - process = { - env: {} - }; - } - if (!process.env) process.env = {}; - - var tmp = process.env.TZ; - process.env.TZ = "America/Phoenix"; - - test.equal(ilib.getTimeZone(), "America/Phoenix"); - - process.env.TZ = tmp; - } - test.done(); - }, - - testGetTimeZoneRhino: function(test) { - if (ilib._getPlatform() !== "rhino" || ilib._global("Intl")) { - // only test this in rhino - test.done(); - return; - } - ilib.tz = undefined; - - if (typeof(process) === 'undefined') { - // under plain rhino - environment.user.timezone = "America/New_York"; - } else { - // under trireme on rhino emulating nodejs - process.env.TZ = "America/New_York"; - } - - test.expect(1); - test.equal(ilib.getTimeZone(), "America/New_York"); - test.done(); - }, - - testGetTimeZoneWebOS: function(test) { - if (ilib._getPlatform() !== "webos" || ilib._global("Intl")) { - // only test this in webos - test.done(); - return; - } - ilib.tz = undefined; - PalmSystem.timezone = "Europe/London"; - - test.expect(1); - test.equal(ilib.getTimeZone(), "Europe/London"); - test.done(); - }, - - testGetLocaleNodejs1: function(test) { - if (ilib._getPlatform() !== "nodejs") { - // only test this in node - test.done(); - return; - } - - ilib.locale = undefined; - if (!process.env) process.env = {}; - - process.env.LANG = "th-TH"; - - test.expect(1); - test.equal(ilib.getLocale(), "th-TH"); - test.done(); - - process.env.LANG = ""; - ilib.locale = undefined; - }, - - testGetLocaleNodejs2: function(test) { - if (ilib._getPlatform() !== "nodejs") { - // only test this in node - test.done(); - return; - } - - ilib.locale = undefined; - - process.env.LC_ALL = "th-TH"; - - test.expect(1); - test.equal(ilib.getLocale(), "th-TH"); - test.done(); - - process.env.LC_ALL = ""; - ilib.locale = undefined; - }, - - testGetLocaleRhino: function(test) { - if (ilib._getPlatform() !== "rhino") { - // only test this in node - test.done(); - return; - } - - ilib.locale = undefined; - - if (typeof(process) === 'undefined') { - // under plain rhino - environment.user.language = "de"; - environment.user.country = "AT"; - } else { - // under trireme on rhino emulating nodejs - process.env.LANG = "de_AT.UTF8"; - } - - test.expect(1); - test.equal(ilib.getLocale(), "de-AT"); - test.done(); - - if (typeof(process) === 'undefined') { - // under plain rhino - environment.user.language = undefined; - environment.user.country = undefined; - } else { - process.env.LANG = "en_US.UTF8"; - } - }, - - testGetLocaleWebOS: function(test) { - if (ilib._getPlatform() !== "webos") { - // only test this in node - test.done(); - return; - } - - ilib.locale = undefined; - - PalmSystem.locale = "ru-RU"; - - test.expect(1); - test.equal(ilib.getLocale(), "ru-RU"); - test.done(); - - PalmSystem.locale = undefined; - }, - - testGetLocaleNotString: function(test) { - if (ilib.isDynCode()) { - // can't test this with dynamically loaded code because the global context - // is different for each module and we cannot set global variables, so we - // cannot simulate the conditions where this code would work - test.done(); - return; - } - ilib._platform = undefined; - ilib.locale = new Locale("it-IT"); - - // should remove the locale object and make it into a string - test.expect(1); - test.equal(ilib.getLocale(), "en-US"); - test.done(); - }, - - testGetLocaleBrowser: function(test) { - if (ilib._getPlatform() !== "browser") { - // only test this in a real browser - test.done(); - return; - } - ilib.locale = undefined; - - var loc = ""; - - if (navigator.language.length > 5) { - var l = navigator.language; - loc = l.substring(0,3) + l.charAt(3).toUpperCase() + l.substring(4,8).toLowerCase() + l.substring(8).toUpperCase(); - } else if (navigator.language.length > 2) { - loc = navigator.language.substring(0,3) + navigator.language.substring(3).toUpperCase(); - } else { - loc = navigator.language; - } - if (loc === "en") { - loc = "en-US"; - } - test.expect(1); - test.equal(ilib.getLocale(), loc); - test.done(); - }, - - testIsArrayNewArrayObj: function(test) { - test.expect(1); - var a = new Array(); - test.ok(ilib.isArray(a)); - test.done(); - }, - - testIsArrayNewArrayBrackets: function(test) { - test.expect(1); - var a = []; - test.ok(ilib.isArray(a)); - test.done(); - }, - - testIsArrayObject: function(test) { - test.expect(1); - var a = {foo:234}; - test.ok(!ilib.isArray(a)); - test.done(); - }, - - testIsArrayObjectWithNumericProperties: function(test) { - test.expect(1); - var a = {"0": "d", "1": "c"}; - test.ok(!ilib.isArray(a)); - test.done(); - }, - - testIsArrayNumber: function(test) { - test.expect(1); - var a = 234; - test.ok(!ilib.isArray(a)); - test.done(); - }, - - testIsArrayString: function(test) { - test.expect(1); - var a = "asdf"; - test.ok(!ilib.isArray(a)); - test.done(); - }, - - testIsArrayNull: function(test) { - test.expect(1); - var a = null; - test.ok(!ilib.isArray(a)); - test.done(); - }, - - testIsArrayUndefined: function(test) { - test.expect(1); - var a = undefined; - test.ok(!ilib.isArray(a)); - test.done(); - }, - - testExtendSimple: function(test) { - test.expect(1); - var object1 = {"a": "A", "b": "B"}, - object2 = {"c": "C", "d": "D"}; - - ilib.extend(object1, object2); - test.deepEqual(object1, {"a": "A", "b": "B", "c": "C", "d": "D"}); - test.done(); - }, - - testExtendReturnObject1: function(test) { - test.expect(1); - var object1 = {"a": "A", "b": "B"}, - object2 = {"c": "C", "d": "D"}; - - var x = ilib.extend(object1, object2); - test.equal(x, object1); - test.done(); - }, - - testExtendArrays: function(test) { - test.expect(1); - var object1 = {"a": ["b", "c"]}, - object2 = {"a": ["d"]}; - - ilib.extend(object1, object2); - test.deepEqual(object1, {"a": ["b", "c", "d"]}); - test.done(); - }, - - testExtendArraysDups: function(test) { - test.expect(1); - var object1 = {"a": ["b", "c"]}, - object2 = {"a": ["c", "d"]}; - - ilib.extend(object1, object2); - test.deepEqual(object1, {"a": ["b", "c", "c", "d"]}); - test.done(); - }, - - testExtendArraysEmptySource: function(test) { - test.expect(1); - var object1 = {"a": []}, - object2 = {"a": ["d"]}; - - ilib.extend(object1, object2); - test.deepEqual(object1, {"a": ["d"]}); - test.done(); - }, - - testExtendArraysEmptyTarget: function(test) { - test.expect(1); - var object1 = {"a": ["b", "c"]}, - object2 = {"a": []}; - - ilib.extend(object1, object2); - test.deepEqual(object1, {"a": ["b", "c"]}); - test.done(); - }, - - testExtendArraysIncongruentTypes1: function(test) { - test.expect(1); - var object1 = {"a": ["b", "c"]}, - object2 = {"a": "d"}; - - ilib.extend(object1, object2); - test.deepEqual(object1, {"a": "d"}); - test.done(); - }, - - testExtendArraysIncongruentTypes2: function(test) { - test.expect(1); - var object1 = {"a": "b"}, - object2 = {"a": ["d"]}; - - ilib.extend(object1, object2); - test.deepEqual(object1, {"a": ["d"]}); - test.done(); - }, - - testExtendSimpleProperty: function(test) { - test.expect(1); - var object1 = {"a": "A", "b": "B"}, - object2 = {"b": "X"}; - - ilib.extend(object1, object2); - test.deepEqual(object1, {"a": "A", "b": "X"}); - test.done(); - }, - - testExtendComplexProperty: function(test) { - test.expect(1); - var object1 = {"a": "A", "b": {"x": "B"}}, - object2 = {"b": "X"}; - - ilib.extend(object1, object2); - test.deepEqual(object1, {"a": "A", "b": "X"}); - test.done(); - }, - - testExtendSubobjects: function(test) { - test.expect(1); - var object1 = {"b": {"x": "X", "y": "Y"}}, - object2 = {"b": {"x": "M", "y": "N"}}; - - ilib.extend(object1, object2); - test.deepEqual(object1, {"b": {"x": "M", "y": "N"}}); - test.done(); - }, - - testExtendSubobjectsLeaveObj1PropsUntouched: function(test) { - test.expect(1); - var object1 = {"a": "A", "b": {"x": "X", "y": "Y", "z": "Z"}}, - object2 = {"b": {"x": "M", "y": "N"}}; - - ilib.extend(object1, object2); - test.deepEqual(object1, {"a": "A", "b": {"x": "M", "y": "N", "z": "Z"}}); - test.done(); - }, - - testExtendSubobjectsAddProps: function(test) { - test.expect(1); - var object1 = {"a": "A", "b": {"x": "X", "y": "Y"}}, - object2 = {"b": {"x": "M", "y": "N", "z": "Z"}}; - - ilib.extend(object1, object2); - test.deepEqual(object1, {"a": "A", "b": {"x": "M", "y": "N", "z": "Z"}}); - test.done(); - }, - - testExtendSubobjectsAddProps: function(test) { - test.expect(1); - var object1 = {"a": "A", "b": {"x": "X", "y": "Y"}}, - object2 = {"b": {"x": "M", "y": "N", "z": "Z"}}; - - ilib.extend(object1, object2); - test.deepEqual(object1, {"a": "A", "b": {"x": "M", "y": "N", "z": "Z"}}); - test.done(); - }, - - testExtendBooleans: function(test) { - test.expect(1); - var object1 = {"a": true, "b": true}, - object2 = {"b": false}; - - ilib.extend(object1, object2); - test.deepEqual(object1, {"a": true, "b": false}); - test.done(); - }, - - testExtendAddBooleans: function(test) { - test.expect(1); - var object1 = {"a": true, "b": true}, - object2 = {"c": false}; - - ilib.extend(object1, object2); - test.deepEqual(object1, {"a": true, "b": true, "c": false}); - test.done(); - }, - - testExtendNumbers: function(test) { - test.expect(1); - var object1 = {"a": 1, "b": 2}, - object2 = {"b": 3}; - - ilib.extend(object1, object2); - test.deepEqual(object1, {"a": 1, "b": 3}); - test.done(); - }, - - testExtendNumbersWithZero: function(test) { - test.expect(1); - var object1 = {"a": 1, "b": 2}, - object2 = {"b": 0}; - - ilib.extend(object1, object2); - test.deepEqual(object1, {"a": 1, "b": 0}); - test.done(); - }, - - testExtendNumbersAddZero: function(test) { - test.expect(1); - var object1 = {"a": 1, "b": 2}, - object2 = {"c": 0}; - - ilib.extend(object1, object2); - test.deepEqual(object1, {"a": 1, "b": 2, "c": 0}); - test.done(); - } - - - /* - var testGlobalNumber = 42; - - testIsGlobal: function(test) { - test.expect(1); - test.ok(ilib._isGlobal("testGlobalNumber")); - test.done(); - }, - - testIsGlobalNot: function(test) { - test.expect(1); - test.ok(!ilib._isGlobal("asdfasdfasdf")); - test.done(); - }, - - testGlobal: function(test) { - test.expect(1); - test.equal(ilib._global("testGlobalNumber"), 42); - test.done(); - }, - - testGlobalUndefined: function(test) { - test.expect(1); - test.ok(typeof(ilib._global("testGlobalNumber2")) === "undefined"); - test.done(); - } - */ - -}; diff --git a/test/testglyphstrasync.js b/test/testglyphstrasync.js deleted file mode 100644 index 9f7184c..0000000 --- a/test/testglyphstrasync.js +++ /dev/null @@ -1,136 +0,0 @@ -/* - * testglyphstrasync.js - test the glyph iteration routines - * - * Copyright © 2018, 2022 JEDLSoft - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import GlyphString from "../src/GlyphString.js"; - -export const testglyphstrasync = { - testGlyphStrAsyncCharIteratorNormal: function(test) { - test.expect(8); - new GlyphString("aÄa", { // the A umlaut is a decomposed char - sync: false, - onLoad: function(s) { - var it = s.charIterator(); - - test.ok(it.hasNext()); - test.equal(it.next(), "a"); - test.ok(it.hasNext()); - test.equal(it.next(), "Ä"); - test.ok(it.hasNext()); - test.equal(it.next(), "a"); - test.ok(!it.hasNext()); - test.equal(it.next(), undefined); - test.done(); - } - }); - }, - - testGlyphStrAsyncCharIteratorEmpty: function(test) { - test.expect(2); - var s = new GlyphString("", { - sync: false, - onLoad: function(s) { - var it = s.charIterator(); - - test.ok(!it.hasNext()); - test.equal(it.next(), undefined); - test.done(); - } - }); - }, - - testGlyphStrAsyncCharIteratorMultipleDecomposed: function(test) { - test.expect(8); - var s = new GlyphString("aẬa", { // the accented A is a decomposed char with 2 accents - sync: false, - onLoad: function(s) { - var it = s.charIterator(); - - test.ok(it.hasNext()); - test.equal(it.next(), "a"); - test.ok(it.hasNext()); - test.equal(it.next(), "Ậ"); - test.ok(it.hasNext()); - test.equal(it.next(), "a"); - test.ok(!it.hasNext()); - test.equal(it.next(), undefined); - test.done(); - } - }); - }, - - testGlyphStrAsyncTruncateWithCombiningAccentsWholeGlyphs: function(test) { - test.expect(1); - var s = new GlyphString("aẬbẬcẬdẬe", { // the accented A is a decomposed char with 2 accents - sync: false, - onLoad: function(s) { - test.equal(s.truncate(4), "aẬbẬ"); - test.done(); - } - }); - }, - - testGlyphStrAsyncTruncateThai: function(test) { - test.expect(1); - var s = new GlyphString("สวัุสดีคุณเป็นอย่างไรบ้าง", { - sync: false, - onLoad: function(s) { - // this tests non-spacing marks that are also non-combining - - test.equal(s.truncate(4), "สวัุสดี"); - test.done(); - } - }); - }, - - testGlyphStrAsyncTruncateDevanagari1: function(test) { - test.expect(1); - var s = new GlyphString("हैलो, आप कैसे हैं?", { - sync: false, - onLoad: function(s) { - // if the 2nd base character has combining spacing accents on it, - // then it will not fit in the two spaces available, so the base - // and all its combining spacing accents have to be removed. - test.equal(s.truncate(2), "है"); - test.done(); - } - }); - }, - - testGlyphStrAsyncEllipsizeDevanagari2: function(test) { - test.expect(1); - var s = new GlyphString("हैलो, आप कैसे हैं?", { - sync: false, - onLoad: function(s) { - test.equal(s.ellipsize(8), "हैलो, आप …"); - test.done(); - } - }); - }, - - testGlyphStrAsyncEllipsizeJapanese: function(test) { - test.expect(1); - var s = new GlyphString("ェドイン", { - sync: false, - onLoad: function(s) { - test.equal(s.ellipsize(3), "ェド…"); - test.done(); - } - }); - } -}; diff --git a/test/testglyphstrpromise.js b/test/testglyphstrpromise.js deleted file mode 100644 index 740af91..0000000 --- a/test/testglyphstrpromise.js +++ /dev/null @@ -1,128 +0,0 @@ -/* - * testglyphstrasync.js - test the glyph iteration routines - * - * Copyright © 2018, 2022 JEDLSoft - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import GlyphString from "../src/GlyphString.js"; - -export const testglyphstrpromise = { - testGlyphStrAsyncCharIteratorNormal: function(test) { - test.expect(8); - GlyphString.create("aÄa", { // the A umlaut is a decomposed char - sync: false - }).then(function(s) { - var it = s.charIterator(); - - test.ok(it.hasNext()); - test.equal(it.next(), "a"); - test.ok(it.hasNext()); - test.equal(it.next(), "Ä"); - test.ok(it.hasNext()); - test.equal(it.next(), "a"); - test.ok(!it.hasNext()); - test.equal(it.next(), undefined); - test.done(); - }); - }, - - testGlyphStrAsyncCharIteratorEmpty: function(test) { - test.expect(2); - var s = GlyphString.create("", { - sync: false - }).then(function(s) { - var it = s.charIterator(); - - test.ok(!it.hasNext()); - test.equal(it.next(), undefined); - test.done(); - }); - }, - - testGlyphStrAsyncCharIteratorMultipleDecomposed: function(test) { - test.expect(8); - var s = GlyphString.create("aẬa", { // the accented A is a decomposed char with 2 accents - sync: false - }).then(function(s) { - var it = s.charIterator(); - - test.ok(it.hasNext()); - test.equal(it.next(), "a"); - test.ok(it.hasNext()); - test.equal(it.next(), "Ậ"); - test.ok(it.hasNext()); - test.equal(it.next(), "a"); - test.ok(!it.hasNext()); - test.equal(it.next(), undefined); - test.done(); - }); - }, - - testGlyphStrAsyncTruncateWithCombiningAccentsWholeGlyphs: function(test) { - test.expect(1); - var s = GlyphString.create("aẬbẬcẬdẬe", { // the accented A is a decomposed char with 2 accents - sync: false - }).then(function(s) { - test.equal(s.truncate(4), "aẬbẬ"); - test.done(); - }); - }, - - testGlyphStrAsyncTruncateThai: function(test) { - test.expect(1); - var s = GlyphString.create("สวัุสดีคุณเป็นอย่างไรบ้าง", { - sync: false - }).then(function(s) { - // this tests non-spacing marks that are also non-combining - - test.equal(s.truncate(4), "สวัุสดี"); - test.done(); - }); - }, - - testGlyphStrAsyncTruncateDevanagari1: function(test) { - test.expect(1); - var s = GlyphString.create("हैलो, आप कैसे हैं?", { - sync: false - }).then(function(s) { - // if the 2nd base character has combining spacing accents on it, - // then it will not fit in the two spaces available, so the base - // and all its combining spacing accents have to be removed. - test.equal(s.truncate(2), "है"); - test.done(); - }); - }, - - testGlyphStrAsyncEllipsizeDevanagari2: function(test) { - test.expect(1); - var s = GlyphString.create("हैलो, आप कैसे हैं?", { - sync: false - }).then(function(s) { - test.equal(s.ellipsize(8), "हैलो, आप …"); - test.done(); - }); - }, - - testGlyphStrAsyncEllipsizeJapanese: function(test) { - test.expect(1); - var s = GlyphString.create("ェドイン", { - sync: false - }).then(function(s) { - test.equal(s.ellipsize(3), "ェド…"); - test.done(); - }); - } -}; diff --git a/test/testlistfmtasync.js b/test/testlistfmtasync.js deleted file mode 100644 index 4b3fb02..0000000 --- a/test/testlistfmtasync.js +++ /dev/null @@ -1,71 +0,0 @@ -/* - * testlistfmtasync.js - test the list formatter object - * - * Copyright © 2018, 2022 JEDLSoft - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSe-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import ListFmt from "../src/ListFmt.js"; - -export const testlistfmtasync = { - testListFmtAsyncConstructorEmpty: function(test) { - new ListFmt({ - sync: false, - onLoad: function(fmt) { - test.expect(1); - test.ok(fmt !== null); - test.done(); - } - }); - }, - - testListFmtAsyncTestPropertyTwo: function(test) { - new ListFmt({ - sync: false, - onLoad: function(fmt) { - test.expect(2); - test.ok(fmt !== null); - test.equal(fmt.format(["true", "false"]), "true & false"); - test.done(); - } - }); - }, - - testListFmtAsyncbnINNumberFormatFour: function(test) { - new ListFmt({ - locale: "bn-IN", - sync: false, - onLoad: function(fmt) { - test.expect(2); - test.ok(fmt !== null); - test.equal(fmt.format(["এক", "দুই", "তিন", "চার"]), "এক, দুই, তিন এবং চার"); - test.done(); - } - }); - }, - - testListFmtAsynckoKRNumberFormatThree: function(test) { - new ListFmt({ - locale: "ko-KR", - sync: false, - onLoad: function(fmt) { - test.expect(2); - test.ok(fmt !== null); - test.equal(fmt.format(["하나", "둘", "셋"]), "하나, 둘 및 셋"); - test.done(); - } - }); - } -}; diff --git a/test/testlistfmtpromise.js b/test/testlistfmtpromise.js deleted file mode 100644 index 1760b5e..0000000 --- a/test/testlistfmtpromise.js +++ /dev/null @@ -1,67 +0,0 @@ -/* - * testlistfmtasync.js - test the list formatter object - * - * Copyright © 2018, 2022 JEDLSoft - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSe-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import ListFmt from "../src/ListFmt.js"; - -export const testlistfmtpromise = { - testListFmtAsyncConstructorEmpty: function(test) { - ListFmt.create({ - sync: false - }).then(function(fmt) { - test.expect(1); - test.ok(fmt !== null); - test.done(); - }); - }, - - testListFmtAsyncTestPropertyTwo: function(test) { - ListFmt.create({ - sync: false - }).then(function(fmt) { - test.expect(2); - test.ok(fmt !== null); - test.equal(fmt.format(["true", "false"]), "true & false"); - test.done(); - }); - }, - - testListFmtAsyncbnINNumberFormatFour: function(test) { - ListFmt.create({ - locale: "bn-IN", - sync: false - }).then(function(fmt) { - test.expect(2); - test.ok(fmt !== null); - test.equal(fmt.format(["এক", "দুই", "তিন", "চার"]), "এক, দুই, তিন এবং চার"); - test.done(); - }); - }, - - testListFmtAsynckoKRNumberFormatThree: function(test) { - ListFmt.create({ - locale: "ko-KR", - sync: false - }).then(function(fmt) { - test.expect(2); - test.ok(fmt !== null); - test.equal(fmt.format(["하나", "둘", "셋"]), "하나, 둘 및 셋"); - test.done(); - }); - } -}; diff --git a/test/testlocale.js b/test/testlocale.js deleted file mode 100644 index e4cff5c..0000000 --- a/test/testlocale.js +++ /dev/null @@ -1,789 +0,0 @@ -/* - * testlocale.js - test the locale object - * - * Copyright © 2018-2019, JEDLSoft - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import ilib from "../src/ilib.js"; -import Locale from "../src/Locale.js"; - -export const testlocale = { - setUp: function(callback) { - ilib.clearCache(); - callback(); - }, - - testLocaleConstructor: function(test) { - test.expect(1); - var loc = new Locale(); - - test.ok(loc !== null); - test.done(); - }, - - testLocaleConstructorCurrent: function(test) { - test.expect(4); - if (ilib._getPlatform() === "browser") { - // make sure it picks it up from the right place - navigator.language = "en-US"; - } - - var loc = new Locale(); // gives locale of the host JS engine - - test.ok(loc !== null); - - test.equal(loc.getLanguage(), "en"); - test.equal(loc.getRegion(), "US"); - test.ok(typeof(loc.getVariant()) === "undefined"); - test.done(); - }, - - testLocaleConstructorDefault: function(test) { - test.expect(4); - ilib.setLocale("it-IT"); - - var loc = new Locale(); - - test.ok(loc !== null); - - test.equal(loc.getLanguage(), "it"); - test.equal(loc.getRegion(), "IT"); - test.ok(typeof(loc.getVariant()) === "undefined"); - test.done(); - - delete ilib.locale; - }, - - testLocaleCopyConstructor: function(test) { - test.expect(4); - var loc2 = new Locale("de", "DE"); - var loc = new Locale(loc2); - - test.ok(loc !== null); - - test.equal(loc.getLanguage(), "de"); - test.equal(loc.getRegion(), "DE"); - test.ok(typeof(loc.getVariant()) === "undefined"); - test.done(); - }, - - testLocaleConstructorFull: function(test) { - test.expect(4); - var loc = new Locale("en", "US", "Midwest"); - - test.ok(loc !== null); - - test.equal(loc.getLanguage(), "en"); - test.equal(loc.getRegion(), "US"); - test.equal(loc.getVariant(), "Midwest"); - test.done(); - }, - - testLocaleConstructorSpecWithVariant: function(test) { - test.expect(5); - var loc = new Locale("en-US-Midwest"); - - test.ok(loc !== null); - - test.equal(loc.getLanguage(), "en"); - test.equal(loc.getRegion(), "US"); - test.equal(loc.getVariant(), "Midwest"); - test.ok(typeof(loc.getScript()) === "undefined"); - test.done(); - }, - - testLocaleConstructorSpecWithScript: function(test) { - test.expect(5); - var loc = new Locale("en-US-Latn"); - - test.ok(loc !== null); - - test.equal(loc.getLanguage(), "en"); - test.equal(loc.getRegion(), "US"); - test.equal(loc.getScript(), "Latn"); - test.ok(typeof(loc.getVariant()) === "undefined"); - test.done(); - }, - - testLocaleConstructorPartial: function(test) { - test.expect(4); - var loc = new Locale("en", "US"); - - test.ok(loc !== null); - - test.equal(loc.getLanguage(), "en"); - test.equal(loc.getRegion(), "US"); - test.ok(typeof(loc.getVariant()) === "undefined"); - test.done(); - }, - - testLocaleConstructorSpecPartial: function(test) { - test.expect(4); - var loc = new Locale("en-US"); - - test.ok(loc !== null); - - test.equal(loc.getLanguage(), "en"); - test.equal(loc.getRegion(), "US"); - test.ok(typeof(loc.getVariant()) === "undefined"); - test.done(); - }, - - testLocaleConstructorShort: function(test) { - test.expect(4); - var loc = new Locale("en"); - - test.ok(loc !== null); - - test.equal(loc.getLanguage(), "en"); - test.ok(typeof(loc.getRegion()) === "undefined"); - test.ok(typeof(loc.getVariant()) === "undefined"); - test.done(); - }, - - testLocaleConstructorUpperCaseLanguage: function(test) { - test.expect(4); - var loc = new Locale("EN", "US"); - - test.ok(loc !== null); - - test.equal(loc.getLanguage(), "en"); - test.equal(loc.getRegion(), "US"); - test.ok(typeof(loc.getVariant()) === "undefined"); - test.done(); - }, - - testLocaleConstructorLowerCaseRegion: function(test) { - test.expect(4); - var loc = new Locale("en", "us"); - - test.ok(loc !== null); - - test.equal(loc.getLanguage(), "en"); - test.equal(loc.getRegion(), "US"); - test.ok(typeof(loc.getVariant()) === "undefined"); - test.done(); - }, - - testLocaleConstructorSpecMissingRegion: function(test) { - test.expect(5); - var loc = new Locale("en--Midwest"); - - test.ok(loc !== null); - - test.equal(loc.getLanguage(), "en"); - test.ok(typeof(loc.getRegion()) === "undefined"); - test.equal(loc.getVariant(), "Midwest"); - test.ok(typeof(loc.getScript()) === "undefined"); - test.done(); - }, - - testLocaleConstructorSpecMissingLanguage: function(test) { - test.expect(5); - var loc = new Locale("-US-Midwest"); - - test.ok(loc !== null); - - test.ok(typeof(loc.getLanguage()) === "undefined"); - test.equal(loc.getRegion(), "US"); - test.equal(loc.getVariant(), "Midwest"); - test.ok(typeof(loc.getScript()) === "undefined"); - test.done(); - }, - - testLocaleConstructorSpecMissingLanguageAndVariant: function(test) { - test.expect(5); - var loc = new Locale("-US"); - - test.ok(loc !== null); - - test.ok(typeof(loc.getLanguage()) === "undefined"); - test.equal(loc.getRegion(), "US"); - test.ok(typeof(loc.getVariant()) === "undefined"); - test.ok(typeof(loc.getScript()) === "undefined"); - test.done(); - }, - - testLocaleEqualsTrue: function(test) { - test.expect(3); - var loc1 = new Locale("en-US"), - loc2 = new Locale("en", "US"); - - test.ok(loc1 !== null); - test.ok(loc2 !== null); - - test.ok(loc1.equals(loc2)); - test.done(); - }, - - testLocaleEqualsFalse: function(test) { - test.expect(3); - var loc1 = new Locale("en-US"), - loc2 = new Locale("en", "CA"); - - test.ok(loc1 !== null); - test.ok(loc2 !== null); - - test.ok(!loc1.equals(loc2)); - test.done(); - }, - - testLocaleEqualsMissing: function(test) { - test.expect(3); - var loc1 = new Locale("en-US"), - loc2 = new Locale("en", "US", "govt"); - - test.ok(loc1 !== null); - test.ok(loc2 !== null); - - test.ok(!loc1.equals(loc2)); - test.done(); - }, - - testLocaleEqualsTrueFull: function(test) { - test.expect(3); - var loc1 = new Locale("en-US-govt"), - loc2 = new Locale("en", "US", "govt"); - - test.ok(loc1 !== null); - test.ok(loc2 !== null); - - test.ok(loc1.equals(loc2)); - test.done(); - }, - - testLocaleEqualsTrueShort: function(test) { - test.expect(3); - var loc1 = new Locale("en"), - loc2 = new Locale("en"); - - test.ok(loc1 !== null); - test.ok(loc2 !== null); - - test.ok(loc1.equals(loc2)); - test.done(); - }, - - testLocaleIsPseudoTrue: function(test) { - test.expect(2); - var loc = new Locale("zxx-XX"); - - test.ok(loc !== null); - - test.ok(loc.isPseudo(loc)); - test.done(); - }, - - testLocaleIsPseudoFalse: function(test) { - test.expect(2); - var loc = new Locale("en-US"); - - test.ok(loc !== null); - - test.ok(!loc.isPseudo(loc)); - test.done(); - }, - - testLocaleIsPseudoFalseButClosLang: function(test) { - test.expect(2); - var loc = new Locale("zxx-US"); - - test.ok(loc !== null); - - test.ok(!loc.isPseudo(loc)); - test.done(); - }, - - testLocaleIsPseudoFalse: function(test) { - test.expect(2); - var loc = new Locale("en-XX"); - - test.ok(loc !== null); - - test.ok(!loc.isPseudo(loc)); - test.done(); - }, - - - testLocaleGetSpecLangOnly: function(test) { - test.expect(2); - var loc = new Locale("en"); - - test.ok(loc !== null); - - test.equal(loc.getSpec(), "en"); - test.done(); - }, - - testLocaleGetSpecRegionOnly: function(test) { - test.expect(2); - var loc = new Locale("CA"); - - test.ok(loc !== null); - - test.equal(loc.getSpec(), "CA"); - test.done(); - }, - - testLocaleGetSpecScriptOnly: function(test) { - test.expect(2); - var loc = new Locale("Latn"); - - test.ok(loc !== null); - - test.equal(loc.getSpec(), "Latn"); - test.done(); - }, - - testLocaleGetSpecVariantOnly: function(test) { - test.expect(2); - var loc = new Locale("asdfasdf"); - - test.ok(loc !== null); - - test.equal(loc.getSpec(), "asdfasdf"); - test.done(); - }, - - testLocaleGetSpecLangAndScript: function(test) { - test.expect(2); - var loc = new Locale("Latn-en"); - - test.ok(loc !== null); - - test.equal(loc.getSpec(), "en-Latn"); - test.done(); - }, - - testLocaleGetSpecLangAndRegion: function(test) { - test.expect(2); - var loc = new Locale("CA-en"); - - test.ok(loc !== null); - - test.equal(loc.getSpec(), "en-CA"); - test.done(); - }, - - testLocaleGetSpecLangAndVariant: function(test) { - test.expect(2); - var loc = new Locale("asdf-en"); - - test.ok(loc !== null); - - test.equal(loc.getSpec(), "en-asdf"); - test.done(); - }, - - testLocaleGetSpecScriptAndRegion: function(test) { - test.expect(2); - var loc = new Locale("CA-Latn"); - - test.ok(loc !== null); - - test.equal(loc.getSpec(), "Latn-CA"); - test.done(); - }, - - testLocaleGetSpecScriptAndVariant: function(test) { - test.expect(2); - var loc = new Locale("asdf-Latn"); - - test.ok(loc !== null); - - test.equal(loc.getSpec(), "Latn-asdf"); - test.done(); - }, - - testLocaleGetSpecRegionAndVariant: function(test) { - test.expect(2); - var loc = new Locale("asdf-CA"); - - test.ok(loc !== null); - - test.equal(loc.getSpec(), "CA-asdf"); - test.done(); - }, - - testLocaleGetSpecLangScriptRegion: function(test) { - test.expect(2); - var loc = new Locale("CA-en-Latn"); - - test.ok(loc !== null); - - test.equal(loc.getSpec(), "en-Latn-CA"); - test.done(); - }, - - testLocaleGetSpecScriptRegionVariant: function(test) { - test.expect(2); - var loc = new Locale("CA-asdf-Latn"); - - test.ok(loc !== null); - - test.equal(loc.getSpec(), "Latn-CA-asdf"); - test.done(); - }, - - testLocaleGetSpecLangScriptVariant: function(test) { - test.expect(2); - var loc = new Locale("asdf-Latn-en"); - - test.ok(loc !== null); - - test.equal(loc.getSpec(), "en-Latn-asdf"); - test.done(); - }, - - testLocaleGetSpecLangRegionVariant: function(test) { - test.expect(2); - var loc = new Locale("asdf-CA-en"); - - test.ok(loc !== null); - - test.equal(loc.getSpec(), "en-CA-asdf"); - test.done(); - }, - - testLocaleGetSpecAll: function(test) { - test.expect(2); - var loc = new Locale("asdf-CA-Latn-en"); - - test.ok(loc !== null); - - test.equal(loc.getSpec(), "en-Latn-CA-asdf"); - test.done(); - }, - - testLocaleM49RegionCodeGetParts: function(test) { - test.expect(4); - var loc = new Locale("en-001"); - - test.ok(loc !== null); - - test.equal(loc.getLanguage(), "en"); - test.equal(loc.getRegion(), "001"); - test.ok(typeof(loc.getVariant()) === "undefined"); - test.done(); - }, - - testLocaleM49RegionCodeGetParts2: function(test) { - test.expect(4); - var loc = new Locale("en-150"); - - test.ok(loc !== null); - - test.equal(loc.getLanguage(), "en"); - test.equal(loc.getRegion(), "150"); - test.ok(typeof(loc.getVariant()) === "undefined"); - test.done(); - }, - - testLocaleM49RegionCodeGetSpec: function(test) { - test.expect(2); - var loc = new Locale("en-001"); - - test.ok(loc !== null); - - test.equal(loc.getSpec(), "en-001"); - test.done(); - }, - - testLocaleNoLocale: function(test) { - test.expect(6); - var loc = new Locale("-"); - - test.ok(loc !== null); - - test.equal(loc.getSpec(), ""); - test.ok(typeof(loc.getLanguage()) === "undefined"); - test.ok(typeof(loc.getRegion()) === "undefined"); - test.ok(typeof(loc.getScript()) === "undefined"); - test.ok(typeof(loc.getVariant()) === "undefined"); - test.done(); - }, - - - testLocaleRegionMap1: function(test) { - test.expect(1); - test.equal(Locale.regionAlpha2ToAlpha3("SG"), "SGP"); - test.done(); - }, - - testLocaleRegionMap2: function(test) { - test.expect(1); - test.equal(Locale.regionAlpha2ToAlpha3("VN"), "VNM"); - test.done(); - }, - - testLocaleRegionMap3: function(test) { - test.expect(1); - test.equal(Locale.regionAlpha2ToAlpha3("KR"), "KOR"); - test.done(); - }, - - testLocaleRegionMapEmpty: function(test) { - test.expect(1); - test.equal(Locale.regionAlpha2ToAlpha3(""), ""); - test.done(); - }, - - testLocaleRegionMapUnknown: function(test) { - test.expect(1); - test.equal(Locale.regionAlpha2ToAlpha3("QQ"), "QQ"); - test.done(); - }, - - testLocaleRegionMapWrongCase: function(test) { - test.expect(1); - test.equal(Locale.regionAlpha2ToAlpha3("sg"), "sg"); - test.done(); - }, - - testLocaleRegionMapUndefined: function(test) { - test.expect(1); - test.ok(typeof(Locale.regionAlpha2ToAlpha3(undefined)) === "undefined"); - test.done(); - }, - - testLocaleLanguageMap1: function(test) { - test.expect(1); - test.equal(Locale.languageAlpha1ToAlpha3("ko"), "kor"); - test.done(); - }, - - testLocaleLanguageMap2: function(test) { - test.expect(1); - test.equal(Locale.languageAlpha1ToAlpha3("th"), "tha"); - test.done(); - }, - - testLocaleLanguageMap3: function(test) { - test.expect(1); - test.equal(Locale.languageAlpha1ToAlpha3("hr"), "hrv"); - test.done(); - }, - - testLocaleLanguageMapEmpty: function(test) { - test.expect(1); - test.equal(Locale.languageAlpha1ToAlpha3(""), ""); - test.done(); - }, - - testLocaleLanguageMapUnknown: function(test) { - test.expect(1); - test.equal(Locale.languageAlpha1ToAlpha3("qq"), "qq"); - test.done(); - }, - - testLocaleLanguageMapWrongCase: function(test) { - test.expect(1); - test.equal(Locale.languageAlpha1ToAlpha3("EN"), "EN"); - test.done(); - }, - - testLocaleLanguageMapUndefined: function(test) { - test.expect(1); - test.ok(typeof(Locale.languageAlpha1ToAlpha3(undefined)) === "undefined"); - test.done(); - }, - - testLocaleGetLanguageAlpha3_1: function(test) { - test.expect(2); - var loc = new Locale("en-US"); - - test.ok(loc !== null); - - test.equal(loc.getLanguageAlpha3(), "eng"); - test.done(); - }, - - testLocaleGetLanguageAlpha3_2: function(test) { - test.expect(2); - var loc = new Locale("ru-RU"); - - test.ok(loc !== null); - - test.equal(loc.getLanguageAlpha3(), "rus"); - test.done(); - }, - - testLocaleGetLanguageAlpha3_3: function(test) { - test.expect(2); - var loc = new Locale("gv-GB"); - - test.ok(loc !== null); - - test.equal(loc.getLanguageAlpha3(), "glv"); - test.done(); - }, - - testLocaleGetLanguageAlpha3NoLanguage: function(test) { - test.expect(2); - var loc = new Locale("GB"); - - test.ok(loc !== null); - - test.ok(typeof(loc.getLanguageAlpha3()) === "undefined"); - test.done(); - }, - - testLocaleGetRegionAlpha3_1: function(test) { - test.expect(2); - var loc = new Locale("en-US"); - - test.ok(loc !== null); - - test.equal(loc.getRegionAlpha3(), "USA"); - test.done(); - }, - - testLocaleGetRegionAlpha3_2: function(test) { - test.expect(2); - var loc = new Locale("ru-RU"); - - test.ok(loc !== null); - - test.equal(loc.getRegionAlpha3(), "RUS"); - test.done(); - }, - - testLocaleGetRegionAlpha3_3: function(test) { - test.expect(2); - var loc = new Locale("gv-GB"); - - test.ok(loc !== null); - - test.equal(loc.getRegionAlpha3(), "GBR"); - test.done(); - }, - - testLocaleGetRegionAlpha3NoRegion: function(test) { - test.expect(2); - var loc = new Locale("en"); - - test.ok(loc !== null); - - test.ok(typeof(loc.getRegionAlpha3()) === "undefined"); - test.done(); - }, - - testLocaleGetAvailableLocalesDefault: function(test) { - test.expect(2); - var locales = Locale.getAvailableLocales(); - test.ok(typeof(locales) !== "undefined"); - test.ok(locales.length > 0); - test.done(); - }, - - testLocaleGetAvailableLocalesCallback: function(test) { - test.expect(2); - Locale.getAvailableLocales(true, function(locales) { - test.ok(typeof(locales) !== "undefined"); - test.ok(locales.length > 0); - test.done(); - }); - }, - - testLocaleGetLanguageSpecSimple: function(test) { - test.expect(2); - - test.ok(loc !== null); - - var loc = new Locale("en"); - test.equal(loc.getLangSpec(), "en"); - test.done(); - }, - - testLocaleGetLanguageSpecLeaveOutRegionAndVariant: function(test) { - test.expect(2); - - test.ok(loc !== null); - - var loc = new Locale("en-US-MILITARY"); - test.equal(loc.getLangSpec(), "en"); - test.done(); - }, - - testLocaleGetLanguageSpecIncludeScript: function(test) { - test.expect(2); - - test.ok(loc !== null); - - var loc = new Locale("zh-Hans"); - test.equal(loc.getLangSpec(), "zh-Hans"); - test.done(); - }, - - testLocaleGetLanguageSpecIncludeScriptButNotOthers: function(test) { - test.expect(2); - - test.ok(loc !== null); - - var loc = new Locale("zh-Hans-CN-GOVT"); - test.equal(loc.getLangSpec(), "zh-Hans"); - test.done(); - }, - - testLocaleGetLanguageSpecLanguageAndScriptMissing: function(test) { - test.expect(2); - - test.ok(loc !== null); - - var loc = new Locale("CN"); - test.equal(loc.getLangSpec(), ""); - test.done(); - }, - - testLocaleGetLanguageSpecNoScriptWithoutLanguage: function(test) { - test.expect(2); - - test.ok(loc !== null); - - var loc = new Locale("Hans-CN"); - test.equal(loc.getLangSpec(), ""); - test.done(); - }, - - testLocaleConstructorCalledWithNonStrings: function(test) { - test.expect(8); - - function a(a) { return a; } - - try { - var loc = new Locale(true, true, false, true); - test.equal(loc.getLangSpec(), ""); - var loc = new Locale(a, a, a, a); - test.equal(loc.getSpec(), ""); - var loc = new Locale(4, 4, 4, 4); - test.equal(loc.getSpec(), ""); - var loc = new Locale({}, {}, {}, {}); - test.equal(loc.getSpec(), ""); - - var loc = new Locale(true); - test.equal(loc.getSpec(), ""); - var loc = new Locale(a); - test.equal(loc.getSpec(), ""); - var loc = new Locale(4); - test.equal(loc.getSpec(), ""); - var loc = new Locale({}); - test.equal(loc.getSpec(), ""); - } catch (e) { - test.fail(); - } - test.done(); - } -}; \ No newline at end of file diff --git a/test/testlocaleinfoasync.js b/test/testlocaleinfoasync.js deleted file mode 100644 index 84cb98f..0000000 --- a/test/testlocaleinfoasync.js +++ /dev/null @@ -1,129 +0,0 @@ -/* - * testlocaleinfoasync.js - test the locale info object - * - * Copyright © 2018-2019, JEDLSoft - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import LocaleInfo from "../src/LocaleInfo.js"; - -export const testlocaleinfoasync = { - testLocaleInfoAsyncConstructor: function(test) { - test.expect(5); - new LocaleInfo(undefined, { - sync: false, - onLoad: function(info) { - test.ok(info !== null); - - var loc = info.getLocale(); - test.ok(loc !== null); - - test.equal(loc.getLanguage(), "en"); - test.equal(loc.getRegion(), "US"); - test.ok(typeof(loc.getVariant()) === "undefined"); - test.done(); - } - }); - }, - - testLocaleInfoAsyncConstructorGivenLocale: function(test) { - test.expect(4); - new LocaleInfo("de-DE", { - sync: false, - onLoad: function(info) { - test.ok(info !== null); - - var loc = info.getLocale(); - - test.equal(loc.getLanguage(), "de"); - test.equal(loc.getRegion(), "DE"); - test.ok(typeof(loc.getVariant()) === "undefined"); - test.done(); - } - }); - }, - - testLocaleInfoAsyncGetTimeZoneDefault: function(test) { - test.expect(2); - new LocaleInfo("zz-ZZ", { - sync: false, - onLoad: function(info) { - test.ok(info !== null); - - test.equal(info.getTimeZone(), "Etc/UTC"); - test.done(); - } - }); - }, - - testLocaleInfoAsyncGetCurrencyUnknown: function(test) { - test.expect(2); - new LocaleInfo("zxx-XX", { - sync: false, - onLoad: function(info) { - test.ok(info !== null); - - test.equal(info.getCurrency(), "USD"); - test.done(); - } - }); - }, - - testLocaleInfoAsyncGetDecimalSeparatorfor_ko_KR: function(test) { - test.expect(5); - new LocaleInfo("ko-KR", { - sync: false, - onLoad: function(info) { - test.ok(info !== null); - test.equal(info.getDecimalSeparator(), "."); - test.equal(info.getGroupingSeparator(), ","); - test.equal(info.getPercentageFormat(), "{n}%"); - test.equal(info.getCurrencyFormats().common, "{s}{n}"); - test.done(); - } - }); - }, - - testLocaleInfoAsyncGetDecimalSeparatorfor_fr_FR: function(test) { - test.expect(5); - new LocaleInfo("fr-FR", { - sync: false, - onLoad: function(info) { - test.ok(info !== null); - test.equal(info.getDecimalSeparator(), ","); - test.equal(info.getGroupingSeparator(), " "); - test.equal(info.getPercentageFormat(), "{n} %"); - test.equal(info.getCurrencyFormats().common, "{n} {s}"); - test.done(); - } - }); - }, - - testLocaleInfoAsyncGetDecimalSeparatorfor_zh_Hant_US: function(test) { - test.expect(5); - // test mixing locale parts for a non-standard locale - new LocaleInfo("zh-Hant-US", { - sync: false, - onLoad: function(info) { - test.ok(info !== null); - test.equal(info.getDecimalSeparator(), "."); - test.equal(info.getGroupingSeparator(), ","); - test.equal(info.getRoundingMode(), "halfdown"); - test.equal(info.getCurrency(), "USD"); - test.done(); - } - }); - } -}; diff --git a/test/testlocaleinfopromise.js b/test/testlocaleinfopromise.js deleted file mode 100644 index 0e216c7..0000000 --- a/test/testlocaleinfopromise.js +++ /dev/null @@ -1,122 +0,0 @@ -/* - * testlocaleinfoasync.js - test the locale info object - * - * Copyright © 2018-2019, JEDLSoft - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import LocaleInfo from "../src/LocaleInfo.js"; - -export const testlocaleinfopromise = { - testLocaleInfoAsyncConstructor: function(test) { - test.expect(5); - LocaleInfo.create(undefined, { - sync: false - }).then(function(info) { - test.ok(info !== null); - - var loc = info.getLocale(); - test.ok(loc !== null); - - test.equal(loc.getLanguage(), "en"); - test.equal(loc.getRegion(), "US"); - test.ok(typeof(loc.getVariant()) === "undefined"); - test.done(); - }); - }, - - testLocaleInfoAsyncConstructorGivenLocale: function(test) { - test.expect(4); - LocaleInfo.create("de-DE", { - sync: false - }).then(function(info) { - test.ok(info !== null); - - var loc = info.getLocale(); - - test.equal(loc.getLanguage(), "de"); - test.equal(loc.getRegion(), "DE"); - test.ok(typeof(loc.getVariant()) === "undefined"); - test.done(); - }); - }, - - testLocaleInfoAsyncGetTimeZoneDefault: function(test) { - test.expect(2); - LocaleInfo.create("zz-ZZ", { - sync: false - }).then(function(info) { - test.ok(info !== null); - - test.equal(info.getTimeZone(), "Etc/UTC"); - test.done(); - }); - }, - - testLocaleInfoAsyncGetCurrencyUnknown: function(test) { - test.expect(2); - LocaleInfo.create("zxx-XX", { - sync: false - }).then(function(info) { - test.ok(info !== null); - - test.equal(info.getCurrency(), "USD"); - test.done(); - }); - }, - - testLocaleInfoAsyncGetDecimalSeparatorfor_ko_KR: function(test) { - test.expect(5); - LocaleInfo.create("ko-KR", { - sync: false - }).then(function(info) { - test.ok(info !== null); - test.equal(info.getDecimalSeparator(), "."); - test.equal(info.getGroupingSeparator(), ","); - test.equal(info.getPercentageFormat(), "{n}%"); - test.equal(info.getCurrencyFormats().common, "{s}{n}"); - test.done(); - }); - }, - - testLocaleInfoAsyncGetDecimalSeparatorfor_fr_FR: function(test) { - test.expect(5); - LocaleInfo.create("fr-FR", { - sync: false - }).then(function(info) { - test.ok(info !== null); - test.equal(info.getDecimalSeparator(), ","); - test.equal(info.getGroupingSeparator(), " "); - test.equal(info.getPercentageFormat(), "{n} %"); - test.equal(info.getCurrencyFormats().common, "{n} {s}"); - test.done(); - }); - }, - - testLocaleInfoAsyncGetDecimalSeparatorfor_zh_Hant_US: function(test) { - test.expect(5); - // test mixing locale parts for a non-standard locale - LocaleInfo.create("zh-Hant-US", { - sync: false - }).then(function(info) { - test.ok(info !== null); - test.equal(info.getDecimalSeparator(), "."); - test.equal(info.getGroupingSeparator(), ","); - test.equal(info.getRoundingMode(), "halfdown"); - test.equal(info.getCurrency(), "USD"); - test.done(); - }); - } -}; diff --git a/test/testlocalematchasync.js b/test/testlocalematchasync.js deleted file mode 100644 index eb221dc..0000000 --- a/test/testlocalematchasync.js +++ /dev/null @@ -1,78 +0,0 @@ -/* - * testlocalematchasync.js - test the locale matcher object - * - * Copyright © 2018, 2022 JEDLSoft - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import LocaleMatcher from "../src/LocaleMatcher.js"; - -export const testlocalematchasync = { - testLocaleMatcherAsyncConstructor: function(test) { - test.expect(1); - new LocaleMatcher({ - sync: false, - onLoad: function(lm) { - test.ok(lm !== null); - test.done(); - } - }); - }, - - testLocaleMatcherAsyncGetLikelyLocaleByLanguage1: function(test) { - test.expect(3); - new LocaleMatcher({ - locale: "uz", - sync: false, - onLoad: function(lm) { - test.ok(typeof(lm) !== "undefined"); - var locale = lm.getLikelyLocale(); - test.ok(typeof(locale) !== "undefined"); - test.equal(locale.getSpec(), "uz-Latn-UZ"); - test.done(); - } - }); - }, - - testLocaleMatcherAsyncGetLikelyLocaleByRegion: function(test) { - test.expect(3); - new LocaleMatcher({ - locale: "UZ", - sync: false, - onLoad: function(lm) { - test.ok(typeof(lm) !== "undefined"); - var locale = lm.getLikelyLocale(); - test.ok(typeof(locale) !== "undefined"); - test.equal(locale.getSpec(), "uz-Latn-UZ"); - test.done(); - } - }); - }, - - testLocaleMatcherAsyncGetLikelyLocaleByScript: function(test) { - test.expect(3); - new LocaleMatcher({ - locale: "Arab", - sync: false, - onLoad: function(lm) { - test.ok(typeof(lm) !== "undefined"); - var locale = lm.getLikelyLocale(); - test.ok(typeof(locale) !== "undefined"); - test.equal(locale.getSpec(), "ar-Arab-EG"); - test.done(); - } - }); - } -}; diff --git a/test/testlocalematchpromise.js b/test/testlocalematchpromise.js deleted file mode 100644 index 3ff9421..0000000 --- a/test/testlocalematchpromise.js +++ /dev/null @@ -1,74 +0,0 @@ -/* - * testlocalematchasync.js - test the locale matcher object - * - * Copyright © 2018, 2022 JEDLSoft - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import LocaleMatcher from "../src/LocaleMatcher.js"; - -export const testlocalematchpromise = { - testLocaleMatcherAsyncConstructor: function(test) { - test.expect(1); - LocaleMatcher.create({ - sync: false - }).then(function(lm) { - test.ok(lm !== null); - test.done(); - }); - }, - - testLocaleMatcherAsyncGetLikelyLocaleByLanguage1: function(test) { - test.expect(3); - LocaleMatcher.create({ - locale: "uz", - sync: false - }).then(function(lm) { - test.ok(typeof(lm) !== "undefined"); - var locale = lm.getLikelyLocale(); - test.ok(typeof(locale) !== "undefined"); - test.equal(locale.getSpec(), "uz-Latn-UZ"); - test.done(); - }); - }, - - testLocaleMatcherAsyncGetLikelyLocaleByRegion: function(test) { - test.expect(3); - LocaleMatcher.create({ - locale: "UZ", - sync: false - }).then(function(lm) { - test.ok(typeof(lm) !== "undefined"); - var locale = lm.getLikelyLocale(); - test.ok(typeof(locale) !== "undefined"); - test.equal(locale.getSpec(), "uz-Latn-UZ"); - test.done(); - }); - }, - - testLocaleMatcherAsyncGetLikelyLocaleByScript: function(test) { - test.expect(3); - LocaleMatcher.create({ - locale: "Arab", - sync: false - }).then(function(lm) { - test.ok(typeof(lm) !== "undefined"); - var locale = lm.getLikelyLocale(); - test.ok(typeof(locale) !== "undefined"); - test.equal(locale.getSpec(), "ar-Arab-EG"); - test.done(); - }); - } -}; diff --git a/test/testnameasync.js b/test/testnameasync.js deleted file mode 100644 index 7f69eb5..0000000 --- a/test/testnameasync.js +++ /dev/null @@ -1,95 +0,0 @@ -/* - * testnameasync.js - test the name object - * - * Copyright © 2018, 2022 JEDLSoft - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import Name from "../src/Name.js"; - -export const testnameasync = { - testNameAsyncEmptyConstructor: function(test) { - test.expect(1); - new Name(undefined, { - sync: false, - onLoad: function(name) { - test.ok(typeof(name) === "undefined"); - test.done(); - } - }); - }, - - testNameAsyncCopyConstructor: function(test) { - test.expect(2); - new Name({ - prefix: "a", - givenName: "b", - middleName: "c", - familyName: "d", - suffix: "e", - honorific: "x" - }, { - sync: false, - onLoad: function(name) { - test.ok(typeof(name) !== "undefined"); - - test.contains(name, { prefix: "a", givenName: "b", middleName: "c", familyName: "d", suffix: "e", honorific: "x"}); - test.done(); - } - }); - }, - - testNameAsyncDEWithMultiplePrefixes: function(test) { - test.expect(2); - new Name("Herr Dr. Josef Hans Jürgen Herzheim", { - locale: "de-DE", - sync: false, - onLoad: function(name) { - test.ok(typeof(name) !== "undefined"); - - test.contains(name, { prefix: "Herr Dr.", givenName: "Josef", middleName: "Hans Jürgen", familyName: "Herzheim" }); - test.done(); - } - }); - }, - - testNameAsyncESFull: function(test) { - test.expect(2); - new Name("Juan Carlos Maria León Arroyo", { - locale: "es-ES", - sync: false, - onLoad: function(name) { - test.ok(typeof(name) !== "undefined"); - - test.contains(name, { givenName: "Juan", middleName: "Carlos Maria", familyName: "León Arroyo" }); - test.done(); - } - }); - }, - - testNameAsyncZHHonorific: function(test) { - test.expect(2); - new Name("堂哥胡锦涛", { - locale: "zh-CN", - sync: false, - onLoad: function(name) { - test.ok(typeof(name) !== "undefined"); - - test.contains(name, { prefix: "堂哥", givenName: "锦涛", familyName: "胡" }); - test.done(); - } - }); - } -}; diff --git a/test/testnamepromise.js b/test/testnamepromise.js deleted file mode 100644 index f46c517..0000000 --- a/test/testnamepromise.js +++ /dev/null @@ -1,90 +0,0 @@ -/* - * testnameasync.js - test the name object - * - * Copyright © 2018, 2022 JEDLSoft - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import Name from "../src/Name.js"; - -export const testnamepromise = { - testNameAsyncEmptyConstructor: function(test) { - test.expect(1); - Name.create(undefined, { - sync: false - }).then(function(name) { - test.ok(typeof(name) === "undefined"); - test.done(); - }); - }, - - testNameAsyncCopyConstructor: function(test) { - test.expect(2); - Name.create({ - prefix: "a", - givenName: "b", - middleName: "c", - familyName: "d", - suffix: "e", - honorific: "x" - }, { - sync: false - }).then(function(name) { - test.ok(typeof(name) !== "undefined"); - - test.contains(name, { prefix: "a", givenName: "b", middleName: "c", familyName: "d", suffix: "e", honorific: "x"}); - test.done(); - }); - }, - - testNameAsyncDEWithMultiplePrefixes: function(test) { - test.expect(2); - Name.create("Herr Dr. Josef Hans Jürgen Herzheim", { - locale: "de-DE", - sync: false - }).then(function(name) { - test.ok(typeof(name) !== "undefined"); - - test.contains(name, { prefix: "Herr Dr.", givenName: "Josef", middleName: "Hans Jürgen", familyName: "Herzheim" }); - test.done(); - }); - }, - - testNameAsyncESFull: function(test) { - test.expect(2); - Name.create("Juan Carlos Maria León Arroyo", { - locale: "es-ES", - sync: false - }).then(function(name) { - test.ok(typeof(name) !== "undefined"); - - test.contains(name, { givenName: "Juan", middleName: "Carlos Maria", familyName: "León Arroyo" }); - test.done(); - }); - }, - - testNameAsyncZHHonorific: function(test) { - test.expect(2); - Name.create("堂哥胡锦涛", { - locale: "zh-CN", - sync: false - }).then(function(name) { - test.ok(typeof(name) !== "undefined"); - - test.contains(name, { prefix: "堂哥", givenName: "锦涛", familyName: "胡" }); - test.done(); - }); - } -}; diff --git a/test/testnormalizepromise.js b/test/testnormalizepromise.js deleted file mode 100644 index 98b53bd..0000000 --- a/test/testnormalizepromise.js +++ /dev/null @@ -1,76 +0,0 @@ -/* - * testnormalizeasync.js - test phonenumber normalize function() - * - * Copyright © 2018, 2022 JEDLSoft - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import PhoneNumber from "../src/PhoneNumber.js"; - -export const testnormalizepromise = { - testNormalizeAsyncIDDPrefix: function(test) { - test.expect(1); - PhoneNumber.create("011 31 456 3453434", { - locale: 'en-US', - sync: false - }).then(function(parsed) { - parsed.normalize({ - locale: 'en-US', - sync: false - }).then(function(normalized) { - var expected = "+314563453434"; - test.equal(normalized, expected); - test.done(); - }); - }); - }, - - testNormalizeAsyncLDNumberUsingDEMCC: function(test) { - test.expect(1); - PhoneNumber.create("02302 654321", { - locale: 'de-DE', - sync: false - }).then(function(parsed) { - parsed.normalize({ - mcc: "262", - locale: 'en-US', - sync: false - }).then(function(normalized) { - var expected = "+492302654321"; - - test.equal(normalized, expected); // 'de-DE' - test.done(); - }); - }); - }, - - testNormalizeAsyncLDNumberUsingUSSpanishLocale: function(test) { - test.expect(1); - PhoneNumber.create("650 7654321", { - locale: 'es-US', - sync: false - }).then(function(parsed) { - parsed.normalize({ - locale: 'en-US', - sync: false - }).then(function(normalized) { - var expected = "+16507654321"; - - test.equal(normalized, expected); // 'es-US' - test.done(); - }); - }); - } -}; diff --git a/test/testnormasync.js b/test/testnormasync.js deleted file mode 100644 index c660fb6..0000000 --- a/test/testnormasync.js +++ /dev/null @@ -1,104 +0,0 @@ -/* - * testnormasync.js - test the Unicode Normalization Algorithm routines - * - * Copyright © 2018, 2022 JEDLSoft - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import NormString from "../src/NormString.js"; - -function toHexString(string) { - var i, result = ""; - - if (!string) { - return ""; - } - for (i = 0; i < string.length; i++) { - var ch = string.charCodeAt(i).toString(16); - result += "0000".substring(0, 4-ch.length) + ch; - if (i < string.length - 1) { - result += " "; - } - } - return result.toUpperCase(); -} - - -export const testnormasync = { - testNormStringAsyncNormalizeNFD: function(test) { - test.expect(1); - - new NormString("ᄀ각ᆨ", { - sync: false, - onLoad: function(str) { - test.equal(str.normalize("nfd"), "ᄀ각ᆨ"); - test.done(); - } - }); - }, - - testNormStringAsyncNormalizeNFKD: function(test) { - test.expect(1); - new NormString("ᄀ각ᆨ", { - sync: false, - onLoad: function(str) { - test.equal(str.normalize("nfkd"), "ᄀ각ᆨ"); - test.done(); - } - }); - }, - - testNormStringAsyncNormalizeNFC: function(test) { - test.expect(1); - new NormString("ᄀ각ᆨ", { - sync: false, - onLoad: function(str) { - test.equal(str.normalize("nfc"), "ᄀ각ᆨ"); - test.done(); - } - }); - }, - - testNormStringAsyncNormalizeNFKC: function(test) { - test.expect(1); - new NormString("ᄀ각ᆨ", { - sync: false, - onLoad: function(str) { - test.equal(str.normalize("nfkc"), "ᄀ각ᆨ"); - test.done(); - } - }); - }, - - testNormStringAsyncCharIteratorDecomposed: function(test) { - test.expect(8); - var s = new NormString("aÄa", { // the A umlaut is a decomposed char - sync: false, - onLoad: function(str) { - var it = str.charIterator(); - - test.ok(it.hasNext()); - test.equal(it.next(), "a"); - test.ok(it.hasNext()); - test.equal(it.next(), "Ä"); - test.ok(it.hasNext()); - test.equal(it.next(), "a"); - test.ok(!it.hasNext()); - test.equal(it.next(), undefined); - test.done(); - } - }); - } -}; diff --git a/test/testnormpromise.js b/test/testnormpromise.js deleted file mode 100644 index 1da2ebf..0000000 --- a/test/testnormpromise.js +++ /dev/null @@ -1,99 +0,0 @@ -/* - * testnormasync.js - test the Unicode Normalization Algorithm routines - * - * Copyright © 2018, 2022 JEDLSoft - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import NormString from "../src/NormString.js"; - -function toHexString(string) { - var i, result = ""; - - if (!string) { - return ""; - } - for (i = 0; i < string.length; i++) { - var ch = string.charCodeAt(i).toString(16); - result += "0000".substring(0, 4-ch.length) + ch; - if (i < string.length - 1) { - result += " "; - } - } - return result.toUpperCase(); -} - - -export const testnormpromise = { - testNormStringAsyncNormalizeNFD: function(test) { - test.expect(1); - - NormString.create("ᄀ각ᆨ", { - sync: false - }).then(function(str) { - test.equal(str.normalize("nfd"), "ᄀ각ᆨ"); - test.done(); - }); - }, - - testNormStringAsyncNormalizeNFKD: function(test) { - test.expect(1); - NormString.create("ᄀ각ᆨ", { - sync: false - }).then(function(str) { - test.equal(str.normalize("nfkd"), "ᄀ각ᆨ"); - test.done(); - }); - }, - - testNormStringAsyncNormalizeNFC: function(test) { - test.expect(1); - NormString.create("ᄀ각ᆨ", { - sync: false - }).then(function(str) { - test.equal(str.normalize("nfc"), "ᄀ각ᆨ"); - test.done(); - }); - }, - - testNormStringAsyncNormalizeNFKC: function(test) { - test.expect(1); - NormString.create("ᄀ각ᆨ", { - sync: false - }).then(function(str) { - test.equal(str.normalize("nfkc"), "ᄀ각ᆨ"); - test.done(); - }); - }, - - testNormStringAsyncCharIteratorDecomposed: function(test) { - test.expect(8); - var s = NormString.create("aÄa", { // the A umlaut is a decomposed char - sync: false - }).then(function(str) { - var it = str.charIterator(); - - test.ok(it.hasNext()); - test.equal(it.next(), "a"); - test.ok(it.hasNext()); - test.equal(it.next(), "Ä"); - test.ok(it.hasNext()); - test.equal(it.next(), "a"); - test.ok(!it.hasNext()); - test.equal(it.next(), undefined); - test.done(); - }); - } -}; diff --git a/test/testnumfmtasync.js b/test/testnumfmtasync.js deleted file mode 100644 index 424e5f2..0000000 --- a/test/testnumfmtasync.js +++ /dev/null @@ -1,130 +0,0 @@ -/* - * testnumfmtasync.js - test the number formatter object - * - * Copyright © 2018, 2022 JEDLSoft - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSe-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import NumFmt from "../src/NumFmt.js"; - -export const testnumfmtasync = { - testNumFmtAsyncDefaults: function(test) { - test.expect(7); - new NumFmt({ - sync: false, - onLoad: function(fmt) { - test.ok(fmt !== null); - - test.equal(fmt.getType(), "number"); - test.equal(fmt.getMaxFractionDigits(), -1); - test.equal(fmt.getMinFractionDigits(), -1); - test.ok(fmt.isGroupingUsed()); - test.equal(fmt.getRoundingMode(), "halfdown"); - test.ok(typeof(fmt.getCurrency()) === "undefined"); - test.done(); - } - }); - - }, - - testNumFmtAsyncNumberFormatSimple: function(test) { - test.expect(2); - new NumFmt({ - sync: false, - onLoad: function(fmt) { - test.ok(fmt !== null); - - test.equal(fmt.format(1.745), "1.745"); - test.done(); - } - }); - }, - - testNumFmtAsyncNumberStyleStringArgument: function(test) { - test.expect(2); - new NumFmt({ - style: "standard", - sync: false, - onLoad: function(fmt) { - test.ok(fmt !== null); - - // should work with string arguments - test.equal(fmt.format("2345678901234567.0"), "2,345,678,901,234,567"); - test.done(); - } - }); - }, - - testNumFmtAsyncFormatNativeDefaultTrue: function(test) { - test.expect(2); - new NumFmt({ - locale: "bn-IN", - sync: false, - onLoad: function(fmt) { - test.ok(fmt !== null); - - test.equal(fmt.format(123.456), "১২৩.৪৫৬"); - test.done(); - } - }); - }, - - testNumFmtAsyncCurrencyFormatUS: function(test) { - test.expect(2); - new NumFmt({ - type: "currency", - currency: "USD", - sync: false, - onLoad: function(fmt) { - test.ok(fmt !== null); - - test.equal(fmt.format(57.3467), "$57.35"); - test.done(); - } - }); - }, - - testNumFmtAsyncCurrencyGetFractionDigitsOtherCurrency: function(test) { - test.expect(3); - new NumFmt({ - type: "currency", - currency: "JPY", // Japanese yen - sync: false, - onLoad: function(fmt) { - test.ok(fmt !== null); - - test.equal(fmt.getMaxFractionDigits(), 0); - test.equal(fmt.getMinFractionDigits(), 0); - test.done(); - } - }); - }, - - testNumFmtAsyncPercentageFormatRegular_kn_IN: function(test) { - test.expect(2); - new NumFmt({ - locale: "kn-IN", - useNative: true, - type: "percentage", - sync: false, - onLoad: function(fmt) { - test.ok(fmt !== null); - - test.equal(fmt.format(57.8), "೫೭.೮%"); - test.done(); - } - }); - } -}; diff --git a/test/testnumfmtpromise.js b/test/testnumfmtpromise.js deleted file mode 100644 index 1623b4a..0000000 --- a/test/testnumfmtpromise.js +++ /dev/null @@ -1,123 +0,0 @@ -/* - * testnumfmtasync.js - test the number formatter object - * - * Copyright © 2018, 2022 JEDLSoft - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSe-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import NumFmt from "../src/NumFmt.js"; - -export const testnumfmtpromise = { - testNumFmtAsyncDefaults: function(test) { - test.expect(7); - NumFmt.create({ - sync: false - }).then(function(fmt) { - test.ok(fmt !== null); - - test.equal(fmt.getType(), "number"); - test.equal(fmt.getMaxFractionDigits(), -1); - test.equal(fmt.getMinFractionDigits(), -1); - test.ok(fmt.isGroupingUsed()); - test.equal(fmt.getRoundingMode(), "halfdown"); - test.ok(typeof(fmt.getCurrency()) === "undefined"); - test.done(); - }); - - }, - - testNumFmtAsyncNumberFormatSimple: function(test) { - test.expect(2); - NumFmt.create({ - sync: false - }).then(function(fmt) { - test.ok(fmt !== null); - - test.equal(fmt.format(1.745), "1.745"); - test.done(); - }); - }, - - testNumFmtAsyncNumberStyleStringArgument: function(test) { - test.expect(2); - NumFmt.create({ - style: "standard", - sync: false - }).then(function(fmt) { - test.ok(fmt !== null); - - // should work with string arguments - test.equal(fmt.format("2345678901234567.0"), "2,345,678,901,234,567"); - test.done(); - }); - }, - - testNumFmtAsyncFormatNativeDefaultTrue: function(test) { - test.expect(2); - NumFmt.create({ - locale: "bn-IN", - sync: false - }).then(function(fmt) { - test.ok(fmt !== null); - - test.equal(fmt.format(123.456), "১২৩.৪৫৬"); - test.done(); - }); - }, - - testNumFmtAsyncCurrencyFormatUS: function(test) { - test.expect(2); - NumFmt.create({ - type: "currency", - currency: "USD", - sync: false - }).then(function(fmt) { - test.ok(fmt !== null); - - test.equal(fmt.format(57.3467), "$57.35"); - test.done(); - }); - }, - - testNumFmtAsyncCurrencyGetFractionDigitsOtherCurrency: function(test) { - test.expect(3); - NumFmt.create({ - type: "currency", - currency: "JPY", // Japanese yen - sync: false - }).then(function(fmt) { - test.ok(fmt !== null); - - test.equal(fmt.getMaxFractionDigits(), 0); - test.equal(fmt.getMinFractionDigits(), 0); - test.done(); - }); - }, - - testNumFmtAsyncPercentageFormatRegular_kn_IN: function(test) { - test.expect(2); - NumFmt.create({ - locale: "kn-IN", - useNative: true, - type: "percentage", - sync: false - }).then(function(fmt) { - test.ok(fmt !== null); - - test.equal(fmt.format(57.8), "೫೭.೮%"); - test.done(); - }); - } -}; diff --git a/test/testnumplanasync.js b/test/testnumplanasync.js deleted file mode 100644 index 521766e..0000000 --- a/test/testnumplanasync.js +++ /dev/null @@ -1,74 +0,0 @@ -/* - * testnumplanasync.js - Test the phone numbering plan. - * - * Copyright © 2018, 2022 JEDLSoft - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import NumberingPlan from "../src/NumberingPlan.js"; - -export const testnumplanasync = { - testNumberingPlanAsync1: function(test) { - test.expect(2); - new NumberingPlan({ - locale: "en-US", - sync: false, - onLoad: function(plan) { - test.ok(typeof(plan) !== "undefined"); - test.equal(plan.getName(), "US"); - test.done(); - } - }); - }, - - testNumberingPlanAsync2: function(test) { - test.expect(2); - new NumberingPlan({ - locale: "de-DE", - sync: false, - onLoad: function(plan) { - test.ok(typeof(plan) !== "undefined"); - test.equal(plan.getName(), "DE"); - test.done(); - } - }); - }, - - testNumberingPlanAsyncUnknown: function(test) { - test.expect(2); - new NumberingPlan({ - locale: "unknown-unknown", - sync: false, - onLoad: function(plan) { - test.ok(typeof(plan) !== "undefined"); - test.equal(plan.getName(), "XX"); - test.done(); - } - }); - }, - - testNumberingPlanAsyncUnrecognized: function(test) { - test.expect(2); - new NumberingPlan({ - locale: "zu-ZZ", - sync: false, - onLoad: function(plan) { - test.ok(typeof(plan) !== "undefined"); - test.equal(plan.getName(), "XX"); - test.done(); - } - }); - } -}; diff --git a/test/testnumplanpromise.js b/test/testnumplanpromise.js deleted file mode 100644 index 57ff846..0000000 --- a/test/testnumplanpromise.js +++ /dev/null @@ -1,70 +0,0 @@ -/* - * testnumplanasync.js - Test the phone numbering plan. - * - * Copyright © 2018, 2022 JEDLSoft - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import NumberingPlan from "../src/NumberingPlan.js"; - -export const testnumplanpromise = { - testNumberingPlanAsync1: function(test) { - test.expect(2); - NumberingPlan.create({ - locale: "en-US", - sync: false - }).then(function(plan) { - test.ok(typeof(plan) !== "undefined"); - test.equal(plan.getName(), "US"); - test.done(); - }); - }, - - testNumberingPlanAsync2: function(test) { - test.expect(2); - NumberingPlan.create({ - locale: "de-DE", - sync: false - }).then(function(plan) { - test.ok(typeof(plan) !== "undefined"); - test.equal(plan.getName(), "DE"); - test.done(); - }); - }, - - testNumberingPlanAsyncUnknown: function(test) { - test.expect(2); - NumberingPlan.create({ - locale: "unknown-unknown", - sync: false - }).then(function(plan) { - test.ok(typeof(plan) !== "undefined"); - test.equal(plan.getName(), "XX"); - test.done(); - }); - }, - - testNumberingPlanAsyncUnrecognized: function(test) { - test.expect(2); - NumberingPlan.create({ - locale: "zu-ZZ", - sync: false - }).then(function(plan) { - test.ok(typeof(plan) !== "undefined"); - test.equal(plan.getName(), "XX"); - test.done(); - }); - } -}; diff --git a/test/testnumprsasync.js b/test/testnumprsasync.js deleted file mode 100644 index 8efdbc5..0000000 --- a/test/testnumprsasync.js +++ /dev/null @@ -1,140 +0,0 @@ -/* - * testnumprsasync.js - test the number parsing routines - * - * Copyright © 2012-2018, 2022 JEDLSoft - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import INumber from "../src/INumber.js"; - -export const testnumprsasync = { - testNumberAsyncConstructorDefault: function(test) { - test.expect(2); - var num = new INumber("3.4", { - sync: false, - onLoad: function(num) { - test.ok(num !== null); - test.equal(num.valueOf(), 3.4); - test.done(); - } - }); - }, - - testNumberAsyncConstructorWithIlibNumber: function(test) { - test.expect(2); - new INumber("3.4", { - sync: false, - onLoad: function(num) { - var num = new INumber(num, { - sync: false, - onLoad: function(num2) { - test.ok(num2 !== null); - test.equal(num2.valueOf(), 3.4); - test.done(); - } - }); - } - }) - }, - - testNumberAsyncGetLocaleOther: function(test) { - test.expect(3); - var num = new INumber("3,4", { - locale: "de-DE", - sync: false, - onLoad: function(num) { - test.ok(num !== null); - - test.equal(num.getLocale().getSpec(), "de-DE"); - test.equal(num.valueOf(), 3.4); - test.done(); - } - }); - }, - - testNumberAsyncPercentage: function(test) { - test.expect(2); - var num = new INumber("58.3%", { - type: "percentage", - sync: false, - onLoad: function(num) { - test.ok(num !== null); - - test.equal(num.valueOf(), 0.583); - test.done(); - } - }); - }, - - testNumberAsyncCurrencyValue: function(test) { - test.expect(2); - var num = new INumber("$5.80", { - type: "currency", - sync: false, - onLoad: function(num) { - test.ok(num !== null); - - test.equal(num.valueOf(), 5.80); - test.done(); - } - }); - }, - - testNumberAsyncCurrencyForLocale: function(test) { - test.expect(3); - var num = new INumber("£5.80", { - type: "currency", - sync: false, - onLoad: function(num) { - test.ok(num !== null); - - var cur = num.getCurrency(); - test.ok(typeof(cur) !== "undefined"); - test.equal(cur.getCode(), "GBP"); - test.done(); - } - }); - }, - - testNumberAsyncParseNonLatinDigits: function(test) { - test.expect(2); - - // tests that the CType isDigit data is loaded - new INumber("১২৩.৪৫৬", { - locale: "bn-IN", - sync: false, - onLoad: function(num) { - test.ok(num !== null); - test.equal(num.valueOf(), 123.456); - test.done(); - } - }); - }, - - testNumberAsyncParseWithSpaces: function(test) { - test.expect(2); - - // tests that CType isSpace data is loaded - new INumber("1 234 567,745", { - locale: "fr-FR", - sync: false, - onLoad: function(num) { - test.ok(num !== null); - test.equal(num.valueOf(), 1234567.745); - test.done(); - } - }); - } -}; diff --git a/test/testnumprspromise.js b/test/testnumprspromise.js deleted file mode 100644 index 046aede..0000000 --- a/test/testnumprspromise.js +++ /dev/null @@ -1,131 +0,0 @@ -/* - * testnumprsasync.js - test the number parsing routines - * - * Copyright © 2012-2018, 2022 JEDLSoft - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import INumber from "../src/INumber.js"; - -export const testnumprspromise = { - testNumberAsyncConstructorDefault: function(test) { - test.expect(2); - var num = INumber.create("3.4", { - sync: false - }).then(function(num) { - test.ok(num !== null); - test.equal(num.valueOf(), 3.4); - test.done(); - }); - }, - - testNumberAsyncConstructorWithIlibNumber: function(test) { - test.expect(2); - INumber.create("3.4", { - sync: false - }).then(function(num) { - return INumber.create(num, { - sync: false - }).then(function(num2) { - test.ok(num2 !== null); - test.equal(num2.valueOf(), 3.4); - test.done(); - }); - }); - }, - - testNumberAsyncGetLocaleOther: function(test) { - test.expect(3); - var num = INumber.create("3,4", { - locale: "de-DE", - sync: false - }).then(function(num) { - test.ok(num !== null); - - test.equal(num.getLocale().getSpec(), "de-DE"); - test.equal(num.valueOf(), 3.4); - test.done(); - }); - }, - - testNumberAsyncPercentage: function(test) { - test.expect(2); - var num = INumber.create("58.3%", { - type: "percentage", - sync: false - }).then(function(num) { - test.ok(num !== null); - - test.equal(num.valueOf(), 0.583); - test.done(); - }); - }, - - testNumberAsyncCurrencyValue: function(test) { - test.expect(2); - var num = INumber.create("$5.80", { - type: "currency", - sync: false - }).then(function(num) { - test.ok(num !== null); - - test.equal(num.valueOf(), 5.80); - test.done(); - }); - }, - - testNumberAsyncCurrencyForLocale: function(test) { - test.expect(3); - var num = INumber.create("£5.80", { - type: "currency", - sync: false - }).then(function(num) { - test.ok(num !== null); - - var cur = num.getCurrency(); - test.ok(typeof(cur) !== "undefined"); - test.equal(cur.getCode(), "GBP"); - test.done(); - }); - }, - - testNumberAsyncParseNonLatinDigits: function(test) { - test.expect(2); - - // tests that the CType isDigit data is loaded - INumber.create("১২৩.৪৫৬", { - locale: "bn-IN", - sync: false - }).then(function(num) { - test.ok(num !== null); - test.equal(num.valueOf(), 123.456); - test.done(); - }); - }, - - testNumberAsyncParseWithSpaces: function(test) { - test.expect(2); - - // tests that CType isSpace data is loaded - INumber.create("1 234 567,745", { - locale: "fr-FR", - sync: false - }).then(function(num) { - test.ok(num !== null); - test.equal(num.valueOf(), 1234567.745); - test.done(); - }); - } -}; diff --git a/test/testphonegeopromise.js b/test/testphonegeopromise.js deleted file mode 100644 index 7b77bce..0000000 --- a/test/testphonegeopromise.js +++ /dev/null @@ -1,233 +0,0 @@ -/* - * testphonegeoasync.js - Test the GeoLocator Object. - * - * Copyright © 2018, 2022 JEDLSoft - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import PhoneNumber from "../src/PhoneNumber.js"; -import PhoneGeoLocator from "../src/PhoneGeoLocator.js"; - -export const testphonegeopromise = { - testPhoneGeoAsyncNANP: function(test) { - test.expect(6); - PhoneNumber.create("+1 650 654 3210", { - sync: false - }).then(function(parsed) { - PhoneGeoLocator.create({ - sync: false - }).then(function(locator) { - var expected = { - country: { - sn: "North America", - ln: "North America and the Caribbean Islands", - code: "US" - }, - area: { - sn: "California", - ln: "Central California: San Mateo, Palo Alto, Redwood City, Menlo Park, Mountain View, southern San Francisco suburbs" - } - }; - test.ok(typeof(locator) !== "undefined"); - - var geoInfo = locator.locate(parsed); - - test.equal(geoInfo.country.code, expected.country.code); - test.equal(geoInfo.country.sn, expected.country.sn); - test.equal(geoInfo.country.ln, expected.country.ln); - test.equal(geoInfo.area.sn, expected.area.sn); - test.equal(geoInfo.area.ln, expected.area.ln); - test.done(); - }); - }); - }, - - testPhoneGeoAsyncNANPOtherLocale: function(test) { - test.expect(6); - PhoneNumber.create("+1 650 654 3210", { - sync: false - }).then(function(parsed) { - PhoneGeoLocator.create({ - locale: 'fr-FR', - sync: false - }).then(function(locator) { - var expected = { - country: { - sn: "Amérique du Nord", - ln: "Amérique du Nord et Îles Caraïbes", - code: "US" - }, - area: { - sn: "Californie", - ln: "Californie centrale : San Mateo, Palo Alto, Redwood City, Menlo Park, Mountain View, banlieues du sud de San Francisco" - } - }; - - test.ok(typeof(locator) !== "undefined"); - var geoInfo = locator.locate(parsed); - - test.equal(geoInfo.country.code, expected.country.code); - test.equal(geoInfo.country.sn, expected.country.sn); - test.equal(geoInfo.country.ln, expected.country.ln); - test.equal(geoInfo.area.sn, expected.area.sn); - test.equal(geoInfo.area.ln, expected.area.ln); - test.done(); - }); - }); - }, - -// for bug NOV-118981 - testPhoneGeoAsyncNANPInvalidNumber: function(test) { - test.expect(6); - PhoneNumber.create("1 234", { - sync: false - }).then(function(parsed) { - PhoneGeoLocator.create({ - locale: 'en-US', - sync: false - }).then(function(locator) { - var expected = { - country: { - sn: "North America", - ln: "North America and the Caribbean Islands", - code: "US" - }, - area: { - sn: "Ohio", - ln: "Ohio" - } - }; - - test.ok(typeof(locator) !== "undefined"); - var geoInfo = locator.locate(parsed); - - test.equal(geoInfo.country.code, expected.country.code); - test.equal(geoInfo.country.sn, expected.country.sn); - test.equal(geoInfo.country.ln, expected.country.ln); - test.equal(geoInfo.area.sn, expected.area.sn); - test.equal(geoInfo.area.ln, expected.area.ln); - test.done(); - }); - }); - - }, - - testPhoneGeoAsyncDefaultDE: function(test) { - test.expect(6); - PhoneNumber.create("06224 123456", { - locale: "de-DE", - sync: false - }).then(function(parsed) { - PhoneGeoLocator.create({ - locale: 'de-DE', - sync: false - }).then(function(locator) { - var expected = { - country: { - sn: "Deutschland", - ln: "Deutschland", - code: "DE" - }, - area: { - sn: "Leimen", - ln: "Leimen, Nußloch, Sandhausen" - } - }; - - test.ok(typeof(locator) !== "undefined"); - var geoInfo = locator.locate(parsed); - - test.equal(geoInfo.country.code, expected.country.code); - test.equal(geoInfo.country.sn, expected.country.sn); - test.equal(geoInfo.country.ln, expected.country.ln); - test.equal(geoInfo.area.sn, expected.area.sn); - test.equal(geoInfo.area.ln, expected.area.ln); - test.done(); - }); - }); - }, - - testPhoneGeoAsyncDEMobileNumber: function(test) { - test.expect(6); - PhoneNumber.create("017 12345678", { - locale: "de-DE", - sync: false - }).then(function(parsed) { - PhoneGeoLocator.create({ - locale: 'de-DE', - sync: false - }).then(function(locator) { - var expected = { - country: { - sn: "Deutschland", - ln: "Deutschland", - code: "DE" - }, - area: { - sn: "Handynummer", - ln: "Handynummer" - } - }; - - test.ok(typeof(locator) !== "undefined"); - var geoInfo = locator.locate(parsed); - - test.equal(geoInfo.country.code, expected.country.code); - test.equal(geoInfo.country.sn, expected.country.sn); - test.equal(geoInfo.country.ln, expected.country.ln); - test.equal(geoInfo.area.sn, expected.area.sn); - test.equal(geoInfo.area.ln, expected.area.ln); - test.done(); - }); - }); - }, - - testPhoneGeoAsyncDefaultHK: function(test) { - test.expect(6); - PhoneNumber.create("0663 12345678", { - locale: 'en-CN', - sync: false - }).then(function(parsed) { - PhoneGeoLocator.create({ - locale: 'zh-HK', - mcc: "460", - sync: false - }).then(function(locator) { - var expected = { - country: { - sn: "中国", - ln: "中华人民共和国", - code: "CN" - }, - area: { - sn: "揭阳市", - ln: "揭阳市" - } - }; - - // give the prc mcc number so that this gives the right geo location - test.ok(typeof(locator) !== "undefined"); - var geoInfo = locator.locate(parsed); - - test.equal(geoInfo.country.code, expected.country.code); - test.equal(geoInfo.country.sn, expected.country.sn); - test.equal(geoInfo.country.ln, expected.country.ln); - test.equal(geoInfo.area.sn, expected.area.sn); - test.equal(geoInfo.area.ln, expected.area.ln); - test.done(); - }); - }); - } -}; diff --git a/test/testphonenumpromise.js b/test/testphonenumpromise.js deleted file mode 100644 index 4513c5b..0000000 --- a/test/testphonenumpromise.js +++ /dev/null @@ -1,154 +0,0 @@ -/* - * testphonenumasync.js - test phonenumber class - * - * Copyright © 2018, 2022 JEDLSoft - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import PhoneNumber from "../src/PhoneNumber.js"; - -export const testphonenumpromise = { - testPhoneParseAsyncAUFull: function(test) { - test.expect(2); - PhoneNumber.create("(08) 1234 5678", { - locale: "en-AU", - sync: false - }).then(function(parsed) { - test.ok(typeof(parsed) !== "undefined"); - - PhoneNumber.create({ - trunkAccess: "0", - areaCode: "8", - subscriberNumber: "12345678" - }, { - locale: "en-AU", - sync: false - }).then(function(expected) { - test.ok(parsed.equals(expected)); - test.done(); - }); - }); - }, - - testPhoneParseAsyncHKFromIntl: function(test) { - test.expect(2); - PhoneNumber.create("+85223897077", { - locale: "en-HK", - sync: false - }).then(function(parsed) { - test.ok(typeof(parsed) !== "undefined"); - - PhoneNumber.create({ - iddPrefix: "+", - countryCode: "852", - subscriberNumber: "23897077" - }, { - locale: "en-HK", - sync: false - }).then(function(expected) { - test.ok(parsed.equals(expected)); - test.done(); - }); - }); - }, - - testPhoneParseAsyncUSFull: function(test) { - test.expect(2); - PhoneNumber.create("(456) 345-3434", { - locale: "en-US", - sync: false - }).then(function(parsed) { - test.ok(typeof(parsed) !== "undefined"); - - PhoneNumber.create({ - areaCode: "456", - subscriberNumber: "3453434" - }, { - locale: "en-US", - sync: false - }).then(function(expected) { - test.ok(parsed.equals(expected)); - test.done(); - }); - }); - }, - - testPhoneParseAsyncKRFullLongAreaCode: function(test) { - test.expect(2); - PhoneNumber.create("033-9467-2345", { - locale: "ko-KR", - sync: false - }).then(function(parsed) { - test.ok(typeof(parsed) !== "undefined"); - - PhoneNumber.create({ - trunkAccess: "0", - areaCode: "33", - subscriberNumber: "94672345" - }, { - locale: "ko-KR", - sync: false - }).then(function(expected) { - test.ok(parsed.equals(expected)); - test.done(); - }); - }); - }, - - testPhoneParseAsyncMXFull: function(test) { - test.expect(2); - PhoneNumber.create("6241234567", { - locale: "es-MX", - sync: false - }).then(function(parsed) { - test.ok(typeof(parsed) !== "undefined"); - - PhoneNumber.create({ - areaCode: "624", - subscriberNumber: "1234567" - }, { - locale: "es-MX", - sync: false - }).then(function(expected) { - test.ok(parsed.equals(expected)); - test.done(); - }); - }); - }, - - testPhoneParseAsyncUndefined: function(test) { - test.expect(1); - PhoneNumber.create(undefined, { - locale: "en-AU", - sync: false - }).then(function(parsed) { - test.ok(typeof(parsed) === "undefined"); - - test.done(); - }); - }, - - testPhoneParseAsyncEmpty: function(test) { - test.expect(1); - PhoneNumber.create("", { - locale: "en-AU", - sync: false - }).then(function(parsed) { - test.ok(typeof(parsed) === "undefined"); - - test.done(); - }); - } -}; \ No newline at end of file diff --git a/test/testresourcesasync.js b/test/testresourcesasync.js deleted file mode 100644 index f72cfc8..0000000 --- a/test/testresourcesasync.js +++ /dev/null @@ -1,237 +0,0 @@ -/* - * testresourcesasync.js - test the Resources object - * - * Copyright © 2018, 2022-2023 JEDLSoft - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import ilib from "../src/ilib.js"; - -import ResBundle from "../src/ResBundle.js"; -import Locale from "../src/Locale.js"; - -import path from 'path'; - -import * as url from 'url'; -const __dirname = url.fileURLToPath(new URL('.', import.meta.url)); - -export const testresourcesasync = { - testResBundleAsyncConstructorEmpty: function(test) { - test.expect(2); - ilib.clearPseudoLocales(); - - new ResBundle({ - sync: false, - onLoad: function(rb) { - test.ok(rb !== null); - - test.equal(rb.getName(), "strings"); - test.done(); - } - }); - }, - - testResBundleAsyncConstructorOtherLocale: function(test) { - test.expect(2); - new ResBundle({ - locale: "de-DE", - sync: false, - onLoad: function(rb) { - test.ok(rb !== null); - - var loc = rb.getLocale(); - - test.equal(loc.toString(), "de-DE"); - test.done(); - } - }); - }, - - testResBundleAsyncGetStringOtherBundleesMX: function(test) { - if (ilib._getPlatform() !== "nodejs" || !ilib._dyndata || !ilib._dyncode) { - test.done(); - return; - } - - test.expect(4); - - // clear this to be sure it is actually loading something - ilib.data.strings = undefined; - ilib.data.strings_es = undefined; - ilib.data.strings_und_MX = undefined; - ilib.data.strings_es_MX = undefined; - ilib.clearCache(); - - var base = path.relative(process.cwd(), path.resolve(__dirname, "./resources")); - - new ResBundle({ - locale: "es-MX", - sync: false, - loadParams: { - base: base - }, - onLoad: function(rb) { - test.ok(rb !== null); - - test.equal(rb.getString("Hello from {country}").toString(), "Que tal de {country}"); - test.equal(rb.getString("Hello from {city}").toString(), "Que tal de {city}"); - test.equal(rb.getString("Greetings from {city} in {country}").toString(), "Hola de {city} en {country}"); - test.done(); - } - }); - - }, - - testResBundleAsyncGetStringOtherBundlePsuedoRaw: function(test) { - test.expect(4); - new ResBundle({ - name: "tester", - locale: "zxx-XX", - type: "raw", - sync: false, - onLoad: function(rb) { - test.ok(rb !== null); - - // should not pseudo-ize the replacement parameter names - test.equal(rb.getString("Hello from {country}").toString(), "[Ħëľľõ fŕõm {çõüñţŕÿ}]"); - test.equal(rb.getString("Hello from {city}").toString(), "[Ħëľľõ fŕõm {çíţÿ}]"); - test.equal(rb.getString("Greetings from {city} in {country}").toString(), "[Ĝŕëëţíñğš fŕõm {çíţÿ} íñ {çõüñţŕÿ}]"); - test.done(); - } - }); - }, - - testResBundleAsyncGetStringNonExistantTranslations: function(test) { - test.expect(2); - new ResBundle({ - name: "tester", - locale: "zh-CN", - sync: false, - onLoad: function(rb) { - test.ok(rb !== null); - - // should return source - test.equal(rb.getString("foobar").toString(), "foobar"); - test.done(); - } - }); - }, - - testResBundleAsyncGetStringNoResourcesReturnSource: function(test) { - test.expect(2); - new ResBundle({ - name: "tester", - locale: "zz-ZZ", - sync: false, - onLoad: function(rb) { - test.ok(rb !== null); - - test.equal(rb.getString("This is a test.").toString(), "This is a test."); - test.done(); - } - }); - }, - - testResBundleAsyncGetStringCyrlPsuedoRaw: function(test) { - test.expect(4); - new ResBundle({ - name: "tester", - locale: "zxx-Cyrl-XX", - type: "raw", - sync: false, - onLoad: function(rb) { - test.ok(rb !== null); - - // should pseudo-ize the replacement parameter names - test.equal(rb.getString("Hello from {country}").toString(), "[Хэлло фром {чоунтря}]"); - test.equal(rb.getString("Hello from {city}").toString(), "[Хэлло фром {читя}]"); - test.equal(rb.getString("Greetings from {city} in {country}").toString(), "[Грээтингс фром {читя} ин {чоунтря}]"); - test.done(); - } - }); - - }, - - testResBundleAsyncGetStringHansPsuedoText: function(test) { - test.expect(4); - new ResBundle({ - name: "tester", - locale: "zxx-Hans-XX", - type: "text", - sync: false, - onLoad: function(rb) { - test.ok(rb !== null); - - // should not pseudo-ize the replacement parameter names - // for Chinese scripts, remove the spaces to the simulate Chinese writing style - test.equal(rb.getString("Hello from {country}").toString(), "[和俄了了夥凡熱夥们{country}]"); - test.equal(rb.getString("Hello from {city}").toString(), "[和俄了了夥凡熱夥们{city}]"); - test.equal(rb.getString("Greetings from {city} in {country}").toString(), "[个熱俄俄推意尼个思凡熱夥们{city}意尼{country}]"); - test.done(); - } - }); - - }, - - testResBundleAsyncGetStringHebrPsuedoText: function(test) { - test.expect(4); - new ResBundle({ - name: "tester", - locale: "zxx-Hebr-XX", - type: "text", - sync: false, - onLoad: function(rb) { - test.ok(rb !== null); - - // should not pseudo-ize the replacement parameter names - test.equal(rb.getString("Hello from {country}").toString(), "[הֶללֹ פרֹמ {country}]"); - test.equal(rb.getString("Hello from {city}").toString(), "[הֶללֹ פרֹמ {city}]"); - test.equal(rb.getString("Greetings from {city} in {country}").toString(), "[גרֶֶטִנגס פרֹמ {city} ִנ {country}]"); - test.done(); - } - }); - - }, - - testResBundleAsyncPseudo_euES: function(test) { - test.expect(1); - ilib.clearPseudoLocales(); - ilib.setAsPseudoLocale("eu-ES"); - new ResBundle({ - locale:'eu-ES', - sync: false, - onLoad: function(rb) { - test.equal(rb.getString("This is psuedo string test").toString(), "[Ťĥíš íš þšüëðõ šţŕíñğ ţëšţ]"); - test.done(); - ilib.clearPseudoLocales(); - } - }); - }, - - testResBundleAsyncPseudo_psAF: function(test) { - test.expect(1); - ilib.clearPseudoLocales(); - ilib.setAsPseudoLocale("ps-AF"); - new ResBundle({ - locale:'ps-AF', - sync: false, - onLoad: function(rb) { - test.equal(rb.getString("This is psuedo string test").toString(), "[טהִס ִס פסֶֻדֹ סטרִנג טֶסט]"); - test.done(); - ilib.clearPseudoLocales(); - } - }); - } -}; diff --git a/test/testresourcespromise.js b/test/testresourcespromise.js deleted file mode 100644 index aa79302..0000000 --- a/test/testresourcespromise.js +++ /dev/null @@ -1,226 +0,0 @@ -/* - * testresourcesasync.js - test the Resources object - * - * Copyright © 2018, 2022-2023 JEDLSoft - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import ilib from "../src/ilib.js"; - -import ResBundle from "../src/ResBundle.js"; -import Locale from "../src/Locale.js"; - -import path from 'path'; - -import * as url from 'url'; -const __dirname = url.fileURLToPath(new URL('.', import.meta.url)); - -export const testresourcespromise = { - testResBundleAsyncConstructorEmpty: function(test) { - test.expect(2); - ilib.clearPseudoLocales(); - - ResBundle.create({ - sync: false - }).then(function(rb) { - test.ok(rb !== null); - - test.equal(rb.getName(), "strings"); - test.done(); - }); - }, - - testResBundleAsyncConstructorOtherLocale: function(test) { - test.expect(2); - ResBundle.create({ - locale: "de-DE", - sync: false - }).then(function(rb) { - test.ok(rb !== null); - - var loc = rb.getLocale(); - - test.equal(loc.toString(), "de-DE"); - test.done(); - }); - }, - - testResBundleAsyncGetStringOtherBundleesMX: function(test) { - if (ilib._getPlatform() !== "nodejs" || !ilib._dyndata || !ilib._dyncode) { - test.done(); - return; - } - - test.expect(4); - - // clear this to be sure it is actually loading something - ilib.data.strings = undefined; - ilib.data.strings_es = undefined; - ilib.data.strings_und_MX = undefined; - ilib.data.strings_es_MX = undefined; - ilib.clearCache(); - - var base = path.relative(process.cwd(), path.resolve(__dirname, "./resources")); - - ResBundle.create({ - locale: "es-MX", - sync: false, - loadParams: { - base: base - } - }).then(function(rb) { - test.ok(rb !== null); - - test.equal(rb.getStringJS("Hello from {country}"), "Que tal de {country}"); - test.equal(rb.getStringJS("Hello from {city}"), "Que tal de {city}"); - test.equal(rb.getStringJS("Greetings from {city} in {country}"), "Hola de {city} en {country}"); - test.done(); - }); - - }, - - testResBundleAsyncGetStringOtherBundlePsuedoRaw: function(test) { - test.expect(4); - ResBundle.create({ - name: "tester", - locale: "zxx-XX", - type: "raw", - sync: false - }).then(function(rb) { - test.ok(rb !== null); - - // should not pseudo-ize the replacement parameter names - test.equal(rb.getString("Hello from {country}").toString(), "[Ħëľľõ fŕõm {çõüñţŕÿ}]"); - test.equal(rb.getString("Hello from {city}").toString(), "[Ħëľľõ fŕõm {çíţÿ}]"); - test.equal(rb.getString("Greetings from {city} in {country}").toString(), "[Ĝŕëëţíñğš fŕõm {çíţÿ} íñ {çõüñţŕÿ}]"); - test.done(); - }); - }, - - testResBundleAsyncGetStringNonExistantTranslations: function(test) { - test.expect(2); - ResBundle.create({ - name: "tester", - locale: "zh-CN", - sync: false - }).then(function(rb) { - test.ok(rb !== null); - - // should return source - test.equal(rb.getString("foobar").toString(), "foobar"); - test.done(); - }); - }, - - testResBundleAsyncGetStringNoResourcesReturnSource: function(test) { - test.expect(2); - ResBundle.create({ - name: "tester", - locale: "zz-ZZ", - sync: false - }).then(function(rb) { - test.ok(rb !== null); - - test.equal(rb.getString("This is a test.").toString(), "This is a test."); - test.done(); - }); - }, - - testResBundleAsyncGetStringCyrlPsuedoRaw: function(test) { - test.expect(4); - ResBundle.create({ - name: "tester", - locale: "zxx-Cyrl-XX", - type: "raw", - sync: false - }).then(function(rb) { - test.ok(rb !== null); - - // should pseudo-ize the replacement parameter names - test.equal(rb.getString("Hello from {country}").toString(), "[Хэлло фром {чоунтря}]"); - test.equal(rb.getString("Hello from {city}").toString(), "[Хэлло фром {читя}]"); - test.equal(rb.getString("Greetings from {city} in {country}").toString(), "[Грээтингс фром {читя} ин {чоунтря}]"); - test.done(); - }); - - }, - - testResBundleAsyncGetStringHansPsuedoText: function(test) { - test.expect(4); - ResBundle.create({ - name: "tester", - locale: "zxx-Hans-XX", - type: "text", - sync: false - }).then(function(rb) { - test.ok(rb !== null); - - // should not pseudo-ize the replacement parameter names - // for Chinese scripts, remove the spaces to the simulate Chinese writing style - test.equal(rb.getString("Hello from {country}").toString(), "[和俄了了夥凡熱夥们{country}]"); - test.equal(rb.getString("Hello from {city}").toString(), "[和俄了了夥凡熱夥们{city}]"); - test.equal(rb.getString("Greetings from {city} in {country}").toString(), "[个熱俄俄推意尼个思凡熱夥们{city}意尼{country}]"); - test.done(); - }); - - }, - - testResBundleAsyncGetStringHebrPsuedoText: function(test) { - test.expect(4); - ResBundle.create({ - name: "tester", - locale: "zxx-Hebr-XX", - type: "text", - sync: false - }).then(function(rb) { - test.ok(rb !== null); - - // should not pseudo-ize the replacement parameter names - test.equal(rb.getString("Hello from {country}").toString(), "[הֶללֹ פרֹמ {country}]"); - test.equal(rb.getString("Hello from {city}").toString(), "[הֶללֹ פרֹמ {city}]"); - test.equal(rb.getString("Greetings from {city} in {country}").toString(), "[גרֶֶטִנגס פרֹמ {city} ִנ {country}]"); - test.done(); - }); - - }, - - testResBundleAsyncPseudo_euES: function(test) { - test.expect(1); - ilib.clearPseudoLocales(); - ilib.setAsPseudoLocale("eu-ES"); - ResBundle.create({ - locale:'eu-ES', - sync: false - }).then(function(rb) { - test.equal(rb.getString("This is psuedo string test").toString(), "[Ťĥíš íš þšüëðõ šţŕíñğ ţëšţ]"); - test.done(); - ilib.clearPseudoLocales(); - }); - }, - - testResBundleAsyncPseudo_psAF: function(test) { - test.expect(1); - ilib.clearPseudoLocales(); - ilib.setAsPseudoLocale("ps-AF"); - ResBundle.create({ - locale:'ps-AF', - sync: false - }).then(function(rb) { - test.equal(rb.getString("This is psuedo string test").toString(), "[טהִס ִס פסֶֻדֹ סטרִנג טֶסט]"); - test.done(); - ilib.clearPseudoLocales(); - }); - } -}; diff --git a/test/testscriptinfoasync.js b/test/testscriptinfoasync.js deleted file mode 100644 index 3342088..0000000 --- a/test/testscriptinfoasync.js +++ /dev/null @@ -1,132 +0,0 @@ -/* - * testscriptinfoasync.js - test the script info object - * - * Copyright © 2018, 2022 JEDLSoft - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import ScriptInfo from "../src/ScriptInfo.js"; - -export const testscriptinfoasync = { - testScriptInfoAsyncConstructor: function(test) { - test.expect(1); - var si = new ScriptInfo(undefined, { - sync: false, - onLoad: function(si) { - test.ok(si !== null); - test.done(); - } - }); - }, - - testScriptInfoAsyncGet1: function(test) { - test.expect(8); - var si = new ScriptInfo("Latn", { - sync: false, - onLoad: function(si) { - test.ok(si !== null); - - test.equal(si.getCode(), "Latn"); - test.equal(si.getCodeNumber(), 215); - test.equal(si.getName(), "Latin"); - test.equal(si.getLongCode(), "Latin"); - test.equal(si.getScriptDirection(), "ltr"); - test.ok(!si.getNeedsIME()); - test.ok(si.getCasing()); - test.done(); - } - }); - }, - - testScriptInfoAsyncGet4: function(test) { - test.expect(8); - var si = new ScriptInfo("Hans", { - sync: false, - onLoad: function(si) { - test.ok(si !== null); - - test.equal(si.getCode(), "Hans"); - test.equal(si.getCodeNumber(), 501); - test.equal(si.getName(), "Han (Simplified variant)"); - test.equal(si.getLongCode(), "Han_(Simplified_variant)"); - test.equal(si.getScriptDirection(), "ltr"); - test.ok(si.getNeedsIME()); - test.ok(!si.getCasing()); - test.done(); - } - }); - }, - - testScriptInfoAsyncGetDefaultLongCodeArab: function(test) { - test.expect(8); - var si = new ScriptInfo("Arab", { - sync: false, - onLoad: function(si) { - test.ok(si !== null); - - test.equal(si.getCode(), "Arab"); - test.equal(si.getCodeNumber(), 160); - test.equal(si.getName(), "Arabic"); - test.equal(si.getLongCode(), "Arabic"); - test.equal(si.getScriptDirection(), "rtl"); - test.ok(!si.getNeedsIME()); - test.ok(!si.getCasing()); - test.done(); - } - }); - }, - testScriptInfoAsyncGetUnknown: function(test) { - test.expect(5); - var si = new ScriptInfo("Fooo", { - sync: false, - onLoad: function(si) { - test.ok(si !== null); - - test.equal(si.getCode(), undefined); - test.equal(si.getCodeNumber(), 0); - test.equal(si.getName(), undefined); - test.equal(si.getLongCode(), undefined); - test.done(); - } - }); - }, - - testScriptInfoAsyncGetAllScripts: function(test) { - test.expect(11); - ScriptInfo.getAllScripts(false, undefined, function(scripts) { - test.ok(scripts !== null); - - test.equal(scripts.length, 213); - - test.equal(scripts[0], "Adlm"); - test.equal(scripts[1], "Afak"); - test.equal(scripts[2], "Aghb"); - test.equal(scripts[4], "Arab"); - test.equal(scripts[scripts.length-1], "Zzzz"); - - // make sure the callback is called after the 2nd call - ScriptInfo.getAllScripts(false, undefined, function(scripts) { - test.ok(scripts !== null); - - test.equal(scripts.length, 213); - - test.equal(scripts[0], "Adlm"); - test.equal(scripts[scripts.length-1], "Zzzz"); - - test.done(); - }); - }); - } -}; diff --git a/test/testscriptinfopromise.js b/test/testscriptinfopromise.js deleted file mode 100644 index 46a6c6b..0000000 --- a/test/testscriptinfopromise.js +++ /dev/null @@ -1,127 +0,0 @@ -/* - * testscriptinfoasync.js - test the script info object - * - * Copyright © 2018, 2022 JEDLSoft - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import ScriptInfo from "../src/ScriptInfo.js"; - -export const testscriptinfopromise = { - testScriptInfoAsyncConstructor: function(test) { - test.expect(1); - var si = ScriptInfo.create(undefined, { - sync: false - }).then(function(si) { - test.ok(si !== null); - test.done(); - }); - }, - - testScriptInfoAsyncGet1: function(test) { - test.expect(8); - var si = ScriptInfo.create("Latn", { - sync: false - }).then(function(si) { - test.ok(si !== null); - - test.equal(si.getCode(), "Latn"); - test.equal(si.getCodeNumber(), 215); - test.equal(si.getName(), "Latin"); - test.equal(si.getLongCode(), "Latin"); - test.equal(si.getScriptDirection(), "ltr"); - test.ok(!si.getNeedsIME()); - test.ok(si.getCasing()); - test.done(); - }); - }, - - testScriptInfoAsyncGet4: function(test) { - test.expect(8); - var si = ScriptInfo.create("Hans", { - sync: false - }).then(function(si) { - test.ok(si !== null); - - test.equal(si.getCode(), "Hans"); - test.equal(si.getCodeNumber(), 501); - test.equal(si.getName(), "Han (Simplified variant)"); - test.equal(si.getLongCode(), "Han_(Simplified_variant)"); - test.equal(si.getScriptDirection(), "ltr"); - test.ok(si.getNeedsIME()); - test.ok(!si.getCasing()); - test.done(); - }); - }, - - testScriptInfoAsyncGetDefaultLongCodeArab: function(test) { - test.expect(8); - var si = ScriptInfo.create("Arab", { - sync: false - }).then(function(si) { - test.ok(si !== null); - - test.equal(si.getCode(), "Arab"); - test.equal(si.getCodeNumber(), 160); - test.equal(si.getName(), "Arabic"); - test.equal(si.getLongCode(), "Arabic"); - test.equal(si.getScriptDirection(), "rtl"); - test.ok(!si.getNeedsIME()); - test.ok(!si.getCasing()); - test.done(); - }); - }, - testScriptInfoAsyncGetUnknown: function(test) { - test.expect(5); - var si = ScriptInfo.create("Fooo", { - sync: false - }).then(function(si) { - test.ok(si !== null); - - test.equal(si.getCode(), undefined); - test.equal(si.getCodeNumber(), 0); - test.equal(si.getName(), undefined); - test.equal(si.getLongCode(), undefined); - test.done(); - }); - }, - - testScriptInfoAsyncGetAllScripts: function(test) { - test.expect(11); - ScriptInfo.getAllScripts(false, undefined, function(scripts) { - test.ok(scripts !== null); - - test.equal(scripts.length, 213); - - test.equal(scripts[0], "Adlm"); - test.equal(scripts[1], "Afak"); - test.equal(scripts[2], "Aghb"); - test.equal(scripts[4], "Arab"); - test.equal(scripts[scripts.length-1], "Zzzz"); - - // make sure the callback is called after the 2nd call - ScriptInfo.getAllScripts(false, undefined, function(scripts) { - test.ok(scripts !== null); - - test.equal(scripts.length, 213); - - test.equal(scripts[0], "Adlm"); - test.equal(scripts[scripts.length-1], "Zzzz"); - - test.done(); - }); - }); - } -}; diff --git a/test/teststringsasync.js b/test/teststringsasync.js deleted file mode 100644 index c4d79a5..0000000 --- a/test/teststringsasync.js +++ /dev/null @@ -1,88 +0,0 @@ -/* - * teststringsasync.js - test the String object - * - * Copyright © 2018, 2022 JEDLSoft - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import NormString from "../src/NormString.js"; -import Locale from "../src/Locale.js"; -import IString from "../src/IString.js"; - -export const teststringsasync = { - testStringLoadPlurals: function(test) { - test.expect(1); - IString.loadPlurals(false, undefined, undefined, function() { - var str = new IString("asdf"); - - test.ok(str !== null); - test.done(); - }) - }, - - testStringSetLocale: function(test) { - test.expect(2); - var str = new IString("1#first string|2#second string"); - str.setLocale("de-DE", false, undefined, function() { - test.ok(str !== null); - - test.equal(str.toString(), "1#first string|2#second string"); - test.done(); - }) - }, - - testStringFormatChoiceSimpleNoPlurals: function(test) { - test.expect(2); - var str = new IString("1#first string|2#second string"); - - test.ok(str !== null); - - // should default to English rules - test.equal(str.formatChoice(1), "first string"); - test.done(); - }, - - testStringFormatChoiceSimpleRussian: function(test) { - test.expect(3); - var str = new IString("1#first string|few#second string|many#third string"); - str.setLocale("ru-RU", false, undefined, function() { - test.ok(str !== null); - - test.equal(str.formatChoice(2), "second string"); - test.equal(str.formatChoice(5), "third string"); - test.done(); - }); - }, - - testStringFormatChoiceSimpleRussianTwice: function(test) { - test.expect(4); - var str = new IString("1#first string|few#second string|many#third string"); - str.setLocale("ru-RU", false, undefined, function() { - test.ok(str !== null); - - test.equal(str.formatChoice(5), "third string"); - str = new IString("1#single|few#few|many#many"); - - // Russian rules should already be loaded. Need to make sure - // the callback is still called anyways - str.setLocale("ru-RU", false, undefined, function() { - test.ok(str !== null); - test.equal(str.formatChoice(5), "many"); - test.done(); - }); - }); - }, - -}; diff --git a/test/teststringspromise.js b/test/teststringspromise.js deleted file mode 100644 index 1e98ade..0000000 --- a/test/teststringspromise.js +++ /dev/null @@ -1,88 +0,0 @@ -/* - * teststringsasync.js - test the String object - * - * Copyright © 2018, 2022 JEDLSoft - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import NormString from "../src/NormString.js"; -import Locale from "../src/Locale.js"; -import IString from "../src/IString.js"; - -export const teststringspromise = { - testStringLoadPlurals: function(test) { - test.expect(1); - IString.loadPlurals(false, undefined).then(function() { - var str = new IString("asdf"); - - test.ok(str !== null); - test.done(); - }); - }, - - testStringSetLocale: function(test) { - test.expect(2); - var str = new IString("1#first string|2#second string"); - str.setLocale("de-DE", false).then(function() { - test.ok(str !== null); - - test.equal(str.toString(), "1#first string|2#second string"); - test.done(); - }); - }, - - testStringFormatChoiceSimpleNoPlurals: function(test) { - test.expect(2); - var str = new IString("1#first string|2#second string"); - - test.ok(str !== null); - - // should default to English rules - test.equal(str.formatChoice(1), "first string"); - test.done(); - }, - - testStringFormatChoiceSimpleRussian: function(test) { - test.expect(3); - var str = new IString("1#first string|few#second string|many#third string"); - str.setLocale("ru-RU", false).then(function() { - test.ok(str !== null); - - test.equal(str.formatChoice(2), "second string"); - test.equal(str.formatChoice(5), "third string"); - test.done(); - }); - }, - - testStringFormatChoiceSimpleRussianTwice: function(test) { - test.expect(4); - var str = new IString("1#first string|few#second string|many#third string"); - str.setLocale("ru-RU", false).then(function() { - test.ok(str !== null); - - test.equal(str.formatChoice(5), "third string"); - str = new IString("1#single|few#few|many#many"); - - // Russian rules should already be loaded. Need to make sure - // the callback is still called anyways - str.setLocale("ru-RU", false).then(function() { - test.ok(str !== null); - test.equal(str.formatChoice(5), "many"); - test.done(); - }); - }); - }, - -}; diff --git a/test/testtimezoneasync.js b/test/testtimezoneasync.js deleted file mode 100644 index d785798..0000000 --- a/test/testtimezoneasync.js +++ /dev/null @@ -1,166 +0,0 @@ -/* - * testtimezoneasync.js - test the timezone objects asynchronously - * - * Copyright © 2018, 2022 JEDLSoft - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import TimeZone from "../src/TimeZone.js"; - -export const testtimezoneasync = { - testTZAsyncGetAvailableIds: function(test) { - test.expect(2); - TimeZone.getAvailableIds(undefined, false, function(zones) { - test.ok(typeof(zones) !== "undefined"); - - test.ok(zones.length > 0); - test.done(); - }); - }, - - testTZAsyncGetAvailableIdsRightValues: function(test) { - test.expect(6); - TimeZone.getAvailableIds(undefined, false, function(zones) { - test.ok(typeof(zones) !== "undefined"); - - //var util = require("util"); - test.contains(zones, "Europe/London"); - test.contains(zones, "America/Los_Angeles"); - test.contains(zones, "Australia/Sydney"); - test.contains(zones, "Asia/Tokyo"); - test.contains(zones, "Africa/Cairo"); - test.done(); - }); - }, - - testTZAsyncGetAvailableIdsNoFilterContainsLocal: function(test) { - test.expect(2); - TimeZone.getAvailableIds(undefined, false, function(zones) { - test.ok(typeof(zones) !== "undefined"); - - test.ok(zones.indexOf("local") != -1); - test.done(); - }); - }, - - testTZAsyncGetAvailableIdsByCountryRightLength: function(test) { - test.expect(2); - TimeZone.getAvailableIds("US", false, function(zones) { - test.ok(typeof(zones) !== "undefined"); - - test.equal(zones.length, 48); - test.done(); - }); - }, - - testTZAsyncGetAvailableIdsWithFilterContainsNoLocal: function(test) { - try { - TimeZone.getAvailableIds("US", false, function(zones) { - test.ok(typeof(zones) !== "undefined"); - - test.ok(zones.indexOf("local") == -1); - }); - } catch (e) { - test.ok(typeof(e) !== "undefined"); - } - test.done(); - }, - - testTZAsyncGetAvailableIdsByCountryRightContents: function(test) { - test.expect(2); - TimeZone.getAvailableIds("US", false, function(zones) { - test.ok(typeof(zones) !== "undefined"); - - var expected = [ - "America/New_York", - "America/Detroit", - "America/Kentucky/Louisville", - "America/Kentucky/Monticello", - "America/Indiana/Indianapolis", - "America/Indiana/Vincennes", - "America/Indiana/Winamac", - "America/Indiana/Marengo", - "America/Indiana/Petersburg", - "America/Indiana/Vevay", - "America/Chicago", - "America/Indiana/Tell_City", - "America/Indiana/Knox", - "America/Menominee", - "America/North_Dakota/Center", - "America/North_Dakota/New_Salem", - "America/North_Dakota/Beulah", - "America/Denver", - "America/Boise", - "America/Phoenix", - "America/Los_Angeles", - "America/Anchorage", - "America/Juneau", - "America/Sitka", - "America/Metlakatla", - "America/Yakutat", - "America/Nome", - "America/Adak", - "Pacific/Honolulu", - "America/Atka", - "America/Fort_Wayne", - "America/Indianapolis", - "America/Knox_IN", - "America/Louisville", - "America/Shiprock", - "Navajo", - "Pacific/Johnston", - "US/Alaska", - "US/Aleutian", - "US/Arizona", - "US/Central", - "US/East-Indiana", - "US/Eastern", - "US/Hawaii", - "US/Indiana-Starke", - "US/Michigan", - "US/Mountain", - "US/Pacific" - ]; - - test.equalIgnoringOrder(zones, expected); - test.done(); - }); - }, - - testTZAsyncGetAvailableIdsByCountry2RightLength: function(test) { - test.expect(2); - var zones = TimeZone.getAvailableIds("SG", false, function(zones) { - test.ok(typeof(zones) !== "undefined"); - - test.equal(zones.length, 2); - test.done(); - }); - }, - - testTZAsyncGetAvailableIdsByCountry2RightContents: function(test) { - test.expect(2); - var zones = TimeZone.getAvailableIds("SG", false, function(zones) { - test.ok(typeof(zones) !== "undefined"); - - var expected = [ - "Asia/Singapore", - "Singapore" // legacy tz - ]; - - test.equalIgnoringOrder(zones, expected); - test.done(); - }); - } -}; diff --git a/test/testtimezonepromise.js b/test/testtimezonepromise.js deleted file mode 100644 index 1f64f65..0000000 --- a/test/testtimezonepromise.js +++ /dev/null @@ -1,166 +0,0 @@ -/* - * testtimezoneasync.js - test the timezone objects asynchronously - * - * Copyright © 2018, 2022 JEDLSoft - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import TimeZone from "../src/TimeZone.js"; - -export const testtimezonepromise = { - testTZAsyncGetAvailableIds: function(test) { - test.expect(2); - TimeZone.getAvailableIds(undefined, false).then(function(zones) { - test.ok(typeof(zones) !== "undefined"); - - test.ok(zones.length > 0); - test.done(); - }); - }, - - testTZAsyncGetAvailableIdsRightValues: function(test) { - test.expect(6); - TimeZone.getAvailableIds(undefined, false).then(function(zones) { - test.ok(typeof(zones) !== "undefined"); - - //var util = require("util"); - test.contains(zones, "Europe/London"); - test.contains(zones, "America/Los_Angeles"); - test.contains(zones, "Australia/Sydney"); - test.contains(zones, "Asia/Tokyo"); - test.contains(zones, "Africa/Cairo"); - test.done(); - }); - }, - - testTZAsyncGetAvailableIdsNoFilterContainsLocal: function(test) { - test.expect(2); - TimeZone.getAvailableIds(undefined, false).then(function(zones) { - test.ok(typeof(zones) !== "undefined"); - - test.ok(zones.indexOf("local") != -1); - test.done(); - }); - }, - - testTZAsyncGetAvailableIdsByCountryRightLength: function(test) { - test.expect(2); - TimeZone.getAvailableIds("US", false).then(function(zones) { - test.ok(typeof(zones) !== "undefined"); - - test.equal(zones.length, 48); - test.done(); - }); - }, - - testTZAsyncGetAvailableIdsWithFilterContainsNoLocal: function(test) { - try { - TimeZone.getAvailableIds("US", false).then(function(zones) { - test.ok(typeof(zones) !== "undefined"); - - test.ok(zones.indexOf("local") == -1); - }); - } catch (e) { - test.ok(typeof(e) !== "undefined"); - } - test.done(); - }, - - testTZAsyncGetAvailableIdsByCountryRightContents: function(test) { - test.expect(2); - TimeZone.getAvailableIds("US", false).then(function(zones) { - test.ok(typeof(zones) !== "undefined"); - - var expected = [ - "America/New_York", - "America/Detroit", - "America/Kentucky/Louisville", - "America/Kentucky/Monticello", - "America/Indiana/Indianapolis", - "America/Indiana/Vincennes", - "America/Indiana/Winamac", - "America/Indiana/Marengo", - "America/Indiana/Petersburg", - "America/Indiana/Vevay", - "America/Chicago", - "America/Indiana/Tell_City", - "America/Indiana/Knox", - "America/Menominee", - "America/North_Dakota/Center", - "America/North_Dakota/New_Salem", - "America/North_Dakota/Beulah", - "America/Denver", - "America/Boise", - "America/Phoenix", - "America/Los_Angeles", - "America/Anchorage", - "America/Juneau", - "America/Sitka", - "America/Metlakatla", - "America/Yakutat", - "America/Nome", - "America/Adak", - "Pacific/Honolulu", - "America/Atka", - "America/Fort_Wayne", - "America/Indianapolis", - "America/Knox_IN", - "America/Louisville", - "America/Shiprock", - "Navajo", - "Pacific/Johnston", - "US/Alaska", - "US/Aleutian", - "US/Arizona", - "US/Central", - "US/East-Indiana", - "US/Eastern", - "US/Hawaii", - "US/Indiana-Starke", - "US/Michigan", - "US/Mountain", - "US/Pacific" - ]; - - test.equalIgnoringOrder(zones, expected); - test.done(); - }); - }, - - testTZAsyncGetAvailableIdsByCountry2RightLength: function(test) { - test.expect(2); - var zones = TimeZone.getAvailableIds("SG", false).then(function(zones) { - test.ok(typeof(zones) !== "undefined"); - - test.equal(zones.length, 2); - test.done(); - }); - }, - - testTZAsyncGetAvailableIdsByCountry2RightContents: function(test) { - test.expect(2); - var zones = TimeZone.getAvailableIds("SG", false).then(function(zones) { - test.ok(typeof(zones) !== "undefined"); - - var expected = [ - "Asia/Singapore", - "Singapore" // legacy tz - ]; - - test.equalIgnoringOrder(zones, expected); - test.done(); - }); - } -}; diff --git a/test/testunitfmtasync.js b/test/testunitfmtasync.js deleted file mode 100644 index c9afc4c..0000000 --- a/test/testunitfmtasync.js +++ /dev/null @@ -1,193 +0,0 @@ -/* - * testunitsasync.js - test the units formatter object - * - * Copyright © 2018-2019, 2022 JEDLSoft - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import UnitFmt from "../src/UnitFmt.js"; -import MeasurementFactory from "../src/MeasurementFactory.js"; - -export const testunitfmtasync = { - testUnitFormatWithScale1: function(test) { - test.expect(1); - var m = MeasurementFactory({ - amount: 3000, - unit: "meter" - }); - new UnitFmt({ - autoConvert: false, - sync: false, - onLoad: function(fmt) { - var str = fmt.format(m); - test.equal(str, "3 kilometers"); - test.done(); - } - }); - }, - - testUnitFormatWithoutScale5: function(test) { - test.expect(1); - var m1 = MeasurementFactory({ - unit: "kilowatt hour", - amount: 1233453 - }); - - new UnitFmt({ - autoScale: false, - autoConvert:false, - sync: false, - onLoad: function(fmt) { - var str = fmt.format(m1); - test.equal(str, "1,233,453 kilowatt-hours"); - test.done(); - } - }); - }, - - testUnitFormatWithScale11: function(test) { - test.expect(1); - var m1 = MeasurementFactory({ - unit: "krunghoonfoop", - amount: 2 - }); - - new UnitFmt({ - autoConvert:false, - sync: false, - onLoad: function(fmt) { - var str = fmt.format(m1); - test.equal(str, "2 krunghoonfoop"); - test.done(); - } - }); - }, - - testUnitFormatArea2: function(test) { - test.expect(1); - var m1 = MeasurementFactory({ - unit: "square centimeter", - amount: 2 - }); - - new UnitFmt({ - locale: "ru-RU", - autoConvert: false, - sync: false, - onLoad: function(fmt) { - var str = fmt.format(m1); - test.equal(str, "2 квадратных сантиметра"); - test.done(); - }}); - }, - - testUnitFormatArea4: function(test) { - test.expect(1); - var m1 = MeasurementFactory({ - unit: "square centimeter", - amount: 1000 - }); - - new UnitFmt({ - locale: "ko-KR", - autoConvert: false, - sync: false, - onLoad: function(fmt) { - var str = fmt.format(m1); - test.equal(str, "1,000제곱센티미터"); - test.done(); - } - }); - }, - - testUnitFormatArea5: function(test) { - test.expect(1); - var m1 = MeasurementFactory({ - unit: "square centimeter", - amount: 1000 - }); - - new UnitFmt({ - locale: "fr-FR", - autoConvert: false, - sync: false, - onLoad: function(fmt) { - var str = fmt.format(m1); - test.equal(str, "1 000 centimètres carrés"); - test.done(); - } - }); - }, - - testUnitFormatPower: function(test) { - test.expect(1); - var m1 = MeasurementFactory({ - unit: "kW", - amount: 1000 - }); - - new UnitFmt({ - locale: "fr-FR", - autoConvert: false, - sync: false, - onLoad: function(fmt) { - var str = fmt.format(m1); - test.equal(str, "1 mégawatt"); - test.done(); - } - }); - }, - - testUnitFormatPressure: function(test) { - test.expect(1); - var m1 = MeasurementFactory({ - unit: "Pa", - amount: 1000 - }); - - new UnitFmt({ - locale: "de-DE", - autoConvert: false, - autoScale: false, - sync: false, - length: "long", - onLoad: function(fmt) { - var str = fmt.format(m1); - test.equal(str, "1.000 Pascal"); - test.done(); - } - }); - }, - - testUnitFormatForce: function(test) { - test.expect(1); - var m1 = MeasurementFactory({ - unit: "N", - amount: 100 - }); - - new UnitFmt({ - locale: "ja-JP", - autoConvert: false, - sync: false, - length: "long", - onLoad: function(fmt) { - var str = fmt.format(m1); - test.equal(str, "100 ニュートン"); - test.done(); - } - }); - } -}; diff --git a/test/testunitfmtpromise.js b/test/testunitfmtpromise.js deleted file mode 100644 index 41f2e16..0000000 --- a/test/testunitfmtpromise.js +++ /dev/null @@ -1,186 +0,0 @@ -/* - * testunitfmtpromise.js - test the units formatter object - * - * Copyright © 2018-2019, 2022 JEDLSoft - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import UnitFmt from "../src/UnitFmt.js"; -import MeasurementFactory from "../src/MeasurementFactory.js"; - -export const testunitfmtpromise = { - testUnitFormatWithScale1: function(test) { - test.expect(1); - var m = MeasurementFactory({ - amount: 3000, - unit: "meter" - }); - UnitFmt.create({ - autoConvert: false, - sync: false - }).then(function(fmt) { - var str = fmt.format(m); - test.equal(str, "3 kilometers"); - test.done(); - }); - }, - - testUnitFormatWithoutScale5: function(test) { - test.expect(1); - var m1 = MeasurementFactory({ - unit: "kilowatt hour", - amount: 1233453 - }); - - UnitFmt.create({ - autoScale: false, - autoConvert:false, - sync: false - }).then(function(fmt) { - var str = fmt.format(m1); - test.equal(str, "1,233,453 kilowatt-hours"); - test.done(); - }); - }, - - testUnitFormatWithScale11: function(test) { - test.expect(1); - var m1 = MeasurementFactory({ - unit: "krunghoonfoop", - amount: 2 - }); - - UnitFmt.create({ - autoConvert:false, - sync: false - }).then(function(fmt) { - var str = fmt.format(m1); - test.equal(str, "2 krunghoonfoop"); - test.done(); - }); - }, - - testUnitFormatArea2: function(test) { - test.expect(1); - var m1 = MeasurementFactory({ - unit: "square centimeter", - amount: 2 - }); - - UnitFmt.create({ - locale: "ru-RU", - autoConvert: false, - sync: false - }).then(function(fmt) { - var str = fmt.format(m1); - test.equal(str, "2 квадратных сантиметра"); - test.done(); - }); - }, - - testUnitFormatArea4: function(test) { - test.expect(1); - var m1 = MeasurementFactory({ - unit: "square centimeter", - amount: 1000 - }); - - UnitFmt.create({ - locale: "ko-KR", - autoConvert: false, - sync: false - }).then(function(fmt) { - var str = fmt.format(m1); - test.equal(str, "1,000제곱센티미터"); - test.done(); - }); - }, - - testUnitFormatArea5: function(test) { - test.expect(1); - var m1 = MeasurementFactory({ - unit: "square centimeter", - amount: 1000 - }); - - UnitFmt.create({ - locale: "fr-FR", - autoConvert: false, - scale: false, - sync: false - }).then(function(fmt) { - var str = fmt.format(m1); - test.equal(str, "1 000 centimètres carrés"); - test.done(); - }); - }, - - testUnitFormatPower: function(test) { - test.expect(1); - var m1 = MeasurementFactory({ - unit: "kW", - amount: 1000 - }); - - UnitFmt.create({ - locale: "fr-FR", - autoConvert: false, - sync: false - }).then(function(fmt) { - var str = fmt.format(m1); - test.equal(str, "1 mégawatt"); - test.done(); - }); - }, - - testUnitFormatPressure: function(test) { - test.expect(1); - var m1 = MeasurementFactory({ - unit: "Pa", - amount: 1000 - }); - - UnitFmt.create({ - locale: "de-DE", - autoConvert: false, - autoScale: false, - sync: false, - length: "long" - }).then(function(fmt) { - var str = fmt.format(m1); - test.equal(str, "1.000 Pascal"); - test.done(); - }); - }, - - testUnitFormatForce: function(test) { - test.expect(1); - var m1 = MeasurementFactory({ - unit: "N", - amount: 100 - }); - - UnitFmt.create({ - locale: "ja-JP", - autoConvert: false, - sync: false, - length: "long" - }).then(function(fmt) { - var str = fmt.format(m1); - test.equal(str, "100 ニュートン"); - test.done(); - }); - } -}; diff --git a/test/timezone.test.js b/test/timezone.test.js new file mode 100644 index 0000000..c390d19 --- /dev/null +++ b/test/timezone.test.js @@ -0,0 +1,159 @@ +/* + * timezone.test.js - test the timezone static methods synchronously + * + * Copyright © 2024 JEDLSoft + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import ilib from '../src/ilib.js'; +import TimeZone from "../src/TimeZone.js"; + +describe("test timezone synchronously", () => { + beforeEach(() => { + ilib.clearCache(); + }); + + test("TZAsyncGetAvailableIds", () => { + expect.assertions(2); + const zones = TimeZone.getAvailableIds(); + expect(typeof(zones) !== "undefined").toBeTruthy(); + + expect(zones.length > 0).toBeTruthy(); + }); + + test("TZAsyncGetAvailableIdsRightValues", () => { + expect.assertions(2); + const zones = TimeZone.getAvailableIds(); + expect(typeof(zones) !== "undefined").toBeTruthy(); + + // not the full list, but the array should at least contain these + const expected = [ + "Europe/London", + "America/Los_Angeles", + "Australia/Sydney", + "Asia/Tokyo", + "Africa/Cairo" + ]; + + expect(zones).toEqual(expect.arrayContaining(expected)); + }); + + test("TZAsyncGetAvailableIdsNoFilterContainsLocal", () => { + expect.assertions(2); + const zones = TimeZone.getAvailableIds(); + expect(typeof(zones) !== "undefined").toBeTruthy(); + + expect(zones.indexOf("local") != -1).toBeTruthy(); + }); + + test("TZAsyncGetAvailableIdsByCountryRightLength", () => { + expect.assertions(2); + const zones = TimeZone.getAvailableIds("US"); + expect(typeof(zones) !== "undefined").toBeTruthy(); + + expect(zones.length).toBe(48); + }); + + test("TZAsyncGetAvailableIdsWithFilterContainsNoLocal", () => { + try { + const zones = TimeZone.getAvailableIds("US"); + expect(typeof(zones) !== "undefined").toBeTruthy(); + + expect(zones.indexOf("local") == -1).toBeTruthy(); + } catch (e) { + expect(typeof(e) !== "undefined").toBeTruthy(); + } + }); + + test("TZAsyncGetAvailableIdsByCountryRightContents", () => { + expect.assertions(2); + const zones = TimeZone.getAvailableIds("US"); + expect(typeof(zones) !== "undefined").toBeTruthy(); + + const expected = [ + "America/New_York", + "America/Detroit", + "America/Kentucky/Louisville", + "America/Kentucky/Monticello", + "America/Indiana/Indianapolis", + "America/Indiana/Vincennes", + "America/Indiana/Winamac", + "America/Indiana/Marengo", + "America/Indiana/Petersburg", + "America/Indiana/Vevay", + "America/Chicago", + "America/Indiana/Tell_City", + "America/Indiana/Knox", + "America/Menominee", + "America/North_Dakota/Center", + "America/North_Dakota/New_Salem", + "America/North_Dakota/Beulah", + "America/Denver", + "America/Boise", + "America/Phoenix", + "America/Los_Angeles", + "America/Anchorage", + "America/Juneau", + "America/Sitka", + "America/Metlakatla", + "America/Yakutat", + "America/Nome", + "America/Adak", + "Pacific/Honolulu", + "America/Atka", + "America/Fort_Wayne", + "America/Indianapolis", + "America/Knox_IN", + "America/Louisville", + "America/Shiprock", + "Navajo", + "Pacific/Johnston", + "US/Alaska", + "US/Aleutian", + "US/Arizona", + "US/Central", + "US/East-Indiana", + "US/Eastern", + "US/Hawaii", + "US/Indiana-Starke", + "US/Michigan", + "US/Mountain", + "US/Pacific" + ]; + + expect(zones).toEqual(expect.arrayContaining(expected)); + }); + + test("TZAsyncGetAvailableIdsByCountry2RightLength", () => { + expect.assertions(2); + const zones = TimeZone.getAvailableIds("SG"); + expect(typeof(zones) !== "undefined").toBeTruthy(); + + expect(zones.length).toBe(2); + }); + + test("TZAsyncGetAvailableIdsByCountry2RightContents", () => { + expect.assertions(2); + const zones = TimeZone.getAvailableIds("SG"); + expect(typeof(zones) !== "undefined").toBeTruthy(); + + const expected = [ + "Asia/Singapore", + "Singapore" // legacy tz + ]; + + expect(zones).toEqual(expect.arrayContaining(expected)); + }); +}); diff --git a/test/timezoneasync.test.js b/test/timezoneasync.test.js new file mode 100644 index 0000000..e7b5007 --- /dev/null +++ b/test/timezoneasync.test.js @@ -0,0 +1,167 @@ +/* + * timezoneasync.test.js - test the timezone objects asynchronously + * + * Copyright © 2018, 2022, 2024 JEDLSoft + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import ilib from '../src/ilib.js'; +import TimeZone from "../src/TimeZone.js"; + +describe("testtimezoneasync", () => { + beforeEach(() => { + ilib.clearCache(); + }); + + test("TZAsyncGetAvailableIds", () => { + expect.assertions(2); + TimeZone.getAvailableIds(undefined, false, zones => { + expect(typeof(zones) !== "undefined").toBeTruthy(); + + expect(zones.length > 0).toBeTruthy(); + }); + }); + + test("TZAsyncGetAvailableIdsRightValues", () => { + expect.assertions(2); + TimeZone.getAvailableIds(undefined, false, zones => { + expect(typeof(zones) !== "undefined").toBeTruthy(); + + // not the full list, but the array should at least contain these + const expected = [ + "Europe/London", + "America/Los_Angeles", + "Australia/Sydney", + "Asia/Tokyo", + "Africa/Cairo" + ]; + + expect(zones).toEqual(expect.arrayContaining(expected)); + }); + }); + + test("TZAsyncGetAvailableIdsNoFilterContainsLocal", () => { + expect.assertions(2); + TimeZone.getAvailableIds(undefined, false, zones => { + expect(typeof(zones) !== "undefined").toBeTruthy(); + + expect(zones.indexOf("local") != -1).toBeTruthy(); + }); + }); + + test("TZAsyncGetAvailableIdsByCountryRightLength", () => { + expect.assertions(2); + TimeZone.getAvailableIds("US", false, zones => { + expect(typeof(zones) !== "undefined").toBeTruthy(); + + expect(zones.length).toBe(48); + }); + }); + + test("TZAsyncGetAvailableIdsWithFilterContainsNoLocal", () => { + try { + TimeZone.getAvailableIds("US", false, zones => { + expect(typeof(zones) !== "undefined").toBeTruthy(); + + expect(zones.indexOf("local") == -1).toBeTruthy(); + }); + } catch (e) { + expect(typeof(e) !== "undefined").toBeTruthy(); + } + }); + + test("TZAsyncGetAvailableIdsByCountryRightContents", () => { + expect.assertions(2); + TimeZone.getAvailableIds("US", false, zones => { + expect(typeof(zones) !== "undefined").toBeTruthy(); + + const expected = [ + "America/New_York", + "America/Detroit", + "America/Kentucky/Louisville", + "America/Kentucky/Monticello", + "America/Indiana/Indianapolis", + "America/Indiana/Vincennes", + "America/Indiana/Winamac", + "America/Indiana/Marengo", + "America/Indiana/Petersburg", + "America/Indiana/Vevay", + "America/Chicago", + "America/Indiana/Tell_City", + "America/Indiana/Knox", + "America/Menominee", + "America/North_Dakota/Center", + "America/North_Dakota/New_Salem", + "America/North_Dakota/Beulah", + "America/Denver", + "America/Boise", + "America/Phoenix", + "America/Los_Angeles", + "America/Anchorage", + "America/Juneau", + "America/Sitka", + "America/Metlakatla", + "America/Yakutat", + "America/Nome", + "America/Adak", + "Pacific/Honolulu", + "America/Atka", + "America/Fort_Wayne", + "America/Indianapolis", + "America/Knox_IN", + "America/Louisville", + "America/Shiprock", + "Navajo", + "Pacific/Johnston", + "US/Alaska", + "US/Aleutian", + "US/Arizona", + "US/Central", + "US/East-Indiana", + "US/Eastern", + "US/Hawaii", + "US/Indiana-Starke", + "US/Michigan", + "US/Mountain", + "US/Pacific" + ]; + + expect(zones).toEqual(expect.arrayContaining(expected)); + }); + }); + + test("TZAsyncGetAvailableIdsByCountry2RightLength", () => { + expect.assertions(2); + const zones = TimeZone.getAvailableIds("SG", false, zones => { + expect(typeof(zones) !== "undefined").toBeTruthy(); + + expect(zones.length).toBe(2); + }); + }); + + test("TZAsyncGetAvailableIdsByCountry2RightContents", () => { + expect.assertions(2); + const zones = TimeZone.getAvailableIds("SG", false, zones => { + expect(typeof(zones) !== "undefined").toBeTruthy(); + + const expected = [ + "Asia/Singapore", + "Singapore" // legacy tz + ]; + + expect(zones).toEqual(expect.arrayContaining(expected)); + }); + }); +}); diff --git a/test/timezonepromise.test.js b/test/timezonepromise.test.js new file mode 100644 index 0000000..f825dbb --- /dev/null +++ b/test/timezonepromise.test.js @@ -0,0 +1,167 @@ +/* + * timezoneasync.test.js - test the timezone objects asynchronously + * + * Copyright © 2018, 2022, 2024 JEDLSoft + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import ilib from '../src/ilib.js'; +import TimeZone from "../src/TimeZone.js"; + +describe("testtimezonepromise", () => { + beforeEach(() => { + ilib.clearCache(); + }); + + test("TZAsyncGetAvailableIds", () => { + expect.assertions(2); + return TimeZone.getAvailableIds(undefined, false).then(zones => { + expect(typeof(zones) !== "undefined").toBeTruthy(); + + expect(zones.length > 0).toBeTruthy(); + }); + }); + + test("TZAsyncGetAvailableIdsRightValues", () => { + expect.assertions(2); + return TimeZone.getAvailableIds(undefined, false).then(zones => { + expect(typeof(zones) !== "undefined").toBeTruthy(); + + // not the full list, but the array should at least contain these + const expected = [ + "Europe/London", + "America/Los_Angeles", + "Australia/Sydney", + "Asia/Tokyo", + "Africa/Cairo" + ]; + + expect(zones).toEqual(expect.arrayContaining(expected)); + }); + }); + + test("TZAsyncGetAvailableIdsNoFilterContainsLocal", () => { + expect.assertions(2); + return TimeZone.getAvailableIds(undefined, false).then(zones => { + expect(typeof(zones) !== "undefined").toBeTruthy(); + + expect(zones.indexOf("local") != -1).toBeTruthy(); + }); + }); + + test("TZAsyncGetAvailableIdsByCountryRightLength", () => { + expect.assertions(2); + return TimeZone.getAvailableIds("US", false).then(zones => { + expect(typeof(zones) !== "undefined").toBeTruthy(); + + expect(zones.length).toBe(48); + }); + }); + + test("TZAsyncGetAvailableIdsWithFilterContainsNoLocal", () => { + try { + return TimeZone.getAvailableIds("US", false).then(zones => { + expect(typeof(zones) !== "undefined").toBeTruthy(); + + expect(zones.indexOf("local") == -1).toBeTruthy(); + }); + } catch (e) { + expect(typeof(e) !== "undefined").toBeTruthy(); + } + }); + + test("TZAsyncGetAvailableIdsByCountryRightContents", () => { + expect.assertions(2); + return TimeZone.getAvailableIds("US", false).then(zones => { + expect(typeof(zones) !== "undefined").toBeTruthy(); + + const expected = [ + "America/New_York", + "America/Detroit", + "America/Kentucky/Louisville", + "America/Kentucky/Monticello", + "America/Indiana/Indianapolis", + "America/Indiana/Vincennes", + "America/Indiana/Winamac", + "America/Indiana/Marengo", + "America/Indiana/Petersburg", + "America/Indiana/Vevay", + "America/Chicago", + "America/Indiana/Tell_City", + "America/Indiana/Knox", + "America/Menominee", + "America/North_Dakota/Center", + "America/North_Dakota/New_Salem", + "America/North_Dakota/Beulah", + "America/Denver", + "America/Boise", + "America/Phoenix", + "America/Los_Angeles", + "America/Anchorage", + "America/Juneau", + "America/Sitka", + "America/Metlakatla", + "America/Yakutat", + "America/Nome", + "America/Adak", + "Pacific/Honolulu", + "America/Atka", + "America/Fort_Wayne", + "America/Indianapolis", + "America/Knox_IN", + "America/Louisville", + "America/Shiprock", + "Navajo", + "Pacific/Johnston", + "US/Alaska", + "US/Aleutian", + "US/Arizona", + "US/Central", + "US/East-Indiana", + "US/Eastern", + "US/Hawaii", + "US/Indiana-Starke", + "US/Michigan", + "US/Mountain", + "US/Pacific" + ]; + + expect(zones).toEqual(expect.arrayContaining(expected)); + }); + }); + + test("TZAsyncGetAvailableIdsByCountry2RightLength", () => { + expect.assertions(2); + return TimeZone.getAvailableIds("SG", false).then(zones => { + expect(typeof(zones) !== "undefined").toBeTruthy(); + + expect(zones.length).toBe(2); + }); + }); + + test("TZAsyncGetAvailableIdsByCountry2RightContents", () => { + expect.assertions(2); + return TimeZone.getAvailableIds("SG", false).then(zones => { + expect(typeof(zones) !== "undefined").toBeTruthy(); + + const expected = [ + "Asia/Singapore", + "Singapore" // legacy tz + ]; + + expect(zones).toEqual(expect.arrayContaining(expected)); + }); + }); +}); diff --git a/test/unitfmtasync.test.js b/test/unitfmtasync.test.js new file mode 100644 index 0000000..3a2e107 --- /dev/null +++ b/test/unitfmtasync.test.js @@ -0,0 +1,184 @@ +/* + * unitsasync.test.js - test the units formatter object + * + * Copyright © 2018-2019, 2022, 2024 JEDLSoft + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import UnitFmt from "../src/UnitFmt.js"; +import MeasurementFactory from "../src/MeasurementFactory.js"; + +describe("testunitfmtasync", () => { + test("UnitFormatWithScale1", () => { + expect.assertions(1); + const m = MeasurementFactory({ + amount: 3000, + unit: "meter" + }); + new UnitFmt({ + autoConvert: false, + sync: false, + onLoad: fmt => { + const str = fmt.format(m); + expect(str).toBe("3 kilometers"); + } + }); + }); + + test("UnitFormatWithoutScale5", () => { + expect.assertions(1); + const m1 = MeasurementFactory({ + unit: "kilowatt hour", + amount: 1233453 + }); + + new UnitFmt({ + autoScale: false, + autoConvert:false, + sync: false, + onLoad: fmt => { + const str = fmt.format(m1); + expect(str).toBe("1,233,453 kilowatt-hours"); + } + }); + }); + + test("UnitFormatWithScale11", () => { + expect.assertions(1); + const m1 = MeasurementFactory({ + unit: "krunghoonfoop", + amount: 2 + }); + + new UnitFmt({ + autoConvert:false, + sync: false, + onLoad: fmt => { + const str = fmt.format(m1); + expect(str).toBe("2 krunghoonfoop"); + } + }); + }); + + test("UnitFormatArea2", () => { + expect.assertions(1); + const m1 = MeasurementFactory({ + unit: "square centimeter", + amount: 2 + }); + + new UnitFmt({ + locale: "ru-RU", + autoConvert: false, + sync: false, + onLoad: fmt => { + const str = fmt.format(m1); + expect(str).toBe("2 квадратных сантиметра"); + }}); + }); + + test("UnitFormatArea4", () => { + expect.assertions(1); + const m1 = MeasurementFactory({ + unit: "square centimeter", + amount: 1000 + }); + + new UnitFmt({ + locale: "ko-KR", + autoConvert: false, + sync: false, + onLoad: fmt => { + const str = fmt.format(m1); + expect(str).toBe("1,000제곱센티미터"); + } + }); + }); + + test("UnitFormatArea5", () => { + expect.assertions(1); + const m1 = MeasurementFactory({ + unit: "square centimeter", + amount: 1000 + }); + + new UnitFmt({ + locale: "fr-FR", + autoConvert: false, + sync: false, + onLoad: fmt => { + const str = fmt.format(m1); + expect(str).toBe("1 000 centimètres carrés"); + } + }); + }); + + test("UnitFormatPower", () => { + expect.assertions(1); + const m1 = MeasurementFactory({ + unit: "kW", + amount: 1000 + }); + + new UnitFmt({ + locale: "fr-FR", + autoConvert: false, + sync: false, + onLoad: fmt => { + const str = fmt.format(m1); + expect(str).toBe("1 mégawatt"); + } + }); + }); + + test("UnitFormatPressure", () => { + expect.assertions(1); + const m1 = MeasurementFactory({ + unit: "Pa", + amount: 1000 + }); + + new UnitFmt({ + locale: "de-DE", + autoConvert: false, + autoScale: false, + sync: false, + length: "long", + onLoad: fmt => { + const str = fmt.format(m1); + expect(str).toBe("1.000 Pascal"); + } + }); + }); + + test("UnitFormatForce", () => { + expect.assertions(1); + const m1 = MeasurementFactory({ + unit: "N", + amount: 100 + }); + + new UnitFmt({ + locale: "ja-JP", + autoConvert: false, + sync: false, + length: "long", + onLoad: fmt => { + const str = fmt.format(m1); + expect(str).toBe("100 ニュートン"); + } + }); + }); +}); diff --git a/test/unitfmtpromise.test.js b/test/unitfmtpromise.test.js new file mode 100644 index 0000000..857f324 --- /dev/null +++ b/test/unitfmtpromise.test.js @@ -0,0 +1,177 @@ +/* + * unitfmtpromise.test.js - test the units formatter object + * + * Copyright © 2018-2019, 2022, 2024 JEDLSoft + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import UnitFmt from "../src/UnitFmt.js"; +import MeasurementFactory from "../src/MeasurementFactory.js"; + +describe("testunitfmtpromise", () => { + test("UnitFormatWithScale1", () => { + expect.assertions(1); + const m = MeasurementFactory({ + amount: 3000, + unit: "meter" + }); + return UnitFmt.create({ + autoConvert: false, + sync: false + }).then(fmt => { + const str = fmt.format(m); + expect(str).toBe("3 kilometers"); + }); + }); + + test("UnitFormatWithoutScale5", () => { + expect.assertions(1); + const m1 = MeasurementFactory({ + unit: "kilowatt hour", + amount: 1233453 + }); + + return UnitFmt.create({ + autoScale: false, + autoConvert:false, + sync: false + }).then(fmt => { + const str = fmt.format(m1); + expect(str).toBe("1,233,453 kilowatt-hours"); + }); + }); + + test("UnitFormatWithScale11", () => { + expect.assertions(1); + const m1 = MeasurementFactory({ + unit: "krunghoonfoop", + amount: 2 + }); + + return UnitFmt.create({ + autoConvert:false, + sync: false + }).then(fmt => { + const str = fmt.format(m1); + expect(str).toBe("2 krunghoonfoop"); + }); + }); + + test("UnitFormatArea2", () => { + expect.assertions(1); + const m1 = MeasurementFactory({ + unit: "square centimeter", + amount: 2 + }); + + return UnitFmt.create({ + locale: "ru-RU", + autoConvert: false, + sync: false + }).then(fmt => { + const str = fmt.format(m1); + expect(str).toBe("2 квадратных сантиметра"); + }); + }); + + test("UnitFormatArea4", () => { + expect.assertions(1); + const m1 = MeasurementFactory({ + unit: "square centimeter", + amount: 1000 + }); + + return UnitFmt.create({ + locale: "ko-KR", + autoConvert: false, + sync: false + }).then(fmt => { + const str = fmt.format(m1); + expect(str).toBe("1,000제곱센티미터"); + }); + }); + + test("UnitFormatArea5", () => { + expect.assertions(1); + const m1 = MeasurementFactory({ + unit: "square centimeter", + amount: 1000 + }); + + return UnitFmt.create({ + locale: "fr-FR", + autoConvert: false, + scale: false, + sync: false + }).then(fmt => { + const str = fmt.format(m1); + expect(str).toBe("1 000 centimètres carrés"); + }); + }); + + test("UnitFormatPower", () => { + expect.assertions(1); + const m1 = MeasurementFactory({ + unit: "kW", + amount: 1000 + }); + + return UnitFmt.create({ + locale: "fr-FR", + autoConvert: false, + sync: false + }).then(fmt => { + const str = fmt.format(m1); + expect(str).toBe("1 mégawatt"); + }); + }); + + test("UnitFormatPressure", () => { + expect.assertions(1); + const m1 = MeasurementFactory({ + unit: "Pa", + amount: 1000 + }); + + return UnitFmt.create({ + locale: "de-DE", + autoConvert: false, + autoScale: false, + sync: false, + length: "long" + }).then(fmt => { + const str = fmt.format(m1); + expect(str).toBe("1.000 Pascal"); + }); + }); + + test("UnitFormatForce", () => { + expect.assertions(1); + const m1 = MeasurementFactory({ + unit: "N", + amount: 100 + }); + + return UnitFmt.create({ + locale: "ja-JP", + autoConvert: false, + sync: false, + length: "long" + }).then(fmt => { + const str = fmt.format(m1); + expect(str).toBe("100 ニュートン"); + }); + }); +});