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

File Template Updates #2142

Merged
merged 2 commits into from
Feb 8, 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
4 changes: 4 additions & 0 deletions Apollo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@
DEFBBC86273470F70088AABC /* IR+Field.swift in Sources */ = {isa = PBXBuildFile; fileRef = DEFBBC85273470F70088AABC /* IR+Field.swift */; };
DEFE0FC52748822900FFA440 /* IR+EntitySelectionTree.swift in Sources */ = {isa = PBXBuildFile; fileRef = DEFE0FC42748822900FFA440 /* IR+EntitySelectionTree.swift */; };
E607AD8E27A88F340026742A /* OperationFileGeneratorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E607AD8D27A88F340026742A /* OperationFileGeneratorTests.swift */; };
E608FBA527B1EFDF00493756 /* HeaderCommentTemplate.swift in Sources */ = {isa = PBXBuildFile; fileRef = E608FBA427B1EFDF00493756 /* HeaderCommentTemplate.swift */; };
E610D8D7278EA2390023E495 /* EnumFileGenerator.swift in Sources */ = {isa = PBXBuildFile; fileRef = E610D8D6278EA2390023E495 /* EnumFileGenerator.swift */; };
E610D8D9278EA2560023E495 /* EnumFileGeneratorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E610D8D8278EA2560023E495 /* EnumFileGeneratorTests.swift */; };
E610D8DB278EB0900023E495 /* InterfaceFileGenerator.swift in Sources */ = {isa = PBXBuildFile; fileRef = E610D8DA278EB0900023E495 /* InterfaceFileGenerator.swift */; };
Expand Down Expand Up @@ -1036,6 +1037,7 @@
DEFBBC85273470F70088AABC /* IR+Field.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "IR+Field.swift"; sourceTree = "<group>"; };
DEFE0FC42748822900FFA440 /* IR+EntitySelectionTree.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "IR+EntitySelectionTree.swift"; sourceTree = "<group>"; };
E607AD8D27A88F340026742A /* OperationFileGeneratorTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OperationFileGeneratorTests.swift; sourceTree = "<group>"; };
E608FBA427B1EFDF00493756 /* HeaderCommentTemplate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HeaderCommentTemplate.swift; sourceTree = "<group>"; };
E610D8D6278EA2390023E495 /* EnumFileGenerator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EnumFileGenerator.swift; sourceTree = "<group>"; };
E610D8D8278EA2560023E495 /* EnumFileGeneratorTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EnumFileGeneratorTests.swift; sourceTree = "<group>"; };
E610D8DA278EB0900023E495 /* InterfaceFileGenerator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InterfaceFileGenerator.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2013,6 +2015,7 @@
children = (
E6C9849227929EBE009481BE /* EnumTemplate.swift */,
DE5FD60A276970FC0033EE23 /* FragmentTemplate.swift */,
E608FBA427B1EFDF00493756 /* HeaderCommentTemplate.swift */,
DE5B318E27A48E060051C9D3 /* ImportStatementTemplate.swift */,
E69BEDA42798B86D00000D10 /* InputObjectTemplate.swift */,
E623FD292797A6F4008B4CED /* InterfaceTemplate.swift */,
Expand Down Expand Up @@ -3118,6 +3121,7 @@
9BE74D3D23FB4A8E006D354F /* FileFinder.swift in Sources */,
E64F7EBC27A11A510059C021 /* GraphQLNamedType+Swift.swift in Sources */,
9B7B6F59233C287200F32205 /* ApolloCodegen.swift in Sources */,
E608FBA527B1EFDF00493756 /* HeaderCommentTemplate.swift in Sources */,
DE2739112769AEBA00B886EF /* SelectionSetTemplate.swift in Sources */,
E6E3BBE2276A8D6200E5218B /* FileGenerator.swift in Sources */,
DEFE0FC52748822900FFA440 /* IR+EntitySelectionTree.swift in Sources */,
Expand Down
4 changes: 3 additions & 1 deletion Sources/ApolloCodegenLib/Templates/EnumTemplate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ struct EnumTemplate {
func render() -> String {
TemplateString(
"""
\(HeaderCommentTemplate.render())

\(ImportStatementTemplate.SchemaType.render())

public enum \(graphqlEnum.name): String, EnumType {
public enum \(graphqlEnum.name.firstUppercased): String, EnumType {
\(graphqlEnum.values.map({
"case \($0.name)"
}), separator: "\n")
Expand Down
2 changes: 2 additions & 0 deletions Sources/ApolloCodegenLib/Templates/FragmentTemplate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ struct FragmentTemplate {
func render() -> String {
TemplateString(
"""
\(HeaderCommentTemplate.render())

\(ImportStatementTemplate.Operation.render(config))

public struct \(fragment.name): \(schema.name).SelectionSet, Fragment {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
struct HeaderCommentTemplate {
static func render() -> String {
"""
// @generated
// This file was automatically generated and should not be edited.
"""
}
}
2 changes: 2 additions & 0 deletions Sources/ApolloCodegenLib/Templates/InputObjectTemplate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ struct InputObjectTemplate {
func render() -> String {
TemplateString(
"""
\(HeaderCommentTemplate.render())

\(ImportStatementTemplate.SchemaType.render())

struct \(graphqlInputObject.name.firstUppercased): InputObject {
Expand Down
2 changes: 2 additions & 0 deletions Sources/ApolloCodegenLib/Templates/InterfaceTemplate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ struct InterfaceTemplate {
func render() -> String {
TemplateString(
"""
\(HeaderCommentTemplate.render())

\(ImportStatementTemplate.SchemaType.render())

public final class \(graphqlInterface.name): Interface { }
Expand Down
2 changes: 2 additions & 0 deletions Sources/ApolloCodegenLib/Templates/ObjectTemplate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ struct ObjectTemplate {
func render() -> String {
TemplateString(
"""
\(HeaderCommentTemplate.render())

\(ImportStatementTemplate.SchemaType.render())

public final class \(graphqlObject.name.firstUppercased): Object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ struct OperationDefinitionTemplate {
func render() -> String {
TemplateString(
"""
\(HeaderCommentTemplate.render())

\(ImportStatementTemplate.Operation.render(config.output))

\(OperationDeclaration(operation.definition))
Expand Down
2 changes: 2 additions & 0 deletions Sources/ApolloCodegenLib/Templates/SchemaTemplate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ struct SchemaTemplate {
func render() -> String {
TemplateString(
"""
\(HeaderCommentTemplate.render())

\(ImportStatementTemplate.SchemaType.render())

public typealias ID = String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,13 @@ struct SwiftPackageManagerModuleTemplate {
.package(url: "https://github.com/apollographql/apollo-ios.git", from: "1.0.0"),
],
targets: [
.target(name: "\(moduleName)", dependencies: [
.product(name: "ApolloAPI", package: "apollo-ios"),
]),
.target(
name: "\(moduleName)",
dependencies: [
.product(name: "ApolloAPI", package: "apollo-ios"),
],
path: "."
),
]
)
""").description
Expand Down
2 changes: 2 additions & 0 deletions Sources/ApolloCodegenLib/Templates/UnionTemplate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ struct UnionTemplate {
func render() -> String {
TemplateString(
"""
\(HeaderCommentTemplate.render())

\(ImportStatementTemplate.SchemaType.render())

public enum \(graphqlUnion.name): UnionType, Equatable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,78 @@ import Nimble
import ApolloCodegenTestSupport

class EnumTemplateTests: XCTestCase {
var subject: EnumTemplate!

func test_render_boilerplate_givenInputObject_generatesImportStatement() {
let graphqlEnum = GraphQLEnumType.mock(name: "TestEnum", values: ["ONE", "TWO"])
let template = EnumTemplate(graphqlEnum: graphqlEnum)
override func tearDown() {
subject = nil

super.tearDown()
}

private func buildSubject(name: String, values: [String]) {
subject = EnumTemplate(
graphqlEnum: GraphQLEnumType.mock(name: name, values: values)
)
}

// MARK: Boilerplate Tests

func test_render_generatesHeaderComment() {
// given
buildSubject(name: "TestEnum", values: ["ONE", "TWO"])

let expected = """
// @generated
// This file was automatically generated and should not be edited.

"""

// when
let actual = subject.render()

// then
expect(actual).to(equalLineByLine(expected, ignoringExtraLines: true))
}

func test_render_generatesImportStatement() {
// given
buildSubject(name: "TestEnum", values: ["ONE", "TWO"])

let expected = """
import ApolloAPI

"""

// when
let actual = template.render()
let actual = subject.render()

// then
expect(actual).to(equalLineByLine(expected, ignoringExtraLines: true))
expect(actual).to(equalLineByLine(expected, atLine: 4, ignoringExtraLines: true))
}

// MARK: Enum Casing Tests

func test_render_givenSchemaEnum_generatesSwiftEnumNameFirstUppercased() throws {
// given
buildSubject(name: "testEnum", values: ["ONE", "TWO"])

let expected = """
public enum TestEnum: String, EnumType {
case ONE
case TWO
}
"""

// when
let actual = subject.render()

// then
expect(actual).to(equalLineByLine(expected, atLine: 6))
}

func test_render_givenSchemaEnum_generatesSwiftEnum() throws {
// given
let graphqlEnum = GraphQLEnumType.mock(name: "TestEnum", values: ["ONE", "TWO"])
let template = EnumTemplate(graphqlEnum: graphqlEnum)
buildSubject(name: "TestEnum", values: ["ONE", "TWO"])

let expected = """
public enum TestEnum: String, EnumType {
Expand All @@ -34,19 +85,15 @@ class EnumTemplateTests: XCTestCase {
"""

// when
let actual = template.render()
let actual = subject.render()

// then
expect(actual).to(equalLineByLine(expected, atLine: 3))
expect(actual).to(equalLineByLine(expected, atLine: 6))
}

func test_render_givenSchemaEnum_generatesSwiftEnumRespectingCase() throws {
// given
let graphqlEnum = GraphQLEnumType.mock(
name: "CasedEnum",
values: ["lower", "UPPER", "Capitalized"]
)
let template = EnumTemplate(graphqlEnum: graphqlEnum)
buildSubject(name: "CasedEnum", values: ["lower", "UPPER", "Capitalized"])

let expected = """
public enum CasedEnum: String, EnumType {
Expand All @@ -57,9 +104,9 @@ class EnumTemplateTests: XCTestCase {
"""

// when
let actual = template.render()
let actual = subject.render()

// then
expect(actual).to(equalLineByLine(expected, atLine: 3))
expect(actual).to(equalLineByLine(expected, atLine: 6))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,31 @@ class FragmentTemplateTests: XCTestCase {
)
}

// MARK: - Boilerplate Tests

func test__render__generatesHeaderComment() throws {
// given
config = .mock(output: .mock(
moduleType: .manuallyLinked(namespace: "TestModuleName"),
operations: .inSchemaModule
))

let expected =
"""
// @generated
// This file was automatically generated and should not be edited.

"""

// when
try buildSubjectAndFragment()

let actual = subject.render()

// then
expect(actual).to(equalLineByLine(expected, ignoringExtraLines: true))
}

// MARK: - Import Statements

func test__render__givenFileOutput_inSchemaModule_schemaModuleManuallyLinked_generatesImportNotIncludingSchemaModule() throws {
Expand All @@ -79,7 +104,7 @@ class FragmentTemplateTests: XCTestCase {
let actual = subject.render()

// then
expect(actual).to(equalLineByLine(expected, ignoringExtraLines: true))
expect(actual).to(equalLineByLine(expected, atLine: 4, ignoringExtraLines: true))
}

func test__render__givenFileOutput_inSchemaModule_schemaModuleNotManuallyLinked_generatesImportNotIncludingSchemaModule() throws {
Expand All @@ -101,7 +126,7 @@ class FragmentTemplateTests: XCTestCase {
let actual = subject.render()

// then
expect(actual).to(equalLineByLine(expected, ignoringExtraLines: true))
expect(actual).to(equalLineByLine(expected, atLine: 4, ignoringExtraLines: true))
}

func test__render__givenFileOutput_notInSchemaModule_schemaModuleNotManuallyLinked_generatesImportIncludingSchemaModule() throws {
Expand All @@ -124,7 +149,7 @@ class FragmentTemplateTests: XCTestCase {
let actual = subject.render()

// then
expect(actual).to(equalLineByLine(expected, ignoringExtraLines: true))
expect(actual).to(equalLineByLine(expected, atLine: 4, ignoringExtraLines: true))
}

// MARK: - Fragment Definition
Expand Down Expand Up @@ -152,7 +177,7 @@ class FragmentTemplateTests: XCTestCase {
let actual = subject.render()

// then
expect(actual).to(equalLineByLine(expected, atLine: 3, ignoringExtraLines: true))
expect(actual).to(equalLineByLine(expected, atLine: 6, ignoringExtraLines: true))
expect(String(actual.reversed())).to(equalLineByLine("}", ignoringExtraLines: true))
}

Expand Down Expand Up @@ -183,7 +208,7 @@ class FragmentTemplateTests: XCTestCase {
let actual = subject.render()

// then
expect(actual).to(equalLineByLine(expected, atLine: 3, ignoringExtraLines: true))
expect(actual).to(equalLineByLine(expected, atLine: 6, ignoringExtraLines: true))
}

func test__render_parentType__givenFragmentTypeConditionAs_Object_rendersParentType() throws {
Expand Down Expand Up @@ -213,7 +238,7 @@ class FragmentTemplateTests: XCTestCase {
let actual = subject.render()

// then
expect(actual).to(equalLineByLine(expected, atLine: 13, ignoringExtraLines: true))
expect(actual).to(equalLineByLine(expected, atLine: 16, ignoringExtraLines: true))
}

func test__render_parentType__givenFragmentTypeConditionAs_Interface_rendersParentType() throws {
Expand Down Expand Up @@ -243,7 +268,7 @@ class FragmentTemplateTests: XCTestCase {
let actual = subject.render()

// then
expect(actual).to(equalLineByLine(expected, atLine: 13, ignoringExtraLines: true))
expect(actual).to(equalLineByLine(expected, atLine: 16, ignoringExtraLines: true))
}

func test__render_parentType__givenFragmentTypeConditionAs_Union_rendersParentType() throws {
Expand Down Expand Up @@ -277,7 +302,7 @@ class FragmentTemplateTests: XCTestCase {
let actual = subject.render()

// then
expect(actual).to(equalLineByLine(expected, atLine: 15, ignoringExtraLines: true))
expect(actual).to(equalLineByLine(expected, atLine: 18, ignoringExtraLines: true))
}

}
Loading