6494
|
1 ## Copyright (C) 2005 Paul Kienzle |
|
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 |
|
51 ## PKG_ADD mark_as_command fail |
6494
|
52 |
|
53 function ret = fail (code, pattern, warning_pattern) |
|
54 |
|
55 if (nargin < 1 || nargin > 3) |
6046
|
56 print_usage (); |
5589
|
57 endif |
|
58 |
|
59 ## sort out arguments |
6494
|
60 test_warning = (nargin > 1 && strcmp (pattern, "warning")); |
|
61 if (nargin == 3) |
5589
|
62 pattern = warning_pattern; |
6494
|
63 elseif (nargin == 1 || (nargin==2 && test_warning)) |
5589
|
64 pattern = ""; |
|
65 endif |
6494
|
66 |
|
67 ## match any nonempty message |
|
68 if (isempty (pattern)) |
|
69 pattern = "."; |
|
70 endif |
5589
|
71 |
|
72 ## allow assert(fail()) |
6494
|
73 if (nargout) |
|
74 ret = 1; |
|
75 endif |
5589
|
76 |
6494
|
77 if (test_warning) |
5589
|
78 ## perform the warning test |
6494
|
79 lastwarn (); # clear old warnings |
|
80 state = warning ("query", "quiet"); # make sure warnings are turned on |
|
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); |
6494
|
86 err = lastwarn (); # retrieve new warnings |
|
87 warning (state.state, "quiet"); |
|
88 if (isempty (err)) |
|
89 msg = sprintf ("expected warning <%s> but got none", pattern); |
5589
|
90 else |
6494
|
91 err([1:9, end]) = []; # transform "warning: ...\n" to "..." |
|
92 if (! isempty (regexp (err, pattern, "once"))) |
|
93 return; |
|
94 endif |
|
95 msg = sprintf ("expected warning <%s>\nbut got <%s>", pattern, err); |
5589
|
96 endif |
|
97 catch |
6494
|
98 warning (state.state, "quiet"); |
5589
|
99 err = lasterr; |
6494
|
100 err([1:7, end]) = []; # transform "error: ...\n", to "..." |
|
101 msg = sprintf ("expected warning <%s> but got error <%s>", pattern, err); |
5589
|
102 end |
|
103 |
|
104 else |
|
105 ## perform the error test |
|
106 try |
6494
|
107 evalin ("caller", sprintf ("%s;", code)); |
|
108 msg = sprintf ("expected error <%s> but got none", pattern); |
5589
|
109 catch |
6494
|
110 err = lasterr (); |
|
111 if (strcmp (err(1:7), "error:")) |
|
112 err([1:6, end]) = []; # transform "error: ...\n", to "..." |
5589
|
113 endif |
6494
|
114 if (! isempty (regexp (err, pattern, "once"))) |
|
115 return; |
|
116 endif |
|
117 msg = sprintf ("expected error <%s>\nbut got <%s>", pattern, err); |
5589
|
118 end |
|
119 endif |
|
120 |
|
121 ## if we get here, then code didn't fail or error didn't match |
6494
|
122 error (msg); |
5589
|
123 endfunction |
|
124 |
|
125 %!fail ('[1,2]*[2,3]','nonconformant') |
|
126 %!fail ("fail('[1,2]*[2;3]','nonconformant')","expected error <nonconformant> but got none") |
|
127 %!fail ("fail('[1,2]*[2,3]','usage:')","expected error <usage:>\nbut got.*nonconformant") |
|
128 %!fail ("warning('test warning')",'warning','test warning'); |
|
129 |
|
130 %!# fail ("warning('next test')",'warning','next test'); ## only allowed one warning test?!? |
|
131 |
|
132 ## Comment out the following tests if you don't want to see what |
|
133 ## errors look like |
|
134 % !fail ('a*[2;3]', 'nonconformant') |
|
135 % !fail ('a*[2,3]', 'usage:') |
|
136 % !fail ("warning('warning failure')", 'warning', 'success') |