Tests/OrgEditorAppKitTests/LineIndexTests.swift

design-themes-widgets
orgstar/Tests/OrgEditorAppKitTests/LineIndexTests.swift history · blame · raw

71 lines · 3110 bytes

 1import Foundation
 2import Testing
 3import OrgPresentation
 4@testable import OrgEditorAppKit
 5
 6/// SplitMix64, so failures reproduce from the seed.
 7struct SeededGenerator: RandomNumberGenerator {
 8    var state: UInt64
 9    mutating func next() -> UInt64 {
10        state &+= 0x9E37_79B9_7F4A_7C15
11        var z = state
12        z = (z ^ (z >> 30)) &* 0xBF58_476D_1CE4_E5B9
13        z = (z ^ (z >> 27)) &* 0x94D0_49BB_1331_11EB
14        return z ^ (z >> 31)
15    }
16}
17
18/// `ORGSTAR_SEED` when set, else `fallback`, so random tests repeat unless asked to vary.
19func testSeed(_ fallback: UInt64) -> UInt64 {
20    ProcessInfo.processInfo.environment["ORGSTAR_SEED"].flatMap { UInt64($0) } ?? fallback
21}
22
23struct LineIndexTests {
24    func naive(_ text: String) -> [Int] {
25        var starts = [0]
26        for (i, u) in text.utf16.enumerated() where u == 10 { starts.append(i + 1) }
27        return starts
28    }
29
30    @Test func followsRandomEdits() {
31        let seed = testSeed(20261122)
32        var generator = SeededGenerator(state: seed)
33        let pieces = ["a", "bc\n", "\n", "\n\n", "xyz", "é\n", "😀", ""]
34        var text = "first\nsecond\n\nthird"
35        let index = LineIndex(text as NSString)
36        for _ in 0..<2000 {
37            let ns = text as NSString
38            let start = Int.random(in: 0...ns.length, using: &generator)
39            let end = min(ns.length, start + Int.random(in: 0...6, using: &generator))
40            let replacement = pieces.randomElement(using: &generator)!
41            text = ns.replacingCharacters(in: NSRange(location: start, length: end - start), with: replacement)
42            index.replace(start..<end, newLength: (replacement as NSString).length, in: text as NSString)
43            #expect(index.starts == naive(text), "seed \(seed)")
44            if index.starts != naive(text) { return }
45        }
46        #expect(index.line(of: 0) == 1)
47        #expect(index.line(of: (text as NSString).length) == naive(text).count)
48    }
49}
50
51@MainActor
52struct ShadedBlockTests {
53    /// Bands after edits match a full search once typing settles.
54    @Test func incrementalMatchesFull() {
55        let seed = testSeed(20261122)
56        var generator = SeededGenerator(state: seed)
57        let pieces = ["x", "word ", "\n", "#+begin_src sh\n", "#+end_src\n", ": fixed\n", "* H\n", "#+begin_quote\n", "#+end_quote\n", ""]
58        let h = Harness("* A\n#+begin_src sh\nls\n#+end_src\ntext\n: one\n* B\nbody\n")
59        for _ in 0..<400 {
60            let length = h.editor.document.text.utf16.count
61            let start = Int.random(in: 0...length, using: &generator)
62            let end = min(length, start + Int.random(in: 0...5, using: &generator))
63            let piece = pieces.randomElement(using: &generator)!
64            h.textView.insertText(piece, replacementRange: NSRange(location: start, length: end - start))
65            h.editor.settleBlocks()
66            let full = Presentation.shadedBlocks(h.editor.document.tree)
67            #expect(h.editor.shaded.all == full, "\(h.editor.document.text) seed \(seed)")
68            if h.editor.shaded.all != full { return }
69        }
70    }
71}