Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for Spies in Code Generation #128

Merged
merged 5 commits into from
Oct 3, 2022
Merged

Conversation

MarcoSignoretto
Copy link
Collaborator

@MarcoSignoretto MarcoSignoretto commented Sep 28, 2022

This patch allows the new ksp based code generator to generate Spies in addition to mocks

TODO:

  • Continue properties implementation
  • Compare generated code for mocks before and after
  • Test Spy generation on an internal project
  • Update README

@MarcoSignoretto MarcoSignoretto changed the base branch from master to cover-codegen-from-regression October 2, 2022 10:54
MarcoSignoretto and others added 3 commits October 3, 2022 15:10
This patch allows the new ksp based code generator to generate Spies in addition to mocks
@MarcoSignoretto MarcoSignoretto changed the base branch from cover-codegen-from-regression to master October 3, 2022 11:11
@MarcoSignoretto MarcoSignoretto changed the title [WIP] Adding support for Spies in Code Generation Adding support for Spies in Code Generation Oct 3, 2022
@MarcoSignoretto MarcoSignoretto marked this pull request as ready for review October 3, 2022 12:17
Comment on lines +29 to +35
interface CodeBlockFactory {
fun resolveSupertype(): String
fun decorateConstructor(classToMock: KSType, classBuilder: TypeSpec.Builder)
fun decorateFunctionBody(classToMock: KSClassDeclaration, function: KSFunctionDeclaration, isUnit: Boolean, functionBuilder: FunSpec.Builder)
fun decoratePropertyGetter(classToMock: KSClassDeclaration, property: KSPropertyDeclaration, propertyBuilder: PropertySpec.Builder, typeResolver: Map<KSTypeParameter, KSTypeArgument>)
fun decoratePropertySetter(classToMock: KSClassDeclaration, property: KSPropertyDeclaration, propertyBuilder: PropertySpec.Builder, typeResolver: Map<KSTypeParameter, KSTypeArgument>)
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this si where codegen is different in Spies respect to mocks

import com.squareup.kotlinpoet.ksp.toKModifier
import com.squareup.kotlinpoet.ksp.toTypeName

abstract class Generator constructor(
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just rename of MockGenerator, non common logic has been extracted to MockGenerator and SpyGenerator

)
}

override fun decorateFunctionBody(
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

function body is different between mocks and spies, this is the spy impl for the code gen of that block

Comment on lines +26 to +28
// @Spy
// lateinit var pippoMock: PippoSample
// FIXME suspend function is not working
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will be supported in future PRs

@MarcoSignoretto MarcoSignoretto merged commit 4714851 into master Oct 3, 2022
@MarcoSignoretto MarcoSignoretto deleted the ksp-spies branch October 4, 2022 11:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants