gitbayTests/CodeLinesTests.swift

v1.11.0
gitbay-ios/gitbayTests/CodeLinesTests.swift history · blame · raw

35 lines · 1224 bytes

 1import Foundation
 2import Testing
 3import UIKit
 4@testable import gitbay
 5
 6struct CodeLinesTests {
 7
 8    private func text(_ lines: [AttributedString]) -> [String] {
 9        lines.map { String($0.characters) }
10    }
11
12    @Test func splitsOnNewlinesKeepingBlankLines() {
13        let lines = codeLines(NSAttributedString(string: "a\n\nb\r\nc"))
14        #expect(text(lines) == ["a", "", "b", "c"])
15    }
16
17    @Test func aTrailingNewlineAddsNoLine() {
18        #expect(text(codeLines(NSAttributedString(string: "a\nb\n"))) == ["a", "b"])
19    }
20
21    @Test func keepsAttributesPerLine() {
22        let source = NSMutableAttributedString(string: "red\nplain")
23        source.addAttribute(.foregroundColor, value: UIColor.red, range: NSRange(location: 0, length: 3))
24        let lines = codeLines(source)
25        #expect(lines[0].runs.first?.uiKit.foregroundColor == UIColor.red)
26        #expect(lines[1].runs.first?.uiKit.foregroundColor == nil)
27    }
28
29    @Test func aLongFileKeepsEveryLine() {
30        let source = (1...2682).map { "line \($0)" }.joined(separator: "\n")
31        let lines = codeLines(NSAttributedString(string: source))
32        #expect(lines.count == 2682)
33        #expect(text([lines.last!]) == ["line 2682"])
34    }
35}