// 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