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 #3376

Open
2 tasks done
hanszt opened this issue Jul 2, 2023 · 2 comments · May be fixed by #3377
Open
2 tasks done

Add Kotlin Sequence support for @TestFactory methods #3376

hanszt opened this issue Jul 2, 2023 · 2 comments · May be fixed by #3377

Comments

@hanszt
Copy link

hanszt commented Jul 2, 2023

Overview

It would be nice if you could write something like this in Kotlin when working with JUnit Jupiter.

        @TestFactory
        fun `dynamic tests returned as Kotlin sequence`() = generateSequence(0) { it + 2 }
                .map { dynamicTest("$it should be even") { assertTrue(it % 2 == 0) } }
                .take(10)

Currently you have to add .toIterable() as an extra line to make it work with JUnit Jupiter.

The Kotlin Sequence class is an example of an iterator providing class, but it is not an Iterable. That's why it does not work as return type for @TestFactory methods at the moment.

A benefit of adding this at this low level is that, Kotlin Sequences, or any other Iterator or Spliterator providing class can now also be used in other JUnit Jupiter methods where an object from an annotated method needs to be converted to a stream.

I have an implementation for it ready. see:

Suggestion

All tests still pass.

I would like to contribute to JUnit 5 and make a pull request for this branch, but I don't have push permission yet for a feature branch in the JUnit 5 project.

Could you please be so kind to take a look at the suggestion I provided?

Thanks in advance! I'm awaiting your response :)

Deliverables

  • Adding support for converting iterator providing classes to a stream in org.junit.platform.commons.util.CollectionUtils.
  • Adding support for converting Spliterator providing classes to a stream in org.junit.platform.commons.util.CollectionUtils.
@hanszt hanszt changed the title Add kotlin sequence support for @testfactory methods Add kotlin Sequence support for @TestFactory annotated methods Jul 2, 2023
@hanszt hanszt linked a pull request Jul 2, 2023 that will close this issue
6 tasks
@marcphilipp marcphilipp linked a pull request Jul 7, 2023 that will close this issue
6 tasks
@marcphilipp
Copy link
Member

Team decision: Add convention-based conversion to Stream that looks for methods called iterator that return Iterator.

@hanszt
Copy link
Author

hanszt commented Jul 19, 2023

Thanks for the feedback. I implemented the requested changes in this pullrequest

@marcphilipp marcphilipp modified the milestones: 5.11 M1, 5.12 Feb 2, 2024
@marcphilipp marcphilipp modified the milestones: 5.11 M2, 5.11 M3 May 17, 2024
hanszt pushed a commit to hanszt/junit5 that referenced this issue May 27, 2024
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.
@sbrannen sbrannen changed the title Add kotlin Sequence support for @TestFactory annotated methods Add Kotlin Sequence support for @TestFactory methods Jun 2, 2024
@marcphilipp marcphilipp removed this from the 5.11 M3 milestone Jul 5, 2024
hanszt pushed a commit to hanszt/junit5 that referenced this issue Sep 27, 2024
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Todo
Development

Successfully merging a pull request may close this issue.

2 participants