Skip to content

Commit

Permalink
Improves after review;
Browse files Browse the repository at this point in the history
  • Loading branch information
ivk1800 committed Mar 4, 2024
1 parent 103b000 commit 4135a77
Showing 1 changed file with 13 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -323,46 +323,27 @@ class MemberNameTest {
}

@Test fun importedMemberClassFunctionNameDontClashForParameterValue() {
fun createSimpleParameterCodeBlock(): CodeBlock {
val member = ClassName(
packageName = "com.squareup",
simpleNames = listOf("Fridge"),
).member("meat")
val block = CodeBlock.builder()
.add("%M", member)
.add(" { }")
.build()
return CodeBlock
.builder()
.add("%L", block)
.build()
}

fun createFuncParameter(name: String) =
CodeBlock
.builder()
.add(name).add(" = ")
.add(createSimpleParameterCodeBlock())
.build()

fun createFuncParameters(names: List<String>): List<CodeBlock> =
names.map { name -> createFuncParameter(name) }

fun createBuildFunc(params: List<String>): FunSpec {
val tacoClassname = ClassName.bestGuess("com.squareup.tacos.Taco")
val paramsCodeBlock = CodeBlock.builder()
val bodyCodeBlock = CodeBlock.builder()
.add("return %T", tacoClassname)
.apply {
add(
params.let(::createFuncParameters)
.map { block -> CodeBlock.of("%L", block) }
.joinToCode(prefix = "(", suffix = ")"),
)
val paramsBlock = params.map { paramName ->
val paramValue = ClassName(
packageName = "com.squareup",
simpleNames = listOf("Fridge"),
).member("meat")
CodeBlock.of("$paramName = %L", CodeBlock.of("%M { }", paramValue))
}
.map { block -> CodeBlock.of("%L", block) }
.joinToCode(prefix = "(", suffix = ")")
add(paramsBlock)
}
.build()
return FunSpec.builder("build")
.returns(tacoClassname)
.addCode(paramsCodeBlock).build()
.addCode(bodyCodeBlock)
.build()
}

val spec = FileSpec.builder("com.squareup.tacos", "Tacos")
Expand Down

0 comments on commit 4135a77

Please sign in to comment.