Skip to content

Commit

Permalink
Merge pull request #90 from outfoxx/extension_import_bug
Browse files Browse the repository at this point in the history
Fix emitting imports using ExternalTypeSpecs
  • Loading branch information
dnkoutso authored Dec 14, 2023
2 parents dc661a0 + 01c700e commit 29299c2
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/main/java/io/outfoxx/swiftpoet/CodeWriter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,9 @@ internal class CodeWriter constructor(
for (i in typeSpecStack.indices.reversed()) {
val typeSpec = typeSpecStack[i]
if (typeSpec is ExternalTypeSpec) {
return stackTypeName(i, simpleName)
if (typeSpec.name == simpleName) {
return stackTypeName(i, simpleName)
}
}
for (visibleChild in typeSpec.typeSpecs) {
if (visibleChild.name == simpleName) {
Expand Down
45 changes: 43 additions & 2 deletions src/test/java/io/outfoxx/swiftpoet/test/FileSpecTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,50 @@ class FileSpecTests {
)
}

@Test
@DisplayName("Generates correct imports when extending different types")
fun testImportsForDifferentExtensionTypes() {
val parentElement = typeName("Foundation.Data")
val obsElement = typeName("RxSwift.Observable.Element")

val extension =
ExtensionSpec.builder(parentElement)
.addFunction(
FunctionSpec.builder("test")
.returns(obsElement)
.build()
)
.build()

val testFile = FileSpec.builder("Test", "Test")
.addExtension(extension)
.build()

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

assertThat(
out.toString(),
equalTo(
"""
import Foundation
import RxSwift
extension Data {
func test() -> Observable.Element {
}
}
""".trimIndent()
)
)
}

@Test
@DisplayName("Generates correct imports for extension types")
fun testImportsAndShortensExtensionTypes() {
fun testImportsForSameExtensionTypes() {

val obsElement = typeName("RxSwift.Observable.Element")

Expand Down Expand Up @@ -109,7 +150,7 @@ class FileSpecTests {
extension Observable {
func test() -> Element {
func test() -> RxSwift.Observable.Element {
}
}
Expand Down

0 comments on commit 29299c2

Please sign in to comment.