164 lines
6.8 KiB
Swift
164 lines
6.8 KiB
Swift
import HelderCore
|
|
import Testing
|
|
|
|
@Suite struct LineDiffTests {
|
|
@Test("identical text has no changes")
|
|
func identical() {
|
|
let diff = LineDiff.build(original: "a\nb\nc\n", updated: "a\nb\nc\n")
|
|
#expect(diff.added == 0)
|
|
#expect(diff.removed == 0)
|
|
#expect(diff.rows.allSatisfy { $0.sign == .same })
|
|
}
|
|
|
|
@Test("one changed line is one addition and one removal")
|
|
func singleChange() {
|
|
let diff = LineDiff.build(original: "a\nb\nc", updated: "a\nB\nc")
|
|
#expect(diff.added == 1)
|
|
#expect(diff.removed == 1)
|
|
#expect(diff.rows.map(\.sign) == [.same, .removed, .added, .same])
|
|
}
|
|
|
|
@Test("rows carry old and new line numbers")
|
|
func lineNumbers() {
|
|
let diff = LineDiff.build(original: "a\nold\nb", updated: "a\nnew\nb\nc")
|
|
#expect(diff.rows == [
|
|
DiffRow(sign: .same, oldNo: 1, newNo: 1, text: "a"),
|
|
DiffRow(sign: .removed, oldNo: 2, newNo: nil, text: "old"),
|
|
DiffRow(sign: .added, oldNo: nil, newNo: 2, text: "new"),
|
|
DiffRow(sign: .same, oldNo: 3, newNo: 3, text: "b"),
|
|
DiffRow(sign: .added, oldNo: nil, newNo: 4, text: "c"),
|
|
])
|
|
}
|
|
|
|
@Test("an empty original is all additions, as for a new file")
|
|
func newFile() {
|
|
let diff = LineDiff.build(original: "", updated: "x\ny", status: .added)
|
|
#expect(diff.added == 2)
|
|
#expect(diff.removed == 0)
|
|
#expect(diff.left.isEmpty)
|
|
#expect(diff.right.count == 2)
|
|
#expect(diff.isAddedFile)
|
|
#expect(!diff.isDeletedFile)
|
|
}
|
|
|
|
@Test("an empty update is all removals, as for a deleted file")
|
|
func deletedFile() {
|
|
let diff = LineDiff.build(original: "x\ny\nz", updated: "", status: .deleted)
|
|
#expect(diff.removed == 3)
|
|
#expect(diff.added == 0)
|
|
#expect(diff.right.isEmpty)
|
|
#expect(diff.isDeletedFile)
|
|
#expect(!diff.isAddedFile)
|
|
}
|
|
|
|
@Test("both sides mark the changed line and leave the rest unmarked")
|
|
func sideMarks() {
|
|
let diff = LineDiff.build(original: "keep\nold\nkeep", updated: "keep\nnew\nkeep")
|
|
#expect(diff.left.first { $0.text == "old" }?.mark == .removed)
|
|
#expect(diff.right.first { $0.text == "new" }?.mark == .added)
|
|
#expect(diff.left.first { $0.text == "keep" }?.mark == nil)
|
|
}
|
|
|
|
@Test("split rows pair unchanged lines and stack changes beside each other")
|
|
func splitAlignment() {
|
|
let diff = LineDiff.build(original: "a\nold\nb", updated: "a\nnew\nb")
|
|
#expect(diff.split.allSatisfy { $0.left != nil || $0.right != nil })
|
|
let paired = diff.split.filter { $0.left != nil && $0.right != nil && $0.left?.text == $0.right?.text }
|
|
#expect(paired.compactMap { $0.left?.text } == ["a", "b"])
|
|
#expect(diff.split[1] == SplitRow(left: SideLine(no: 2, text: "old", mark: .removed), right: SideLine(no: 2, text: "new", mark: .added)))
|
|
}
|
|
|
|
@Test("a longer removal than addition leaves fillers on the right")
|
|
func splitFillers() {
|
|
let diff = LineDiff.build(original: "a\nx\ny\nb", updated: "a\nz\nb")
|
|
#expect(diff.split.map { [$0.left?.text, $0.right?.text] } == [["a", "a"], ["x", "z"], ["y", nil], ["b", "b"]])
|
|
}
|
|
|
|
@Test("a single trailing newline difference is no change")
|
|
func trailingNewline() {
|
|
let diff = LineDiff.build(original: "a\nb", updated: "a\nb\n")
|
|
#expect(diff.added == 0)
|
|
#expect(diff.removed == 0)
|
|
}
|
|
|
|
@Test("CRLF text keeps its carriage returns, so a line-ending change shows as a change")
|
|
func crlf() {
|
|
let diff = LineDiff.build(original: "a\r\nb\r\n", updated: "a\nb\n")
|
|
#expect(diff.removed == 2)
|
|
#expect(diff.added == 2)
|
|
}
|
|
|
|
@Test("unicode lines compare whole")
|
|
func unicode() {
|
|
let diff = LineDiff.build(original: "café\n😀", updated: "cafe\u{301}\n😀")
|
|
#expect(diff.rows.last == DiffRow(sign: .same, oldNo: 2, newNo: 2, text: "😀"))
|
|
}
|
|
|
|
@Test("on a tie a removal comes before an addition")
|
|
func tieOrder() {
|
|
let diff = LineDiff.build(original: "a\nb", updated: "b\na")
|
|
#expect(diff.rows.map(\.sign) == [.removed, .same, .added])
|
|
}
|
|
|
|
@Test("the diff keeps the text pair it was built from")
|
|
func carriesTexts() {
|
|
let diff = LineDiff.build(original: "bye", updated: "", status: .deleted)
|
|
#expect(diff.original == "bye")
|
|
#expect(diff.updated == "")
|
|
}
|
|
|
|
@Test("lines() drops one trailing newline and reads empty text as no lines")
|
|
func lines() {
|
|
#expect(LineDiff.lines("") == [])
|
|
#expect(LineDiff.lines("a\n") == ["a"])
|
|
#expect(LineDiff.lines("a\n\n") == ["a", ""])
|
|
#expect(LineDiff.lines("\n") == [""])
|
|
#expect(LineDiff.lines("a\r\nb\r\n") == ["a\r", "b\r"])
|
|
}
|
|
|
|
@Test("a 60k-line file with one edit diffs without a full matrix")
|
|
func largeFileSingleEdit() {
|
|
var lines = (0..<60_000).map { "line \($0)" }
|
|
let base = lines.joined(separator: "\n")
|
|
lines[30_000] = "line 30000 edited"
|
|
let diff = LineDiff.build(original: base, updated: lines.joined(separator: "\n"))
|
|
#expect(diff.added == 1)
|
|
#expect(diff.removed == 1)
|
|
#expect(diff.right[30_000].mark == .added)
|
|
}
|
|
|
|
@Test("a large rewrite with no shared lines replaces everything")
|
|
func largeRewrite() {
|
|
let a = (0..<40_000).map { "old \($0)" }.joined(separator: "\n")
|
|
let b = (0..<40_000).map { "new \($0)" }.joined(separator: "\n")
|
|
let diff = LineDiff.build(original: a, updated: b)
|
|
#expect(diff.removed == 40_000)
|
|
#expect(diff.added == 40_000)
|
|
}
|
|
|
|
@Test("a large, heavily edited file keeps its unchanged head and tail")
|
|
func largeHeadAndTail() {
|
|
let head = (0..<5_000).map { "head \($0)" }
|
|
let tail = (0..<5_000).map { "tail \($0)" }
|
|
func middle(_ prefix: String) -> [String] { (0..<30_000).map { "\(prefix) \($0 % 7)" } }
|
|
let diff = LineDiff.build(
|
|
original: (head + middle("x") + tail).joined(separator: "\n"),
|
|
updated: (head + middle("y") + tail).joined(separator: "\n")
|
|
)
|
|
#expect(diff.left.prefix(5_000).allSatisfy { $0.mark == nil })
|
|
#expect(diff.left.suffix(5_000).allSatisfy { $0.mark == nil })
|
|
#expect(diff.added == 30_000)
|
|
}
|
|
|
|
@Test("over the cell budget, unique anchor lines still pair the moved-around middle")
|
|
func anchorsSplitLargeRegions() {
|
|
let shared = (0..<3_000).map { "shared \($0)" }
|
|
let a = shared.flatMap { ["\($0)", "a-noise"] }
|
|
let b = shared.flatMap { ["\($0)", "b-noise"] }
|
|
let diff = LineDiff.build(original: a.joined(separator: "\n"), updated: b.joined(separator: "\n"))
|
|
#expect(diff.rows.filter { $0.sign == .same }.count == 3_000)
|
|
#expect(diff.added == 3_000)
|
|
#expect(diff.removed == 3_000)
|
|
}
|
|
}
|