[b4b313]: / matlab_xunit_3.1 / matlab_xunit / xunit / assertExceptionThrown.m

Download this file

54 lines (49 with data), 2.0 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
function assertExceptionThrown(f, expectedId, custom_message)
%assertExceptionThrown Assert that specified exception is thrown
% assertExceptionThrown(F, expectedId) calls the function handle F with no
% input arguments. If the result is a thrown exception whose identifier is
% expectedId, then assertExceptionThrown returns silently. If no exception is
% thrown, then assertExceptionThrown throws an exception with identifier equal
% to 'assertExceptionThrown:noException'. If a different exception is thrown,
% then assertExceptionThrown throws an exception identifier equal to
% 'assertExceptionThrown:wrongException'.
%
% assertExceptionThrown(F, expectedId, msg) prepends the string msg to the
% assertion message.
%
% Example
% -------
% % This call returns silently.
% f = @() error('a:b:c', 'error message');
% assertExceptionThrown(f, 'a:b:c');
%
% % This call returns silently.
% assertExceptionThrown(@() sin, 'MATLAB:minrhs');
%
% % This call throws an error because calling sin(pi) does not error.
% assertExceptionThrown(@() sin(pi), 'MATLAB:foo');
% Steven L. Eddins
% Copyright 2008-2010 The MathWorks, Inc.
noException = false;
try
f();
noException = true;
catch exception
if ~strcmp(exception.identifier, expectedId)
message = sprintf('Expected exception %s but got exception %s.', ...
expectedId, exception.identifier);
if nargin >= 3
message = sprintf('%s\n%s', custom_message, message);
end
throwAsCaller(MException('assertExceptionThrown:wrongException', ...
'%s', message));
end
end
if noException
message = sprintf('Expected exception "%s", but none thrown.', ...
expectedId);
if nargin >= 3
message = sprintf('%s\n%s', custom_message, message);
end
throwAsCaller(MException('assertExceptionThrown:noException', '%s', message));
end