import Foundation import Testing import UIKit @testable import gitbay struct CodeLinesTests { private func text(_ lines: [AttributedString]) -> [String] { lines.map { String($0.characters) } } @Test func splitsOnNewlinesKeepingBlankLines() { let lines = codeLines(NSAttributedString(string: "a\n\nb\r\nc")) #expect(text(lines) == ["a", "", "b", "c"]) } @Test func aTrailingNewlineAddsNoLine() { #expect(text(codeLines(NSAttributedString(string: "a\nb\n"))) == ["a", "b"]) } @Test func keepsAttributesPerLine() { let source = NSMutableAttributedString(string: "red\nplain") source.addAttribute(.foregroundColor, value: UIColor.red, range: NSRange(location: 0, length: 3)) let lines = codeLines(source) #expect(lines[0].runs.first?.uiKit.foregroundColor == UIColor.red) #expect(lines[1].runs.first?.uiKit.foregroundColor == nil) } @Test func aLongFileKeepsEveryLine() { let source = (1...2682).map { "line \($0)" }.joined(separator: "\n") let lines = codeLines(NSAttributedString(string: source)) #expect(lines.count == 2682) #expect(text([lines.last!]) == ["line 2682"]) } }