From e91ac70f199f8d8dd7831d49e57c8a996b7d998c Mon Sep 17 00:00:00 2001 From: Kattouf Date: Thu, 26 Sep 2024 11:08:45 +0700 Subject: [PATCH] convert command name case improvements --- Sources/Sake/String+Case.swift | 2 + .../CommandNameCaseConverterTests.swift | 42 +++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/Sources/Sake/String+Case.swift b/Sources/Sake/String+Case.swift index b03c58c..33c339a 100644 --- a/Sources/Sake/String+Case.swift +++ b/Sources/Sake/String+Case.swift @@ -1,11 +1,13 @@ extension String { func toSnakeCase() -> String { replacingOccurrences(of: "([a-z])([A-Z])", with: "$1_$2", options: .regularExpression) + .replacingOccurrences(of: "-", with: "_") .lowercased() } func toKebabCase() -> String { replacingOccurrences(of: "([a-z])([A-Z])", with: "$1-$2", options: .regularExpression) + .replacingOccurrences(of: "_", with: "-") .lowercased() } } diff --git a/Tests/SakeTests/CommandNameCaseConverterTests.swift b/Tests/SakeTests/CommandNameCaseConverterTests.swift index 7c24c73..6bed8b2 100644 --- a/Tests/SakeTests/CommandNameCaseConverterTests.swift +++ b/Tests/SakeTests/CommandNameCaseConverterTests.swift @@ -9,6 +9,20 @@ final class CommandNameCaseConverterTests: XCTestCase { ] let convertedCommands = CommandNameCaseConverter.convert(commands, strategy: .keepOriginal) XCTAssertEqual(Set(convertedCommands.keys), ["commandOne", "commandTwo"]) + + let commands2: [String: Command] = [ + "command_one": Command(description: "description1"), + "command_two": Command(description: "description2"), + ] + let convertedCommands2 = CommandNameCaseConverter.convert(commands2, strategy: .keepOriginal) + XCTAssertEqual(Set(convertedCommands2.keys), ["command_one", "command_two"]) + + let commands3: [String: Command] = [ + "command-one": Command(description: "description1"), + "command-two": Command(description: "description2"), + ] + let convertedCommands3 = CommandNameCaseConverter.convert(commands3, strategy: .keepOriginal) + XCTAssertEqual(Set(convertedCommands3.keys), ["command-one", "command-two"]) } func testToSnakeCaseStrategy() { @@ -18,6 +32,20 @@ final class CommandNameCaseConverterTests: XCTestCase { ] let convertedCommands = CommandNameCaseConverter.convert(commands, strategy: .toSnakeCase) XCTAssertEqual(Set(convertedCommands.keys), ["command_one", "command_two"]) + + let commands2: [String: Command] = [ + "command_one": Command(description: "description1"), + "command_two": Command(description: "description2"), + ] + let convertedCommands2 = CommandNameCaseConverter.convert(commands2, strategy: .toSnakeCase) + XCTAssertEqual(Set(convertedCommands2.keys), ["command_one", "command_two"]) + + let commands3: [String: Command] = [ + "command-one": Command(description: "description1"), + "command-two": Command(description: "description2"), + ] + let convertedCommands3 = CommandNameCaseConverter.convert(commands3, strategy: .toSnakeCase) + XCTAssertEqual(Set(convertedCommands3.keys), ["command_one", "command_two"]) } func testToKebabCaseStrategy() { @@ -27,5 +55,19 @@ final class CommandNameCaseConverterTests: XCTestCase { ] let convertedCommands = CommandNameCaseConverter.convert(commands, strategy: .toKebabCase) XCTAssertEqual(Set(convertedCommands.keys), ["command-one", "command-two"]) + + let commands2: [String: Command] = [ + "command_one": Command(description: "description1"), + "command_two": Command(description: "description2"), + ] + let convertedCommands2 = CommandNameCaseConverter.convert(commands2, strategy: .toKebabCase) + XCTAssertEqual(Set(convertedCommands2.keys), ["command-one", "command-two"]) + + let commands3: [String: Command] = [ + "command-one": Command(description: "description1"), + "command-two": Command(description: "description2"), + ] + let convertedCommands3 = CommandNameCaseConverter.convert(commands3, strategy: .toKebabCase) + XCTAssertEqual(Set(convertedCommands3.keys), ["command-one", "command-two"]) } }