| 1 | package markup |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | ) |
| 6 | |
| 7 | const mergeBase = "# Plan\n\nOne.\n\nTwo.\n\nThree." |
| 8 | |
| 9 | func settled(t *testing.T, mine, theirs string) string { |
| 10 | t.Helper() |
| 11 | hunks, clean := Merge3(mergeBase, mine, theirs) |
| 12 | if !clean { |
| 13 | t.Fatalf("a conflict merging\n%q\n%q", mine, theirs) |
| 14 | } |
| 15 | body, ok := Settle(hunks, nil) |
| 16 | if !ok { |
| 17 | t.Fatal("a clean merge did not settle") |
| 18 | } |
| 19 | return body |
| 20 | } |
| 21 | |
| 22 | func TestEditsToDifferentBlocksMergeSilently(t *testing.T) { |
| 23 | got := settled(t, "# Plan\n\nOne, mine.\n\nTwo.\n\nThree.", |
| 24 | "# Plan\n\nOne.\n\nTwo.\n\nThree, theirs.") |
| 25 | if got != "# Plan\n\nOne, mine.\n\nTwo.\n\nThree, theirs." { |
| 26 | t.Fatalf("got %q", got) |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | func TestInsertionsAndDeletionsMerge(t *testing.T) { |
| 31 | got := settled(t, "# Plan\n\nZero.\n\nOne.\n\nTwo.\n\nThree.", |
| 32 | "# Plan\n\nOne.\n\nThree.\n\nFour.") |
| 33 | if got != "# Plan\n\nZero.\n\nOne.\n\nThree.\n\nFour." { |
| 34 | t.Fatalf("got %q", got) |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | func TestTheSameChangeTwiceIsOne(t *testing.T) { |
| 39 | both := "# Plan\n\nOne!\n\nTwo.\n\nThree." |
| 40 | if got := settled(t, both, both); got != both { |
| 41 | t.Fatalf("got %q", got) |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | func TestTwoEditsToOneBlockAreAConflictTheyChoose(t *testing.T) { |
| 46 | mine := "# Plan\n\nOne.\n\nTwo, mine.\n\nThree." |
| 47 | theirs := "# Plan\n\nOne.\n\nTwo, theirs.\n\nThree, theirs." |
| 48 | hunks, clean := Merge3(mergeBase, mine, theirs) |
| 49 | if clean { |
| 50 | t.Fatal("two edits to one block merged silently") |
| 51 | } |
| 52 | conflicts := 0 |
| 53 | for _, hunk := range hunks { |
| 54 | if hunk.Conflict { |
| 55 | conflicts++ |
| 56 | } |
| 57 | } |
| 58 | if conflicts != 1 { |
| 59 | t.Fatalf("%d conflicts, want 1", conflicts) |
| 60 | } |
| 61 | for choice, want := range map[Choice]string{ |
| 62 | KeepMine: "# Plan\n\nOne.\n\nTwo, mine.\n\nThree, theirs.", |
| 63 | KeepTheirs: "# Plan\n\nOne.\n\nTwo, theirs.\n\nThree, theirs.", |
| 64 | KeepBoth: "# Plan\n\nOne.\n\nTwo, mine.\n\nTwo, theirs.\n\n" + |
| 65 | "Three, theirs.", |
| 66 | } { |
| 67 | got, ok := Settle(hunks, []Choice{choice}) |
| 68 | if !ok || got != want { |
| 69 | t.Errorf("choice %d: %q", choice, got) |
| 70 | } |
| 71 | } |
| 72 | if _, ok := Settle(hunks, nil); ok { |
| 73 | t.Fatal("an unchosen conflict settled") |
| 74 | } |
| 75 | } |
| 76 | |