summaryrefslogtreecommitdiff
path: root/test/fixtures/language-transformer-test.js
diff options
context:
space:
mode:
authorStefanVukovic99 <stefanvukovic44@gmail.com>2024-04-18 21:04:57 +0200
committerGitHub <noreply@github.com>2024-04-18 19:04:57 +0000
commit468c121f66dc94b916d853acaefbe0376bbc851f (patch)
tree7b2e2439b947cad6af71e2287962d919f4dbba17 /test/fixtures/language-transformer-test.js
parent297f089d6fe2b555cc2a2c0482c0b69a25f05b87 (diff)
abstract deinflection tests (#784)
* abstract deinflection tests * add a couple of phrasal verb tests * fix past simple phrasal verb deinflection
Diffstat (limited to 'test/fixtures/language-transformer-test.js')
-rw-r--r--test/fixtures/language-transformer-test.js81
1 files changed, 81 insertions, 0 deletions
diff --git a/test/fixtures/language-transformer-test.js b/test/fixtures/language-transformer-test.js
new file mode 100644
index 00000000..1701b4f9
--- /dev/null
+++ b/test/fixtures/language-transformer-test.js
@@ -0,0 +1,81 @@
+/*
+ * Copyright (C) 2023-2024 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
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+import {describe, expect, test} from 'vitest';
+import {LanguageTransformer} from '../../ext/js/language/language-transformer.js';
+
+/**
+ * @param {LanguageTransformer} languageTransformer
+ * @param {string} source
+ * @param {string} expectedTerm
+ * @param {string|null} expectedConditionName
+ * @param {string[]|null} expectedReasons
+ * @returns {{has: false, reasons: null, rules: null}|{has: true, reasons: string[], rules: number}}
+ */
+function hasTermReasons(languageTransformer, source, expectedTerm, expectedConditionName, expectedReasons) {
+ for (const {text, conditions, trace} of languageTransformer.transform(source)) {
+ if (text !== expectedTerm) { continue; }
+ if (expectedConditionName !== null) {
+ const expectedConditions = languageTransformer.getConditionFlagsFromConditionType(expectedConditionName);
+ if (!LanguageTransformer.conditionsMatch(conditions, expectedConditions)) { continue; }
+ }
+ let okay = true;
+ if (expectedReasons !== null) {
+ if (trace.length !== expectedReasons.length) { continue; }
+ for (let i = 0, ii = expectedReasons.length; i < ii; ++i) {
+ if (expectedReasons[i] !== trace[i].transform) {
+ okay = false;
+ break;
+ }
+ }
+ }
+ if (okay) {
+ return {
+ has: true,
+ reasons: trace.map((frame) => frame.transform),
+ rules: conditions
+ };
+ }
+ }
+ return {has: false, reasons: null, rules: null};
+}
+
+/**
+ * @param {LanguageTransformer} languageTransformer
+ * @param {import('test/language-transformer-test').LanguageTransformerTestCategory[]} data
+ */
+export function testLanguageTransformer(languageTransformer, data) {
+ describe('deinflections', () => {
+ describe.each(data)('$category', ({valid, tests}) => {
+ for (const {source, term, rule, reasons} of tests) {
+ const {has} = hasTermReasons(languageTransformer, source, term, rule, reasons);
+ let message = `${source} ${valid ? 'has' : 'does not have'} term candidate ${JSON.stringify(term)}`;
+ if (rule !== null) {
+ message += ` with rule ${JSON.stringify(rule)}`;
+ }
+ if (reasons !== null) {
+ message += (typeof rule !== 'undefined' ? ' and' : ' with');
+ message += ` reasons ${JSON.stringify(reasons)}`;
+ }
+ test(`${message}`, () => {
+ expect(has).toStrictEqual(valid);
+ });
+ }
+ });
+ });
+}