Files
Lyrebird/LiveCompile/Tests/LyrebirdLiveCompileTests/SwiftFormatterTests.swift
T
josh b6fdee8bfc Lyrebird 2.0.0 — Linux support
Consolidated release: the cross-platform Linux port (engine, generators, CLI,
REPL, NRT, portable DSP), live ALSA audio + ALSA MIDI + POSIX OSC + portable
WAV codec, the GTK4 GUI (LyrebirdUI on the SwiftOpenUI backend), and the Linux
live-coding scratchpad (LiveCompile). Apple platforms unchanged; all additive.
See RELEASES.md and LINUX_README.md.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-09-10 21:09:11 -07:00

53 lines
1.8 KiB
Swift

// SwiftFormatterTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
#if os(macOS) || os(Linux)
import Testing
import Foundation
@testable import LyrebirdLiveCompile
@Suite("SwiftFormatter")
struct SwiftFormatterTests {
@Test func formatsMessySource() throws {
guard SwiftFormatter.isAvailable else { return } // no toolchain formatter — skip
let messy: String = "let x=1\nstruct Foo{let a:Int}\n"
let formatted: String = try SwiftFormatter.format(source: messy)
// Canonical spacing appears; the mangled originals are gone.
#expect(formatted.contains("let x = 1"))
#expect(!formatted.contains("let x=1"))
#expect(!formatted.contains("struct Foo"))
}
@Test func alreadyFormattedIsStable() throws {
guard SwiftFormatter.isAvailable else { return }
let clean: String = "let x = 1\n"
let once: String = try SwiftFormatter.format(source: clean)
let twice: String = try SwiftFormatter.format(source: once)
#expect(once == twice) // idempotent
}
@Test func parseErrorThrowsFormatFailed() {
guard SwiftFormatter.isAvailable else { return }
// Unbalanced brace — swift-format should exit non-zero.
let broken: String = "func f( {\n"
var caught: SwiftFormatterError?
do {
_ = try SwiftFormatter.format(source: broken)
} catch let error as SwiftFormatterError {
caught = error
} catch {
Issue.record("unexpected error type: \(error)")
}
guard case .formatFailed = caught else {
Issue.record("expected .formatFailed, got \(String(describing: caught))")
return
}
}
}
#endif