我正在尝试为我最近编写的一些 Ada 代码编写一些单元测试,我有一个特殊的情况,我希望得到一个异常(如果代码工作正常,我不会,但在这种情况下我所做的只是测试,而不是编写代码)。如果我在测试例程中处理异常,那么我不知道如何继续在该过程中进行测试。
IE。 (这只是一个示例,不可编译的代码)
procedure Test_Function is
begin
from -20 to 20
Result := SQRT(i);
if Result = (Expected) then
print "Passed";
end_if;
exception:
print "FAILED";
end Test_Function
我的第一个想法是我是否有一个“更深层次的函数”,它实际上执行了调用并通过它返回了异常。
IE。 (这只是一个示例,不可编译的代码)
procedure Test_Function is
begin
from -20 to 20
Result := my_SQRT(i);
if Result = (Expected) then
print "Passed";
end_if;
exception:
print "FAILED";
end Test_Function
function my_SQRT(integer) return Integer is
begin
return SQRT(i);
exception:
return -1;
end my_SQRT;
理论上我希望这会起作用,我只是讨厌当我的 test_function 预计进行实际测试时必须继续编写子函数。
有没有办法在 Test_Function 中遇到异常后继续执行,而不必编写包装函数并通过它进行调用?
或者
有没有更简单/更好的方法来处理这种情况?
*对于糟糕的代码示例感到抱歉,但我认为这个想法应该很清楚,如果不是我会重新编写代码。
您可以在循环内添加一个块。
使用你的伪语法,它看起来像:
procedure Test_Function is
begin
from -20 to 20
begin
Result := SQRT(i);
if Result = (Expected) then
print "Passed";
end_if;
exception:
print "FAILED";
end;
end loop;
end Test_Function
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)