|
| 1 | +import XCTest |
| 2 | +import Darwin |
| 3 | +import Foundation |
| 4 | +@testable import Zero |
| 5 | + |
| 6 | +@MainActor |
| 7 | +final class CodeEditorViewTests: XCTestCase { |
| 8 | + override func setUp() { |
| 9 | + super.setUp() |
| 10 | + AppLogStore.shared.clear() |
| 11 | + } |
| 12 | + |
| 13 | + override func tearDown() { |
| 14 | + AppLogStore.shared.clear() |
| 15 | + super.tearDown() |
| 16 | + } |
| 17 | + |
| 18 | + func testSetupWithInvalidThemeDoesNotPrintToStdout() { |
| 19 | + let textView = HighlightedTextView(frame: .zero) |
| 20 | + |
| 21 | + let stdout = captureStdout { |
| 22 | + textView.setup(language: "swift", themeName: "__invalid_theme__") |
| 23 | + } |
| 24 | + |
| 25 | + XCTAssertTrue(stdout.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines).isEmpty) |
| 26 | + } |
| 27 | + |
| 28 | + func testSetupWithInvalidThemeAppendsFailureToAppLogStore() { |
| 29 | + let textView = HighlightedTextView(frame: .zero) |
| 30 | + |
| 31 | + textView.setup(language: "swift", themeName: "__invalid_theme__") |
| 32 | + |
| 33 | + let logEntries = AppLogStore.shared.recentEntries() |
| 34 | + XCTAssertTrue(logEntries.contains { entry in |
| 35 | + entry.contains("Failed to load theme") && entry.contains("__invalid_theme__") |
| 36 | + }) |
| 37 | + } |
| 38 | + |
| 39 | + private func captureStdout(_ operation: () -> Void) -> String { |
| 40 | + fflush(stdout) |
| 41 | + |
| 42 | + let outputPipe = Pipe() |
| 43 | + let originalStdout = dup(STDOUT_FILENO) |
| 44 | + dup2(outputPipe.fileHandleForWriting.fileDescriptor, STDOUT_FILENO) |
| 45 | + |
| 46 | + operation() |
| 47 | + |
| 48 | + fflush(stdout) |
| 49 | + dup2(originalStdout, STDOUT_FILENO) |
| 50 | + close(originalStdout) |
| 51 | + |
| 52 | + outputPipe.fileHandleForWriting.closeFile() |
| 53 | + let outputData = outputPipe.fileHandleForReading.readDataToEndOfFile() |
| 54 | + outputPipe.fileHandleForReading.closeFile() |
| 55 | + |
| 56 | + return String(data: outputData, encoding: .utf8) ?? "" |
| 57 | + } |
| 58 | +} |
0 commit comments