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

Finish support for .other module type #2195

Merged
merged 4 commits into from
Mar 10, 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 @@ -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 */; };
Expand Down Expand Up @@ -1105,6 +1106,7 @@
E66F8898276C15580000BDA8 /* ObjectFileGenerator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ObjectFileGenerator.swift; sourceTree = "<group>"; };
E674DB40274C0A9B009BB90E /* Glob.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Glob.swift; sourceTree = "<group>"; };
E674DB42274C0AD9009BB90E /* GlobTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GlobTests.swift; sourceTree = "<group>"; };
E686B0D127D951630095D04E /* ImportStatementTemplateTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ImportStatementTemplateTests.swift; sourceTree = "<group>"; };
E68D824627A228A80040A46F /* SchemaModuleFileGenerator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SchemaModuleFileGenerator.swift; sourceTree = "<group>"; };
E69BEDA42798B86D00000D10 /* InputObjectTemplate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InputObjectTemplate.swift; sourceTree = "<group>"; };
E69BEDA62798B89600000D10 /* InputObjectTemplateTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InputObjectTemplateTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2089,6 +2091,7 @@
DE5FD608276956C70033EE23 /* SchemaTemplateTests.swift */,
E6B42D0C27A4749100A3BD58 /* SwiftPackageManagerModuleTemplateTests.swift */,
E64F7EB927A085D90059C021 /* UnionTemplateTests.swift */,
E686B0D127D951630095D04E /* ImportStatementTemplateTests.swift */,
);
path = Templates;
sourceTree = "<group>";
Expand Down Expand Up @@ -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 */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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!"])
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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))
}
}
Loading