Files
Lyrebird/LiveCompile/Tests/LyrebirdLiveCompileUITests/ScratchTextViewTests.swift
T
josh 8519cabe42 v1.4.2 release: live-coding editor — Run Selection, slot release, engine panel, console, manual completion
Additive conveniences to the macOS-only Live Scratch editor. No changes to
existing public API.

- Run Selection (⌘E): evaluate just the highlighted text, additively.
- Graceful slot release: context.release(slot:) / free(slot:).
- Engine panel: live oscilloscope (ScopeCapture) + CPU avg/peak + counts.
- Reset Session / Restart to Reclaim Memory.
- Always-open console panel (report messages + errors with line:col).
- Faster typing: debounced highlight, updateNSView fast-path, publish guards.
- Manual code completion (Esc / F5), no auto-trigger on `.`; completion
  replaces the partial word, not the receiver expression.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-21 11:09:30 -07:00

87 lines
3.5 KiB
Swift

// ScratchTextViewTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
#if os(macOS)
import Testing
import AppKit
@testable import LyrebirdLiveCompileUI
@Suite("ScratchTextView.rangeForUserCompletion")
@MainActor
struct ScratchTextViewTests {
private func makeTextView(_ text: String, caret: Int) -> ScratchTextView {
let tv: ScratchTextView = ScratchTextView(frame: .zero)
tv.string = text
tv.setSelectedRange(NSRange(location: caret, length: 0))
return tv
}
/// Right after a `.`, the completion range is EMPTY at the caret — so a
/// member completion inserts after the dot instead of replacing the
/// receiver expression (the `context.` → erases `context` bug).
@Test func afterDotRangeIsEmptyAtCaret() {
let tv: ScratchTextView = makeTextView("context.", caret: 8)
let range: NSRange = tv.rangeForUserCompletion
#expect(range == NSRange(location: 8, length: 0))
}
/// A dotted member expression: completing after the second dot must not
/// reach back over `context.engine`.
@Test func afterNestedDotRangeIsEmptyAtCaret() {
let text: String = "context.engine."
let tv: ScratchTextView = makeTextView(text, caret: text.utf16.count)
let range: NSRange = tv.rangeForUserCompletion
#expect(range == NSRange(location: text.utf16.count, length: 0))
}
/// Mid-identifier (no preceding dot): the range covers the run of identifier
/// characters before the caret, so the partial word gets replaced.
@Test func partialIdentifierCoversWordRange() {
let tv: ScratchTextView = makeTextView("OscSi", caret: 5)
let range: NSRange = tv.rangeForUserCompletion
#expect(range == NSRange(location: 0, length: 5))
}
/// Right after an open paren, the range is EMPTY at the caret — an argument
/// label (`freq:`) inserts there instead of replacing the receiver
/// (`OscSin.Audio(` → erases `OscSin.Audio` bug).
@Test func afterOpenParenRangeIsEmptyAtCaret() {
let text: String = "OscSin.Audio("
let tv: ScratchTextView = makeTextView(text, caret: text.utf16.count)
let range: NSRange = tv.rangeForUserCompletion
#expect(range == NSRange(location: text.utf16.count, length: 0))
}
/// After a comma + space (next argument position), the range is empty too.
@Test func afterCommaSpaceRangeIsEmptyAtCaret() {
let text: String = "OscSin.Audio(freq: 440, "
let tv: ScratchTextView = makeTextView(text, caret: text.utf16.count)
let range: NSRange = tv.rangeForUserCompletion
#expect(range == NSRange(location: text.utf16.count, length: 0))
}
/// A partial argument label inside parens replaces just that partial word,
/// not the receiver expression.
@Test func partialArgLabelCoversOnlyThePartial() {
let text: String = "OscSin.Audio(fr"
let tv: ScratchTextView = makeTextView(text, caret: text.utf16.count)
let range: NSRange = tv.rangeForUserCompletion
// `fr` starts at offset 13 (after `OscSin.Audio(`), length 2.
#expect(range == NSRange(location: 13, length: 2))
}
/// Underscore is an identifier character, so a leading-underscore partial is
/// covered whole.
@Test func underscoreIsIdentifierChar() {
let tv: ScratchTextView = makeTextView("_foo", caret: 4)
let range: NSRange = tv.rangeForUserCompletion
#expect(range == NSRange(location: 0, length: 4))
}
}
#endif