From 4b44673f847abf9342e07b0c8b47b7334a4c7daf Mon Sep 17 00:00:00 2001 From: Calvin Cestari Date: Thu, 10 Mar 2022 11:50:51 -0800 Subject: [PATCH] Add ImportStatementTemplate tests --- Apollo.xcodeproj/project.pbxproj | 4 + .../ImportStatementTemplateTests.swift | 207 ++++++++++++++++++ 2 files changed, 211 insertions(+) create mode 100644 Tests/ApolloCodegenTests/CodeGeneration/Templates/ImportStatementTemplateTests.swift diff --git a/Apollo.xcodeproj/project.pbxproj b/Apollo.xcodeproj/project.pbxproj index b7c9135c37..26aa0d9165 100644 --- a/Apollo.xcodeproj/project.pbxproj +++ b/Apollo.xcodeproj/project.pbxproj @@ -377,6 +377,7 @@ E66F8899276C15580000BDA8 /* ObjectFileGenerator.swift in Sources */ = {isa = PBXBuildFile; fileRef = E66F8898276C15580000BDA8 /* ObjectFileGenerator.swift */; }; E674DB41274C0A9B009BB90E /* Glob.swift in Sources */ = {isa = PBXBuildFile; fileRef = E674DB40274C0A9B009BB90E /* Glob.swift */; }; E674DB43274C0AD9009BB90E /* GlobTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E674DB42274C0AD9009BB90E /* GlobTests.swift */; }; + E686B0D227D951630095D04E /* ImportStatementTemplateTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E686B0D127D951630095D04E /* ImportStatementTemplateTests.swift */; }; E68D824527A1D8A60040A46F /* ObjectTemplateTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E64F7EC227A1243A0059C021 /* ObjectTemplateTests.swift */; }; E68D824727A228A80040A46F /* SchemaModuleFileGenerator.swift in Sources */ = {isa = PBXBuildFile; fileRef = E68D824627A228A80040A46F /* SchemaModuleFileGenerator.swift */; }; E69BEDA52798B86D00000D10 /* InputObjectTemplate.swift in Sources */ = {isa = PBXBuildFile; fileRef = E69BEDA42798B86D00000D10 /* InputObjectTemplate.swift */; }; @@ -1105,6 +1106,7 @@ E66F8898276C15580000BDA8 /* ObjectFileGenerator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ObjectFileGenerator.swift; sourceTree = ""; }; E674DB40274C0A9B009BB90E /* Glob.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Glob.swift; sourceTree = ""; }; E674DB42274C0AD9009BB90E /* GlobTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GlobTests.swift; sourceTree = ""; }; + E686B0D127D951630095D04E /* ImportStatementTemplateTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ImportStatementTemplateTests.swift; sourceTree = ""; }; E68D824627A228A80040A46F /* SchemaModuleFileGenerator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SchemaModuleFileGenerator.swift; sourceTree = ""; }; E69BEDA42798B86D00000D10 /* InputObjectTemplate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InputObjectTemplate.swift; sourceTree = ""; }; E69BEDA62798B89600000D10 /* InputObjectTemplateTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InputObjectTemplateTests.swift; sourceTree = ""; }; @@ -2089,6 +2091,7 @@ DE5FD608276956C70033EE23 /* SchemaTemplateTests.swift */, E6B42D0C27A4749100A3BD58 /* SwiftPackageManagerModuleTemplateTests.swift */, E64F7EB927A085D90059C021 /* UnionTemplateTests.swift */, + E686B0D127D951630095D04E /* ImportStatementTemplateTests.swift */, ); path = Templates; sourceTree = ""; @@ -3310,6 +3313,7 @@ E6B42D0D27A4749100A3BD58 /* SwiftPackageManagerModuleTemplateTests.swift in Sources */, 9F62DFBF2590560000E6E808 /* Helpers.swift in Sources */, DE296539279B3B8200BF9B49 /* SelectionSetTemplateTests.swift in Sources */, + E686B0D227D951630095D04E /* ImportStatementTemplateTests.swift in Sources */, E66F8897276C136B0000BDA8 /* ObjectFileGeneratorTests.swift in Sources */, DE79642B276999E700978A03 /* IRNamedFragmentBuilderTests.swift in Sources */, E69BEDA72798B89600000D10 /* InputObjectTemplateTests.swift in Sources */, diff --git a/Tests/ApolloCodegenTests/CodeGeneration/Templates/ImportStatementTemplateTests.swift b/Tests/ApolloCodegenTests/CodeGeneration/Templates/ImportStatementTemplateTests.swift new file mode 100644 index 0000000000..87267e0f1e --- /dev/null +++ b/Tests/ApolloCodegenTests/CodeGeneration/Templates/ImportStatementTemplateTests.swift @@ -0,0 +1,207 @@ +import XCTest +@testable import ApolloCodegenLib +import Nimble +import ApolloCodegenTestSupport + +class ImportStatementTemplateTests: XCTestCase { + + var config: ApolloCodegenConfiguration.FileOutput! + + override func tearDown() { + config = nil + + super.tearDown() + } + + // MARK: Helpers + + func buildConfig( + moduleType: ApolloCodegenConfiguration.SchemaTypesFileOutput.ModuleType, + operations: ApolloCodegenConfiguration.OperationsFileOutput + ) { + config = .mock( + moduleType: moduleType, + schemaName: "ImportStatementTestsSchema", + operations: operations + ) + } + + // MARK: Tests for operations generated into the schema module (schema module import not expected) + + func test__operationRender__givenOperationsOutput_inSchemaModule_whenModuleType_none_generatesImportNotIncludingSchemaName() throws { + // given + buildConfig( + moduleType: .none, + operations: .inSchemaModule + ) + + let expected = + """ + import ApolloAPI + """ + + // when + let actual = ImportStatementTemplate.Operation.render(config).description + + // then + expect(actual).to(equalLineByLine(expected)) + } + + func test__operationRender__givenOperationsOutput_inSchemaModule_whenModuleType_swiftPackageManager_generatesImportNotIncludingSchemaName() throws { + // given + buildConfig( + moduleType: .swiftPackageManager, + operations: .inSchemaModule + ) + + let expected = + """ + import ApolloAPI + """ + + // when + let actual = ImportStatementTemplate.Operation.render(config).description + + // then + expect(actual).to(equalLineByLine(expected)) + } + + func test__operationRender__givenOperationsOutput_inSchemaModule_whenModuleType_other_generatesImportNotIncludingSchemaName() throws { + // given + buildConfig( + moduleType: .other, + operations: .inSchemaModule + ) + + let expected = + """ + import ApolloAPI + """ + + // when + let actual = ImportStatementTemplate.Operation.render(config).description + + // then + expect(actual).to(equalLineByLine(expected)) + } + + // MARK: Tests for operations generated outside the schema module + + func test__operationRender__givenOperationsOutput_relative_whenModuleType_none_generatesImportNotIncludingSchemaName() throws { + // given + buildConfig( + moduleType: .none, + operations: .relative(subpath: nil) + ) + + let expected = + """ + import ApolloAPI + """ + + // when + let actual = ImportStatementTemplate.Operation.render(config).description + + // then + expect(actual).to(equalLineByLine(expected)) + } + + func test__operationRender__givenOperationsOutput_relative_whenModuleType_swiftPackageManager_generatesImportIncludingSchemaName() throws { + // given + buildConfig( + moduleType: .swiftPackageManager, + operations: .relative(subpath: nil) + ) + + let expected = + """ + import ApolloAPI + import ImportStatementTestsSchema + """ + + // when + let actual = ImportStatementTemplate.Operation.render(config).description + + // then + expect(actual).to(equalLineByLine(expected)) + } + + func test__operationRender__givenOperationsOutput_relative_whenModuleType_other_generatesImportIncludingSchemaName() throws { + // given + buildConfig( + moduleType: .other, + operations: .relative(subpath: nil) + ) + + let expected = + """ + import ApolloAPI + import ImportStatementTestsSchema + """ + + // when + let actual = ImportStatementTemplate.Operation.render(config).description + + // then + expect(actual).to(equalLineByLine(expected)) + } + + func test__operationRender__givenOperationsOutput_absolute_whenModuleType_none_generatesImportNotIncludingSchemaName() throws { + // given + buildConfig( + moduleType: .none, + operations: .absolute(path: "path") + ) + + let expected = + """ + import ApolloAPI + """ + + // when + let actual = ImportStatementTemplate.Operation.render(config).description + + // then + expect(actual).to(equalLineByLine(expected)) + } + + func test__operationRender__givenOperationsOutput_absolute_whenModuleType_swiftPackageManager_generatesImportIncludingSchemaName() throws { + // given + buildConfig( + moduleType: .swiftPackageManager, + operations: .absolute(path: "path") + ) + + let expected = + """ + import ApolloAPI + import ImportStatementTestsSchema + """ + + // when + let actual = ImportStatementTemplate.Operation.render(config).description + + // then + expect(actual).to(equalLineByLine(expected)) + } + + func test__operationRender__givenOperationsOutput_absolute_whenModuleType_other_generatesImportIncludingSchemaName() throws { + // given + buildConfig( + moduleType: .other, + operations: .absolute(path: "path") + ) + + let expected = + """ + import ApolloAPI + import ImportStatementTestsSchema + """ + + // when + let actual = ImportStatementTemplate.Operation.render(config).description + + // then + expect(actual).to(equalLineByLine(expected)) + } +}