summaryrefslogtreecommitdiff
path: root/test/test-anki-note-builder.js
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2021-03-25 19:55:31 -0400
committerGitHub <noreply@github.com>2021-03-25 19:55:31 -0400
commit4be5c8fd9f7860e701d0b7d3c8c0ee934bc60a4f (patch)
treedcd78316afdf00bbb67d3d1aa6555a9c8ea3efec /test/test-anki-note-builder.js
parente7035dcff41d94f20c0bc8865d413412afc7c229 (diff)
Refactor Translator and dictionary entry format (#1553)
* Update test data * Move translator.js * Create new version of Translator * Update Backend * Update DictionaryDataUtil * Update DisplayGenerator * Create AnkiNoteDataCreator * Replace AnkiNoteData with AnkiNoteDataCreator * Update tests * Remove AnkiNoteData * Update test data * Remove translator-old.js * Add TypeScript interface definitions for the new translator data format
Diffstat (limited to 'test/test-anki-note-builder.js')
-rw-r--r--test/test-anki-note-builder.js31
1 files changed, 20 insertions, 11 deletions
diff --git a/test/test-anki-note-builder.js b/test/test-anki-note-builder.js
index deee5b6c..73a967b1 100644
--- a/test/test-anki-note-builder.js
+++ b/test/test-anki-note-builder.js
@@ -42,21 +42,20 @@ async function createVM() {
const [
JapaneseUtil,
TemplateRenderer,
- AnkiNoteData,
AnkiNoteBuilder
] = vm.get([
'JapaneseUtil',
'TemplateRenderer',
- 'AnkiNoteData',
'AnkiNoteBuilder'
]);
+ const ankiNoteDataCreator = vm.ankiNoteDataCreator;
class TemplateRendererProxy {
constructor() {
const japaneseUtil = new JapaneseUtil(null);
this._templateRenderer = new TemplateRenderer(japaneseUtil);
this._templateRenderer.registerDataType('ankiNote', {
- modifier: ({data, marker}) => new AnkiNoteData(japaneseUtil, marker, data).createPublic()
+ modifier: ({data, marker}) => ankiNoteDataCreator.create(marker, data)
});
}
@@ -122,7 +121,7 @@ function getFieldMarkers(type) {
}
}
-async function getRenderResults(definitions, type, mode, templates, AnkiNoteBuilder, write) {
+async function getRenderResults(dictionaryEntries, type, mode, templates, AnkiNoteBuilder, write) {
const markers = getFieldMarkers(type);
const fields = [];
for (const marker of markers) {
@@ -132,8 +131,18 @@ async function getRenderResults(definitions, type, mode, templates, AnkiNoteBuil
const clozePrefix = 'cloze-prefix';
const clozeSuffix = 'cloze-suffix';
const results = [];
- for (const definition of definitions) {
- const source = definition.type === 'kanji' ? definition.character : definition.rawSource;
+ for (const dictionaryEntry of dictionaryEntries) {
+ let source = '';
+ switch (dictionaryEntry.type) {
+ case 'kanji':
+ source = dictionaryEntry.character;
+ break;
+ case 'term':
+ if (dictionaryEntry.headwords.length > 0 && dictionaryEntry.headwords[0].sources.length > 0) {
+ source = dictionaryEntry.headwords[0].sources[0].originalText;
+ }
+ break;
+ }
const ankiNoteBuilder = new AnkiNoteBuilder();
const context = {
url: 'url:',
@@ -145,7 +154,7 @@ async function getRenderResults(definitions, type, mode, templates, AnkiNoteBuil
};
const errors = [];
const noteFields = (await ankiNoteBuilder.createNote({
- definition,
+ definition: dictionaryEntry,
mode: null,
context,
templates,
@@ -193,8 +202,8 @@ async function main() {
{
const {name, mode, text} = test;
const options = vm.buildOptions(optionsPresets, test.options);
- const [definitions] = clone(await vm.translator.findTerms(mode, text, options));
- const results = mode !== 'simple' ? clone(await getRenderResults(definitions, 'terms', mode, templates, AnkiNoteBuilder, write)) : null;
+ const {dictionaryEntries} = clone(await vm.translator.findTerms(mode, text, options));
+ const results = mode !== 'simple' ? clone(await getRenderResults(dictionaryEntries, 'terms', mode, templates, AnkiNoteBuilder, write)) : null;
actualResults1.push({name, results});
if (!write) {
assert.deepStrictEqual(results, expected1.results);
@@ -205,8 +214,8 @@ async function main() {
{
const {name, text} = test;
const options = vm.buildOptions(optionsPresets, test.options);
- const definitions = clone(await vm.translator.findKanji(text, options));
- const results = clone(await getRenderResults(definitions, 'kanji', null, templates, AnkiNoteBuilder, write));
+ const dictionaryEntries = clone(await vm.translator.findKanji(text, options));
+ const results = clone(await getRenderResults(dictionaryEntries, 'kanji', null, templates, AnkiNoteBuilder, write));
actualResults1.push({name, results});
if (!write) {
assert.deepStrictEqual(results, expected1.results);