Skip to content

Commit

Permalink
Add and fix tests with options upgrades (#671)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
Kuuuube and Casheeew authored Feb 12, 2024
1 parent 92f6652 commit fd48f18
Show file tree
Hide file tree
Showing 7 changed files with 181 additions and 23 deletions.
56 changes: 48 additions & 8 deletions ext/data/templates/anki-field-templates-upgrade-v24.handlebars
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
{{#*inline "cloze-body-kana"}}
{{~#if definition.cloze}}{{definition.cloze.bodyKana}}{{/if~}}
{{/inline}}

{{#*inline "phonetic-transcriptions"}}
{{~#if (op ">" definition.phoneticTranscriptions.length 0)~}}
<ul>
Expand All @@ -20,6 +24,15 @@
{{/inline}}

{{<<<<<<<}}
{{#*inline "conjugation"}}
{{~#if definition.reasons~}}
{{~#each definition.reasons~}}
{{~#if (op ">" @index 0)}} « {{/if~}}
{{.}}
{{~/each~}}
{{~/if~}}
{{/inline}}
{{=======}}
{{#*inline "conjugation"}}
{{~#if (op ">" definition.inflectionRuleChainCandidates.length 0)~}}
{{~set "multiple" false~}}
Expand All @@ -40,13 +53,40 @@
{{~#if (get "multiple")~}}</ul>{{/if~}}
{{~/if~}}
{{/inline}}
{{=======}}
{{#*inline "conjugation"}}
{{~#if definition.reasons~}}
{{~#each definition.reasons~}}
{{~#if (op ">" @index 0)}} « {{/if~}}
{{.}}
{{~/each~}}
{{>>>>>>>}}

{{#*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~}}
12 changes: 8 additions & 4 deletions ext/js/data/options-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -1115,8 +1115,10 @@ export class OptionsUtil {
*/
_updateVersion23(options) {
for (const {options: profileOptions} of options.profiles) {
for (const dictionary of profileOptions.dictionaries) {
dictionary.partsOfSpeechFilter = true;
if (Array.isArray(profileOptions.dictionaries)) {
for (const dictionary of profileOptions.dictionaries) {
dictionary.partsOfSpeechFilter = true;
}
}
}
}
Expand All @@ -1129,8 +1131,10 @@ export class OptionsUtil {
await this._applyAnkiFieldTemplatesPatch(options, '/data/templates/anki-field-templates-upgrade-v24.handlebars');

for (const {options: profileOptions} of options.profiles) {
for (const dictionary of profileOptions.dictionaries) {
dictionary.useDeinflections = true;
if (Array.isArray(profileOptions.dictionaries)) {
for (const dictionary of profileOptions.dictionaries) {
dictionary.useDeinflections = true;
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"test-ts-test": "npx tsc --noEmit --project test/jsconfig.json",
"test-code": "vitest run",
"test-code-write": "vitest run --config test/data/vitest.write.config.json",
"test-options-update": "vitest run --config test/data/vitest.options.config.json",
"test-build": "node ./dev/bin/build.js --dryRun --all",
"license-report": "license-report --output=html --only=prod --fields=name --fields=installedVersion --fields=licenseType --fields=link --html.cssFile=dev/data/legal-npm.css > ext/legal-npm.html",
"license-report-markdown": "license-report --output=markdown --only=prod --fields=name --fields=installedVersion --fields=licenseType --fields=link",
Expand Down
1 change: 1 addition & 0 deletions test/data/json.json
Original file line number Diff line number Diff line change
Expand Up @@ -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},

{
Expand Down
7 changes: 7 additions & 0 deletions test/data/vitest.options.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"test": {
"include": [
"../**/options-util.test.js"
]
}
}
122 changes: 111 additions & 11 deletions test/options-util.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")~}}<ul>{{/if~}}
{{~#each definition.inflectionRuleChainCandidates~}}
{{~#if (op ">" inflectionRules.length 0)~}}
{{~#if (get "multiple")~}}<li>{{/if~}}
{{~#each inflectionRules~}}
{{~#if (op ">" @index 0)}} « {{/if~}}
{{.}}
{{~/each~}}
{{~#if (get "multiple")~}}</li>{{/if~}}
{{~/if~}}
{{~/each~}}
{{~#if (get "multiple")~}}</ul>{{/if~}}
{{~/if~}}
{{/inline}}
{{#*inline "cloze-body-kana"}}
{{~#if definition.cloze}}{{definition.cloze.bodyKana}}{{/if~}}
{{/inline}}
{{#*inline "phonetic-transcriptions"}}
{{~#if (op ">" definition.phoneticTranscriptions.length 0)~}}
<ul>
{{~#each definition.phoneticTranscriptions~}}
{{~#each phoneticTranscriptions~}}
<li>
{{~set "any" false~}}
{{~#each tags~}}
{{~#if (get "any")}}, {{else}}<i>({{/if~}}
{{name}}
{{~set "any" true~}}
{{~/each~}}
{{~#if (get "any")}})</i> {{/if~}}
{{ipa~}}
</li>
{{~/each~}}
{{~/each~}}
</ul>
{{~/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()
}
];

Expand Down
5 changes: 5 additions & 0 deletions types/ext/options-util.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,8 @@ export type IntermediateOptions = Core.SafeAny;
export type LegacyUpdateFunction = (options: LegacyOptions) => void;

export type UpdateFunction = (options: IntermediateOptions) => void | Promise<void>;

export type TemplateFieldUpdate = {
version: number;
changes: string;
};

0 comments on commit fd48f18

Please sign in to comment.