aboutsummaryrefslogtreecommitdiff
path: root/test/test-translator.js
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2021-03-15 20:55:26 -0400
committerGitHub <noreply@github.com>2021-03-15 20:55:26 -0400
commitcba45b5e304afe0623e1d032d65e5efc2fc3929b (patch)
tree0b8475f8dcf84198619dbda7d5ef793443d52564 /test/test-translator.js
parent9ad9961ca81d2ee41c26d2c2bc2a399abad1bac5 (diff)
Improve translator test data (#1531)
* Organize files * Split translator test data into two files * Add test for AnkiNoteData
Diffstat (limited to 'test/test-translator.js')
-rw-r--r--test/test-translator.js96
1 files changed, 66 insertions, 30 deletions
diff --git a/test/test-translator.js b/test/test-translator.js
index 307329d0..ef431c46 100644
--- a/test/test-translator.js
+++ b/test/test-translator.js
@@ -37,28 +37,32 @@ async function createVM() {
const vm = new DatabaseVM();
vm.execute([
'js/core.js',
- 'js/general/cache-map.js',
- 'js/language/japanese-util.js',
- 'js/data/json-schema.js',
- 'js/media/media-util.js',
- 'js/language/dictionary-importer.js',
+ 'js/data/anki-note-data.js',
'js/data/database.js',
- 'js/language/dictionary-database.js',
+ 'js/data/json-schema.js',
+ 'js/general/cache-map.js',
'js/general/regex-util.js',
'js/general/text-source-map.js',
'js/language/deinflector.js',
- 'js/language/translator.js'
+ 'js/language/dictionary-data-util.js',
+ 'js/language/dictionary-importer.js',
+ 'js/language/dictionary-database.js',
+ 'js/language/japanese-util.js',
+ 'js/language/translator.js',
+ 'js/media/media-util.js'
]);
const [
DictionaryImporter,
DictionaryDatabase,
JapaneseUtil,
- Translator
+ Translator,
+ AnkiNoteData
] = vm.get([
'DictionaryImporter',
'DictionaryDatabase',
'JapaneseUtil',
- 'Translator'
+ 'Translator',
+ 'AnkiNoteData'
]);
// Dictionary
@@ -85,8 +89,11 @@ async function createVM() {
const deinflectionReasions = JSON.parse(fs.readFileSync(path.join(__dirname, '..', 'ext', 'data/deinflect.json')));
translator.prepare(deinflectionReasions);
+ // Note data creation
+ const createPublicAnkiNoteData = (marker, data) => new AnkiNoteData(japaneseUtil, marker, data).createPublic();
+
// Done
- return {vm, translator, dictionary: result};
+ return {vm, translator, dictionary: result, createPublicAnkiNoteData};
}
function buildOptions(optionsPresets, optionsArray, dictionaryTitle) {
@@ -140,37 +147,64 @@ function buildOptions(optionsPresets, optionsArray, dictionaryTitle) {
async function main() {
const write = (process.argv[2] === '--write');
- const {translator, dictionary: {title}} = await createVM();
-
- const dataFilePath = path.join(__dirname, 'data', 'test-translator-data.json');
- const data = JSON.parse(fs.readFileSync(dataFilePath, {encoding: 'utf8'}));
- const {optionsPresets, tests} = data;
- for (const test of tests) {
+ const {translator, dictionary: {title}, createPublicAnkiNoteData} = await createVM();
+
+ const createTestAnkiNoteData = (definition, mode) => createPublicAnkiNoteData('{marker}', {
+ definition,
+ resultOutputMode: mode,
+ mode: 'mode',
+ glossaryLayoutMode: 'default',
+ compactTags: false,
+ context: {
+ url: 'url:',
+ sentence: {text: '', offset: 0},
+ documentTitle: 'title'
+ },
+ injectedMedia: null
+ });
+
+ const testInputsFilePath = path.join(__dirname, 'data', 'translator-test-inputs.json');
+ const {optionsPresets, tests} = JSON.parse(fs.readFileSync(testInputsFilePath, {encoding: 'utf8'}));
+
+ const testResults1FilePath = path.join(__dirname, 'data', 'translator-test-results.json');
+ const expectedResults1 = JSON.parse(fs.readFileSync(testResults1FilePath, {encoding: 'utf8'}));
+ const actualResults1 = [];
+
+ const testResults2FilePath = path.join(__dirname, 'data', 'translator-test-results-note-data1.json');
+ const expectedResults2 = JSON.parse(fs.readFileSync(testResults2FilePath, {encoding: 'utf8'}));
+ const actualResults2 = [];
+
+ for (let i = 0, ii = tests.length; i < ii; ++i) {
+ const test = tests[i];
+ const expected1 = expectedResults1[i];
+ const expected2 = expectedResults2[i];
switch (test.func) {
case 'findTerms':
{
- const {mode, text} = test;
+ const {name, mode, text} = test;
const options = buildOptions(optionsPresets, test.options, title);
const [definitions, length] = clone(await translator.findTerms(mode, text, options));
- if (write) {
- test.expected = {length, definitions};
- } else {
- const {expected} = test;
- assert.deepStrictEqual(length, expected.length);
- assert.deepStrictEqual(definitions, expected.definitions);
+ const noteDataList = clone(definitions.map((definition) => createTestAnkiNoteData(clone(definition), null)));
+ actualResults1.push({name, length, definitions});
+ actualResults2.push({name, noteDataList});
+ if (!write) {
+ assert.deepStrictEqual(length, expected1.length);
+ assert.deepStrictEqual(definitions, expected1.definitions);
+ assert.deepStrictEqual(noteDataList, expected2.noteDataList);
}
}
break;
case 'findKanji':
{
- const {text} = test;
+ const {name, text} = test;
const options = buildOptions(optionsPresets, test.options, title);
const definitions = clone(await translator.findKanji(text, options));
- if (write) {
- test.expected = {definitions};
- } else {
- const {expected} = test;
- assert.deepStrictEqual(definitions, expected.definitions);
+ const noteDataList = clone(definitions.map((definition) => createTestAnkiNoteData(clone(definition), null)));
+ actualResults1.push({name, definitions});
+ actualResults2.push({name, noteDataList});
+ if (!write) {
+ assert.deepStrictEqual(definitions, expected1.definitions);
+ assert.deepStrictEqual(noteDataList, expected2.noteDataList);
}
}
break;
@@ -178,7 +212,9 @@ async function main() {
}
if (write) {
- fs.writeFileSync(dataFilePath, JSON.stringify(data, null, 4), {encoding: 'utf8'});
+ // Use 2 indent instead of 4 to save a bit of file size
+ fs.writeFileSync(testResults1FilePath, JSON.stringify(actualResults1, null, 2), {encoding: 'utf8'});
+ fs.writeFileSync(testResults2FilePath, JSON.stringify(actualResults2, null, 2), {encoding: 'utf8'});
}
}