Files
Lyrebird/Tests/LyrebirdGraphDSLTests/DSLInterpreterTests.swift
T

141 lines
5.4 KiB
Swift

// DSLInterpreterTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Testing
import LyrebirdSynth
import LyrebirdGraphDSL
@MainActor
@Suite("DSLInterpreter")
struct DSLInterpreterTests {
/// Add each built output to the engine and render one block.
private func render(_ outputs: [DSLOutput]) throws -> SynthesisEngine {
let engine: SynthesisEngine = makeDSLTestEngine()
try engine.start()
for output: DSLOutput in outputs {
try engine.add(ClosureSynthesizer { output.graph })
}
engine.processBlock(inputSamples: [])
return engine
}
@Test func evaluatesAndRendersASineTimesGain() throws {
let outputs: [DSLOutput] = try DSLInterpreter.evaluate("OscSin(freq: 440) * 0.1")
#expect(outputs.count == 1)
#expect(outputs[0].slot == nil)
let engine: SynthesisEngine = try render(outputs)
#expect(busHasSignal(engine, bus: 0))
}
@Test func slotAssignmentCarriesTheSlotName() throws {
let outputs: [DSLOutput] = try DSLInterpreter.evaluate("bass = OscSin(freq: 55) * 0.2")
#expect(outputs[0].slot == "bass")
let engine: SynthesisEngine = try render(outputs)
#expect(busHasSignal(engine, bus: 0))
}
@Test func nestedGraphRendersStereo() throws {
let outputs: [DSLOutput] = try DSLInterpreter.evaluate(
"Pan2(input: OscSin(freq: 440) * 0.1, pan: 0)"
)
let engine: SynthesisEngine = try render(outputs)
#expect(busHasSignal(engine, bus: 0))
#expect(busHasSignal(engine, bus: 1))
}
@Test func positionalArgumentsBind() throws {
// OscSin(freq) positionally.
let outputs: [DSLOutput] = try DSLInterpreter.evaluate("OscSin(440) * 0.1")
let engine: SynthesisEngine = try render(outputs)
#expect(busHasSignal(engine, bus: 0))
}
@Test func multipleStatementsProduceMultipleOutputs() throws {
let outputs: [DSLOutput] = try DSLInterpreter.evaluate(
"bass = OscSin(freq: 55) * 0.2\nlead = OscSin(freq: 440) * 0.1"
)
#expect(outputs.count == 2)
#expect(outputs.map { $0.slot } == ["bass", "lead"])
}
@Test func unknownGeneratorThrows() {
#expect(throws: DSLBuildError.unknownBuiltin("NotAGenerator")) {
_ = try DSLInterpreter.evaluate("NotAGenerator(freq: 1)")
}
}
@Test func unknownArgumentThrows() {
#expect(throws: DSLBuildError.unknownArgument(builtin: "OscSin", label: "nope")) {
_ = try DSLInterpreter.evaluate("OscSin(nope: 1)")
}
}
@Test func missingRequiredArgumentThrows() {
// OscSin.freq is required.
#expect(throws: DSLBuildError.missingArgument(builtin: "OscSin", label: "freq")) {
_ = try DSLInterpreter.evaluate("OscSin(mul: 0.1)")
}
}
@Test func controlOnlyGeneratorResolvesViaFallback() throws {
// MouseX is control-only; the builder falls back to its sole rate.
let outputs: [DSLOutput] = try DSLInterpreter.evaluate("MouseX()")
#expect(outputs.count == 1)
}
@Test func explicitControlRateSelectsControlVariant() throws {
// `.Control` requests the control-rate OscSin driving an audio OscSin's freq.
let outputs: [DSLOutput] = try DSLInterpreter.evaluate(
"OscSin(freq: OscSin.Control(freq: 4, mul: 50, add: 440), mul: 0.1)"
)
let engine: SynthesisEngine = try render(outputs)
#expect(busHasSignal(engine, bus: 0))
}
@Test func explicitRateOnUnavailableVariantThrows() {
// Pan2 is audio-only; `.Control` must fail rather than silently pick audio.
#expect(throws: DSLBuildError.rateUnavailable(builtin: "Pan2", rate: "control")) {
_ = try DSLInterpreter.evaluate("Pan2.Control(input: OscSin(freq: 1), pan: 0)")
}
}
@Test func invalidRateSuffixThrows() {
#expect(throws: DSLParseError.self) {
_ = try DSLInterpreter.evaluate("OscSin.xyz(freq: 440)")
}
}
/// Paste-compatibility: the exact Swift form `OscSin.Audio(freq: 440) * 0.1`
/// — capitalized nested-class name and all — evaluates and renders in the
/// DSL, so a graph expression copied to/from a Lyrebird `build()` is spelled
/// the same in both places.
@Test func swiftFormGraphExpressionEvaluates() throws {
let outputs: [DSLOutput] = try DSLInterpreter.evaluate(
"Pan2(input: OscSin.Audio(freq: 440) * 0.1, pan: 0)"
)
let engine: SynthesisEngine = try render(outputs)
#expect(busHasSignal(engine, bus: 0))
#expect(busHasSignal(engine, bus: 1))
}
@Test func duplicateArgumentThrows() {
// A parameter bound twice (positional then label) is an error.
#expect(throws: DSLBuildError.duplicateArgument(builtin: "OscSin", label: "freq")) {
_ = try DSLInterpreter.evaluate("OscSin(440, freq: 550)")
}
}
@Test func foldedConstantSatisfiesNumericParameter() throws {
// DelayLine.delay is a Double (numeric-literal-only). A folded computed
// constant must reach it as a number, not a signal — build must succeed
// (an un-folded `0.1 * 2` would be a generator and throw expectedNumber).
_ = try DSLInterpreter.evaluate(
"DelayLine(input: OscSin(freq: 440) * 0.1, maxDelay: 0.5, delay: 0.1 * 2)"
)
}
}