|
| 1 | +/* |
| 2 | + This source file is part of the Swift.org open source project |
| 3 | + |
| 4 | + Copyright (c) 2020 Apple Inc. and the Swift project authors |
| 5 | + Licensed under Apache License v2.0 with Runtime Library Exception |
| 6 | + |
| 7 | + See http://swift.org/LICENSE.txt for license information |
| 8 | + See http://swift.org/CONTRIBUTORS.txt for Swift project authors |
| 9 | +*/ |
| 10 | + |
| 11 | +import XCTest |
| 12 | + |
| 13 | +import TSCBasic |
| 14 | +import TSCUtility |
| 15 | + |
| 16 | +class DiagnosticsTests: XCTestCase { |
| 17 | + |
| 18 | + func testDiagnosticsLocationProviding() throws { |
| 19 | + let diagnostics = DiagnosticsEngine() |
| 20 | + |
| 21 | + struct BazLocation: DiagnosticLocation { |
| 22 | + let name: String |
| 23 | + |
| 24 | + var description: String { |
| 25 | + return name |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + struct CustomError: Error, CustomStringConvertible, DiagnosticLocationProviding { |
| 30 | + var location: String? |
| 31 | + var diagnosticLocation: DiagnosticLocation? { |
| 32 | + return location.flatMap{ BazLocation(name: $0) } |
| 33 | + } |
| 34 | + var description: String { |
| 35 | + return "provided location is '\(location ?? "nil")'" |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + diagnostics.with(location: BazLocation(name: "elsewhere")) { diagnostics in |
| 40 | + diagnostics.wrap { |
| 41 | + throw CustomError(location: "somewhere") |
| 42 | + } |
| 43 | + diagnostics.wrap { |
| 44 | + throw CustomError(location: nil) |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + XCTAssertEqual(diagnostics.diagnostics.count, 2) |
| 49 | + |
| 50 | + let firstDiagnostic = try XCTUnwrap(diagnostics.diagnostics.first) |
| 51 | + XCTAssertEqual(firstDiagnostic.location.description, "somewhere") |
| 52 | + XCTAssertEqual(firstDiagnostic.description, "provided location is 'somewhere'") |
| 53 | + XCTAssertEqual(firstDiagnostic.message.behavior, .error) |
| 54 | + |
| 55 | + let secondDiagnostic = try XCTUnwrap(diagnostics.diagnostics.last) |
| 56 | + XCTAssertEqual(secondDiagnostic.location.description, "elsewhere") |
| 57 | + XCTAssertEqual(secondDiagnostic.description, "provided location is 'nil'") |
| 58 | + XCTAssertEqual(secondDiagnostic.message.behavior, .error) |
| 59 | + } |
| 60 | +} |
0 commit comments