Tests/OrgCoreTests/ListItemScanTests.swift
36 lines · 1688 bytes
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 func strings() -> [Substring] {
11 var generator = SystemRandomNumberGenerator()
12 var out: [Substring] = ["", "[@3]", "[@start:3] x", "[@b]", "[@start:]", "[@start:b]", "[@3", "[@ab]", "a :: b", "a ::", "a::b",
13 "a :: b :: c", "a\t::\tb", " :: x", "::", "a ::b", "tag :: rest", "x :: y\nz :: w"]
14 for _ in 0..<5000 {
15 let count = Int.random(in: 1...7, using: &generator)
16 out.append(Substring((0..<count).map { _ in pieces.randomElement(using: &generator)! }.joined().replacingOccurrences(of: "\\n", with: "\n")))
17 }
18 return out
19 }
20
21 @Test func counterMatchesThePattern() {
22 for s in Self.strings() {
23 let expected = s.prefixMatch(of: /\[@(?:start:)?(?:[0-9]+|[A-Za-z])\]/).map { s[$0.range] }
24 #expect(Parser.counter(s) == expected, "\(s.debugDescription)")
25 }
26 }
27
28 @Test func descriptionTagMatchesThePattern() {
29 for s in Self.strings() {
30 let m = s.prefixMatch(of: /(.*)([ \t]+::)(?:[ \t]+|$)/.anchorsMatchLineEndings())
31 let found = Parser.descriptionTag(s)
32 #expect(found?.tag == m.map { s[$0.1.startIndex..<$0.1.endIndex] }, "\(s.debugDescription)")
33 #expect(found?.separator == m.map { s[$0.2.startIndex..<$0.2.endIndex] }, "\(s.debugDescription)")
34 }
35 }
36}