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

Fix outstanding cases with importing #97

Merged
merged 5 commits into from
Dec 16, 2023
Merged

Fix outstanding cases with importing #97

merged 5 commits into from
Dec 16, 2023

Conversation

kdubb
Copy link
Contributor

@kdubb kdubb commented Dec 15, 2023

Builds on #90 and fixes nested and same type importing

Builds on #90 and fixes nested and same type importing
@dnkoutso
Copy link
Collaborator

dnkoutso commented Dec 15, 2023

I am not sure if this test / bug is related to this PR:

  @Test
  fun testSomeTest() {
    val type =
      TypeSpec.structBuilder("SomeType")
        .addProperty(
          PropertySpec.varBuilder(
            "foundation_order",
            typeName("Foundation.SortOrder")
          ).build()
        )
        .addProperty(
          PropertySpec.varBuilder(
            "order",
            typeName("some_other_module.SortOrder")
          ).build()
        )
        .build()

    val testFile = FileSpec.builder("Test", "Test")
      .addImport("Foundation")
      .addType(type)
      .build()

    val out = StringWriter()
    testFile.writeTo(out)

    assertThat(
      out.toString(),
      equalTo(
        """
            import Foundation

            struct SomeType {

              var foundation_order: SortOrder
              var order: some_other_module.SortOrder

            }

        """.trimIndent()
      )
    )
  }

@kdubb
Copy link
Contributor Author

kdubb commented Dec 15, 2023

Added test and reworked to include fix.

This required me to rework your withCollectedImports. It’s now collectImports and returns both the imported types and all referenced modules. I had to make it return explicit values (rather than returning a pre-configured CodeWriter) because I need to feed the referenced modules into the FileSpec.emit and their is no correct value on CodeWriter itself (and adding one specifically for FileSpec.emit didn’t seem right).

All tests are passing and, additionally, it caught a missing import in one of the existing tests.

@kdubb kdubb changed the title Fix outstanding cases with importing in an extension Fix outstanding cases with importing Dec 15, 2023
@dnkoutso
Copy link
Collaborator

Update from Slack conversations, we identified one more test case that fails with alwaysQualify = true

@Test
  @DisplayName("Generates all required imports with naming conflicts and always qualified")
  fun testSomething() {
    val type =
      TypeSpec.structBuilder("SomeType")
        .addProperty(
          PropertySpec.varBuilder(
            "foundation_order",
            typeName("Foundation.SortOrder", alwaysQualify = true)
          ).build()
        )
        .addProperty(
          PropertySpec.varBuilder(
            "order",
            typeName("some_other_module.SortOrder")
          ).build()
        )
        .build()

    val testFile = FileSpec.builder("Test", "Test")
      .addType(type)
      .build()

    val out = StringWriter()
    testFile.writeTo(out)

    assertThat(
      out.toString(),
      equalTo(
        """
            import Foundation
            import some_other_module

            struct SomeType {

              var foundation_order: Foundation.SortOrder
              var order: SortOrder

            }

        """.trimIndent()
      )
    )
  }

@kdubb kdubb merged commit 5eb895e into main Dec 16, 2023
1 check passed
@kdubb kdubb deleted the fix/ext-imports branch December 16, 2023 11:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants