1## Copyright (C) 2005, 2006, 2007, 2009 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},@var{pattern})
24## @deftypefnx {Function File} {} fail (@var{code},'warning',@var{pattern})
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:
31## expected error but got none
34## If the code fails with a different error, the message produced is:
39## but got <text of actual error>
43## The angle brackets are not part of the output.
45## Called with three arguments, the behavior is similar to
46## @code{fail(@var{code}, @var{pattern})}, but produces an error if no
47## warning is given during code execution or if the code fails.
51## Author: Paul Kienzle <pkienzle@users.sf.net>
53function ret = fail (code, pattern, warning_pattern)
55 if (nargin < 1 || nargin > 3)
60 test_warning = (nargin > 1 && strcmp (pattern, "warning"));
62 pattern = warning_pattern;
63 elseif (nargin == 1 || (nargin == 2 && test_warning))
67 ## match any nonempty message
68 if (isempty (pattern))
72 ## allow assert(fail())
78 ## Perform the warning test.
79 ## Clear old warnings.
81 ## Make sure warnings are turned on.
82 state = warning ("query", "quiet");
83 warning ("on", "quiet");
85 ## printf("lastwarn before %s: %s\n",code,lastwarn);
86 evalin ("caller", sprintf ("%s;", code));
87 ## printf("lastwarn after %s: %s\n",code,lastwarn);
88 ## Retrieve new warnings.
90 warning (state.state, "quiet");
92 msg = sprintf ("expected warning <%s> but got none", pattern);
94 ## Transform "warning: ...\n" to "...".
96 if (! isempty (regexp (err, pattern, "once")))
99 msg = sprintf ("expected warning <%s>\nbut got <%s>", pattern, err);
102 warning (state.state, "quiet");
104 ## Transform "error: ...\n", to "...".
105 err([1:7, end]) = [];
106 msg = sprintf ("expected warning <%s> but got error <%s>", pattern, err);
110 ## Perform the error test.
112 evalin ("caller", sprintf ("%s;", code));
113 msg = sprintf ("expected error <%s> but got none", pattern);
116 if (strcmp (err(1:7), "error:"))
117 err([1:6, end]) = []; # transform "error: ...\n", to "..."
119 if (! isempty (regexp (err, pattern, "once")))
122 msg = sprintf ("expected error <%s>\nbut got <%s>", pattern, err);
126 ## If we get here, then code didn't fail or error didn't match.
131%!fail ('[1,2]*[2,3]','nonconformant')
132%!fail ("fail('[1,2]*[2;3]','nonconformant')","expected error <nonconformant> but got none")
133%!fail ("fail('[1,2]*[2,3]','usage:')","expected error <usage:>\nbut got.*nonconformant")
134%!fail ("warning('test warning')",'warning','test warning');
136%!# fail ("warning('next test')",'warning','next test'); ## only allowed one warning test?!?
138## Comment out the following tests if you don't want to see what
140% !fail ('a*[2;3]', 'nonconformant')
141% !fail ('a*[2,3]', 'usage:')
142% !fail ("warning('warning failure')", 'warning', 'success')