package app import ( "os" "path/filepath" "strings" "testing" "github.com/egoist/mygo/ui/editor" ) func TestReplaceInTextLiteral(t *testing.T) { re, err := buildReplaceRe("过磅", searchOpts{}) if err != nil { t.Fatal(err) } text := "过磅记录\n原料过磅\n过磅过磅\n" got, n := replaceInText(text, re, "称重") if n != 4 { t.Fatalf("应替换 4 处,实际 %d", n) } if want := "称重记录\n原料称重\n称重称重\n"; got != want { t.Fatalf("结果不对: %q", got) } } func TestReplaceInTextCaseInsensitive(t *testing.T) { re, err := buildReplaceRe("Foo", searchOpts{}) if err != nil { t.Fatal(err) } got, n := replaceInText("foo FOO Foo", re, "Bar") if n != 3 { t.Fatalf("默认忽略大小写,应替换 3 处,实际 %d", n) } if got != "Bar Bar Bar" { t.Fatalf("结果不对: %q", got) } } func TestReplaceInTextCaseSensitive(t *testing.T) { re, err := buildReplaceRe("Foo", searchOpts{matchCase: true}) if err != nil { t.Fatal(err) } _, n := replaceInText("foo FOO Foo", re, "Bar") if n != 1 { t.Fatalf("区分大小写应只替换 1 处,实际 %d", n) } } func TestReplaceInTextRegexGroups(t *testing.T) { re, err := buildReplaceRe(`(\w+)@example\.com`, searchOpts{useRegex: true}) if err != nil { t.Fatal(err) } got, n := replaceInText("cgh@example.com; x@example.com", re, "$1@x.org") if n != 2 { t.Fatalf("应替换 2 处,实际 %d", n) } if want := "cgh@x.org; x@x.org"; got != want { t.Fatalf("分组引用不对: %q", got) } } func TestReplaceInTextWholeWord(t *testing.T) { re, err := buildReplaceRe("id", searchOpts{wholeWord: true}) if err != nil { t.Fatal(err) } got, n := replaceInText("id idx identify id", re, "ID") if n != 2 { t.Fatalf("整词应只替换 2 处,实际 %d", n) } if got != "ID idx identify ID" { t.Fatalf("结果不对: %q", got) } } func TestReplaceInTextEmptyReplacement(t *testing.T) { re, err := buildReplaceRe("debug_", searchOpts{}) if err != nil { t.Fatal(err) } got, n := replaceInText("a debug_x debug_y", re, "") if n != 2 || got != "a x y" { t.Fatalf("空替换即删除: n=%d got=%q", n, got) } } func TestReplaceInTextCRLFPreserved(t *testing.T) { re, err := buildReplaceRe("old", searchOpts{}) if err != nil { t.Fatal(err) } got, n := replaceInText("a old\r\nold\r\n", re, "new") if n != 2 { t.Fatalf("应替换 2 处,实际 %d", n) } if got != "a new\r\nnew\r\n" { t.Fatalf("CRLF 应原样保留: %q", got) } } func TestReplaceInTextNoMatch(t *testing.T) { re, err := buildReplaceRe("不存在", searchOpts{}) if err != nil { t.Fatal(err) } text := "原文不动\n" got, n := replaceInText(text, re, "x") if n != 0 || got != text { t.Fatalf("无匹配应原样返回: n=%d got=%q", n, got) } } func TestBuildReplaceReInvalid(t *testing.T) { if _, err := buildReplaceRe("a(", searchOpts{useRegex: true}); err == nil { t.Fatal("非法正则应报错") } } func TestReplaceInTextMultipleLines(t *testing.T) { re, err := buildReplaceRe("v1", searchOpts{}) if err != nil { t.Fatal(err) } text := strings.Repeat("x := v1\n", 3) got, n := replaceInText(text, re, "v2") if n != 3 || got != strings.Repeat("x := v2\n", 3) { t.Fatalf("跨行累计不对: n=%d got=%q", n, got) } } // TestApplyReplacePlan 执行层三条规则:磁盘 UTF-8 直接写回、GBK 按原编码写回、 // 打开中的文件只改缓冲区(变脏)且不动盘。 func TestApplyReplacePlan(t *testing.T) { root := t.TempDir() write := func(name string, data []byte) string { t.Helper() p := filepath.Join(root, name) if err := os.WriteFile(p, data, 0o644); err != nil { t.Fatal(err) } return p } // 1) 磁盘 UTF-8 文件。 u := write("utf8.txt", []byte("host = old\nport = 1\n")) // 2) GBK 文件(「过磅」的 GBK 编码)。 gbk, err := encodeFile("过磅 = old\n", encGBK) if err != nil { t.Fatal(err) } g := write("gbk.cfg", gbk) // 3) 打开中的文件:缓冲区有未保存内容,磁盘上没有匹配。 bufPath := write("open.txt", []byte("nothing here\n")) d := &Doc{path: bufPath, buf: editor.NewBuffer("open old line\n")} a := &App{root: root, docs: []*Doc{d}} plan, err := a.buildReplacePlan(map[string]string{bufPath: d.buf.String()}, "old", "new", searchOpts{}) if err != nil { t.Fatal(err) } if plan.Total != 3 || len(plan.Files) != 3 { t.Fatalf("预览应为 3 处 / 3 个文件,实际 %d 处 / %d 个: %+v", plan.Total, len(plan.Files), plan.Files) } hits, nfiles, failed := a.applyReplacePlan("old", "new", searchOpts{}, plan.Files) if len(failed) > 0 { t.Fatalf("不应有失败: %v", failed) } if hits != 3 || nfiles != 3 { t.Fatalf("应替换 3 处 / 3 个文件,实际 %d / %d", hits, nfiles) } // UTF-8 文件已写盘。 if b, err := os.ReadFile(u); err != nil || string(b) != "host = new\nport = 1\n" { t.Fatalf("UTF-8 文件没写对: %q %v", b, err) } // GBK 文件写回后仍是 GBK,且内容正确。 b, err := os.ReadFile(g) if err != nil { t.Fatal(err) } text, enc, ok := decodeFile(b) if !ok || enc != encGBK || text != "过磅 = new\n" { t.Fatalf("GBK 文件应保持 GBK 且内容正确: enc=%v ok=%v text=%q", enc, ok, text) } // 打开中的文件:缓冲区已替换并变脏,磁盘未动。 if got := d.buf.String(); got != "open new line\n" { t.Fatalf("缓冲区应已替换: %q", got) } if !d.buf.Modified() { t.Fatal("缓冲区应保持未保存(脏)状态") } if disk, _ := os.ReadFile(bufPath); string(disk) != "nothing here\n" { t.Fatalf("打开中的文件不应直接写盘: %q", disk) } }