]> Creatis software - CreaPhase.git/blob - octave_packages/m/testfun/fail.m
update packages
[CreaPhase.git] / octave_packages / m / testfun / fail.m
1 ## Copyright (C) 2005-2012 Paul Kienzle
2 ##
3 ## This file is part of Octave.
4 ##
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.
14 ##
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/>.
18 ##
19 ## Original version by Paul Kienzle distributed as free software in the
20 ## public domain.
21
22 ## -*- texinfo -*-
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})
26 ##
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:
30 ##
31 ## @example
32 ##           expected error but got none
33 ## @end example
34 ##
35 ## If the code fails with a different error, the message produced is:
36 ##
37 ## @example
38 ## @group
39 ##           expected <pattern>
40 ##           but got <text of actual error>
41 ## @end group
42 ## @end example
43 ##
44 ## The angle brackets are not part of the output.
45 ##
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.
49 ## @seealso{assert}
50 ## @end deftypefn
51
52 ## Author: Paul Kienzle <pkienzle@users.sf.net>
53
54 function ret = fail (code, pattern, warning_pattern)
55
56   if (nargin < 1 || nargin > 3)
57     print_usage ();
58   endif
59
60   ## sort out arguments
61   test_warning = (nargin > 1 && strcmp (pattern, "warning"));
62   if (nargin == 3)
63     pattern = warning_pattern;
64   elseif (nargin == 1 || (nargin == 2 && test_warning))
65     pattern = "";
66   endif
67
68   ## match any nonempty message
69   if (isempty (pattern))
70     pattern = ".";
71   endif
72
73   ## allow assert(fail())
74   if (nargout)
75     ret = 1;
76   endif
77
78   if (test_warning)
79     ## Perform the warning test.
80     ## Clear old warnings.
81     lastwarn ();
82     ## Make sure warnings are turned on.
83     state = warning ("query", "quiet");
84     warning ("on", "quiet");
85     try
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.
90       err = lastwarn ();
91       warning (state.state, "quiet");
92       if (isempty (err))
93         msg = sprintf ("expected warning <%s> but got none", pattern);
94       else
95         ## Transform "warning: ...\n" to "...".
96         err([1:9, end]) = [];
97         if (! isempty (regexp (err, pattern, "once")))
98           return;
99         endif
100         msg = sprintf ("expected warning <%s>\nbut got <%s>", pattern, err);
101       endif
102     catch
103       warning (state.state, "quiet");
104       err = lasterr;
105       ## Transform "error: ...\n", to "...".
106       err([1:7, end]) = [];
107       msg = sprintf ("expected warning <%s> but got error <%s>", pattern, err);
108     end_try_catch
109
110   else
111     ## Perform the error test.
112     try
113       evalin ("caller", sprintf ("%s;", code));
114       msg = sprintf ("expected error <%s> but got none", pattern);
115     catch
116       err = lasterr ();
117       if (strcmp (err(1:7), "error:"))
118          err([1:6, end]) = []; # transform "error: ...\n", to "..."
119       endif
120       if (! isempty (regexp (err, pattern, "once")))
121         return;
122       endif
123       msg = sprintf ("expected error <%s>\nbut got <%s>", pattern, err);
124     end_try_catch
125   endif
126
127   ## If we get here, then code didn't fail or error didn't match.
128   error (msg);
129
130 endfunction
131
132
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');
137
138 ##% !fail ("warning('next test')",'warning','next test');  ## only allowed one warning test?!?
139
140 %% Test that fail() itself will generate an error
141 %!error fail ("1");
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')