| 1 | package gitrepo |
| 2 | |
| 3 | import ( |
| 4 | "strconv" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | ) |
| 8 | |
| 9 | func TestParseDiffAndSuggestion(t *testing.T) { |
| 10 | raw := "" + |
| 11 | "diff --git a/hello.txt b/hello.txt\n" + |
| 12 | "--- a/hello.txt\n" + |
| 13 | "+++ b/hello.txt\n" + |
| 14 | "@@ -1,3 +1,4 @@\n" + |
| 15 | " alpha\n" + |
| 16 | "-beta\n" + |
| 17 | "+BETA\n" + |
| 18 | "+extra\n" + |
| 19 | " gamma\n" |
| 20 | files := ParseDiff(raw) |
| 21 | if len(files) != 1 || files[0].Path != "hello.txt" { |
| 22 | t.Fatalf("%+v", files) |
| 23 | } |
| 24 | var news []int |
| 25 | for _, ln := range files[0].Lines { |
| 26 | if ln.New > 0 { |
| 27 | news = append(news, ln.New) |
| 28 | if ln.New == 2 && (ln.Kind != "add" || ln.Text != "BETA") { |
| 29 | t.Fatalf("regel 2 %+v", ln) |
| 30 | } |
| 31 | } |
| 32 | if ln.Kind == "del" && (ln.New != 0 || ln.Text != "beta") { |
| 33 | t.Fatalf("verwijderd %+v", ln) |
| 34 | } |
| 35 | } |
| 36 | gotNums := make([]string, len(news)) |
| 37 | for i, n := range news { |
| 38 | gotNums[i] = strconv.Itoa(n) |
| 39 | } |
| 40 | if strings.Join(gotNums, ",") != "1,2,3,4" { |
| 41 | t.Fatalf("nieuwe regels %v", news) |
| 42 | } |
| 43 | body := "zie\n\n```suggestion\nbeta\nextra\n```\n" |
| 44 | got, ok := ParseSuggestion(body) |
| 45 | if !ok || got != "beta\nextra" { |
| 46 | t.Fatalf("%q %v", got, ok) |
| 47 | } |
| 48 | if _, ok := ParseSuggestion("alleen tekst"); ok { |
| 49 | t.Fatal("geen blok") |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | func TestApplySuggestions(t *testing.T) { |
| 54 | files := map[string]string{"hello.txt": "alpha\nbeta\ngamma\n"} |
| 55 | out, err := ApplySuggestions(files, []SuggestionEdit{{ |
| 56 | Path: "hello.txt", Start: 2, End: 2, Base: "beta", Replacement: "BETA", |
| 57 | }}) |
| 58 | if err != nil || out["hello.txt"] != "alpha\nBETA\ngamma\n" { |
| 59 | t.Fatalf("%q %v", out["hello.txt"], err) |
| 60 | } |
| 61 | if _, err := ApplySuggestions(files, []SuggestionEdit{{ |
| 62 | Path: "hello.txt", Start: 2, End: 2, Base: "oud", Replacement: "BETA", |
| 63 | }}); err == nil || !strings.Contains(err.Error(), "hello.txt:2-2") { |
| 64 | t.Fatal(err) |
| 65 | } |
| 66 | if _, err := ApplySuggestions(files, []SuggestionEdit{ |
| 67 | {Path: "hello.txt", Start: 1, End: 2, Base: "alpha\nbeta", Replacement: "a"}, |
| 68 | {Path: "hello.txt", Start: 2, End: 3, Base: "beta\ngamma", Replacement: "b"}, |
| 69 | }); err == nil || !strings.Contains(err.Error(), "overlap") { |
| 70 | t.Fatal(err) |
| 71 | } |
| 72 | two, err := ApplySuggestions(files, []SuggestionEdit{ |
| 73 | {Path: "hello.txt", Start: 1, End: 1, Base: "alpha", Replacement: "ALPHA"}, |
| 74 | {Path: "hello.txt", Start: 3, End: 3, Base: "gamma", Replacement: "GAMMA"}, |
| 75 | }) |
| 76 | if err != nil || two["hello.txt"] != "ALPHA\nbeta\nGAMMA\n" { |
| 77 | t.Fatalf("%q %v", two["hello.txt"], err) |
| 78 | } |
| 79 | if _, err := ApplySuggestions(files, []SuggestionEdit{{ |
| 80 | Path: "hello.txt", Start: 2, End: 2, Base: "beta", Replacement: "beta", |
| 81 | }}); err == nil { |
| 82 | t.Fatal("lege wijziging") |
| 83 | } |
| 84 | } |