@@ -9,6 +9,18 @@ import (
9
9
"testing"
10
10
)
11
11
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
+
12
24
func splitChars (s string ) []string {
13
25
chars := make ([]string , 0 , len (s ))
14
26
// Assume ASCII inputs
@@ -20,20 +32,9 @@ func splitChars(s string) []string {
20
32
21
33
func TestSequenceMatcherRatio (t * testing.T ) {
22
34
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 )
37
38
}
38
39
39
40
func TestGetOptCodes (t * testing.T ) {
@@ -147,18 +148,6 @@ four`
147
148
}
148
149
}
149
150
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
-
162
151
func rep (s string , count int ) string {
163
152
return strings .Repeat (s , count )
164
153
}
@@ -206,3 +195,40 @@ func TestWithAsciiBJunk(t *testing.T) {
206
195
splitChars (rep ("a" , 44 )+ rep ("b" , 40 )+ rep (" " , 20 )), false , isJunk )
207
196
assertEqual (t , sm .bJunk , map [string ]struct {}{" " : struct {}{}, "b" : struct {}{}})
208
197
}
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