From fd48f18f8aa05a362f3168cbddcc03659e7510db Mon Sep 17 00:00:00 2001 From: Kuuuube <61125188+Kuuuube@users.noreply.github.com> Date: Mon, 12 Feb 2024 01:10:52 -0500 Subject: Add and fix tests with options upgrades (#671) * Add and fix tests and options applying * Fix comma * Add test file to json.json * Fix direction of conjugation handlebars update * Add frequency and cloze-body-kana to upgrade * Use Array.isArray() instead of arbitrary length check * Add pitch-accent-categories handlebar * programmatically get upgrade files * add type --------- Co-authored-by: Cashew --- test/data/json.json | 1 + test/data/vitest.options.config.json | 7 ++ test/options-util.test.js | 122 +++++++++++++++++++++++++++++++---- 3 files changed, 119 insertions(+), 11 deletions(-) create mode 100644 test/data/vitest.options.config.json (limited to 'test') diff --git a/test/data/json.json b/test/data/json.json index 1f664587..a99f53a3 100644 --- a/test/data/json.json +++ b/test/data/json.json @@ -23,6 +23,7 @@ {"path": "test/data/dictionaries/invalid-dictionary6/index.json", "ignore": true}, {"path": "test/jsconfig.json", "ignore": true}, {"path": "test/data/vitest.write.config.json", "ignore": true}, + {"path": "test/data/vitest.options.config.json", "ignore": true}, {"path": "benches/jsconfig.json", "ignore": true}, { diff --git a/test/data/vitest.options.config.json b/test/data/vitest.options.config.json new file mode 100644 index 00000000..e2bca721 --- /dev/null +++ b/test/data/vitest.options.config.json @@ -0,0 +1,7 @@ +{ + "test": { + "include": [ + "../**/options-util.test.js" + ] + } +} diff --git a/test/options-util.test.js b/test/options-util.test.js index 4a75fa14..8fe0e212 100644 --- a/test/options-util.test.js +++ b/test/options-util.test.js @@ -663,19 +663,25 @@ async function testFieldTemplatesUpdate() { * @returns {string} */ const loadDataFile = (fileName) => { - const content = fs.readFileSync(path.join(dirname, '..', 'ext', fileName), {encoding: 'utf8'}); + const content = fs.readFileSync(fileName, {encoding: 'utf8'}); return templatePatcher.parsePatch(content).addition; }; - const updates = [ - {version: 2, changes: loadDataFile('data/templates/anki-field-templates-upgrade-v2.handlebars')}, - {version: 4, changes: loadDataFile('data/templates/anki-field-templates-upgrade-v4.handlebars')}, - {version: 6, changes: loadDataFile('data/templates/anki-field-templates-upgrade-v6.handlebars')}, - {version: 8, changes: loadDataFile('data/templates/anki-field-templates-upgrade-v8.handlebars')}, - {version: 10, changes: loadDataFile('data/templates/anki-field-templates-upgrade-v10.handlebars')}, - {version: 12, changes: loadDataFile('data/templates/anki-field-templates-upgrade-v12.handlebars')}, - {version: 13, changes: loadDataFile('data/templates/anki-field-templates-upgrade-v13.handlebars')}, - {version: 21, changes: loadDataFile('data/templates/anki-field-templates-upgrade-v21.handlebars')} - ]; + + /** @type {import('options-util').TemplateFieldUpdate[]} */ + const updates = []; + const fileNameRegex = /^anki-field-templates-upgrade-v(\d+)\.handlebars$/; + const templatesDirPath = path.join(dirname, '..', 'ext', 'data', 'templates'); + const templatesDir = fs.readdirSync(templatesDirPath, {encoding: 'utf8'}); + for (const fileName of templatesDir) { + const match = fileNameRegex.exec(fileName); + if (match !== null) { + updates.push({ + version: Number.parseInt(match[1]), + changes: loadDataFile(path.join(templatesDirPath, match[0])) + }); + } + } + updates.sort((a, b) => a.version - b.version); /** * @param {number} startVersion * @param {number} targetVersion @@ -1569,6 +1575,100 @@ async function testFieldTemplatesUpdate() { {{/inline}} {{~> (lookup . "marker") ~}}`.trimStart() + }, + { + oldVersion: 21, + newVersion: 24, + old: ` +{{#*inline "conjugation"}} + {{~#if definition.reasons~}} + {{~#each definition.reasons~}} + {{~#if (op ">" @index 0)}} « {{/if~}} + {{.}} + {{~/each~}} + {{~/if~}} +{{/inline}}`.trimStart(), + + expected: ` +{{#*inline "conjugation"}} + {{~#if (op ">" definition.inflectionRuleChainCandidates.length 0)~}} + {{~set "multiple" false~}} + {{~#if (op ">" definition.inflectionRuleChainCandidates.length 1)~}} + {{~set "multiple" true~}} + {{~/if~}} + {{~#if (get "multiple")~}}{{/if~}} + {{~/if~}} +{{/inline}} +{{#*inline "cloze-body-kana"}} + {{~#if definition.cloze}}{{definition.cloze.bodyKana}}{{/if~}} +{{/inline}} + +{{#*inline "phonetic-transcriptions"}} + {{~#if (op ">" definition.phoneticTranscriptions.length 0)~}} + + {{~/if~}} +{{/inline}} +{{#*inline "frequency-harmonic-rank"}} + {{~#if (op "===" definition.frequencyHarmonic -1) ~}} + 9999999 + {{~else ~}} + {{definition.frequencyHarmonic}} + {{~/if~}} +{{/inline}} + +{{#*inline "frequency-harmonic-occurrence"}} + {{~#if (op "===" definition.frequencyHarmonic -1) ~}} + 0 + {{~else ~}} + {{definition.frequencyHarmonic}} + {{~/if~}} +{{/inline}} + +{{#*inline "frequency-average-rank"}} + {{~#if (op "===" definition.frequencyAverage -1) ~}} + 9999999 + {{~else ~}} + {{definition.frequencyAverage}} + {{~/if~}} +{{/inline}} + +{{#*inline "frequency-average-occurrence"}} + {{~#if (op "===" definition.frequencyAverage -1) ~}} + 0 + {{~else ~}} + {{definition.frequencyAverage}} + {{~/if~}} +{{/inline}} + +{{~#*inline "pitch-accent-categories"~}} + {{~#each (pitchCategories @root)~}}{{~.~}}{{~#unless @last~}},{{~/unless~}}{{~/each~}} +{{~/inline~}}`.trimStart() } ]; -- cgit v1.2.3