Files
Lyrebird/LiveCompile/Tests/LyrebirdLiveCompileTests/SnippetHarnessTests.swift
T
josh 62b2f5dbec v1.5.0 release: LyrebirdRandom + Live Scratch editor
LyrebirdRandom scalar distribution sampling (seedable value type + shared
Random) with DistributionKind.sample dispatcher. Live Scratch editor: rename to
LyrebirdScratch, App Support artifact staging, Format Code (⌥⌘F), Render to File
(⌥⌘R offline CAF), line-number gutter, manual completion, Foundation in snippets,
scroll-jump fix, scheduling/NRT docs.
2026-07-22 07:53:12 -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)
@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