Force the returned value in test.catch() and test.catchOrNull() (#1910)
This commit is contained in:
1 parent
9f79fe47da
commit
e18d962033
5 files changed
+40
-4
No files matched your search
@@ -39,7 +39,7 @@ public final class TestNodes {
|
||||
@Specialization
|
||||
protected String eval(@SuppressWarnings("unused") VmTyped self, VmFunction function) {
|
||||
try {
|
||||
applyLambdaNode.execute(function);
|
||||
VmValue.force(applyLambdaNode.execute(function), false);
|
||||
} catch (VmException e) {
|
||||
return render(e);
|
||||
}
|
||||
@@ -54,7 +54,7 @@ public final class TestNodes {
|
||||
@Specialization
|
||||
protected Object eval(@SuppressWarnings("unused") VmTyped self, VmFunction function) {
|
||||
try {
|
||||
applyLambdaNode.execute(function);
|
||||
VmValue.force(applyLambdaNode.execute(function), false);
|
||||
} catch (VmException e) {
|
||||
return render(e);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
amends "../snippetTest.pkl"
|
||||
|
||||
import "pkl:test"
|
||||
|
||||
examples {
|
||||
["catch"] {
|
||||
test.catch(() -> throw("ouch"))
|
||||
// the returned value is fully evaluated, so these are caught too
|
||||
test.catch(() -> new Listing { 1 } as Listing<String>)
|
||||
test.catch(() -> new Dynamic { foo { bar = throw("nested") } })
|
||||
}
|
||||
["catchOrNull"] {
|
||||
test.catchOrNull(() -> 42)
|
||||
test.catchOrNull(() -> throw("ouch"))
|
||||
// the returned value is fully evaluated, so these are caught too
|
||||
test.catchOrNull(() -> new Listing { 1 } as Listing<String>)
|
||||
test.catchOrNull(() -> new Dynamic { foo { bar = throw("nested") } })
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
examples {
|
||||
["catch"] {
|
||||
"ouch"
|
||||
"Expected value of type `String`, but got type `Int`. Value: 1"
|
||||
"nested"
|
||||
}
|
||||
["catchOrNull"] {
|
||||
null
|
||||
"ouch"
|
||||
"Expected value of type `String`, but got type `Int`. Value: 1"
|
||||
"nested"
|
||||
}
|
||||
}
|
||||
@@ -5,11 +5,11 @@ examples {
|
||||
"Expected value of type `Int`, but got type `String`. Value: \"one\""
|
||||
}
|
||||
["listing"] {
|
||||
true
|
||||
false
|
||||
"Expected value of type `String`, but got type `Int`. Value: 1"
|
||||
}
|
||||
["mapping"] {
|
||||
true
|
||||
false
|
||||
"Expected value of type `String`, but got type `Int`. Value: 1"
|
||||
"Expected value of type `String`, but got type `Int`. Value: 2"
|
||||
"Expected value of type `Int`, but got type `String`. Value: \"hi\""
|
||||
|
||||
@@ -88,10 +88,14 @@ examples: Mapping<String, Listing<unknown>>?
|
||||
|
||||
/// Applies [fun] and returns the error thrown.
|
||||
///
|
||||
/// The value returned by [fun] is fully evaluated, so an error inside a nested object member will be caught too.
|
||||
///
|
||||
/// Throws if [fun] did not throw an error.
|
||||
external const function catch(fun: () -> Any): String
|
||||
|
||||
/// Applies [fun] and returns the error thrown.
|
||||
///
|
||||
/// The value returned by [fun] is fully evaluated, so an error inside a nested object member will be caught too.
|
||||
///
|
||||
/// Returns [null] if [fun] did not throw an error.
|
||||
external const function catchOrNull(fun: () -> Any): String?
|
||||
Reference in new issue
Block a user