Closed kdubb closed 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()
)
)
}
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.
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()
)
)
}
Builds on #90 and fixes nested and same type importing