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