| 1 | import { describe, expect, test } from "vitest"; |
| 2 | |
| 3 | import { dedent } from "../util"; |
| 4 | |
| 5 | import { myersCharDiff, myersDiff } from "./myers"; |
| 6 | |
| 7 | describe("Test myers diff function", () => { |
| 8 | test("should ...", () => { |
| 9 | const linesA = dedent` |
| 10 | A |
| 11 | B |
| 12 | C |
| 13 | D |
| 14 | E |
| 15 | `; |
| 16 | const linesB = dedent` |
| 17 | A |
| 18 | B |
| 19 | C' |
| 20 | D' |
| 21 | E |
| 22 | `; |
| 23 | const diffLines = myersDiff(linesA, linesB); |
| 24 | expect(diffLines).toEqual([ |
| 25 | { type: "same", line: "A" }, |
| 26 | { type: "same", line: "B" }, |
| 27 | { type: "old", line: "C" }, |
| 28 | { type: "old", line: "D" }, |
| 29 | { type: "new", line: "C'" }, |
| 30 | { type: "new", line: "D'" }, |
| 31 | { type: "same", line: "E" }, |
| 32 | ]); |
| 33 | }); |
| 34 | |
| 35 | test("should ignore newline differences at end", () => { |
| 36 | const linesA = "A\nB\nC\n"; |
| 37 | const linesB = "A\nB\nC"; |
| 38 | |
| 39 | const diffLines = myersDiff(linesA, linesB); |
| 40 | expect(diffLines).toEqual([ |
| 41 | { type: "same", line: "A" }, |
| 42 | { type: "same", line: "B" }, |
| 43 | { type: "same", line: "C" }, |
| 44 | ]); |
| 45 | }); |
| 46 | |
| 47 | test("should ignore single-line whitespace-only differences", () => { |
| 48 | const linesA = "A\n B\nC\n"; |
| 49 | const linesB = "A\nB\nC"; |
| 50 | |
| 51 | const diffLines = myersDiff(linesA, linesB); |
| 52 | expect(diffLines).toEqual([ |
| 53 | { type: "same", line: "A" }, |
| 54 | { type: "same", line: " B" }, |
| 55 | { type: "same", line: "C" }, |
| 56 | ]); |
| 57 | }); |
| 58 | }); |
| 59 | |
| 60 | describe("Test myersCharDiff function on the same line", () => { |
| 61 | test("should differentiate character changes", () => { |
| 62 | const oldContent = "hello world"; |
| 63 | const newContent = "hello earth"; |
| 64 | |
| 65 | const diffChars = myersCharDiff(oldContent, newContent); |
| 66 | expect(diffChars).toEqual([ |
| 67 | { |
| 68 | type: "same", |
| 69 | char: "hello ", |
| 70 | oldIndex: 0, |
| 71 | newIndex: 0, |
| 72 | oldCharIndexInLine: 0, |
| 73 | newCharIndexInLine: 0, |
| 74 | oldLineIndex: 0, |
| 75 | newLineIndex: 0, |
| 76 | }, |
| 77 | { |
| 78 | type: "old", |
| 79 | char: "wo", |
| 80 | oldIndex: 6, |
| 81 | oldCharIndexInLine: 6, |
| 82 | oldLineIndex: 0, |
| 83 | }, |
| 84 | { |
| 85 | type: "new", |
| 86 | char: "ea", |
| 87 | newIndex: 6, |
| 88 | newCharIndexInLine: 6, |
| 89 | newLineIndex: 0, |
| 90 | }, |
| 91 | { |
| 92 | type: "same", |
| 93 | char: "r", |
| 94 | oldIndex: 8, |
| 95 | newIndex: 8, |
| 96 | oldCharIndexInLine: 8, |
| 97 | newCharIndexInLine: 8, |
| 98 | oldLineIndex: 0, |
| 99 | newLineIndex: 0, |
| 100 | }, |
| 101 | { |
| 102 | type: "old", |
| 103 | char: "ld", |
| 104 | oldIndex: 9, |
| 105 | oldCharIndexInLine: 9, |
| 106 | oldLineIndex: 0, |
| 107 | }, |
| 108 | { |
| 109 | type: "new", |
| 110 | char: "th", |
| 111 | newIndex: 9, |
| 112 | newCharIndexInLine: 9, |
| 113 | newLineIndex: 0, |
| 114 | }, |
| 115 | ]); |
| 116 | }); |
| 117 | |
| 118 | test("should handle insertions", () => { |
| 119 | const oldContent = "abc"; |
| 120 | const newContent = "abxyzc"; |
| 121 | |
| 122 | const diffChars = myersCharDiff(oldContent, newContent); |
| 123 | expect(diffChars).toEqual([ |
| 124 | { |
| 125 | type: "same", |
| 126 | char: "ab", |
| 127 | oldIndex: 0, |
| 128 | newIndex: 0, |
| 129 | oldCharIndexInLine: 0, |
| 130 | newCharIndexInLine: 0, |
| 131 | oldLineIndex: 0, |
| 132 | newLineIndex: 0, |
| 133 | }, |
| 134 | { |
| 135 | type: "new", |
| 136 | char: "xyz", |
| 137 | newIndex: 2, |
| 138 | newCharIndexInLine: 2, |
| 139 | newLineIndex: 0, |
| 140 | }, |
| 141 | { |
| 142 | type: "same", |
| 143 | char: "c", |
| 144 | oldIndex: 2, |
| 145 | newIndex: 5, |
| 146 | oldCharIndexInLine: 2, |
| 147 | newCharIndexInLine: 5, |
| 148 | oldLineIndex: 0, |
| 149 | newLineIndex: 0, |
| 150 | }, |
| 151 | ]); |
| 152 | }); |
| 153 | |
| 154 | test("should handle deletions", () => { |
| 155 | const oldContent = "abxyzc"; |
| 156 | const newContent = "abc"; |
| 157 | |
| 158 | const diffChars = myersCharDiff(oldContent, newContent); |
| 159 | expect(diffChars).toEqual([ |
| 160 | { |
| 161 | type: "same", |
| 162 | char: "ab", |
| 163 | oldIndex: 0, |
| 164 | newIndex: 0, |
| 165 | oldCharIndexInLine: 0, |
| 166 | newCharIndexInLine: 0, |
| 167 | oldLineIndex: 0, |
| 168 | newLineIndex: 0, |
| 169 | }, |
| 170 | { |
| 171 | type: "old", |
| 172 | char: "xyz", |
| 173 | oldIndex: 2, |
| 174 | oldCharIndexInLine: 2, |
| 175 | oldLineIndex: 0, |
| 176 | }, |
| 177 | { |
| 178 | type: "same", |
| 179 | char: "c", |
| 180 | oldIndex: 5, |
| 181 | newIndex: 2, |
| 182 | oldCharIndexInLine: 5, |
| 183 | newCharIndexInLine: 2, |
| 184 | oldLineIndex: 0, |
| 185 | newLineIndex: 0, |
| 186 | }, |
| 187 | ]); |
| 188 | }); |
| 189 | |
| 190 | test("should handle empty strings", () => { |
| 191 | const oldContent = ""; |
| 192 | const newContent = "abc"; |
| 193 | |
| 194 | const diffChars = myersCharDiff(oldContent, newContent); |
| 195 | expect(diffChars).toEqual([ |
| 196 | { |
| 197 | type: "new", |
| 198 | char: "abc", |
| 199 | newIndex: 0, |
| 200 | newCharIndexInLine: 0, |
| 201 | newLineIndex: 0, |
| 202 | }, |
| 203 | ]); |
| 204 | }); |
| 205 | |
| 206 | test("should handle identical strings", () => { |
| 207 | const content = "no changes here"; |
| 208 | |
| 209 | const diffChars = myersCharDiff(content, content); |
| 210 | expect(diffChars).toEqual([ |
| 211 | { |
| 212 | type: "same", |
| 213 | char: "no changes here", |
| 214 | oldIndex: 0, |
| 215 | newIndex: 0, |
| 216 | oldCharIndexInLine: 0, |
| 217 | newCharIndexInLine: 0, |
| 218 | oldLineIndex: 0, |
| 219 | newLineIndex: 0, |
| 220 | }, |
| 221 | ]); |
| 222 | }); |
| 223 | |
| 224 | test("should handle whitespace changes", () => { |
| 225 | const oldContent = "hello world"; |
| 226 | const newContent = "hello world"; |
| 227 | |
| 228 | const diffChars = myersCharDiff(oldContent, newContent); |
| 229 | expect(diffChars).toEqual([ |
| 230 | { |
| 231 | type: "same", |
| 232 | char: "hello ", |
| 233 | oldIndex: 0, |
| 234 | newIndex: 0, |
| 235 | oldCharIndexInLine: 0, |
| 236 | newCharIndexInLine: 0, |
| 237 | oldLineIndex: 0, |
| 238 | newLineIndex: 0, |
| 239 | }, |
| 240 | { |
| 241 | type: "new", |
| 242 | char: " ", |
| 243 | newIndex: 6, |
| 244 | newCharIndexInLine: 6, |
| 245 | newLineIndex: 0, |
| 246 | }, |
| 247 | { |
| 248 | type: "same", |
| 249 | char: "world", |
| 250 | oldIndex: 6, |
| 251 | newIndex: 7, |
| 252 | oldCharIndexInLine: 6, |
| 253 | newCharIndexInLine: 7, |
| 254 | oldLineIndex: 0, |
| 255 | newLineIndex: 0, |
| 256 | }, |
| 257 | ]); |
| 258 | }); |
| 259 | |
| 260 | test("should handle complex changes", () => { |
| 261 | const oldContent = "The quick brown fox jumps over the lazy dog"; |
| 262 | const newContent = "The fast brown fox leaps over the sleeping dog"; |
| 263 | |
| 264 | const diffChars = myersCharDiff(oldContent, newContent); |
| 265 | expect(diffChars).toEqual([ |
| 266 | { |
| 267 | type: "same", |
| 268 | char: "The ", |
| 269 | oldIndex: 0, |
| 270 | newIndex: 0, |
| 271 | oldCharIndexInLine: 0, |
| 272 | newCharIndexInLine: 0, |
| 273 | oldLineIndex: 0, |
| 274 | newLineIndex: 0, |
| 275 | }, |
| 276 | { |
| 277 | type: "old", |
| 278 | char: "quick", |
| 279 | oldIndex: 4, |
| 280 | oldCharIndexInLine: 4, |
| 281 | oldLineIndex: 0, |
| 282 | }, |
| 283 | { |
| 284 | type: "new", |
| 285 | char: "fast", |
| 286 | newIndex: 4, |
| 287 | newCharIndexInLine: 4, |
| 288 | newLineIndex: 0, |
| 289 | }, |
| 290 | { |
| 291 | type: "same", |
| 292 | char: " brown fox ", |
| 293 | oldIndex: 9, |
| 294 | newIndex: 8, |
| 295 | oldCharIndexInLine: 9, |
| 296 | newCharIndexInLine: 8, |
| 297 | oldLineIndex: 0, |
| 298 | newLineIndex: 0, |
| 299 | }, |
| 300 | { |
| 301 | type: "old", |
| 302 | char: "jum", |
| 303 | oldIndex: 20, |
| 304 | oldCharIndexInLine: 20, |
| 305 | oldLineIndex: 0, |
| 306 | }, |
| 307 | { |
| 308 | type: "new", |
| 309 | char: "lea", |
| 310 | newIndex: 19, |
| 311 | newCharIndexInLine: 19, |
| 312 | newLineIndex: 0, |
| 313 | }, |
| 314 | { |
| 315 | type: "same", |
| 316 | char: "ps over the ", |
| 317 | oldIndex: 23, |
| 318 | newIndex: 22, |
| 319 | oldCharIndexInLine: 23, |
| 320 | newCharIndexInLine: 22, |
| 321 | oldLineIndex: 0, |
| 322 | newLineIndex: 0, |
| 323 | }, |
| 324 | { |
| 325 | type: "new", |
| 326 | char: "s", |
| 327 | newIndex: 34, |
| 328 | newCharIndexInLine: 34, |
| 329 | newLineIndex: 0, |
| 330 | }, |
| 331 | { |
| 332 | type: "same", |
| 333 | char: "l", |
| 334 | oldIndex: 35, |
| 335 | newIndex: 35, |
| 336 | oldCharIndexInLine: 35, |
| 337 | newCharIndexInLine: 35, |
| 338 | oldLineIndex: 0, |
| 339 | newLineIndex: 0, |
| 340 | }, |
| 341 | { |
| 342 | type: "old", |
| 343 | char: "azy", |
| 344 | oldIndex: 36, |
| 345 | oldCharIndexInLine: 36, |
| 346 | oldLineIndex: 0, |
| 347 | }, |
| 348 | { |
| 349 | type: "new", |
| 350 | char: "eeping", |
| 351 | newIndex: 36, |
| 352 | newCharIndexInLine: 36, |
| 353 | newLineIndex: 0, |
| 354 | }, |
| 355 | { |
| 356 | type: "same", |
| 357 | char: " dog", |
| 358 | oldIndex: 39, |
| 359 | newIndex: 42, |
| 360 | oldCharIndexInLine: 39, |
| 361 | newCharIndexInLine: 42, |
| 362 | oldLineIndex: 0, |
| 363 | newLineIndex: 0, |
| 364 | }, |
| 365 | ]); |
| 366 | }); |
| 367 | }); |
| 368 | |
| 369 | describe("Test myersCharDiff function on different lines", () => { |
| 370 | test("should track line indices for multi-line changes", () => { |
| 371 | const oldContent = ["Line one", "Line two", "Line three"].join("\n"); |
| 372 | |
| 373 | const newContent = ["Line one", "Modified line", "Line three"].join("\n"); |
| 374 | |
| 375 | const diffChars = myersCharDiff(oldContent, newContent); |
| 376 | expect(diffChars).toEqual([ |
| 377 | { |
| 378 | type: "same", |
| 379 | char: "Line one", |
| 380 | oldIndex: 0, |
| 381 | newIndex: 0, |
| 382 | oldCharIndexInLine: 0, |
| 383 | newCharIndexInLine: 0, |
| 384 | oldLineIndex: 0, |
| 385 | newLineIndex: 0, |
| 386 | }, |
| 387 | { |
| 388 | type: "same", |
| 389 | char: "\n", |
| 390 | oldIndex: 8, |
| 391 | newIndex: 8, |
| 392 | oldCharIndexInLine: 8, |
| 393 | newCharIndexInLine: 8, |
| 394 | oldLineIndex: 0, |
| 395 | newLineIndex: 0, |
| 396 | }, |
| 397 | { |
| 398 | type: "old", |
| 399 | char: "L", |
| 400 | oldIndex: 9, |
| 401 | oldCharIndexInLine: 0, |
| 402 | oldLineIndex: 1, |
| 403 | }, |
| 404 | { |
| 405 | type: "new", |
| 406 | char: "Mod", |
| 407 | newIndex: 9, |
| 408 | newCharIndexInLine: 0, |
| 409 | newLineIndex: 1, |
| 410 | }, |
| 411 | { |
| 412 | char: "i", |
| 413 | newCharIndexInLine: 3, |
| 414 | newIndex: 12, |
| 415 | newLineIndex: 1, |
| 416 | oldCharIndexInLine: 1, |
| 417 | oldIndex: 10, |
| 418 | oldLineIndex: 1, |
| 419 | type: "same", |
| 420 | }, |
| 421 | { |
| 422 | char: "n", |
| 423 | oldCharIndexInLine: 2, |
| 424 | oldIndex: 11, |
| 425 | oldLineIndex: 1, |
| 426 | type: "old", |
| 427 | }, |
| 428 | { |
| 429 | char: "fi", |
| 430 | newCharIndexInLine: 4, |
| 431 | newIndex: 13, |
| 432 | newLineIndex: 1, |
| 433 | type: "new", |
| 434 | }, |
| 435 | { |
| 436 | char: "e", |
| 437 | newCharIndexInLine: 6, |
| 438 | newIndex: 15, |
| 439 | newLineIndex: 1, |
| 440 | oldCharIndexInLine: 3, |
| 441 | oldIndex: 12, |
| 442 | oldLineIndex: 1, |
| 443 | type: "same", |
| 444 | }, |
| 445 | { |
| 446 | char: "d", |
| 447 | newCharIndexInLine: 7, |
| 448 | newIndex: 16, |
| 449 | newLineIndex: 1, |
| 450 | type: "new", |
| 451 | }, |
| 452 | { |
| 453 | char: " ", |
| 454 | newCharIndexInLine: 8, |
| 455 | newIndex: 17, |
| 456 | newLineIndex: 1, |
| 457 | oldCharIndexInLine: 4, |
| 458 | oldIndex: 13, |
| 459 | oldLineIndex: 1, |
| 460 | type: "same", |
| 461 | }, |
| 462 | { |
| 463 | type: "old", |
| 464 | char: "two", |
| 465 | oldCharIndexInLine: 5, |
| 466 | oldIndex: 14, |
| 467 | oldLineIndex: 1, |
| 468 | }, |
| 469 | { |
| 470 | type: "new", |
| 471 | char: "line", |
| 472 | newCharIndexInLine: 9, |
| 473 | newIndex: 18, |
| 474 | newLineIndex: 1, |
| 475 | }, |
| 476 | { |
| 477 | type: "same", |
| 478 | char: "\n", |
| 479 | oldIndex: 17, |
| 480 | oldCharIndexInLine: 8, |
| 481 | oldLineIndex: 1, |
| 482 | newIndex: 22, |
| 483 | newCharIndexInLine: 13, |
| 484 | newLineIndex: 1, |
| 485 | }, |
| 486 | { |
| 487 | type: "same", |
| 488 | char: "Line three", |
| 489 | oldIndex: 18, |
| 490 | newIndex: 23, |
| 491 | oldCharIndexInLine: 0, |
| 492 | newCharIndexInLine: 0, |
| 493 | oldLineIndex: 2, |
| 494 | newLineIndex: 2, |
| 495 | }, |
| 496 | ]); |
| 497 | }); |
| 498 | |
| 499 | test("should track line indices when adding new lines", () => { |
| 500 | const oldContent = ["First line", "Last line"].join("\n"); |
| 501 | |
| 502 | const newContent = [ |
| 503 | "First line", |
| 504 | "Middle line", |
| 505 | "Another middle", |
| 506 | "Last line", |
| 507 | ].join("\n"); |
| 508 | |
| 509 | const diffChars = myersCharDiff(oldContent, newContent); |
| 510 | expect(diffChars).toEqual([ |
| 511 | { |
| 512 | type: "same", |
| 513 | char: "First line", |
| 514 | oldIndex: 0, |
| 515 | newIndex: 0, |
| 516 | oldCharIndexInLine: 0, |
| 517 | newCharIndexInLine: 0, |
| 518 | oldLineIndex: 0, |
| 519 | newLineIndex: 0, |
| 520 | }, |
| 521 | { |
| 522 | type: "same", |
| 523 | char: "\n", |
| 524 | oldIndex: 10, |
| 525 | newIndex: 10, |
| 526 | oldCharIndexInLine: 10, |
| 527 | newCharIndexInLine: 10, |
| 528 | oldLineIndex: 0, |
| 529 | newLineIndex: 0, |
| 530 | }, |
| 531 | { |
| 532 | type: "new", |
| 533 | char: "Middle line", |
| 534 | newIndex: 11, |
| 535 | newCharIndexInLine: 0, |
| 536 | newLineIndex: 1, |
| 537 | }, |
| 538 | { |
| 539 | type: "new", |
| 540 | char: "\n", |
| 541 | newIndex: 22, |
| 542 | newCharIndexInLine: 11, |
| 543 | newLineIndex: 1, |
| 544 | }, |
| 545 | { |
| 546 | type: "new", |
| 547 | char: "Another middle", |
| 548 | newCharIndexInLine: 0, |
| 549 | newIndex: 23, |
| 550 | newLineIndex: 2, |
| 551 | }, |
| 552 | { |
| 553 | type: "new", |
| 554 | char: "\n", |
| 555 | newCharIndexInLine: 14, |
| 556 | newIndex: 37, |
| 557 | newLineIndex: 2, |
| 558 | }, |
| 559 | { |
| 560 | type: "same", |
| 561 | char: "Last line", |
| 562 | oldIndex: 11, |
| 563 | oldCharIndexInLine: 0, |
| 564 | oldLineIndex: 1, |
| 565 | newIndex: 38, |
| 566 | newCharIndexInLine: 0, |
| 567 | newLineIndex: 3, |
| 568 | }, |
| 569 | ]); |
| 570 | }); |
| 571 | |
| 572 | test("should track line indices when removing lines", () => { |
| 573 | const oldContent = [ |
| 574 | "Start", |
| 575 | "Line to remove", |
| 576 | "Another to remove", |
| 577 | "End", |
| 578 | ].join("\n"); |
| 579 | |
| 580 | const newContent = ["Start", "End"].join("\n"); |
| 581 | |
| 582 | const diffChars = myersCharDiff(oldContent, newContent); |
| 583 | expect(diffChars).toEqual([ |
| 584 | { |
| 585 | type: "same", |
| 586 | char: "Start", |
| 587 | oldIndex: 0, |
| 588 | newIndex: 0, |
| 589 | oldCharIndexInLine: 0, |
| 590 | newCharIndexInLine: 0, |
| 591 | oldLineIndex: 0, |
| 592 | newLineIndex: 0, |
| 593 | }, |
| 594 | { |
| 595 | type: "same", |
| 596 | char: "\n", |
| 597 | oldIndex: 5, |
| 598 | newIndex: 5, |
| 599 | oldCharIndexInLine: 5, |
| 600 | newCharIndexInLine: 5, |
| 601 | oldLineIndex: 0, |
| 602 | newLineIndex: 0, |
| 603 | }, |
| 604 | { |
| 605 | type: "old", |
| 606 | char: "Line to remove", |
| 607 | oldIndex: 6, |
| 608 | oldCharIndexInLine: 0, |
| 609 | oldLineIndex: 1, |
| 610 | }, |
| 611 | { |
| 612 | type: "old", |
| 613 | char: "\n", |
| 614 | oldCharIndexInLine: 14, |
| 615 | oldIndex: 20, |
| 616 | oldLineIndex: 1, |
| 617 | }, |
| 618 | { |
| 619 | type: "old", |
| 620 | char: "Another to remove", |
| 621 | oldCharIndexInLine: 0, |
| 622 | oldIndex: 21, |
| 623 | oldLineIndex: 2, |
| 624 | }, |
| 625 | { |
| 626 | type: "old", |
| 627 | char: "\n", |
| 628 | oldCharIndexInLine: 17, |
| 629 | oldIndex: 38, |
| 630 | oldLineIndex: 2, |
| 631 | }, |
| 632 | { |
| 633 | type: "same", |
| 634 | char: "End", |
| 635 | oldIndex: 39, |
| 636 | newIndex: 6, |
| 637 | oldCharIndexInLine: 0, |
| 638 | newCharIndexInLine: 0, |
| 639 | oldLineIndex: 3, |
| 640 | newLineIndex: 1, |
| 641 | }, |
| 642 | ]); |
| 643 | }); |
| 644 | }); |