| 1 |
/* |
| 2 |
* Copyright 2015-2024 the original author or authors. |
| 3 |
* |
| 4 |
* All rights reserved. This program and the accompanying materials are |
| 5 |
* made available under the terms of the Eclipse Public License v2.0 which |
| 6 |
* accompanies this distribution and is available at |
| 7 |
* |
| 8 |
* https://www.eclipse.org/legal/epl-v20.html |
| 9 |
*/ |
| 10 |
@file:API(status = STABLE, since = "5.7") |
| 11 |
|
| 12 |
package org.junit.jupiter.api |
| 13 |
|
| 14 |
import org.apiguardian.api.API |
| 15 |
import org.apiguardian.api.API.Status.EXPERIMENTAL |
| 16 |
import org.apiguardian.api.API.Status.STABLE |
| 17 |
import org.junit.jupiter.api.function.Executable |
| 18 |
import org.junit.jupiter.api.function.ThrowingSupplier |
| 19 |
import java.time.Duration |
| 20 |
import java.util.function.Supplier |
| 21 |
import java.util.stream.Stream |
| 22 |
|
| 23 |
/** |
| 24 |
* @see Assertions.fail |
| 25 |
*/ |
| 26 |
fun fail(message: String?, throwable: Throwable? = null): Nothing = |
| 27 |
Assertions.fail<Nothing>(message, throwable) |
| 28 |
|
| 29 |
/** |
| 30 |
* @see Assertions.fail |
| 31 |
*/ |
| 32 |
fun fail(message: (() -> String)?): Nothing = |
| 33 |
Assertions.fail<Nothing>(message) |
| 34 |
|
| 35 |
/** |
| 36 |
* @see Assertions.fail |
| 37 |
*/ |
| 38 |
fun fail(throwable: Throwable?): Nothing = |
| 39 |
Assertions.fail<Nothing>(throwable) |
| 40 |
|
| 41 |
/** |
| 42 |
* [Stream] of functions to be executed. |
| 43 |
*/ |
| 44 |
private typealias ExecutableStream = Stream<() -> Unit> |
| 45 |
private fun ExecutableStream.convert() = map { Executable(it) } |
| 46 |
|
| 47 |
/** |
| 48 |
* @see Assertions.assertAll |
| 49 |
*/ |
| 50 |
fun assertAll(executables: ExecutableStream) = |
| 51 |
Assertions.assertAll(executables.convert()) |
| 52 |
|
| 53 |
/** |
| 54 |
* @see Assertions.assertAll |
| 55 |
*/ |
| 56 |
fun assertAll(heading: String?, executables: ExecutableStream) = |
| 57 |
Assertions.assertAll(heading, executables.convert()) |
| 58 |
|
| 59 |
/** |
| 60 |
* [Collection] of functions to be executed. |
| 61 |
*/ |
| 62 |
private typealias ExecutableCollection = Collection<() -> Unit> |
| 63 |
private fun ExecutableCollection.convert() = map { Executable(it) } |
| 64 |
|
| 65 |
/** |
| 66 |
* @see Assertions.assertAll |
| 67 |
*/ |
| 68 |
fun assertAll(executables: ExecutableCollection) = |
| 69 |
Assertions.assertAll(executables.convert()) |
| 70 |
|
| 71 |
/** |
| 72 |
* @see Assertions.assertAll |
| 73 |
*/ |
| 74 |
fun assertAll(heading: String?, executables: ExecutableCollection) = |
| 75 |
Assertions.assertAll(heading, executables.convert()) |
| 76 |
|
| 77 |
/** |
| 78 |
* @see Assertions.assertAll |
| 79 |
*/ |
| 80 |
fun assertAll(vararg executables: () -> Unit) = |
| 81 |
assertAll(executables.toList().stream()) |
| 82 |
|
| 83 |
/** |
| 84 |
* @see Assertions.assertAll |
| 85 |
*/ |
| 86 |
fun assertAll(heading: String?, vararg executables: () -> Unit) = |
| 87 |
assertAll(heading, executables.toList().stream()) |
| 88 |
|
| 89 |
/** |
| 90 |
* Example usage: |
| 91 |
* ```kotlin |
| 92 |
* val exception = assertThrows<IllegalArgumentException> { |
| 93 |
* throw IllegalArgumentException("Talk to a duck") |
| 94 |
* } |
| 95 |
* assertEquals("Talk to a duck", exception.message) |
| 96 |
* ``` |
| 97 |
* @see Assertions.assertThrows |
| 98 |
*/ |
| 99 |
inline fun <reified T : Throwable> assertThrows(executable: () -> Unit): T { |
| 100 |
val throwable: Throwable? = try { |
| 101 |
executable() |
| 102 |
} catch (caught: Throwable) { |
| 103 |
caught |
| 104 |
} as? Throwable |
| 105 |
|
| 106 |
return Assertions.assertThrows(T::class.java) { |
| 107 |
if (throwable != null) { |
| 108 |
throw throwable |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Example usage: |
| 115 |
* ```kotlin |
| 116 |
* val exception = assertThrows<IllegalArgumentException>("Should throw an Exception") { |
| 117 |
* throw IllegalArgumentException("Talk to a duck") |
| 118 |
* } |
| 119 |
* assertEquals("Talk to a duck", exception.message) |
| 120 |
* ``` |
| 121 |
* @see Assertions.assertThrows |
| 122 |
*/ |
| 123 |
inline fun <reified T : Throwable> assertThrows(message: String, executable: () -> Unit): T = |
| 124 |
assertThrows({ message }, executable) |
| 125 |
|
| 126 |
/** |
| 127 |
* Example usage: |
| 128 |
* ```kotlin |
| 129 |
* val exception = assertThrows<IllegalArgumentException>({ "Should throw an Exception" }) { |
| 130 |
* throw IllegalArgumentException("Talk to a duck") |
| 131 |
* } |
| 132 |
* assertEquals("Talk to a duck", exception.message) |
| 133 |
* ``` |
| 134 |
* @see Assertions.assertThrows |
| 135 |
*/ |
| 136 |
inline fun <reified T : Throwable> assertThrows(noinline message: () -> String, executable: () -> Unit): T { |
| 137 |
val throwable: Throwable? = try { |
| 138 |
executable() |
| 139 |
} catch (caught: Throwable) { |
| 140 |
caught |
| 141 |
} as? Throwable |
| 142 |
|
| 143 |
return Assertions.assertThrows( |
| 144 |
T::class.java, |
| 145 |
Executable { |
| 146 |
if (throwable != null) { |
| 147 |
throw throwable |
| 148 |
} |
| 149 |
}, |
| 150 |
Supplier(message) |
| 151 |
) |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Example usage: |
| 156 |
* ```kotlin |
| 157 |
* val result = assertDoesNotThrow { |
| 158 |
* // Code block that is expected to not throw an exception |
| 159 |
* } |
| 160 |
* ``` |
| 161 |
* @see Assertions.assertDoesNotThrow |
| 162 |
* @param R the result type of the [executable] |
| 163 |
*/ |
| 164 |
@API(status = EXPERIMENTAL, since = "5.5") |
| 165 |
inline fun <R> assertDoesNotThrow(executable: () -> R): R = |
| 166 |
Assertions.assertDoesNotThrow(evaluateAndWrap(executable)) |
| 167 |
|
| 168 |
/** |
| 169 |
* Example usage: |
| 170 |
* ```kotlin |
| 171 |
* val result = assertDoesNotThrow("Should not throw an exception") { |
| 172 |
* // Code block that is expected to not throw an exception |
| 173 |
* } |
| 174 |
* ``` |
| 175 |
* @see Assertions.assertDoesNotThrow |
| 176 |
* @param R the result type of the [executable] |
| 177 |
*/ |
| 178 |
@API(status = EXPERIMENTAL, since = "5.5") |
| 179 |
inline fun <R> assertDoesNotThrow(message: String, executable: () -> R): R = |
| 180 |
assertDoesNotThrow({ message }, executable) |
| 181 |
|
| 182 |
/** |
| 183 |
* Example usage: |
| 184 |
* ```kotlin |
| 185 |
* val result = assertDoesNotThrow({ "Should not throw an exception" }) { |
| 186 |
* // Code block that is expected to not throw an exception |
| 187 |
* } |
| 188 |
* ``` |
| 189 |
* @see Assertions.assertDoesNotThrow |
| 190 |
* @param R the result type of the [executable] |
| 191 |
*/ |
| 192 |
@API(status = EXPERIMENTAL, since = "5.5") |
| 193 |
inline fun <R> assertDoesNotThrow(noinline message: () -> String, executable: () -> R): R = |
| 194 |
Assertions.assertDoesNotThrow( |
| 195 |
evaluateAndWrap(executable), |
| 196 |
Supplier(message) |
| 197 |
) |
| 198 |
|
| 199 |
@PublishedApi |
| 200 |
internal inline fun <R> evaluateAndWrap(executable: () -> R): ThrowingSupplier<R> = try { |
| 201 |
val result = executable() |
| 202 |
ThrowingSupplier { result } |
| 203 |
} catch (throwable: Throwable) { |
| 204 |
ThrowingSupplier { throw throwable } |
| 205 |
} |
| 206 |
|
| 207 |
/** |
| 208 |
* Example usage: |
| 209 |
* ```kotlin |
| 210 |
* val result = assertTimeout(Duration.seconds(1)) { |
| 211 |
* // Code block that is being timed. |
| 212 |
* } |
| 213 |
* ``` |
| 214 |
* @see Assertions.assertTimeout |
| 215 |
* @paramR the result of the [executable]. |
| 216 |
*/ |
| 217 |
@API(status = EXPERIMENTAL, since = "5.5") |
| 218 |
fun <R> assertTimeout(timeout: Duration, executable: () -> R): R = |
| 219 |
Assertions.assertTimeout(timeout, executable) |
| 220 |
|
| 221 |
/** |
| 222 |
* Example usage: |
| 223 |
* ```kotlin |
| 224 |
* val result = assertTimeout(Duration.seconds(1), "Should only take one second") { |
| 225 |
* // Code block that is being timed. |
| 226 |
* } |
| 227 |
* ``` |
| 228 |
* @see Assertions.assertTimeout |
| 229 |
* @paramR the result of the [executable]. |
| 230 |
*/ |
| 231 |
@API(status = EXPERIMENTAL, since = "5.5") |
| 232 |
fun <R> assertTimeout(timeout: Duration, message: String, executable: () -> R): R = |
| 233 |
Assertions.assertTimeout(timeout, executable, message) |
| 234 |
|
| 235 |
/** |
| 236 |
* Example usage: |
| 237 |
* ```kotlin |
| 238 |
* val result = assertTimeout(Duration.seconds(1), { "Should only take one second" }) { |
| 239 |
* // Code block that is being timed. |
| 240 |
* } |
| 241 |
* ``` |
| 242 |
* @see Assertions.assertTimeout |
| 243 |
* @paramR the result of the [executable]. |
| 244 |
*/ |
| 245 |
@API(status = EXPERIMENTAL, since = "5.5") |
| 246 |
fun <R> assertTimeout(timeout: Duration, message: () -> String, executable: () -> R): R = |
| 247 |
Assertions.assertTimeout(timeout, executable, message) |
| 248 |
|
| 249 |
/** |
| 250 |
* Example usage: |
| 251 |
* ```kotlin |
| 252 |
* val result = assertTimeoutPreemptively(Duration.seconds(1)) { |
| 253 |
* // Code block that is being timed. |
| 254 |
* } |
| 255 |
* ``` |
| 256 |
* @see Assertions.assertTimeoutPreemptively |
| 257 |
* @paramR the result of the [executable]. |
| 258 |
*/ |
| 259 |
@API(status = EXPERIMENTAL, since = "5.5") |
| 260 |
fun <R> assertTimeoutPreemptively(timeout: Duration, executable: () -> R): R = |
| 261 |
Assertions.assertTimeoutPreemptively(timeout, executable) |
| 262 |
|
| 263 |
/** |
| 264 |
* Example usage: |
| 265 |
* ```kotlin |
| 266 |
* val result = assertTimeoutPreemptively(Duration.seconds(1), "Should only take one second") { |
| 267 |
* // Code block that is being timed. |
| 268 |
* } |
| 269 |
* ``` |
| 270 |
* @see Assertions.assertTimeoutPreemptively |
| 271 |
* @paramR the result of the [executable]. |
| 272 |
*/ |
| 273 |
@API(status = EXPERIMENTAL, since = "5.5") |
| 274 |
fun <R> assertTimeoutPreemptively(timeout: Duration, message: String, executable: () -> R): R = |
| 275 |
Assertions.assertTimeoutPreemptively(timeout, executable, message) |
| 276 |
|
| 277 |
/** |
| 278 |
* Example usage: |
| 279 |
* ```kotlin |
| 280 |
* val result = assertTimeoutPreemptively(Duration.seconds(1), { "Should only take one second" }) { |
| 281 |
* // Code block that is being timed. |
| 282 |
* } |
| 283 |
* ``` |
| 284 |
* @see Assertions.assertTimeoutPreemptively |
| 285 |
* @paramR the result of the [executable]. |
| 286 |
*/ |
| 287 |
@API(status = EXPERIMENTAL, since = "5.5") |
| 288 |
fun <R> assertTimeoutPreemptively(timeout: Duration, message: () -> String, executable: () -> R): R = |
| 289 |
Assertions.assertTimeoutPreemptively(timeout, executable, message) |