-
-
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.
Kotlin Sequence support for @testfactory
Classes that expose an Iterator returning method, can be converted to a stream. Classes that expose a Spliterator returning method, can be converted to a stream. ```markdown --- I hereby agree to the terms of the JUnit Contributor License Agreement. ```
- Loading branch information
Hans Zuidervaart
committed
Jul 2, 2023
1 parent
1f36bc8
commit 7cb201b
Showing
4 changed files
with
253 additions
and
8 deletions.
There are no files selected for viewing
76 changes: 76 additions & 0 deletions
76
junit-jupiter-engine/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,76 @@ | ||
/* | ||
* Copyright 2015-2023 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.Assertions.assertFalse | ||
import org.junit.jupiter.api.Assertions.assertTrue | ||
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.10 | ||
*/ | ||
class KotlinDynamicTests { | ||
|
||
@Nested | ||
inner class SequenceReturningTestFactoryTests { | ||
|
||
@TestFactory | ||
fun `dynamic tests returned as Kotlin sequence`() = generateSequence(0) { it + 2 } | ||
.map { dynamicTest("$it should be even") { assertTrue(it % 2 == 0) } } | ||
.take(10) | ||
|
||
@TestFactory | ||
fun `is anagram tests`(): Sequence<DynamicTest> { | ||
infix fun CharSequence.isAngramOf(other: CharSequence) = groupBy { it } == other.groupBy { it } | ||
|
||
infix fun CharSequence.`should be an anagram of`(other: CharSequence) = | ||
dynamicTest("'$this' should be an anagram of '$other'") { assertTrue(this isAngramOf other) } | ||
|
||
infix fun CharSequence.`should not be an anagram of`(other: CharSequence) = | ||
dynamicTest("'$this' should not be an anagram of '$other'") { assertFalse(this isAngramOf other) } | ||
|
||
return sequenceOf( | ||
"a gentleman" `should be an anagram of` "elegant man", | ||
"laptop machines" `should be an anagram of` "apple macintosh", | ||
"salvador dali" `should be an anagram of` "avida dollars", | ||
"a gentleman" `should not be an anagram of` "spider man", | ||
"laptop computers" `should not be an anagram of` "apple macintosh", | ||
"salvador dali" `should not be an anagram of` "picasso" | ||
) | ||
} | ||
|
||
@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) | ||
} | ||
} | ||
} |
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
97 changes: 97 additions & 0 deletions
97
junit-platform-commons/src/main/java/org/junit/platform/commons/util/StreamUtils.java
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,97 @@ | ||
/* | ||
* Copyright 2015-2023 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.platform.commons.util; | ||
|
||
import static java.util.Spliterator.ORDERED; | ||
import static java.util.Spliterators.spliteratorUnknownSize; | ||
import static java.util.stream.StreamSupport.stream; | ||
|
||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
import java.util.Arrays; | ||
import java.util.Iterator; | ||
import java.util.Optional; | ||
import java.util.Spliterator; | ||
import java.util.stream.Stream; | ||
|
||
import org.junit.platform.commons.JUnitException; | ||
import org.junit.platform.commons.PreconditionViolationException; | ||
|
||
/** | ||
* Collection of utilities for working with {@link Stream Streams}. | ||
* | ||
* @since 5.10 | ||
*/ | ||
final class StreamUtils { | ||
|
||
private StreamUtils() { | ||
} | ||
|
||
static Stream<?> tryConvertToStreamByIterator(Object object) { | ||
Class<?> theClass = object.getClass(); | ||
try { | ||
Method method = theClass.getMethod("iterator"); | ||
return stream(() -> tryIteratorToSpliterator(object, method), ORDERED, false); | ||
} | ||
catch (NoSuchMethodException e) { | ||
return tryConvertToStreamBySpliterator(object); | ||
} | ||
} | ||
|
||
private static Stream<?> tryConvertToStreamBySpliterator(Object object) { | ||
try { | ||
Method method = object.getClass().getMethod("spliterator"); | ||
return stream(() -> tryInvokeSpliterator(object, method), ORDERED, false); | ||
} | ||
catch (NoSuchMethodException ex) { | ||
return tryConvertByIteratorSpliteratorReturnType(object); | ||
} | ||
} | ||
|
||
private static Stream<?> tryConvertByIteratorSpliteratorReturnType(Object object) { | ||
Optional<? extends Stream<?>> stream = streamFromSpliteratorSupplier(object); | ||
// is necessary because orElseGet gives generics compile errors | ||
return stream.isPresent() ? stream.get() | ||
: streamFromIteratorSupplier(object).orElseThrow(() -> new PreconditionViolationException( | ||
"Cannot convert instance of " + object.getClass().getName() + " into a Stream: " + object)); | ||
|
||
} | ||
|
||
private static Optional<? extends Stream<?>> streamFromSpliteratorSupplier(Object object) { | ||
return Arrays.stream(object.getClass().getMethods()).filter( | ||
m -> m.getReturnType() == Spliterator.class).findFirst().map( | ||
m -> stream(() -> tryInvokeSpliterator(object, m), ORDERED, false)); | ||
} | ||
|
||
private static Optional<Stream<?>> streamFromIteratorSupplier(Object object) { | ||
return Arrays.stream(object.getClass().getMethods()).filter( | ||
m -> m.getReturnType() == Iterator.class).findFirst().map( | ||
m -> stream(() -> tryIteratorToSpliterator(object, m), ORDERED, false)); | ||
} | ||
|
||
private static Spliterator<?> tryInvokeSpliterator(Object object, Method method) { | ||
try { | ||
return (Spliterator<?>) method.invoke(object); | ||
} | ||
catch (IllegalAccessException | InvocationTargetException | ClassCastException ex) { | ||
throw new JUnitException("Cannot invoke method " + method.getName() + " onto " + object); | ||
} | ||
} | ||
|
||
private static Spliterator<?> tryIteratorToSpliterator(Object object, Method method) { | ||
try { | ||
return spliteratorUnknownSize((Iterator<?>) method.invoke(object), ORDERED); | ||
} | ||
catch (IllegalAccessException | InvocationTargetException | ClassCastException e) { | ||
throw new JUnitException("Cannot invoke method " + method.getName() + " onto " + object); | ||
} | ||
} | ||
} |
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