Skip to content

Commit

Permalink
Refine warning messages and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
diskdance committed Jan 10, 2025
1 parent f788e1d commit a83010c
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 27 deletions.
26 changes: 17 additions & 9 deletions lib/conversion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,12 @@ function safeElect(candidates: Candidates, locale: string): string {
locale = safelyToString(locale);
}

const result = elect(candidates, locale);
const result = elect(candidates, locale) ?? '';

if (typeof result !== 'string') {
mw.log.warn('[HanAssist] Non-string conversion result detected. Please check your code.');
}

if (result === null) {
return '';
}

// Wrap in another guard to ensure result is really string at runtime
return safelyToString(result);
}
Expand Down Expand Up @@ -99,15 +95,27 @@ function batchConv(
if (!isPlainObject(candidatesDict)) {
throw new TypeError('[HanAssist] Invalid parameter. Must be an object.');
}
if (typeof locale !== 'string') {
mw.log.warn('[HanAssist] locale parameter must be a string. Please check your code.');
locale = safelyToString(locale);
}

const result: Record<string, string> = {};

for (const key in candidatesDict) {
const candidates = candidatesDict[key];
const electionResult = isPlainObject(candidates)
? safeElect(candidates, locale)
: safelyToString(candidates);
result[key] = electionResult;
if (isPlainObject(candidates)) {
const electResult = elect(candidates, locale) ?? '';
if (typeof electResult !== 'string') {
mw.log.warn(`[HanAssist] In '${key}': Non-string conversion result detected. Please check your code.`);
}
result[key] = safelyToString(electResult);
} else {
if (typeof candidates !== 'string') {
mw.log.warn(`[HanAssist] In '${key}': Value is neither a candidate list nor a string. Please check your code.`);
}
result[key] = safelyToString(candidates);
}
}

return result;
Expand Down
61 changes: 43 additions & 18 deletions tests/batchConv.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,6 @@ describe('batchConv', () => {
});

describe('works', () => {
const RAW_MSG = {
locale: {
zh: 'zh',
hans: 'hans',
hant: 'hant',
cn: 'cn',
tw: 'tw',
hk: 'hk',
mo: 'mo',
my: 'my',
sg: 'sg',
other: 'other',
},
'lovely-str': "I'm a lovely string",
SearchHint: { hans: '搜索包含$1的页面', hant: '搜尋包含$1的頁面' },
};

test.each([
{ locale: 'zh', expected: { locale: 'zh', 'lovely-str': "I'm a lovely string", SearchHint: '搜索包含$1的页面' } },
{ locale: 'zh-hans', expected: { locale: 'hans', 'lovely-str': "I'm a lovely string", SearchHint: '搜索包含$1的页面' } },
Expand All @@ -40,7 +23,49 @@ describe('batchConv', () => {
])('in $locale', ({ locale, expected }) => {
getter.mockReturnValue(locale);

expect(batchConv(RAW_MSG)).toStrictEqual(expected);
expect(batchConv({
locale: {
zh: 'zh',
hans: 'hans',
hant: 'hant',
cn: 'cn',
tw: 'tw',
hk: 'hk',
mo: 'mo',
my: 'my',
sg: 'sg',
other: 'other',
},
'lovely-str': "I'm a lovely string",
SearchHint: { hans: '搜索包含$1的页面', hant: '搜尋包含$1的頁面' },
})).toStrictEqual(expected);
});
});

describe('returns string when erroneous inputs are given', () => {
test.each([
{ locale: 'zh', expected: { notStr: '1', innerNotStr: '2', innerEmpty: '' } },
{ locale: 'zh-hans', expected: { notStr: '1', innerNotStr: '2', innerEmpty: '' } },
{ locale: 'zh-hant', expected: { notStr: '1', innerNotStr: '2', innerEmpty: '' } },
{ locale: 'zh-cn', expected: { notStr: '1', innerNotStr: '2', innerEmpty: '' } },
{ locale: 'zh-sg', expected: { notStr: '1', innerNotStr: '2', innerEmpty: '' } },
{ locale: 'zh-my', expected: { notStr: '1', innerNotStr: '2', innerEmpty: '' } },
{ locale: 'zh-mo', expected: { notStr: '1', innerNotStr: '2', innerEmpty: '' } },
{ locale: 'zh-hk', expected: { notStr: '1', innerNotStr: '2', innerEmpty: '' } },
{ locale: 'zh-tw', expected: { notStr: '1', innerNotStr: '2', innerEmpty: '' } },
{ locale: 'en', expected: { notStr: '1', innerNotStr: '2', innerEmpty: '' } },
{ locale: 'fr', expected: { notStr: '1', innerNotStr: '2', innerEmpty: '' } },
{ locale: null, expected: { notStr: '1', innerNotStr: '2', innerEmpty: '' } },
])('in $locale', ({ locale, expected }) => {
getter.mockReturnValue(locale);

expect(batchConv({
/// @ts-expect-error For testing
notStr: 1,
/// @ts-expect-error For testing
innerNotStr: { zh: 2 },
innerEmpty: {},
})).toStrictEqual(expected);
});
});

Expand Down
2 changes: 2 additions & 0 deletions tests/conv.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ describe('conv', () => {
{ locale: 'zh-tw', expected: 'tw' },
{ locale: 'en', expected: 'other' },
{ locale: 'fr', expected: 'other' },
{ locale: null, expected: 'other' },
])('in $locale', ({ locale, expected }) => {
getter.mockReturnValue(locale);

Expand All @@ -54,6 +55,7 @@ describe('conv', () => {
{ locale: 'zh-tw', expected: '一天一蘋果,醫生遠離我。' },
{ locale: 'en', expected: '一天一苹果,医生远离我。' },
{ locale: 'fr', expected: '一天一苹果,医生远离我。' },
{ locale: null, expected: '一天一苹果,医生远离我。' },
])('in $locale', ({ locale, expected }) => {
getter.mockReturnValue(locale);

Expand Down
2 changes: 2 additions & 0 deletions tests/convByVar.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ describe('convByVar', () => {
{ locale: 'zh-tw', expected: 'tw' },
{ locale: 'en', expected: 'other' },
{ locale: 'fr', expected: 'other' },
{ locale: null, expected: 'other' },
])('in $locale', ({ locale, expected }) => {
getter.mockImplementation((val) => {
if (val === 'wgUserVariant') {
Expand Down Expand Up @@ -59,6 +60,7 @@ describe('convByVar', () => {
{ locale: 'zh-tw', expected: '一天一蘋果,醫生遠離我。' },
{ locale: 'en', expected: '一天一苹果,医生远离我。' },
{ locale: 'fr', expected: '一天一苹果,医生远离我。' },
{ locale: null, expected: '一天一苹果,医生远离我。' },
])('in $locale', ({ locale, expected }) => {
getter.mockImplementation((val) => {
if (val === 'wgUserVariant') {
Expand Down

0 comments on commit a83010c

Please sign in to comment.