Exception Handling in Ada during Unit Test

ada, exception, unit-testing

Solution

You can add a block inside the loop. Using your pseudo-syntax, it will look something like:

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

Problem

I am attempting to write some unit tests for some Ada code I recently wrote, I have a particular case where I am expecting to get an Exception (if the code worked correctly I wouldn't but in this case all I'm doing is testing, not writing code). If I handle the exception in the Testing routine then I don't see how I can continue testing in that procedure. I.E. (This is very much an example and NOT compilable code) ``` 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 ``` My first thought is if I had a "deeper function" which actually did the call and the exception returned through that. I.E. (This is very much an example and NOT compilable code) ``` 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; ``` And in theory I expect that would work, I just hate to have to keep writing sub functions when my test_function, is expected to do the actual testing. Is there a way to continue execution after hitting the exception IN Test_Function, rather than having to write a wrapper function and calling through that? OR Is there a easier/better way to handle this kind of scenario? *Sorry for the poor code example, but I think the idea should be clear, if not I'll re-write the code.

Original source