Skip to content

Commit 29440d8

Browse files
authored
Merge pull request #29 from mattpolzin/fix-periods-in-path
fix period in path for function naming
2 parents f959054 + ba11281 commit 29440d8

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

Sources/JSONAPISwiftGen/Swift Generators/Test Generators/TestFunctionName.swift

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public struct TestFunctionName: Equatable, RawRepresentable {
5252
public var rawValue: String {
5353
return Self.testPrefix +
5454
nameApplying(pathComponentTransform: Self.functionEncodedName)
55-
.replacingOccurrences(of: ".", with: "\(Self.periodReplacementCharacter)")
55+
.replacingOccurrences(of: ".", with: "\(Self.moduleSeparatorPeriodReplacementCharacter)")
5656
}
5757

5858
/// The fully qualified test function name is the name of the test
@@ -83,7 +83,7 @@ public struct TestFunctionName: Equatable, RawRepresentable {
8383

8484
let value = rawValue[rangeOfPrefix.upperBound...]
8585

86-
var components = value.split(separator: Self.periodReplacementCharacter)
86+
var components = value.split(separator: Self.moduleSeparatorPeriodReplacementCharacter)
8787

8888
guard components.count > 2 else {
8989
return nil
@@ -130,21 +130,25 @@ public struct TestFunctionName: Equatable, RawRepresentable {
130130
.replacingOccurrences(of: "{", with: "\(Self.openBraceReplacementCharacter)")
131131
.replacingOccurrences(of: "}", with: "\(Self.closeBraceReplacementCharacter)")
132132
.replacingOccurrences(of: " ", with: "\(Self.spaceReplacementCharacter)")
133+
.replacingOccurrences(of: ".", with: "\(Self.namePeriodReplacementCharacter)")
133134
}
134135

135136
internal static func functionDecodedName(from string: String) -> String {
136137
return string
137138
.replacingOccurrences(of: "\(Self.openBraceReplacementCharacter)", with: "{")
138139
.replacingOccurrences(of: "\(Self.closeBraceReplacementCharacter)", with: "}")
139140
.replacingOccurrences(of: "\(Self.spaceReplacementCharacter)", with: " ")
141+
.replacingOccurrences(of: "\(Self.namePeriodReplacementCharacter)", with: ".")
140142
}
141143

142-
/// For swift names, we remove braces, escape reserved words, and convert spaces to underscores.
144+
/// For swift names, we remove braces, escape reserved words, and convert
145+
/// spaces and periods to underscores.
143146
public static func swiftName(from string: String) -> String {
144147
let name = string
145148
.replacingOccurrences(of: "{", with: "")
146149
.replacingOccurrences(of: "}", with: "")
147150
.replacingOccurrences(of: " ", with: "_")
151+
.replacingOccurrences(of: ".", with: "_")
148152
return Self.escapedKeyword(name)
149153
}
150154

@@ -160,7 +164,8 @@ public struct TestFunctionName: Equatable, RawRepresentable {
160164
private static var openBraceReplacementCharacter: Character = ""
161165
private static var closeBraceReplacementCharacter: Character = ""
162166
private static var spaceReplacementCharacter: Character = ""
163-
private static var periodReplacementCharacter: Character = ""
167+
private static var moduleSeparatorPeriodReplacementCharacter: Character = ""
168+
private static var namePeriodReplacementCharacter: Character = ""
164169
// ➎ taken by `TestFunctionLocalContext.prefixSeparatorCharacter`.
165170
}
166171

Tests/JSONAPISwiftGenTests/TestFunctionNameTests.swift

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,35 @@ final class TestFunctionNameTests: XCTestCase {
8585

8686
assertReflexiveRawValue(
8787
TestFunctionName(
88-
path: .init(["go", "continue", "do", "accept"]),
88+
path: .init(["go", "_", "continue", "do", "try", "accept"]),
8989
endpoint: .post,
9090
direction: .request,
9191
context: TestFunctionLocalContext(functionName: "_hello_world➎")!
9292
)
9393
)
94+
95+
assertReflexiveRawValue(
96+
TestFunctionName(
97+
path: .init(["hello.world", "hi"]),
98+
endpoint: .post,
99+
direction: .request,
100+
context: TestFunctionLocalContext(functionName: "_hello_world➎")!
101+
)
102+
)
103+
}
104+
105+
func test_periodInPath() {
106+
let t1 = TestFunctionName(
107+
path: .init(["hello.world", "hi"]),
108+
endpoint: .post,
109+
direction: .request,
110+
context: TestFunctionLocalContext(functionName: "_hello_world➎")!
111+
)
112+
XCTAssertEqual(t1.fullyQualifiedTestFunctionName, "hello_world.hi.POST.Request._hello_world➎")
113+
XCTAssertEqual(t1.rawValue, "test__hello❺world➍hi➍POST➍Request➍_hello_world➎")
114+
XCTAssertEqual(t1.testName, "_hello_world➎")
115+
116+
XCTAssertEqual(TestFunctionName(rawValue: t1.rawValue)?.path, .init(["hello.world", "hi"]))
94117
}
95118

96119
func test_reservedWordsInPath() {

0 commit comments

Comments
 (0)