| 1 | package builtin |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "go/parser" |
| 6 | "go/token" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | ) |
| 12 | |
| 13 | func TestDeleteSymbolGoFunc(t *testing.T) { |
| 14 | dir := t.TempDir() |
| 15 | f := filepath.Join(dir, "example.go") |
| 16 | src := "package example\n\nfunc Greet() string {\n\treturn \"hello\"\n}\n\nfunc Farewell() string {\n\treturn \"bye\"\n}\n" |
| 17 | os.WriteFile(f, []byte(src), 0o644) |
| 18 | |
| 19 | out := runTool(t, deleteSymbol{}, map[string]any{"path": f, "name": "Greet"}) |
| 20 | if !strings.Contains(out, "--- a/") || !strings.Contains(out, "+++ b/") { |
| 21 | t.Errorf("expected unified diff, got %q", out) |
| 22 | } |
| 23 | got, _ := os.ReadFile(f) |
| 24 | if strings.Contains(string(got), "Greet") { |
| 25 | t.Error("Greet was not deleted") |
| 26 | } |
| 27 | if !strings.Contains(string(got), "Farewell") { |
| 28 | t.Error("Farewell was incorrectly deleted") |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | func TestDeleteSymbolMethod(t *testing.T) { |
| 33 | dir := t.TempDir() |
| 34 | f := filepath.Join(dir, "example.go") |
| 35 | src := "package example\n\ntype Server struct{}\n\nfunc (s *Server) Start() error { return nil }\nfunc (s *Server) Stop() error { return nil }\n" |
| 36 | os.WriteFile(f, []byte(src), 0o644) |
| 37 | |
| 38 | out := runTool(t, deleteSymbol{}, map[string]any{"path": f, "name": "Start", "kind": "method", "parent": "Server"}) |
| 39 | if !strings.Contains(out, "--- a/") { |
| 40 | t.Errorf("expected unified diff, got %q", out) |
| 41 | } |
| 42 | got, _ := os.ReadFile(f) |
| 43 | if strings.Contains(string(got), "Start") { |
| 44 | t.Error("Start was not deleted") |
| 45 | } |
| 46 | if !strings.Contains(string(got), "Stop") { |
| 47 | t.Error("Stop was incorrectly deleted") |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | func TestDeleteSymbolType(t *testing.T) { |
| 52 | dir := t.TempDir() |
| 53 | f := filepath.Join(dir, "example.go") |
| 54 | src := "package example\n\ntype User struct {\n\tName string\n}\n\ntype Admin struct {\n\tRole string\n}\n" |
| 55 | os.WriteFile(f, []byte(src), 0o644) |
| 56 | |
| 57 | out := runTool(t, deleteSymbol{}, map[string]any{"path": f, "name": "User", "kind": "type"}) |
| 58 | if !strings.Contains(out, "--- a/") { |
| 59 | t.Errorf("expected unified diff, got %q", out) |
| 60 | } |
| 61 | got, _ := os.ReadFile(f) |
| 62 | if strings.Contains(string(got), "type User") { |
| 63 | t.Error("User type was not deleted") |
| 64 | } |
| 65 | if !strings.Contains(string(got), "Admin") { |
| 66 | t.Error("Admin was incorrectly deleted") |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | func TestDeleteSymbolMultiLineValueSpec(t *testing.T) { |
| 71 | dir := t.TempDir() |
| 72 | f := filepath.Join(dir, "example.go") |
| 73 | src := "package example\n\nvar Tools = []string{\n\t\"a\",\n\t\"b\",\n}\n\nconst Banner = `line1\nline2`\n\nfunc Keep() {}\n" |
| 74 | os.WriteFile(f, []byte(src), 0o644) |
| 75 | |
| 76 | runTool(t, deleteSymbol{}, map[string]any{"path": f, "name": "Tools", "kind": "var"}) |
| 77 | runTool(t, deleteSymbol{}, map[string]any{"path": f, "name": "Banner", "kind": "const"}) |
| 78 | |
| 79 | got, _ := os.ReadFile(f) |
| 80 | if _, err := parser.ParseFile(token.NewFileSet(), f, got, parser.ParseComments); err != nil { |
| 81 | t.Fatalf("result no longer parses: %v\n%s", err, got) |
| 82 | } |
| 83 | for _, leftover := range []string{"Tools", "Banner", "line1", "\"a\""} { |
| 84 | if strings.Contains(string(got), leftover) { |
| 85 | t.Errorf("multi-line value left %q dangling:\n%s", leftover, got) |
| 86 | } |
| 87 | } |
| 88 | if !strings.Contains(string(got), "func Keep") { |
| 89 | t.Error("Keep was incorrectly deleted") |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | func TestDeleteSymbolMultiMatch(t *testing.T) { |
| 94 | dir := t.TempDir() |
| 95 | f := filepath.Join(dir, "example.go") |
| 96 | src := "package example\n\ntype Foo struct{}\ntype Foo int\n" |
| 97 | os.WriteFile(f, []byte(src), 0o644) |
| 98 | |
| 99 | _, err := deleteSymbol{}.Execute(context.Background(), argsJSON(t, map[string]any{"path": f, "name": "Foo"})) |
| 100 | if err == nil || !strings.Contains(err.Error(), "Multiple") { |
| 101 | t.Errorf("expected multi-match error, got %v", err) |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | func TestDeleteSymbolRejectsMultiNameValueSpec(t *testing.T) { |
| 106 | dir := t.TempDir() |
| 107 | f := filepath.Join(dir, "example.go") |
| 108 | src := "package example\n\nvar A, B = 1, 2\nconst C, D = 3, 4\n" |
| 109 | os.WriteFile(f, []byte(src), 0o644) |
| 110 | |
| 111 | _, err := deleteSymbol{}.Execute(context.Background(), argsJSON(t, map[string]any{ |
| 112 | "path": f, "name": "A", "kind": "var", |
| 113 | })) |
| 114 | if err == nil || !strings.Contains(err.Error(), "multi-name") { |
| 115 | t.Fatalf("expected multi-name var error, got %v", err) |
| 116 | } |
| 117 | _, err = deleteSymbol{}.Execute(context.Background(), argsJSON(t, map[string]any{ |
| 118 | "path": f, "name": "C", "kind": "const", |
| 119 | })) |
| 120 | if err == nil || !strings.Contains(err.Error(), "multi-name") { |
| 121 | t.Fatalf("expected multi-name const error, got %v", err) |
| 122 | } |
| 123 | got, _ := os.ReadFile(f) |
| 124 | if string(got) != src { |
| 125 | t.Errorf("file modified despite rejected delete:\n got %q\nwant %q", got, src) |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | func TestDeleteSymbolNotFound(t *testing.T) { |
| 130 | dir := t.TempDir() |
| 131 | f := filepath.Join(dir, "example.go") |
| 132 | os.WriteFile(f, []byte("package example\n"), 0o644) |
| 133 | |
| 134 | _, err := deleteSymbol{}.Execute(context.Background(), argsJSON(t, map[string]any{"path": f, "name": "NoSuchFunc"})) |
| 135 | if err == nil || !strings.Contains(err.Error(), "not found") { |
| 136 | t.Errorf("expected not-found error, got %v", err) |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | func TestDeleteSymbolNonGoFile(t *testing.T) { |
| 141 | dir := t.TempDir() |
| 142 | f := filepath.Join(dir, "script.py") |
| 143 | os.WriteFile(f, []byte("def hello():\n pass\n"), 0o644) |
| 144 | |
| 145 | _, err := deleteSymbol{}.Execute(context.Background(), argsJSON(t, map[string]any{"path": f, "name": "hello"})) |
| 146 | if err == nil || !strings.Contains(err.Error(), "only supports Go") { |
| 147 | t.Errorf("expected unsupported-language error, got %v", err) |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | func TestDeleteSymbolPreview(t *testing.T) { |
| 152 | dir := t.TempDir() |
| 153 | f := filepath.Join(dir, "example.go") |
| 154 | src := "package main\n\nfunc main() {}\n\nfunc helper() {}\n" |
| 155 | os.WriteFile(f, []byte(src), 0o644) |
| 156 | |
| 157 | change, err := deleteSymbol{}.Preview(argsJSON(t, map[string]any{"path": f, "name": "helper"})) |
| 158 | if err != nil { |
| 159 | t.Fatalf("Preview: %v", err) |
| 160 | } |
| 161 | if !strings.Contains(change.Diff, "helper") { |
| 162 | t.Errorf("diff missing helper:\n%s", change.Diff) |
| 163 | } |
| 164 | got, _ := os.ReadFile(f) |
| 165 | if string(got) != src { |
| 166 | t.Errorf("Preview mutated the file:\n got %q\nwant %q", got, src) |
| 167 | } |
| 168 | } |
| 169 |