|
| 1 | +import { describe, it, expect } from "vitest"; |
| 2 | +import { |
| 3 | + generateExportMap, |
| 4 | + getExportExtractor, |
| 5 | + UnsupportedExtensionForExportExtractorError, |
| 6 | +} from "./index"; |
| 7 | +import Parser from "tree-sitter"; |
| 8 | +import Python from "tree-sitter-python"; |
| 9 | + |
| 10 | +// Set up the Tree-sitter parser for Python |
| 11 | +const parser = new Parser(); |
| 12 | +parser.setLanguage(Python); |
| 13 | + |
| 14 | +describe("generateExportMap", () => { |
| 15 | + it("should generate an export map for a valid Python file", () => { |
| 16 | + const files = [{ path: "test.py", code: "def my_function(): pass" }]; |
| 17 | + const exportMap = generateExportMap(files); |
| 18 | + |
| 19 | + expect(exportMap).toHaveProperty("test.py"); |
| 20 | + expect(exportMap["test.py"].couldNotProcess).toBe(false); |
| 21 | + expect(exportMap["test.py"].exportStatements.length).toBe(1); |
| 22 | + expect( |
| 23 | + exportMap["test.py"].exportStatements[0].members[0].identifierNode.text, |
| 24 | + ).toBe("my_function"); |
| 25 | + }); |
| 26 | + |
| 27 | + it("should return an empty export map for a Python file with no exports", () => { |
| 28 | + const files = [{ path: "empty.py", code: "" }]; |
| 29 | + const exportMap = generateExportMap(files); |
| 30 | + |
| 31 | + expect(exportMap).toHaveProperty("empty.py"); |
| 32 | + expect(exportMap["empty.py"].language).toBe(Python.name); |
| 33 | + expect(exportMap["empty.py"].couldNotProcess).toBe(false); |
| 34 | + expect(exportMap["empty.py"].exportStatements).toEqual([]); |
| 35 | + }); |
| 36 | + |
| 37 | + it("should handle multiple files", () => { |
| 38 | + const files = [ |
| 39 | + { path: "file1.py", code: "def my_function(): pass" }, |
| 40 | + { path: "file2.py", code: "class MyClass: pass" }, |
| 41 | + ]; |
| 42 | + const exportMap = generateExportMap(files); |
| 43 | + |
| 44 | + expect(Object.keys(exportMap)).toEqual(["file1.py", "file2.py"]); |
| 45 | + expect(exportMap["file1.py"].language).toBe(Python.name); |
| 46 | + expect(exportMap["file1.py"].couldNotProcess).toBe(false); |
| 47 | + expect(exportMap["file1.py"].exportStatements.length).toBe(1); |
| 48 | + expect( |
| 49 | + exportMap["file1.py"].exportStatements[0].members[0].identifierNode.text, |
| 50 | + ).toBe("my_function"); |
| 51 | + expect(exportMap["file2.py"].language).toBe(Python.name); |
| 52 | + expect(exportMap["file2.py"].couldNotProcess).toBe(false); |
| 53 | + expect(exportMap["file2.py"].exportStatements.length).toBe(1); |
| 54 | + expect( |
| 55 | + exportMap["file2.py"].exportStatements[0].members[0].identifierNode.text, |
| 56 | + ).toBe("MyClass"); |
| 57 | + }); |
| 58 | + |
| 59 | + it("should return an error for unsupported file extensions", () => { |
| 60 | + const files = [{ path: "script.js", code: "function foo() {}" }]; |
| 61 | + const exportMap = generateExportMap(files); |
| 62 | + |
| 63 | + expect(exportMap).toHaveProperty("script.js"); |
| 64 | + expect(exportMap["script.js"].language).toBe("unknown"); |
| 65 | + expect(exportMap["script.js"].couldNotProcess).toBe(true); |
| 66 | + expect(exportMap["script.js"].exportStatements).toEqual([]); |
| 67 | + }); |
| 68 | + |
| 69 | + it("should handle syntax errors gracefully", () => { |
| 70 | + // tree sitter struggle to parse long lines |
| 71 | + const code = ".".repeat(100000); |
| 72 | + const files = [ |
| 73 | + { |
| 74 | + path: "broken.py", |
| 75 | + code, |
| 76 | + }, |
| 77 | + ]; |
| 78 | + const exportMap = generateExportMap(files); |
| 79 | + |
| 80 | + expect(exportMap).toHaveProperty("broken.py"); |
| 81 | + expect(exportMap["broken.py"].language).toBe(Python.name); |
| 82 | + expect(exportMap["broken.py"].couldNotProcess).toBe(true); |
| 83 | + expect(exportMap["broken.py"].exportStatements).toEqual([]); |
| 84 | + }); |
| 85 | + |
| 86 | + it("should throw an error for completely unknown file types", () => { |
| 87 | + expect(() => getExportExtractor("unknown.xyz")).toThrow( |
| 88 | + UnsupportedExtensionForExportExtractorError, |
| 89 | + ); |
| 90 | + }); |
| 91 | + |
| 92 | + it("should extract functions, classes, and assignments together", () => { |
| 93 | + const files = [ |
| 94 | + { |
| 95 | + path: "combined.py", |
| 96 | + code: ` |
| 97 | +def my_function(): pass |
| 98 | +class MyClass: pass |
| 99 | +MY_CONSTANT = 42 |
| 100 | + `.trim(), |
| 101 | + }, |
| 102 | + ]; |
| 103 | + |
| 104 | + const exportMap = generateExportMap(files); |
| 105 | + |
| 106 | + expect(exportMap).toHaveProperty("combined.py"); |
| 107 | + expect(exportMap["combined.py"].language).toBe(Python.name); |
| 108 | + expect(exportMap["combined.py"].couldNotProcess).toBe(false); |
| 109 | + expect(exportMap["combined.py"].exportStatements.length).toBe(3); |
| 110 | + |
| 111 | + const [funcExport, classExport, constExport] = |
| 112 | + exportMap["combined.py"].exportStatements; |
| 113 | + |
| 114 | + expect(funcExport.members[0].identifierNode.text).toBe("my_function"); |
| 115 | + expect(classExport.members[0].identifierNode.text).toBe("MyClass"); |
| 116 | + expect(constExport.members[0].identifierNode.text).toBe("MY_CONSTANT"); |
| 117 | + }); |
| 118 | +}); |
0 commit comments