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/Sources/ApolloCodegenLib/FileGenerators/SchemaModuleFileGenerator.swift b/Sources/ApolloCodegenLib/FileGenerators/SchemaModuleFileGenerator.swift index 1ad77f1b52..5ebff604f5 100644 --- a/Sources/ApolloCodegenLib/FileGenerators/SchemaModuleFileGenerator.swift +++ b/Sources/ApolloCodegenLib/FileGenerators/SchemaModuleFileGenerator.swift @@ -18,6 +18,10 @@ struct SchemaModuleFileGenerator { fileManager: fileManager ) + case .other: + // no-op - the implementation is import statements in the generated operation files + return + default: throw NSError(domain: "ApolloCodegen", code: -1, userInfo: [NSLocalizedDescriptionKey: "Only Swift Package Manager is supported at the moment!"]) } diff --git a/Tests/ApolloCodegenTests/CodeGeneration/FileGenerators/SchemaModuleFileGeneratorTests.swift b/Tests/ApolloCodegenTests/CodeGeneration/FileGenerators/SchemaModuleFileGeneratorTests.swift index 8d151f508a..498c1310dc 100644 --- a/Tests/ApolloCodegenTests/CodeGeneration/FileGenerators/SchemaModuleFileGeneratorTests.swift +++ b/Tests/ApolloCodegenTests/CodeGeneration/FileGenerators/SchemaModuleFileGeneratorTests.swift @@ -37,11 +37,13 @@ class SchemaModuleFileGeneratorTests: XCTestCase { expect(mockFileManager.allClosuresCalled).to(beTrue()) } - func test__generate__givenUnimplementedConfigurations_shouldThrow() throws { + func test__generate__givenOtherConfiguration_shouldReturn() throws { expect(try SchemaModuleFileGenerator.generate( ApolloCodegenConfiguration.mock(.other, schemaName: "TestModule").output.schemaTypes - )).to(throwError()) + )).notTo(throwError()) + } + func test__generate__givenUnimplementedConfigurations_shouldThrow() throws { expect(try SchemaModuleFileGenerator.generate( ApolloCodegenConfiguration.mock(.none, schemaName: "TestModule").output.schemaTypes )).to(throwError()) diff --git a/Tests/ApolloCodegenTests/CodeGeneration/Templates/FragmentTemplateTests.swift b/Tests/ApolloCodegenTests/CodeGeneration/Templates/FragmentTemplateTests.swift index 6d4996dfe4..01ef4b448a 100644 --- a/Tests/ApolloCodegenTests/CodeGeneration/Templates/FragmentTemplateTests.swift +++ b/Tests/ApolloCodegenTests/CodeGeneration/Templates/FragmentTemplateTests.swift @@ -84,78 +84,6 @@ class FragmentTemplateTests: XCTestCase { expect(actual).to(equalLineByLine(expected, ignoringExtraLines: true)) } - // MARK: - Import Statements - - func test__render__givenFileOutput_inSchemaModule_schemaModuleManuallyLinked_generatesImportNotIncludingSchemaModule() throws { - // given - config = .mock(output: .mock( - moduleType: .none, - schemaName: "TestModuleName", - operations: .inSchemaModule - )) - - let expected = - """ - import ApolloAPI - - """ - - // when - try buildSubjectAndFragment() - - let actual = subject.render() - - // then - expect(actual).to(equalLineByLine(expected, atLine: 4, ignoringExtraLines: true)) - } - - func test__render__givenFileOutput_inSchemaModule_schemaModuleNotManuallyLinked_generatesImportNotIncludingSchemaModule() throws { - // given - config = .mock(output: .mock( - moduleType: .swiftPackageManager, - schemaName: "TestModuleName", - operations: .inSchemaModule - )) - - let expected = - """ - import ApolloAPI - - """ - - // when - try buildSubjectAndFragment() - - let actual = subject.render() - - // then - expect(actual).to(equalLineByLine(expected, atLine: 4, ignoringExtraLines: true)) - } - - func test__render__givenFileOutput_notInSchemaModule_schemaModuleNotManuallyLinked_generatesImportIncludingSchemaModule() throws { - // given - config = .mock(output: .mock( - moduleType: .swiftPackageManager, - schemaName: "TestModuleName", - operations: .absolute(path: "") - )) - - let expected = - """ - import ApolloAPI - import TestModuleName - - """ - - // when - try buildSubjectAndFragment() - - let actual = subject.render() - - // then - expect(actual).to(equalLineByLine(expected, atLine: 4, ignoringExtraLines: true)) - } - // MARK: - Fragment Definition func test__render__givenFragment_generatesFragmentDeclarationDefinitionAndBoilerplate() throws { 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)) + } +} diff --git a/Tests/ApolloCodegenTests/CodeGeneration/Templates/OperationDefinitionTemplateTests.swift b/Tests/ApolloCodegenTests/CodeGeneration/Templates/OperationDefinitionTemplateTests.swift index ca5651da5c..25fb2a2ec8 100644 --- a/Tests/ApolloCodegenTests/CodeGeneration/Templates/OperationDefinitionTemplateTests.swift +++ b/Tests/ApolloCodegenTests/CodeGeneration/Templates/OperationDefinitionTemplateTests.swift @@ -78,78 +78,6 @@ class OperationDefinitionTemplateTests: XCTestCase { expect(actual).to(equalLineByLine(expected, ignoringExtraLines: true)) } - // MARK: - Import Statements - - func test__generate__givenFileOutput_inSchemaModule_schemaModuleManuallyLinked_generatesImportNotIncludingSchemaModule() throws { - // given - config = .mock(output: .mock( - moduleType: .none, - schemaName: "TestModuleName", - operations: .inSchemaModule - )) - - let expected = - """ - import ApolloAPI - - """ - - // when - try buildSubjectAndOperation() - - let actual = subject.render() - - // then - expect(actual).to(equalLineByLine(expected, atLine: 4, ignoringExtraLines: true)) - } - - func test__generate__givenFileOutput_inSchemaModule_schemaModuleNotManuallyLinked_generatesImportNotIncludingSchemaModule() throws { - // given - config = .mock(output: .mock( - moduleType: .swiftPackageManager, - schemaName: "TestModuleName", - operations: .inSchemaModule - )) - - let expected = - """ - import ApolloAPI - - """ - - // when - try buildSubjectAndOperation() - - let actual = subject.render() - - // then - expect(actual).to(equalLineByLine(expected, atLine: 4, ignoringExtraLines: true)) - } - - func test__generate__givenFileOutput_notInSchemaModule_schemaModuleNotManuallyLinked_generatesImportIncludingSchemaModule() throws { - // given - config = .mock(output: .mock( - moduleType: .swiftPackageManager, - schemaName: "TestModuleName", - operations: .absolute(path: "") - )) - - let expected = - """ - import ApolloAPI - import TestModuleName - - """ - - // when - try buildSubjectAndOperation() - - let actual = subject.render() - - // then - expect(actual).to(equalLineByLine(expected, atLine: 4, ignoringExtraLines: true)) - } - // MARK: - Operation Definition func test__generate__givenQuery_generatesQueryOperation() throws { diff --git a/Tests/ApolloCodegenTests/TestHelpers/LineByLineComparison.swift b/Tests/ApolloCodegenTests/TestHelpers/LineByLineComparison.swift index bd702bdbfd..0cb406e977 100644 --- a/Tests/ApolloCodegenTests/TestHelpers/LineByLineComparison.swift +++ b/Tests/ApolloCodegenTests/TestHelpers/LineByLineComparison.swift @@ -39,7 +39,7 @@ public func equalLineByLine( } else { return PredicateResult( status: .fail, - message: .fail("Expected \(expectedLines.count), actual ended at line \(index).") + message: .fail("Expected \(expectedLines.count), actual ended at line \(actualLines.count)") ) } } @@ -52,6 +52,13 @@ public func equalLineByLine( } } + guard expectedLinesBuffer.isEmpty else { + return PredicateResult( + status: .fail, + message: .fail("Expected \(expectedLines.count), actual ended at line \(actualLines.count).") + ) + } + return PredicateResult( status: .matches, message: .expectedTo("be equal")