Files
Lyrebird/LiveCompile/Tests/LyrebirdLiveCompileTests/SnippetHarnessTests.swift
T
josh b6fdee8bfc Lyrebird 2.0.0 — Linux support
Consolidated release: the cross-platform Linux port (engine, generators, CLI,
REPL, NRT, portable DSP), live ALSA audio + ALSA MIDI + POSIX OSC + portable
WAV codec, the GTK4 GUI (LyrebirdUI on the SwiftOpenUI backend), and the Linux
live-coding scratchpad (LiveCompile). Apple platforms unchanged; all additive.
See RELEASES.md and LINUX_README.md.

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

50 lines
1.9 KiB
Swift

// SnippetHarnessTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Testing
import Foundation
@testable import LyrebirdLiveCompile
#if os(macOS) || os(Linux)
@Suite("SnippetHarness")
struct SnippetHarnessTests {
@Test
func wrappedSourceContainsPreambleAndUserBody() {
let wrapped: WrappedSnippet = SnippetHarness.wrap(userBody: "context.reportMessage(\"hello\")")
// Foundation so snippets can use `pow`, `sin`, `Date`, … without an import
// (a snippet can't declare one — its body is inside the entry function).
#expect(wrapped.source.contains("import Foundation"))
#expect(wrapped.source.contains("import LyrebirdSynth"))
#expect(wrapped.source.contains("import LyrebirdLiveCompile"))
#expect(wrapped.source.contains("@_cdecl(\"lyrebird_snippet_entry\")"))
#expect(wrapped.source.contains("Unmanaged<SnippetContext>"))
#expect(wrapped.source.contains("context.reportMessage(\"hello\")"))
}
@Test
func userBodyLineOffsetMatchesActualSourceLayout() {
let body: String = "let x = 1"
let wrapped: WrappedSnippet = SnippetHarness.wrap(userBody: body)
let lines: [String.SubSequence] = wrapped.source.split(separator: "\n", omittingEmptySubsequences: false)
// Line at index `offset - 1` (0-indexed) should be the first line of the body.
let bodyLineIndex: Int = wrapped.userBodyLineOffset - 1
#expect(bodyLineIndex >= 0)
#expect(bodyLineIndex < lines.count)
#expect(String(lines[bodyLineIndex]) == body)
}
@Test
func multilineUserBodyIsPreservedVerbatim() {
let body: String = "let a = 1\nlet b = 2\ncontext.reportMessage(\"\\(a + b)\")"
let wrapped: WrappedSnippet = SnippetHarness.wrap(userBody: body)
#expect(wrapped.source.contains(body))
}
}
#endif