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

Optional function parameters should not be considered a "branch" #303

Closed
MaxBeauchemin opened this issue Feb 14, 2023 · 6 comments
Closed
Assignees
Labels
Feature Feature request issue type S: waiting for clarification Status: additional information required to proceed

Comments

@MaxBeauchemin
Copy link

MaxBeauchemin commented Feb 14, 2023

What is your use-case and why do you need this feature?

Function: fun foo(bar: Boolean = false) = if (bar) "bar" else "foobar"

Invocation: foo() is missing coverage since it's utilizing the default parameter

This is especially important when the function that has optional parameters is not one that we wrote or have control over

i.e. kotlinx.coroutines.flow.toList is giving us this issue today

image

Describe the solution you'd like

Optional parameters should not be considered a branching condition, otherwise we can't leverage them without missing coverage.

(A setting to toggle this feature would also be sufficient if there are projects that like the current behavior)

@MaxBeauchemin MaxBeauchemin added Feature Feature request issue type S: untriaged Status: issue reported but unprocessed labels Feb 14, 2023
@shanshin
Copy link
Collaborator

shanshin commented Feb 14, 2023

Hi,
unfortunately, it is difficult to understand the reason for incomplete coverage by the screenshot.

However, it is not related to the default argument, because for a function that is not fully covered, coverage problems are displayed at the place of definition of this function, and not at call side.

In your example, for the function fun foo(bar: Boolean = false) = if (bar) "bar" else "foobar" if you invoke

        foo(true)
        foo()

or

        foo(true)
        foo(false)

this function will be fully covered in the report
image

For a more detailed learning of the issue, could you please send a binary coverage report (located in the build/kover/<launched_test_name>.ic directory)?

@shanshin shanshin added S: waiting for clarification Status: additional information required to proceed and removed S: untriaged Status: issue reported but unprocessed labels Feb 14, 2023
@MaxBeauchemin
Copy link
Author

Due to the nature of the source code, I'm not sure I can share a whole test report, but I can try to explain it a bit more clearly.

We are using R2DBC to read data from database tables.

This means we are calling a Repository and when we try to retrieve multiple records, we use the toList() function to "collect" all the elements of the Flow<T> into a List<T>

This is leading to a "branch" missing coverage on that toList() call

image

image

After running into this issue in a few other places, we believe the issue has to do with optional parameters not being passed into the function

@MaxBeauchemin
Copy link
Author

(for reference) This is the toList() function we're invoking (in the kotlinx.coroutines.flow package)

public suspend fun <T> Flow<T>.toList(destination: MutableList<T> = ArrayList()): List<T> = toCollection(destination)

@MaxBeauchemin
Copy link
Author

Hi, unfortunately, it is difficult to understand the reason for incomplete coverage by the screenshot.

However, it is not related to the default argument, because for a function that is not fully covered, coverage problems are displayed at the place of definition of this function, and not at call side.

In your example, for the function fun foo(bar: Boolean = false) = if (bar) "bar" else "foobar" if you invoke

        foo(true)
        foo()

or

        foo(true)
        foo(false)

this function will be fully covered in the report image

For a more detailed learning of the issue, could you please send a binary coverage report (located in the build/kover/<launched_test_name>.ic directory)?

In reply to this, the line of code I'm trying to get covered is foo() not the actual function implementation itself

@shanshin
Copy link
Collaborator

shanshin commented Feb 14, 2023

In reply to this, the line of code I'm trying to get covered is foo() not the actual function implementation itself

Default arguments can only affect coverage of the definition of a function.
In your case, the problem may be caused by an additional byte-code change by the coroutines.

@shanshin
Copy link
Collaborator

Fixed in 0.7.4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature Feature request issue type S: waiting for clarification Status: additional information required to proceed
Projects
None yet
Development

No branches or pull requests

2 participants