restores Helder in all its glory
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
import HelderCore
|
||||
import Testing
|
||||
|
||||
@Suite struct DiffViewsTests {
|
||||
private func actual(_ original: String, _ updated: String) -> [ViewLine] {
|
||||
DiffViews.actual(LineDiff.build(original: original, updated: updated, status: .modified))
|
||||
}
|
||||
|
||||
@Test("a rewritten line is tinted in place and draws no gap rule")
|
||||
func rewrittenLine() {
|
||||
let lines = actual("a\nold\nb", "a\nnew\nb")
|
||||
#expect(lines.map(\.text) == ["a", "new", "b"])
|
||||
#expect(lines[1].style == .added)
|
||||
#expect(lines.allSatisfy { $0.gap == nil })
|
||||
}
|
||||
|
||||
@Test("a pure deletion marks the next line's top edge")
|
||||
func deletionMarksNextLine() {
|
||||
let lines = actual("a\ngone\nb", "a\nb")
|
||||
#expect(lines[1].text == "b")
|
||||
#expect(lines[1].gap == .above)
|
||||
#expect(lines[1].style == nil)
|
||||
}
|
||||
|
||||
@Test("a deletion at the end of the file rules under the last line")
|
||||
func deletionAtEnd() {
|
||||
let lines = actual("a\nb\ntail", "a\nb")
|
||||
#expect(lines[1].gap == .below)
|
||||
#expect(lines[0].gap == nil)
|
||||
}
|
||||
|
||||
@Test("a deletion at the start of the file rules above the first line")
|
||||
func deletionAtStart() {
|
||||
let lines = actual("gone\na", "a")
|
||||
#expect(lines == [ViewLine(no: 1, text: "a", gap: .above)])
|
||||
}
|
||||
|
||||
@Test("an insertion is tinted and leaves its neighbours alone")
|
||||
func insertion() {
|
||||
let lines = actual("a\nb", "a\nNEW\nb")
|
||||
#expect(lines[1].style == .added)
|
||||
#expect(lines.allSatisfy { $0.gap == nil })
|
||||
}
|
||||
|
||||
@Test("an unchanged file carries no marks")
|
||||
func unchanged() {
|
||||
let lines = actual("a\nb\n", "a\nb\n")
|
||||
#expect(lines.allSatisfy { $0.style == nil && $0.gap == nil })
|
||||
}
|
||||
|
||||
@Test("a deleted file has no Actual lines")
|
||||
func deletedFile() {
|
||||
#expect(DiffViews.actual(LineDiff.build(original: "a\nb", updated: "", status: .deleted)).isEmpty)
|
||||
}
|
||||
|
||||
@Test("the removed line only leaves one rule")
|
||||
func onlyOneRule() {
|
||||
let lines = actual("keep\ngone\ntail\n", "keep\ntail\n")
|
||||
#expect(lines.filter { $0.gap != nil }.count == 1)
|
||||
#expect(lines.filter { $0.style != nil }.isEmpty)
|
||||
}
|
||||
|
||||
@Test("a plain file is numbered from 1 and drops one trailing newline")
|
||||
func plain() {
|
||||
#expect(DiffViews.plain("x\ny\n") == [ViewLine(no: 1, text: "x"), ViewLine(no: 2, text: "y")])
|
||||
#expect(DiffViews.plain("") == [ViewLine(no: 1, text: "")])
|
||||
}
|
||||
}
|
||||
|
||||
@Suite struct DiffViewsLineEndingTests {
|
||||
@Test("a CRLF file keeps one line per CRLF in the plain view")
|
||||
func crlfPlain() {
|
||||
#expect(DiffViews.plain("a\r\nb\r\n").map(\.text) == ["a\r", "b\r"])
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,163 @@
|
||||
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)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
import HelderCore
|
||||
import Testing
|
||||
|
||||
@Suite struct ViewModeTests {
|
||||
@Test func rawValuesMatchTheOldSessionStrings() {
|
||||
#expect(ViewMode.actual.rawValue == "updated")
|
||||
#expect(ViewMode(DefaultDiffMode.updated) == .actual)
|
||||
#expect(ViewMode(DefaultDiffMode.diff) == .diff)
|
||||
}
|
||||
|
||||
@Test func anOriginalModeIsNotAViewMode() {
|
||||
#expect(ViewMode(rawValue: "original") == nil)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user