import AppKit import HelderCore @testable import HelderUI import Testing @MainActor @Suite struct CodeEditorShowDiffTests { /// Line 2 is a blank added line, line 3 a changed one; lines 1 and 4 are unchanged. private func open(offersShowDiff: Bool = true) -> (CodeEditorController, NSWindow) { let controller = CodeEditorController() let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 700, height: 400), styleMask: [.titled], backing: .buffered, defer: false) window.contentView = controller.scrollView let marks = [2: CodeLineMark(tint: true, tone: .added), 3: CodeLineMark(tint: true, tone: .added)] controller.apply(CodeEditorConfiguration(documentID: "a.php", path: "a.php", text: "one\n\nthree\nfour", revision: 1, language: nil, isEditable: true, wrap: false, tabSize: 4, marksKey: "m", makeMarks: { marks }, offersShowDiff: offersShowDiff, knownSymbols: [], symbolsVersion: 0, flash: nil, focusRequest: 0, selectAllRequest: 0, fontScale: 1, isShown: true)) window.displayIfNeeded() return (controller, window) } /// A window point inside the first row of `line`, `x` points from the text view's left edge. private func point(line: Int, x: CGFloat, in controller: CodeEditorController) -> NSPoint { var rect: NSRect? controller.forEachRow(in: controller.textView.bounds) { row in if row.line == line, rect == nil { rect = row.rect } } let row = rect ?? .zero return controller.textView.convert(NSPoint(x: x, y: row.midY), to: nil) } @Test func aBlankAddedLineOffersShowDiff() { let (controller, window) = open() #expect(controller.showDiffLine(atWindowPoint: point(line: 2, x: 40, in: controller)) == 2) withExtendedLifetime(window) {} } @Test func theSpaceAfterTheLastGlyphCounts() { let (controller, window) = open() #expect(controller.showDiffLine(atWindowPoint: point(line: 3, x: 500, in: controller)) == 3) withExtendedLifetime(window) {} } @Test func anUnchangedLineOffersNothing() { let (controller, window) = open() #expect(controller.showDiffLine(atWindowPoint: point(line: 1, x: 10, in: controller)) == nil) #expect(controller.showDiffLine(atWindowPoint: point(line: 4, x: 500, in: controller)) == nil) withExtendedLifetime(window) {} } @Test func aFileWithoutShowDiffOffersNothingOnAMarkedLine() { let (controller, window) = open(offersShowDiff: false) #expect(controller.showDiffLine(atWindowPoint: point(line: 3, x: 10, in: controller)) == nil) withExtendedLifetime(window) {} } }