mirror of
https://github.com/nicklockwood/SwiftFormat.git
synced 2026-06-16 10:34:34 +00:00
Test for duplicate specifiers in --specifierorder
This commit is contained in:
1 parent
8c30088a1f
commit
0d3719a692
4 files changed
+36
-1
No files matched your search
@@ -391,6 +391,9 @@ private func processOption(_ key: String,
|
||||
guard !value.isEmpty else {
|
||||
throw FormatError.options("--\(key) option expects a value")
|
||||
}
|
||||
if case let FormatError.options(string) = error {
|
||||
throw FormatError.options("\(string) in --\(key)")
|
||||
}
|
||||
throw FormatError.options("Unsupported --\(key) value '\(value)'")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -208,7 +208,12 @@ extension FormatOptions {
|
||||
type = .array
|
||||
toOptions = { value, options in
|
||||
let values = parseCommaDelimitedList(value)
|
||||
try values.forEach(validate)
|
||||
for (index, value) in values.enumerated() {
|
||||
if values[0 ..< index].contains(value) {
|
||||
throw FormatError.options("Duplicate value '\(value)'")
|
||||
}
|
||||
try validate(value)
|
||||
}
|
||||
options[keyPath: keyPath] = values
|
||||
}
|
||||
fromOptions = { options in
|
||||
|
||||
@@ -639,6 +639,8 @@ class OptionsDescriptorTests: XCTestCase {
|
||||
]
|
||||
validateFromOptions(descriptor, keyPath: \FormatOptions.noSpaceOperators, expectations: fromOptionExpectations)
|
||||
validateArgumentsFreeTextType(descriptor, expectations: validations)
|
||||
var options = FormatOptions()
|
||||
XCTAssertNoThrow(try descriptor.toOptions("+,+", &options))
|
||||
}
|
||||
|
||||
func testNoWrapOperators() {
|
||||
@@ -662,6 +664,30 @@ class OptionsDescriptorTests: XCTestCase {
|
||||
]
|
||||
validateFromOptions(descriptor, keyPath: \FormatOptions.noWrapOperators, expectations: fromOptionExpectations)
|
||||
validateArgumentsFreeTextType(descriptor, expectations: validations)
|
||||
var options = FormatOptions()
|
||||
XCTAssertNoThrow(try descriptor.toOptions("+,+", &options))
|
||||
}
|
||||
|
||||
func testSpecifierOrder() {
|
||||
let descriptor = FormatOptions.Descriptor.specifierOrder
|
||||
let validations: [FreeTextValidationExpectation] = [
|
||||
(input: "public", isValid: true),
|
||||
(input: "", isValid: true),
|
||||
(input: "private(set)", isValid: true),
|
||||
(input: "override", isValid: true),
|
||||
(input: "class", isValid: true),
|
||||
(input: "struct", isValid: false),
|
||||
]
|
||||
let fromOptionExpectations: [OptionArgumentMapping<[String]>] = [
|
||||
(optionValue: [], argumentValue: ""),
|
||||
(optionValue: ["public(set)", "override"], argumentValue: "public(set),override"),
|
||||
]
|
||||
validateFromOptions(descriptor, keyPath: \FormatOptions.specifierOrder, expectations: fromOptionExpectations)
|
||||
validateArgumentsFreeTextType(descriptor, expectations: validations)
|
||||
var options = FormatOptions()
|
||||
XCTAssertThrowsError(try descriptor.toOptions("public,open,public", &options)) { error in
|
||||
XCTAssert(error.localizedDescription.contains("Duplicate"))
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: Deprecated
|
||||
|
||||
@@ -315,6 +315,7 @@ extension OptionsDescriptorTests {
|
||||
("testRemoveBlankLines", testRemoveBlankLines),
|
||||
("testSpaceAroundOperatorDeclarations", testSpaceAroundOperatorDeclarations),
|
||||
("testSpaceAroundRangeOperators", testSpaceAroundRangeOperators),
|
||||
("testSpecifierOrder", testSpecifierOrder),
|
||||
("testStripUnusedArguments", testStripUnusedArguments),
|
||||
("testTrailingCommas", testTrailingCommas),
|
||||
("testTruncateBlankLines", testTruncateBlankLines),
|
||||
|
||||
Reference in new issue
Block a user