improves plak
This commit is contained in:
@@ -0,0 +1,464 @@
|
||||
import AppKit
|
||||
import PlakCore
|
||||
@testable import PlakUI
|
||||
import Testing
|
||||
|
||||
/// Code blocks and tables: page file, paste and copy, and their editors.
|
||||
@MainActor
|
||||
@Suite struct ObjectTests {
|
||||
private func pasteboard(_ text: String) -> NSPasteboard {
|
||||
let board = NSPasteboard(name: NSPasteboard.Name("plak-tests-\(UUID().uuidString)"))
|
||||
board.clearContents()
|
||||
board.setString(text, forType: .string)
|
||||
return board
|
||||
}
|
||||
|
||||
private func objects(_ page: TestPage) -> [(location: Int, attachment: NSTextAttachment)] {
|
||||
guard let storage = page.textView.textStorage else { return [] }
|
||||
var found: [(Int, NSTextAttachment)] = []
|
||||
storage.enumerateAttribute(.attachment, in: NSRange(location: 0, length: storage.length)) { value, range, _ in
|
||||
if let attachment = value as? NSTextAttachment { found.append((range.location, attachment)) }
|
||||
}
|
||||
return found
|
||||
}
|
||||
|
||||
private func key(_ code: UInt16, _ characters: String, _ flags: NSEvent.ModifierFlags) throws -> NSEvent {
|
||||
try #require(NSEvent.keyEvent(with: .keyDown, location: .zero, modifierFlags: flags, timestamp: 0, windowNumber: 0, context: nil,
|
||||
characters: characters, charactersIgnoringModifiers: characters, isARepeat: false, keyCode: code))
|
||||
}
|
||||
|
||||
private func grid(_ page: TestPage, at location: Int) throws -> TableGridView {
|
||||
page.textView.openEditor(at: location)
|
||||
return try #require(page.textView.editor as? TableGridView)
|
||||
}
|
||||
|
||||
private func codeEditor(_ page: TestPage, at location: Int) throws -> CodeEditorView {
|
||||
page.textView.openEditor(at: location)
|
||||
return try #require(page.textView.editor as? CodeEditorView)
|
||||
}
|
||||
|
||||
// MARK: Page file
|
||||
|
||||
@Test("a page file with a code block and a table loads and saves back byte for byte, dates in place")
|
||||
func loadAndSave() throws {
|
||||
let text = """
|
||||
\(dateLine(2026, 10, 16, 8, 55))
|
||||
```php
|
||||
<?php
|
||||
|
||||
echo 1;
|
||||
```
|
||||
under the code
|
||||
|
||||
\(dateLine(2025, 3, 3, 14, 2))
|
||||
```csv
|
||||
a,b
|
||||
"x, y",2
|
||||
```
|
||||
|
||||
"""
|
||||
let page = try TestPage(text)
|
||||
#expect(page.fileText == text)
|
||||
#expect(page.textView.string == "\u{FFFC}\nunder the code\n\n\u{FFFC}")
|
||||
let found = objects(page).map(\.attachment)
|
||||
let code = try #require(found.first as? CodeAttachment)
|
||||
let table = try #require(found.last as? TableAttachment)
|
||||
#expect(code.kind == .php && code.body == "<?php\n\necho 1;")
|
||||
#expect(table.table.rows == [["a", "b"], ["x, y", "2"]])
|
||||
}
|
||||
|
||||
@Test("a code block spans the column and a table is never wider than it")
|
||||
func sizes() throws {
|
||||
let wide = (1...30).map { "column\($0)" }.joined(separator: ",")
|
||||
let page = try TestPage("```json\n{\"a\": 1}\n```\n\n```csv\n\(wide)\n1,2\n```\n")
|
||||
let column = try #require(page.textView.textContainer).size.width
|
||||
let code = page.textView.objectRect(at: 0)
|
||||
#expect(code.width == floor(column) && code.height > page.textView.pageText.lineHeight)
|
||||
let table = page.textView.objectRect(at: 3)
|
||||
#expect(table.width <= column && table.width >= column - 1)
|
||||
}
|
||||
|
||||
// MARK: Paste and copy
|
||||
|
||||
@Test("⌘V of JSON on one line puts one pretty-printed code block at the caret, on its own line")
|
||||
func pasteJSON() throws {
|
||||
let page = try TestPage("one two\n")
|
||||
let board = pasteboard(#"{"b":1,"a":2}"#)
|
||||
defer { board.releaseGlobally() }
|
||||
page.caret(3)
|
||||
page.textView.paste(from: board)
|
||||
#expect(page.textView.string == "one\n\u{FFFC}\n two")
|
||||
#expect(page.fileText.contains("one\n```json\n{\n \"b\": 1,\n \"a\": 2\n}\n```\n two\n"))
|
||||
}
|
||||
|
||||
@Test("⌘V of PHP, XML and CSV gives a code block, a code block and a table")
|
||||
func pasteKinds() throws {
|
||||
let page = try TestPage("start\n")
|
||||
for text in ["<?php echo 1;", "<a><b/></a>", "x;y\n1;2"] {
|
||||
let board = pasteboard(text)
|
||||
defer { board.releaseGlobally() }
|
||||
page.caretAtEnd()
|
||||
page.type("\n\n")
|
||||
page.textView.paste(from: board)
|
||||
}
|
||||
let found = objects(page).map(\.attachment)
|
||||
#expect(found.count == 3)
|
||||
#expect((found[0] as? CodeAttachment)?.kind == .php)
|
||||
#expect((found[1] as? CodeAttachment)?.kind == .xml)
|
||||
#expect((found[2] as? TableAttachment)?.table.delimiter == ";")
|
||||
#expect(page.fileText.hasSuffix("```csv\nx;y\n1;2\n```\n"))
|
||||
}
|
||||
|
||||
@Test("Paste as Plain Text keeps JSON, fences and image lines as text; a sentence with commas stays text")
|
||||
func plainPaste() throws {
|
||||
let page = try TestPage("a\n", images: ["a.png": NSSize(width: 4, height: 4)])
|
||||
let json = pasteboard(#"{"a":1}"#)
|
||||
defer { json.releaseGlobally() }
|
||||
page.textView.pasteboard = json
|
||||
page.caretAtEnd()
|
||||
page.textView.pasteAsPlainText(nil)
|
||||
let sentence = pasteboard("Hello, world, and more")
|
||||
defer { sentence.releaseGlobally() }
|
||||
page.textView.paste(from: sentence)
|
||||
let fenced = pasteboard("\n```php\n<?php\n```\n")
|
||||
defer { fenced.releaseGlobally() }
|
||||
page.textView.pasteboard = fenced
|
||||
page.textView.pasteAsPlainText(nil)
|
||||
#expect(objects(page).isEmpty)
|
||||
#expect(page.textView.string == "a{\"a\":1}Hello, world, and more\n```php\n<?php\n```\n")
|
||||
}
|
||||
|
||||
@Test("copy of a selection with an object gives its fenced text; pasting that text recreates the object")
|
||||
func copyAndPaste() throws {
|
||||
let page = try TestPage("a\n```xml\n<r/>\n```\nb\n")
|
||||
let board = NSPasteboard(name: NSPasteboard.Name("plak-tests-\(UUID().uuidString)"))
|
||||
defer { board.releaseGlobally() }
|
||||
page.textView.setSelectedRange(NSRange(location: 0, length: page.textView.length))
|
||||
#expect(page.textView.writeSelection(to: board, types: [.string]))
|
||||
#expect(board.string(forType: .string) == "a\n```xml\n<r/>\n```\nb")
|
||||
|
||||
page.caretAtEnd()
|
||||
page.type("\n\n")
|
||||
page.textView.paste(from: board)
|
||||
let found = objects(page).compactMap { $0.attachment as? CodeAttachment }
|
||||
#expect(found.map(\.body) == ["<r/>", "<r/>"])
|
||||
#expect(page.fileText.hasSuffix("\na\n```xml\n<r/>\n```\nb\n"))
|
||||
}
|
||||
|
||||
// MARK: Code editor
|
||||
|
||||
@Test("a click on a code block opens its editor exactly over the block, with the keys")
|
||||
func clickOpensEditor() throws {
|
||||
let page = try TestPage("a\n```php\n<?php echo 1;\n```\nb\n")
|
||||
let rect = page.textView.objectRect(at: 2)
|
||||
let point = page.textView.convert(NSPoint(x: rect.midX, y: rect.midY), to: nil)
|
||||
let event = try #require(NSEvent.mouseEvent(with: .leftMouseDown, location: point, modifierFlags: [], timestamp: 0,
|
||||
windowNumber: page.textView.window?.windowNumber ?? 0, context: nil,
|
||||
eventNumber: 0, clickCount: 1, pressure: 1))
|
||||
page.textView.mouseDown(with: event)
|
||||
let editor = try #require(page.textView.editor as? CodeEditorView)
|
||||
#expect(editor.frame == page.textView.objectRect(at: 2))
|
||||
#expect(editor.frame.size == rect.size)
|
||||
#expect(editor.window?.firstResponder === editor)
|
||||
#expect(editor.string == "<?php echo 1;")
|
||||
}
|
||||
|
||||
@Test("a code edit goes back as one change: ⌘Z on the page undoes all of it")
|
||||
func codeEditAndUndo() throws {
|
||||
let text = "a\n\n```php\n<?php echo 1;\n```\n\nb\n"
|
||||
let page = try TestPage(text)
|
||||
let editor = try codeEditor(page, at: 3)
|
||||
editor.setSelectedRange(NSRange(location: (editor.string as NSString).length, length: 0))
|
||||
editor.insertText(" // one", replacementRange: editor.selectedRange())
|
||||
editor.insertTab(nil)
|
||||
editor.insertText("two", replacementRange: editor.selectedRange())
|
||||
#expect(page.fileText.contains("<?php echo 1; // one two\n"))
|
||||
editor.cancelOperation(nil)
|
||||
#expect(page.textView.editor == nil)
|
||||
#expect(page.fileText == "a\n\n```php\n<?php echo 1; // one two\n```\n\nb\n")
|
||||
#expect(page.textView.selectedRange() == NSRange(location: 4, length: 0))
|
||||
page.textView.undoManager?.undo()
|
||||
#expect(page.fileText == text)
|
||||
}
|
||||
|
||||
@Test("a code block whose body becomes empty is removed with its line")
|
||||
func emptyCodeRemoved() throws {
|
||||
let page = try TestPage("a\n```json\n{}\n```\nb\n")
|
||||
let editor = try codeEditor(page, at: 2)
|
||||
editor.setSelectedRange(NSRange(location: 0, length: (editor.string as NSString).length))
|
||||
editor.deleteBackward(nil)
|
||||
editor.cancelOperation(nil)
|
||||
#expect(page.fileText == "a\nb\n")
|
||||
#expect(objects(page).isEmpty)
|
||||
}
|
||||
|
||||
@Test("↑ on the first line and ↓ on the last line leave the code editor to the text above and below")
|
||||
func codeArrowsLeave() throws {
|
||||
let page = try TestPage("above\n```php\none\ntwo\n```\nbelow\n")
|
||||
var editor = try codeEditor(page, at: 6)
|
||||
editor.setSelectedRange(NSRange(location: 5, length: 0))
|
||||
editor.moveUp(nil)
|
||||
#expect(page.textView.editor === editor)
|
||||
editor.moveUp(nil)
|
||||
#expect(page.textView.editor == nil)
|
||||
#expect(page.textView.selectedRange() == NSRange(location: 5, length: 0))
|
||||
|
||||
editor = try codeEditor(page, at: 6)
|
||||
editor.setSelectedRange(NSRange(location: 1, length: 0))
|
||||
editor.moveDown(nil)
|
||||
editor.moveDown(nil)
|
||||
#expect(page.textView.editor == nil)
|
||||
#expect(page.textView.selectedRange() == NSRange(location: 8, length: 0))
|
||||
}
|
||||
|
||||
@Test("Esc in the code editor, the grid or a cell edit never hides Plak; Esc after that does")
|
||||
func escInEditors() throws {
|
||||
let page = try TestPage("```php\n<?php\n```\n\n```csv\na,b\nc,d\n```\n")
|
||||
var hides = 0
|
||||
page.controller.onHide = { hides += 1 }
|
||||
let editor = try codeEditor(page, at: 0)
|
||||
editor.cancelOperation(nil)
|
||||
#expect(page.textView.editor == nil && hides == 0)
|
||||
|
||||
let table = try grid(page, at: 3)
|
||||
table.beginCellEdit(keepingValue: true)
|
||||
try #require(table.cellEditor).cancelOperation(nil)
|
||||
#expect(table.cellEditor == nil && page.textView.editor === table && hides == 0)
|
||||
table.cancelOperation(nil)
|
||||
#expect(page.textView.editor == nil && hides == 0)
|
||||
#expect(page.textView.window?.firstResponder === page.textView)
|
||||
|
||||
page.textView.cancelOperation(nil)
|
||||
#expect(hides == 1)
|
||||
}
|
||||
|
||||
@Test("hiding Plak puts an open edit in the page first, and a save while editing holds the edit")
|
||||
func hideCommits() throws {
|
||||
let page = try TestPage("```php\n<?php\n```\n")
|
||||
var hides = 0
|
||||
page.controller.onHide = { hides += 1 }
|
||||
let editor = try codeEditor(page, at: 0)
|
||||
editor.setSelectedRange(NSRange(location: 5, length: 0))
|
||||
editor.insertText(" echo 2;", replacementRange: editor.selectedRange())
|
||||
#expect(page.fileText == "```php\n<?php echo 2;\n```\n")
|
||||
#expect(page.textView.editor != nil)
|
||||
let window = try #require(page.controller.window)
|
||||
#expect(!page.controller.windowShouldClose(window))
|
||||
#expect(hides == 1 && page.textView.editor == nil)
|
||||
#expect((objects(page).first?.attachment as? CodeAttachment)?.body == "<?php echo 2;")
|
||||
}
|
||||
|
||||
@Test("a bigger text size makes code bigger too, also in an open editor")
|
||||
func textSize() throws {
|
||||
let page = try TestPage("```php\n<?php\necho 1;\n```\n")
|
||||
let before = page.textView.objectRect(at: 0).height
|
||||
let editor = try codeEditor(page, at: 0)
|
||||
page.page.makeTextBigger(nil)
|
||||
#expect(page.textView.pageText.codeFont.pointSize == round((TextSize.standard + TextSize.step) * PageMetrics.codeScale))
|
||||
#expect(editor.textStorage?.attribute(.font, at: 0, effectiveRange: nil) as? NSFont == page.textView.pageText.codeFont)
|
||||
#expect(editor.frame.height > before)
|
||||
editor.cancelOperation(nil)
|
||||
#expect(page.textView.objectRect(at: 0).height > before)
|
||||
}
|
||||
|
||||
@Test("a CR or CRLF typed or pasted into code is saved as LF, so the fence stays whole")
|
||||
func codeLineEndings() throws {
|
||||
let page = try TestPage("```php\n<?php\n```\nb\n")
|
||||
let editor = try codeEditor(page, at: 0)
|
||||
editor.setSelectedRange(NSRange(location: 5, length: 0))
|
||||
editor.insertText("\r\n```\recho 1;", replacementRange: editor.selectedRange())
|
||||
editor.cancelOperation(nil)
|
||||
#expect(page.fileText == "````php\n<?php\n```\necho 1;\n````\nb\n")
|
||||
}
|
||||
|
||||
@Test("leaving the last object of the page with Esc or ↓ puts the caret on a new line of its own")
|
||||
func exitAtEnd() throws {
|
||||
let page = try TestPage("a\n```json\n{}\n```\n")
|
||||
let editor = try codeEditor(page, at: 2)
|
||||
editor.cancelOperation(nil)
|
||||
#expect(page.textView.string == "a\n\u{FFFC}\n")
|
||||
#expect(page.textView.selectedRange() == NSRange(location: 4, length: 0))
|
||||
let again = try codeEditor(page, at: 2)
|
||||
again.moveDown(nil)
|
||||
#expect(page.textView.string == "a\n\u{FFFC}\n")
|
||||
#expect(page.textView.selectedRange() == NSRange(location: 4, length: 0))
|
||||
}
|
||||
|
||||
@Test("a save while editing holds the edit without a new object; an edit that empties the block drops its line")
|
||||
func draftSave() throws {
|
||||
let page = try TestPage("a\n```php\n<?php\n```\nb\n")
|
||||
let before = try #require(objects(page).first?.attachment)
|
||||
let editor = try codeEditor(page, at: 2)
|
||||
editor.setSelectedRange(NSRange(location: 5, length: 0))
|
||||
editor.insertText(" echo 2;", replacementRange: editor.selectedRange())
|
||||
#expect(page.fileText == "a\n```php\n<?php echo 2;\n```\nb\n")
|
||||
#expect(objects(page).first?.attachment === before)
|
||||
editor.setSelectedRange(NSRange(location: 0, length: (editor.string as NSString).length))
|
||||
editor.deleteBackward(nil)
|
||||
#expect(page.fileText == "a\nb\n")
|
||||
}
|
||||
|
||||
@Test("the editor follows its object when the text above it changes, and puts the edit back there")
|
||||
func editorFollowsObject() throws {
|
||||
let page = try TestPage("a\n```php\n<?php\n```\nb\n")
|
||||
let editor = try codeEditor(page, at: 2)
|
||||
let storage = try #require(page.textView.textStorage)
|
||||
storage.replaceCharacters(in: NSRange(location: 0, length: 0), with: NSAttributedString(string: "top\n", attributes: page.textView.pageText.attributes))
|
||||
page.textView.positionEditor()
|
||||
#expect(page.textView.editorLocation == 6)
|
||||
#expect(editor.frame.origin == page.textView.objectRect(at: 6).origin)
|
||||
editor.setSelectedRange(NSRange(location: 5, length: 0))
|
||||
editor.insertText(" echo 3;", replacementRange: editor.selectedRange())
|
||||
editor.cancelOperation(nil)
|
||||
#expect(page.fileText == "top\na\n```php\n<?php echo 3;\n```\nb\n")
|
||||
}
|
||||
|
||||
@Test("a key command the code editor or the grid does not know never reaches the page")
|
||||
func unknownCommandsStay() throws {
|
||||
let page = try TestPage("before\n```csv\na,b\n```\nafter\n")
|
||||
let table = try grid(page, at: 7)
|
||||
for selector in [#selector(NSResponder.deleteWordBackward(_:)), #selector(NSResponder.deleteToEndOfLine(_:)),
|
||||
#selector(NSResponder.moveToEndOfDocument(_:)), #selector(NSResponder.insertNewlineIgnoringFieldEditor(_:))] {
|
||||
table.doCommand(by: selector)
|
||||
}
|
||||
table.pasteAsPlainText(nil)
|
||||
#expect(page.textView.string == "before\n\u{FFFC}\nafter")
|
||||
#expect(page.textView.editor === table)
|
||||
}
|
||||
|
||||
@Test("drawing with an empty clip, a clip beside the objects or a null clip never traps")
|
||||
func emptyClips() throws {
|
||||
let page = try TestPage("```csv\na,b\nc,d\n```\n\n```php\n<?php\n```\n")
|
||||
let textView = page.textView
|
||||
let rep = try #require(textView.bitmapImageRepForCachingDisplay(in: textView.bounds))
|
||||
let context = try #require(NSGraphicsContext(bitmapImageRep: rep))
|
||||
NSGraphicsContext.saveGraphicsState()
|
||||
defer { NSGraphicsContext.restoreGraphicsState() }
|
||||
NSGraphicsContext.current = context
|
||||
let object = textView.objectRect(at: 0)
|
||||
for clip in [NSRect.zero, NSRect(x: 0, y: textView.bounds.maxY - 10, width: 10, height: 10)] {
|
||||
context.saveGraphicsState()
|
||||
clip.clip()
|
||||
#expect(drawingClip(within: object) == nil)
|
||||
textView.draw(textView.bounds)
|
||||
context.restoreGraphicsState()
|
||||
}
|
||||
let table = try #require(objects(page).first?.attachment as? TableAttachment)
|
||||
let font = textView.pageText.codeFont
|
||||
TableLayout(table.table, font: font).draw(table.table, at: .zero, clip: .null, scale: 2)
|
||||
TableLayout(table.table, font: font).draw(table.table, at: .zero, clip: .zero, scale: 2)
|
||||
#expect(TableLayout(table.table, font: font).position(at: NSPoint(x: CGFloat.nan, y: CGFloat.infinity)) == nil)
|
||||
let code = CodeLayout()
|
||||
code.set("<?php", kind: .php, font: font)
|
||||
code.draw(in: NSRect(x: 0, y: 0, width: 300, height: 100), clip: .null)
|
||||
#expect(!NSRect.null.isFiniteArea)
|
||||
}
|
||||
|
||||
// MARK: Table grid
|
||||
|
||||
@Test("typing in a cell saves while the cell is still open")
|
||||
func cellTypingSaves() throws {
|
||||
let page = try TestPage("```csv\na,b\nc,d\n```\nend\n")
|
||||
var changes = 0
|
||||
page.textView.onEditorChange = { changes += 1 }
|
||||
let table = try grid(page, at: 0)
|
||||
table.insertText("x")
|
||||
try #require(table.cellEditor).insertText("y", replacementRange: try #require(table.cellEditor).selectedRange())
|
||||
#expect(changes >= 2)
|
||||
#expect(page.fileText == "```csv\nxy,b\nc,d\n```\nend\n")
|
||||
}
|
||||
|
||||
@Test("the arrows and Tab move the selected cell; ↑ on the first row leaves to the text above")
|
||||
func gridMoves() throws {
|
||||
let page = try TestPage("above\n```csv\na,b\nc,d\n```\n")
|
||||
let table = try grid(page, at: 6)
|
||||
#expect(table.selected == TablePosition(row: 0, column: 0))
|
||||
table.moveRight(nil)
|
||||
table.moveRight(nil)
|
||||
#expect(table.selected == TablePosition(row: 0, column: 1))
|
||||
table.moveDown(nil)
|
||||
table.insertBacktab(nil)
|
||||
#expect(table.selected == TablePosition(row: 1, column: 0))
|
||||
table.insertTab(nil)
|
||||
table.moveLeft(nil)
|
||||
table.moveUp(nil)
|
||||
#expect(table.selected == TablePosition(row: 0, column: 0))
|
||||
table.moveUp(nil)
|
||||
#expect(page.textView.editor == nil)
|
||||
#expect(page.textView.selectedRange() == NSRange(location: 5, length: 0))
|
||||
#expect(page.fileText == "above\n```csv\na,b\nc,d\n```\n")
|
||||
}
|
||||
|
||||
@Test("typing a character replaces the value; Enter or F2 edits it keeping the value; Esc cancels the cell edit")
|
||||
func gridTyping() throws {
|
||||
let page = try TestPage("```csv\nab,cd\nef,gh\n```\n")
|
||||
let table = try grid(page, at: 0)
|
||||
table.insertText("x")
|
||||
#expect(table.cellEditor?.string == "x")
|
||||
try #require(table.cellEditor).insertNewline(nil)
|
||||
#expect(table.table[0, 0] == "x" && table.selected == TablePosition(row: 1, column: 0))
|
||||
|
||||
table.insertNewline(nil)
|
||||
let editor = try #require(table.cellEditor)
|
||||
#expect(editor.string == "ef" && editor.selectedRange() == NSRange(location: 2, length: 0))
|
||||
editor.insertText("!", replacementRange: editor.selectedRange())
|
||||
editor.cancelOperation(nil)
|
||||
#expect(table.table[1, 0] == "ef" && table.cellEditor == nil)
|
||||
|
||||
table.keyDown(with: try key(120, String(UnicodeScalar(UInt16(NSF2FunctionKey))!), [.function]))
|
||||
#expect(table.cellEditor?.string == "ef")
|
||||
try #require(table.cellEditor).insertTab(nil)
|
||||
#expect(table.selected == TablePosition(row: 1, column: 1))
|
||||
table.cancelOperation(nil)
|
||||
#expect(page.fileText == "```csv\nx,cd\nef,gh\n```\n\n")
|
||||
}
|
||||
|
||||
@Test("Enter on the last row adds a row, Tab on the last column adds a column")
|
||||
func gridGrows() throws {
|
||||
let page = try TestPage("```csv\na,b\nc,d\n```\n")
|
||||
let table = try grid(page, at: 0)
|
||||
let height = table.view.frame.height
|
||||
table.select(TablePosition(row: 1, column: 0))
|
||||
table.insertText("z")
|
||||
try #require(table.cellEditor).insertNewline(nil)
|
||||
#expect(table.table.rows.count == 3 && table.selected == TablePosition(row: 2, column: 0))
|
||||
#expect(table.view.frame.height > height)
|
||||
table.select(TablePosition(row: 0, column: 1))
|
||||
table.insertTab(nil)
|
||||
#expect(table.table.columnCount == 3 && table.selected == TablePosition(row: 0, column: 2))
|
||||
table.cancelOperation(nil)
|
||||
#expect(page.fileText == "```csv\na,b,\nz,d,\n,,\n```\n\n")
|
||||
}
|
||||
|
||||
@Test("⌘⌫ deletes the row, ⌥⌘⌫ the column, Backspace clears the cell; with no cells left the table goes")
|
||||
func gridDeletes() throws {
|
||||
let page = try TestPage("a\n```csv\n1,2,3\n4,5,6\n```\nb\n")
|
||||
let table = try grid(page, at: 2)
|
||||
table.select(TablePosition(row: 1, column: 1))
|
||||
table.keyDown(with: try key(51, "\u{7F}", [.command]))
|
||||
#expect(table.table.rows == [["1", "2", "3"]] && table.selected == TablePosition(row: 0, column: 1))
|
||||
table.keyDown(with: try key(51, "\u{7F}", [.command, .option]))
|
||||
#expect(table.table.rows == [["1", "3"]])
|
||||
table.deleteBackward(nil)
|
||||
#expect(table.table.rows == [["1", ""]])
|
||||
table.cancelOperation(nil)
|
||||
#expect(page.fileText == "a\n```csv\n1,\n```\nb\n")
|
||||
|
||||
let again = try grid(page, at: 2)
|
||||
again.keyDown(with: try key(51, "\u{7F}", [.command]))
|
||||
#expect(page.textView.editor == nil)
|
||||
#expect(page.fileText == "a\nb\n")
|
||||
}
|
||||
|
||||
@Test("a click on a table cell opens grid mode with that cell selected")
|
||||
func clickSelectsCell() throws {
|
||||
let page = try TestPage("```csv\nfirst,second\nthird,fourth\n```\n")
|
||||
let rect = page.textView.objectRect(at: 0)
|
||||
let layout = TableLayout(CSVTable(text: "first,second\nthird,fourth"), font: page.textView.pageText.codeFont)
|
||||
let cell = layout.rect(TablePosition(row: 1, column: 1))
|
||||
page.textView.openEditor(at: 0, point: NSPoint(x: rect.minX + cell.midX, y: rect.minY + cell.midY))
|
||||
let table = try #require(page.textView.editor as? TableGridView)
|
||||
#expect(table.selected == TablePosition(row: 1, column: 1))
|
||||
#expect(table.view.frame.origin == rect.origin)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user