gpt4 book ai didi

unit-testing - 单元测试期间 Ada 中的异常处理

转载 作者:行者123 更新时间:2023-12-04 04:40:00 26 4
gpt4 key购买 nike

我正在尝试为我最近写的一些 Ada 代码编写一些单元测试,我有一个特殊的情况,我希望得到一个异常(如果代码工作正常我不会,但在这种情况下我是做的是测试,而不是编写代码)。如果我在测试例程中处理异常,那么我看不出如何在该过程中继续测试。

即(这只是一个示例,不可编译代码)

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

我的第一个想法是,如果我有一个“更深层次的函数”,它实际上执行了调用并通过它返回了异常。

即(这只是一个示例,不可编译代码)

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 遇到异常后继续执行,而不是必须编写包装函数并通过它调用?或者有没有更简单/更好的方法来处理这种情况?

*对于糟糕的代码示例,我深表歉意,但我认为思路应该很清晰,否则我将重写代码。

最佳答案

您可以在循环内添加一个 block 。使用您的伪语法,它看起来像:

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

关于unit-testing - 单元测试期间 Ada 中的异常处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13073793/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com