Skip to content

Commit

Permalink
test(bin): add tests for extracting keys and values from expressions …
Browse files Browse the repository at this point in the history
…like t.t(key, text)
  • Loading branch information
eyelly-wu committed May 15, 2024
1 parent 554cb78 commit 144164e
Show file tree
Hide file tree
Showing 3 changed files with 200 additions and 11 deletions.
17 changes: 8 additions & 9 deletions src/bin/extra-text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,29 +105,28 @@ export function extraTextFromTDotT(
if (textValidate) {
textSuccess.push(textContent)
} else {
textError.push(textLabel)
textError.push(textContent)
}

// 自定义key和文案都有效
if (keyValidate && textValidate) {
const text = keyTextMap[keyContent]
if (text && text != text) {
if (text && text != textContent) {
textError.push(
t(
'当前自定义key={0},配置了不一样的文案({1}{2}),应该满足key与文案一对一的关系',
'当前自定义 key=`{0}`,配置了不一样的文案(`{1}` 和 `{2}`),应该满足 key 与文案一对一的关系',
keyContent,
text,
textContent,
),
)
} else if (!text) {
keyTextMap[keyContent] = textContent
}

const keys = textKeyMap[textContent] || []
if (!keys.includes(keyContent)) {
keys.push(keyContent)
textKeyMap[textContent] = keys
const keys = textKeyMap[textContent] || []
if (!keys.includes(keyContent)) {
keys.push(keyContent)
textKeyMap[textContent] = keys
}
}
}
}
Expand Down
161 changes: 159 additions & 2 deletions test/bin/extra-text.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { readFileSync } from 'fs'
import { join } from 'path'
import { binExtraText } from '../utils'

const { extraTextFromT } = binExtraText
const { extraTextFromT, extraTextFromTDotT } = binExtraText

describe('验证翻译文本提取功能', () => {
describe('验证 t 翻译文本提取功能', () => {
const content = readFileSync(join(__dirname, '../i18n/text.ts'), {
encoding: 'utf-8',
})
Expand Down Expand Up @@ -58,3 +58,160 @@ describe('验证翻译文本提取功能', () => {
})
})
})

describe('验证 t.t 翻译文本提取功能', () => {
const content = readFileSync(join(__dirname, '../i18n/text2.ts'), {
encoding: 'utf-8',
})

const successTexts = [
'中间 有空格',
'普通文本',
'普通文本{0}',
'普通文案',
'a',
'自定义key',
'自定义key',
]

const errorTexts = [
'${foo}',
'你好\\n啊', // NOTE 这里需要是\\n,不然匹配不正确
'你好\\t啊',
' 前面有空格',
'后面空格 ',
'当前自定义 key=`test`,配置了不一样的文案(`普通文案` 和 `a`),应该满足 key 与文案一对一的关系',
]

const successKeys = [
'中间 有空格的key',
'普通文本key',
'普通文本',
'test',
'test',
'key2',
'key3',
]

const errorKeys = [
'${key}',
'key\\n中间', // NOTE 这里需要是\\n,不然匹配不正确
'key\\t中间',
' key前面有空格',
'key后面空格 ',
]

const expectKeyTextMap = {
'中间 有空格的key': '中间 有空格',
普通文本key: '普通文本',
普通文本: '普通文本{0}',
test: '普通文案',
key2: '自定义key',
key3: '自定义key',
}

const expectTextKeyMap = {
'中间 有空格': ['中间 有空格的key'],
普通文本: ['普通文本key'],
'普通文本{0}': ['普通文本'],
普通文案: ['test'],
自定义key: ['key2', 'key3'],
}

function getParams() {
return {
keySuccess: [],
keyError: [],
keyTextMap: {},
textKeyMap: {},
textSuccess: [],
textError: [],
}
}

describe('验证规范文本提取', () => {
it('默认提取', () => {
const {
keySuccess,
keyError,
keyTextMap,
textKeyMap,
textSuccess,
textError,
} = getParams()
extraTextFromTDotT(
content,
't',
keyTextMap,
textKeyMap,
textSuccess,
textError,
keySuccess,
keyError,
)
expect(textSuccess).toEqual(successTexts)
expect(textError).toEqual(errorTexts)
expect(keySuccess).toEqual(successKeys)
expect(keyError).toEqual(errorKeys)
expect(keyTextMap).toEqual(expectKeyTextMap)
expect(textKeyMap).toEqual(expectTextKeyMap)
})

it('自定义函数名(t -> i18n)', () => {
const {
keySuccess,
keyError,
keyTextMap,
textKeyMap,
textSuccess,
textError,
} = getParams()
extraTextFromTDotT(
content.replaceAll(/t\.t\(/g, 'i18n.t('),
'i18n',
keyTextMap,
textKeyMap,
textSuccess,
textError,
keySuccess,
keyError,
)
expect(textSuccess).toEqual(successTexts)
expect(textError).toEqual(errorTexts)
expect(keySuccess).toEqual(successKeys)
expect(keyError).toEqual(errorKeys)
expect(keyError).toEqual(errorKeys)
expect(keyTextMap).toEqual(expectKeyTextMap)
expect(textKeyMap).toEqual(expectTextKeyMap)
})
})

describe('验证不规范文本提取', () => {
it('未知匹配函数名i18n', () => {
const {
keySuccess,
keyError,
keyTextMap,
textKeyMap,
textSuccess,
textError,
} = getParams()
extraTextFromTDotT(
content,
'i18n',
keyTextMap,
textKeyMap,
textSuccess,
textError,
keySuccess,
keyError,
)
expect(keySuccess).toEqual([])
expect(keyError).toEqual([])
expect(textSuccess).toEqual([])
expect(textSuccess).toEqual([])
expect(keyTextMap).toEqual({})
expect(textKeyMap).toEqual({})
})
})
})
33 changes: 33 additions & 0 deletions test/i18n/text2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { initI18n } from '../../src/lib'
const { t } = initI18n({ namespace: 'text' })
const foo = 'foo'
const key = 'key'
const fooFunc = (x: string) => x
const keyFunc = (x: string) => x

// 不能记录的错误信息的错误
t.t(key, foo)
t.t(keyFunc(key), fooFunc(foo))
t.t('xxx' + key, 'xxx' + foo)
t.t(
`中间有
换行`,
`中间有
换行`,
)

// 能记录到错误信息的错误
t.t(`${key}`, `${foo}`)
t.t('key\n中间', '你好\n啊')
t.t('key\t中间', '你好\t啊')
t.t(' key前面有空格', ' 前面有空格')
t.t('key后面空格 ', '后面空格 ')

// 正常解析的示例
t.t('中间 有空格的key', '中间 有空格')
t.t('普通文本key', '普通文本')
t.t('普通文本', '普通文本{0}', foo)
t.t('test', '普通文案')
t.t('test', 'a', t('b'))
t.t('key2', '自定义key')
t.t('key3', '自定义key')

0 comments on commit 144164e

Please sign in to comment.