285 lines
12 KiB
Swift
285 lines
12 KiB
Swift
import CoreGraphics
|
|
import Foundation
|
|
import HelderCore
|
|
@testable import HelderUI
|
|
import Testing
|
|
|
|
@Suite struct EditorPresentationTests {
|
|
@Test func anUnchangedFileShowsItsEditableCodeViewAndNoButtons() {
|
|
let view = EditorPresentation(path: "a.php", requested: nil, diff: nil)
|
|
#expect(view.mode == .code)
|
|
#expect(view.barButtons.isEmpty)
|
|
#expect(view.isEditable)
|
|
}
|
|
|
|
@Test func aChangedFileOffersOnlyTheDiffButton() {
|
|
let view = EditorPresentation(path: "a.php", requested: nil, diff: makeDiff(original: "a", updated: "b"))
|
|
#expect(view.mode == .actual)
|
|
#expect(view.barButtons == [.diff])
|
|
#expect(view.opensDiffOnClick)
|
|
}
|
|
|
|
@Test func unchangedMarkdownOffersOnlyPreview() {
|
|
let view = EditorPresentation(path: "README.md", requested: nil, diff: nil)
|
|
#expect(view.barButtons == [.preview])
|
|
}
|
|
|
|
@Test func changedMarkdownOffersPreviewThenDiff() {
|
|
let view = EditorPresentation(path: "README.md", requested: .preview, diff: makeDiff(original: "a", updated: "b"))
|
|
#expect(view.barButtons == [.preview, .diff])
|
|
#expect(view.mode == .preview)
|
|
#expect(!view.isEditable)
|
|
}
|
|
|
|
@Test func aFileOverTheDiffCapStillShowsItsLiveText() {
|
|
var diff = makeDiff(original: "", updated: "", added: 0)
|
|
diff.isTooBig = true
|
|
let view = EditorPresentation(path: "dump.sql", requested: .actual, diff: diff)
|
|
#expect(view.placeholder == nil)
|
|
#expect(view.isEditable)
|
|
}
|
|
|
|
@Test func aDiffRequestOnAnUnchangedFileFallsBackToCode() {
|
|
let view = EditorPresentation(path: "a.php", requested: .diff, diff: nil)
|
|
#expect(view.mode == .code)
|
|
}
|
|
|
|
@Test func aCodeModeOnAChangedFileResolvesToActual() {
|
|
let view = EditorPresentation(path: "a.php", requested: .code, diff: makeDiff(original: "a", updated: "b"))
|
|
#expect(view.mode == .actual)
|
|
}
|
|
|
|
@Test func theDiffOverlayLeavesActualInThePaneBehindIt() {
|
|
let view = EditorPresentation(path: "a.php", requested: .diff, diff: makeDiff(original: "a", updated: "b"))
|
|
#expect(view.mode == .diff)
|
|
#expect(view.paneMode == .actual)
|
|
}
|
|
|
|
@Test func anAddedFileHasNoClickToDiff() {
|
|
let diff = makeDiff(original: "", updated: "b", isAddedFile: true)
|
|
let actual = EditorPresentation(path: "a.php", requested: .actual, diff: diff)
|
|
#expect(actual.placeholder == nil)
|
|
#expect(!actual.opensDiffOnClick)
|
|
#expect(actual.status == .added)
|
|
}
|
|
|
|
@Test func aDeletedFileHasNoUpdatedVersion() {
|
|
let diff = makeDiff(original: "a", updated: "", isDeletedFile: true)
|
|
let view = EditorPresentation(path: "a.php", requested: .actual, diff: diff)
|
|
#expect(view.placeholder == .noUpdated)
|
|
#expect(!view.isEditable)
|
|
}
|
|
|
|
@Test func aBinaryFileGetsAPlaceholderAndNoButtons() {
|
|
let view = EditorPresentation(path: "font.woff2", requested: nil, diff: makeDiff(original: "", updated: ""))
|
|
#expect(view.placeholder == .cannotDisplay)
|
|
#expect(view.barButtons.isEmpty)
|
|
#expect(!view.hasDiff)
|
|
}
|
|
|
|
@Test func anImageIsNeitherTextNorDiffed() {
|
|
let view = EditorPresentation(path: "logo.png", requested: nil, diff: makeDiff(original: "", updated: ""))
|
|
#expect(view.kind == .image)
|
|
#expect(view.barButtons.isEmpty)
|
|
#expect(!view.isEditable)
|
|
}
|
|
}
|
|
|
|
@Suite struct LineIndexTests {
|
|
@Test func linesAreFoundByUTF16Offset() {
|
|
let index = LineIndex("ab\ncd\n")
|
|
#expect(index.lineCount == 3)
|
|
#expect(index.line(containing: 0) == 1)
|
|
#expect(index.line(containing: 2) == 1)
|
|
#expect(index.line(containing: 3) == 2)
|
|
#expect(index.line(containing: 6) == 3)
|
|
#expect(index.column(of: 4) == 2)
|
|
#expect(index.start(ofLine: 2) == 3)
|
|
}
|
|
|
|
@Test func anEmptyTextIsOneEmptyLine() {
|
|
let index = LineIndex("")
|
|
#expect(index.lineCount == 1)
|
|
#expect(index.line(containing: 0) == 1)
|
|
}
|
|
|
|
@Test func wrappedRowsShowTheNumberOnlyOnTheirFirstRow() {
|
|
// Line 1 folds into three rows (starting at 0, 10, 20); line 2 is one row at 31.
|
|
let index = LineIndex(String(repeating: "x", count: 30) + "\nshort")
|
|
#expect(GutterNumbers.labels(rowStarts: [0, 10, 20, 31], index: index) == [1, nil, nil, 2])
|
|
}
|
|
}
|
|
|
|
@Suite struct ChangeMarksTests {
|
|
@Test func actualTintsAddedLinesAndRulesTheEdgeOfAPureDeletion() {
|
|
let lines = [
|
|
ViewLine(no: 1, text: "a"),
|
|
ViewLine(no: 2, text: "b", style: .added),
|
|
ViewLine(no: 3, text: "c", gap: .above),
|
|
ViewLine(no: 4, text: "d", gap: .below),
|
|
]
|
|
let marks = ChangeMarks.actual(lines)
|
|
#expect(marks[1] == nil)
|
|
#expect(marks[2] == CodeLineMark(tint: true, tone: .added))
|
|
#expect(marks[3] == CodeLineMark(tint: false, tone: nil, gap: .above))
|
|
#expect(marks[4]?.gap == .below)
|
|
}
|
|
}
|
|
|
|
/// Marks and layout built from real Core diffs, end to end.
|
|
@Suite struct EditorRealDiffTests {
|
|
@Test func actualMarksComeFromTheRealDiff() {
|
|
let diff = LineDiff.build(original: "a\nb\nc\n", updated: "a\nc\n")
|
|
#expect(ChangeMarks.actual(DiffViews.actual(diff))[2]?.gap == .above)
|
|
}
|
|
}
|
|
|
|
@Suite struct DiffOverlayLayoutTests {
|
|
private func rows(_ count: Int) -> [SplitRow] {
|
|
(1...count).map { SplitRow(left: SideLine(no: $0, text: ""), right: SideLine(no: $0, text: "")) }
|
|
}
|
|
|
|
@Test func theClickedLineLandsInTheMiddleOfTheViewport() {
|
|
let index = DiffOverlayLayout.rowIndex(forUpdatedLine: 100, in: rows(200))!
|
|
let offset = DiffOverlayLayout.centredOffset(rowIndex: index, rowCount: 200, viewportHeight: 440)
|
|
let rowMid = 6 + CGFloat(index) * 22 + 11
|
|
#expect(rowMid - offset == 220)
|
|
}
|
|
|
|
@Test func aLineNearTheTopGoesAsNearTheMiddleAsTheFilePermits() {
|
|
#expect(DiffOverlayLayout.centredOffset(rowIndex: 1, rowCount: 200, viewportHeight: 440) == 0)
|
|
}
|
|
|
|
@Test func aLineNearTheEndStopsAtTheBottom() {
|
|
let offset = DiffOverlayLayout.centredOffset(rowIndex: 199, rowCount: 200, viewportHeight: 440)
|
|
#expect(abs(offset - CGFloat(12 + 200 * 22 - 440)) < 0.001)
|
|
}
|
|
|
|
@Test func fillerRowsOnTheUpdatedSideAreSkipped() {
|
|
let split = [
|
|
SplitRow(left: SideLine(no: 1, text: "a", mark: .removed), right: nil),
|
|
SplitRow(left: nil, right: SideLine(no: 1, text: "b", mark: .added)),
|
|
]
|
|
#expect(DiffOverlayLayout.rowIndex(forUpdatedLine: 1, in: split) == 1)
|
|
}
|
|
}
|
|
|
|
@Suite struct IndentationTests {
|
|
@Test func tabOnOneLineInsertsSpacesAtTheCaret() {
|
|
let edit = Indentation.edit("ab", selection: NSRange(location: 1, length: 0), tabSize: 4, outdent: false)
|
|
#expect(edit == TextEdit(range: NSRange(location: 1, length: 0), replacement: " ", selection: NSRange(location: 5, length: 0)))
|
|
}
|
|
|
|
@Test func tabOverSeveralLinesIndentsEveryTouchedLine() {
|
|
let edit = Indentation.edit("a\nb\nc", selection: NSRange(location: 0, length: 3), tabSize: 2, outdent: false)!
|
|
#expect(edit.replacement == " a\n b")
|
|
#expect(edit.range == NSRange(location: 0, length: 3))
|
|
}
|
|
|
|
@Test func aSelectionEndingAtColumnZeroLeavesThatLineAlone() {
|
|
let edit = Indentation.edit("a\nb\nc", selection: NSRange(location: 0, length: 4), tabSize: 2, outdent: false)!
|
|
#expect(edit.replacement == " a\n b")
|
|
}
|
|
|
|
@Test func shiftTabOutdentsTheCurrentLine() {
|
|
let edit = Indentation.edit("x\n ab", selection: NSRange(location: 7, length: 0), tabSize: 4, outdent: true)!
|
|
#expect(edit.replacement == "ab")
|
|
#expect(edit.selection == NSRange(location: 3, length: 0))
|
|
}
|
|
|
|
@Test func shiftTabOnAnUnindentedLineChangesNothing() {
|
|
#expect(Indentation.edit("ab", selection: NSRange(location: 1, length: 0), tabSize: 4, outdent: true) == nil)
|
|
}
|
|
}
|
|
|
|
@Suite struct EditorReferenceTests {
|
|
@Test func aSelectionNamesItsFirstAndLastLine() {
|
|
let index = LineIndex("a\nb\nc\nd\n")
|
|
let lines = EditorReference.lines(of: NSRange(location: 2, length: 3), in: index)
|
|
#expect(lines == 2...3)
|
|
#expect(EditorReference.make(path: "x.php", selectedLines: lines, clickedLine: 9).description == "x.php:2-3")
|
|
}
|
|
|
|
@Test func aSingleLineSelectionIsASingleLineReference() {
|
|
#expect(EditorReference.make(path: "x.php", selectedLines: 4...4, clickedLine: 9).description == "x.php:4")
|
|
}
|
|
|
|
@Test func withoutASelectionTheClickedLineIsTheReference() {
|
|
#expect(EditorReference.make(path: "x.php", selectedLines: nil, clickedLine: 42).description == "x.php:42")
|
|
}
|
|
}
|
|
|
|
@Suite struct IncrementalLineIndexTests {
|
|
private func edited(_ text: String, replacing range: NSRange, with insert: String) -> (LineIndex, String) {
|
|
var index = LineIndex(text)
|
|
let next = (text as NSString).replacingCharacters(in: range, with: insert)
|
|
let insertedLength = (insert as NSString).length
|
|
index.applyEdit(editedRange: NSRange(location: range.location, length: insertedLength),
|
|
delta: insertedLength - range.length, inserted: insert)
|
|
return (index, next)
|
|
}
|
|
|
|
@Test func typingANewlineMatchesAFullRescan() {
|
|
let (index, text) = edited("ab\ncd\nef", replacing: NSRange(location: 4, length: 0), with: "\n")
|
|
#expect(index == LineIndex(text))
|
|
}
|
|
|
|
@Test func deletingAcrossLinesMatchesAFullRescan() {
|
|
let (index, text) = edited("ab\ncd\nef\ngh", replacing: NSRange(location: 1, length: 6), with: "")
|
|
#expect(index == LineIndex(text))
|
|
}
|
|
|
|
@Test func pastingSeveralLinesMatchesAFullRescan() {
|
|
let (index, text) = edited("ab\ncd", replacing: NSRange(location: 3, length: 1), with: "x\ny\nz")
|
|
#expect(index == LineIndex(text))
|
|
}
|
|
}
|
|
|
|
@Suite struct HighlightDiffTests {
|
|
@Test func typingInsidePlainTextRepaintsOnlyTheEditedRange() {
|
|
let old = [Token(location: 0, length: 3, role: .keyword), Token(location: 10, length: 2, role: .number)]
|
|
let new = [Token(location: 0, length: 3, role: .keyword), Token(location: 11, length: 2, role: .number)]
|
|
let range = HighlightDiff.repaintRange(old: old, new: new, edited: NSRange(location: 5, length: 1), delta: 1, newLength: 14)
|
|
#expect(range == NSRange(location: 5, length: 1))
|
|
}
|
|
|
|
@Test func growingAStringRepaintsTheWholeToken() {
|
|
let old = [Token(location: 4, length: 4, role: .string)]
|
|
let new = [Token(location: 4, length: 5, role: .string)]
|
|
let range = HighlightDiff.repaintRange(old: old, new: new, edited: NSRange(location: 6, length: 1), delta: 1, newLength: 20)
|
|
#expect(range == NSRange(location: 4, length: 5))
|
|
}
|
|
|
|
@Test func openingABlockCommentRepaintsEverythingAfterIt() {
|
|
let old = [Token(location: 10, length: 3, role: .keyword), Token(location: 30, length: 3, role: .keyword)]
|
|
let new = [Token(location: 0, length: 40, role: .comment)]
|
|
let range = HighlightDiff.repaintRange(old: old, new: new, edited: NSRange(location: 0, length: 2), delta: 2, newLength: 40)
|
|
#expect(range == NSRange(location: 0, length: 40))
|
|
}
|
|
}
|
|
|
|
@Suite struct SymbolTargetsTests {
|
|
private let lookup = SymbolLookup(
|
|
name: "User",
|
|
definitions: [SymbolDef(name: "User", path: "app/User.php", line: 7, kind: "class", namespace: "App")],
|
|
references: [RefGroup(path: "a.php", hits: [ContentHit(line: 3, text: "", matchOffset: 0), ContentHit(line: 9, text: "", matchOffset: 0)])],
|
|
referenceCount: 2)
|
|
|
|
@Test func theKeyboardWalksDeclarationsThenEveryReferenceLine() {
|
|
#expect(SymbolTargets.flatten(lookup) == [
|
|
SymbolTarget(path: "app/User.php", line: 7), SymbolTarget(path: "a.php", line: 3), SymbolTarget(path: "a.php", line: 9),
|
|
])
|
|
}
|
|
|
|
@Test func oneDeclarationIsNamedInFull() {
|
|
#expect(SymbolTargets.title("User", lookup) == "App\\User")
|
|
}
|
|
|
|
@Test func severalDeclarationsFallBackToTheShortName() {
|
|
var two = lookup
|
|
two.definitions.append(SymbolDef(name: "User", path: "b/User.php", line: 1, kind: "class", namespace: "B"))
|
|
#expect(SymbolTargets.title("User", two) == "User")
|
|
#expect(SymbolTargets.title("User", nil) == "User")
|
|
}
|
|
}
|