| 1 | // Generated by continue |
| 2 | |
| 3 | import { findInAst } from "./findInAst"; |
| 4 | import Parser from "web-tree-sitter"; |
| 5 | |
| 6 | describe("findInAst", () => { |
| 7 | it("should find a node matching the criterion", () => { |
| 8 | const mockAst = createMockSyntaxNode("root", [ |
| 9 | createMockSyntaxNode("child1", []), |
| 10 | createMockSyntaxNode("function", [createMockSyntaxNode("child2", [])]), |
| 11 | ]); |
| 12 | |
| 13 | const criterion = (node: Parser.SyntaxNode) => node.type === "function"; |
| 14 | |
| 15 | const result = findInAst(mockAst, criterion); |
| 16 | |
| 17 | expect(result).not.toBeNull(); |
| 18 | expect(result!.type).toBe("function"); |
| 19 | }); |
| 20 | |
| 21 | it("should return null if no node matches the criterion", () => { |
| 22 | const mockAst = createMockSyntaxNode("root", [ |
| 23 | createMockSyntaxNode("child1", []), |
| 24 | createMockSyntaxNode("child2", []), |
| 25 | ]); |
| 26 | |
| 27 | const criterion = (node: Parser.SyntaxNode) => node.type === "function"; |
| 28 | |
| 29 | const result = findInAst(mockAst, criterion); |
| 30 | |
| 31 | expect(result).toBeNull(); |
| 32 | }); |
| 33 | |
| 34 | it("should find a nested node matching the criterion", () => { |
| 35 | const mockAst = createMockSyntaxNode("root", [ |
| 36 | createMockSyntaxNode("child1", [ |
| 37 | createMockSyntaxNode("child2", [createMockSyntaxNode("function", [])]), |
| 38 | ]), |
| 39 | createMockSyntaxNode("child3", []), |
| 40 | ]); |
| 41 | |
| 42 | const criterion = (node: Parser.SyntaxNode) => node.type === "function"; |
| 43 | |
| 44 | const result = findInAst(mockAst, criterion); |
| 45 | |
| 46 | expect(result).not.toBeNull(); |
| 47 | expect(result!.type).toBe("function"); |
| 48 | }); |
| 49 | |
| 50 | it("should return the first node matching the criterion in depth-first traversal", () => { |
| 51 | const firstFunctionNode = createMockSyntaxNode("function", []); |
| 52 | const secondFunctionNode = createMockSyntaxNode("function", []); |
| 53 | const mockAst = createMockSyntaxNode("root", [ |
| 54 | firstFunctionNode, // First function node |
| 55 | createMockSyntaxNode("child1", [ |
| 56 | secondFunctionNode, // Second function node |
| 57 | ]), |
| 58 | ]); |
| 59 | |
| 60 | const criterion = (node: Parser.SyntaxNode) => node.type === "function"; |
| 61 | |
| 62 | const result = findInAst(mockAst, criterion); |
| 63 | |
| 64 | expect(result).not.toBeNull(); |
| 65 | expect(result!.type).toBe("function"); |
| 66 | expect(JSON.stringify(result)).toBe(JSON.stringify(firstFunctionNode)); // Ensure it's the first function node |
| 67 | }); |
| 68 | |
| 69 | it("should return the root node if it matches the criterion", () => { |
| 70 | const mockAst = createMockSyntaxNode("function", [ |
| 71 | createMockSyntaxNode("child1", []), |
| 72 | createMockSyntaxNode("child2", []), |
| 73 | ]); |
| 74 | |
| 75 | const criterion = (node: Parser.SyntaxNode) => node.type === "function"; |
| 76 | |
| 77 | const result = findInAst(mockAst, criterion); |
| 78 | |
| 79 | expect(result).not.toBeNull(); |
| 80 | expect(result!.type).toBe("function"); |
| 81 | expect(result).toBe(mockAst); |
| 82 | }); |
| 83 | |
| 84 | it("should return null when searching an empty tree", () => { |
| 85 | const mockAst = createMockSyntaxNode("root", []); |
| 86 | |
| 87 | const criterion = (node: Parser.SyntaxNode) => node.type === "function"; |
| 88 | |
| 89 | const result = findInAst(mockAst, criterion); |
| 90 | |
| 91 | expect(result).toBeNull(); |
| 92 | }); |
| 93 | }); |
| 94 | |
| 95 | function createMockSyntaxNode( |
| 96 | type: string, |
| 97 | children: Parser.SyntaxNode[], |
| 98 | ): Parser.SyntaxNode { |
| 99 | // Define minimal properties of SyntaxNode required for the test |
| 100 | const node: Partial<Parser.SyntaxNode> = { |
| 101 | type, |
| 102 | children, |
| 103 | childCount: children.length, |
| 104 | child: (index: number) => { |
| 105 | return children[index] || null; |
| 106 | }, |
| 107 | }; |
| 108 | return node as Parser.SyntaxNode; |
| 109 | } |