Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dictionary data tests + write mode #415

Merged
merged 10 commits into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"test-ts-dev": "npx tsc --noEmit --project dev/jsconfig.json",
"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-build": "node ./dev/bin/build.js --dry-run --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
215 changes: 0 additions & 215 deletions test/anki-note-builder.test.js

This file was deleted.

1 change: 1 addition & 0 deletions test/data/json.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
{"path": "test/data/dictionaries/invalid-dictionary6/term_meta_bank_1.json", "ignore": true},
{"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": "dev/data/manifest-variants.json",
Expand Down
7 changes: 7 additions & 0 deletions test/data/vitest.write.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"test": {
"include": [
"../**/*.write.js"
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import {fileURLToPath} from 'node:url';
import path from 'path';
import {expect, test} from 'vitest';
import {describe, test} from 'vitest';
import * as dictionaryValidate from '../dev/dictionary-validate.js';
import {createDictionaryArchive} from '../dev/util.js';

Expand All @@ -34,10 +34,8 @@ function createTestDictionaryArchive(dictionary, dictionaryName) {
return createDictionaryArchive(dictionaryDirectory, dictionaryName);
}


/** */
async function main() {
const dictionaries = [
describe('Dictionary validation', () => {
const testCases = [
{name: 'valid-dictionary1', valid: true},
{name: 'invalid-dictionary1', valid: false},
{name: 'invalid-dictionary2', valid: false},
Expand All @@ -46,20 +44,15 @@ async function main() {
{name: 'invalid-dictionary5', valid: false},
{name: 'invalid-dictionary6', valid: false}
];

const schemas = dictionaryValidate.getSchemas();

for (const {name, valid} of dictionaries) {
test(`${name} is ${valid ? 'valid' : 'invalid'}`, async () => {
describe.each(testCases)('Test dictionary $name', ({name, valid}) => {
test(`Should be ${valid ? 'valid' : 'invalid'}`, async ({expect}) => {
const archive = createTestDictionaryArchive(name);

if (valid) {
await expect(dictionaryValidate.validateDictionary(null, archive, schemas)).resolves.not.toThrow();
} else {
await expect(dictionaryValidate.validateDictionary(null, archive, schemas)).rejects.toThrow();
}
});
}
}

await main();
});
});
54 changes: 33 additions & 21 deletions test/translator.test.js → test/dictionary-data.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/*
* Copyright (C) 2023 Yomitan Authors
* Copyright (C) 2020-2022 Yomichan Authors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -22,29 +21,38 @@ import path from 'path';
import {describe} from 'vitest';
import {parseJson} from '../dev/json.js';
import {createTranslatorTest} from './fixtures/translator-test.js';
import {createTestAnkiNoteData} from './utilities/anki.js';
import {createTestAnkiNoteData, getTemplateRenderResults} from './utilities/anki.js';
import {createFindOptions} from './utilities/translator.js';

const dirname = path.dirname(fileURLToPath(import.meta.url));
const dictionaryName = 'Test Dictionary 2';
const test = await createTranslatorTest(void 0, path.join(dirname, 'data/dictionaries/valid-dictionary1'), dictionaryName);

const testInputsFilePath = path.join(dirname, 'data/translator-test-inputs.json');
/** @type {import('test/translator').TranslatorTestInputs} */
const {optionsPresets, tests} = parseJson(readFileSync(testInputsFilePath, {encoding: 'utf8'}));
describe('Dictionary data', () => {
const testInputsFilePath = path.join(dirname, 'data/translator-test-inputs.json');
/** @type {import('test/translator').TranslatorTestInputs} */
const {optionsPresets, tests} = parseJson(readFileSync(testInputsFilePath, {encoding: 'utf8'}));

const testResults1FilePath = path.join(dirname, 'data/translator-test-results.json');
/** @type {import('test/translator').TranslatorTestResults} */
const expectedResults1 = parseJson(readFileSync(testResults1FilePath, {encoding: 'utf8'}));
const testResults1FilePath = path.join(dirname, 'data/translator-test-results.json');
const testResults2FilePath = path.join(dirname, 'data/translator-test-results-note-data1.json');
const testResults3FilePath = path.join(dirname, 'data/anki-note-builder-test-results.json');

const testResults2FilePath = path.join(dirname, 'data/translator-test-results-note-data1.json');
/** @type {import('test/translator').TranslatorTestNoteDataResults} */
const expectedResults2 = parseJson(readFileSync(testResults2FilePath, {encoding: 'utf8'}));
/** @type {import('test/translator').TranslatorTestResults} */
const expectedResults1 = parseJson(readFileSync(testResults1FilePath, {encoding: 'utf8'}));
/** @type {import('test/translator').TranslatorTestNoteDataResults} */
const expectedResults2 = parseJson(readFileSync(testResults2FilePath, {encoding: 'utf8'}));
/** @type {import('test/translator').AnkiNoteBuilderTestResults} */
const expectedResults3 = parseJson(readFileSync(testResults3FilePath, {encoding: 'utf8'}));

const dictionaryName = 'Test Dictionary 2';
const test = await createTranslatorTest(void 0, path.join(dirname, 'data/dictionaries/valid-dictionary1'), dictionaryName);
const template = readFileSync(path.join(dirname, '../ext/data/templates/default-anki-field-templates.handlebars'), {encoding: 'utf8'});

describe('Translator', () => {
const testData = tests.map((data, i) => ({data, expected1: expectedResults1[i], expected2: expectedResults2[i]}));
describe.each(testData)('Test %#: $data.name', ({data, expected1, expected2}) => {
const testCases = tests.map((data, i) => ({
data,
expected1: expectedResults1[i],
expected2: expectedResults2[i],
expected3: expectedResults3[i]
}));
describe.each(testCases)('Test %#: $data.name', ({data, expected1, expected2, expected3}) => {
test('Test', async ({translator, ankiNoteDataCreator, expect}) => {
switch (data.func) {
case 'findTerms':
Expand All @@ -53,10 +61,12 @@ describe('Translator', () => {
/** @type {import('translation').FindTermsOptions} */
const options = createFindOptions(dictionaryName, optionsPresets, data.options);
const {dictionaryEntries, originalTextLength} = await translator.findTerms(mode, text, options);
const renderResults = mode !== 'simple' ? await getTemplateRenderResults(dictionaryEntries, 'terms', mode, template, expect) : null;
const noteDataList = mode !== 'simple' ? dictionaryEntries.map((dictionaryEntry) => createTestAnkiNoteData(ankiNoteDataCreator, dictionaryEntry, mode)) : null;
expect(originalTextLength).toStrictEqual(expected1.originalTextLength);
expect(dictionaryEntries).toStrictEqual(expected1.dictionaryEntries);
expect(noteDataList).toEqual(expected2.noteDataList);
expect.soft(originalTextLength).toStrictEqual(expected1.originalTextLength);
expect.soft(dictionaryEntries).toStrictEqual(expected1.dictionaryEntries);
expect.soft(noteDataList).toEqual(expected2.noteDataList);
expect.soft(renderResults).toStrictEqual(expected3.results);
}
break;
case 'findKanji':
Expand All @@ -65,9 +75,11 @@ describe('Translator', () => {
/** @type {import('translation').FindKanjiOptions} */
const options = createFindOptions(dictionaryName, optionsPresets, data.options);
const dictionaryEntries = await translator.findKanji(text, options);
const renderResults = await getTemplateRenderResults(dictionaryEntries, 'kanji', 'split', template, expect);
const noteDataList = dictionaryEntries.map((dictionaryEntry) => createTestAnkiNoteData(ankiNoteDataCreator, dictionaryEntry, 'split'));
expect(dictionaryEntries).toStrictEqual(expected1.dictionaryEntries);
expect(noteDataList).toEqual(expected2.noteDataList);
expect.soft(dictionaryEntries).toStrictEqual(expected1.dictionaryEntries);
expect.soft(noteDataList).toEqual(expected2.noteDataList);
expect.soft(renderResults).toStrictEqual(expected3.results);
}
break;
}
Expand Down
Loading