Skip to content

Commit

Permalink
Merge pull request #76 from anthonycr/kotlinpoet-2
Browse files Browse the repository at this point in the history
Updating kotlinpoet to version 2.0.0
  • Loading branch information
anthonycr authored Oct 24, 2024
2 parents 73eb033 + acebe92 commit e5f4135
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 23 deletions.
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[versions]
junit = "4.13.2"
jetbrainsKotlin = "2.0.21"
kotlinpoet = "1.18.1"
kotlinpoet = "2.0.0"
ksp = "2.0.21-1.0.26"
jvm = "17"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,19 @@ class MockingbirdSymbolProcessor(
.map { declaration ->
require(declaration is KSPropertyDeclaration)

val ksType = declaration.type.resolve()
ksType to ksType.declaration
declaration.type.resolve().declaration
}
.check(
message = "Only interfaces can be verified",
logger = logger,
node = Pair<KSType, KSDeclaration>::second,
condition = { (_, declaration) -> declaration.isInterface }
node = { it },
condition = { declaration -> declaration.isInterface }
)
.distinctBy { (_, declaration) -> declaration.qualifiedName!!.asString() }
.map { (ksType, declaration) ->
.distinctBy { declaration -> declaration.qualifiedName!!.asString() }
.map { declaration ->
require(declaration is KSClassDeclaration)

val (typeSpec, fileSpec) = fakeImplementationGenerator.generate(ksType, declaration)
val (typeSpec, fileSpec) = fakeImplementationGenerator.generate(declaration)
fileSpec.writeTo(codeGenerator, true)
logger.info("Generating fake for: ${declaration.qualifiedName}")

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package com.anthonycr.mockingbird.processor.internal.generator

import com.anthonycr.mockingbird.core.Verifiable
import com.anthonycr.mockingbird.processor.internal.noWrap
import com.anthonycr.mockingbird.processor.internal.safePackageName
import com.google.devtools.ksp.symbol.KSClassDeclaration
import com.google.devtools.ksp.symbol.KSFunctionDeclaration
import com.google.devtools.ksp.symbol.KSType
import com.google.devtools.ksp.symbol.KSTypeReference
import com.google.devtools.ksp.symbol.Modifier
import com.squareup.kotlinpoet.ClassName
Expand All @@ -31,8 +29,8 @@ class FakeImplementationGenerator {
/**
* Generate the [TypeSpec] and [FileSpec] for a fake implementation of an interface.
*/
fun generate(ksType: KSType, declaration: KSClassDeclaration): Pair<TypeSpec, FileSpec> {
val fakeTypeSpec = generateFakeImplementation(ksType, declaration)
fun generate(declaration: KSClassDeclaration): Pair<TypeSpec, FileSpec> {
val fakeTypeSpec = generateFakeImplementation(declaration)

return fakeTypeSpec to FileSpec.builder(
declaration.safePackageName,
Expand All @@ -45,7 +43,6 @@ class FakeImplementationGenerator {
}

private fun generateFakeImplementation(
ksType: KSType,
interfaceDeclaration: KSClassDeclaration
): TypeSpec {
val interfaceName = interfaceDeclaration.simpleName.asString()
Expand All @@ -56,7 +53,7 @@ class FakeImplementationGenerator {
}
val implementationTypeSpec = TypeSpec.classBuilder(implementationClassName)
.addModifiers(KModifier.PUBLIC)
.addSuperinterface(ksType.toClassName().maybeParameterizedBy(typeParameters))
.addSuperinterface(interfaceDeclaration.toClassName().maybeParameterizedBy(typeParameters))
.addSuperinterface(Verifiable::class.asTypeName())

implementationTypeSpec.addProperty(
Expand Down Expand Up @@ -146,13 +143,13 @@ class FakeImplementationGenerator {
.addStatement("val invocation = _mockingbird_invocations.firstOrNull()")
.beginControlFlow("check(invocation != null)")
.addStatement(
"\"Expected an invocation, but got none instead\"".noWrap(),
"\"Expected an invocation, but got none instead\"",
)
.endControlFlow()
.addStatement("_mockingbird_invocations.removeAt(0)")
.beginControlFlow("check(invocation.functionName == %S)", functionName)
.addStatement(
"\"Expected function call %1L, \${invocation.functionName} was called instead\"".noWrap(),
"\"Expected function call %1L, \${invocation.functionName} was called instead\"",
functionName,
)
.endControlFlow()
Expand All @@ -169,7 +166,7 @@ class FakeImplementationGenerator {
name
)
addStatement(
"\"Expected argument \$%1L, found \${invocation.parameters[%2L]} instead.\"".noWrap(),
"\"Expected argument \$%1L, found \${invocation.parameters[%2L]} instead.\"",
name,
index
)
Expand Down

This file was deleted.

0 comments on commit e5f4135

Please sign in to comment.