-
-
Notifications
You must be signed in to change notification settings - Fork 356
/
Copy pathcompilation.test.ts
98 lines (87 loc) · 2.28 KB
/
compilation.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// utils
vi.mock('@intlify/shared', async () => {
const actual = await vi.importActual<object>('@intlify/shared')
return {
...actual,
warn: vi.fn()
}
})
import { baseCompile } from '@intlify/message-compiler'
import { isMessageAST } from '../src/ast'
import { clearCompileCache, compile } from '../src/compilation'
import { createMessageContext as context } from '../src/runtime'
const DEFAULT_CONTEXT = { locale: 'en', key: 'key' }
beforeEach(() => {
clearCompileCache()
})
describe('isMessageAST', () => {
describe('basic AST', () => {
test('should be true', () => {
expect(isMessageAST({ type: 0, body: '' })).toBe(true)
})
})
describe('minify AST', () => {
test('should be true', () => {
expect(isMessageAST({ type: 0, b: '' })).toBe(true)
})
})
describe('not message compiler AST format', () => {
test('should be false', () => {
expect(isMessageAST({ b: '' })).toBe(false)
})
})
})
describe('compile', () => {
test('basic', () => {
const msg = compile('hello {name}!', DEFAULT_CONTEXT)
const ctx = context({
named: { name: 'kazupon' }
})
expect(msg(ctx)).toBe('hello kazupon!')
})
describe('AST', () => {
test('basic', () => {
const { ast } = baseCompile('hello {name}!', {
location: false,
jit: true
})
const msg = compile(ast, DEFAULT_CONTEXT)
const ctx = context({
named: { name: 'kazupon' }
})
expect(msg(ctx)).toBe('hello kazupon!')
})
test('minify', () => {
const { ast } = baseCompile('hello {name}!', {
location: false,
jit: true,
minify: true
})
const msg = compile(ast, DEFAULT_CONTEXT)
const ctx = context({
named: { name: 'kazupon' }
})
expect(msg(ctx)).toBe('hello kazupon!')
})
})
test('list issue', () => {
const { ast } = baseCompile('hello {0}!', {
location: false,
jit: true,
minify: true
})
const msg = compile(ast, DEFAULT_CONTEXT)
const ctx = context({
list: ['kazupon']
})
expect(msg(ctx)).toBe('hello kazupon!')
})
test('error', () => {
let occured = false
compile('hello {name!', {
...DEFAULT_CONTEXT,
onError: () => (occured = true)
})
expect(occured).toBe(true)
})
})