v1.4.0 release: live-coding scratch editor, interpreted Graph DSL, ClosureSynthesizer + SampleLoop
This commit is contained in:
@@ -0,0 +1,101 @@
|
||||
// ScratchSwiftTokenizerTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#if os(macOS)
|
||||
import Testing
|
||||
import Foundation
|
||||
@testable import LyrebirdLiveCompileUI
|
||||
|
||||
@Suite("ScratchSwiftTokenizer")
|
||||
struct ScratchSwiftTokenizerTests {
|
||||
|
||||
/// The token kind covering the first occurrence of `substring`, or `nil` if
|
||||
/// no token exactly covers it. Verifies the range maps back to `substring`.
|
||||
private func kind(of substring: String, in source: String) -> ScratchSwiftTokenizer.Kind? {
|
||||
let ns: NSString = source as NSString
|
||||
let target: NSRange = ns.range(of: substring)
|
||||
for token: ScratchSwiftTokenizer.Token in ScratchSwiftTokenizer.tokens(in: source) {
|
||||
if NSEqualRanges(token.range, target) { return token.kind }
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@Test func keywordsAndTypesAndPlainIdentifiers() {
|
||||
let src: String = "let osc = OscSin.Audio(freq: 440)"
|
||||
#expect(kind(of: "let", in: src) == .keyword)
|
||||
#expect(kind(of: "OscSin", in: src) == .type)
|
||||
// Lowercase identifiers produce no token.
|
||||
#expect(kind(of: "osc", in: src) == nil)
|
||||
#expect(kind(of: "freq", in: src) == nil)
|
||||
}
|
||||
|
||||
@Test func numbers() {
|
||||
#expect(kind(of: "440", in: "freq: 440") == .number)
|
||||
#expect(kind(of: "0.15", in: "mul: 0.15") == .number)
|
||||
#expect(kind(of: "0xFF", in: "let x = 0xFF") == .number)
|
||||
}
|
||||
|
||||
@Test func numberDoesNotEatRangeOperator() {
|
||||
// `1...5` must not be swallowed into one number token — the `.` is only
|
||||
// consumed when followed by a digit.
|
||||
let src: String = "for i in 1...5 {}"
|
||||
#expect(kind(of: "1", in: src) == .number)
|
||||
#expect(kind(of: "5", in: src) == .number)
|
||||
}
|
||||
|
||||
@Test func strings() {
|
||||
#expect(kind(of: "\"hello\"", in: "reportMessage(\"hello\")") == .string)
|
||||
// Escaped quote stays inside the string.
|
||||
let esc: String = "let s = \"a\\\"b\""
|
||||
#expect(kind(of: "\"a\\\"b\"", in: esc) == .string)
|
||||
}
|
||||
|
||||
@Test func lineAndBlockComments() {
|
||||
#expect(kind(of: "// hello", in: "let x = 1 // hello") == .comment)
|
||||
// Nested block comment closes at the outer `*/`.
|
||||
let nested: String = "/* a /* b */ c */"
|
||||
#expect(kind(of: nested, in: nested) == .comment)
|
||||
}
|
||||
|
||||
@Test func attributesColorAsKeyword() {
|
||||
#expect(kind(of: "@MainActor", in: "@MainActor func f() {}") == .keyword)
|
||||
}
|
||||
|
||||
@Test func utf16RangesSurviveMultiUnitCharacters() {
|
||||
// An emoji (2 UTF-16 units) before a keyword: the keyword's NSRange must
|
||||
// still line up with the NSString substring.
|
||||
let src: String = "// 🎹\nlet x = 1"
|
||||
#expect(kind(of: "let", in: src) == .keyword)
|
||||
#expect(kind(of: "1", in: src) == .number)
|
||||
}
|
||||
|
||||
@Test func emptyStringProducesNoTokens() {
|
||||
#expect(ScratchSwiftTokenizer.tokens(in: "").isEmpty)
|
||||
}
|
||||
|
||||
// Regression: a lone trailing backslash inside an unterminated string used to
|
||||
// overrun the UTF-16 offset table and trap. Tokenizing must not crash, for
|
||||
// both single-line and triple-quoted strings.
|
||||
@Test func trailingBackslashAtEndOfBufferDoesNotCrash() {
|
||||
_ = ScratchSwiftTokenizer.tokens(in: "\"\\") // opening " then \
|
||||
_ = ScratchSwiftTokenizer.tokens(in: "\"\"\"\\") // triple-open then \
|
||||
_ = ScratchSwiftTokenizer.tokens(in: "let s = \"abc\\") // realistic edit
|
||||
// A backslash as the very last character overall.
|
||||
_ = ScratchSwiftTokenizer.tokens(in: "\\")
|
||||
}
|
||||
|
||||
@Test func signedExponentIsOneNumberToken() {
|
||||
#expect(kind(of: "1e-10", in: "let x = 1e-10") == .number)
|
||||
#expect(kind(of: "2.5E+3", in: "let y = 2.5E+3") == .number)
|
||||
}
|
||||
|
||||
@Test func concurrencyKeywords() {
|
||||
#expect(kind(of: "actor", in: "actor A {}") == .keyword)
|
||||
#expect(kind(of: "nonisolated", in: "nonisolated func f() {}") == .keyword)
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user