8920
|
1 ## Copyright (C) 2005, 2006, 2007, 2009 Paul Kienzle |
6494
|
2 ## |
7016
|
3 ## This file is part of Octave. |
6494
|
4 ## |
7016
|
5 ## Octave is free software; you can redistribute it and/or modify it |
|
6 ## under the terms of the GNU General Public License as published by |
|
7 ## the Free Software Foundation; either version 3 of the License, or (at |
|
8 ## your option) any later version. |
|
9 ## |
|
10 ## Octave is distributed in the hope that it will be useful, but |
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 ## General Public License for more details. |
6494
|
14 ## |
|
15 ## You should have received a copy of the GNU General Public License |
7016
|
16 ## along with Octave; see the file COPYING. If not, see |
|
17 ## <http://www.gnu.org/licenses/>. |
|
18 ## |
|
19 ## Original version by Paul Kienzle distributed as free software in the |
|
20 ## public domain. |
6494
|
21 |
5589
|
22 ## -*- texinfo -*- |
|
23 ## @deftypefn {Function File} {} fail (@var{code},@var{pattern}) |
|
24 ## @deftypefnx {Function File} {} fail (@var{code},'warning',@var{pattern}) |
|
25 ## |
|
26 ## Return true if @var{code} fails with an error message matching |
|
27 ## @var{pattern}, otherwise produce an error. Note that @var{code} |
|
28 ## is a string and if @var{code} runs successfully, the error produced is: |
|
29 ## |
|
30 ## @example |
|
31 ## expected error but got none |
|
32 ## @end example |
|
33 ## |
|
34 ## If the code fails with a different error, the message produced is: |
|
35 ## |
|
36 ## @example |
|
37 ## expected <pattern> |
|
38 ## but got <text of actual error> |
|
39 ## @end example |
|
40 ## |
|
41 ## The angle brackets are not part of the output. |
|
42 ## |
|
43 ## Called with three arguments, the behavior is similar to |
|
44 ## @code{fail(@var{code}, @var{pattern})}, but produces an error if no |
|
45 ## warning is given during code execution or if the code fails. |
|
46 ## |
|
47 ## @end deftypefn |
|
48 |
|
49 ## Author: Paul Kienzle <pkienzle@users.sf.net> |
|
50 |
6494
|
51 function ret = fail (code, pattern, warning_pattern) |
|
52 |
|
53 if (nargin < 1 || nargin > 3) |
6046
|
54 print_usage (); |
5589
|
55 endif |
|
56 |
|
57 ## sort out arguments |
6494
|
58 test_warning = (nargin > 1 && strcmp (pattern, "warning")); |
|
59 if (nargin == 3) |
5589
|
60 pattern = warning_pattern; |
8507
|
61 elseif (nargin == 1 || (nargin == 2 && test_warning)) |
5589
|
62 pattern = ""; |
|
63 endif |
6494
|
64 |
|
65 ## match any nonempty message |
|
66 if (isempty (pattern)) |
|
67 pattern = "."; |
|
68 endif |
5589
|
69 |
|
70 ## allow assert(fail()) |
6494
|
71 if (nargout) |
|
72 ret = 1; |
|
73 endif |
5589
|
74 |
6494
|
75 if (test_warning) |
8506
|
76 ## Perform the warning test. |
|
77 ## Clear old warnings. |
|
78 lastwarn (); |
|
79 ## Make sure warnings are turned on. |
|
80 state = warning ("query", "quiet"); |
6494
|
81 warning ("on", "quiet"); |
5589
|
82 try |
|
83 ## printf("lastwarn before %s: %s\n",code,lastwarn); |
6494
|
84 evalin ("caller", sprintf ("%s;", code)); |
5589
|
85 ## printf("lastwarn after %s: %s\n",code,lastwarn); |
8506
|
86 ## Retrieve new warnings. |
|
87 err = lastwarn (); |
6494
|
88 warning (state.state, "quiet"); |
|
89 if (isempty (err)) |
|
90 msg = sprintf ("expected warning <%s> but got none", pattern); |
5589
|
91 else |
8506
|
92 ## Transform "warning: ...\n" to "...". |
|
93 err([1:9, end]) = []; |
6494
|
94 if (! isempty (regexp (err, pattern, "once"))) |
|
95 return; |
|
96 endif |
|
97 msg = sprintf ("expected warning <%s>\nbut got <%s>", pattern, err); |
5589
|
98 endif |
|
99 catch |
6494
|
100 warning (state.state, "quiet"); |
5589
|
101 err = lasterr; |
8506
|
102 ## Transform "error: ...\n", to "...". |
|
103 err([1:7, end]) = []; |
6494
|
104 msg = sprintf ("expected warning <%s> but got error <%s>", pattern, err); |
7151
|
105 end_try_catch |
5589
|
106 |
|
107 else |
8506
|
108 ## Perform the error test. |
5589
|
109 try |
6494
|
110 evalin ("caller", sprintf ("%s;", code)); |
|
111 msg = sprintf ("expected error <%s> but got none", pattern); |
5589
|
112 catch |
6494
|
113 err = lasterr (); |
|
114 if (strcmp (err(1:7), "error:")) |
|
115 err([1:6, end]) = []; # transform "error: ...\n", to "..." |
5589
|
116 endif |
6494
|
117 if (! isempty (regexp (err, pattern, "once"))) |
|
118 return; |
|
119 endif |
|
120 msg = sprintf ("expected error <%s>\nbut got <%s>", pattern, err); |
7151
|
121 end_try_catch |
5589
|
122 endif |
|
123 |
8506
|
124 ## If we get here, then code didn't fail or error didn't match. |
6494
|
125 error (msg); |
8506
|
126 |
5589
|
127 endfunction |
|
128 |
|
129 %!fail ('[1,2]*[2,3]','nonconformant') |
|
130 %!fail ("fail('[1,2]*[2;3]','nonconformant')","expected error <nonconformant> but got none") |
|
131 %!fail ("fail('[1,2]*[2,3]','usage:')","expected error <usage:>\nbut got.*nonconformant") |
|
132 %!fail ("warning('test warning')",'warning','test warning'); |
|
133 |
|
134 %!# fail ("warning('next test')",'warning','next test'); ## only allowed one warning test?!? |
|
135 |
|
136 ## Comment out the following tests if you don't want to see what |
|
137 ## errors look like |
|
138 % !fail ('a*[2;3]', 'nonconformant') |
|
139 % !fail ('a*[2,3]', 'usage:') |
|
140 % !fail ("warning('warning failure')", 'warning', 'success') |