Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public struct TestFunctionName: Equatable, RawRepresentable {
public var rawValue: String {
return Self.testPrefix +
nameApplying(pathComponentTransform: Self.functionEncodedName)
.replacingOccurrences(of: ".", with: "\(Self.periodReplacementCharacter)")
.replacingOccurrences(of: ".", with: "\(Self.moduleSeparatorPeriodReplacementCharacter)")
}

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

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

var components = value.split(separator: Self.periodReplacementCharacter)
var components = value.split(separator: Self.moduleSeparatorPeriodReplacementCharacter)

guard components.count > 2 else {
return nil
Expand Down Expand Up @@ -130,21 +130,25 @@ public struct TestFunctionName: Equatable, RawRepresentable {
.replacingOccurrences(of: "{", with: "\(Self.openBraceReplacementCharacter)")
.replacingOccurrences(of: "}", with: "\(Self.closeBraceReplacementCharacter)")
.replacingOccurrences(of: " ", with: "\(Self.spaceReplacementCharacter)")
.replacingOccurrences(of: ".", with: "\(Self.namePeriodReplacementCharacter)")
}

internal static func functionDecodedName(from string: String) -> String {
return string
.replacingOccurrences(of: "\(Self.openBraceReplacementCharacter)", with: "{")
.replacingOccurrences(of: "\(Self.closeBraceReplacementCharacter)", with: "}")
.replacingOccurrences(of: "\(Self.spaceReplacementCharacter)", with: " ")
.replacingOccurrences(of: "\(Self.namePeriodReplacementCharacter)", with: ".")
}

/// For swift names, we remove braces, escape reserved words, and convert spaces to underscores.
/// For swift names, we remove braces, escape reserved words, and convert
/// spaces and periods to underscores.
public static func swiftName(from string: String) -> String {
let name = string
.replacingOccurrences(of: "{", with: "")
.replacingOccurrences(of: "}", with: "")
.replacingOccurrences(of: " ", with: "_")
.replacingOccurrences(of: ".", with: "_")
return Self.escapedKeyword(name)
}

Expand All @@ -160,7 +164,8 @@ public struct TestFunctionName: Equatable, RawRepresentable {
private static var openBraceReplacementCharacter: Character = "➊"
private static var closeBraceReplacementCharacter: Character = "➋"
private static var spaceReplacementCharacter: Character = "➌"
private static var periodReplacementCharacter: Character = "➍"
private static var moduleSeparatorPeriodReplacementCharacter: Character = "➍"
private static var namePeriodReplacementCharacter: Character = "❺"
// ➎ taken by `TestFunctionLocalContext.prefixSeparatorCharacter`.
}

Expand Down
25 changes: 24 additions & 1 deletion Tests/JSONAPISwiftGenTests/TestFunctionNameTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,35 @@ final class TestFunctionNameTests: XCTestCase {

assertReflexiveRawValue(
TestFunctionName(
path: .init(["go", "continue", "do", "accept"]),
path: .init(["go", "_", "continue", "do", "try", "accept"]),
endpoint: .post,
direction: .request,
context: TestFunctionLocalContext(functionName: "_hello_world➎")!
)
)

assertReflexiveRawValue(
TestFunctionName(
path: .init(["hello.world", "hi"]),
endpoint: .post,
direction: .request,
context: TestFunctionLocalContext(functionName: "_hello_world➎")!
)
)
}

func test_periodInPath() {
let t1 = TestFunctionName(
path: .init(["hello.world", "hi"]),
endpoint: .post,
direction: .request,
context: TestFunctionLocalContext(functionName: "_hello_world➎")!
)
XCTAssertEqual(t1.fullyQualifiedTestFunctionName, "hello_world.hi.POST.Request._hello_world➎")
XCTAssertEqual(t1.rawValue, "test__hello❺world➍hi➍POST➍Request➍_hello_world➎")
XCTAssertEqual(t1.testName, "_hello_world➎")

XCTAssertEqual(TestFunctionName(rawValue: t1.rawValue)?.path, .init(["hello.world", "hi"]))
}

func test_reservedWordsInPath() {
Expand Down