outfoxx / swiftpoet

Kotlin and Java API for generating .swift source files.
Apache License 2.0
277 stars 26 forks source link

Fix outstanding cases with importing #97

Closed kdubb closed 6 months ago

kdubb commented 6 months ago

Builds on #90 and fixes nested and same type importing

dnkoutso commented 6 months ago

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 commented 6 months ago

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.

dnkoutso commented 6 months ago

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()
      )
    )
  }