-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extension of to stream converter method.
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: #3376 I hereby agree to the terms of the JUnit Contributor License Agreement.
- Loading branch information
Showing
6 changed files
with
186 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
jupiter-tests/src/test/kotlin/org/junit/jupiter/api/KotlinDynamicTests.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright 2015-2024 the original author or authors. | ||
* | ||
* All rights reserved. This program and the accompanying materials are | ||
* made available under the terms of the Eclipse Public License v2.0 which | ||
* accompanies this distribution and is available at | ||
* | ||
* https://www.eclipse.org/legal/epl-v20.html | ||
*/ | ||
package org.junit.jupiter.api | ||
|
||
import org.junit.jupiter.api.Assertions.assertEquals | ||
import org.junit.jupiter.api.DynamicTest.dynamicTest | ||
import java.math.BigDecimal | ||
import java.math.BigDecimal.ONE | ||
import java.math.MathContext | ||
import java.math.BigInteger as BigInt | ||
import java.math.RoundingMode as Rounding | ||
|
||
/** | ||
* Unit tests for JUnit Jupiter [TestFactory] use in kotlin classes. | ||
* | ||
* @since 5.12 | ||
*/ | ||
class KotlinDynamicTests { | ||
@Nested | ||
inner class SequenceReturningTestFactoryTests { | ||
@TestFactory | ||
fun `Dynamic tests returned as Kotlin sequence`() = | ||
generateSequence(0) { it + 2 } | ||
.map { dynamicTest("$it should be even") { assertEquals(0, it % 2) } } | ||
.take(10) | ||
|
||
@TestFactory | ||
fun `Consecutive fibonacci nr ratios, should converge to golden ratio as n increases`(): Sequence<DynamicTest> { | ||
val scale = 5 | ||
val goldenRatio = | ||
(ONE + 5.toBigDecimal().sqrt(MathContext(scale + 10, Rounding.HALF_UP))) | ||
.divide(2.toBigDecimal(), scale, Rounding.HALF_UP) | ||
|
||
fun shouldApproximateGoldenRatio( | ||
cur: BigDecimal, | ||
next: BigDecimal | ||
) = next.divide(cur, scale, Rounding.HALF_UP).let { | ||
dynamicTest("$cur / $next = $it should approximate the golden ratio in $scale decimals") { | ||
assertEquals(goldenRatio, it) | ||
} | ||
} | ||
return generateSequence(BigInt.ONE to BigInt.ONE) { (cur, next) -> next to cur + next } | ||
.map { (cur) -> cur.toBigDecimal() } | ||
.zipWithNext(::shouldApproximateGoldenRatio) | ||
.drop(14) | ||
.take(10) | ||
} | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...ter-tests/src/test/kotlin/org/junit/jupiter/params/aggregator/KotlinParameterizedTests.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright 2015-2024 the original author or authors. | ||
* | ||
* All rights reserved. This program and the accompanying materials are | ||
* made available under the terms of the Eclipse Public License v2.0 which | ||
* accompanies this distribution and is available at | ||
* | ||
* https://www.eclipse.org/legal/epl-v20.html | ||
*/ | ||
package org.junit.jupiter.params.aggregator | ||
|
||
import org.junit.jupiter.api.Assertions.assertEquals | ||
import org.junit.jupiter.params.ParameterizedTest | ||
import org.junit.jupiter.params.provider.Arguments.arguments | ||
import org.junit.jupiter.params.provider.MethodSource | ||
import java.time.Month | ||
|
||
/** | ||
* Tests for ParameterizedTest kotlin compatibility | ||
*/ | ||
object KotlinParameterizedTests { | ||
@ParameterizedTest | ||
@MethodSource("dataProvidedByKotlinSequence") | ||
fun `a method source can be supplied by a Sequence returning method`( | ||
value: Int, | ||
month: Month | ||
) { | ||
assertEquals(value, month.value) | ||
} | ||
|
||
@JvmStatic | ||
private fun dataProvidedByKotlinSequence() = | ||
sequenceOf( | ||
arguments(1, Month.JANUARY), | ||
arguments(3, Month.MARCH), | ||
arguments(8, Month.AUGUST), | ||
arguments(5, Month.MAY), | ||
arguments(12, Month.DECEMBER) | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters