|
1 | 1 | import { Chapter } from '../types' |
2 | 2 | import { expectDisplayResult, expectParsedError } from '../utils/testing' |
3 | 3 |
|
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[]] |
31 | 7 |
|
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 | +] |
37 | 54 |
|
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 | +) |
43 | 58 |
|
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."` |
47 | 62 | ) |
48 | 63 | }) |
49 | 64 |
|
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"` |
65 | 68 | ) |
66 | 69 | }) |
0 commit comments