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

Import statements update (w/tests) #245

Merged
merged 21 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
7dabb6f
[codegen] add import directive
Jan 10, 2024
95cf909
[codegen] update codegen test
Jan 11, 2024
b7e3d8e
[codege] make importDirective struct private to package
Jan 11, 2024
5bc7ad5
[codegen] update tests
Jan 11, 2024
640e5c0
[codegen] render multiple import statements correctly
Jan 11, 2024
d2dad3f
[codegen] add import directive to fragments and operations, if a refe…
Jan 12, 2024
a90e823
[codegen] cleanup when adding additional imports in referenced fragments
Jan 12, 2024
4bb9b81
[codegen] pr review comment updates: user ordered set for import modu…
Jan 16, 2024
556cd98
[codegen] pr review comment updates: user ordered set for import modu…
Jan 16, 2024
4598e53
[codegen] pr review comment updates: user ordered set for import modu…
Jan 16, 2024
dfa41c7
[codegen] pr review comment updates: user ordered set for import modu…
Jan 17, 2024
615f8b2
[codegen] pr review comment updates: user ordered set for import modu…
Jan 17, 2024
031dbf2
[codegen] pr review comment updates: user ordered set for import modu…
Jan 17, 2024
854cb32
Update CodegenLib to use OrderedCollections 1.0.6
AnthonyMDev Jan 23, 2024
311eab6
fix callsites in tests for operationFile TemplateTarget
AnthonyMDev Jan 23, 2024
82eb3f5
Add template renderer test
AnthonyMDev Jan 23, 2024
8c11b03
Add tests for compilation of module imports
AnthonyMDev Jan 23, 2024
a014104
Add Template tests
AnthonyMDev Jan 23, 2024
6ac8137
Merge branch 'main' into import_statements_update
AnthonyMDev Jan 23, 2024
09b0e99
improve documentation
AnthonyMDev Jan 24, 2024
96b4bc5
Merge branch 'main' into import_statements_update
AnthonyMDev Jan 25, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import XCTest
@testable import ApolloCodegenLib
@testable import ApolloCodegenInternalTestHelpers
import Nimble
import OrderedCollections

class TemplateRenderer_OperationFile_Tests: XCTestCase {

Expand All @@ -21,8 +22,13 @@ class TemplateRenderer_OperationFile_Tests: XCTestCase {
)
}

private func buildSubject(config: ApolloCodegenConfiguration = .mock()) -> MockFileTemplate {
MockFileTemplate(target: .operationFile(), config: ApolloCodegen.ConfigurationContext(config: config))
private func buildSubject(
config: ApolloCodegenConfiguration = .mock(),
moduleImports: OrderedSet<String>? = nil
) -> MockFileTemplate {
MockFileTemplate(
target: .operationFile(moduleImports: moduleImports),
config: ApolloCodegen.ConfigurationContext(config: config))
}

// MARK: Render Target .operationFile Tests
Expand Down Expand Up @@ -359,6 +365,32 @@ class TemplateRenderer_OperationFile_Tests: XCTestCase {
expect(actual).to(equalLineByLine(expected, atLine: 6, ignoringExtraLines: true))
}

func test__moduleImports__givenValues_shouldGenerateImportStatements() {
// given
let config = buildConfig(
moduleType:
.embeddedInTarget(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks as though the custom imports should be rendered regardless of the moduleType configuration - it might be worth having additional tests for the other module types to ensure that remains true.

name: "MockApplication",
accessModifier: .public),
operations: .inSchemaModule
)

let moduleImports = OrderedSet(["TestA", "TestB"])

let subject = buildSubject(config: config, moduleImports: moduleImports)

let expected = """
import TestA
import TestB
"""

// when
let (actual, _) = subject.render()

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

func test__casing__givenUppercasedSchemaName_shouldGenerateUppercasedNamespace() {
// given

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,17 @@ extension TemplateRenderer {

let body = {
switch target {
case let .schemaFile(type): return renderSchemaFile(type, errorRecorder)
case let .operationFile(moduleImports): return renderOperationFile(moduleImports, errorRecorder)
case .moduleFile: return renderModuleFile(errorRecorder)
case .testMockFile: return renderTestMockFile(errorRecorder)
case let .schemaFile(type):
return renderSchemaFile(type, errorRecorder)

case let .operationFile(moduleImports):
return renderOperationFile(moduleImports, errorRecorder)

case .moduleFile:
return renderModuleFile(errorRecorder)

case .testMockFile:
return renderTestMockFile(errorRecorder)
}
}()

Expand Down Expand Up @@ -161,7 +168,7 @@ extension TemplateRenderer {
"""
\(ifLet: renderHeaderTemplate(nonFatalErrorRecorder: errorRecorder), { "\($0)\n" })
\(ImportStatementTemplate.Operation.template(for: config))
\(ifLet: ModuleImportStatementTemplate.template(moduleImports: moduleImports), { "\($0)" })
\(ifLet: moduleImports, { "\(ModuleImportStatementTemplate.template(moduleImports: $0))" })

\(if: config.output.operations.isInModule && !config.output.schemaTypes.isInModule,
renderBodyTemplate(nonFatalErrorRecorder: errorRecorder)
Expand Down Expand Up @@ -357,9 +364,8 @@ struct ImportStatementTemplate {
struct ModuleImportStatementTemplate {

static func template(
moduleImports: OrderedSet<String>?
) -> TemplateString? {
guard let moduleImports else { return nil }
moduleImports: OrderedSet<String>
) -> TemplateString {
return """
\(moduleImports.map { "import \($0)" }.joined(separator: "\n"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we have a comment above these to identify these as custom configured imports?

"""
Expand Down