-
-
Notifications
You must be signed in to change notification settings - Fork 355
/
Copy pathtokenizer.test.ts
138 lines (133 loc) · 3.37 KB
/
tokenizer.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import { parse } from '../src/tokenizer'
import path from 'node:path'
import { promises as fs } from 'node:fs'
import type { CompileError } from '../src/errors'
import type { TokenizeOptions } from '../src/options'
const CASES = [
`hello world`,
`hi {name} !`,
`{first} {middle} {last}`, // eslint-disable-line no-irregular-whitespace
`hi { name } !`,
`{first}\n{middle}\r\n{last}`,
`hi {0} !`,
`{0} {1} {2}`, // eslint-disable-line no-irregular-whitespace
`hi { -1 } !`,
`{0}\n{1}\r\n{2}`,
`hi {'kazupon'} !`,
`hi @:name !`,
`hi @:{'hello world'} !`,
`hi @:{name}\n !`,
`hi @.upper:name !`,
`hi @:{name} @:{0}!`,
`no apples | one apple | too much apples `,
`no apples |\n one apple |\n too much apples `,
`@.lower:{'no apples'} | {1} apple | {count} apples`, // eslint-disable-line no-irregular-whitespace
`hello\nworld`,
`こんにちは、世界`,
`😺`,
``,
`...`,
`hypen-nate`,
`hello:`,
`1 + 1`,
`name = foo`,
`'single-quote'`,
`"double-qoute"`,
` hello world `,
`hello\\nworld`,
`hi, :-) !`,
`hi, :-} !`,
`hi {} !`,
`hi {{ !`,
`hi {{}} !`,
`hi { } !`,
`hi %name`,
`hi {name$} !`,
`hi {snake_case} !`,
`hi {\nname\n} !`,
`hi {{name}} !`,
`hi { { name } } !`,
`hi {name`,
`hi {name !`,
`hi { name !`,
`hi { name !`,
`hi {{0}} !`,
`hi { { 0 } } !`,
`hi {0`,
`hi {0 !`,
`hi { 0 !`,
`hi {@.lower:name !`,
`hi { @:name !`,
`hi { | hello {name} !`,
`hi { @:name | hello {name} !`,
`hi { 'foo`,
`hi { 'foo }`,
`hi { 'foo\n' }`,
`hi { '\\x41' }`,
`hi { '\\uw' }`,
`hi {$} !`,
`hi {-} !`,
`hi @:{ name } !`,
`hi @:{ 'name' } !`,
`hi @:{ {name} } !`,
`hi @\n:name !`,
`hi @ :name !`,
`hi @. {name} !`,
`hi @.\n{name} !`,
`hi @.upper\n{name} !`,
`hi @.upper {name} !`,
`hi @:\nname !`,
`hi @:名前`,
`hi @: {'name'} !`,
`hi @\n. upper\n: {'name'}\n !`,
` | | |`,
` foo | | bar`,
`@.lower: {'no apples'} | {1 apple | @:{count apples` // eslint-disable-line no-irregular-whitespace
]
test('token analysis', () => {
for (const _case of CASES) {
const errors: CompileError[] = []
const options: TokenizeOptions = {
onError: err => {
errors.push({ ...err, message: err.message })
}
}
const tokens = parse(_case, options)
expect(tokens).toMatchSnapshot(`${JSON.stringify(_case)} tokens`)
if (errors.length) {
expect(errors).toMatchSnapshot(`${JSON.stringify(_case)} errors`)
}
}
})
test('tokenize options: location disable', () => {
for (const _case of CASES) {
const errors: CompileError[] = []
const options: TokenizeOptions = {
location: false,
onError: err => {
errors.push({ ...err, message: err.message })
}
}
const tokens = parse(_case, options)
expect(tokens).toMatchSnapshot(`${JSON.stringify(_case)} tokens`)
for (const token of tokens) {
expect(token.loc).toBeUndefined()
}
if (errors.length) {
expect(errors).toMatchSnapshot(`${JSON.stringify(_case)} errors`)
for (const error of errors) {
expect(error.location).toBeUndefined()
}
}
}
})
describe('edge cases', () => {
test('long text', async () => {
const data = await fs.readFile(
path.join(__dirname, './fixtures/20_newsgroups_alt_atheism_51060.txt'),
'utf8'
)
expect(parse(data).length).toEqual(2)
})
})