SPB Git

Repository Public

LibraryTests.swift 5.7 KB 98 lines swift

1// ORIX Sonata2// File: Packages/SonataUI/Tests/SonataUITests/LibraryTests.swift3// Description: Library space: the catalogue matches the standard library and the registry, search4// works, and every piece of code the space can insert is accepted by the real compiler.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import Foundation9import SonataBridge10import Testing11@testable import SonataUI1213let repositoryRoot = URL(fileURLWithPath: #filePath).deletingLastPathComponent().deletingLastPathComponent().deletingLastPathComponent().deletingLastPathComponent().deletingLastPathComponent()1415func repositoryCatalogue() throws -> LibraryCatalogue {16    let std = try String(contentsOf: repositoryRoot.appendingPathComponent("StandardLibrary/std/instruments.sonata"), encoding: .utf8)17    return LibraryCatalogue(instruments: StdInstrument.parse(std), registry: SonataLibrary.registry, examples: LibraryExample.load(from: repositoryRoot.appendingPathComponent("Examples")))18}1920func errors(_ source: String) async throws -> [String] {21    try await SonataCompilerSession().compile(name: "main", source: source).errors.map { "\($0.code) \($0.message) @\($0.line)" }22}2324@Test func catalogueMatchesTheStandardLibraryAndTheRegistry() throws {25    let c = try repositoryCatalogue()26    #expect(c.instruments.count >= 13)27    #expect(Set(c.instruments.map(\.name)).count == c.instruments.count, "no duplicate names")28    #expect(c.instruments.contains { $0.name == "GrandPiano" && $0.model == "piano" && $0.preset == "grand" })29    // Every named instrument is built on an engine and a preset the registry really has.30    for i in c.instruments {31        let engine = try #require(c.registry.engines.first { $0.model == i.model }, "\(i.name): unknown model \(i.model)")32        if let p = i.preset { #expect(engine.presets.contains { $0.name == p }, "\(i.name): unknown preset \(p)") }33        #expect(!i.group.isEmpty)34    }35    #expect(c.enginePresetCount == c.registry.engines.reduce(0) { $0 + $1.presets.count })36    #expect(c.examples.count >= 10)37    #expect(c.examples.contains { $0.name == "aurora" && !$0.summary.isEmpty && !$0.needsProject })38    #expect(c.examples.contains { $0.needsProject })39}4041@Test func searchFindsByNameParameterAndText() throws {42    let c = try repositoryCatalogue()43    let all = c.sections().flatMap(\.items)44    #expect(Set(all.map(\.id)).count == all.count, "identifiers are unique")45    #expect(c.sections().map(\.title) == ["Instruments", "Engines", "Effects", "Patch Nodes", "Examples", "Snippets"])46    let reverb = c.sections(matching: "reverb").flatMap(\.items)47    #expect(reverb.contains { $0.id == "effect:reverb" })48    #expect(c.sections(matching: "GRANDPIANO").flatMap(\.items).contains { $0.id == "instrument:GrandPiano" })49    // A parameter name finds the engine that has it; several words must all match.50    #expect(c.sections(matching: "plucked damping").flatMap(\.items).contains { $0.id == "engine:plucked_string" })51    #expect(c.sections(matching: "zzzz-not-there").isEmpty)52    #expect(c.sections(matching: "   ").flatMap(\.items).count == all.count)53}5455@Test func everyInsertionCompiles() async throws {56    let c = try repositoryCatalogue()57    let base = Templates.starter58    #expect(try await errors(base).isEmpty, "the starter template compiles")59    var checked = 060    for item in c.sections().flatMap(\.items) {61        guard let candidate = LibraryInsertion.inserting(item, into: base) else {62            // Only examples and patch nodes have no insertion.63            switch item {64            case .example, .node: continue65            default: Issue.record("\(item.id) could not be inserted into the starter template")66            }67            continue68        }69        #expect(candidate != base && candidate.count > base.count)70        let found = try await errors(candidate)71        #expect(found.isEmpty, "\(item.id): \(found)")72        checked += 173    }74    #expect(checked == c.instruments.count + c.registry.engines.count + c.registry.effects.count + LibrarySnippet.all.count)75}7677@Test func insertionPlacesCodeWhereItBelongs() throws {78    let c = try repositoryCatalogue()79    let source = "// header\ncomposition \"A\" {\n    tempo 120bpm // a { brace in a comment\n    instrument piano: GrandPiano {\n        play C4 duration 1/4\n    }\n}\n"80    let harp = try #require(c.instruments.first { $0.name == "Harp" })81    let withTrack = try #require(LibraryInsertion.inserting(.instrument(harp), into: source))82    #expect(withTrack.contains("import std.instruments\ncomposition") || withTrack.contains("import std.instruments\n"))83    #expect(withTrack.contains("    instrument harp: Harp {\n        play C4 duration 1/4\n    }\n}"))84    // A second insertion picks a new track name.85    #expect(try #require(LibraryInsertion.inserting(.instrument(harp), into: withTrack)).contains("instrument harp2: Harp"))86    let reverb = try #require(c.registry.effects.first { $0.kind == "reverb" })87    let withEffect = try #require(LibraryInsertion.inserting(.effect(reverb), into: source))88    #expect(withEffect.contains("        play C4 duration 1/4\n        effects { reverb "))89    // No composition: tracks and effects have nowhere to go.90    #expect(LibraryInsertion.inserting(.instrument(harp), into: "const x = 1\n") == nil)91    #expect(LibraryInsertion.inserting(.effect(reverb), into: "composition \"A\" {\n}\n") == nil)92    #expect(LibraryInsertion.inserting(.example(c.examples[0]), into: source) == nil)93    // Braces inside strings do not confuse the scan.94    let tricky = "composition \"A } B\" {\n    tempo 120bpm\n}\n"95    #expect(LibraryInsertion.compositionBody(in: tricky).map { String(tricky[$0.1...]) } == "}\n")96    #expect(LibraryInsertion.format(0.005, unit: "seconds") == "5ms" && LibraryInsertion.format(440, unit: "hertz") == "440Hz")97}98
Files main
Menu

Theme