When writing tests for a package, one might want to test that a function with a given invalid input throws an error as expected. Looking at UnitTest module, I didn't find any assertThrows function. What is the idiomatic way of testing this?
Off the top of my head, I would try something like this
use UnitTest;
use UnitTest.TestError; // needed to define the custom AssertionError
proc fun(x : int) throws {
if x > 0 then throw new IllegalArgumentError("received input: " + x : string);
else return 3;
}
proc testFun(test : borrowed Test) throws {
try {
fun(3);
throw new owned AssertionError("function was expected to throw an error");
} catch e: IllegalArgumentError {
test.assertEqual(e.message(), "received input: 3");
}
}
UnitTest.main();
comments? suggestions on other approaches?