-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test cases for current translations WASM bindings
Adds test cases that test the current translation functionality end-to-end, including plaint-text translations and HTML translations.
- Loading branch information
Showing
7 changed files
with
195 additions
and
7 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
import { it, expect } from "vitest"; | ||
import { TranslationsEngine } from "./engine/translations-engine.mjs"; | ||
|
||
/** | ||
* Runs a translation test, constructing a Translations Engine for the given | ||
* sourceLanguage and targetLanguage, then asserting that the translation of | ||
* the sourceText matches the expectedText. | ||
* | ||
* @param {Object} params - The parameters for the test. | ||
* @param {string} params.sourceLanguage - The source language code. | ||
* @param {string} params.targetLanguage - The target language code. | ||
* @param {string} params.sourceText - The text to translate. | ||
* @param {string} params.expectedText - The expected translated text. | ||
* @param {boolean} params.isHTML - Whether the text to translate contains HTML tags. | ||
*/ | ||
export function runTranslationTest({ | ||
sourceLanguage, | ||
targetLanguage, | ||
sourceText, | ||
expectedText, | ||
isHTML = false, | ||
}) { | ||
it(`(${sourceLanguage} -> ${targetLanguage}): Translate "${sourceText}"`, async () => { | ||
const translator = new TranslationsEngine(sourceLanguage, targetLanguage); | ||
|
||
const translatedText = await translator.translate(sourceText, isHTML); | ||
|
||
expect(translatedText).toBe(expectedText); | ||
|
||
translator.terminate(); | ||
}); | ||
} |
46 changes: 46 additions & 0 deletions
46
inference/wasm/tests/test-cases/translate-html-no-pivot.test.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
import { describe } from "vitest"; | ||
import { runTranslationTest } from "./test-cases/shared.mjs"; | ||
|
||
/** | ||
* This file tests the WASM bindings for non-pivot translation requests | ||
* that contain HTML tags within the source text. | ||
*/ | ||
|
||
const testCases = [ | ||
{ | ||
sourceLanguage: "es", | ||
targetLanguage: "en", | ||
sourceText: "<b>El perro</b> azul.", | ||
expectedText: "<b>The</b> blue <b>dog</b>.", | ||
isHTML: true, | ||
}, | ||
{ | ||
sourceLanguage: "en", | ||
targetLanguage: "es", | ||
sourceText: "<b>The blue</b> dog.", | ||
expectedText: "<b>El</b> perro <b>azul</b>.", | ||
isHTML: true, | ||
}, | ||
{ | ||
sourceLanguage: "fr", | ||
targetLanguage: "en", | ||
sourceText: "<b>Le chien</b> bleu.", | ||
expectedText: "<b>The</b> blue <b>dog</b>.", | ||
isHTML: true, | ||
}, | ||
{ | ||
sourceLanguage: "en", | ||
targetLanguage: "fr", | ||
sourceText: "<b>The blue</b> dog.", | ||
expectedText: "<b>Le</b> chien <b>bleu</b>.", | ||
isHTML: true, | ||
}, | ||
]; | ||
|
||
describe("HTML Non-Pivot Translations", () => { | ||
testCases.forEach(runTranslationTest); | ||
}); |
32 changes: 32 additions & 0 deletions
32
inference/wasm/tests/test-cases/translate-html-with-pivot.test.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
import { describe } from "vitest"; | ||
import { runTranslationTest } from "./test-cases/shared.mjs"; | ||
|
||
/** | ||
* This file tests the WASM bindings for pivot translation requests | ||
* that contain HTML tags within the source text. | ||
*/ | ||
|
||
const testCases = [ | ||
{ | ||
sourceLanguage: "es", | ||
targetLanguage: "fr", | ||
sourceText: "<b>El perro</b> azul.", | ||
expectedText: "<b>Le chien</b> bleu.", | ||
isHTML: true, | ||
}, | ||
{ | ||
sourceLanguage: "fr", | ||
targetLanguage: "es", | ||
sourceText: "<b>Le chien</b> bleu.", | ||
expectedText: "<b>El perro</b> azul.", | ||
isHTML: true, | ||
}, | ||
]; | ||
|
||
describe("HTML Pivot Translations", () => { | ||
testCases.forEach(runTranslationTest); | ||
}); |
42 changes: 42 additions & 0 deletions
42
inference/wasm/tests/test-cases/translate-plain-text-no-pivot.test.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
import { describe } from "vitest"; | ||
import { runTranslationTest } from "./test-cases/shared.mjs"; | ||
|
||
/** | ||
* This file tests the WASM bindings for non-pivot translation requests | ||
* that contain only plain text without HTML tags. | ||
*/ | ||
|
||
const testCases = [ | ||
{ | ||
sourceLanguage: "es", | ||
targetLanguage: "en", | ||
sourceText: "Hola mundo", | ||
expectedText: "Hello world", | ||
}, | ||
{ | ||
sourceLanguage: "en", | ||
targetLanguage: "es", | ||
sourceText: "Hello world", | ||
expectedText: "Hola mundo", | ||
}, | ||
{ | ||
sourceLanguage: "fr", | ||
targetLanguage: "en", | ||
sourceText: "Bonjour le monde", | ||
expectedText: "Hello world", | ||
}, | ||
{ | ||
sourceLanguage: "en", | ||
targetLanguage: "fr", | ||
sourceText: "Hello world", | ||
expectedText: "Bonjour le monde", | ||
}, | ||
]; | ||
|
||
describe("Plain-Text Non-Pivot Translations", () => { | ||
testCases.forEach(runTranslationTest); | ||
}); |
32 changes: 32 additions & 0 deletions
32
inference/wasm/tests/test-cases/translate-plain-text-with-pivot.test.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
import { describe } from "vitest"; | ||
import { runTranslationTest } from "./test-cases/shared.mjs"; | ||
|
||
/** | ||
* This file tests the WASM bindings for pivot translation requests | ||
* that contain only plain text without HTML tags. | ||
*/ | ||
|
||
const testCases = [ | ||
{ | ||
sourceLanguage: "es", | ||
targetLanguage: "fr", | ||
sourceText: "El perro azul.", | ||
expectedText: "Le chien bleu.", | ||
isHTML: true, | ||
}, | ||
{ | ||
sourceLanguage: "fr", | ||
targetLanguage: "es", | ||
sourceText: "Le chien bleu.", | ||
expectedText: "El perro azul.", | ||
isHTML: true, | ||
}, | ||
]; | ||
|
||
describe("Plain-Text Pivot Translations", () => { | ||
testCases.forEach(runTranslationTest); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { defineConfig } from "vitest/config"; | ||
|
||
export default defineConfig({ | ||
test: { | ||
reporters: ["default"], | ||
}, | ||
}); |