From d2ba30e185a0f660d93242260575436f13876a6c Mon Sep 17 00:00:00 2001 From: Calvin Cestari Date: Mon, 7 Feb 2022 10:25:30 -0800 Subject: [PATCH 1/2] Adds the path parameter to the package target --- .../Templates/SwiftPackageManagerModuleTemplate.swift | 10 +++++++--- .../SwiftPackageManagerModuleTemplateTests.swift | 10 +++++++--- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/Sources/ApolloCodegenLib/Templates/SwiftPackageManagerModuleTemplate.swift b/Sources/ApolloCodegenLib/Templates/SwiftPackageManagerModuleTemplate.swift index 5589e2b5b3..cf22911fe7 100644 --- a/Sources/ApolloCodegenLib/Templates/SwiftPackageManagerModuleTemplate.swift +++ b/Sources/ApolloCodegenLib/Templates/SwiftPackageManagerModuleTemplate.swift @@ -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 diff --git a/Tests/ApolloCodegenTests/CodeGeneration/Templates/SwiftPackageManagerModuleTemplateTests.swift b/Tests/ApolloCodegenTests/CodeGeneration/Templates/SwiftPackageManagerModuleTemplateTests.swift index b9093ee9a3..965daaa35d 100644 --- a/Tests/ApolloCodegenTests/CodeGeneration/Templates/SwiftPackageManagerModuleTemplateTests.swift +++ b/Tests/ApolloCodegenTests/CodeGeneration/Templates/SwiftPackageManagerModuleTemplateTests.swift @@ -100,9 +100,13 @@ class SwiftPackageManagerModuleTemplateTests: XCTestCase { // given let expected = """ targets: [ - .target(name: "TestModule", dependencies: [ - .product(name: "ApolloAPI", package: "apollo-ios"), - ]), + .target( + name: "TestModule", + dependencies: [ + .product(name: "ApolloAPI", package: "apollo-ios"), + ], + path: "." + ), ] """ From f5d762211d143b98d7fd9efbae3cf63178c886dc Mon Sep 17 00:00:00 2001 From: Calvin Cestari Date: Mon, 7 Feb 2022 22:28:40 -0800 Subject: [PATCH 2/2] Add generated file header comment to templates --- Apollo.xcodeproj/project.pbxproj | 4 + .../Templates/EnumTemplate.swift | 4 +- .../Templates/FragmentTemplate.swift | 2 + .../Templates/HeaderCommentTemplate.swift | 8 + .../Templates/InputObjectTemplate.swift | 2 + .../Templates/InterfaceTemplate.swift | 2 + .../Templates/ObjectTemplate.swift | 2 + .../OperationDefinitionTemplate.swift | 2 + .../Templates/SchemaTemplate.swift | 2 + .../Templates/UnionTemplate.swift | 2 + .../Templates/EnumTemplateTests.swift | 79 +++++++-- .../Templates/FragmentTemplateTests.swift | 41 ++++- .../Templates/InputObjectTemplateTests.swift | 155 +++++++++++------- .../Templates/InterfaceTemplateTests.swift | 61 ++++++- .../Templates/ObjectTemplateTests.swift | 66 ++++++-- .../OperationDefinitionTemplateTests.swift | 35 +++- .../Templates/SchemaTemplateTests.swift | 153 +++++++++++++---- .../Templates/UnionTemplateTests.swift | 108 +++++++++--- 18 files changed, 551 insertions(+), 177 deletions(-) create mode 100644 Sources/ApolloCodegenLib/Templates/HeaderCommentTemplate.swift diff --git a/Apollo.xcodeproj/project.pbxproj b/Apollo.xcodeproj/project.pbxproj index ee491049e3..d3e2b57e4a 100644 --- a/Apollo.xcodeproj/project.pbxproj +++ b/Apollo.xcodeproj/project.pbxproj @@ -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 */; }; @@ -1036,6 +1037,7 @@ DEFBBC85273470F70088AABC /* IR+Field.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "IR+Field.swift"; sourceTree = ""; }; DEFE0FC42748822900FFA440 /* IR+EntitySelectionTree.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "IR+EntitySelectionTree.swift"; sourceTree = ""; }; E607AD8D27A88F340026742A /* OperationFileGeneratorTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OperationFileGeneratorTests.swift; sourceTree = ""; }; + E608FBA427B1EFDF00493756 /* HeaderCommentTemplate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HeaderCommentTemplate.swift; sourceTree = ""; }; E610D8D6278EA2390023E495 /* EnumFileGenerator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EnumFileGenerator.swift; sourceTree = ""; }; E610D8D8278EA2560023E495 /* EnumFileGeneratorTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EnumFileGeneratorTests.swift; sourceTree = ""; }; E610D8DA278EB0900023E495 /* InterfaceFileGenerator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InterfaceFileGenerator.swift; sourceTree = ""; }; @@ -2013,6 +2015,7 @@ children = ( E6C9849227929EBE009481BE /* EnumTemplate.swift */, DE5FD60A276970FC0033EE23 /* FragmentTemplate.swift */, + E608FBA427B1EFDF00493756 /* HeaderCommentTemplate.swift */, DE5B318E27A48E060051C9D3 /* ImportStatementTemplate.swift */, E69BEDA42798B86D00000D10 /* InputObjectTemplate.swift */, E623FD292797A6F4008B4CED /* InterfaceTemplate.swift */, @@ -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 */, diff --git a/Sources/ApolloCodegenLib/Templates/EnumTemplate.swift b/Sources/ApolloCodegenLib/Templates/EnumTemplate.swift index 52f80411ba..7ea928a9bf 100644 --- a/Sources/ApolloCodegenLib/Templates/EnumTemplate.swift +++ b/Sources/ApolloCodegenLib/Templates/EnumTemplate.swift @@ -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") diff --git a/Sources/ApolloCodegenLib/Templates/FragmentTemplate.swift b/Sources/ApolloCodegenLib/Templates/FragmentTemplate.swift index 1cc4fc49bf..26d22efd6c 100644 --- a/Sources/ApolloCodegenLib/Templates/FragmentTemplate.swift +++ b/Sources/ApolloCodegenLib/Templates/FragmentTemplate.swift @@ -7,6 +7,8 @@ struct FragmentTemplate { func render() -> String { TemplateString( """ + \(HeaderCommentTemplate.render()) + \(ImportStatementTemplate.Operation.render(config)) public struct \(fragment.name): \(schema.name).SelectionSet, Fragment { diff --git a/Sources/ApolloCodegenLib/Templates/HeaderCommentTemplate.swift b/Sources/ApolloCodegenLib/Templates/HeaderCommentTemplate.swift new file mode 100644 index 0000000000..1a4c81389a --- /dev/null +++ b/Sources/ApolloCodegenLib/Templates/HeaderCommentTemplate.swift @@ -0,0 +1,8 @@ +struct HeaderCommentTemplate { + static func render() -> String { + """ + // @generated + // This file was automatically generated and should not be edited. + """ + } +} diff --git a/Sources/ApolloCodegenLib/Templates/InputObjectTemplate.swift b/Sources/ApolloCodegenLib/Templates/InputObjectTemplate.swift index b6e24c57c7..55a8fcfa02 100644 --- a/Sources/ApolloCodegenLib/Templates/InputObjectTemplate.swift +++ b/Sources/ApolloCodegenLib/Templates/InputObjectTemplate.swift @@ -7,6 +7,8 @@ struct InputObjectTemplate { func render() -> String { TemplateString( """ + \(HeaderCommentTemplate.render()) + \(ImportStatementTemplate.SchemaType.render()) struct \(graphqlInputObject.name.firstUppercased): InputObject { diff --git a/Sources/ApolloCodegenLib/Templates/InterfaceTemplate.swift b/Sources/ApolloCodegenLib/Templates/InterfaceTemplate.swift index 015c599130..8a93bf3f96 100644 --- a/Sources/ApolloCodegenLib/Templates/InterfaceTemplate.swift +++ b/Sources/ApolloCodegenLib/Templates/InterfaceTemplate.swift @@ -6,6 +6,8 @@ struct InterfaceTemplate { func render() -> String { TemplateString( """ + \(HeaderCommentTemplate.render()) + \(ImportStatementTemplate.SchemaType.render()) public final class \(graphqlInterface.name): Interface { } diff --git a/Sources/ApolloCodegenLib/Templates/ObjectTemplate.swift b/Sources/ApolloCodegenLib/Templates/ObjectTemplate.swift index c7685e716f..db7a97f2ee 100644 --- a/Sources/ApolloCodegenLib/Templates/ObjectTemplate.swift +++ b/Sources/ApolloCodegenLib/Templates/ObjectTemplate.swift @@ -6,6 +6,8 @@ struct ObjectTemplate { func render() -> String { TemplateString( """ + \(HeaderCommentTemplate.render()) + \(ImportStatementTemplate.SchemaType.render()) public final class \(graphqlObject.name.firstUppercased): Object { diff --git a/Sources/ApolloCodegenLib/Templates/OperationDefinitionTemplate.swift b/Sources/ApolloCodegenLib/Templates/OperationDefinitionTemplate.swift index 5db1f2ef00..c578fe729b 100644 --- a/Sources/ApolloCodegenLib/Templates/OperationDefinitionTemplate.swift +++ b/Sources/ApolloCodegenLib/Templates/OperationDefinitionTemplate.swift @@ -9,6 +9,8 @@ struct OperationDefinitionTemplate { func render() -> String { TemplateString( """ + \(HeaderCommentTemplate.render()) + \(ImportStatementTemplate.Operation.render(config.output)) \(OperationDeclaration(operation.definition)) diff --git a/Sources/ApolloCodegenLib/Templates/SchemaTemplate.swift b/Sources/ApolloCodegenLib/Templates/SchemaTemplate.swift index 10e46447b4..e32c4aa1c7 100644 --- a/Sources/ApolloCodegenLib/Templates/SchemaTemplate.swift +++ b/Sources/ApolloCodegenLib/Templates/SchemaTemplate.swift @@ -5,6 +5,8 @@ struct SchemaTemplate { func render() -> String { TemplateString( """ + \(HeaderCommentTemplate.render()) + \(ImportStatementTemplate.SchemaType.render()) public typealias ID = String diff --git a/Sources/ApolloCodegenLib/Templates/UnionTemplate.swift b/Sources/ApolloCodegenLib/Templates/UnionTemplate.swift index 5541a201f9..02f90db5a1 100644 --- a/Sources/ApolloCodegenLib/Templates/UnionTemplate.swift +++ b/Sources/ApolloCodegenLib/Templates/UnionTemplate.swift @@ -5,6 +5,8 @@ struct UnionTemplate { func render() -> String { TemplateString( """ + \(HeaderCommentTemplate.render()) + \(ImportStatementTemplate.SchemaType.render()) public enum \(graphqlUnion.name): UnionType, Equatable { diff --git a/Tests/ApolloCodegenTests/CodeGeneration/Templates/EnumTemplateTests.swift b/Tests/ApolloCodegenTests/CodeGeneration/Templates/EnumTemplateTests.swift index ac77e29cd9..7b85676a96 100644 --- a/Tests/ApolloCodegenTests/CodeGeneration/Templates/EnumTemplateTests.swift +++ b/Tests/ApolloCodegenTests/CodeGeneration/Templates/EnumTemplateTests.swift @@ -4,10 +4,42 @@ 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 @@ -15,16 +47,35 @@ class EnumTemplateTests: XCTestCase { """ // 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 { @@ -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 { @@ -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)) } } diff --git a/Tests/ApolloCodegenTests/CodeGeneration/Templates/FragmentTemplateTests.swift b/Tests/ApolloCodegenTests/CodeGeneration/Templates/FragmentTemplateTests.swift index 8cb2ec43d2..db5cd3efd0 100644 --- a/Tests/ApolloCodegenTests/CodeGeneration/Templates/FragmentTemplateTests.swift +++ b/Tests/ApolloCodegenTests/CodeGeneration/Templates/FragmentTemplateTests.swift @@ -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 { @@ -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 { @@ -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 { @@ -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 @@ -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)) } @@ -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 { @@ -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 { @@ -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 { @@ -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)) } } diff --git a/Tests/ApolloCodegenTests/CodeGeneration/Templates/InputObjectTemplateTests.swift b/Tests/ApolloCodegenTests/CodeGeneration/Templates/InputObjectTemplateTests.swift index 1f6550cc97..1921fa33c6 100644 --- a/Tests/ApolloCodegenTests/CodeGeneration/Templates/InputObjectTemplateTests.swift +++ b/Tests/ApolloCodegenTests/CodeGeneration/Templates/InputObjectTemplateTests.swift @@ -6,6 +6,7 @@ import JavaScriptCore class InputObjectTemplateTests: XCTestCase { var jsVM: JSVirtualMachine! var jsContext: JSContext! + var subject: InputObjectTemplate! override func setUp() { super.setUp() @@ -15,14 +16,43 @@ class InputObjectTemplateTests: XCTestCase { } override func tearDown() { + subject = nil jsContext = nil jsVM = nil super.tearDown() } - func test_render_boilerplate_givenInputObject_generatesImportStatement() { - let graphqlInputObject = GraphQLInputObjectType.mock("mockInput", fields: [ + private func buildSubject(name: String = "MockInput", fields: [GraphQLInputField] = []) { + subject = InputObjectTemplate( + graphqlInputObject: GraphQLInputObjectType.mock(name, fields: fields) + ) + } + + // MARK: Boilerplate Tests + + func test_render_generatesHeaderComment() { + // given + buildSubject(fields: [ + GraphQLInputField.mock("field", type: .scalar(.integer()), defaultValue: nil) + ]) + + 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(fields: [ GraphQLInputField.mock("field", type: .scalar(.integer()), defaultValue: nil) ]) @@ -32,64 +62,67 @@ class InputObjectTemplateTests: XCTestCase { """ // when - let actual = InputObjectTemplate(graphqlInputObject: graphqlInputObject).render() + let actual = subject.render() // then - expect(actual).to(equalLineByLine(expected, ignoringExtraLines: true)) + expect(actual).to(equalLineByLine(expected, atLine: 4, ignoringExtraLines: true)) } // MARK: Casing Tests func test__render__givenLowercasedInputObjectField__generatesCorrectlyCasedSwiftDefinition() throws { // given - let graphqlInputObject = GraphQLInputObjectType.mock("mockInput", fields: [ - GraphQLInputField.mock("field", type: .scalar(.integer()), defaultValue: nil) - ]) + buildSubject( + name: "mockInput", + fields: [GraphQLInputField.mock("field", type: .scalar(.integer()), defaultValue: nil)] + ) let expected = "struct MockInput: InputObject {" // when - let actual = InputObjectTemplate(graphqlInputObject: graphqlInputObject).render() + 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__givenUppercasedInputObjectField__generatesCorrectlyCasedSwiftDefinition() throws { // given - let graphqlInputObject = GraphQLInputObjectType.mock("MOCKInput", fields: [ - GraphQLInputField.mock("field", type: .scalar(.integer()), defaultValue: nil) - ]) + buildSubject( + name: "MOCKInput", + fields: [GraphQLInputField.mock("field", type: .scalar(.integer()), defaultValue: nil)] + ) let expected = "struct MOCKInput: InputObject {" // when - let actual = InputObjectTemplate(graphqlInputObject: graphqlInputObject).render() + 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__givenMixedCaseInputObjectField__generatesCorrectlyCasedSwiftDefinition() throws { // given - let graphqlInputObject = GraphQLInputObjectType.mock("mOcK_Input", fields: [ - GraphQLInputField.mock("field", type: .scalar(.integer()), defaultValue: nil) - ]) + buildSubject( + name: "mOcK_Input", + fields: [GraphQLInputField.mock("field", type: .scalar(.integer()), defaultValue: nil)] + ) let expected = "struct MOcK_Input: InputObject {" // when - let actual = InputObjectTemplate(graphqlInputObject: graphqlInputObject).render() + let actual = subject.render() // then - expect(actual).to(equalLineByLine(expected, atLine: 3, ignoringExtraLines: true)) + expect(actual).to(equalLineByLine(expected, atLine: 6, ignoringExtraLines: true)) } // MARK: Field Type Tests func test__render__givenSingleFieldType__generatesCorrectParameterAndInitializer_withClosingBrace() throws { // given - let graphqlInputObject = GraphQLInputObjectType.mock("MockInput", fields: [ + buildSubject(fields: [ GraphQLInputField.mock("field", type: .scalar(.string()), defaultValue: nil) ]) @@ -110,15 +143,15 @@ class InputObjectTemplateTests: XCTestCase { """ // when - let actual = InputObjectTemplate(graphqlInputObject: graphqlInputObject).render() + let actual = subject.render() // then - expect(actual).to(equalLineByLine(expected, atLine: 6, ignoringExtraLines: false)) + expect(actual).to(equalLineByLine(expected, atLine: 9, ignoringExtraLines: false)) } func test__render__givenAllPossibleSchemaInputFieldTypes__generatesCorrectParametersAndInitializer() throws { // given - let graphqlInputObject = GraphQLInputObjectType.mock("MockInput", fields: [ + buildSubject(fields: [ GraphQLInputField.mock( "stringField", type: .scalar(.string()), @@ -219,17 +252,17 @@ class InputObjectTemplateTests: XCTestCase { """ // when - let actual = InputObjectTemplate(graphqlInputObject: graphqlInputObject).render() + let actual = subject.render() // then - expect(actual).to(equalLineByLine(expected, atLine: 6, ignoringExtraLines: true)) + expect(actual).to(equalLineByLine(expected, atLine: 9, ignoringExtraLines: true)) } // MARK: Nullable Field Tests func test__render__given_NullableField_NoDefault__generates_NullableParameter_InitializerNilDefault() throws { // given - let graphqlInputObject = GraphQLInputObjectType.mock("MockInput", fields: [ + buildSubject(fields: [ GraphQLInputField.mock("nullable", type: .scalar(.integer()), defaultValue: nil) ]) @@ -246,15 +279,15 @@ class InputObjectTemplateTests: XCTestCase { """ // when - let actual = InputObjectTemplate(graphqlInputObject: graphqlInputObject).render() + let actual = subject.render() // then - expect(actual).to(equalLineByLine(expected, atLine: 6, ignoringExtraLines: true)) + expect(actual).to(equalLineByLine(expected, atLine: 9, ignoringExtraLines: true)) } func test__render__given_NullableField_WithDefault__generates_NullableParameter_NoInitializerDefault() throws { // given - let graphqlInputObject = GraphQLInputObjectType.mock("MockInput", fields: [ + buildSubject(fields: [ GraphQLInputField.mock("nullableWithDefault", type: .scalar(.integer()), defaultValue: JSValue(int32: 3, in: jsContext)) ]) @@ -271,15 +304,15 @@ class InputObjectTemplateTests: XCTestCase { """ // when - let actual = InputObjectTemplate(graphqlInputObject: graphqlInputObject).render() + let actual = subject.render() // then - expect(actual).to(equalLineByLine(expected, atLine: 6, ignoringExtraLines: true)) + expect(actual).to(equalLineByLine(expected, atLine: 9, ignoringExtraLines: true)) } func test__render__given_NonNullableField_NoDefault__generates_NonNullableNonOptionalParameter_NoInitializerDefault() throws { // given - let graphqlInputObject = GraphQLInputObjectType.mock("MockInput", fields: [ + buildSubject(fields: [ GraphQLInputField.mock("nonNullable", type: .nonNull(.scalar(.integer())), defaultValue: nil) ]) @@ -296,15 +329,15 @@ class InputObjectTemplateTests: XCTestCase { """ // when - let actual = InputObjectTemplate(graphqlInputObject: graphqlInputObject).render() + let actual = subject.render() // then - expect(actual).to(equalLineByLine(expected, atLine: 6, ignoringExtraLines: true)) + expect(actual).to(equalLineByLine(expected, atLine: 9, ignoringExtraLines: true)) } func test__render__given_NonNullableField_WithDefault__generates_OptionalParameter_NoInitializerDefault() throws { // given - let graphqlInputObject = GraphQLInputObjectType.mock("MockInput", fields: [ + buildSubject(fields: [ GraphQLInputField.mock("nonNullableWithDefault", type: .nonNull(.scalar(.integer())), defaultValue: JSValue(int32: 3, in: jsContext)) ]) @@ -321,15 +354,15 @@ class InputObjectTemplateTests: XCTestCase { """ // when - let actual = InputObjectTemplate(graphqlInputObject: graphqlInputObject).render() + let actual = subject.render() // then - expect(actual).to(equalLineByLine(expected, atLine: 6, ignoringExtraLines: true)) + expect(actual).to(equalLineByLine(expected, atLine: 9, ignoringExtraLines: true)) } func test__render__given_NullableList_NullableItem_NoDefault__generates_NullableParameter_OptionalItem_InitializerNilDefault() throws { // given - let graphqlInputObject = GraphQLInputObjectType.mock("MockInput", fields: [ + buildSubject(fields: [ GraphQLInputField.mock("nullableListNullableItem", type: .list(.scalar(.string())), defaultValue: nil) ]) @@ -346,15 +379,15 @@ class InputObjectTemplateTests: XCTestCase { """ // when - let actual = InputObjectTemplate(graphqlInputObject: graphqlInputObject).render() + let actual = subject.render() // then - expect(actual).to(equalLineByLine(expected, atLine: 6, ignoringExtraLines: true)) + expect(actual).to(equalLineByLine(expected, atLine: 9, ignoringExtraLines: true)) } func test__render__given_NullableList_NullableItem_WithDefault__generates_NullableParameter_OptionalItem_NoInitializerDefault() throws { // given - let graphqlInputObject = GraphQLInputObjectType.mock("MockInput", fields: [ + buildSubject(fields: [ GraphQLInputField.mock("nullableListNullableItemWithDefault", type: .list(.scalar(.string())), defaultValue: JSValue(object: ["val"], in: jsContext)) ]) @@ -371,15 +404,15 @@ class InputObjectTemplateTests: XCTestCase { """ // when - let actual = InputObjectTemplate(graphqlInputObject: graphqlInputObject).render() + let actual = subject.render() // then - expect(actual).to(equalLineByLine(expected, atLine: 6, ignoringExtraLines: true)) + expect(actual).to(equalLineByLine(expected, atLine: 9, ignoringExtraLines: true)) } func test__render__given_NullableList_NonNullableItem_NoDefault__generates_NullableParameter_NonOptionalItem_InitializerNilDefault() throws { // given - let graphqlInputObject = GraphQLInputObjectType.mock("MockInput", fields: [ + buildSubject(fields: [ GraphQLInputField.mock("nullableListNonNullableItem", type: .list(.nonNull(.scalar(.string()))), defaultValue: nil) ]) @@ -396,15 +429,15 @@ class InputObjectTemplateTests: XCTestCase { """ // when - let actual = InputObjectTemplate(graphqlInputObject: graphqlInputObject).render() + let actual = subject.render() // then - expect(actual).to(equalLineByLine(expected, atLine: 6, ignoringExtraLines: true)) + expect(actual).to(equalLineByLine(expected, atLine: 9, ignoringExtraLines: true)) } func test__render__given_NullableList_NonNullableItem_WithDefault__generates_NullableParameter_NonOptionalItem_NoInitializerDefault() throws { // given - let graphqlInputObject = GraphQLInputObjectType.mock("MockInput", fields: [ + buildSubject(fields: [ GraphQLInputField.mock("nullableListNonNullableItemWithDefault", type: .list(.nonNull(.scalar(.string()))), defaultValue: JSValue(object: ["val"], in: jsContext)) ]) @@ -421,15 +454,15 @@ class InputObjectTemplateTests: XCTestCase { """ // when - let actual = InputObjectTemplate(graphqlInputObject: graphqlInputObject).render() + let actual = subject.render() // then - expect(actual).to(equalLineByLine(expected, atLine: 6, ignoringExtraLines: true)) + expect(actual).to(equalLineByLine(expected, atLine: 9, ignoringExtraLines: true)) } func test__render__given_NonNullableList_NullableItem_NoDefault__generates_NonNullableNonOptionalParameter_OptionalItem_NoInitializerDefault() throws { // given - let graphqlInputObject = GraphQLInputObjectType.mock("MockInput", fields: [ + buildSubject(fields: [ GraphQLInputField.mock("nonNullableListNullableItem", type: .nonNull(.list(.scalar(.string()))), defaultValue: nil) ]) @@ -446,15 +479,15 @@ class InputObjectTemplateTests: XCTestCase { """ // when - let actual = InputObjectTemplate(graphqlInputObject: graphqlInputObject).render() + let actual = subject.render() // then - expect(actual).to(equalLineByLine(expected, atLine: 6, ignoringExtraLines: true)) + expect(actual).to(equalLineByLine(expected, atLine: 9, ignoringExtraLines: true)) } func test__render__given_NonNullableList_NullableItem_WithDefault__generates_OptionalParameter_OptionalItem_NoInitializerDefault() throws { // given - let graphqlInputObject = GraphQLInputObjectType.mock("MockInput", fields: [ + buildSubject(fields: [ GraphQLInputField.mock("nonNullableListNullableItemWithDefault", type: .nonNull(.list(.scalar(.string()))), defaultValue: JSValue(object: ["val"], in: jsContext)) ]) @@ -471,15 +504,15 @@ class InputObjectTemplateTests: XCTestCase { """ // when - let actual = InputObjectTemplate(graphqlInputObject: graphqlInputObject).render() + let actual = subject.render() // then - expect(actual).to(equalLineByLine(expected, atLine: 6, ignoringExtraLines: true)) + expect(actual).to(equalLineByLine(expected, atLine: 9, ignoringExtraLines: true)) } func test__render__given_NonNullableList_NonNullableItem_NoDefault__generates_NonNullableNonOptionalParameter_NonOptionalItem_NoInitializerDefault() throws { // given - let graphqlInputObject = GraphQLInputObjectType.mock("MockInput", fields: [ + buildSubject(fields: [ GraphQLInputField.mock("nonNullableListNonNullableItem", type: .nonNull(.list(.nonNull(.scalar(.string())))), defaultValue: nil) ]) @@ -496,15 +529,15 @@ class InputObjectTemplateTests: XCTestCase { """ // when - let actual = InputObjectTemplate(graphqlInputObject: graphqlInputObject).render() + let actual = subject.render() // then - expect(actual).to(equalLineByLine(expected, atLine: 6, ignoringExtraLines: true)) + expect(actual).to(equalLineByLine(expected, atLine: 9, ignoringExtraLines: true)) } func test__render__given_NonNullableList_NonNullableItem_WithDefault__generates_OptionalParameter_NonOptionalItem_NoInitializerDefault() throws { // given - let graphqlInputObject = GraphQLInputObjectType.mock("MockInput", fields: [ + buildSubject(fields: [ GraphQLInputField.mock("nonNullableListNonNullableItemWithDefault", type: .nonNull(.list(.nonNull(.scalar(.string())))), defaultValue: JSValue(object: ["val"], in: jsContext)) ]) @@ -521,9 +554,9 @@ class InputObjectTemplateTests: XCTestCase { """ // when - let actual = InputObjectTemplate(graphqlInputObject: graphqlInputObject).render() + let actual = subject.render() // then - expect(actual).to(equalLineByLine(expected, atLine: 6, ignoringExtraLines: true)) + expect(actual).to(equalLineByLine(expected, atLine: 9, ignoringExtraLines: true)) } } diff --git a/Tests/ApolloCodegenTests/CodeGeneration/Templates/InterfaceTemplateTests.swift b/Tests/ApolloCodegenTests/CodeGeneration/Templates/InterfaceTemplateTests.swift index d79695ba42..69906e9475 100644 --- a/Tests/ApolloCodegenTests/CodeGeneration/Templates/InterfaceTemplateTests.swift +++ b/Tests/ApolloCodegenTests/CodeGeneration/Templates/InterfaceTemplateTests.swift @@ -4,26 +4,69 @@ import Nimble import ApolloCodegenTestSupport class InterfaceTemplateTests: XCTestCase { + var subject: InterfaceTemplate! - func test_render_givenSchemaInterface_generatesSwiftClass() throws { - // given - let graphqlInterface = GraphQLInterfaceType.mock( - "MockInterface", - fields: [:], - interfaces: [] + override func tearDown() { + subject = nil + + super.tearDown() + } + + private func buildSubject() { + subject = InterfaceTemplate( + graphqlInterface: GraphQLInterfaceType.mock("MockInterface", fields: [:], interfaces: []) ) - let template = InterfaceTemplate(graphqlInterface: graphqlInterface) + } + + // MARK: Boilerplate Tests + + func test_render_generatesHeaderComment() { + // given + buildSubject() + + 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() let expected = """ import ApolloAPI + """ + + // when + let actual = subject.render() + + // then + expect(actual).to(equalLineByLine(expected, atLine: 4, ignoringExtraLines: true)) + } + + // MARK: Class Definition Tests + + func test_render_givenSchemaInterface_generatesSwiftClass() throws { + // given + buildSubject() + + let expected = """ public final class MockInterface: Interface { } """ // when - let actual = template.render() + let actual = subject.render() // then - expect(actual).to(equalLineByLine(expected)) + expect(actual).to(equalLineByLine(expected, atLine: 6)) } } diff --git a/Tests/ApolloCodegenTests/CodeGeneration/Templates/ObjectTemplateTests.swift b/Tests/ApolloCodegenTests/CodeGeneration/Templates/ObjectTemplateTests.swift index 6c46a5224d..a354160756 100644 --- a/Tests/ApolloCodegenTests/CodeGeneration/Templates/ObjectTemplateTests.swift +++ b/Tests/ApolloCodegenTests/CodeGeneration/Templates/ObjectTemplateTests.swift @@ -4,60 +4,90 @@ import Nimble import ApolloCodegenTestSupport class ObjectTemplateTests: XCTestCase { + var subject: ObjectTemplate! + + override func tearDown() { + subject = nil + + super.tearDown() + } + + private func buildSubject(name: String = "Dog", interfaces: [GraphQLInterfaceType] = []) { + subject = ObjectTemplate( + graphqlObject: GraphQLObjectType.mock(name, interfaces: interfaces) + ) + } // MARK: Boilerplate tests - func test_boilerplate_givenSchemaType_generatesImportStatement() { + func test_render_generatesHeaderComment() { // given - let graphqlObject = GraphQLObjectType.mock("Dog") + buildSubject() let expected = """ - import ApolloAPI + // @generated + // This file was automatically generated and should not be edited. """ // when - let actual = ObjectTemplate(graphqlObject: graphqlObject).render() + let actual = subject.render() // then expect(actual).to(equalLineByLine(expected, ignoringExtraLines: true)) } - func test_boilerplate_givenSchemaType_generatesSwiftClassDefinition() { + func test_render_generatesImportStatement() { // given - let graphqlObject = GraphQLObjectType.mock("Dog") + buildSubject() let expected = """ - public final class Dog: Object { - override public class var __typename: String { "Dog" } + import ApolloAPI """ // when - let actual = ObjectTemplate(graphqlObject: graphqlObject).render() + let actual = subject.render() // then - expect(actual).to(equalLineByLine(expected, atLine: 3, ignoringExtraLines: true)) + expect(actual).to(equalLineByLine(expected, atLine: 4, ignoringExtraLines: true)) } - func test_boilerplate_givenSchemaType_generatesClosingBrace() { + func test_render_generatesClosingBrace() { // given - let graphqlObject = GraphQLObjectType.mock("Dog") + buildSubject() // when - let actual = ObjectTemplate(graphqlObject: graphqlObject).render() + let actual = subject.render() // then expect(String(actual.reversed())).to(equalLineByLine("}", ignoringExtraLines: true)) } + // MARK: Class Definition Tests + + func test_render_givenSchemaType_generatesSwiftClassDefinition() { + // given + buildSubject() + + let expected = """ + public final class Dog: Object { + override public class var __typename: String { "Dog" } + + """ + + // when + let actual = subject.render() + + // then + expect(actual).to(equalLineByLine(expected, atLine: 6, ignoringExtraLines: true)) + } + // MARK: Metadata Tests func test_render_givenSchemaType_generatesTypeMetadata() { // given - let graphqlObject = GraphQLObjectType.mock( - "Dog", - interfaces: [ + buildSubject(interfaces: [ GraphQLInterfaceType.mock("Animal", fields: ["species": GraphQLField.mock("species", type: .scalar(.string()))]), GraphQLInterfaceType.mock("Pet", fields: ["name": GraphQLField.mock("name", type: .scalar(.string()))]) ] @@ -72,9 +102,9 @@ class ObjectTemplateTests: XCTestCase { """ // when - let actual = ObjectTemplate(graphqlObject: graphqlObject).render() + let actual = subject.render() // then - expect(actual).to(equalLineByLine(expected, atLine: 6, ignoringExtraLines: true)) + expect(actual).to(equalLineByLine(expected, atLine: 9, ignoringExtraLines: true)) } } diff --git a/Tests/ApolloCodegenTests/CodeGeneration/Templates/OperationDefinitionTemplateTests.swift b/Tests/ApolloCodegenTests/CodeGeneration/Templates/OperationDefinitionTemplateTests.swift index deeb05dd8e..88df8a9d00 100644 --- a/Tests/ApolloCodegenTests/CodeGeneration/Templates/OperationDefinitionTemplateTests.swift +++ b/Tests/ApolloCodegenTests/CodeGeneration/Templates/OperationDefinitionTemplateTests.swift @@ -59,6 +59,25 @@ class OperationDefinitionTemplateTests: XCTestCase { ) } + // MARK: - Boilerplate Tests + + func test__render__generatesHeaderComment() throws { + // given + let expected = """ + // @generated + // This file was automatically generated and should not be edited. + + """ + + // when + try buildSubjectAndOperation() + + let actual = subject.render() + + // then + expect(actual).to(equalLineByLine(expected, ignoringExtraLines: true)) + } + // MARK: - Import Statements func test__generate__givenFileOutput_inSchemaModule_schemaModuleManuallyLinked_generatesImportNotIncludingSchemaModule() throws { @@ -80,7 +99,7 @@ class OperationDefinitionTemplateTests: XCTestCase { let actual = subject.render() // then - expect(actual).to(equalLineByLine(expected, ignoringExtraLines: true)) + expect(actual).to(equalLineByLine(expected, atLine: 4, ignoringExtraLines: true)) } func test__generate__givenFileOutput_inSchemaModule_schemaModuleNotManuallyLinked_generatesImportNotIncludingSchemaModule() throws { @@ -102,7 +121,7 @@ class OperationDefinitionTemplateTests: XCTestCase { let actual = subject.render() // then - expect(actual).to(equalLineByLine(expected, ignoringExtraLines: true)) + expect(actual).to(equalLineByLine(expected, atLine: 4, ignoringExtraLines: true)) } func test__generate__givenFileOutput_notInSchemaModule_schemaModuleNotManuallyLinked_generatesImportIncludingSchemaModule() throws { @@ -125,7 +144,7 @@ class OperationDefinitionTemplateTests: XCTestCase { let actual = subject.render() // then - expect(actual).to(equalLineByLine(expected, ignoringExtraLines: true)) + expect(actual).to(equalLineByLine(expected, atLine: 4, ignoringExtraLines: true)) } // MARK: - Operation Definition @@ -144,7 +163,7 @@ class OperationDefinitionTemplateTests: 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__generate__givenQueryWithNameEndingInQuery_generatesQueryOperationWithoutDoubledTypeSuffix() throws { @@ -169,7 +188,7 @@ class OperationDefinitionTemplateTests: 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__generate__givenMutationWithNameEndingInQuery_generatesQueryOperationWithBothSuffixes() throws { @@ -208,7 +227,7 @@ class OperationDefinitionTemplateTests: 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__generate__givenMutation_generatesMutationOperation() throws { @@ -247,7 +266,7 @@ class OperationDefinitionTemplateTests: 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__generate__givenSubscription_generatesSubscriptionOperation() throws { @@ -286,6 +305,6 @@ class OperationDefinitionTemplateTests: XCTestCase { let actual = subject.render() // then - expect(actual).to(equalLineByLine(expected, atLine: 3, ignoringExtraLines: true)) + expect(actual).to(equalLineByLine(expected, atLine: 6, ignoringExtraLines: true)) } } diff --git a/Tests/ApolloCodegenTests/CodeGeneration/Templates/SchemaTemplateTests.swift b/Tests/ApolloCodegenTests/CodeGeneration/Templates/SchemaTemplateTests.swift index c8f855842b..20e92a1ca3 100644 --- a/Tests/ApolloCodegenTests/CodeGeneration/Templates/SchemaTemplateTests.swift +++ b/Tests/ApolloCodegenTests/CodeGeneration/Templates/SchemaTemplateTests.swift @@ -4,43 +4,134 @@ import Nimble import ApolloCodegenTestSupport class SchemaTemplateTests: XCTestCase { + var subject: SchemaTemplate! - func test__generate__givenSchemaName_generatesBoilerplate() { + override func tearDown() { + subject = nil + + super.tearDown() + } + + private func buildSubject(name: String = "TestSchema", referencedTypes: IR.Schema.ReferencedTypes = .init([])) { + subject = SchemaTemplate(schema: IR.Schema(name: name, referencedTypes: referencedTypes)) + } + + // MARK: Boilerplate Tests + + func test__render__generatesHeaderComment() { // given - let schema = IR.Schema(name: "TestSchemaName", referencedTypes: .init([])) - let template = SchemaTemplate(schema: schema) + buildSubject() + + 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() let expected = """ import ApolloAPI + """ + + // when + let actual = subject.render() + + // then + expect(actual).to(equalLineByLine(expected, atLine: 4, ignoringExtraLines: true)) + } + + func test__render__generatesIDTypeAlias() { + // given + buildSubject() + + let expected = """ public typealias ID = String + """ + + // when + let actual = subject.render() + + // then + expect(actual).to(equalLineByLine(expected, atLine: 6, ignoringExtraLines: true)) + } + + // MARK: Protocol Tests + + + + func test__render__givenSchemaName_generatesSelectionSetProtocol() { + // given + buildSubject() + + let expected = """ public protocol SelectionSet: ApolloAPI.SelectionSet & ApolloAPI.RootSelectionSet - where Schema == TestSchemaName.Schema {} + where Schema == TestSchema.Schema {} + + """ + + // when + let actual = subject.render() + + // then + expect(actual).to(equalLineByLine(expected, atLine: 8, ignoringExtraLines: true)) + } + + func test__render__givenSchemaName_generatesTypeCaseProtocol() { + // given + buildSubject() + let expected = """ public protocol TypeCase: ApolloAPI.SelectionSet & ApolloAPI.TypeCase - where Schema == TestSchemaName.Schema {} + where Schema == TestSchema.Schema {} + + """ + + // when + let actual = subject.render() + + // then + expect(actual).to(equalLineByLine(expected, atLine: 11, ignoringExtraLines: true)) + } + + // MARK: Schema Tests + + func test__render__generatesEnumDefinition() { + // given + buildSubject() + let expected = """ public enum Schema: SchemaConfiguration { - public static func objectType(forTypename __typename: String) -> Object.Type? { - switch __typename { """ // when - let actual = template.render() + let actual = subject.render() // then - expect(actual).to(equalLineByLine(expected, ignoringExtraLines: true)) + expect(actual).to(equalLineByLine(expected, atLine: 14, ignoringExtraLines: true)) } - func test__generate__givenWithReferencedObjects_generatesObjectTypeFunction() { + func test__render__givenWithReferencedObjects_generatesObjectTypeFunction() { // given - let schema = IR.Schema(name: "ObjectSchema", referencedTypes: .init([ - GraphQLObjectType.mock("ObjA"), - GraphQLObjectType.mock("ObjB"), - GraphQLObjectType.mock("ObjC"), - ])) - let template = SchemaTemplate(schema: schema) + buildSubject( + name: "ObjectSchema", + referencedTypes: .init([ + GraphQLObjectType.mock("ObjA"), + GraphQLObjectType.mock("ObjB"), + GraphQLObjectType.mock("ObjC"), + ]) + ) let expected = """ public static func objectType(forTypename __typename: String) -> Object.Type? { @@ -55,23 +146,25 @@ class SchemaTemplateTests: XCTestCase { """ // when - let actual = template.render() + let actual = subject.render() // then - expect(actual).to(equalLineByLine(expected, atLine: 12)) + expect(actual).to(equalLineByLine(expected, atLine: 15)) } - func test__generate__givenWithReferencedOtherTypes_generatesObjectTypeNotIncludingNonObjectTypesFunction() { + func test__render__givenWithReferencedOtherTypes_generatesObjectTypeNotIncludingNonObjectTypesFunction() { // given - let schema = IR.Schema(name: "ObjectSchema", referencedTypes: .init([ - GraphQLObjectType.mock("ObjectA"), - GraphQLInterfaceType.mock("InterfaceB"), - GraphQLUnionType.mock("UnionC"), - GraphQLScalarType.mock(name: "ScalarD"), - GraphQLEnumType.mock(name: "EnumE"), - GraphQLInputObjectType.mock("InputObjectC"), - ])) - let template = SchemaTemplate(schema: schema) + buildSubject( + name: "ObjectSchema", + referencedTypes: .init([ + GraphQLObjectType.mock("ObjectA"), + GraphQLInterfaceType.mock("InterfaceB"), + GraphQLUnionType.mock("UnionC"), + GraphQLScalarType.mock(name: "ScalarD"), + GraphQLEnumType.mock(name: "EnumE"), + GraphQLInputObjectType.mock("InputObjectC"), + ]) + ) let expected = """ public static func objectType(forTypename __typename: String) -> Object.Type? { @@ -84,9 +177,9 @@ class SchemaTemplateTests: XCTestCase { """ // when - let actual = template.render() + let actual = subject.render() // then - expect(actual).to(equalLineByLine(expected, atLine: 12)) + expect(actual).to(equalLineByLine(expected, atLine: 15)) } } diff --git a/Tests/ApolloCodegenTests/CodeGeneration/Templates/UnionTemplateTests.swift b/Tests/ApolloCodegenTests/CodeGeneration/Templates/UnionTemplateTests.swift index 4fb9c62709..774d4f1fc8 100644 --- a/Tests/ApolloCodegenTests/CodeGeneration/Templates/UnionTemplateTests.swift +++ b/Tests/ApolloCodegenTests/CodeGeneration/Templates/UnionTemplateTests.swift @@ -4,50 +4,97 @@ import Nimble import ApolloAPI class UnionTemplateTests: XCTestCase { - let graphqlUnion = GraphQLUnionType.mock( - "ClassroomPet", - types: [ - GraphQLObjectType.mock("Cat"), - GraphQLObjectType.mock("Bird"), - GraphQLObjectType.mock("Rat"), - GraphQLObjectType.mock("PetRock") - ] - ) + var subject: UnionTemplate! + + override func tearDown() { + subject = nil + + super.tearDown() + } + + private func buildSubject() { + subject = UnionTemplate( + moduleName: "ModuleAPI", + graphqlUnion: GraphQLUnionType.mock( + "ClassroomPet", + types: [ + GraphQLObjectType.mock("Cat"), + GraphQLObjectType.mock("Bird"), + GraphQLObjectType.mock("Rat"), + GraphQLObjectType.mock("PetRock") + ] + ) + ) + } // MARK: Boilerplate tests - func test_boilerplate_importsApolloAPI_generatesSwiftEnumDefinition() throws { + func test_render_generatesHeaderComment() throws { // given + buildSubject() + let expected = """ - import ApolloAPI + // @generated + // This file was automatically generated and should not be edited. - public enum ClassroomPet: UnionType, Equatable { """ // when - let actual = UnionTemplate(moduleName: "ModuleAPI", graphqlUnion: graphqlUnion).render() + let actual = subject.render() // then expect(actual).to(equalLineByLine(expected, ignoringExtraLines: true)) } - func test_boilerplate_generatesEnumClosingBrace() throws { + func test_render_gneratesImportStatement() throws { + // given + buildSubject() + let expected = """ - } + import ApolloAPI + """ // when - let actual = UnionTemplate(moduleName: "ModuleAPI", graphqlUnion: graphqlUnion).render() + let actual = subject.render() + + // then + expect(actual).to(equalLineByLine(expected, atLine: 4, ignoringExtraLines: true)) + } + + func test_render_generatesClosingBrace() throws { + // given + buildSubject() + + // when + let actual = subject.render() // then - expect(String(actual.reversed())).to(equalLineByLine(expected, ignoringExtraLines: true)) + expect(String(actual.reversed())).to(equalLineByLine("}", ignoringExtraLines: true)) } // MARK: Enum Generation Tests - func test_render_givenSchemaUnion_generatesEnumCases() throws { + func test_render_generatesSwiftEnumDefinition() throws { + // given + buildSubject() + let expected = """ public enum ClassroomPet: UnionType, Equatable { + """ + + // when + let actual = subject.render() + + // then + expect(actual).to(equalLineByLine(expected, atLine: 6, ignoringExtraLines: true)) + } + + func test_render_givenSchemaUnion_generatesEnumCases() throws { + // given + buildSubject() + + let expected = """ case Cat(Cat) case Bird(Bird) case Rat(Rat) @@ -55,13 +102,16 @@ class UnionTemplateTests: XCTestCase { """ // when - let actual = UnionTemplate(moduleName: "ModuleAPI", graphqlUnion: graphqlUnion).render() + let actual = subject.render() // then - expect(actual).to(equalLineByLine(expected, atLine: 3, ignoringExtraLines: true)) + expect(actual).to(equalLineByLine(expected, atLine: 7, ignoringExtraLines: true)) } func test_render_givenSchemaUnion_generatesEnumInitializer() throws { + // given + buildSubject() + let expected = """ public init?(_ object: Object) { switch object { @@ -75,13 +125,16 @@ class UnionTemplateTests: XCTestCase { """ // when - let actual = UnionTemplate(moduleName: "ModuleAPI", graphqlUnion: graphqlUnion).render() + let actual = subject.render() // then - expect(actual).to(equalLineByLine(expected, atLine: 9, ignoringExtraLines: true)) + expect(actual).to(equalLineByLine(expected, atLine: 12, ignoringExtraLines: true)) } func test_render_givenSchemaUnion_generatesObjectProperty() throws { + // given + buildSubject() + let expected = """ public var object: Object { switch self { @@ -95,13 +148,16 @@ class UnionTemplateTests: XCTestCase { """ // when - let actual = UnionTemplate(moduleName: "ModuleAPI", graphqlUnion: graphqlUnion).render() + let actual = subject.render() // then - expect(actual).to(equalLineByLine(expected, atLine: 19, ignoringExtraLines: true)) + expect(actual).to(equalLineByLine(expected, atLine: 22, ignoringExtraLines: true)) } func test_render_givenSchemaUnion_generatesPossibleTypesProperty() throws { + // given + buildSubject() + let expected = """ public static let possibleTypes: [Object.Type] = [ ModuleAPI.Cat.self, @@ -112,9 +168,9 @@ class UnionTemplateTests: XCTestCase { """ // when - let actual = UnionTemplate(moduleName: "ModuleAPI", graphqlUnion: graphqlUnion).render() + let actual = subject.render() // then - expect(actual).to(equalLineByLine(expected, atLine: 29, ignoringExtraLines: true)) + expect(actual).to(equalLineByLine(expected, atLine: 32, ignoringExtraLines: true)) } }