| 1 |
package difflib |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"fmt" |
| 6 |
"math" |
| 7 |
"reflect" |
| 8 |
"runtime" |
| 9 |
"strings" |
| 10 |
"testing" |
| 11 |
"./tester" |
| 12 |
) |
| 13 |
|
| 14 |
func assertAlmostEqual(t *testing.T, a, b float64, places int) { |
| 15 |
if math.Abs(a-b) > math.Pow10(-places) { |
| 16 |
t.Errorf("%.7f != %.7f", a, b) |
| 17 |
} |
| 18 |
} |
| 19 |
|
| 20 |
func assertEqual(t *testing.T, a, b interface{}) { |
| 21 |
if !reflect.DeepEqual(a, b) { |
| 22 |
t.Errorf("%v != %v", a, b) |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
func splitChars(s string) []string { |
| 27 |
chars := make([]string, 0, len(s)) |
| 28 |
// Assume ASCII inputs |
| 29 |
for i := 0; i != len(s); i++ { |
| 30 |
chars = append(chars, string(s[i])) |
| 31 |
} |
| 32 |
return chars |
| 33 |
} |
| 34 |
|
| 35 |
func TestlistifyString(t *testing.T) { |
| 36 |
lst := listifyString("qwerty") |
| 37 |
if reflect.DeepEqual(lst, []string{"q", "w", "e", "r", "t", "y"}) != true { |
| 38 |
t.Fatal("listifyString failure:", lst) |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
func TestSequenceMatcherRatio(t *testing.T) { |
| 43 |
s := NewMatcher(splitChars("abcd"), splitChars("bcde")) |
| 44 |
assertEqual(t, s.Ratio(), 0.75) |
| 45 |
assertEqual(t, s.QuickRatio(), 0.75) |
| 46 |
assertEqual(t, s.RealQuickRatio(), 1.0) |
| 47 |
} |
| 48 |
|
| 49 |
func TestGetOptCodes(t *testing.T) { |
| 50 |
a := "qabxcd" |
| 51 |
b := "abycdf" |
| 52 |
s := NewMatcher(splitChars(a), splitChars(b)) |
| 53 |
w := &bytes.Buffer{} |
| 54 |
for _, op := range s.GetOpCodes() { |
| 55 |
fmt.Fprintf(w, "%s a[%d:%d], (%s) b[%d:%d] (%s)\n", string(op.Tag), |
| 56 |
op.I1, op.I2, a[op.I1:op.I2], op.J1, op.J2, b[op.J1:op.J2]) |
| 57 |
} |
| 58 |
result := string(w.Bytes()) |
| 59 |
expected := `d a[0:1], (q) b[0:0] () |
| 60 |
e a[1:3], (ab) b[0:2] (ab) |
| 61 |
r a[3:4], (x) b[2:3] (y) |
| 62 |
e a[4:6], (cd) b[3:5] (cd) |
| 63 |
i a[6:6], () b[5:6] (f) |
| 64 |
` |
| 65 |
if expected != result { |
| 66 |
t.Errorf("unexpected op codes: \n%s", result) |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
func TestGroupedOpCodes(t *testing.T) { |
| 71 |
a := []string{} |
| 72 |
for i := 0; i != 39; i++ { |
| 73 |
a = append(a, fmt.Sprintf("%02d", i)) |
| 74 |
} |
| 75 |
b := []string{} |
| 76 |
b = append(b, a[:8]...) |
| 77 |
b = append(b, " i") |
| 78 |
b = append(b, a[8:19]...) |
| 79 |
b = append(b, " x") |
| 80 |
b = append(b, a[20:22]...) |
| 81 |
b = append(b, a[27:34]...) |
| 82 |
b = append(b, " y") |
| 83 |
b = append(b, a[35:]...) |
| 84 |
s := NewMatcher(a, b) |
| 85 |
w := &bytes.Buffer{} |
| 86 |
for _, g := range s.GetGroupedOpCodes(-1) { |
| 87 |
fmt.Fprintf(w, "group\n") |
| 88 |
for _, op := range g { |
| 89 |
fmt.Fprintf(w, " %s, %d, %d, %d, %d\n", string(op.Tag), |
| 90 |
op.I1, op.I2, op.J1, op.J2) |
| 91 |
} |
| 92 |
} |
| 93 |
result := string(w.Bytes()) |
| 94 |
expected := `group |
| 95 |
e, 5, 8, 5, 8 |
| 96 |
i, 8, 8, 8, 9 |
| 97 |
e, 8, 11, 9, 12 |
| 98 |
group |
| 99 |
e, 16, 19, 17, 20 |
| 100 |
r, 19, 20, 20, 21 |
| 101 |
e, 20, 22, 21, 23 |
| 102 |
d, 22, 27, 23, 23 |
| 103 |
e, 27, 30, 23, 26 |
| 104 |
group |
| 105 |
e, 31, 34, 27, 30 |
| 106 |
r, 34, 35, 30, 31 |
| 107 |
e, 35, 38, 31, 34 |
| 108 |
` |
| 109 |
if expected != result { |
| 110 |
t.Errorf("unexpected op codes: \n%s", result) |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
func ExampleGetUnifiedDiffCode() { |
| 115 |
a := `one |
| 116 |
two |
| 117 |
three |
| 118 |
four |
| 119 |
fmt.Printf("%s,%T",a,b)` |
| 120 |
b := `zero |
| 121 |
one |
| 122 |
three |
| 123 |
four` |
| 124 |
diff := LineDiffParams{ |
| 125 |
A: SplitLines(a), |
| 126 |
B: SplitLines(b), |
| 127 |
FromFile: "Original", |
| 128 |
FromDate: "2005-01-26 23:30:50", |
| 129 |
ToFile: "Current", |
| 130 |
ToDate: "2010-04-02 10:20:52", |
| 131 |
Context: 3, |
| 132 |
} |
| 133 |
result, _ := GetUnifiedDiffString(diff) |
| 134 |
fmt.Println(strings.Replace(result, "\t", " ", -1)) |
| 135 |
// Output: |
| 136 |
// --- Original 2005-01-26 23:30:50 |
| 137 |
// +++ Current 2010-04-02 10:20:52 |
| 138 |
// @@ -1,5 +1,4 @@ |
| 139 |
// +zero |
| 140 |
// one |
| 141 |
// -two |
| 142 |
// three |
| 143 |
// four |
| 144 |
// -fmt.Printf("%s,%T",a,b) |
| 145 |
} |
| 146 |
|
| 147 |
func ExampleGetContextDiffCode() { |
| 148 |
a := `one |
| 149 |
two |
| 150 |
three |
| 151 |
four |
| 152 |
fmt.Printf("%s,%T",a,b)` |
| 153 |
b := `zero |
| 154 |
one |
| 155 |
tree |
| 156 |
four` |
| 157 |
diff := ContextDiff{ |
| 158 |
A: SplitLines(a), |
| 159 |
B: SplitLines(b), |
| 160 |
FromFile: "Original", |
| 161 |
ToFile: "Current", |
| 162 |
Context: 3, |
| 163 |
Eol: "\n", |
| 164 |
} |
| 165 |
result, _ := GetContextDiffString(diff) |
| 166 |
fmt.Print(strings.Replace(result, "\t", " ", -1)) |
| 167 |
// Output: |
| 168 |
// *** Original |
| 169 |
// --- Current |
| 170 |
// *************** |
| 171 |
// *** 1,5 **** |
| 172 |
// one |
| 173 |
// ! two |
| 174 |
// ! three |
| 175 |
// four |
| 176 |
// - fmt.Printf("%s,%T",a,b) |
| 177 |
// --- 1,4 ---- |
| 178 |
// + zero |
| 179 |
// one |
| 180 |
// ! tree |
| 181 |
// four |
| 182 |
} |
| 183 |
|
| 184 |
func ExampleGetContextDiffString() { |
| 185 |
a := `one |
| 186 |
two |
| 187 |
three |
| 188 |
four` |
| 189 |
b := `zero |
| 190 |
one |
| 191 |
tree |
| 192 |
four` |
| 193 |
diff := ContextDiff{ |
| 194 |
A: SplitLines(a), |
| 195 |
B: SplitLines(b), |
| 196 |
FromFile: "Original", |
| 197 |
ToFile: "Current", |
| 198 |
Context: 3, |
| 199 |
Eol: "\n", |
| 200 |
} |
| 201 |
result, _ := GetContextDiffString(diff) |
| 202 |
fmt.Printf(strings.Replace(result, "\t", " ", -1)) |
| 203 |
// Output: |
| 204 |
// *** Original |
| 205 |
// --- Current |
| 206 |
// *************** |
| 207 |
// *** 1,4 **** |
| 208 |
// one |
| 209 |
// ! two |
| 210 |
// ! three |
| 211 |
// four |
| 212 |
// --- 1,4 ---- |
| 213 |
// + zero |
| 214 |
// one |
| 215 |
// ! tree |
| 216 |
// four |
| 217 |
} |
| 218 |
|
| 219 |
func rep(s string, count int) string { |
| 220 |
return strings.Repeat(s, count) |
| 221 |
} |
| 222 |
|
| 223 |
func TestWithAsciiOneInsert(t *testing.T) { |
| 224 |
sm := NewMatcher(splitChars(rep("b", 100)), |
| 225 |
splitChars("a"+rep("b", 100))) |
| 226 |
assertAlmostEqual(t, sm.Ratio(), 0.995, 3) |
| 227 |
assertEqual(t, sm.GetOpCodes(), |
| 228 |
[]OpCode{{'i', 0, 0, 0, 1}, {'e', 0, 100, 1, 101}}) |
| 229 |
assertEqual(t, len(sm.bPopular), 0) |
| 230 |
|
| 231 |
sm = NewMatcher(splitChars(rep("b", 100)), |
| 232 |
splitChars(rep("b", 50)+"a"+rep("b", 50))) |
| 233 |
assertAlmostEqual(t, sm.Ratio(), 0.995, 3) |
| 234 |
assertEqual(t, sm.GetOpCodes(), |
| 235 |
[]OpCode{{'e', 0, 50, 0, 50}, {'i', 50, 50, 50, 51}, {'e', 50, 100, 51, 101}}) |
| 236 |
assertEqual(t, len(sm.bPopular), 0) |
| 237 |
} |
| 238 |
|
| 239 |
func TestWithAsciiOnDelete(t *testing.T) { |
| 240 |
sm := NewMatcher(splitChars(rep("a", 40)+"c"+rep("b", 40)), |
| 241 |
splitChars(rep("a", 40)+rep("b", 40))) |
| 242 |
assertAlmostEqual(t, sm.Ratio(), 0.994, 3) |
| 243 |
assertEqual(t, sm.GetOpCodes(), |
| 244 |
[]OpCode{{'e', 0, 40, 0, 40}, {'d', 40, 41, 40, 40}, {'e', 41, 81, 40, 80}}) |
| 245 |
} |
| 246 |
|
| 247 |
func TestWithAsciiBJunk(t *testing.T) { |
| 248 |
isJunk := func(s string) bool { |
| 249 |
return s == " " |
| 250 |
} |
| 251 |
sm := NewMatcherWithJunk(splitChars(rep("a", 40)+rep("b", 40)), |
| 252 |
splitChars(rep("a", 44)+rep("b", 40)), true, isJunk) |
| 253 |
assertEqual(t, sm.bJunk, map[string]bool{}) |
| 254 |
|
| 255 |
sm = NewMatcherWithJunk(splitChars(rep("a", 40)+rep("b", 40)), |
| 256 |
splitChars(rep("a", 44)+rep("b", 40)+rep(" ", 20)), false, isJunk) |
| 257 |
assertEqual(t, sm.bJunk, map[string]bool{" ": true}) |
| 258 |
|
| 259 |
isJunk = func(s string) bool { |
| 260 |
return s == " " || s == "b" |
| 261 |
} |
| 262 |
sm = NewMatcherWithJunk(splitChars(rep("a", 40)+rep("b", 40)), |
| 263 |
splitChars(rep("a", 44)+rep("b", 40)+rep(" ", 20)), false, isJunk) |
| 264 |
assertEqual(t, sm.bJunk, map[string]bool{" ": true, "b": true}) |
| 265 |
} |
| 266 |
|
| 267 |
func TestSFBugsRatioForNullSeqn(t *testing.T) { |
| 268 |
sm := NewMatcher(nil, nil) |
| 269 |
assertEqual(t, sm.Ratio(), 1.0) |
| 270 |
assertEqual(t, sm.QuickRatio(), 1.0) |
| 271 |
assertEqual(t, sm.RealQuickRatio(), 1.0) |
| 272 |
} |
| 273 |
|
| 274 |
func TestSFBugsComparingEmptyLists(t *testing.T) { |
| 275 |
groups := NewMatcher(nil, nil).GetGroupedOpCodes(-1) |
| 276 |
assertEqual(t, len(groups), 0) |
| 277 |
diff := LineDiffParams{ |
| 278 |
FromFile: "Original", |
| 279 |
ToFile: "Current", |
| 280 |
Context: 3, |
| 281 |
} |
| 282 |
result, err := GetUnifiedDiffString(diff) |
| 283 |
assertEqual(t, err, nil) |
| 284 |
assertEqual(t, result, "") |
| 285 |
} |
| 286 |
|
| 287 |
func TestOutputFormatRangeFormatUnified(t *testing.T) { |
| 288 |
// Per the diff spec at http://www.unix.org/single_unix_specification/ |
| 289 |
// |
| 290 |
// Each <range> field shall be of the form: |
| 291 |
// %1d", <beginning line number> if the range contains exactly one line, |
| 292 |
// and: |
| 293 |
// "%1d,%1d", <beginning line number>, <number of lines> otherwise. |
| 294 |
// If a range is empty, its beginning line number shall be the number of |
| 295 |
// the line just before the range, or 0 if the empty range starts the file. |
| 296 |
fm := formatRangeUnified |
| 297 |
assertEqual(t, fm(3, 3), "3,0") |
| 298 |
assertEqual(t, fm(3, 4), "4") |
| 299 |
assertEqual(t, fm(3, 5), "4,2") |
| 300 |
assertEqual(t, fm(3, 6), "4,3") |
| 301 |
assertEqual(t, fm(0, 0), "0,0") |
| 302 |
} |
| 303 |
|
| 304 |
func TestOutputFormatRangeFormatContext(t *testing.T) { |
| 305 |
// Per the diff spec at http://www.unix.org/single_unix_specification/ |
| 306 |
// |
| 307 |
// The range of lines in file1 shall be written in the following format |
| 308 |
// if the range contains two or more lines: |
| 309 |
// "*** %d,%d ****\n", <beginning line number>, <ending line number> |
| 310 |
// and the following format otherwise: |
| 311 |
// "*** %d ****\n", <ending line number> |
| 312 |
// The ending line number of an empty range shall be the number of the preceding line, |
| 313 |
// or 0 if the range is at the start of the file. |
| 314 |
// |
| 315 |
// Next, the range of lines in file2 shall be written in the following format |
| 316 |
// if the range contains two or more lines: |
| 317 |
// "--- %d,%d ----\n", <beginning line number>, <ending line number> |
| 318 |
// and the following format otherwise: |
| 319 |
// "--- %d ----\n", <ending line number> |
| 320 |
fm := formatRangeContext |
| 321 |
assertEqual(t, fm(3, 3), "3") |
| 322 |
assertEqual(t, fm(3, 4), "4") |
| 323 |
assertEqual(t, fm(3, 5), "4,5") |
| 324 |
assertEqual(t, fm(3, 6), "4,6") |
| 325 |
assertEqual(t, fm(0, 0), "0") |
| 326 |
} |
| 327 |
|
| 328 |
func TestOutputFormatTabDelimiter(t *testing.T) { |
| 329 |
diff := LineDiffParams{ |
| 330 |
A: splitChars("one"), |
| 331 |
B: splitChars("two"), |
| 332 |
FromFile: "Original", |
| 333 |
FromDate: "2005-01-26 23:30:50", |
| 334 |
ToFile: "Current", |
| 335 |
ToDate: "2010-04-12 10:20:52", |
| 336 |
Eol: "\n", |
| 337 |
} |
| 338 |
ud, err := GetUnifiedDiffString(diff) |
| 339 |
assertEqual(t, err, nil) |
| 340 |
assertEqual(t, SplitLines(ud)[:2], []string{ |
| 341 |
"--- Original\t2005-01-26 23:30:50\n", |
| 342 |
"+++ Current\t2010-04-12 10:20:52\n", |
| 343 |
}) |
| 344 |
cd, err := GetContextDiffString(ContextDiff(diff)) |
| 345 |
assertEqual(t, err, nil) |
| 346 |
assertEqual(t, SplitLines(cd)[:2], []string{ |
| 347 |
"*** Original\t2005-01-26 23:30:50\n", |
| 348 |
"--- Current\t2010-04-12 10:20:52\n", |
| 349 |
}) |
| 350 |
} |
| 351 |
|
| 352 |
func TestOutputFormatNoTrailingTabOnEmptyFiledate(t *testing.T) { |
| 353 |
diff := LineDiffParams{ |
| 354 |
A: splitChars("one"), |
| 355 |
B: splitChars("two"), |
| 356 |
FromFile: "Original", |
| 357 |
ToFile: "Current", |
| 358 |
Eol: "\n", |
| 359 |
} |
| 360 |
ud, err := GetUnifiedDiffString(diff) |
| 361 |
assertEqual(t, err, nil) |
| 362 |
assertEqual(t, SplitLines(ud)[:2], []string{"--- Original\n", "+++ Current\n"}) |
| 363 |
|
| 364 |
cd, err := GetContextDiffString(ContextDiff(diff)) |
| 365 |
assertEqual(t, err, nil) |
| 366 |
assertEqual(t, SplitLines(cd)[:2], []string{"*** Original\n", "--- Current\n"}) |
| 367 |
} |
| 368 |
|
| 369 |
func TestOmitFilenames(t *testing.T) { |
| 370 |
diff := LineDiffParams{ |
| 371 |
A: SplitLines("o\nn\ne\n"), |
| 372 |
B: SplitLines("t\nw\no\n"), |
| 373 |
Eol: "\n", |
| 374 |
} |
| 375 |
ud, err := GetUnifiedDiffString(diff) |
| 376 |
assertEqual(t, err, nil) |
| 377 |
assertEqual(t, SplitLines(ud), []string{ |
| 378 |
"@@ -0,0 +1,2 @@\n", |
| 379 |
"+t\n", |
| 380 |
"+w\n", |
| 381 |
"@@ -2,2 +3,0 @@\n", |
| 382 |
"-n\n", |
| 383 |
"-e\n", |
| 384 |
"\n", |
| 385 |
}) |
| 386 |
|
| 387 |
cd, err := GetContextDiffString(ContextDiff(diff)) |
| 388 |
assertEqual(t, err, nil) |
| 389 |
assertEqual(t, SplitLines(cd), []string{ |
| 390 |
"***************\n", |
| 391 |
"*** 0 ****\n", |
| 392 |
"--- 1,2 ----\n", |
| 393 |
"+ t\n", |
| 394 |
"+ w\n", |
| 395 |
"***************\n", |
| 396 |
"*** 2,3 ****\n", |
| 397 |
"- n\n", |
| 398 |
"- e\n", |
| 399 |
"--- 3 ----\n", |
| 400 |
"\n", |
| 401 |
}) |
| 402 |
} |
| 403 |
|
| 404 |
func TestSplitLines(t *testing.T) { |
| 405 |
allTests := []struct { |
| 406 |
input string |
| 407 |
want []string |
| 408 |
}{ |
| 409 |
{"foo", []string{"foo\n"}}, |
| 410 |
{"foo\nbar", []string{"foo\n", "bar\n"}}, |
| 411 |
{"foo\nbar\n", []string{"foo\n", "bar\n", "\n"}}, |
| 412 |
} |
| 413 |
for _, test := range allTests { |
| 414 |
assertEqual(t, SplitLines(test.input), test.want) |
| 415 |
} |
| 416 |
} |
| 417 |
|
| 418 |
func benchmarkSplitLines(b *testing.B, count int) { |
| 419 |
str := strings.Repeat("foo\n", count) |
| 420 |
|
| 421 |
b.ResetTimer() |
| 422 |
|
| 423 |
n := 0 |
| 424 |
for i := 0; i < b.N; i++ { |
| 425 |
n += len(SplitLines(str)) |
| 426 |
} |
| 427 |
} |
| 428 |
|
| 429 |
func BenchmarkSplitLines100(b *testing.B) { |
| 430 |
benchmarkSplitLines(b, 100) |
| 431 |
} |
| 432 |
|
| 433 |
func BenchmarkSplitLines10000(b *testing.B) { |
| 434 |
benchmarkSplitLines(b, 10000) |
| 435 |
} |
| 436 |
|
| 437 |
func prepareFilesToDiff(count, seed int) (As, Bs [][]string) { |
| 438 |
defer runtime.GC() |
| 439 |
return tester.PrepareStringsToDiff(count, seed) |
| 440 |
} |
| 441 |
|
| 442 |
func BenchmarkDiffer(b *testing.B) { |
| 443 |
A, B := prepareFilesToDiff(b.N, 0) |
| 444 |
fmt.Printf("\nDiff length:") |
| 445 |
b.ResetTimer() |
| 446 |
differ := NewDiffer() |
| 447 |
for i := range A { |
| 448 |
var x []string |
| 449 |
for n := 0; n < 5; n++ { |
| 450 |
x, _ = differ.Compare(A[i], B[i]) |
| 451 |
} |
| 452 |
fmt.Printf(" %v", len(x)) |
| 453 |
} |
| 454 |
fmt.Printf("\n") |
| 455 |
} |
| 456 |
|
| 457 |
func BenchmarkMatcher(b *testing.B) { |
| 458 |
A, B := prepareFilesToDiff(b.N, 0) |
| 459 |
fmt.Printf("\nOpcodes count:") |
| 460 |
b.ResetTimer() |
| 461 |
for i := range A { |
| 462 |
var x []OpCode |
| 463 |
for n := 0; n < 5; n++ { |
| 464 |
sm := NewMatcher(A[i], B[i]) |
| 465 |
x = sm.GetOpCodes() |
| 466 |
} |
| 467 |
fmt.Printf(" %v", len(x)) |
| 468 |
} |
| 469 |
fmt.Printf("\n") |
| 470 |
} |
| 471 |
|
| 472 |
func TestDifferCompare(t *testing.T) { |
| 473 |
diff := NewDiffer() |
| 474 |
// Test |
| 475 |
aLst := []string{"foo\n", "bar\n", "baz\n"} |
| 476 |
bLst := []string{"foo\n", "bar1\n", "asdf\n", "baz\n"} |
| 477 |
out, err := diff.Compare(aLst, bLst) |
| 478 |
if err != nil { |
| 479 |
t.Fatal("Differ Compare() error:", err) |
| 480 |
} |
| 481 |
if reflect.DeepEqual(out, []string{ |
| 482 |
" foo\n", |
| 483 |
"- bar\n", |
| 484 |
"+ bar1\n", |
| 485 |
"? +\n", |
| 486 |
"+ asdf\n", |
| 487 |
" baz\n", |
| 488 |
}) != true { |
| 489 |
t.Fatal("Differ Compare failure:", out) |
| 490 |
} |
| 491 |
} |
| 492 |
|
| 493 |
func TestDifferStructuredDump(t *testing.T) { |
| 494 |
diff := NewDiffer() |
| 495 |
out := diff.StructuredDump('+', |
| 496 |
[]string{"foo", "bar", "baz", "quux", "qwerty"}, |
| 497 |
1, 3) |
| 498 |
expected := []DiffLine{DiffLine{'+', "bar"}, DiffLine{'+', "baz"}} |
| 499 |
if !reflect.DeepEqual(out, expected) { |
| 500 |
t.Fatal("Differ StructuredDump failure:", out) |
| 501 |
} |
| 502 |
} |
| 503 |
|
| 504 |
func TestDifferDump(t *testing.T) { |
| 505 |
diff := NewDiffer() |
| 506 |
out := diff.Dump("+", |
| 507 |
[]string{"foo", "bar", "baz", "quux", "qwerty"}, |
| 508 |
1, 3) |
| 509 |
if reflect.DeepEqual(out, []string{"+ bar", "+ baz"}) != true { |
| 510 |
t.Fatal("Differ Dump() failure:", out) |
| 511 |
} |
| 512 |
} |
| 513 |
|
| 514 |
func TestDifferPlainReplace(t *testing.T) { |
| 515 |
diff := NewDiffer() |
| 516 |
aLst := []string{"one\n", "two\n", "three\n", "four\n", "five\n"} |
| 517 |
bLst := []string{"one\n", "two2\n", "three\n", "extra\n"} |
| 518 |
// Test a then b |
| 519 |
out, err := diff.PlainReplace(aLst, 1, 2, bLst, 1, 2) |
| 520 |
if err != nil { |
| 521 |
t.Fatal("Differ PlainReplace() error:", err) |
| 522 |
} |
| 523 |
if reflect.DeepEqual(out, []string{"- two\n", "+ two2\n"}) != true { |
| 524 |
t.Fatal("Differ PlainReplace() failure:", out) |
| 525 |
} |
| 526 |
// Test b then a |
| 527 |
out, err = diff.PlainReplace(aLst, 3, 5, bLst, 3, 4) |
| 528 |
if err != nil { |
| 529 |
t.Fatal("Differ PlainReplace() error:", err) |
| 530 |
} |
| 531 |
if reflect.DeepEqual(out, |
| 532 |
[]string{"+ extra\n", "- four\n", "- five\n"}) != true { |
| 533 |
t.Fatal("Differ PlainReplace() failure:", out) |
| 534 |
} |
| 535 |
} |
| 536 |
|
| 537 |
func TestDifferFancyReplaceAndHelper(t *testing.T) { |
| 538 |
diff := NewDiffer() |
| 539 |
// Test identical sync point, both full |
| 540 |
aLst := []string{"one\n", "asdf\n", "three\n"} |
| 541 |
bLst := []string{"one\n", "two2\n", "three\n"} |
| 542 |
out, err := diff.FancyReplace(aLst, 0, 3, bLst, 0, 3) |
| 543 |
if err != nil { |
| 544 |
t.Fatal("Differ FancyReplace() error:", err) |
| 545 |
} |
| 546 |
if reflect.DeepEqual(out, |
| 547 |
[]string{" one\n", "- asdf\n", "+ two2\n", " three\n"}) != true { |
| 548 |
t.Fatal("Differ FancyReplace() failure:", out) |
| 549 |
} |
| 550 |
// Test close sync point, both full |
| 551 |
aLst = []string{"one\n", "two123456\n", "asdf\n", "three\n"} |
| 552 |
bLst = []string{"one\n", "two123457\n", "qwerty\n", "three\n"} |
| 553 |
out, err = diff.FancyReplace(aLst, 1, 3, bLst, 1, 3) |
| 554 |
if err != nil { |
| 555 |
t.Fatal("Differ FancyReplace() error:", err) |
| 556 |
} |
| 557 |
if reflect.DeepEqual(out, []string{ |
| 558 |
"- two123456\n", |
| 559 |
"? ^\n", |
| 560 |
"+ two123457\n", |
| 561 |
"? ^\n", |
| 562 |
"- asdf\n", |
| 563 |
"+ qwerty\n", |
| 564 |
}) != true { |
| 565 |
t.Fatal("Differ FancyReplace() failure:", out) |
| 566 |
} |
| 567 |
// Test no identical no close |
| 568 |
aLst = []string{"one\n", "asdf\n", "three\n"} |
| 569 |
bLst = []string{"one\n", "qwerty\n", "three\n"} |
| 570 |
out, err = diff.FancyReplace(aLst, 1, 2, bLst, 1, 2) |
| 571 |
if err != nil { |
| 572 |
t.Fatal("Differ FancyReplace() error:", err) |
| 573 |
} |
| 574 |
if reflect.DeepEqual(out, []string{ |
| 575 |
"- asdf\n", |
| 576 |
"+ qwerty\n", |
| 577 |
}) != true { |
| 578 |
t.Fatal("Differ FancyReplace() failure:", out) |
| 579 |
} |
| 580 |
} |
| 581 |
|
| 582 |
func TestDifferQFormat(t *testing.T) { |
| 583 |
diff := NewDiffer() |
| 584 |
aStr := "\tfoo2bar\n" |
| 585 |
aTag := " ^ ^" |
| 586 |
bStr := "\tfoo3baz\n" |
| 587 |
bTag := " ^ ^" |
| 588 |
out := diff.QFormat(aStr, bStr, aTag, bTag) |
| 589 |
if reflect.DeepEqual(out, []string{ |
| 590 |
"- \tfoo2bar\n", |
| 591 |
"? \t ^ ^\n", |
| 592 |
"+ \tfoo3baz\n", |
| 593 |
"? \t ^ ^\n", |
| 594 |
}) != true { |
| 595 |
t.Fatal("Differ QFormat() failure:", out) |
| 596 |
} |
| 597 |
} |
| 598 |
|
| 599 |
func TestGetUnifiedDiffString(t *testing.T) { |
| 600 |
A := "one\ntwo\nthree\nfour\nfive\nsix\nseven\neight\nnine\nten\n" |
| 601 |
B := "one\ntwo\nthr33\nfour\nfive\nsix\nseven\neight\nnine\nten\n" |
| 602 |
// Build diff |
| 603 |
diff := LineDiffParams{A: SplitLines(A), |
| 604 |
FromFile: "file", FromDate: "then", |
| 605 |
B: SplitLines(B), |
| 606 |
ToFile: "tile", ToDate: "now", Eol: "", Context: 1} |
| 607 |
// Run test |
| 608 |
diffStr, err := GetUnifiedDiffString(diff) |
| 609 |
if err != nil { |
| 610 |
t.Fatal("GetUnifiedDiffString error:", err) |
| 611 |
} |
| 612 |
exp := "--- file\tthen\n+++ tile\tnow\n@@ -2,3 +2,3 @@\n two\n-three\n+thr33\n four\n" |
| 613 |
if diffStr != exp { |
| 614 |
t.Fatal("GetUnifiedDiffString failure:", diffStr) |
| 615 |
} |
| 616 |
} |