-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from giginet/snake-case-conversion
Implement snake_case Conversion and fix inifinite loops
- Loading branch information
Showing
4 changed files
with
102 additions
and
23 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
Sources/RevolutionKit/Rewriter/TestMethodNameConverter.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import Foundation | ||
|
||
struct TestMethodNameConverter { | ||
let shouldStripPrefix: Bool | ||
private let testPrefix = "test" | ||
private let snakeCasePrefix = "test_" | ||
|
||
/// Strip first `test` prefix from test case names. | ||
/// `testCamelCase` -> `camelCase` | ||
/// `test_snake_case` -> `snake_case` | ||
/// `test` -> `test` | ||
func convert(_ testMethodName: String) -> String { | ||
guard shouldStripPrefix else { return testMethodName } | ||
|
||
if testMethodName.hasPrefix(snakeCasePrefix) { | ||
return testMethodName.strippedFirst(snakeCasePrefix.count) | ||
} else if testMethodName == testPrefix { | ||
return testMethodName | ||
} else if testMethodName.hasPrefix(testPrefix) { | ||
return testMethodName | ||
.strippedFirst(testPrefix.count) | ||
.lowercasedFirstLetter() | ||
} | ||
assertionFailure("Unexpected call") | ||
return testMethodName | ||
} | ||
} | ||
|
||
extension String { | ||
fileprivate func strippedFirst(_ k: Int) -> String { | ||
var mutating = self | ||
mutating.removeFirst(k) | ||
return mutating | ||
} | ||
|
||
fileprivate func lowercasedFirstLetter() -> String { | ||
guard let firstLetter = first else { return self } | ||
return firstLetter.lowercased() + self[index(after: startIndex)..<endIndex] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
Tests/RevolutionKitTests/TestMethodNameConverterTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import Testing | ||
@testable import RevolutionKit | ||
|
||
struct TestMethodNameConverterTests { | ||
private static let fixtures = [ | ||
(true, "testDoSomething", "doSomething"), | ||
(false, "testDoSomething", "testDoSomething"), | ||
(true, "test_do_something", "do_something"), | ||
(false, "test_do_something", "test_do_something"), | ||
(true, "test", "test"), | ||
(false, "test", "test"), | ||
] | ||
|
||
@Test("TestMethodNameConverter can convert method names", arguments: fixtures) | ||
func testConversion(enableStripping: Bool, input: String, expected: String) { | ||
let converter = TestMethodNameConverter(shouldStripPrefix: enableStripping) | ||
|
||
let actual = converter.convert(input) | ||
#expect(actual == expected) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters