Debian Codegraph @

internal/diff/merge_test.go

raw symbols SCIP
1 // Copyright 2025 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package diff_test
6
7 import (
8 "testing"
9
10 "golang.org/x/tools/internal/diff"
11 )
12
13 func TestMerge(t *testing.T) {
14 // For convenience, we test Merge using strings, not sequences
15 // of edits, though this does put us at the mercy of the diff
16 // algorithm.
17 for _, test := range []struct {
18 base, x, y string
19 want string // "!" => conflict
20 }{
21 // independent insertions
22 {"abcdef", "abXcdef", "abcdeYf", "abXcdeYf"},
23 // independent deletions
24 {"abcdef", "acdef", "abcdf", "acdf"},
25 // colocated insertions (X first)
26 {"abcdef", "abcXdef", "abcYdef", "abcXYdef"},
27 // colocated identical insertions (coalesced)
28 {"abcdef", "abcXdef", "abcXdef", "abcXdef"},
29 // colocated insertions with common prefix (X first)
30 // TODO(adonovan): would "abcXYdef" be better?
31 // i.e. should we dissect the insertions?
32 {"abcdef", "abcXdef", "abcXYdef", "abcXXYdef"},
33 // mix of identical and independent insertions (X first)
34 {"abcdef", "aIbcdXef", "aIbcdYef", "aIbcdXYef"},
35 // independent deletions
36 {"abcdef", "def", "abc", ""},
37 // overlapping deletions: conflict
38 {"abcdef", "adef", "abef", "!"},
39 // overlapping deletions with distinct insertions, X first
40 {"abcdef", "abXef", "abcYf", "!"},
41 // overlapping deletions with distinct insertions, Y first
42 {"abcdef", "abcXf", "abYef", "!"},
43 // overlapping deletions with common insertions
44 {"abcdef", "abXef", "abcXf", "!"},
45 // trailing insertions in X (observe X bias)
46 {"abcdef", "aXbXcXdXeXfX", "aYbcdef", "aXYbXcXdXeXfX"},
47 // trailing insertions in Y (observe X bias)
48 {"abcdef", "aXbcdef", "aYbYcYdYeYfY", "aXYbYcYdYeYfY"},
49 } {
50 dx := diff.Strings(test.base, test.x)
51 dy := diff.Strings(test.base, test.y)
52 got := "!" // conflict
53 if dz, ok := diff.Merge(dx, dy); ok {
54 var err error
55 got, err = diff.Apply(test.base, dz)
56 if err != nil {
57 t.Errorf("Merge(%q, %q, %q) produced invalid edits %v: %v", test.base, test.x, test.y, dz, err)
58 continue
59 }
60 }
61 if test.want != got {
62 t.Errorf("base=%q x=%q y=%q: got %q, want %q", test.base, test.x, test.y, got, test.want)
63 }
64 }
65 }