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 Kotlin Sequence support for @TestFactory methods #3377

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

hanszt
Copy link

@hanszt hanszt commented Jul 2, 2023

Overview

Add support for converting Iterator-providing classes to a Stream in org.junit.platform.commons.util.CollectionUtils.


I hereby agree to the terms of the JUnit Contributor License Agreement.


Definition of Done

@hanszt hanszt force-pushed the feature/testfactory-kotlin-sequence-support branch 10 times, most recently from 8a0a767 to 52d6c31 Compare July 5, 2023 08:15
@hanszt hanszt changed the title Feature/TestFactory kotlin sequence support Feature/TestFactory Kotlin Sequence support Jul 5, 2023
@marcphilipp marcphilipp linked an issue Jul 7, 2023 that may be closed by this pull request
2 tasks
@hanszt hanszt force-pushed the feature/testfactory-kotlin-sequence-support branch from 52d6c31 to f32d798 Compare July 7, 2023 20:38
@hanszt hanszt requested a review from marcphilipp July 7, 2023 20:54
@hanszt
Copy link
Author

hanszt commented Jul 25, 2023

Hello, I am curious if my changes in response to the review satisfy the requirements. I would love to hear what you think and if it can be merged to the main branch

@hanszt hanszt force-pushed the feature/testfactory-kotlin-sequence-support branch from f32d798 to 9e7a9a3 Compare May 8, 2024 09:05
Copy link
Member

@marcphilipp marcphilipp left a comment

Choose a reason for hiding this comment

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

@hanszt Sorry for the delay! I think this would be a very useful change.

|| (type.isArray() && type.getComponentType().isPrimitive()));
|| (type.isArray() && type.getComponentType().isPrimitive())//
|| Arrays.stream(type.getMethods())//
.filter(m -> m.getName().equals("iterator"))//
Copy link
Member

Choose a reason for hiding this comment

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

❓ If possible, I think we should check whether type implements kotlin.sequences.Sequence here because it's safer. We will need to defensively load the Kotlin type via reflection.

Copy link
Author

@hanszt hanszt May 17, 2024

Choose a reason for hiding this comment

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

I followed the same logic, the Kotlin language is following for for-loops, namely that anything that provides an iterator, can be iterated over using a for-loop. See also https://kotlinlang.org/docs/control-flow.html#for-loops.

I applied this logic to the conversion from anything that provides an iterator to convert to a stream. This also allows a Kotlin sequence to be converted to a stream. Don't you think it is safe enough to check whether the object to convert to a stream contains an iterator providing method with the name 'iterator' and actually provides a java.util.Iterator?

I'm not sure yet how to check via reflection if we are dealing with a Kotlin Sequence without having a dependency to the Kotlin standard library in the junit-platform-commons module.

|| Arrays.stream(type.getMethods())//
.filter(m -> m.getName().equals("iterator"))//
.map(Method::getReturnType)//
.anyMatch(returnType -> returnType == Iterator.class));
Copy link
Member

Choose a reason for hiding this comment

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

❓ How does this work? Isn't the return type kotlin.collections.Iterator, not java.util.Iterator?

Copy link
Author

Choose a reason for hiding this comment

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

This works because when Kotlin is used under the Jvm, some types are mapped to Java types. The kotlin.collections-Iterator is one of them and is mapped to java.util.Iterator. See also: https://kotlinlang.org/docs/java-interop.html#mapped-types.

@hanszt
Copy link
Author

hanszt commented May 17, 2024

No worries. I can imagine you are busy. I'm glad you think it is a good improvement.

Examples of benefits:

- Kotlin Sequence support for @testfactory
- Kotlin Sequence support for @MethodSource
- Classes that expose an Iterator returning method,
can be converted to a stream.

Issue: junit-team#3376

I hereby agree to the terms of the JUnit Contributor License Agreement.
@hanszt hanszt force-pushed the feature/testfactory-kotlin-sequence-support branch from 9e7a9a3 to 4ce948f Compare May 27, 2024 16:36
@hanszt hanszt requested a review from marcphilipp May 27, 2024 16:50
@sbrannen sbrannen changed the title Feature/TestFactory Kotlin Sequence support Add Kotlin Sequence support for @TestFactory methods Jun 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add Kotlin Sequence support for @TestFactory methods
3 participants