|
1 | 1 | import { getASTLocation } from "../astBreakpointLocation.js"; |
2 | 2 | import { getSource } from "../../../workers/parser/tests/helpers"; |
3 | | -import * as I from "immutable"; |
4 | | - |
5 | | -describe("ast", () => { |
6 | | - describe("valid location", () => { |
7 | | - it("returns the scope and offset", async () => { |
8 | | - const source = I.Map(getSource("math")); |
9 | | - const location = { line: 6, column: 0 }; |
10 | | - const astLocation = await getASTLocation(source, location); |
11 | | - expect(astLocation.name).toBe("math"); |
12 | | - expect(astLocation).toMatchSnapshot(); |
13 | | - }); |
| 3 | +import getSymbols from "../../../workers/parser/getSymbols"; |
| 4 | +import cases from "jest-in-case"; |
14 | 5 |
|
15 | | - it("returns name for a nested anon fn as the parent func", async () => { |
16 | | - const source = I.Map(getSource("outOfScope")); |
17 | | - const location = { line: 25, column: 0 }; |
18 | | - const astLocation = await getASTLocation(source, location); |
19 | | - expect(astLocation.name).toBe("outer"); |
20 | | - expect(astLocation).toMatchSnapshot(); |
21 | | - }); |
| 6 | +import * as I from "immutable"; |
22 | 7 |
|
23 | | - it("returns name for a nested named fn", async () => { |
24 | | - const source = I.Map(getSource("outOfScope")); |
25 | | - const location = { line: 5, column: 0 }; |
26 | | - const astLocation = await getASTLocation(source, location); |
27 | | - expect(astLocation.name).toBe("inner"); |
28 | | - expect(astLocation).toMatchSnapshot(); |
29 | | - }); |
| 8 | +async function setup({ fileName, location, functionName }) { |
| 9 | + const source = I.Map(getSource(fileName)); |
| 10 | + const symbols = getSymbols(source.toJS()); |
30 | 11 |
|
31 | | - it("returns name for an anon fn with a named variable", async () => { |
32 | | - const source = I.Map(getSource("outOfScope")); |
33 | | - const location = { line: 40, column: 0 }; |
34 | | - const astLocation = await getASTLocation(source, location); |
35 | | - expect(astLocation.name).toBe("globalDeclaration"); |
36 | | - expect(astLocation).toMatchSnapshot(); |
37 | | - }); |
38 | | - }); |
| 12 | + const astLocation = getASTLocation(source, symbols, location); |
| 13 | + expect(astLocation.name).toBe(functionName); |
| 14 | + expect(astLocation).toMatchSnapshot(); |
| 15 | +} |
39 | 16 |
|
40 | | - describe("invalid location", () => { |
41 | | - it("returns the scope name for global scope as undefined", async () => { |
42 | | - const source = I.Map(getSource("class")); |
43 | | - const location = { line: 10, column: 0 }; |
44 | | - const astLocation = await getASTLocation(source, location); |
45 | | - expect(astLocation.name).toBe(undefined); |
46 | | - expect(astLocation).toMatchSnapshot(); |
47 | | - }); |
| 17 | +describe("ast", () => { |
| 18 | + cases("valid location", setup, [ |
| 19 | + { |
| 20 | + name: "returns the scope and offset", |
| 21 | + fileName: "math", |
| 22 | + location: { line: 6, column: 0 }, |
| 23 | + functionName: "math" |
| 24 | + }, |
| 25 | + { |
| 26 | + name: "returns name for a nested anon fn as the parent func", |
| 27 | + fileName: "outOfScope", |
| 28 | + location: { line: 25, column: 0 }, |
| 29 | + functionName: "outer" |
| 30 | + }, |
| 31 | + { |
| 32 | + name: "returns name for a nested named fn", |
| 33 | + fileName: "outOfScope", |
| 34 | + location: { line: 5, column: 0 }, |
| 35 | + functionName: "inner" |
| 36 | + }, |
| 37 | + { |
| 38 | + name: "returns name for an anon fn with a named variable", |
| 39 | + fileName: "outOfScope", |
| 40 | + location: { line: 40, column: 0 }, |
| 41 | + functionName: "globalDeclaration" |
| 42 | + } |
| 43 | + ]); |
48 | 44 |
|
49 | | - it("returns name for an anon fn in global scope as undefined", async () => { |
50 | | - const source = I.Map(getSource("outOfScope")); |
51 | | - const location = { line: 44, column: 0 }; |
52 | | - const astLocation = await getASTLocation(source, location); |
53 | | - expect(astLocation.name).toBe(undefined); |
54 | | - expect(astLocation).toMatchSnapshot(); |
55 | | - }); |
56 | | - }); |
| 45 | + cases("invalid location", setup, [ |
| 46 | + { |
| 47 | + name: "returns the scope name for global scope as undefined", |
| 48 | + fileName: "class", |
| 49 | + location: { line: 10, column: 0 }, |
| 50 | + functionName: undefined |
| 51 | + }, |
| 52 | + { |
| 53 | + name: "returns name for an anon fn in global scope as undefined", |
| 54 | + fileName: "outOfScope", |
| 55 | + location: { line: 44, column: 0 }, |
| 56 | + functionName: undefined |
| 57 | + } |
| 58 | + ]); |
57 | 59 | }); |
0 commit comments