-
Notifications
You must be signed in to change notification settings - Fork 0
/
tokenizer.spec.ts
58 lines (48 loc) · 1.56 KB
/
tokenizer.spec.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
import { test, expect } from 'vitest'
import { tokenizer, TokenTypes } from './tokenizer'
test('(add 2 (subtract 4 2))', () => {
const code = `(add 2 (subtract 4 2))`
const tokens = [
{ type: TokenTypes.Paren, value: '(' },
{ type: TokenTypes.String, value: 'add' },
{ type: TokenTypes.Number, value: '2' },
{ type: TokenTypes.Paren, value: '(' },
{ type: TokenTypes.String, value: 'subtract' },
{ type: TokenTypes.Number, value: '4' },
{ type: TokenTypes.Number, value: '2' },
{ type: TokenTypes.Paren, value: ')' },
{ type: TokenTypes.Paren, value: ')' }
]
expect(tokenizer(code)).toEqual(tokens)
})
test('left parent', () => {
const code = `(`
const tokens = [{ type: TokenTypes.Paren, value: '(' }]
expect(tokenizer(code)).toEqual(tokens)
})
test('right parent', () => {
const code = `)`
const tokens = [{ type: TokenTypes.Paren, value: ')' }]
expect(tokenizer(code)).toEqual(tokens)
})
test('add', () => {
const code = `add`
const tokens = [{ type: TokenTypes.String, value: 'add' }]
expect(tokenizer(code)).toEqual(tokens)
})
test('number', () => {
const code = `22`
const tokens = [{ type: TokenTypes.Number, value: '22' }]
expect(tokenizer(code)).toEqual(tokens)
})
test('(add 1 2)', () => {
const code = `(add 1 2)`
const tokens = [
{ type: TokenTypes.Paren, value: '(' },
{ type: TokenTypes.String, value: 'add' },
{ type: TokenTypes.Number, value: '1' },
{ type: TokenTypes.Number, value: '2' },
{ type: TokenTypes.Paren, value: ')' }
]
expect(tokenizer(code)).toEqual(tokens)
})