Files
Lyrebird/LiveCompile/Tests/LyrebirdLiveCompileTests/SwiftFormatterTests.swift
T
josh 62b2f5dbec v1.5.0 release: LyrebirdRandom + Live Scratch editor
LyrebirdRandom scalar distribution sampling (seedable value type + shared
Random) with DistributionKind.sample dispatcher. Live Scratch editor: rename to
LyrebirdScratch, App Support artifact staging, Format Code (⌥⌘F), Render to File
(⌥⌘R offline CAF), line-number gutter, manual completion, Foundation in snippets,
scroll-jump fix, scheduling/NRT docs.
2026-07-22 07:53:12 -07:00

53 lines
1.7 KiB
Swift

// SwiftFormatterTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
#if os(macOS)
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