GoPLS Viewer

Home|gopls/internal/diff/diff_test.go
1// Copyright 2019 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
5package diff_test
6
7import (
8    "math/rand"
9    "reflect"
10    "testing"
11    "unicode/utf8"
12
13    "golang.org/x/tools/internal/diff"
14    "golang.org/x/tools/internal/diff/difftest"
15)
16
17func TestApply(t *testing.T) {
18    for _tc := range difftest.TestCases {
19        t.Run(tc.Name, func(t *testing.T) {
20            goterr := diff.Apply(tc.Intc.Edits)
21            if err != nil {
22                t.Fatalf("Apply(Edits) failed: %v"err)
23            }
24            if got != tc.Out {
25                t.Errorf("Apply(Edits): got %q, want %q"gottc.Out)
26            }
27            if tc.LineEdits != nil {
28                goterr := diff.Apply(tc.Intc.LineEdits)
29                if err != nil {
30                    t.Fatalf("Apply(LineEdits) failed: %v"err)
31                }
32                if got != tc.Out {
33                    t.Errorf("Apply(LineEdits): got %q, want %q"gottc.Out)
34                }
35            }
36        })
37    }
38}
39
40func TestNEdits(t *testing.T) {
41    for _tc := range difftest.TestCases {
42        edits := diff.Strings(tc.Intc.Out)
43        goterr := diff.Apply(tc.Inedits)
44        if err != nil {
45            t.Fatalf("Apply failed: %v"err)
46        }
47        if got != tc.Out {
48            t.Fatalf("%s: got %q wanted %q"tc.Namegottc.Out)
49        }
50        if len(edits) < len(tc.Edits) { // should find subline edits
51            t.Errorf("got %v, expected %v for %#v"editstc.Editstc)
52        }
53    }
54}
55
56func TestNRandom(t *testing.T) {
57    rand.Seed(1)
58    for i := 0i < 1000i++ {
59        a := randstr("abω"16)
60        b := randstr("abωc"16)
61        edits := diff.Strings(ab)
62        goterr := diff.Apply(aedits)
63        if err != nil {
64            t.Fatalf("Apply failed: %v"err)
65        }
66        if got != b {
67            t.Fatalf("%d: got %q, wanted %q, starting with %q"igotba)
68        }
69    }
70}
71
72// $ go test -fuzz=FuzzRoundTrip ./internal/diff
73func FuzzRoundTrip(f *testing.F) {
74    f.Fuzz(func(t *testing.Tab string) {
75        if !utf8.ValidString(a) || !utf8.ValidString(b) {
76            return // inputs must be text
77        }
78        edits := diff.Strings(ab)
79        goterr := diff.Apply(aedits)
80        if err != nil {
81            t.Fatalf("Apply failed: %v"err)
82        }
83        if got != b {
84            t.Fatalf("applying diff(%q, %q) gives %q; edits=%v"abgotedits)
85        }
86    })
87}
88
89func TestLineEdits(t *testing.T) {
90    for _tc := range difftest.TestCases {
91        t.Run(tc.Name, func(t *testing.T) {
92            // if line edits not specified, it is the same as edits
93            edits := tc.LineEdits
94            if edits == nil {
95                edits = tc.Edits
96            }
97            goterr := diff.LineEdits(tc.Intc.Edits)
98            if err != nil {
99                t.Fatalf("LineEdits: %v"err)
100            }
101            if !reflect.DeepEqual(gotedits) {
102                t.Errorf("LineEdits got %q, want %q"gotedits)
103            }
104        })
105    }
106}
107
108func TestToUnified(t *testing.T) {
109    for _tc := range difftest.TestCases {
110        t.Run(tc.Name, func(t *testing.T) {
111            unifiederr := diff.ToUnified(difftest.FileAdifftest.FileBtc.Intc.Edits)
112            if err != nil {
113                t.Fatal(err)
114            }
115            if unified != tc.Unified {
116                t.Errorf("Unified(Edits): got diff:\n%v\nexpected:\n%v"unifiedtc.Unified)
117            }
118            if tc.LineEdits != nil {
119                unifiederr := diff.ToUnified(difftest.FileAdifftest.FileBtc.Intc.LineEdits)
120                if err != nil {
121                    t.Fatal(err)
122                }
123                if unified != tc.Unified {
124                    t.Errorf("Unified(LineEdits): got diff:\n%v\nexpected:\n%v"unifiedtc.Unified)
125                }
126            }
127        })
128    }
129}
130
131func TestRegressionOld001(t *testing.T) {
132    a := "// Copyright 2019 The Go Authors. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license that can be found in the LICENSE file.\n\npackage diff_test\n\nimport (\n\t\"fmt\"\n\t\"math/rand\"\n\t\"strings\"\n\t\"testing\"\n\n\t\"golang.org/x/tools/gopls/internal/lsp/diff\"\n\t\"golang.org/x/tools/internal/diff/difftest\"\n\t\"golang.org/x/tools/gopls/internal/span\"\n)\n"
133
134    b := "// Copyright 2019 The Go Authors. All rights reserved.\n// Use of this source code is governed by a BSD-style\n// license that can be found in the LICENSE file.\n\npackage diff_test\n\nimport (\n\t\"fmt\"\n\t\"math/rand\"\n\t\"strings\"\n\t\"testing\"\n\n\t\"github.com/google/safehtml/template\"\n\t\"golang.org/x/tools/gopls/internal/lsp/diff\"\n\t\"golang.org/x/tools/internal/diff/difftest\"\n\t\"golang.org/x/tools/gopls/internal/span\"\n)\n"
135    diffs := diff.Strings(ab)
136    goterr := diff.Apply(adiffs)
137    if err != nil {
138        t.Fatalf("Apply failed: %v"err)
139    }
140    if got != b {
141        i := 0
142        for ; i < len(a) && i < len(b) && got[i] == b[i]; i++ {
143        }
144        t.Errorf("oops %vd\n%q\n%q"diffsgotb)
145        t.Errorf("\n%q\n%q"got[i:], b[i:])
146    }
147}
148
149func TestRegressionOld002(t *testing.T) {
150    a := "n\"\n)\n"
151    b := "n\"\n\t\"golang.org/x//nnal/stack\"\n)\n"
152    diffs := diff.Strings(ab)
153    goterr := diff.Apply(adiffs)
154    if err != nil {
155        t.Fatalf("Apply failed: %v"err)
156    }
157    if got != b {
158        i := 0
159        for ; i < len(a) && i < len(b) && got[i] == b[i]; i++ {
160        }
161        t.Errorf("oops %vd\n%q\n%q"diffsgotb)
162        t.Errorf("\n%q\n%q"got[i:], b[i:])
163    }
164}
165
166// return a random string of length n made of characters from s
167func randstr(s stringn intstring {
168    src := []rune(s)
169    x := make([]runen)
170    for i := 0i < ni++ {
171        x[i] = src[rand.Intn(len(src))]
172    }
173    return string(x)
174}
175
MembersX
TestRegressionOld001.b
TestRegressionOld002
TestRegressionOld002.err
TestApply.RangeStmt_357.BlockStmt.BlockStmt.err
randstr.src
TestNEdits.RangeStmt_927.BlockStmt.err
TestToUnified.RangeStmt_2610.BlockStmt.BlockStmt.err
TestRegressionOld001.BlockStmt.i
TestRegressionOld002.a
TestRegressionOld002.got
FuzzRoundTrip.f
TestRegressionOld001.err
TestNRandom.t
TestNRandom.BlockStmt.edits
TestNRandom.BlockStmt.err
FuzzRoundTrip.BlockStmt.edits
TestToUnified.RangeStmt_2610.BlockStmt.BlockStmt.BlockStmt.err
TestApply.RangeStmt_357.BlockStmt.BlockStmt.BlockStmt.got
TestNEdits
FuzzRoundTrip.BlockStmt.got
rand
difftest
TestNRandom.i
TestNRandom.BlockStmt.got
TestLineEdits.RangeStmt_2150.BlockStmt.BlockStmt.edits
TestRegressionOld001.a
TestRegressionOld001.got
TestRegressionOld002.t
TestApply.RangeStmt_357.tc
TestNEdits.RangeStmt_927.tc
TestToUnified.RangeStmt_2610.tc
TestToUnified.RangeStmt_2610.BlockStmt.BlockStmt.unified
TestToUnified.RangeStmt_2610.BlockStmt.BlockStmt.BlockStmt.unified
diff
TestApply.RangeStmt_357.BlockStmt.BlockStmt.BlockStmt.err
TestNRandom
TestNRandom.BlockStmt.b
TestRegressionOld002.b
testing
TestLineEdits.RangeStmt_2150.BlockStmt.BlockStmt.err
TestRegressionOld001.t
TestRegressionOld002.BlockStmt.i
TestApply.t
TestApply.RangeStmt_357.BlockStmt.BlockStmt.got
FuzzRoundTrip
reflect
TestNEdits.t
TestNEdits.RangeStmt_927.BlockStmt.edits
TestNEdits.RangeStmt_927.BlockStmt.got
TestNRandom.BlockStmt.a
randstr
randstr.s
TestApply
TestLineEdits.RangeStmt_2150.tc
TestLineEdits.RangeStmt_2150.BlockStmt.BlockStmt.got
TestToUnified
TestRegressionOld001
TestRegressionOld002.diffs
utf8
FuzzRoundTrip.BlockStmt.err
TestToUnified.t
TestLineEdits
TestRegressionOld001.diffs
randstr.n
randstr.i
TestLineEdits.t
randstr.x
Members
X