SPB Git

Repository Public

BundledExamplesTests.swift 2.7 KB 45 lines swift

1// ORIX Sonata2// File: Packages/SonataUI/Tests/SonataUITests/BundledExamplesTests.swift3// Description: The examples catalogue and opening an example as a copy that still compiles.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import Foundation8import SonataBridge9import Testing10@testable import SonataUI1112let repositoryExamples = URL(fileURLWithPath: #filePath).deletingLastPathComponent().deletingLastPathComponent().deletingLastPathComponent().deletingLastPathComponent().deletingLastPathComponent().appendingPathComponent("Examples")1314@Test func catalogueListsRealFilesInCategoryOrder() {15    let all = BundledExamples.load(from: repositoryExamples)16    #expect(all.count >= 36)17    #expect(Set(all.map(\.file)).count == all.count)18    for e in all { #expect(FileManager.default.fileExists(atPath: repositoryExamples.appendingPathComponent(e.file).path), "\(e.file)") }19    let groups = BundledExamples.grouped(all)20    #expect(groups.map(\.examples.count).reduce(0, +) == all.count)21    let ranks = groups.map { BundledExamples.categoryOrder.firstIndex(of: $0.category) ?? 99 }22    #expect(ranks == ranks.sorted())23    #expect(!BundledExamples.isPlainFileName("../x.sonata") && !BundledExamples.isPlainFileName("lib/x.sonata") && BundledExamples.isPlainFileName("aurora.sonata"))24}2526@Test func openingCopiesTheExampleWithItsModulesAndNeverOverwritesEdits() async throws {27    let dir = FileManager.default.temporaryDirectory.appendingPathComponent("sonata-examples-" + UUID().uuidString)28    defer { try? FileManager.default.removeItem(at: dir) }29    let first = try BundledExamples.copy("uses_modules.sonata", from: repositoryExamples, to: dir)30    #expect(first.lastPathComponent == "uses_modules.sonata")31    #expect(FileManager.default.fileExists(atPath: dir.appendingPathComponent("lib/motifs.sonata").path))32    // The copy compiles from its new folder, imports included.33    let session = SonataCompilerSession()34    await session.setRoot(dir)35    let result = try await session.compile(name: "uses_modules.sonata", source: try String(contentsOf: first, encoding: .utf8))36    #expect(result.ok, "\(result.diagnostics.map(\.message))")37    // Opening again reuses an unchanged copy; a copy the user edited is left alone.38    #expect(try BundledExamples.copy("uses_modules.sonata", from: repositoryExamples, to: dir) == first)39    try Data("// mine\n".utf8).write(to: first)40    let second = try BundledExamples.copy("uses_modules.sonata", from: repositoryExamples, to: dir)41    #expect(second.lastPathComponent == "uses_modules 2.sonata")42    #expect(try String(contentsOf: first, encoding: .utf8) == "// mine\n")43    #expect(throws: (any Error).self) { try BundledExamples.copy("../secret.sonata", from: repositoryExamples, to: dir) }44}45
Files main
Menu

Theme