Skip to content

Commit 3efe0cf

Browse files
committed
Update previously broken tests relating to display
1 parent 1f8e451 commit 3efe0cf

File tree

8 files changed

+340
-94
lines changed

8 files changed

+340
-94
lines changed

src/__tests__/display.ts

Lines changed: 58 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,69 @@
11
import { Chapter } from '../types'
22
import { expectDisplayResult, expectParsedError } from '../utils/testing'
33

4-
test('display throw error if second argument is non-string when used', () => {
5-
return expectParsedError(`display(31072020, 0xDEADC0DE);`).toMatchInlineSnapshot(
6-
`"Line 1: TypeError: display expects the second argument to be a string"`
7-
)
8-
})
9-
10-
test('display second argument can be a string', () => {
11-
return expectDisplayResult(`display(31072020, "my_first_String");`).toMatchInlineSnapshot(
12-
`Array []`
13-
)
14-
})
15-
16-
test('display can be used to display numbers', () => {
17-
return expectDisplayResult(`display(0);`).toMatchInlineSnapshot(`Array []`)
18-
})
19-
20-
test('display can be used to display funny numbers', () => {
21-
return expectDisplayResult(
22-
`display(1e38); display(NaN); display(Infinity);`
23-
).toMatchInlineSnapshot(`Array []`)
24-
})
25-
26-
test('display can be used to display (escaped) strings', () => {
27-
return expectDisplayResult(
28-
`display("Tom's assisstant said: \\"tuna.\\"");`
29-
).toMatchInlineSnapshot(`Array []`)
30-
})
4+
type TestCase =
5+
| [desc: string, code: string, expectedDisplay: string[], chapter: Chapter]
6+
| [desc: string, code: string, expectedDisplay: string[]]
317

32-
test('raw_display can be used to display (unescaped) strings directly', () => {
33-
return expectDisplayResult(
34-
`raw_display("Tom's assisstant said: \\"tuna.\\"");`
35-
).toMatchInlineSnapshot(`Array []`)
36-
})
8+
const testCases: TestCase[] = [
9+
[
10+
'display second argument can be a string',
11+
`display(31072020, "my_first_String");`,
12+
['my_first_String 31072020']
13+
],
14+
['display can be used to display numbers', 'display(0);', ['0']],
15+
[
16+
'display can be used to display funny numbers',
17+
`display(1e38); display(NaN); display(Infinity);`,
18+
['1e+38', 'NaN', 'Infinity']
19+
],
20+
[
21+
'display can be used to display (escaped) strings',
22+
`display("Tom's assistant said: \\"tuna.\\"");`,
23+
[`"Tom's assistant said: \\"tuna.\\""`]
24+
],
25+
[
26+
'raw_display can be used to display (unescaped) strings directly',
27+
`raw_display("Tom's assistant said: \\"tuna.\\"");`,
28+
[`Tom's assistant said: "tuna."`]
29+
],
30+
[
31+
'display can be used to display functions',
32+
`display(x => x); display((x, y) => x + y);`,
33+
['x => x', '(x, y) => x + y']
34+
],
35+
[
36+
'display can be used to display lists',
37+
`display(list(1, 2));`,
38+
['[1, [2, null]]'],
39+
Chapter.SOURCE_2
40+
],
41+
[
42+
'display can be used to display arrays',
43+
`display([1, 2, [4, 5]]);`,
44+
['[1, 2, [4, 5]]'],
45+
Chapter.SOURCE_3
46+
],
47+
[
48+
'display can be used to display objects',
49+
`display({a: 1, b: 2, c: {d: 3}});`,
50+
['{"a": 1, "b": 2, "c": {"d": 3}}'],
51+
Chapter.LIBRARY_PARSER
52+
]
53+
]
3754

38-
test('display can be used to display functions', () => {
39-
return expectDisplayResult(`display(x => x); display((x, y) => x + y);`).toMatchInlineSnapshot(
40-
`Array []`
41-
)
42-
})
55+
test.each(testCases)('%s', (_, code, expectedDisplay, chapter = undefined) =>
56+
expectDisplayResult(code, chapter).toMatchObject(expectedDisplay)
57+
)
4358

44-
test('display can be used to display lists', () => {
45-
return expectDisplayResult(`display(list(1, 2));`, Chapter.SOURCE_2).toMatchInlineSnapshot(
46-
`Array []`
59+
test('display with no arguments throws an error', () => {
60+
return expectParsedError(`display();`, Chapter.LIBRARY_PARSER).toMatchInlineSnapshot(
61+
`"Line 1: Expected 1 or more arguments, but got 0."`
4762
)
4863
})
4964

50-
test('display can be used to display arrays', () => {
51-
return expectDisplayResult(`display([1, 2, [4, 5]]);`, {
52-
chapter: Chapter.SOURCE_3
53-
}).toMatchInlineSnapshot(`Array []`)
54-
})
55-
56-
test('display can be used to display objects', () => {
57-
return expectDisplayResult(`display({a: 1, b: 2, c: {d: 3}});`, {
58-
chapter: Chapter.LIBRARY_PARSER
59-
}).toMatchInlineSnapshot(`Array []`)
60-
})
61-
62-
test('display with no arguments throws an error', () => {
63-
return expectParsedError(`display();`, { chapter: Chapter.LIBRARY_PARSER }).toMatchInlineSnapshot(
64-
`"Line 1: Expected 1 or more arguments, but got 0."`
65+
test('display throw error if second argument is non-string when used', () => {
66+
return expectParsedError(`display(31072020, 0xDEADC0DE);`).toMatchInlineSnapshot(
67+
`"Line 1: TypeError: display expects the second argument to be a string"`
6568
)
6669
})

src/parser/__tests__/allowed-syntax.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,10 @@ describe.each([
343343

344344
test('Test stdlib parser', () => {
345345
const parseSnippet = `parse(${JSON.stringify(snippet)});`
346-
return expectFinishedResult(parseSnippet, chapter).toMatchSnapshot()
346+
return expectFinishedResult(
347+
parseSnippet,
348+
Math.max(Chapter.SOURCE_4, chapter)
349+
).toMatchSnapshot()
347350
})
348351
}
349352

src/parser/__tests__/tokenize.ts

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,55 @@ test('tokenize works for a good program', () => {
1717
`) +
1818
'));',
1919
{ chapter: Chapter.SOURCE_4 }
20-
).toMatchInlineSnapshot(`Array []`)
20+
).toMatchInlineSnapshot(`
21+
Array [
22+
"list(\\"function\\",
23+
\\"f\\",
24+
\\"(\\",
25+
\\"x\\",
26+
\\")\\",
27+
\\"{\\",
28+
\\"const\\",
29+
\\"y\\",
30+
\\"=\\",
31+
\\"x\\",
32+
\\"+\\",
33+
\\"x\\",
34+
\\"+\\",
35+
\\"x\\",
36+
\\"+\\",
37+
\\"\\\\\\"123\\\\\\"\\",
38+
\\";\\",
39+
\\"return\\",
40+
\\"z\\",
41+
\\"=>\\",
42+
\\"(\\",
43+
\\"a\\",
44+
\\",\\",
45+
\\"b\\",
46+
\\")\\",
47+
\\"=>\\",
48+
\\"{\\",
49+
\\"let\\",
50+
\\"w\\",
51+
\\"=\\",
52+
\\"z\\",
53+
\\"+\\",
54+
\\"1\\",
55+
\\";\\",
56+
\\"return\\",
57+
\\"y\\",
58+
\\";\\",
59+
\\"}\\",
60+
\\";\\",
61+
\\"}\\",
62+
\\"f\\",
63+
\\"(\\",
64+
\\"\\\\\\"55\\\\\\"\\",
65+
\\")\\",
66+
\\";\\")",
67+
]
68+
`)
2169
})
2270

2371
test('tokenize works even with parse errors', () => {
@@ -29,7 +77,11 @@ test('tokenize works even with parse errors', () => {
2977
`) +
3078
'));',
3179
{ chapter: Chapter.SOURCE_4 }
32-
).toMatchInlineSnapshot(`Array []`)
80+
).toMatchInlineSnapshot(`
81+
Array [
82+
"list(\\"function\\", \\"f\\", \\"(\\", \\"x\\", \\")\\", \\"{\\", \\";\\", \\";\\", \\";\\", \\";\\", \\";\\", \\";\\", \\";\\")",
83+
]
84+
`)
3385
})
3486

3587
test('tokenize prints suitable error when tokenization fails', () => {

src/runner/__tests__/execMethod.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ import * as validator from '../../validator/validator'
1010

1111
jest.spyOn(validator, 'validateAndAnnotate')
1212

13+
// Required since the Typed variant tries to load modules
14+
jest.mock('../../modules/loader/loaders')
15+
1316
jest.mock('../sourceRunner', () => {
1417
const { default: actualRunners } = jest.requireActual('../sourceRunner')
1518

@@ -30,9 +33,6 @@ jest.mock('../sourceRunner', () => {
3033
}
3134
})
3235

33-
// Required since Typed variant tries to load modules
34-
jest.mock('../../modules/loader')
35-
3636
beforeEach(() => {
3737
jest.clearAllMocks()
3838
})

0 commit comments

Comments
 (0)