|
9 | 9 | import XCTest
|
10 | 10 | @testable import SwiftFormat
|
11 | 11 |
|
| 12 | +private let projectDirectory = URL(fileURLWithPath: #file) |
| 13 | + .deletingLastPathComponent().deletingLastPathComponent() |
| 14 | + |
12 | 15 | class OptionDescriptorTests: XCTestCase {
|
13 | 16 | private typealias OptionArgumentMapping<T> = (optionValue: T, argumentValue: String)
|
14 | 17 |
|
@@ -114,6 +117,31 @@ class OptionDescriptorTests: XCTestCase {
|
114 | 117 |
|
115 | 118 | // MARK: All options
|
116 | 119 |
|
| 120 | + func testAllDescriptorsHaveCorrectKeypath() throws { |
| 121 | + let rulesFile = projectDirectory.appendingPathComponent("Sources/OptionDescriptor.swift") |
| 122 | + let rulesSource = try String(contentsOf: rulesFile, encoding: .utf8) |
| 123 | + let tokens = tokenize(rulesSource) |
| 124 | + let formatter = Formatter(tokens) |
| 125 | + formatter.forEach(.identifier("OptionDescriptor")) { i, _ in |
| 126 | + guard formatter.token(at: i + 1) == .startOfScope("("), |
| 127 | + let endOfScope = formatter.endOfScope(at: i + 1), |
| 128 | + !formatter.tokens[i ..< endOfScope].contains(.stringBody("deprecated")), |
| 129 | + let nameIndex = formatter.index(of: .identifier, before: i), |
| 130 | + case let .identifier(name) = formatter.tokens[nameIndex] |
| 131 | + else { |
| 132 | + return |
| 133 | + } |
| 134 | + guard let keypathIndex = formatter.tokens[i ..< endOfScope].firstIndex(of: .identifier(name)), |
| 135 | + formatter.tokens[keypathIndex - 1].isOperator("."), |
| 136 | + let prevToken = formatter.token(at: keypathIndex - 2), |
| 137 | + [.operator("\\", .prefix), .identifier("FormatOptions")].contains(prevToken) |
| 138 | + else { |
| 139 | + XCTFail("Descriptor for \(name) has incorrect keyPath (must match descriptor name)") |
| 140 | + return |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + |
117 | 145 | func testAllDescriptorsHaveProperty() {
|
118 | 146 | let allProperties = Set(FormatOptions.default.allOptions.keys)
|
119 | 147 | for descriptor in Descriptors.all where !descriptor.isDeprecated {
|
|
0 commit comments