// DiagnosticParserTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Testing import Foundation @testable import LyrebirdLiveCompile #if os(macOS) @Suite("DiagnosticParser") struct DiagnosticParserTests { @Test func parsesSingleErrorAtUserBodyLine() throws { // Representative offset: for a preamble whose user body starts at line 9, // a swiftc error on wrapped line 10 maps to user line 2. (This tests the // parser's arithmetic directly; the actual harness offset — which shifts // when the preamble changes — is exercised by // `endToEndOffsetMatchesSnippetHarness` below.) let stderr: String = "/tmp/Snippet.swift:10:14: error: cannot find 'foo' in scope" let diagnostics: [Diagnostic] = DiagnosticParser.parse(stderr: stderr, userBodyLineOffset: 9) try #require(diagnostics.count == 1) let d: Diagnostic = diagnostics[0] #expect(d.line == 2) #expect(d.column == 14) #expect(d.severity == .error) #expect(d.message == "cannot find 'foo' in scope") #expect(d.rawLine == 10) } @Test func diagnosticOnPreambleLineHasNilUserLine() throws { let stderr: String = "/tmp/Snippet.swift:1:8: error: no such module 'Lyrebird'" let diagnostics: [Diagnostic] = DiagnosticParser.parse(stderr: stderr, userBodyLineOffset: 9) try #require(diagnostics.count == 1) #expect(diagnostics[0].line == nil) #expect(diagnostics[0].rawLine == 1) #expect(diagnostics[0].column == 8) } @Test func parsesMultipleDiagnosticsInOrder() throws { let stderr: String = """ /tmp/Snippet.swift:10:1: warning: result of call to 'foo()' is unused /tmp/Snippet.swift:11:5: error: cannot convert value of type 'String' to expected argument type 'Int' /tmp/Snippet.swift:11:5: note: change type to fix """ let diagnostics: [Diagnostic] = DiagnosticParser.parse(stderr: stderr, userBodyLineOffset: 9) try #require(diagnostics.count == 3) #expect(diagnostics[0].severity == .warning) #expect(diagnostics[1].severity == .error) #expect(diagnostics[2].severity == .note) #expect(diagnostics[0].line == 2) #expect(diagnostics[1].line == 3) } @Test func ignoresSourceContextAndCaretLines() { // Real swiftc emits header line then a source-context line then a caret line. // Only the header should parse. let stderr: String = """ /tmp/Snippet.swift:10:14: error: cannot find 'foo' in scope 10 | foo() | `- error: cannot find 'foo' in scope """ let diagnostics: [Diagnostic] = DiagnosticParser.parse(stderr: stderr, userBodyLineOffset: 9) #expect(diagnostics.count == 1) #expect(diagnostics[0].message == "cannot find 'foo' in scope") } @Test func emptyStderrYieldsEmptyArray() { #expect(DiagnosticParser.parse(stderr: "", userBodyLineOffset: 9) == []) } @Test func malformedLinesAreIgnored() throws { let stderr: String = """ this is not a diagnostic /tmp/Snippet.swift:NOT_A_NUMBER:5: error: bogus /tmp/Snippet.swift:10:14: error: real error another junk line """ let diagnostics: [Diagnostic] = DiagnosticParser.parse(stderr: stderr, userBodyLineOffset: 9) try #require(diagnostics.count == 1) #expect(diagnostics[0].message == "real error") } @Test func endToEndOffsetMatchesSnippetHarness() throws { // Use the actual harness offset and confirm a fabricated swiftc line that // points at "line 1 of the user body" maps to user line 1. let wrapped: WrappedSnippet = SnippetHarness.wrap(userBody: "let x = 1") let userBodyLine: Int = wrapped.userBodyLineOffset let stderr: String = "/tmp/Snippet.swift:\(userBodyLine):5: error: synthetic" let diagnostics: [Diagnostic] = DiagnosticParser.parse( stderr: stderr, userBodyLineOffset: wrapped.userBodyLineOffset ) try #require(diagnostics.count == 1) #expect(diagnostics[0].line == 1) } } #endif