Skip to content

Commit 35c8dc4

Browse files
committed
difflib_test: add more tests
1 parent 3859285 commit 35c8dc4

File tree

1 file changed

+52
-26
lines changed

1 file changed

+52
-26
lines changed

difflib/difflib_test.go

Lines changed: 52 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,18 @@ import (
99
"testing"
1010
)
1111

12+
func assertAlmostEqual(t *testing.T, a, b float64, places int) {
13+
if math.Abs(a-b) > math.Pow10(-places) {
14+
t.Errorf("%.7f != %.7f", a, b)
15+
}
16+
}
17+
18+
func assertEqual(t *testing.T, a, b interface{}) {
19+
if !reflect.DeepEqual(a, b) {
20+
t.Errorf("%v != %v", a, b)
21+
}
22+
}
23+
1224
func splitChars(s string) []string {
1325
chars := make([]string, 0, len(s))
1426
// Assume ASCII inputs
@@ -20,20 +32,9 @@ func splitChars(s string) []string {
2032

2133
func TestSequenceMatcherRatio(t *testing.T) {
2234
s := NewMatcher(splitChars("abcd"), splitChars("bcde"))
23-
ratio := s.Ratio()
24-
if ratio != 0.75 {
25-
t.Errorf("unexpected ratio: %v", ratio)
26-
}
27-
28-
ratio = s.QuickRatio()
29-
if ratio != 0.75 {
30-
t.Errorf("unexpected quick ratio: %v", ratio)
31-
}
32-
33-
ratio = s.RealQuickRatio()
34-
if ratio != 1.0 {
35-
t.Errorf("unexpected real quick ratio: %v", ratio)
36-
}
35+
assertEqual(t, s.Ratio(), 0.75)
36+
assertEqual(t, s.QuickRatio(), 0.75)
37+
assertEqual(t, s.RealQuickRatio(), 1.0)
3738
}
3839

3940
func TestGetOptCodes(t *testing.T) {
@@ -147,18 +148,6 @@ four`
147148
}
148149
}
149150

150-
func assertAlmostEqual(t *testing.T, a, b float64, places int) {
151-
if math.Abs(a-b) > math.Pow10(-places) {
152-
t.Errorf("%.7f != %.7f", a, b)
153-
}
154-
}
155-
156-
func assertEqual(t *testing.T, a, b interface{}) {
157-
if !reflect.DeepEqual(a, b) {
158-
t.Errorf("%v != %v", a, b)
159-
}
160-
}
161-
162151
func rep(s string, count int) string {
163152
return strings.Repeat(s, count)
164153
}
@@ -206,3 +195,40 @@ func TestWithAsciiBJunk(t *testing.T) {
206195
splitChars(rep("a", 44)+rep("b", 40)+rep(" ", 20)), false, isJunk)
207196
assertEqual(t, sm.bJunk, map[string]struct{}{" ": struct{}{}, "b": struct{}{}})
208197
}
198+
199+
func TestSFBugsRatioForNullSeqn(t *testing.T) {
200+
sm := NewMatcher(nil, nil)
201+
assertEqual(t, sm.Ratio(), 1.0)
202+
assertEqual(t, sm.QuickRatio(), 1.0)
203+
assertEqual(t, sm.RealQuickRatio(), 1.0)
204+
}
205+
206+
func TestSFBugsComparingEmptyLists(t *testing.T) {
207+
groups := NewMatcher(nil, nil).GetGroupedOpCodes(-1)
208+
assertEqual(t, len(groups), 0)
209+
diff := UnifiedDiff{
210+
FromFile: "Original",
211+
ToFile: "Current",
212+
Context: 3,
213+
}
214+
result, err := GetUnifiedDiffString(diff)
215+
assertEqual(t, err, nil)
216+
assertEqual(t, result, "")
217+
}
218+
219+
func TestOutputFormatRangeFormatUnified(t *testing.T) {
220+
// Per the diff spec at http://www.unix.org/single_unix_specification/
221+
//
222+
// Each <range> field shall be of the form:
223+
// %1d", <beginning line number> if the range contains exactly one line,
224+
// and:
225+
// "%1d,%1d", <beginning line number>, <number of lines> otherwise.
226+
// If a range is empty, its beginning line number shall be the number of
227+
// the line just before the range, or 0 if the empty range starts the file.
228+
fm := formatRangeUnified
229+
assertEqual(t, fm(3, 3), "3,0")
230+
assertEqual(t, fm(3, 4), "4")
231+
assertEqual(t, fm(3, 5), "4,2")
232+
assertEqual(t, fm(3, 6), "4,3")
233+
assertEqual(t, fm(0, 0), "0,0")
234+
}

0 commit comments

Comments
 (0)