Skip to content

Commit eb4fa32

Browse files
committed
Fix JS test runner to load translations across import tree
1 parent 3ebafd1 commit eb4fa32

1 file changed

Lines changed: 16 additions & 21 deletions

File tree

js/test-runner.ts

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -146,24 +146,10 @@ function runTest(filePath: string, content: string, testItem: TestItem, crlf: bo
146146
let dialogueCount: number = 0;
147147
let parsedScript: Script | null = null;
148148

149-
// Build options
149+
// Translations are built after parsing the main script so that
150+
// loadLocale can walk the import tree and merge per-file `.<locale>.lor`
151+
// siblings. See the parse block below.
150152
let options: InterpreterOptions | undefined = undefined;
151-
if (testItem.translation) {
152-
const lang: string = testItem.translation;
153-
const basePath: string = filePath.substring(0, filePath.length - 4);
154-
const translationPath: string = basePath + '.' + lang + '.lor';
155-
let translationContent: string = readFileSync(translationPath, 'utf-8');
156-
if (crlf) {
157-
translationContent = translationContent.replace(/\r\n/g, '\n').replace(/\n/g, '\r\n');
158-
} else {
159-
translationContent = translationContent.replace(/\r\n/g, '\n');
160-
}
161-
const translationScript: Script | null = Loreline.parse(translationContent, translationPath, handleFile);
162-
if (translationScript) {
163-
const translations: Translations = Loreline.extractTranslations(translationScript);
164-
options = { translations };
165-
}
166-
}
167153

168154
// Load restoreFile content if specified
169155
let restoreInput: string | null = null;
@@ -258,12 +244,21 @@ function runTest(filePath: string, content: string, testItem: TestItem, crlf: bo
258244

259245
try {
260246
const script: Script | null = Loreline.parse(content, filePath, handleFile);
261-
if (script) {
262-
parsedScript = script;
263-
Loreline.play(script, handleDialogue, handleChoice, handleFinish, beatName, options);
264-
} else {
247+
if (!script) {
265248
resolve({ passed: false, actual: output, expected, error: 'Error parsing script' });
249+
return;
266250
}
251+
parsedScript = script;
252+
253+
if (testItem.translation) {
254+
const lang: string = testItem.translation;
255+
const translations: Translations | null = Loreline.loadLocale(lang, script, filePath, handleFile);
256+
if (translations) {
257+
options = { translations };
258+
}
259+
}
260+
261+
Loreline.play(script, handleDialogue, handleChoice, handleFinish, beatName, options);
267262
} catch (e) {
268263
resolve({ passed: false, actual: output, expected, error: (e as Error).toString() });
269264
}

0 commit comments

Comments
 (0)