diff --git a/locale-tests/all.test.js b/locale-tests/all.test.js new file mode 100644 index 00000000..1993f332 --- /dev/null +++ b/locale-tests/all.test.js @@ -0,0 +1,81 @@ +import { formatNumber, parseNumber, formatDate, parseDate, firstDay, weekendRange, setData } from '../src/main'; +import { LOCALES, NO_CURRENCY_LOCALE, clean } from './utils'; + +describe('generated-locales', () => { + const date = new Date(2000, 0, 1); + const number = 5.55; + const numberString = '5.55'; + + describe.each(LOCALES)('%s (all)', (locale) => { + beforeAll(() => { + const all = require(`./locales/${ locale }/all`).default; + setData(all); + }); + + afterAll(() => { + clean(); + }); + + it('format number', () => { + expect(() => { + formatNumber(number, 'n', locale); + }).not.toThrow(); + }); + + it('parse number', () => { + expect(() => { + parseNumber(numberString, locale, 'n'); + }).not.toThrow(); + }); + + if (locale !== NO_CURRENCY_LOCALE) { + it('format currency', () => { + expect(() => { + formatNumber(number, 'c', locale); + }).not.toThrow(); + }); + + it('parse currency', () => { + expect(() => { + parseNumber(numberString, locale, 'c'); + }).not.toThrow(); + }); + + it('format accounting', () => { + expect(() => { + formatNumber(number, 'a', locale); + }).not.toThrow(); + }); + + it('parse accounting', () => { + expect(() => { + parseNumber(numberString, locale, 'a'); + }).not.toThrow(); + }); + } + + it('format date', () => { + expect(() => { + formatDate(date, 'F', locale); + }).not.toThrow(); + }); + + it('parse date', () => { + expect(() => { + parseDate(formatDate(date, 'F'), 'F', locale); + }).not.toThrow(); + }); + + it('firstDay', () => { + expect(() => { + firstDay(locale); + }).not.toThrow(); + }); + + it('weekendRange', () => { + expect(() => { + weekendRange(locale); + }).not.toThrow(); + }); + }); +}); diff --git a/locale-tests/calendar.test.js b/locale-tests/calendar.test.js new file mode 100644 index 00000000..5092ef1d --- /dev/null +++ b/locale-tests/calendar.test.js @@ -0,0 +1,41 @@ +import { formatDate, parseDate, firstDay, weekendRange, setData } from '../src/main'; +import { LOCALES, clean } from './utils'; + +describe('generated-locales', () => { + const date = new Date(2000, 0, 1); + + describe.each(LOCALES)('%s (calendar)', (locale) => { + beforeAll(() => { + const calendar = require(`./locales/${ locale }/calendar`).default; + setData(calendar); + }); + + afterAll(() => { + clean(); + }); + + it('format', () => { + expect(() => { + formatDate(date, 'F', locale); + }).not.toThrow(); + }); + + it('parse', () => { + expect(() => { + parseDate(formatDate(date, 'F'), 'F', locale); + }).not.toThrow(); + }); + + it('firstDay', () => { + expect(() => { + firstDay(locale); + }).not.toThrow(); + }); + + it('weekendRange', () => { + expect(() => { + weekendRange(locale); + }).not.toThrow(); + }); + }); +}); diff --git a/locale-tests/currency.test.js b/locale-tests/currency.test.js new file mode 100644 index 00000000..54247631 --- /dev/null +++ b/locale-tests/currency.test.js @@ -0,0 +1,45 @@ +import { formatNumber, parseNumber, setData } from '../src/main'; +import { LOCALES, clean, NO_CURRENCY_LOCALE } from './utils'; + +const number = 5.55; +const numberString = '5.55'; +const currencyLocales = LOCALES.filter(locale => locale !== NO_CURRENCY_LOCALE); + +describe.each(currencyLocales)('%s currency', (locale) => { + beforeAll(() => { + const numbers = require(`./locales/${ locale }/numbers`).default; + const currencies = require(`./locales/${ locale }/currencies`).default; + + setData(numbers); + setData(currencies); + + }); + + afterAll(() => { + clean(); + }); + + it('format', () => { + expect(() => { + formatNumber(number, 'c', locale); + }).not.toThrow(); + }); + + it('parse', () => { + expect(() => { + parseNumber(numberString, locale, 'c'); + }).not.toThrow(); + }); + + it('format accounting', () => { + expect(() => { + formatNumber(number, 'a', locale); + }).not.toThrow(); + }); + + it('parse accounting', () => { + expect(() => { + parseNumber(numberString, locale, 'a'); + }).not.toThrow(); + }); +}); diff --git a/locale-tests/generated-locales.test.js b/locale-tests/generated-locales.test.js deleted file mode 100644 index 663e96d7..00000000 --- a/locale-tests/generated-locales.test.js +++ /dev/null @@ -1,209 +0,0 @@ -import { cldr, formatNumber, parseNumber, formatDate, parseDate, firstDay, weekendRange, setData } from '../src/main'; - -const fs = require('fs'); -const path = require('path'); -const locales = fs.readdirSync(path.join('./node_modules', 'cldr-localenames-full', 'main')); -const defaultDataClone = clone(cldr); -const NO_CURRENCY_LOCALE = 'es-419'; - -function clone(obj) { - return JSON.parse(JSON.stringify(obj)); -} - -function clean() { - for (let field in cldr) { - delete cldr[field]; - } - Object.assign(cldr, clone(defaultDataClone)); -} - -describe('generated-locales', () => { - const date = new Date(2000, 0, 1); - const number = 5.55; - const numberString = '5.55'; - - locales.forEach((locale) => { - if (locale === 'root') { - return; - } - - describe(`${ locale }`, () => { - - describe(`numbers`, () => { - beforeAll(() => { - const numbers = require(`./locales/${ locale }/numbers`).default; - setData(numbers); - }); - - afterAll(() => { - clean(); - }); - - it('format', () => { - expect(() => { - formatNumber(number, 'n', locale); - }).not.toThrow(); - }); - - it('parse', () => { - expect(() => { - parseNumber(numberString, locale, 'n'); - }).not.toThrow(); - }); - - }); - - if (locale !== NO_CURRENCY_LOCALE) { - - describe(`currency`, () => { - beforeAll(() => { - const numbers = require(`./locales/${ locale }/numbers`).default; - const currencies = require(`./locales/${ locale }/currencies`).default; - - setData(numbers); - setData(currencies); - - }); - - afterAll(() => { - clean(); - }); - - it('format', () => { - expect(() => { - formatNumber(number, 'c', locale); - }).not.toThrow(); - }); - - it('parse', () => { - expect(() => { - parseNumber(numberString, locale, 'c'); - }).not.toThrow(); - }); - - it('format accounting', () => { - expect(() => { - formatNumber(number, 'a', locale); - }).not.toThrow(); - }); - - it('parse accounting', () => { - expect(() => { - parseNumber(numberString, locale, 'a'); - }).not.toThrow(); - }); - - }); - } - - describe(`calendar`, () => { - beforeAll(() => { - const calendar = require(`./locales/${ locale }/calendar`).default; - setData(calendar); - }); - - afterAll(() => { - clean(); - }); - - it('format', () => { - expect(() => { - formatDate(date, 'F', locale); - }).not.toThrow(); - }); - - it('parse', () => { - expect(() => { - parseDate(formatDate(date, 'F'), 'F', locale); - }).not.toThrow(); - }); - - it('firstDay', () => { - expect(() => { - firstDay(locale); - }).not.toThrow(); - }); - - it('weekendRange', () => { - expect(() => { - weekendRange(locale); - }).not.toThrow(); - }); - }); - - describe(`all`, () => { - beforeAll(() => { - const all = require(`./locales/${ locale }/all`).default; - setData(all); - }); - - afterAll(() => { - clean(); - }); - - it('format number', () => { - expect(() => { - formatNumber(number, 'n', locale); - }).not.toThrow(); - }); - - it('parse number', () => { - expect(() => { - parseNumber(numberString, locale, 'n'); - }).not.toThrow(); - }); - if (locale !== NO_CURRENCY_LOCALE) { - - it('format currency', () => { - expect(() => { - formatNumber(number, 'c', locale); - }).not.toThrow(); - }); - - it('parse currency', () => { - expect(() => { - parseNumber(numberString, locale, 'c'); - }).not.toThrow(); - }); - - it('format accounting', () => { - expect(() => { - formatNumber(number, 'a', locale); - }).not.toThrow(); - }); - - it('parse accounting', () => { - expect(() => { - parseNumber(numberString, locale, 'a'); - }).not.toThrow(); - }); - } - - it('format date', () => { - expect(() => { - formatDate(date, 'F', locale); - }).not.toThrow(); - }); - - it('parse date', () => { - expect(() => { - parseDate(formatDate(date, 'F'), 'F', locale); - }).not.toThrow(); - }); - - it('firstDay', () => { - expect(() => { - firstDay(locale); - }).not.toThrow(); - }); - - it('weekendRange', () => { - expect(() => { - weekendRange(locale); - }).not.toThrow(); - }); - }); - - }); - }); -}); diff --git a/locale-tests/numbers.test.js b/locale-tests/numbers.test.js new file mode 100644 index 00000000..772e8edb --- /dev/null +++ b/locale-tests/numbers.test.js @@ -0,0 +1,30 @@ +import { formatNumber, parseNumber, setData } from '../src/main'; +import { LOCALES, clean } from './utils'; + +describe('generated-locales', () => { + const number = 5.55; + const numberString = '5.55'; + + describe.each(LOCALES)('%s numbers', (locale) => { + beforeAll(() => { + const numbers = require(`./locales/${ locale }/numbers`).default; + setData(numbers); + }); + + afterAll(() => { + clean(); + }); + + it('format', () => { + expect(() => { + formatNumber(number, 'n', locale); + }).not.toThrow(); + }); + + it('parse', () => { + expect(() => { + parseNumber(numberString, locale, 'n'); + }).not.toThrow(); + }); + }); +}); diff --git a/locale-tests/utils.js b/locale-tests/utils.js new file mode 100644 index 00000000..870a191f --- /dev/null +++ b/locale-tests/utils.js @@ -0,0 +1,22 @@ +import { cldr } from '../src/main'; + +const fs = require('fs'); +const path = require('path'); +const defaultDataClone = clone(cldr); + +function clone(obj) { + return JSON.parse(JSON.stringify(obj)); +} + +export function clean() { + for (let field in cldr) { + delete cldr[field]; + } + Object.assign(cldr, clone(defaultDataClone)); +} + +export const LOCALES = + fs.readdirSync(path.join('./node_modules', 'cldr-localenames-full', 'main')) + .filter(locale => locale !== 'root'); + +export const NO_CURRENCY_LOCALE = 'es-419';