Tests/OrgCoreTests/ListItemScanTests.swift

38 lines · 1783 bytes

4 symbols in this file
 1import Foundation
 2import Testing
 3@testable import OrgCore
 4
 5/// The hand-written scans for list item counters and description tags against the
 6/// patterns they replace.
 7struct ListItemScanTests {
 8    static let pieces = ["[@", "start:", "3", "12", "b", "B", "]", " ", "\t", "::", ":", "a", "é", "x y", "[", "\\n"]
 9
10    static let seed = testSeed(20261122)
11
12    static func strings() -> [Substring] {
13        var generator = SeededGenerator(state: seed)
14        var out: [Substring] = ["", "[@3]", "[@start:3] x", "[@b]", "[@start:]", "[@start:b]", "[@3", "[@ab]", "a :: b", "a ::", "a::b",
15                                "a :: b :: c", "a\t::\tb", " :: x", "::", "a ::b", "tag  ::  rest", "x :: y\nz :: w"]
16        for _ in 0..<5000 {
17            let count = Int.random(in: 1...7, using: &generator)
18            out.append(Substring((0..<count).map { _ in pieces.randomElement(using: &generator)! }.joined().replacingOccurrences(of: "\\n", with: "\n")))
19        }
20        return out
21    }
22
23    @Test func counterMatchesThePattern() {
24        for s in Self.strings() {
25            let expected = s.prefixMatch(of: /\[@(?:start:)?(?:[0-9]+|[A-Za-z])\]/).map { s[$0.range] }
26            #expect(Parser.counter(s) == expected, "\(s.debugDescription) seed \(Self.seed)")
27        }
28    }
29
30    @Test func descriptionTagMatchesThePattern() {
31        for s in Self.strings() {
32            let m = s.prefixMatch(of: /(.*)([ \t]+::)(?:[ \t]+|$)/.anchorsMatchLineEndings())
33            let found = Parser.descriptionTag(s)
34            #expect(found?.tag == m.map { s[$0.1.startIndex..<$0.1.endIndex] }, "\(s.debugDescription) seed \(Self.seed)")
35            #expect(found?.separator == m.map { s[$0.2.startIndex..<$0.2.endIndex] }, "\(s.debugDescription) seed \(Self.seed)")
36        }
37    }
38}