|
10 | 10 | % warningMessage: The specific warning message that need to be tested
|
11 | 11 | %
|
12 | 12 | % OUPUTS:
|
13 |
| -% success: Verify if the function throws the provided warning message and catch it in tline |
| 13 | +% success: Boolean indicating whether the warning message is thrown |
14 | 14 | %
|
15 | 15 | % EXAMPLE:
|
16 | 16 | % % Test whether PaCER function throws the provided warning message:
|
17 |
| -% assert(verifyFunctionWarning('PaCER', warningMessage, 'inputs', {niiCT})); |
| 17 | +% verifyFunctionWarning('PaCER', warningMessage, 'inputs', {niiCT}); |
18 | 18 | %
|
19 | 19 | % AUTORS:
|
20 | 20 | % Laurent Heirendt, April 2019
|
|
25 | 25 | % https://github.com/opencobra/cobratoolbox/blob/master/test/verifyCobraFunctionError.m
|
26 | 26 | % [5aa9ccd]
|
27 | 27 |
|
28 |
| -parser = inputParser(); |
29 |
| -parser.addRequired('functionCall', @ischar) |
30 |
| -parser.addRequired('warningMessage', @ischar) |
31 |
| -parser.addParamValue('inputs', {}, @iscell) |
32 |
| -parser.addParamValue('outputArgCount', 0, @(x) isnumeric(x) && mod(x,1) == 0); |
33 |
| -parser.parse(functionCall, warningMessage, varargin{:}); |
34 |
| -outputArgcount = parser.Results.outputArgCount; |
35 |
| -inputs = parser.Results.inputs; |
36 |
| -functionCall = str2func(functionCall); |
37 |
| -% initialize the test |
38 |
| -success = false; |
39 |
| -% create an hidden file |
40 |
| -logFile = '.testCatch.log'; |
41 |
| -% run the function in diary mode |
42 |
| -diary(logFile); |
43 |
| -try |
44 |
| - if outputArgcount > 0 |
45 |
| - outArgs = cell(outputArgcount, 1); |
46 |
| - [outArgs{:}] = functionCall(inputs{:}); |
47 |
| - else |
48 |
| - functionCall(inputs{:}); |
| 28 | + parser = inputParser(); |
| 29 | + parser.addRequired('functionCall', @ischar) |
| 30 | + parser.addRequired('warningMessage', @ischar) |
| 31 | + parser.addParamValue('inputs', {}, @iscell) |
| 32 | + parser.addParamValue('outputArgCount', 0, @(x) isnumeric(x) && mod(x,1) == 0); |
| 33 | + parser.parse(functionCall, warningMessage, varargin{:}); |
| 34 | + outputArgcount = parser.Results.outputArgCount; |
| 35 | + |
| 36 | + inputs = parser.Results.inputs; |
| 37 | + functionCall = str2func(functionCall); |
| 38 | + |
| 39 | + % initialize the test |
| 40 | + success = false; |
| 41 | + |
| 42 | + % create an hidden file |
| 43 | + logFile = '.testCatch.log'; |
| 44 | + |
| 45 | + % run the function in diary mode |
| 46 | + diary(logFile); |
| 47 | + try |
| 48 | + if outputArgcount > 0 |
| 49 | + outArgs = cell(outputArgcount, 1); |
| 50 | + [outArgs{:}] = functionCall(inputs{:}); |
| 51 | + else |
| 52 | + functionCall(inputs{:}); |
| 53 | + end |
49 | 54 | end
|
50 |
| -end |
51 | 55 | diary off
|
52 |
| -% loop through the temporary diary file |
| 56 | + |
| 57 | + % loop through the temporary diary file |
53 | 58 | fid = fopen(logFile);
|
54 | 59 | tline = fgetl(fid);
|
55 | 60 | counter = 0;
|
56 |
| - % Read the code until it reaches the end of the script |
| 61 | + |
| 62 | + % read the code until it reaches the end of the script |
57 | 63 | while ~feof(fid)
|
58 |
| - % Setup the function to read the script three lines by three lines. If a |
59 |
| - % warning message is found in these three lines, this message will be |
60 |
| - % concatenated in tline. If not, tline will be empty. |
| 64 | + % setup the function to read the script three lines by three lines. |
61 | 65 | tline = [tline ' ' fgetl(fid)];
|
62 | 66 | if ~isempty(tline) && contains(tline, warningMessage)
|
63 | 67 | success = true;
|
64 | 68 | break;
|
65 | 69 | end
|
| 70 | + |
| 71 | + % if a warning message is found in consecutive three lines, this message will be |
| 72 | + % concatenated in tline. If not, tline will be reset. |
66 | 73 | if counter > 3
|
67 | 74 | tline = '';
|
68 | 75 | counter = 0;
|
69 | 76 | end
|
70 | 77 | counter = counter + 1;
|
71 | 78 | end
|
72 | 79 | fclose(fid);
|
| 80 | + |
73 | 81 | % remove the temporary diary file
|
74 | 82 | delete(logFile);
|
75 | 83 | end
|
0 commit comments