From f4e9554a40e51fcbfbbc799cc70d741923cd66e9 Mon Sep 17 00:00:00 2001 From: Nick Lockwood Date: Wed, 11 Jul 2018 09:58:03 +0100 Subject: [PATCH] Infer defaultArgument from FormatOptions struct --- Sources/CommandLine.swift | 4 +- Sources/Options.swift | 4 +- Sources/OptionsDescriptor.swift | 37 ++----------- Sources/OptionsStore.swift | 2 +- Sources/SwiftFormat.swift | 4 +- Tests/CommandLineTests.swift | 6 +-- Tests/FormatterTests.swift | 10 ++-- Tests/OptionsDescriptorTests.swift | 87 +++++++++++++----------------- 8 files changed, 57 insertions(+), 97 deletions(-) diff --git a/Sources/CommandLine.swift b/Sources/CommandLine.swift index 47853a2d..11d458cc 100644 --- a/Sources/CommandLine.swift +++ b/Sources/CommandLine.swift @@ -326,7 +326,7 @@ func processArguments(_ args: [String], in directory: String) -> ExitCode { } if inputURLs.count > 0 { print("inferring swiftformat options from source file(s)...") - var filesParsed = 0, options = FormatOptions(), errors = [Error]() + var filesParsed = 0, options = FormatOptions.default, errors = [Error]() let time = timeEvent { (filesParsed, options, errors) = inferOptions(from: inputURLs, excluding: excludedURLs) } @@ -908,7 +908,7 @@ func fileOptionsFor(_ args: [String: String]) throws -> FileOptions { } func formatOptionsFor(_ args: [String: String]) throws -> FormatOptions { - var options = FormatOptions() + var options = FormatOptions.default var arguments = Set(formatArguments) let optionsToProcess = FormatOptions.Descriptor.formats + FormatOptions.Descriptor.files diff --git a/Sources/Options.swift b/Sources/Options.swift index 3a212604..de3efc26 100644 --- a/Sources/Options.swift +++ b/Sources/Options.swift @@ -140,6 +140,8 @@ public struct FormatOptions: CustomStringConvertible { public var experimentalRules: Bool public var fragment: Bool + public static let `default` = FormatOptions() + // Doesn't really belong here, but hard to put elsewhere public var ignoreConflictMarkers: Bool @@ -221,7 +223,7 @@ public struct FormatOptions: CustomStringConvertible { /// Infer default options by examining the existing source public func inferOptions(from tokens: [Token]) -> FormatOptions { let formatter = Formatter(tokens) - var options = FormatOptions() + var options = FormatOptions.default options.indent = { var indents = [(indent: String, count: Int)]() diff --git a/Sources/OptionsDescriptor.swift b/Sources/OptionsDescriptor.swift index cb3f84ff..77bc333d 100644 --- a/Sources/OptionsDescriptor.swift +++ b/Sources/OptionsDescriptor.swift @@ -43,9 +43,12 @@ extension FormatOptions { let propertyName: String // internal property; ok to change this let name: String let type: ArgumentType - let defaultArgument: String let toOptions: (String, inout FormatOptions) throws -> Void let fromOptions: (FormatOptions) -> String + + var defaultArgument: String { + return fromOptions(FormatOptions.default) + } } } @@ -95,7 +98,6 @@ extension FormatOptions.Descriptor { result = result || Int(input.trimmingCharacters(in: .whitespaces)) != nil return result }), - defaultArgument: "4", toOptions: { input, options in switch input.lowercased() { case "tab", "tabs", "tabbed": @@ -118,7 +120,6 @@ extension FormatOptions.Descriptor { propertyName: "linebreak", name: "Linebreaks Character", type: .list(["cr", "lf", "crlf"]), - defaultArgument: "lf", toOptions: { input, options in switch input.lowercased() { case "cr": @@ -149,7 +150,6 @@ extension FormatOptions.Descriptor { propertyName: "allowInlineSemicolons", name: "Semicolons", type: .binary(true: ["inline"], false: ["never", "false"]), - defaultArgument: "inline", toOptions: { input, options in switch input.lowercased() { case "inline": @@ -167,7 +167,6 @@ extension FormatOptions.Descriptor { propertyName: "spaceAroundRangeOperators", name: "Ranges", type: .binary(true: ["space", "spaced", "spaces"], false: ["nospace"]), - defaultArgument: "space", toOptions: { input, options in switch input.lowercased() { case "space", "spaced", "spaces": @@ -185,7 +184,6 @@ extension FormatOptions.Descriptor { propertyName: "spaceAroundOperatorDeclarations", name: "Operator Func", type: .binary(true: ["space", "spaced", "spaces"], false: ["nospace"]), - defaultArgument: "space", toOptions: { input, options in switch input.lowercased() { case "space", "spaced", "spaces": @@ -203,7 +201,6 @@ extension FormatOptions.Descriptor { propertyName: "useVoid", name: "Empty", type: .binary(true: ["void"], false: ["tuple", "tuples"]), - defaultArgument: "void", toOptions: { input, options in switch input.lowercased() { case "void": @@ -221,7 +218,6 @@ extension FormatOptions.Descriptor { propertyName: "indentCase", name: "Indent Case", type: .binary(true: ["true"], false: ["false"]), - defaultArgument: "false", toOptions: { input, options in switch input.lowercased() { case "true": @@ -239,7 +235,6 @@ extension FormatOptions.Descriptor { propertyName: "trailingCommas", name: "Commas", type: .binary(true: ["always", "true"], false: ["inline", "false"]), - defaultArgument: "always", toOptions: { input, options in switch input.lowercased() { case "always", "true": @@ -257,7 +252,6 @@ extension FormatOptions.Descriptor { propertyName: "indentComments", name: "Comments", type: .binary(true: ["indent", "indented"], false: ["ignore"]), - defaultArgument: "indent", toOptions: { input, options in switch input.lowercased() { case "indent", "indented": @@ -275,7 +269,6 @@ extension FormatOptions.Descriptor { propertyName: "truncateBlankLines", name: "Trim White Space", type: .binary(true: ["always"], false: ["nonblank-lines", "nonblank", "non-blank-lines", "non-blank", "nonempty-lines", "nonempty", "non-empty-lines", "non-empty"]), - defaultArgument: "always", toOptions: { input, options in switch input.lowercased() { case "always": @@ -293,7 +286,6 @@ extension FormatOptions.Descriptor { propertyName: "allmanBraces", name: "Allman Braces", type: .binary(true: ["true", "enabled"], false: ["false", "disabled"]), - defaultArgument: "false", toOptions: { input, options in switch input.lowercased() { case "true", "enabled": @@ -311,7 +303,6 @@ extension FormatOptions.Descriptor { propertyName: "fileHeader", name: "Header", type: .freeText(validationStrategy: { _ in true }), - defaultArgument: "ignore", toOptions: { input, options in switch input.lowercased() { case "strip": @@ -348,7 +339,6 @@ extension FormatOptions.Descriptor { propertyName: "ifdefIndent", name: "Ifdef Indent", type: .list(["indent", "noindent", "outdent"]), - defaultArgument: "indent", toOptions: { input, options in if let mode = IndentMode(rawValue: input.lowercased()) { options.ifdefIndent = mode @@ -363,7 +353,6 @@ extension FormatOptions.Descriptor { propertyName: "wrapArguments", name: "Wrap Arguments", type: .list(["beforefirst", "afterfirst", "disabled"]), - defaultArgument: "disabled", toOptions: { input, options in if let mode = WrapMode(rawValue: input.lowercased()) { options.wrapArguments = mode @@ -378,7 +367,6 @@ extension FormatOptions.Descriptor { propertyName: "wrapCollections", name: "Wrap Collections", type: .list(["beforefirst", "afterfirst", "disabled"]), - defaultArgument: "beforefirst", toOptions: { input, options in if let mode = WrapMode(rawValue: input.lowercased()) { options.wrapCollections = mode @@ -393,7 +381,6 @@ extension FormatOptions.Descriptor { propertyName: "uppercaseHex", name: "Hex Literal Case", type: .binary(true: ["uppercase", "upper"], false: ["lowercase", "lower"]), - defaultArgument: "uppercase", toOptions: { input, options in switch input.lowercased() { case "uppercase", "upper": @@ -411,7 +398,6 @@ extension FormatOptions.Descriptor { propertyName: "uppercaseExponent", name: "Exponent Case", type: .binary(true: ["uppercase", "upper"], false: ["lowercase", "lower"]), - defaultArgument: "lowercase", toOptions: { input, options in switch input.lowercased() { case "uppercase", "upper": @@ -429,7 +415,6 @@ extension FormatOptions.Descriptor { propertyName: "decimalGrouping", name: "Decimal Grouping", type: .freeText(validationStrategy: { Grouping(rawValue: $0) != nil }), - defaultArgument: "3,6", toOptions: { input, options in guard let grouping = Grouping(rawValue: input.lowercased()) else { throw FormatError.options("") @@ -443,7 +428,6 @@ extension FormatOptions.Descriptor { propertyName: "binaryGrouping", name: "Binary Grouping", type: .freeText(validationStrategy: { Grouping(rawValue: $0) != nil }), - defaultArgument: "4,8", toOptions: { input, options in guard let grouping = Grouping(rawValue: input.lowercased()) else { throw FormatError.options("") @@ -457,7 +441,6 @@ extension FormatOptions.Descriptor { propertyName: "octalGrouping", name: "Octal Grouping", type: .freeText(validationStrategy: { Grouping(rawValue: $0) != nil }), - defaultArgument: "4,8", toOptions: { input, options in guard let grouping = Grouping(rawValue: input.lowercased()) else { throw FormatError.options("") @@ -471,7 +454,6 @@ extension FormatOptions.Descriptor { propertyName: "hexGrouping", name: "Hex Grouping", type: .freeText(validationStrategy: { Grouping(rawValue: $0) != nil }), - defaultArgument: "4,8", toOptions: { input, options in guard let grouping = Grouping(rawValue: input.lowercased()) else { throw FormatError.options("") @@ -485,7 +467,6 @@ extension FormatOptions.Descriptor { propertyName: "hoistPatternLet", name: "Pattern Let", type: .binary(true: ["hoist"], false: ["inline"]), - defaultArgument: "hoist", toOptions: { input, options in switch input.lowercased() { case "hoist": @@ -503,7 +484,6 @@ extension FormatOptions.Descriptor { propertyName: "stripUnusedArguments", name: "Strip Unused Arguments", type: .list(["unnamed-only", "closure-only", "always"]), - defaultArgument: "always", toOptions: { input, options in guard let type = ArgumentStrippingMode(rawValue: input.lowercased()) else { throw FormatError.options("") @@ -517,7 +497,6 @@ extension FormatOptions.Descriptor { propertyName: "elseOnNextLine", name: "Else Position", type: .binary(true: ["next-line", "nextline"], false: ["same-line", "sameline"]), - defaultArgument: "same-line", toOptions: { input, options in switch input.lowercased() { case "nextline", "next-line": @@ -535,7 +514,6 @@ extension FormatOptions.Descriptor { propertyName: "removeSelf", name: "Self", type: .binary(true: ["remove"], false: ["insert"]), - defaultArgument: "remove", toOptions: { input, options in switch input.lowercased() { case "remove": @@ -553,7 +531,6 @@ extension FormatOptions.Descriptor { propertyName: "experimentalRules", name: "Experimental Rules", type: .binary(true: ["enabled", "true"], false: ["disabled", "false"]), - defaultArgument: "disabled", toOptions: { input, options in switch input.lowercased() { case "enabled", "true": @@ -571,7 +548,6 @@ extension FormatOptions.Descriptor { propertyName: "fragment", name: "Fragment", type: .binary(true: ["true", "enabled"], false: ["false", "disabled"]), - defaultArgument: "false", toOptions: { input, options in switch input.lowercased() { case "true", "enabled": @@ -589,7 +565,6 @@ extension FormatOptions.Descriptor { propertyName: "ignoreConflictMarkers", name: "Conflict Markers", type: .binary(true: ["ignore", "true", "enabled"], false: ["reject", "false", "disabled"]), - defaultArgument: "reject", toOptions: { input, options in switch input.lowercased() { case "ignore", "true", "enabled": @@ -630,7 +605,6 @@ extension FormatOptions.Descriptor { propertyName: "insertBlankLines", name: "Insert Lines", type: .binary(true: ["enabled", "true"], false: ["disabled", "false"]), - defaultArgument: "enabled", toOptions: { input, options in switch input.lowercased() { case "enabled", "true": @@ -648,7 +622,6 @@ extension FormatOptions.Descriptor { propertyName: "removeBlankLines", name: "Remove Lines", type: .binary(true: ["enabled", "true"], false: ["disabled", "false"]), - defaultArgument: "enabled", toOptions: { input, options in switch input.lowercased() { case "enabled", "true": @@ -666,7 +639,6 @@ extension FormatOptions.Descriptor { propertyName: "hexLiteralCase", name: "hexliterals", type: .binary(true: ["uppercase", "upper"], false: ["lowercase", "lower"]), - defaultArgument: "uppercase", toOptions: { input, options in switch input.lowercased() { case "uppercase", "upper": @@ -684,7 +656,6 @@ extension FormatOptions.Descriptor { propertyName: "wrapCollections", name: "Wrap Elements", type: .list(["beforefirst", "afterfirst", "disabled"]), - defaultArgument: "beforefirst", toOptions: { input, options in if let mode = WrapMode(rawValue: input.lowercased()) { options.wrapCollections = mode diff --git a/Sources/OptionsStore.swift b/Sources/OptionsStore.swift index d60e9afd..e059cf9b 100644 --- a/Sources/OptionsStore.swift +++ b/Sources/OptionsStore.swift @@ -110,7 +110,7 @@ struct OptionsStore { var formatOptions: FormatOptions { let allOptions = options - var formatOptions = FormatOptions() + var formatOptions = FormatOptions.default allOptions.forEach { try! $0.descriptor.toOptions($0.argumentValue, &formatOptions) } return formatOptions } diff --git a/Sources/SwiftFormat.swift b/Sources/SwiftFormat.swift index 74deba4f..5c9339be 100644 --- a/Sources/SwiftFormat.swift +++ b/Sources/SwiftFormat.swift @@ -254,13 +254,13 @@ public func applyRules(_ rules: [FormatRule], /// Returns the formatted token array, and the number of edits made public func format(_ tokens: [Token], rules: [FormatRule] = FormatRules.default, - options: FormatOptions = FormatOptions()) throws -> [Token] { + options: FormatOptions = .default) throws -> [Token] { return try applyRules(rules, to: tokens, with: options) } /// Format code with specified rules and options public func format(_ source: String, rules: [FormatRule] = FormatRules.default, - options: FormatOptions = FormatOptions()) throws -> String { + options: FormatOptions = .default) throws -> String { return sourceCode(for: try format(tokenize(source), rules: rules, options: options)) } diff --git a/Tests/CommandLineTests.swift b/Tests/CommandLineTests.swift index 0306b5f9..34b9ef6c 100644 --- a/Tests/CommandLineTests.swift +++ b/Tests/CommandLineTests.swift @@ -108,14 +108,14 @@ class CommandLineTests: XCTestCase { // MARK: format options to arguments func testCommandLineArgumentsHaveValidNames() { - let arguments = commandLineArguments(for: FormatOptions()) + let arguments = commandLineArguments(for: .default) for key in arguments.keys { XCTAssertTrue(commandLineArguments.contains(key), "\(key) is not a valid argument name") } } func testCommandLineArgumentsAreCorrect() { - let options = FormatOptions() + let options = FormatOptions.default let output = ["allman": "false", "wraparguments": "disabled", "wrapcollections": "beforefirst", "self": "remove", "header": "ignore", "binarygrouping": "4,8", "octalgrouping": "4,8", "patternlet": "hoist", "indentcase": "false", "trimwhitespace": "always", "decimalgrouping": "3,6", "commas": "always", "semicolons": "inline", "indent": "4", "exponentcase": "lowercase", "operatorfunc": "spaced", "elseposition": "same-line", "empty": "void", "ranges": "spaced", "hexliteralcase": "uppercase", "linebreaks": "lf", "hexgrouping": "4,8", "comments": "indent", "ifdef": "indent", "stripunusedargs": "always", "experimental": "disabled", "fragment": "false", "conflictmarkers": "reject"] XCTAssertEqual(commandLineArguments(for: options), output) } @@ -125,7 +125,7 @@ class CommandLineTests: XCTestCase { func testFormatArgumentsAreAllImplemented() { CLI.print = { _, _ in } for key in formatArguments { - guard let value = commandLineArguments(for: FormatOptions())[key] else { + guard let value = commandLineArguments(for: .default)[key] else { XCTAssert(deprecatedArguments.contains(key)) continue } diff --git a/Tests/FormatterTests.swift b/Tests/FormatterTests.swift index 25055ce4..1f023579 100644 --- a/Tests/FormatterTests.swift +++ b/Tests/FormatterTests.swift @@ -40,7 +40,7 @@ class FormatterTests: XCTestCase { .identifier("baz"), ] var output: [Token] = [] - let formatter = Formatter(input, options: FormatOptions()) + let formatter = Formatter(input, options: .default) formatter.forEachToken { i, token in output.append(token) if i == 1 { @@ -57,7 +57,7 @@ class FormatterTests: XCTestCase { .identifier("baz"), ] var output: [Token] = [] - let formatter = Formatter(input, options: FormatOptions()) + let formatter = Formatter(input, options: .default) formatter.forEachToken { i, token in output.append(token) if i == 1 { @@ -74,7 +74,7 @@ class FormatterTests: XCTestCase { .identifier("baz"), ] var output: [Token] = [] - let formatter = Formatter(input, options: FormatOptions()) + let formatter = Formatter(input, options: .default) formatter.forEachToken { i, token in output.append(token) if i == 1 { @@ -92,7 +92,7 @@ class FormatterTests: XCTestCase { .commentBody("bar"), .linebreak("\n"), ] - let formatter = Formatter(input, options: FormatOptions()) + let formatter = Formatter(input, options: .default) let index = formatter.index(before: 4, where: { !$0.isSpaceOrComment }) XCTAssertEqual(index, 0) } @@ -107,7 +107,7 @@ class FormatterTests: XCTestCase { .endOfScope("*/"), .linebreak("\n"), ] - let formatter = Formatter(input, options: FormatOptions()) + let formatter = Formatter(input, options: .default) let index = formatter.index(before: 6, where: { !$0.isSpaceOrComment }) XCTAssertEqual(index, 0) } diff --git a/Tests/OptionsDescriptorTests.swift b/Tests/OptionsDescriptorTests.swift index bcf727d3..36a672e9 100644 --- a/Tests/OptionsDescriptorTests.swift +++ b/Tests/OptionsDescriptorTests.swift @@ -25,7 +25,7 @@ class OptionsDescriptorTests: XCTestCase { private func validateDescriptorThrowsOptionsError(_ descriptor: FormatOptions.Descriptor, invalidArguments: String = "invalid", testName: String = #function) { - var options = FormatOptions() + var options = FormatOptions.default XCTAssertThrowsError(try descriptor.toOptions(invalidArguments, &options), "\(testName): Invalid format Throws") { err in guard case FormatError.options = err else { @@ -39,7 +39,7 @@ class OptionsDescriptorTests: XCTestCase { keyPath: WritableKeyPath, expectations: [OptionArgumentMapping], testName: String = #function) { - var options = FormatOptions() + var options = FormatOptions.default expectations.forEach { try! descriptor.toOptions($0.argumentValue, &options) XCTAssertEqual(options[keyPath: keyPath], $0.optionValue, "\(testName): Argument \($0.argumentValue) map to option \($0.optionValue)") @@ -55,7 +55,7 @@ class OptionsDescriptorTests: XCTestCase { expectations: [OptionArgumentMapping], testCaseVariation: Bool = true, testName: String = #function) { - var options = FormatOptions() + var options = FormatOptions.default expectations.forEach { try! descriptor.toOptions($0.argumentValue, &options) XCTAssertEqual(options[keyPath: keyPath], $0.optionValue, "\(testName): Argument \($0.argumentValue) map to option \(String(describing: $0.optionValue))") @@ -71,7 +71,7 @@ class OptionsDescriptorTests: XCTestCase { /// Validate From FormatOptions to Argument String /// /// - Parameters: - /// - sut: System Under Test + /// - descriptor: FormatOptions.Descriptor being tested /// - keyPath: to the FormatOptions property that is beeing validated /// - expectations: Array of expectations for different inputs /// - invalid: Provide if an invalid input can be store in FormatOptions. In Which case the default value should be return instead @@ -81,7 +81,7 @@ class OptionsDescriptorTests: XCTestCase { expectations: [OptionArgumentMapping], invalid: T? = nil, testName: String = #function) { - var options = FormatOptions() + var options = FormatOptions.default for item in expectations { options[keyPath: keyPath] = item.optionValue XCTAssertEqual(descriptor.fromOptions(options), item.argumentValue, "\(testName): Option is transformed to argument") @@ -96,13 +96,9 @@ class OptionsDescriptorTests: XCTestCase { private func validateArgumentsBinaryType(_ descriptor: FormatOptions.Descriptor, controlTrue: [String], controlFalse: [String], - default: Bool, testName: String = #function) { let values: (true: [String], false: [String]) = descriptor.type.associatedValue() - let defaultControl = `default` ? controlTrue : controlFalse - XCTAssertTrue(defaultControl.contains(descriptor.defaultArgument), "\(testName): Default argument map to \(`default`)") - XCTAssertEqual(values.true[0], controlTrue[0], "\(testName): First item is prefered parameter name") XCTAssertEqual(values.false[0], controlFalse[0], "\(testName): First item is prefered parameter name") XCTAssertEqual(Set(values.true), Set(controlTrue), "\(testName): All possible true value have representation") @@ -120,12 +116,10 @@ class OptionsDescriptorTests: XCTestCase { private func validateArgumentsListType(_ descriptor: FormatOptions.Descriptor, validArguments: Set, - default: String, testName: String = #function) { let values: [String] = descriptor.type.associatedValue() XCTAssertEqual(Set(values), validArguments, "\(testName): All valid arguments are accounted for") - XCTAssertEqual(descriptor.defaultArgument, `default`, "\(testName): Default argument is \(`default`)") XCTAssertTrue(validArguments.contains(descriptor.defaultArgument), "\(testName): Default argument is part of the valide arguments") } @@ -133,14 +127,12 @@ class OptionsDescriptorTests: XCTestCase { private func validateArgumentsFreeTextType(_ descriptor: FormatOptions.Descriptor, expectations: [FreeTextValidationExpectation], - default: String, testName: String = #function) { guard case let FormatOptions.Descriptor.ArgumentType.freeText(validator) = descriptor.type else { XCTAssert(false) return } - XCTAssertEqual(descriptor.defaultArgument, `default`, "\(testName): Default Argument value is: \(`default`)") expectations.forEach { XCTAssert(validator($0.input) == $0.isValid, "\(testName): \($0.input) isValid: \($0.isValid)") } @@ -150,7 +142,6 @@ class OptionsDescriptorTests: XCTestCase { name: String, argumentName: String, propertyName: String, - default: String, keyPath: WritableKeyPath, testName: String = #function) { let expectations: [FreeTextValidationExpectation] = [ @@ -176,7 +167,7 @@ class OptionsDescriptorTests: XCTestCase { ] validateDescriptor(descriptor, name: name, argumentName: argumentName, propertyName: propertyName, testName: testName) - validateArgumentsFreeTextType(descriptor, expectations: expectations, default: `default`, testName: testName) + validateArgumentsFreeTextType(descriptor, expectations: expectations, testName: testName) validateFromOptions(descriptor, keyPath: keyPath, expectations: fromOptionExpectations, testName: testName) validateFromArguments(descriptor, keyPath: keyPath, expectations: fromArgumentExpectations, testName: testName) validateDescriptorThrowsOptionsError(descriptor, testName: testName) @@ -185,7 +176,7 @@ class OptionsDescriptorTests: XCTestCase { // MARK: All options func testAllPropertyHaveDescriptor() { - let allProperties = Set(FormatOptions().allOptions.keys) + let allProperties = Set(FormatOptions.default.allOptions.keys) let allDescriptorWithProperty = FormatOptions.Descriptor.deprecatedWithProperty + FormatOptions.Descriptor.formats + FormatOptions.Descriptor.files let allDescriptorProperty = Set(allDescriptorWithProperty.map { $0.propertyName }) @@ -197,8 +188,8 @@ class OptionsDescriptorTests: XCTestCase { func testDeprecatedPropertyList() { let deprecated = FormatOptions.Descriptor.deprecated let controlArgumentNames = Set(["insertlines", "removelines", "hexliterals", "wrapelements"]) - let sutArgumentNames = Set(deprecated.map { $0.argumentName }) - XCTAssertEqual(sutArgumentNames, controlArgumentNames, "All deprecated name are represented by a descriptor") + let descriptorArgumentNames = Set(deprecated.map { $0.argumentName }) + XCTAssertEqual(descriptorArgumentNames, controlArgumentNames, "All deprecated name are represented by a descriptor") } // MARK: Individual options @@ -210,7 +201,7 @@ class OptionsDescriptorTests: XCTestCase { (optionValue: true, argumentValue: "void"), ] validateDescriptor(descriptor, name: "Empty", argumentName: "empty", propertyName: "useVoid") - validateArgumentsBinaryType(descriptor, controlTrue: ["void"], controlFalse: ["tuple", "tuples"], default: true) + validateArgumentsBinaryType(descriptor, controlTrue: ["void"], controlFalse: ["tuple", "tuples"]) validateFromOptions(descriptor, keyPath: \FormatOptions.useVoid, expectations: fromOptionsExpectation) validateFromArgumentsBinaryType(descriptor, keyPath: \FormatOptions.useVoid) validateDescriptorThrowsOptionsError(descriptor) @@ -223,7 +214,7 @@ class OptionsDescriptorTests: XCTestCase { (optionValue: true, argumentValue: "inline"), ] validateDescriptor(descriptor, name: "Semicolons", argumentName: "semicolons", propertyName: "allowInlineSemicolons") - validateArgumentsBinaryType(descriptor, controlTrue: ["inline"], controlFalse: ["never", "false"], default: true) + validateArgumentsBinaryType(descriptor, controlTrue: ["inline"], controlFalse: ["never", "false"]) validateFromOptions(descriptor, keyPath: \FormatOptions.allowInlineSemicolons, expectations: fromOptionsExpectation) validateFromArgumentsBinaryType(descriptor, keyPath: \FormatOptions.allowInlineSemicolons) validateDescriptorThrowsOptionsError(descriptor) @@ -236,7 +227,7 @@ class OptionsDescriptorTests: XCTestCase { (optionValue: false, argumentValue: "nospace"), ] validateDescriptor(descriptor, name: "Ranges", argumentName: "ranges", propertyName: "spaceAroundRangeOperators") - validateArgumentsBinaryType(descriptor, controlTrue: ["space", "spaced", "spaces"], controlFalse: ["nospace"], default: true) + validateArgumentsBinaryType(descriptor, controlTrue: ["space", "spaced", "spaces"], controlFalse: ["nospace"]) validateFromOptions(descriptor, keyPath: \FormatOptions.spaceAroundRangeOperators, expectations: fromOptionsExpectation) validateFromArgumentsBinaryType(descriptor, keyPath: \FormatOptions.spaceAroundRangeOperators) validateDescriptorThrowsOptionsError(descriptor) @@ -249,7 +240,7 @@ class OptionsDescriptorTests: XCTestCase { (optionValue: false, argumentValue: "nospace"), ] validateDescriptor(descriptor, name: "Operator Func", argumentName: "operatorfunc", propertyName: "spaceAroundOperatorDeclarations") - validateArgumentsBinaryType(descriptor, controlTrue: ["space", "spaced", "spaces"], controlFalse: ["nospace"], default: true) + validateArgumentsBinaryType(descriptor, controlTrue: ["space", "spaced", "spaces"], controlFalse: ["nospace"]) validateFromOptions(descriptor, keyPath: \FormatOptions.spaceAroundOperatorDeclarations, expectations: fromOptionsExpectation) validateFromArgumentsBinaryType(descriptor, keyPath: \FormatOptions.spaceAroundOperatorDeclarations) validateDescriptorThrowsOptionsError(descriptor) @@ -262,7 +253,7 @@ class OptionsDescriptorTests: XCTestCase { (optionValue: false, argumentValue: "false"), ] validateDescriptor(descriptor, name: "Indent Case", argumentName: "indentcase", propertyName: "indentCase") - validateArgumentsBinaryType(descriptor, controlTrue: ["true"], controlFalse: ["false"], default: false) + validateArgumentsBinaryType(descriptor, controlTrue: ["true"], controlFalse: ["false"]) validateFromOptions(descriptor, keyPath: \FormatOptions.indentCase, expectations: fromOptionsExpectation) validateFromArgumentsBinaryType(descriptor, keyPath: \FormatOptions.indentCase) validateDescriptorThrowsOptionsError(descriptor) @@ -275,7 +266,7 @@ class OptionsDescriptorTests: XCTestCase { (optionValue: false, argumentValue: "inline"), ] validateDescriptor(descriptor, name: "Commas", argumentName: "commas", propertyName: "trailingCommas") - validateArgumentsBinaryType(descriptor, controlTrue: ["always", "true"], controlFalse: ["inline", "false"], default: true) + validateArgumentsBinaryType(descriptor, controlTrue: ["always", "true"], controlFalse: ["inline", "false"]) validateFromOptions(descriptor, keyPath: \FormatOptions.trailingCommas, expectations: fromOptionsExpectation) validateFromArgumentsBinaryType(descriptor, keyPath: \FormatOptions.trailingCommas) validateDescriptorThrowsOptionsError(descriptor) @@ -288,7 +279,7 @@ class OptionsDescriptorTests: XCTestCase { (optionValue: false, argumentValue: "ignore"), ] validateDescriptor(descriptor, name: "Comments", argumentName: "comments", propertyName: "indentComments") - validateArgumentsBinaryType(descriptor, controlTrue: ["indent", "indented"], controlFalse: ["ignore"], default: true) + validateArgumentsBinaryType(descriptor, controlTrue: ["indent", "indented"], controlFalse: ["ignore"]) validateFromOptions(descriptor, keyPath: \FormatOptions.indentComments, expectations: fromOptionsExpectation) validateFromArgumentsBinaryType(descriptor, keyPath: \FormatOptions.indentComments) validateDescriptorThrowsOptionsError(descriptor) @@ -301,7 +292,7 @@ class OptionsDescriptorTests: XCTestCase { (optionValue: false, argumentValue: "nonblank-lines"), ] validateDescriptor(descriptor, name: "Trim White Space", argumentName: "trimwhitespace", propertyName: "truncateBlankLines") - validateArgumentsBinaryType(descriptor, controlTrue: ["always"], controlFalse: ["nonblank-lines", "nonblank", "non-blank-lines", "non-blank", "nonempty-lines", "nonempty", "non-empty-lines", "non-empty"], default: true) + validateArgumentsBinaryType(descriptor, controlTrue: ["always"], controlFalse: ["nonblank-lines", "nonblank", "non-blank-lines", "non-blank", "nonempty-lines", "nonempty", "non-empty-lines", "non-empty"]) validateFromOptions(descriptor, keyPath: \FormatOptions.truncateBlankLines, expectations: fromOptionsExpectation) validateFromArgumentsBinaryType(descriptor, keyPath: \FormatOptions.truncateBlankLines) validateDescriptorThrowsOptionsError(descriptor) @@ -314,7 +305,7 @@ class OptionsDescriptorTests: XCTestCase { (optionValue: false, argumentValue: "false"), ] validateDescriptor(descriptor, name: "Allman Braces", argumentName: "allman", propertyName: "allmanBraces") - validateArgumentsBinaryType(descriptor, controlTrue: ["true", "enabled"], controlFalse: ["false", "disabled"], default: false) + validateArgumentsBinaryType(descriptor, controlTrue: ["true", "enabled"], controlFalse: ["false", "disabled"]) validateFromOptions(descriptor, keyPath: \FormatOptions.allmanBraces, expectations: fromOptionsExpectation) validateFromArgumentsBinaryType(descriptor, keyPath: \FormatOptions.allmanBraces) validateDescriptorThrowsOptionsError(descriptor) @@ -327,7 +318,7 @@ class OptionsDescriptorTests: XCTestCase { (optionValue: false, argumentValue: "lowercase"), ] validateDescriptor(descriptor, name: "Hex Literal Case", argumentName: "hexliteralcase", propertyName: "uppercaseHex") - validateArgumentsBinaryType(descriptor, controlTrue: ["uppercase", "upper"], controlFalse: ["lowercase", "lower"], default: true) + validateArgumentsBinaryType(descriptor, controlTrue: ["uppercase", "upper"], controlFalse: ["lowercase", "lower"]) validateFromOptions(descriptor, keyPath: \FormatOptions.uppercaseHex, expectations: fromOptionsExpectation) validateFromArgumentsBinaryType(descriptor, keyPath: \FormatOptions.uppercaseHex) validateDescriptorThrowsOptionsError(descriptor) @@ -340,7 +331,7 @@ class OptionsDescriptorTests: XCTestCase { (optionValue: false, argumentValue: "lowercase"), ] validateDescriptor(descriptor, name: "Exponent Case", argumentName: "exponentcase", propertyName: "uppercaseExponent") - validateArgumentsBinaryType(descriptor, controlTrue: ["uppercase", "upper"], controlFalse: ["lowercase", "lower"], default: false) + validateArgumentsBinaryType(descriptor, controlTrue: ["uppercase", "upper"], controlFalse: ["lowercase", "lower"]) validateFromOptions(descriptor, keyPath: \FormatOptions.uppercaseExponent, expectations: fromOptionsExpectation) validateFromArgumentsBinaryType(descriptor, keyPath: \FormatOptions.uppercaseExponent) validateDescriptorThrowsOptionsError(descriptor) @@ -353,7 +344,7 @@ class OptionsDescriptorTests: XCTestCase { (optionValue: false, argumentValue: "inline"), ] validateDescriptor(descriptor, name: "Pattern Let", argumentName: "patternlet", propertyName: "hoistPatternLet") - validateArgumentsBinaryType(descriptor, controlTrue: ["hoist"], controlFalse: ["inline"], default: true) + validateArgumentsBinaryType(descriptor, controlTrue: ["hoist"], controlFalse: ["inline"]) validateFromOptions(descriptor, keyPath: \FormatOptions.hoistPatternLet, expectations: fromOptionsExpectation) validateFromArgumentsBinaryType(descriptor, keyPath: \FormatOptions.hoistPatternLet) validateDescriptorThrowsOptionsError(descriptor) @@ -366,7 +357,7 @@ class OptionsDescriptorTests: XCTestCase { (optionValue: false, argumentValue: "same-line"), ] validateDescriptor(descriptor, name: "Else Position", argumentName: "elseposition", propertyName: "elseOnNextLine") - validateArgumentsBinaryType(descriptor, controlTrue: ["next-line", "nextline"], controlFalse: ["same-line", "sameline"], default: false) + validateArgumentsBinaryType(descriptor, controlTrue: ["next-line", "nextline"], controlFalse: ["same-line", "sameline"]) validateFromOptions(descriptor, keyPath: \FormatOptions.elseOnNextLine, expectations: fromOptionsExpectation) validateFromArgumentsBinaryType(descriptor, keyPath: \FormatOptions.elseOnNextLine) validateDescriptorThrowsOptionsError(descriptor) @@ -379,7 +370,7 @@ class OptionsDescriptorTests: XCTestCase { (optionValue: false, argumentValue: "insert"), ] validateDescriptor(descriptor, name: "Self", argumentName: "self", propertyName: "removeSelf") - validateArgumentsBinaryType(descriptor, controlTrue: ["remove"], controlFalse: ["insert"], default: true) + validateArgumentsBinaryType(descriptor, controlTrue: ["remove"], controlFalse: ["insert"]) validateFromOptions(descriptor, keyPath: \FormatOptions.removeSelf, expectations: fromOptionsExpectation) validateFromArgumentsBinaryType(descriptor, keyPath: \FormatOptions.removeSelf) validateDescriptorThrowsOptionsError(descriptor) @@ -392,7 +383,7 @@ class OptionsDescriptorTests: XCTestCase { (optionValue: false, argumentValue: "disabled"), ] validateDescriptor(descriptor, name: "Experimental Rules", argumentName: "experimental", propertyName: "experimentalRules") - validateArgumentsBinaryType(descriptor, controlTrue: ["enabled", "true"], controlFalse: ["disabled", "false"], default: false) + validateArgumentsBinaryType(descriptor, controlTrue: ["enabled", "true"], controlFalse: ["disabled", "false"]) validateFromOptions(descriptor, keyPath: \FormatOptions.experimentalRules, expectations: fromOptionsExpectation) validateFromArgumentsBinaryType(descriptor, keyPath: \FormatOptions.experimentalRules) validateDescriptorThrowsOptionsError(descriptor) @@ -405,7 +396,7 @@ class OptionsDescriptorTests: XCTestCase { (optionValue: false, argumentValue: "false"), ] validateDescriptor(descriptor, name: "Fragment", argumentName: "fragment", propertyName: "fragment") - validateArgumentsBinaryType(descriptor, controlTrue: ["true", "enabled"], controlFalse: ["false", "disabled"], default: false) + validateArgumentsBinaryType(descriptor, controlTrue: ["true", "enabled"], controlFalse: ["false", "disabled"]) validateFromOptions(descriptor, keyPath: \FormatOptions.fragment, expectations: fromOptionsExpectation) validateFromArgumentsBinaryType(descriptor, keyPath: \FormatOptions.fragment) validateDescriptorThrowsOptionsError(descriptor) @@ -418,7 +409,7 @@ class OptionsDescriptorTests: XCTestCase { (optionValue: false, argumentValue: "reject"), ] validateDescriptor(descriptor, name: "Conflict Markers", argumentName: "conflictmarkers", propertyName: "ignoreConflictMarkers") - validateArgumentsBinaryType(descriptor, controlTrue: ["ignore", "true", "enabled"], controlFalse: ["reject", "false", "disabled"], default: false) + validateArgumentsBinaryType(descriptor, controlTrue: ["ignore", "true", "enabled"], controlFalse: ["reject", "false", "disabled"]) validateFromOptions(descriptor, keyPath: \FormatOptions.ignoreConflictMarkers, expectations: fromOptionsExpectation) validateFromArgumentsBinaryType(descriptor, keyPath: \FormatOptions.ignoreConflictMarkers) validateDescriptorThrowsOptionsError(descriptor) @@ -433,7 +424,7 @@ class OptionsDescriptorTests: XCTestCase { ] validateDescriptor(descriptor, name: "Ifdef Indent", argumentName: "ifdef", propertyName: "ifdefIndent") - validateArgumentsListType(descriptor, validArguments: ["indent", "noindent", "outdent"], default: "indent") + validateArgumentsListType(descriptor, validArguments: ["indent", "noindent", "outdent"]) validateFromOptions(descriptor, keyPath: \FormatOptions.ifdefIndent, expectations: expectedMapping) validateFromArguments(descriptor, keyPath: \FormatOptions.ifdefIndent, expectations: expectedMapping) validateDescriptorThrowsOptionsError(descriptor) @@ -447,7 +438,7 @@ class OptionsDescriptorTests: XCTestCase { (optionValue: "\r\n", argumentValue: "crlf"), ] validateDescriptor(descriptor, name: "Linebreaks Character", argumentName: "linebreaks", propertyName: "linebreak") - validateArgumentsListType(descriptor, validArguments: ["cr", "lf", "crlf"], default: "lf") + validateArgumentsListType(descriptor, validArguments: ["cr", "lf", "crlf"]) validateFromOptions(descriptor, keyPath: \FormatOptions.linebreak, expectations: expectedMapping, invalid: "invalid") validateFromArguments(descriptor, keyPath: \FormatOptions.linebreak, expectations: expectedMapping) validateDescriptorThrowsOptionsError(descriptor) @@ -461,7 +452,7 @@ class OptionsDescriptorTests: XCTestCase { (optionValue: .disabled, argumentValue: "disabled"), ] validateDescriptor(descriptor, name: "Wrap Arguments", argumentName: "wraparguments", propertyName: "wrapArguments") - validateArgumentsListType(descriptor, validArguments: ["beforefirst", "afterfirst", "disabled"], default: "disabled") + validateArgumentsListType(descriptor, validArguments: ["beforefirst", "afterfirst", "disabled"]) validateFromOptions(descriptor, keyPath: \FormatOptions.wrapArguments, expectations: expectedMapping) validateFromArguments(descriptor, keyPath: \FormatOptions.wrapArguments, expectations: expectedMapping) validateDescriptorThrowsOptionsError(descriptor) @@ -475,7 +466,7 @@ class OptionsDescriptorTests: XCTestCase { (optionValue: .disabled, argumentValue: "disabled"), ] validateDescriptor(descriptor, name: "Wrap Collections", argumentName: "wrapcollections", propertyName: "wrapCollections") - validateArgumentsListType(descriptor, validArguments: ["beforefirst", "afterfirst", "disabled"], default: "beforefirst") + validateArgumentsListType(descriptor, validArguments: ["beforefirst", "afterfirst", "disabled"]) validateFromOptions(descriptor, keyPath: \FormatOptions.wrapCollections, expectations: expectedMapping) validateFromArguments(descriptor, keyPath: \FormatOptions.wrapCollections, expectations: expectedMapping) validateDescriptorThrowsOptionsError(descriptor) @@ -489,7 +480,7 @@ class OptionsDescriptorTests: XCTestCase { (optionValue: .all, argumentValue: "always"), ] validateDescriptor(descriptor, name: "Strip Unused Arguments", argumentName: "stripunusedargs", propertyName: "stripUnusedArguments") - validateArgumentsListType(descriptor, validArguments: ["unnamed-only", "closure-only", "always"], default: "always") + validateArgumentsListType(descriptor, validArguments: ["unnamed-only", "closure-only", "always"]) validateFromOptions(descriptor, keyPath: \FormatOptions.stripUnusedArguments, expectations: expectedMapping) validateFromArguments(descriptor, keyPath: \FormatOptions.stripUnusedArguments, expectations: expectedMapping) validateDescriptorThrowsOptionsError(descriptor) @@ -500,7 +491,6 @@ class OptionsDescriptorTests: XCTestCase { name: "Decimal Grouping", argumentName: "decimalgrouping", propertyName: "decimalGrouping", - default: "3,6", keyPath: \FormatOptions.decimalGrouping) } @@ -509,7 +499,6 @@ class OptionsDescriptorTests: XCTestCase { name: "Binary Grouping", argumentName: "binarygrouping", propertyName: "binaryGrouping", - default: "4,8", keyPath: \FormatOptions.binaryGrouping) } @@ -518,7 +507,6 @@ class OptionsDescriptorTests: XCTestCase { name: "Octal Grouping", argumentName: "octalgrouping", propertyName: "octalGrouping", - default: "4,8", keyPath: \FormatOptions.octalGrouping) } @@ -527,7 +515,6 @@ class OptionsDescriptorTests: XCTestCase { name: "Hex Grouping", argumentName: "hexgrouping", propertyName: "hexGrouping", - default: "4,8", keyPath: \FormatOptions.hexGrouping) } @@ -564,7 +551,7 @@ class OptionsDescriptorTests: XCTestCase { ] validateDescriptor(descriptor, name: "Indent", argumentName: "indent", propertyName: "indent") - validateArgumentsFreeTextType(descriptor, expectations: validations, default: "4") + validateArgumentsFreeTextType(descriptor, expectations: validations) validateFromOptions(descriptor, keyPath: \FormatOptions.indent, expectations: fromOptionExpectations) validateFromArguments(descriptor, keyPath: \FormatOptions.indent, expectations: fromArgumentExpectations) validateDescriptorThrowsOptionsError(descriptor) @@ -602,7 +589,7 @@ class OptionsDescriptorTests: XCTestCase { ] validateDescriptor(descriptor, name: "Header", argumentName: "header", propertyName: "fileHeader") - validateArgumentsFreeTextType(descriptor, expectations: validations, default: "ignore") + validateArgumentsFreeTextType(descriptor, expectations: validations) validateFromOptions(descriptor, keyPath: \FormatOptions.fileHeader, expectations: fromOptionExpectations) validateFromOptionalArguments(descriptor, keyPath: \FormatOptions.fileHeader, expectations: fromArgumentExpectations, testCaseVariation: false) } @@ -616,7 +603,7 @@ class OptionsDescriptorTests: XCTestCase { (optionValue: false, argumentValue: "disabled"), ] validateDescriptor(descriptor, name: "Insert Lines", argumentName: "insertlines", propertyName: "insertBlankLines") - validateArgumentsBinaryType(descriptor, controlTrue: ["enabled", "true"], controlFalse: ["disabled", "false"], default: true) + validateArgumentsBinaryType(descriptor, controlTrue: ["enabled", "true"], controlFalse: ["disabled", "false"]) validateFromOptions(descriptor, keyPath: \FormatOptions.insertBlankLines, expectations: fromOptionsExpectation) validateFromArgumentsBinaryType(descriptor, keyPath: \FormatOptions.insertBlankLines) validateDescriptorThrowsOptionsError(descriptor) @@ -629,7 +616,7 @@ class OptionsDescriptorTests: XCTestCase { (optionValue: false, argumentValue: "disabled"), ] validateDescriptor(descriptor, name: "Remove Lines", argumentName: "removelines", propertyName: "removeBlankLines") - validateArgumentsBinaryType(descriptor, controlTrue: ["enabled", "true"], controlFalse: ["disabled", "false"], default: true) + validateArgumentsBinaryType(descriptor, controlTrue: ["enabled", "true"], controlFalse: ["disabled", "false"]) validateFromOptions(descriptor, keyPath: \FormatOptions.removeBlankLines, expectations: fromOptionsExpectation) validateFromArgumentsBinaryType(descriptor, keyPath: \FormatOptions.removeBlankLines) validateDescriptorThrowsOptionsError(descriptor) @@ -642,7 +629,7 @@ class OptionsDescriptorTests: XCTestCase { (optionValue: false, argumentValue: "lowercase"), ] validateDescriptor(descriptor, name: "hexliterals", argumentName: "hexliterals", propertyName: "hexLiteralCase") - validateArgumentsBinaryType(descriptor, controlTrue: ["uppercase", "upper"], controlFalse: ["lowercase", "lower"], default: true) + validateArgumentsBinaryType(descriptor, controlTrue: ["uppercase", "upper"], controlFalse: ["lowercase", "lower"]) validateFromOptions(descriptor, keyPath: \FormatOptions.uppercaseHex, expectations: fromOptionsExpectation) validateFromArgumentsBinaryType(descriptor, keyPath: \FormatOptions.uppercaseHex) validateDescriptorThrowsOptionsError(descriptor) @@ -656,7 +643,7 @@ class OptionsDescriptorTests: XCTestCase { (optionValue: .disabled, argumentValue: "disabled"), ] validateDescriptor(descriptor, name: "Wrap Elements", argumentName: "wrapelements", propertyName: "wrapCollections") - validateArgumentsListType(descriptor, validArguments: ["beforefirst", "afterfirst", "disabled"], default: "beforefirst") + validateArgumentsListType(descriptor, validArguments: ["beforefirst", "afterfirst", "disabled"]) validateFromOptions(descriptor, keyPath: \FormatOptions.wrapCollections, expectations: expectedMapping) validateFromArguments(descriptor, keyPath: \FormatOptions.wrapCollections, expectations: expectedMapping) validateDescriptorThrowsOptionsError(descriptor)