Hello everyone,
I’m working on a PR for #17277 to add a way to assert if a procedure throws an error, and I think the standard idea with a proc assertThrows(someProc, errType: Error?, ...args?) will work well.
But I thought it’d be cool to have, rather than the above, something like
manage test.raises(SomeError) {
someProc(arg1, arg2=arg2); // throws SomeError
}
// OR
manage test.raises(DivideByZeroError) {
var bar = 1.0 / 0.0;
}
This is similar to pytest.raises and would be helpful if the user wanted to test if a procedure throws an error while still allowing them to use keyword arguments (handy as # of args grows), or even a group of statements.
Unfortunately, I’m still a bit green with chapel and especially about the rules of using a manage statement. I’m struggling to get the following to compile:
module Foo {
class AssertionError: Error { // thanks UnitTest
proc init(msg: string = "") {
super.init(msg);
}
}
class RaiseErrorManager: contextManager {
type errType;
proc enterContext() {}
proc exitContext(in err: owned Error?) throws {
try {
if err then throw err;
throw new owned AssertionError("manage stmt didn't throw any error!");
}
catch e: errType { // anticipated error
return;
}
catch e { // unanticipated error
throw e;
}
}
}
class SomeError : Error {
proc init() {}
}
proc main() {
proc foo() throws {
throw new owned SomeError();
}
manage new owned RaiseErrorManager(SomeError) {
foo();
}
}
}
I’m not sure if contextManager supports a generic subclass like this (I was unable to return a type from enterContext as a usable resource), or if try-catch is possible in the exitContext?