Skip to content

Commit 189d25b

Browse files
committed
Add tests for slang
1 parent 31761b0 commit 189d25b

File tree

5 files changed

+391
-0
lines changed

5 files changed

+391
-0
lines changed

src/mocks/context.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { Context } from '../slang/types'
2+
import createContext from '../slang/createContext'
3+
4+
export function mockContext(): Context {
5+
return createContext()
6+
}
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Arrow function definition returns itself 1`] = `
4+
Object {
5+
"status": "finished",
6+
"value": ArrowClosure {
7+
"frame": Object {
8+
"environment": Object {
9+
"display": [Function],
10+
"error": [Function],
11+
"math_PI": 3.141592653589793,
12+
"math_sqrt": [Function],
13+
"parse_int": [Function],
14+
"prompt": [Function],
15+
"runtime": [Function],
16+
"undefined": undefined,
17+
},
18+
"name": "global",
19+
"parent": null,
20+
},
21+
"fun": [Function],
22+
"name": "Anonymous1",
23+
"node": Node {
24+
"__id": "node_12",
25+
"body": Node {
26+
"__id": "node_11",
27+
"end": 8,
28+
"loc": SourceLocation {
29+
"end": Position {
30+
"column": 8,
31+
"line": 1,
32+
},
33+
"start": Position {
34+
"column": 6,
35+
"line": 1,
36+
},
37+
},
38+
"raw": "42",
39+
"start": 6,
40+
"type": "Literal",
41+
"value": 42,
42+
},
43+
"end": 8,
44+
"expression": true,
45+
"generator": false,
46+
"id": null,
47+
"loc": SourceLocation {
48+
"end": Position {
49+
"column": 8,
50+
"line": 1,
51+
},
52+
"start": Position {
53+
"column": 0,
54+
"line": 1,
55+
},
56+
},
57+
"params": Array [],
58+
"start": 0,
59+
"type": "ArrowFunctionExpression",
60+
},
61+
},
62+
}
63+
`;
64+
65+
exports[`Arrow function definition returns itself 2`] = `
66+
ArrowClosure {
67+
"frame": Object {
68+
"environment": Object {
69+
"display": [Function],
70+
"error": [Function],
71+
"math_PI": 3.141592653589793,
72+
"math_sqrt": [Function],
73+
"parse_int": [Function],
74+
"prompt": [Function],
75+
"runtime": [Function],
76+
"undefined": undefined,
77+
},
78+
"name": "global",
79+
"parent": null,
80+
},
81+
"fun": [Function],
82+
"name": "Anonymous1",
83+
"node": Node {
84+
"__id": "node_12",
85+
"body": Node {
86+
"__id": "node_11",
87+
"end": 8,
88+
"loc": SourceLocation {
89+
"end": Position {
90+
"column": 8,
91+
"line": 1,
92+
},
93+
"start": Position {
94+
"column": 6,
95+
"line": 1,
96+
},
97+
},
98+
"raw": "42",
99+
"start": 6,
100+
"type": "Literal",
101+
"value": 42,
102+
},
103+
"end": 8,
104+
"expression": true,
105+
"generator": false,
106+
"id": null,
107+
"loc": SourceLocation {
108+
"end": Position {
109+
"column": 8,
110+
"line": 1,
111+
},
112+
"start": Position {
113+
"column": 0,
114+
"line": 1,
115+
},
116+
},
117+
"params": Array [],
118+
"start": 0,
119+
"type": "ArrowFunctionExpression",
120+
},
121+
}
122+
`;
123+
124+
exports[`Empty code returns undefined 1`] = `
125+
Object {
126+
"status": "finished",
127+
"value": undefined,
128+
}
129+
`;
130+
131+
exports[`Factorial arrow function 1`] = `
132+
Object {
133+
"status": "finished",
134+
"value": 120,
135+
}
136+
`;
137+
138+
exports[`Single boolean self-evaluates to itself 1`] = `
139+
Object {
140+
"status": "finished",
141+
"value": true,
142+
}
143+
`;
144+
145+
exports[`Single number self-evaluates to itself 1`] = `
146+
Object {
147+
"status": "finished",
148+
"value": 42,
149+
}
150+
`;
151+
152+
exports[`Single string self-evaluates to itself 1`] = `
153+
Object {
154+
"status": "finished",
155+
"value": "42",
156+
}
157+
`;
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Empty parse returns empty Program Node 1`] = `
4+
Node {
5+
"__id": "node_1",
6+
"body": Array [],
7+
"end": 0,
8+
"loc": SourceLocation {
9+
"end": Position {
10+
"column": 0,
11+
"line": 1,
12+
},
13+
"start": Position {
14+
"column": 0,
15+
"line": 1,
16+
},
17+
},
18+
"sourceType": "script",
19+
"start": 0,
20+
"type": "Program",
21+
}
22+
`;
23+
24+
exports[`Parse a single number 1`] = `
25+
Node {
26+
"__id": "node_7",
27+
"body": Array [
28+
Node {
29+
"__id": "node_6",
30+
"end": 3,
31+
"expression": Node {
32+
"__id": "node_5",
33+
"end": 2,
34+
"loc": SourceLocation {
35+
"end": Position {
36+
"column": 2,
37+
"line": 1,
38+
},
39+
"start": Position {
40+
"column": 0,
41+
"line": 1,
42+
},
43+
},
44+
"raw": "42",
45+
"start": 0,
46+
"type": "Literal",
47+
"value": 42,
48+
},
49+
"loc": SourceLocation {
50+
"end": Position {
51+
"column": 3,
52+
"line": 1,
53+
},
54+
"start": Position {
55+
"column": 0,
56+
"line": 1,
57+
},
58+
},
59+
"start": 0,
60+
"type": "ExpressionStatement",
61+
},
62+
],
63+
"end": 3,
64+
"loc": SourceLocation {
65+
"end": Position {
66+
"column": 3,
67+
"line": 1,
68+
},
69+
"start": Position {
70+
"column": 0,
71+
"line": 1,
72+
},
73+
},
74+
"sourceType": "script",
75+
"start": 0,
76+
"type": "Program",
77+
}
78+
`;
79+
80+
exports[`Parse a single string 1`] = `
81+
Node {
82+
"__id": "node_4",
83+
"body": Array [
84+
Node {
85+
"__id": "node_3",
86+
"directive": "42",
87+
"end": 5,
88+
"expression": Node {
89+
"__id": "node_2",
90+
"end": 4,
91+
"loc": SourceLocation {
92+
"end": Position {
93+
"column": 4,
94+
"line": 1,
95+
},
96+
"start": Position {
97+
"column": 0,
98+
"line": 1,
99+
},
100+
},
101+
"raw": "'42'",
102+
"start": 0,
103+
"type": "Literal",
104+
"value": "42",
105+
},
106+
"loc": SourceLocation {
107+
"end": Position {
108+
"column": 5,
109+
"line": 1,
110+
},
111+
"start": Position {
112+
"column": 0,
113+
"line": 1,
114+
},
115+
},
116+
"start": 0,
117+
"type": "ExpressionStatement",
118+
},
119+
],
120+
"end": 5,
121+
"loc": SourceLocation {
122+
"end": Position {
123+
"column": 5,
124+
"line": 1,
125+
},
126+
"start": Position {
127+
"column": 0,
128+
"line": 1,
129+
},
130+
},
131+
"sourceType": "script",
132+
"start": 0,
133+
"type": "Program",
134+
}
135+
`;

src/slang/__tests__/index.ts

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import { stripIndent } from 'common-tags'
2+
3+
import { mockContext } from '../../mocks/context'
4+
import { runInContext } from '../index'
5+
6+
test('Empty code returns undefined', () => {
7+
const code = ''
8+
const context = mockContext()
9+
const promise = runInContext(code, context)
10+
return promise.then(obj => {
11+
expect(obj).toMatchSnapshot()
12+
expect(obj.status).toBe('finished')
13+
expect(obj.value).toBe(undefined)
14+
})
15+
})
16+
17+
test('Single string self-evaluates to itself', () => {
18+
const code = "'42';"
19+
const context = mockContext()
20+
const promise = runInContext(code, context)
21+
return promise.then(obj => {
22+
expect(obj).toMatchSnapshot()
23+
expect(obj.status).toBe('finished')
24+
expect(obj.value).toBe('42')
25+
})
26+
})
27+
28+
test('Single number self-evaluates to itself', () => {
29+
const code = '42;'
30+
const context = mockContext()
31+
const promise = runInContext(code, context)
32+
return promise.then(obj => {
33+
expect(obj).toMatchSnapshot()
34+
expect(obj.status).toBe('finished')
35+
expect(obj.value).toBe(42)
36+
})
37+
})
38+
39+
test('Single boolean self-evaluates to itself', () => {
40+
const code = 'true;'
41+
const context = mockContext()
42+
const promise = runInContext(code, context)
43+
return promise.then(obj => {
44+
expect(obj).toMatchSnapshot()
45+
expect(obj.status).toBe('finished')
46+
expect(obj.value).toBe(true)
47+
})
48+
})
49+
50+
test('Arrow function definition returns itself', () => {
51+
const code = '() => 42;'
52+
const context = mockContext()
53+
const promise = runInContext(code, context)
54+
return promise.then(obj => {
55+
expect(obj).toMatchSnapshot()
56+
expect(obj.status).toBe('finished')
57+
expect(obj.value).toMatchSnapshot()
58+
})
59+
})
60+
61+
test('Factorial arrow function', () => {
62+
const code = stripIndent`
63+
const fac = (i) => i === 1 ? 1 : i * fac(i-1);
64+
fac(5);
65+
`
66+
const context = mockContext()
67+
const promise = runInContext(code, context)
68+
return promise.then(obj => {
69+
expect(obj).toMatchSnapshot()
70+
expect(obj.status).toBe('finished')
71+
expect(obj.value).toBe(120)
72+
})
73+
})

src/slang/__tests__/parser.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { mockContext } from '../../mocks/context'
2+
import { parse } from '../parser'
3+
4+
test('Empty parse returns empty Program Node', () => {
5+
const context = mockContext()
6+
const program = parse('', context)
7+
expect(program).toMatchSnapshot()
8+
})
9+
10+
test('Parse a single string', () => {
11+
const context = mockContext()
12+
const program = parse("'42';", context)
13+
expect(program).toMatchSnapshot()
14+
})
15+
16+
test('Parse a single number', () => {
17+
const context = mockContext()
18+
const program = parse('42;', context)
19+
expect(program).toMatchSnapshot()
20+
})

0 commit comments

Comments
 (0)