-
Notifications
You must be signed in to change notification settings - Fork 0
/
eslox.spec.js
334 lines (295 loc) · 9.78 KB
/
eslox.spec.js
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
import path from 'node:path'
import { afterEach, beforeEach, describe, expect, it } from 'vitest'
import { ESLox } from '../bin/eslox'
describe('ESLox - runFile()', () => {
let eslox
beforeEach(() => {
eslox = new ESLox()
})
afterEach(() => {
eslox = null
})
it('should read files', () => {
const result = eslox.runFile(path.resolve(__dirname, './test.eslox'))
expect(result).toEqual(true)
})
})
describe('ESLox - scanSource()', () => {
let eslox = null
beforeEach(() => {
eslox = new ESLox()
})
afterEach(() => {
eslox = null
})
it('scanSource("/") should generate the correct token list', () => {
const res = eslox.scanSource('/').map(token => token.toString())
expect(res).toEqual([
'type=/ lexeme=/ literal=undefined',
'type=eof lexeme=empty literal=undefined'
])
})
it('scanSource("//") should generate the correct token list for comments', () => {
const res = eslox.scanSource('//').map(token => token.toString())
expect(res).toEqual(['type=eof lexeme=empty literal=undefined'])
})
it('scanSource("(( )){}") should generate the correct token list for groupings', () => {
const res = eslox.scanSource('(( )){}').map(token => token.toString())
expect(res).toEqual([
'type=( lexeme=( literal=undefined',
'type=( lexeme=( literal=undefined',
'type=) lexeme=) literal=undefined',
'type=) lexeme=) literal=undefined',
'type={ lexeme={ literal=undefined',
'type=} lexeme=} literal=undefined',
'type=eof lexeme=empty literal=undefined'
])
})
it('scanSource("!*+-/=<> <= ==") should generate the correct token list for operators', () => {
const res = eslox
.scanSource('!*+-/=<> <= ==')
.map(token => token.toString())
expect(res).toEqual([
'type=! lexeme=! literal=undefined',
'type=* lexeme=* literal=undefined',
'type=+ lexeme=+ literal=undefined',
'type=- lexeme=- literal=undefined',
'type=/ lexeme=/ literal=undefined',
'type== lexeme== literal=undefined',
'type=< lexeme=< literal=undefined',
'type=> lexeme=> literal=undefined',
'type=<= lexeme=<= literal=undefined',
'type=== lexeme=== literal=undefined',
'type=eof lexeme=empty literal=undefined'
])
})
it('scanSource("test") should generate the correct token list', () => {
const res = eslox.scanSource('"test"').map(token => token.toString())
expect(res).toEqual([
`type=str lexeme="test" literal=test`,
'type=eof lexeme=empty literal=undefined'
])
})
it('scanSource("") should generate the correct token list', () => {
const res = eslox.scanSource('""').map(token => token.toString())
expect(res).toEqual([
`type=str lexeme="" literal=`,
'type=eof lexeme=empty literal=undefined'
])
})
it('scanSource("test a word") should generate the correct token list', () => {
const res = eslox.scanSource('"test a word"').map(token => token.toString())
expect(res).toEqual([
`type=str lexeme="test a word" literal=test a word`,
'type=eof lexeme=empty literal=undefined'
])
})
it('scanSource("1") should generate the correct token list', () => {
const res = eslox.scanSource('1').map(token => token.toString())
expect(res).toEqual([
`type=num lexeme=1 literal=1`,
'type=eof lexeme=empty literal=undefined'
])
})
it('scanSource("120") should generate the correct token list', () => {
const res = eslox.scanSource('120').map(token => token.toString())
expect(res).toEqual([
`type=num lexeme=120 literal=120`,
'type=eof lexeme=empty literal=undefined'
])
})
it('scanSource("120.50") should generate the correct token list', () => {
const res = eslox.scanSource('120.50').map(token => token.toString())
expect(res).toEqual([
`type=num lexeme=120.50 literal=120.5`,
'type=eof lexeme=empty literal=undefined'
])
})
it('scanSource("test=120.50") should generate the correct token list', () => {
const res = eslox.scanSource('"test"=120.50').map(token => token.toString())
expect(res).toEqual([
`type=str lexeme="test" literal=test`,
`type== lexeme== literal=undefined`,
`type=num lexeme=120.50 literal=120.5`,
'type=eof lexeme=empty literal=undefined'
])
})
it('scanSource("class") should generate the correct token list', () => {
const res = eslox.scanSource('class').map(token => token.toString())
expect(res).toEqual([
`type=class lexeme=class literal=undefined`,
'type=eof lexeme=empty literal=undefined'
])
})
it('scanSource("var test = "value"") should generate the correct token list', () => {
const res = eslox
.scanSource(`var test = "value"`)
.map(token => token.toString())
expect(res).toEqual([
'type=var lexeme=var literal=undefined',
'type=id lexeme=test literal=undefined',
'type== lexeme== literal=undefined',
`type=str lexeme="value" literal=value`,
'type=eof lexeme=empty literal=undefined'
])
})
it('scanSource("var test = "value"") should generate the correct token list', () => {
const res = eslox
.scanSource(`var test = "value"`)
.map(token => token.toString())
expect(res).toEqual([
'type=var lexeme=var literal=undefined',
'type=id lexeme=test literal=undefined',
'type== lexeme== literal=undefined',
`type=str lexeme="value" literal=value`,
'type=eof lexeme=empty literal=undefined'
])
})
it('scanSource("var test = "value"\nif(test) { return }") should generate the correct token list', () => {
const res = eslox
.scanSource(
`var test = "value"
if (test) { return }
`
)
.map(token => token.toString())
expect(res).toEqual([
'type=var lexeme=var literal=undefined',
'type=id lexeme=test literal=undefined',
'type== lexeme== literal=undefined',
`type=str lexeme="value" literal=value`,
'type=if lexeme=if literal=undefined',
'type=( lexeme=( literal=undefined',
'type=id lexeme=test literal=undefined',
'type=) lexeme=) literal=undefined',
'type={ lexeme={ literal=undefined',
'type=return lexeme=return literal=undefined',
'type=} lexeme=} literal=undefined',
'type=eof lexeme=empty literal=undefined'
])
})
it('scanSource("/* comment */") should generate the correct token list', () => {
const res = eslox
.scanSource(
`/* comment */
`
)
.map(token => token.toString())
expect(res).toEqual([
'type=/* lexeme=/* literal=undefined',
'type=*/ lexeme=*/ literal=undefined',
'type=eof lexeme=empty literal=undefined'
])
})
it('scanSource("/* this is a block level comment */") should generate the correct token list', () => {
const res = eslox
.scanSource(
`/*
this is a block level
comment
*/
`
)
.map(token => token.toString())
expect(res).toEqual([
'type=/* lexeme=/* literal=undefined',
'type=*/ lexeme=*/ literal=undefined',
'type=eof lexeme=empty literal=undefined'
])
})
})
describe('ESLox - run()', () => {
let eslox
beforeEach(() => {
eslox = new ESLox()
})
afterEach(() => {
eslox = null
})
it.each([
['false;', false],
['true;', true],
['nil;', 'nil'],
['100;', 100]
])('should interpret literal values', (input, expected) => {
const result = eslox.run(input)
expect(result).toEqual(expected)
})
it.each([
['!false;', true],
['!true;', false],
['-1;', -1]
])('should interpret unary values', (input, expected) => {
const result = eslox.run(input)
expect(result).toEqual(expected)
})
it.each([
['1 - 1;', 0],
['15 / 3;', 5],
['2 * 2;', 4],
['2 + 12;', 14],
[`"a" + "b";`, 'ab'],
['5 > 2;', true],
['5 >= 5;', true],
['4 >= 5;', false],
['10 <= 10;', true],
['0 == 0;', true],
['1 == 1;', true],
['3 == 1;', false],
['2 != 1;', true],
['0 != 0;', false],
['100 != 100;', false]
])('should interpret binary values', (input, expected) => {
const result = eslox.run(input)
expect(result).toEqual(expected)
})
it.each([
['print 1 - 1;', 0],
['print "one";', 'one'],
['print true;', true],
['print 2 + 1;', 3]
])('should handle print statements', (input, expected) => {
const result = eslox.run(input)
expect(result).toEqual(expected)
})
it('should handle variable assignments', () => {
eslox.run(`var a = 1;`)
eslox.run(`var b = 2;`)
const result = eslox.run(`print a + b;`)
expect(result).toEqual(3)
})
it('should handle variable re-assignments', () => {
eslox.run(`var a = 1;`)
eslox.run(`a = 2;`)
const result = eslox.run(`print a;`)
expect(result).toEqual(2)
})
it('should handle if statement control flow', () => {
eslox.run(`var a = 1;`)
eslox.run(`if (true) { var a = 2; }`)
const result = eslox.run(`print a;`)
expect(result).toEqual(2)
})
it('should handle short circuit or conditional control flow', () => {
const left = eslox.run(`print "hi" or 2;`)
expect(left).toEqual('hi')
const right = eslox.run(`print nil or "yes";`)
expect(right).toEqual('yes')
})
it('should handle while statement control flows', () => {
eslox.run(`var a = 1;`)
eslox.run(`while (a < 3) { var a = a + 1; }`)
const result = eslox.run(`print a;`)
expect(result).toEqual(3)
})
it('should handle the ability to define no-op functions', () => {
eslox.run(`fun test(){}`)
const result = eslox.run(`print test;`)
expect(result).toEqual('<test()>')
})
it('should handle the ability to return values from functions', () => {
eslox.run(`fun calc(){ return 2 * 2; }`)
const result = eslox.run(`print calc();`)
expect(result).toEqual(4)
})
})