| 1 | //go:build treesitter && cgo |
| 2 | |
| 3 | package builtin |
| 4 | |
| 5 | import ( |
| 6 | "path/filepath" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | ) |
| 10 | |
| 11 | func TestCodeIndexTreeSitterTypeScriptMethods(t *testing.T) { |
| 12 | if !codeIndexTreeSitterEnabled() { |
| 13 | t.Fatal("tree-sitter code_index is not enabled") |
| 14 | } |
| 15 | root := t.TempDir() |
| 16 | mkfile(t, filepath.Join(root, "src", "client.ts"), `export abstract class Client { |
| 17 | async loadUser(id: string) { |
| 18 | return id |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | export const makeClient = () => new Client() |
| 23 | `) |
| 24 | |
| 25 | out := runTool(t, codeIndex{workDir: root}, map[string]any{ |
| 26 | "action": "outline", |
| 27 | "path": ".", |
| 28 | }) |
| 29 | for _, want := range []string{ |
| 30 | "src/client.ts:1: class Client", |
| 31 | "src/client.ts:2: method Client.loadUser", |
| 32 | "src/client.ts:7: func makeClient", |
| 33 | } { |
| 34 | if !strings.Contains(out, want) { |
| 35 | t.Fatalf("tree-sitter code_index missing %q; got:\n%s", want, out) |
| 36 | } |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | func TestCodeIndexTreeSitterPythonAndRust(t *testing.T) { |
| 41 | root := t.TempDir() |
| 42 | mkfile(t, filepath.Join(root, "pkg", "worker.py"), `class Worker: |
| 43 | async def run(self): |
| 44 | pass |
| 45 | `) |
| 46 | mkfile(t, filepath.Join(root, "src", "lib.rs"), `pub struct Store {} |
| 47 | pub trait Repository {} |
| 48 | pub fn load_store() {} |
| 49 | `) |
| 50 | |
| 51 | out := runTool(t, codeIndex{workDir: root}, map[string]any{ |
| 52 | "action": "outline", |
| 53 | "path": ".", |
| 54 | }) |
| 55 | for _, want := range []string{ |
| 56 | "pkg/worker.py:1: class Worker", |
| 57 | "pkg/worker.py:2: func run", |
| 58 | "src/lib.rs:1: struct Store", |
| 59 | "src/lib.rs:2: trait Repository", |
| 60 | "src/lib.rs:3: fn load_store", |
| 61 | } { |
| 62 | if !strings.Contains(out, want) { |
| 63 | t.Fatalf("tree-sitter code_index missing %q; got:\n%s", want, out) |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 |