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