(test) validate translations

This commit is contained in:
Valentyne Stigloher 2023-12-25 10:13:08 +01:00
parent 75653d45ce
commit 93db7e672e
2 changed files with 66 additions and 5 deletions

View File

@ -358,11 +358,10 @@ contact:
team:
name: 'The “Neutral Language Council” collective' # TODO
nameShort: 'Collective'
description: # TODO
- >
We are a queer collective dedicated to assembling, researching, shaping and promoting
gender neutral and nonbinary language.
We also support actions towards equality and social justice.
description: > # TODO
We are a queer collective dedicated to assembling, researching, shaping and promoting
gender neutral and nonbinary language.
We also support actions towards equality and social justice.
logo: 'Logo of the collective is a combination of the transgender symbol and a speech bubble that symbolises language.' # TODO
members: 'Current members' # TODO
member: 'Member of the collective' # TODO

62
test/translations.test.js Normal file
View File

@ -0,0 +1,62 @@
import { expect, test } from '@jest/globals';
import { loadSumlFromBase } from '../server/loader.js';
import locales from '../locale/locales.js';
const baseTranslations = loadSumlFromBase('locale/_base/translations');
const typeFlexibleKeys = new Set(['home.generator.alt']);
function specificTypeOf(value) {
if (typeof(value) === 'object') {
if (value === null) {
return null;
} else if (Array.isArray(value)) {
return 'array';
} else {
return 'object';
}
} else {
return typeof(value);
}
}
function toMatchBaseTranslationsSchema(actual) {
const messages = recursivelyValidateSchema(actual, baseTranslations);
if (messages.length > 0) {
return {
message: () =>
`expected translations to match schema of base translations\n\n${messages.join('\n')}`,
pass: false,
}
} else {
return {
message: () =>
'expected translations to mismatch schema of base translations',
pass: true,
}
}
}
function recursivelyValidateSchema(actual, base, parentKey = '') {
const messages = [];
for (const [property, value] of Object.entries(actual)) {
const key = parentKey ? `${parentKey}.${property}` : property;
if (base[property] === undefined || base[property] === null) {
continue;
}
if (value !== null && !typeFlexibleKeys.has(key) && specificTypeOf(value) !== specificTypeOf(base[property])) {
messages.push(`${key} has type ${specificTypeOf(value)}, expected ${specificTypeOf(base[property])}`);
}
if (specificTypeOf(value) === 'object') {
messages.push(...recursivelyValidateSchema(value, base[property], key));
}
}
return messages;
}
test.each(locales)('translations of $code match schema of base translations', ({ code }) => {
const translations = loadSumlFromBase(`locale/${code}/translations`);
expect(translations).toMatchBaseTranslationSchema();
});
expect.extend({ toMatchBaseTranslationSchema: toMatchBaseTranslationsSchema });