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

Add support for top-level Kotlin functions #847 #1147

Merged
merged 6 commits into from
Oct 28, 2022

Conversation

volivan239
Copy link
Collaborator

@volivan239 volivan239 commented Oct 12, 2022

Description

With these changes, UtBot is able to generate tests for top-level Kotlin functions including extension functions. There are two main subjects that were changed:

  1. In plugin, we have to look for KtFiles and their FacadeClasses among the other classes that we were already looking for. Note that as Kotlin reflection is not fully supported for such classes (see issue), some pieces of code were rewritten to use Java reflection.
  2. In Kotlin renderer, we should omit calling to classes like MainKt when rendering top-level function calls. In order to support this, method ClassId.isKotlinFile was added, which checks kind in Kotlin's @Metadata annotation (see docs for more info). Also, for extension functions we should replace fictive caller with extension receiver, borrowing it from arguments (see comment to CgMethodCall.takeCallerFromArgumentsIfNeeded() for more detials).

Alongside with above changes in plugin and codegen, following side-changes were made to handle exploited problems:

  1. Fixed inconsistency between KClass<*>.signature() and MemberInfo.signature()
  2. Fixed name in builtin-classids timeunitClassId and durationClassId
  3. KCallable<*>.declaringClazz and MemberInfo.paramNames were slightly changed to work properly when given function is an extension function

Fixes #847

Type of Change

Please delete options that are not relevant.

  • New feature (non-breaking change which adds functionality)

How Has This Been Tested?

Automated Testing

Added tests to org/utbot/examples/codegen/FileWithTopLevelFunctionsTest.kt

Manual Scenario

Checked on some top-level methods (including fun main() in main class) -- generates correct tests both in Java and Kotlin

Checklist (remove irrelevant options):

  • The change followed the style guidelines of the UTBot project
  • Self-review of the code is passed
  • The change contains enough commentaries, particularly in hard-to-understand areas
  • New documentation is provided or existed one is altered
  • No new warnings
  • New tests have been added
  • All tests pass locally with my changes

@volivan239 volivan239 marked this pull request as ready for review October 12, 2022 14:45
@volivan239 volivan239 force-pushed the volivan239/make_top_level_methods_testable branch from e3fb541 to d9aa881 Compare October 12, 2022 14:50
@volivan239
Copy link
Collaborator Author

Note that this doesn't resolve all problems featured by #847, as currently this doesn't work for extension functions. Needs a little bit more investigation and additional commit/PR

@volivan239 volivan239 force-pushed the volivan239/make_top_level_methods_testable branch 5 times, most recently from 1c4543c to 16d859b Compare October 18, 2022 17:40
@volivan239
Copy link
Collaborator Author

UPD. Now extension functions should also work fine, see PR description for details

@volivan239 volivan239 force-pushed the volivan239/make_top_level_methods_testable branch from 16d859b to 85795ac Compare October 19, 2022 14:26
@volivan239 volivan239 force-pushed the volivan239/make_top_level_methods_testable branch 2 times, most recently from 92d3f5d to 2b56cb2 Compare October 23, 2022 23:06
@volivan239 volivan239 force-pushed the volivan239/make_top_level_methods_testable branch from 2b56cb2 to cfe5932 Compare October 25, 2022 14:21
@volivan239 volivan239 merged commit 792389a into main Oct 28, 2022
@volivan239 volivan239 deleted the volivan239/make_top_level_methods_testable branch October 28, 2022 09:35
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.

Plugin does not discover top-level methods in .kt files
2 participants