Mercurial > hg > octave-lyh
annotate scripts/testfun/fail.m @ 14138:72c96de7a403 stable
maint: update copyright notices for 2012
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Mon, 02 Jan 2012 14:25:41 -0500 |
parents | 756af8385e26 |
children | 7277fe922e99 7fe4ea72ba4d |
rev | line source |
---|---|
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
13066
diff
changeset
|
1 ## Copyright (C) 2005-2012 Paul Kienzle |
6494 | 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 -*- |
12632
2dbac27e0e40
doc: miscellaneous touch-ups to documentation strings
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
23 ## @deftypefn {Function File} {} fail (@var{code}) |
2dbac27e0e40
doc: miscellaneous touch-ups to documentation strings
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
24 ## @deftypefnx {Function File} {} fail (@var{code}, @var{pattern}) |
11563
3c6e8aaa9555
Grammarcheck m-files before 3.4 release.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
25 ## @deftypefnx {Function File} {} fail (@var{code}, 'warning', @var{pattern}) |
5589 | 26 ## |
27 ## Return true if @var{code} fails with an error message matching | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
28 ## @var{pattern}, otherwise produce an error. Note that @var{code} |
5589 | 29 ## is a string and if @var{code} runs successfully, the error produced is: |
30 ## | |
31 ## @example | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11563
diff
changeset
|
32 ## expected error but got none |
5589 | 33 ## @end example |
34 ## | |
35 ## If the code fails with a different error, the message produced is: | |
36 ## | |
37 ## @example | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
38 ## @group |
5589 | 39 ## expected <pattern> |
40 ## but got <text of actual error> | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
41 ## @end group |
5589 | 42 ## @end example |
43 ## | |
44 ## The angle brackets are not part of the output. | |
45 ## | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11563
diff
changeset
|
46 ## Called with three arguments, the behavior is similar to |
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11563
diff
changeset
|
47 ## @code{fail(@var{code}, @var{pattern})}, but produces an error if no |
5589 | 48 ## warning is given during code execution or if the code fails. |
12632
2dbac27e0e40
doc: miscellaneous touch-ups to documentation strings
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
49 ## @seealso{assert} |
5589 | 50 ## @end deftypefn |
51 | |
52 ## Author: Paul Kienzle <pkienzle@users.sf.net> | |
53 | |
6494 | 54 function ret = fail (code, pattern, warning_pattern) |
55 | |
56 if (nargin < 1 || nargin > 3) | |
6046 | 57 print_usage (); |
5589 | 58 endif |
59 | |
60 ## sort out arguments | |
6494 | 61 test_warning = (nargin > 1 && strcmp (pattern, "warning")); |
62 if (nargin == 3) | |
5589 | 63 pattern = warning_pattern; |
8507 | 64 elseif (nargin == 1 || (nargin == 2 && test_warning)) |
5589 | 65 pattern = ""; |
66 endif | |
6494 | 67 |
68 ## match any nonempty message | |
69 if (isempty (pattern)) | |
70 pattern = "."; | |
71 endif | |
5589 | 72 |
73 ## allow assert(fail()) | |
6494 | 74 if (nargout) |
75 ret = 1; | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11563
diff
changeset
|
76 endif |
5589 | 77 |
6494 | 78 if (test_warning) |
8506 | 79 ## Perform the warning test. |
80 ## Clear old warnings. | |
81 lastwarn (); | |
82 ## Make sure warnings are turned on. | |
83 state = warning ("query", "quiet"); | |
6494 | 84 warning ("on", "quiet"); |
5589 | 85 try |
86 ## printf("lastwarn before %s: %s\n",code,lastwarn); | |
6494 | 87 evalin ("caller", sprintf ("%s;", code)); |
5589 | 88 ## printf("lastwarn after %s: %s\n",code,lastwarn); |
8506 | 89 ## Retrieve new warnings. |
90 err = lastwarn (); | |
6494 | 91 warning (state.state, "quiet"); |
92 if (isempty (err)) | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11563
diff
changeset
|
93 msg = sprintf ("expected warning <%s> but got none", pattern); |
5589 | 94 else |
10549 | 95 ## Transform "warning: ...\n" to "...". |
8506 | 96 err([1:9, end]) = []; |
6494 | 97 if (! isempty (regexp (err, pattern, "once"))) |
10549 | 98 return; |
99 endif | |
6494 | 100 msg = sprintf ("expected warning <%s>\nbut got <%s>", pattern, err); |
5589 | 101 endif |
102 catch | |
6494 | 103 warning (state.state, "quiet"); |
5589 | 104 err = lasterr; |
8506 | 105 ## Transform "error: ...\n", to "...". |
106 err([1:7, end]) = []; | |
6494 | 107 msg = sprintf ("expected warning <%s> but got error <%s>", pattern, err); |
7151 | 108 end_try_catch |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11563
diff
changeset
|
109 |
5589 | 110 else |
8506 | 111 ## Perform the error test. |
5589 | 112 try |
6494 | 113 evalin ("caller", sprintf ("%s;", code)); |
114 msg = sprintf ("expected error <%s> but got none", pattern); | |
5589 | 115 catch |
6494 | 116 err = lasterr (); |
117 if (strcmp (err(1:7), "error:")) | |
118 err([1:6, end]) = []; # transform "error: ...\n", to "..." | |
5589 | 119 endif |
6494 | 120 if (! isempty (regexp (err, pattern, "once"))) |
10549 | 121 return; |
6494 | 122 endif |
123 msg = sprintf ("expected error <%s>\nbut got <%s>", pattern, err); | |
7151 | 124 end_try_catch |
5589 | 125 endif |
126 | |
8506 | 127 ## If we get here, then code didn't fail or error didn't match. |
6494 | 128 error (msg); |
8506 | 129 |
5589 | 130 endfunction |
131 | |
13066
756af8385e26
codesprint: Tests for fail.m
Rik <octave@nomad.inbox5.com>
parents:
12632
diff
changeset
|
132 |
756af8385e26
codesprint: Tests for fail.m
Rik <octave@nomad.inbox5.com>
parents:
12632
diff
changeset
|
133 %!fail ('[1,2]*[2,3]', 'nonconformant') |
756af8385e26
codesprint: Tests for fail.m
Rik <octave@nomad.inbox5.com>
parents:
12632
diff
changeset
|
134 %!fail ("fail('[1,2]*[2;3]', 'nonconformant')", "expected error <nonconformant> but got none") |
756af8385e26
codesprint: Tests for fail.m
Rik <octave@nomad.inbox5.com>
parents:
12632
diff
changeset
|
135 %!fail ("fail('[1,2]*[2,3]','usage:')", "expected error <usage:>\nbut got.*nonconformant") |
756af8385e26
codesprint: Tests for fail.m
Rik <octave@nomad.inbox5.com>
parents:
12632
diff
changeset
|
136 %!fail ("warning('test warning')", 'warning','test warning'); |
5589 | 137 |
13066
756af8385e26
codesprint: Tests for fail.m
Rik <octave@nomad.inbox5.com>
parents:
12632
diff
changeset
|
138 ##% !fail ("warning('next test')",'warning','next test'); ## only allowed one warning test?!? |
5589 | 139 |
13066
756af8385e26
codesprint: Tests for fail.m
Rik <octave@nomad.inbox5.com>
parents:
12632
diff
changeset
|
140 %% Test that fail() itself will generate an error |
756af8385e26
codesprint: Tests for fail.m
Rik <octave@nomad.inbox5.com>
parents:
12632
diff
changeset
|
141 %!error fail ("1"); |
756af8385e26
codesprint: Tests for fail.m
Rik <octave@nomad.inbox5.com>
parents:
12632
diff
changeset
|
142 %!error <undefined> fail ('a*[2;3]', 'nonconformant') |
756af8385e26
codesprint: Tests for fail.m
Rik <octave@nomad.inbox5.com>
parents:
12632
diff
changeset
|
143 %!error <expected error> fail ('a*[2,3]', 'usage:') |
756af8385e26
codesprint: Tests for fail.m
Rik <octave@nomad.inbox5.com>
parents:
12632
diff
changeset
|
144 %!error <warning failure> fail ("warning('warning failure')", 'warning', 'success') |