1 ## Copyright (C) 2005-2012 Paul Kienzle
3 ## This file is part of Octave.
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.
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.
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING. If not, see
17 ## <http://www.gnu.org/licenses/>.
19 ## Original version by Paul Kienzle distributed as free software in the
23 ## @deftypefn {Function File} {} fail (@var{code})
24 ## @deftypefnx {Function File} {} fail (@var{code}, @var{pattern})
25 ## @deftypefnx {Function File} {} fail (@var{code}, 'warning', @var{pattern})
27 ## Return true if @var{code} fails with an error message matching
28 ## @var{pattern}, otherwise produce an error. Note that @var{code}
29 ## is a string and if @var{code} runs successfully, the error produced is:
32 ## expected error but got none
35 ## If the code fails with a different error, the message produced is:
40 ## but got <text of actual error>
44 ## The angle brackets are not part of the output.
46 ## Called with three arguments, the behavior is similar to
47 ## @code{fail(@var{code}, @var{pattern})}, but produces an error if no
48 ## warning is given during code execution or if the code fails.
52 ## Author: Paul Kienzle <pkienzle@users.sf.net>
54 function ret = fail (code, pattern, warning_pattern)
56 if (nargin < 1 || nargin > 3)
61 test_warning = (nargin > 1 && strcmp (pattern, "warning"));
63 pattern = warning_pattern;
64 elseif (nargin == 1 || (nargin == 2 && test_warning))
68 ## match any nonempty message
69 if (isempty (pattern))
73 ## allow assert(fail())
79 ## Perform the warning test.
80 ## Clear old warnings.
82 ## Make sure warnings are turned on.
83 state = warning ("query", "quiet");
84 warning ("on", "quiet");
86 ## printf("lastwarn before %s: %s\n",code,lastwarn);
87 evalin ("caller", sprintf ("%s;", code));
88 ## printf("lastwarn after %s: %s\n",code,lastwarn);
89 ## Retrieve new warnings.
91 warning (state.state, "quiet");
93 msg = sprintf ("expected warning <%s> but got none", pattern);
95 ## Transform "warning: ...\n" to "...".
97 if (! isempty (regexp (err, pattern, "once")))
100 msg = sprintf ("expected warning <%s>\nbut got <%s>", pattern, err);
103 warning (state.state, "quiet");
105 ## Transform "error: ...\n", to "...".
106 err([1:7, end]) = [];
107 msg = sprintf ("expected warning <%s> but got error <%s>", pattern, err);
111 ## Perform the error test.
113 evalin ("caller", sprintf ("%s;", code));
114 msg = sprintf ("expected error <%s> but got none", pattern);
117 if (strcmp (err(1:7), "error:"))
118 err([1:6, end]) = []; # transform "error: ...\n", to "..."
120 if (! isempty (regexp (err, pattern, "once")))
123 msg = sprintf ("expected error <%s>\nbut got <%s>", pattern, err);
127 ## If we get here, then code didn't fail or error didn't match.
133 %!fail ('[1,2]*[2,3]', 'nonconformant')
134 %!fail ("fail('[1,2]*[2;3]', 'nonconformant')", "expected error <nonconformant> but got none")
135 %!fail ("fail('[1,2]*[2,3]','usage:')", "expected error <usage:>\nbut got.*nonconformant")
136 %!fail ("warning('test warning')", 'warning','test warning');
138 ##% !fail ("warning('next test')",'warning','next test'); ## only allowed one warning test?!?
140 %% Test that fail() itself will generate an error
142 %!error <undefined> fail ('a*[2;3]', 'nonconformant')
143 %!error <expected error> fail ('a*[2,3]', 'usage:')
144 %!error <warning failure> fail ("warning('warning failure')", 'warning', 'success')