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 19 commits
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
4 changes: 2 additions & 2 deletions .package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-collections",
"state" : {
"revision" : "a902f1823a7ff3c9ab2fba0f992396b948eda307",
"version" : "1.0.5"
"revision" : "d029d9d39c87bed85b1c50adee7c41795261a192",
"version" : "1.0.6"
}
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,30 @@ class FragmentTemplateTests: XCTestCase {
subject.renderBodyTemplate(nonFatalErrorRecorder: .init()).description
}

// MARK: - Target Configuration Tests

func test__target__givenModuleImports_targetHasModuleImports() async throws {
// given
document = """
fragment TestFragment on Query @import(module: "ModuleA") {
allAnimals {
species
}
}
"""

// when
try await buildSubjectAndFragment()

guard case let .operationFile(actual) = subject.target else {
fail("expected operationFile target")
return
}

// then
expect(actual).to(equal(["ModuleA"]))
}

// MARK: Fragment Definition

func test__render__givenFragment_generatesFragmentDeclarationDefinitionAndBoilerplate() async throws {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,30 @@ class LocalCacheMutationDefinitionTemplateTests: XCTestCase {
subject.renderBodyTemplate(nonFatalErrorRecorder: .init()).description
}

// MARK: - Target Configuration Tests

func test__target__givenModuleImports_targetHasModuleImports() async throws {
// given
document = """
query TestOperation @apollo_client_ios_localCacheMutation @import(module: "ModuleA") {
allAnimals {
species
}
}
"""

// when
try await buildSubjectAndOperation()

guard case let .operationFile(actual) = subject.target else {
fail("expected operationFile target")
return
}

// then
expect(actual).to(equal(["ModuleA"]))
}

// MARK: - Access Level Tests

func test__generate__givenQuery_whenModuleTypeIsSwiftPackageManager_andOperationsInSchemaModule_generatesWithPublicAccess() async throws {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,31 @@ class OperationDefinitionTemplateTests: XCTestCase {
subject.renderBodyTemplate(nonFatalErrorRecorder: .init()).description
}

// MARK: - Operation Definition
// MARK: - Target Configuration Tests

func test__target__givenModuleImports_targetHasModuleImports() async throws {
// given
document = """
query TestOperation @import(module: "ModuleA") {
allAnimals {
species
}
}
"""

// when
try await buildSubjectAndOperation()

guard case let .operationFile(actual) = subject.target else {
fail("expected operationFile target")
return
}

// then
expect(actual).to(equal(["ModuleA"]))
}

// MARK: - Operation Definition Tests

func test__generate__givenQuery_generatesQueryOperation() async throws {
// given
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
)
))
let mockTemplateRenderer = MockTemplateRenderer(
target: .operationFile,
target: .operationFile(),
template: "",
config: config
)
Expand Down Expand Up @@ -1716,11 +1716,11 @@
))
}

#warning("need more tests here - same test cases as IRRootFieldBuilderTests")

Check warning on line 1719 in Tests/ApolloCodegenTests/CodeGeneration/Templates/SelectionSet/SelectionSetTemplateTests.swift

View workflow job for this annotation

GitHub Actions / Codegen Lib Unit Tests - macOS

need more tests here - same test cases as IRRootFieldBuilderTests

// MARK: Selections - Deferred Named Fragment

#warning("need more tests here - same test cases as IRRootFieldBuilderTests")

Check warning on line 1723 in Tests/ApolloCodegenTests/CodeGeneration/Templates/SelectionSet/SelectionSetTemplateTests.swift

View workflow job for this annotation

GitHub Actions / Codegen Lib Unit Tests - macOS

need more tests here - same test cases as IRRootFieldBuilderTests

// MARK: Selections - Include/Skip

Expand Down Expand Up @@ -4664,11 +4664,11 @@
))
}

#warning("need more tests here - same test cases as IRRootFieldBuilderTests")

Check warning on line 4667 in Tests/ApolloCodegenTests/CodeGeneration/Templates/SelectionSet/SelectionSetTemplateTests.swift

View workflow job for this annotation

GitHub Actions / Codegen Lib Unit Tests - macOS

need more tests here - same test cases as IRRootFieldBuilderTests

// MARK: Field Accessors - Deferred Named Fragments

#warning("need more tests here - same test cases as IRRootFieldBuilderTests")

Check warning on line 4671 in Tests/ApolloCodegenTests/CodeGeneration/Templates/SelectionSet/SelectionSetTemplateTests.swift

View workflow job for this annotation

GitHub Actions / Codegen Lib Unit Tests - macOS

need more tests here - same test cases as IRRootFieldBuilderTests

// MARK: - Inline Fragment Accessors

Expand Down Expand Up @@ -5522,11 +5522,11 @@
))
}

#warning("need more tests here - same test cases as IRRootFieldBuilderTests")

Check warning on line 5525 in Tests/ApolloCodegenTests/CodeGeneration/Templates/SelectionSet/SelectionSetTemplateTests.swift

View workflow job for this annotation

GitHub Actions / Codegen Lib Unit Tests - macOS

need more tests here - same test cases as IRRootFieldBuilderTests

// MARK: Fragment Accessors - Deferred Named Fragment

#warning("need more tests here - same test cases as IRRootFieldBuilderTests")

Check warning on line 5529 in Tests/ApolloCodegenTests/CodeGeneration/Templates/SelectionSet/SelectionSetTemplateTests.swift

View workflow job for this annotation

GitHub Actions / Codegen Lib Unit Tests - macOS

need more tests here - same test cases as IRRootFieldBuilderTests

// MARK: - Nested Selection Sets

Expand Down Expand Up @@ -7532,7 +7532,7 @@
))
}

#warning("need more tests here - same test cases as IRRootFieldBuilderTests (inline fragment only")

Check warning on line 7535 in Tests/ApolloCodegenTests/CodeGeneration/Templates/SelectionSet/SelectionSetTemplateTests.swift

View workflow job for this annotation

GitHub Actions / Codegen Lib Unit Tests - macOS

need more tests here - same test cases as IRRootFieldBuilderTests (inline fragment only

// MARK: - Documentation Tests

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class SelectionSetTemplate_ErrorHandling_Tests: XCTestCase {
options: .init()
)
let mockTemplateRenderer = MockTemplateRenderer(
target: .operationFile,
target: .operationFile(),
template: "",
config: .init(config: config)
)
Expand All @@ -64,7 +64,7 @@ class SelectionSetTemplate_ErrorHandling_Tests: XCTestCase {
options: .init()
)
let mockTemplateRenderer = MockTemplateRenderer(
target: .operationFile,
target: .operationFile(),
template: "",
config: .init(config: config)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class SelectionSetTemplate_Initializers_Tests: XCTestCase {
options: .init()
)
let mockTemplateRenderer = MockTemplateRenderer(
target: .operationFile,
target: .operationFile(),
template: "",
config: .init(config: config)
)
Expand Down Expand Up @@ -70,7 +70,7 @@ class SelectionSetTemplate_Initializers_Tests: XCTestCase {
options: .init()
)
let mockTemplateRenderer = MockTemplateRenderer(
target: .operationFile,
target: .operationFile(),
template: "",
config: .init(config: config)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class SelectionSetTemplate_LocalCacheMutationTests: XCTestCase {
)
)
let mockTemplateRenderer = MockTemplateRenderer(
target: .operationFile,
target: .operationFile(),
template: "",
config: config
)
Expand Down
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
Loading
Loading