Add additional Result api test
This commit is contained in:
committed by
Alexander Udalov
parent
49efa5fbc4
commit
d8646e29b7
@@ -0,0 +1,42 @@
|
||||
inline class Result<out T>(val value: Any?) {
|
||||
fun exceptionOrNull(): Throwable? =
|
||||
when (value) {
|
||||
is Failure -> value.exception
|
||||
else -> null
|
||||
}
|
||||
|
||||
public companion object {
|
||||
public inline fun <T> success(value: T): Result<T> =
|
||||
Result(value)
|
||||
|
||||
public inline fun <T> failure(exception: Throwable): Result<T> =
|
||||
Result(Failure(exception))
|
||||
}
|
||||
|
||||
class Failure(
|
||||
val exception: Throwable
|
||||
)
|
||||
}
|
||||
|
||||
inline fun <T, R> T.runCatching(block: T.() -> R): Result<R> {
|
||||
return try {
|
||||
Result.success(block())
|
||||
} catch (e: Throwable) {
|
||||
Result.failure(e)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
inline fun <R, T : R> Result<T>.getOrElse(onFailure: (exception: Throwable) -> R): R {
|
||||
return when (val exception = exceptionOrNull()) {
|
||||
null -> value as T
|
||||
else -> onFailure(exception)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class A {
|
||||
fun f() = runCatching { "OK" }.getOrElse { throw it }
|
||||
}
|
||||
|
||||
fun box(): String = A().f()
|
||||
+5
@@ -12800,6 +12800,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
|
||||
runTest("compiler/testData/codegen/box/inlineClasses/resultInlining.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("resultRunCatchingOrElse.kt")
|
||||
public void testResultRunCatchingOrElse() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/inlineClasses/resultRunCatchingOrElse.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("secondaryConstructorWithVararg.kt")
|
||||
public void testSecondaryConstructorWithVararg() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/inlineClasses/secondaryConstructorWithVararg.kt");
|
||||
|
||||
+5
@@ -12805,6 +12805,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
|
||||
runTest("compiler/testData/codegen/box/inlineClasses/resultInlining.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("resultRunCatchingOrElse.kt")
|
||||
public void testResultRunCatchingOrElse() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/inlineClasses/resultRunCatchingOrElse.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("secondaryConstructorWithVararg.kt")
|
||||
public void testSecondaryConstructorWithVararg() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/inlineClasses/secondaryConstructorWithVararg.kt");
|
||||
|
||||
+5
@@ -11685,6 +11685,11 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
|
||||
runTest("compiler/testData/codegen/box/inlineClasses/resultInlining.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("resultRunCatchingOrElse.kt")
|
||||
public void testResultRunCatchingOrElse() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/inlineClasses/resultRunCatchingOrElse.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("secondaryConstructorWithVararg.kt")
|
||||
public void testSecondaryConstructorWithVararg() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/inlineClasses/secondaryConstructorWithVararg.kt");
|
||||
|
||||
Generated
+5
@@ -10085,6 +10085,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest {
|
||||
runTest("compiler/testData/codegen/box/inlineClasses/resultInlining.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("resultRunCatchingOrElse.kt")
|
||||
public void testResultRunCatchingOrElse() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/inlineClasses/resultRunCatchingOrElse.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("secondaryConstructorWithVararg.kt")
|
||||
public void testSecondaryConstructorWithVararg() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/inlineClasses/secondaryConstructorWithVararg.kt");
|
||||
|
||||
+5
@@ -11240,6 +11240,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
|
||||
runTest("compiler/testData/codegen/box/inlineClasses/resultInlining.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("resultRunCatchingOrElse.kt")
|
||||
public void testResultRunCatchingOrElse() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/inlineClasses/resultRunCatchingOrElse.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("secondaryConstructorWithVararg.kt")
|
||||
public void testSecondaryConstructorWithVararg() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/inlineClasses/secondaryConstructorWithVararg.kt");
|
||||
|
||||
Reference in New Issue
Block a user