-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
119 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import AstPath from "../../src/common/ast-path.js"; | ||
|
||
const error = new Error("A dummy error"); | ||
const throwError = () => { | ||
throw error; | ||
}; | ||
|
||
describe("AstPath", () => { | ||
const ast = { | ||
property: { | ||
deep: { name: "deep" }, | ||
}, | ||
children: [{ index: 0 }, { index: 1 }], | ||
}; | ||
|
||
test("AstPath#call()", () => { | ||
const path = new AstPath(ast); | ||
|
||
expect(path.call(() => path.getValue(), "property")).toBe(ast.property); | ||
expect(() => path.call(throwError, "property")).toThrow(error); | ||
expect(path.stack.length).toBe(1); | ||
}); | ||
|
||
test("AstPath#callParent()", () => { | ||
const path = new AstPath(ast); | ||
path.stack.push("property", ast.property, "deep", ast.property.deep); | ||
|
||
expect(path.callParent(() => path.getValue())).toBe(ast.property); | ||
expect(() => path.callParent(throwError)).toThrow(error); | ||
expect(path.stack.length).toBe(5); | ||
}); | ||
|
||
test("AstPath#each()", () => { | ||
const path = new AstPath(ast); | ||
|
||
{ | ||
const called = []; | ||
path.each(() => called.push(path.getValue()), "children"); | ||
expect(called).toEqual(ast.children); | ||
} | ||
|
||
{ | ||
const called = []; | ||
expect(() => { | ||
path.each((_, index) => { | ||
if (index === 1) { | ||
throwError(); | ||
} | ||
called.push(path.getValue()); | ||
}, "children"); | ||
}).toThrow(error); | ||
expect(called.length).toBe(1); | ||
expect(path.stack.length).toBe(1); | ||
} | ||
}); | ||
|
||
test("AstPath#map()", () => { | ||
const path = new AstPath(ast); | ||
|
||
expect(path.map(() => path.getValue(), "children")).toEqual(ast.children); | ||
|
||
{ | ||
let result; | ||
expect(() => { | ||
result = path.map((_, index) => { | ||
if (index === 1) { | ||
throwError(); | ||
} | ||
return path.getValue(); | ||
}, "children"); | ||
}).toThrow(error); | ||
expect(result).toBeUndefined(); | ||
expect(path.stack.length).toBe(1); | ||
} | ||
}); | ||
}); |