Files

113 lines
4.2 KiB
Swift

// DSLParserTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Testing
import LyrebirdGraphDSL
@Suite("DSLParser")
struct DSLParserTests {
@Test func parsesAnonymousCall() throws {
let program: DSLProgram = try DSLParser.parse("OscSin(freq: 440, mul: 0.1)")
#expect(program.statements.count == 1)
guard case .expression(let expr) = program.statements[0],
case .call(let name, _, let args, _) = expr else {
Issue.record("expected a call expression"); return
}
#expect(name == "OscSin")
#expect(args.map { $0.label } == ["freq", "mul"])
}
@Test func parsesSlotAssignment() throws {
let program: DSLProgram = try DSLParser.parse("bass = OscSin(freq: 55)")
guard case .assignment(let name, _, _) = program.statements[0] else {
Issue.record("expected an assignment"); return
}
#expect(name == "bass")
}
@Test func operatorPrecedenceMultiplyBeforeAdd() throws {
// a + b * c ==> a + (b * c)
let program: DSLProgram = try DSLParser.parse("A(freq: 1) + B(freq: 2) * C(freq: 3)")
guard case .expression(let expr) = program.statements[0],
case .binary(let op, _, let rhs, _) = expr else {
Issue.record("expected a binary add"); return
}
#expect(op == .add)
guard case .binary(let innerOp, _, _, _) = rhs else {
Issue.record("rhs should be the multiply"); return
}
#expect(innerOp == .multiply)
}
@Test func parensOverridePrecedence() throws {
// (a + b) * c ==> multiply at the top
let program: DSLProgram = try DSLParser.parse("(A(freq: 1) + B(freq: 2)) * C(freq: 3)")
guard case .expression(let expr) = program.statements[0],
case .binary(let op, _, _, _) = expr else {
Issue.record("expected a binary"); return
}
#expect(op == .multiply)
}
@Test func nestedCallAsArgument() throws {
let program: DSLProgram = try DSLParser.parse("Pan2(input: OscSin(freq: 440), pan: 0)")
guard case .expression(.call(_, _, let args, _)) = program.statements[0],
case .call(let innerName, _, _, _) = args[0].value else {
Issue.record("expected a nested call argument"); return
}
#expect(innerName == "OscSin")
}
@Test func multipleStatementsAcrossLines() throws {
let program: DSLProgram = try DSLParser.parse("bass = A(freq: 1)\nlead = B(freq: 2)")
#expect(program.statements.count == 2)
}
@Test func trailingGarbageThrows() {
#expect(throws: DSLParseError.self) {
_ = try DSLParser.parse("OscSin(freq: 440) 123")
}
}
@Test func deeplyNestedInputThrowsInsteadOfCrashing() {
// Pathological / generated input must not overflow the stack.
let deep: String = String(repeating: "(", count: 1000) + "1"
+ String(repeating: ")", count: 1000)
#expect(throws: DSLParseError.self) {
_ = try DSLParser.parse(deep)
}
}
@Test func unclosedParenThrowsSpecificError() throws {
do {
_ = try DSLParser.parse("OscSin(freq: 440")
Issue.record("expected unclosedParen")
} catch let error as DSLParseError {
#expect(error.kind == .unclosedParen)
}
}
@Test func constantFoldingProducesNumberNodes() throws {
// 220 * 2 folds to a single number literal.
let program: DSLProgram = try DSLParser.parse("Osc(freq: 220 * 2)")
guard case .expression(.call(_, _, let args, _)) = program.statements[0],
case .number(let n, _) = args[0].value else {
Issue.record("expected a folded number"); return
}
#expect(n == 440)
}
@Test func unaryMinusFoldsToNegativeNumber() throws {
let program: DSLProgram = try DSLParser.parse("Osc(add: -0.5)")
guard case .expression(.call(_, _, let args, _)) = program.statements[0],
case .number(let n, _) = args[0].value else {
Issue.record("expected a negative number literal"); return
}
#expect(n == -0.5)
}
}