Cotton code
34 lines · 2.0 KB · SwiftRaw
import XCTest@testable import CottonUIfinal class NativeSettingsOwnershipTests: XCTestCase {    static func publishedTables() throws -> Set<String> {        let core = SourceSweep.withoutComments(try SourceSweep.mustRead(            "crates/cotton-core/src/definitions/mod.rs", ownedBy: "native Settings table manifest"))        let declaration = try XCTUnwrap(core.range(of: #"\b(?:const|static)\s+SETTINGS_RECORD_TABLES\b[^=]*=\s*&?\["#, options: .regularExpression))        let end = try XCTUnwrap(core.range(of: "];", range: declaration.upperBound..<core.endIndex))        let manifest = String(core[declaration.upperBound..<end.lowerBound])        let tables = Set(try SourceSweep.matches(#""([a-z][a-z_0-9]*)""#, in: manifest).map {            String($0.dropFirst().dropLast())        })        return tables    }    /// RULE: A Settings table whose verbs are live in core has no Swift record source, so assistants and app pages cannot disagree about its rows or writes.    func testNativeSettingsTablesHaveNoSwiftRecordSource() throws {        let tables = try Self.publishedTables()        XCTAssertFalse(tables.isEmpty, "The core published no native Settings tables")        XCTAssertTrue(tables.isDisjoint(with: SettingsRecords.sources.map(\.table)),                      "A native Settings table still registers a Swift verb source")        for (name, text) in try SourceSweep.swiftSources() {            let code = SourceSweep.withoutComments(text)            guard code.range(of: #"\b(?:struct|class|actor)\s+\w+[^\{]*:\s*[^\{]*\b(?:SettingsTableSource|SettingsCommandFinishing|SettingsCreationPreparing)\b"#,                             options: .regularExpression) != nil else { continue }            for table in tables {                let pattern = #"\b(?:let|var)\s+table\s*(?::\s*String)?\s*=\s*""# + table + #"""#                XCTAssertTrue(try SourceSweep.matches(pattern, in: code).isEmpty,                              "\(name) answers native \(table) record verbs in Swift")            }        }    }}