Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not generate a convenience initializer for mock objects without fields #2634

Merged
merged 1 commit into from
Nov 3, 2022
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
24 changes: 14 additions & 10 deletions Sources/ApolloCodegenLib/Templates/MockObjectTemplate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,21 @@ struct MockObjectTemplate: TemplateRenderer {
}, separator: "\n")
}
}

public extension Mock where O == \(objectName) {
convenience init(
\(fields.map { """
\($0.propertyName)\(ifLet: $0.initializerParameterName, {" \($0)"}): \($0.mockType)? = nil
""" }, separator: ",\n")
) {
self.init()
\(fields.map { "self.\($0.propertyName) = \($0.initializerParameterName ?? $0.propertyName)" }, separator: "\n")
\(!fields.isEmpty ?
TemplateString("""

public extension Mock where O == \(objectName) {
convenience init(
\(fields.map { """
\($0.propertyName)\(ifLet: $0.initializerParameterName, {" \($0)"}): \($0.mockType)? = nil
""" }, separator: ",\n")
) {
self.init()
\(fields.map { "self.\($0.propertyName) = \($0.initializerParameterName ?? $0.propertyName)" }, separator: "\n")
}
}
}
""") : TemplateString(stringLiteral: "")
)

"""
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,25 @@ class MockObjectTemplateTests: XCTestCase {
ignoringExtraLines: false)
)
}

func test_render_givenSchemaTypeWithoutFields_doesNotgenerateConvenienceInitializer() {
// given
buildSubject(moduleType: .swiftPackageManager)

let expected = """
}

"""
// when
let actual = renderSubject()

// then
expect(actual).to(equalLineByLine(
expected,
atLine: 8 + self.subject.graphqlObject.fields.count,
ignoringExtraLines: false)
)
}

func test_render_givenFieldsWithSwiftReservedKeyworkNames_generatesConvenienceInitializerParamatersEscapedWithBackticksAndInternalNames() {
// given
Expand Down