import CoreGraphics import CoreText import XCTest @testable import Codepad final class LineLayoutTests: XCTestCase { private struct NoStyles: StyleResolver { func style(_ id: Int) -> TokenStyle? { nil } } private let font = CTFontCreateWithName("Menlo" as CFString, 14, nil) private lazy var metrics = RowMetrics(font: font, lineHeight: 1.4, tabSize: 4) private func layout(_ text: String, wrapWidth: Int = 0, spans: [Int] = []) -> LineLayout { LineLayout( text: Array(text.utf16), spans: spans, font: font, styles: NoStyles(), foreground: ThemeColors.default.foreground, metrics: metrics, wrapWidth: wrapWidth) } private var charWidth: CGFloat { metrics.spaceWidth } func testRowHeightIsRoundedFontSizeTimesLineHeight() { XCTAssertEqual(metrics.rowHeight, 20) XCTAssertEqual(metrics.ascent + metrics.descent, 20, accuracy: 1) } func testWrapOffIsOneRow() { let l = layout(String(repeating: "x", count: 60)) XCTAssertEqual(l.rowCount, 1) XCTAssertEqual(l.length, 60) XCTAssertEqual(l.width(), charWidth * 60, accuracy: 0.5) } func testWrapOnBreaksAtTheWidth() { let l = layout(String(repeating: "x", count: 60), wrapWidth: Int((charWidth * 20.5).rounded(.up))) XCTAssertEqual(l.rowCount, 3) var covered = 0 for row in 0..