Mercurial > hg > octave-nkf
annotate scripts/testfun/fail.m @ 20144:2d5cbb6ac845
fail.m: Use internal variable name in BIST test (bug #44525).
* fail.m: Rename variable 'a' to '__a__' in case there is an a.m m-file.
author | Rik <rik@octave.org> |
---|---|
date | Thu, 12 Mar 2015 15:00:32 -0700 |
parents | 9fc020886ae9 |
children | 64a2d4c87ecb |
rev | line source |
---|---|
19898
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
19207
diff
changeset
|
1 ## Copyright (C) 2005-2015 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}) |
19207
ffa20d287355
fail.m: Overhaul function and return logical true rather than double (1).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
25 ## @deftypefnx {Function File} {} fail (@var{code}, "warning") |
14359
7277fe922e99
doc: Use Octave preference for double quote in docstrings in scripts/
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
26 ## @deftypefnx {Function File} {} fail (@var{code}, "warning", @var{pattern}) |
5589 | 27 ## |
28 ## Return true if @var{code} fails with an error message matching | |
19207
ffa20d287355
fail.m: Overhaul function and return logical true rather than double (1).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
29 ## @var{pattern}, otherwise produce an error. |
ffa20d287355
fail.m: Overhaul function and return logical true rather than double (1).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
30 ## |
ffa20d287355
fail.m: Overhaul function and return logical true rather than double (1).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
31 ## @var{code} must be in the form of a string that is passed to the Octave |
ffa20d287355
fail.m: Overhaul function and return logical true rather than double (1).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
32 ## interpreter via the @code{evalin} function, i.e., a (quoted) string constant |
ffa20d287355
fail.m: Overhaul function and return logical true rather than double (1).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
33 ## or a string variable. |
ffa20d287355
fail.m: Overhaul function and return logical true rather than double (1).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
34 ## |
ffa20d287355
fail.m: Overhaul function and return logical true rather than double (1).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
35 ## Note that if @var{code} runs successfully, rather than failing, the error |
ffa20d287355
fail.m: Overhaul function and return logical true rather than double (1).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
36 ## printed is: |
5589 | 37 ## |
38 ## @example | |
16190
7fe4ea72ba4d
doc: Update 'test' and "fail" documentation
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
39 ## expected error <.> but got none |
5589 | 40 ## @end example |
41 ## | |
19207
ffa20d287355
fail.m: Overhaul function and return logical true rather than double (1).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
42 ## If called with two arguments, the return value will be true only if |
ffa20d287355
fail.m: Overhaul function and return logical true rather than double (1).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
43 ## @var{code} fails with an error message containing @var{pattern} |
ffa20d287355
fail.m: Overhaul function and return logical true rather than double (1).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
44 ## (case sensitive). If the code fails with a different error than the one |
ffa20d287355
fail.m: Overhaul function and return logical true rather than double (1).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
45 ## specified in @var{pattern} then the message produced is: |
5589 | 46 ## |
47 ## @example | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
48 ## @group |
19207
ffa20d287355
fail.m: Overhaul function and return logical true rather than double (1).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
49 ## expected <@var{pattern}> |
5589 | 50 ## but got <text of actual error> |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
51 ## @end group |
5589 | 52 ## @end example |
53 ## | |
54 ## The angle brackets are not part of the output. | |
55 ## | |
19207
ffa20d287355
fail.m: Overhaul function and return logical true rather than double (1).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
56 ## When called with the @qcode{"warning"} option @code{fail} will produce |
ffa20d287355
fail.m: Overhaul function and return logical true rather than double (1).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
57 ## an error if executing the code produces no warning. |
ffa20d287355
fail.m: Overhaul function and return logical true rather than double (1).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
58 ## @seealso{assert, error} |
5589 | 59 ## @end deftypefn |
60 | |
61 ## Author: Paul Kienzle <pkienzle@users.sf.net> | |
62 | |
19207
ffa20d287355
fail.m: Overhaul function and return logical true rather than double (1).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
63 function retval = fail (code, pattern, warning_pattern) |
6494 | 64 |
65 if (nargin < 1 || nargin > 3) | |
6046 | 66 print_usage (); |
5589 | 67 endif |
68 | |
19207
ffa20d287355
fail.m: Overhaul function and return logical true rather than double (1).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
69 ## Parse input arguments |
6494 | 70 test_warning = (nargin > 1 && strcmp (pattern, "warning")); |
71 if (nargin == 3) | |
5589 | 72 pattern = warning_pattern; |
8507 | 73 elseif (nargin == 1 || (nargin == 2 && test_warning)) |
5589 | 74 pattern = ""; |
75 endif | |
6494 | 76 |
19207
ffa20d287355
fail.m: Overhaul function and return logical true rather than double (1).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
77 ## Match any nonempty message |
6494 | 78 if (isempty (pattern)) |
79 pattern = "."; | |
80 endif | |
5589 | 81 |
19207
ffa20d287355
fail.m: Overhaul function and return logical true rather than double (1).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
82 ## Allow assert (fail ()) |
6494 | 83 if (nargout) |
19207
ffa20d287355
fail.m: Overhaul function and return logical true rather than double (1).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
84 retval = true; |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11563
diff
changeset
|
85 endif |
5589 | 86 |
6494 | 87 if (test_warning) |
8506 | 88 ## Perform the warning test. |
89 ## Clear old warnings. | |
19207
ffa20d287355
fail.m: Overhaul function and return logical true rather than double (1).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
90 lastwarn (""); |
8506 | 91 ## Make sure warnings are turned on. |
92 state = warning ("query", "quiet"); | |
6494 | 93 warning ("on", "quiet"); |
5589 | 94 try |
19207
ffa20d287355
fail.m: Overhaul function and return logical true rather than double (1).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
95 evalin ("caller", [code ";"]); |
8506 | 96 ## Retrieve new warnings. |
19207
ffa20d287355
fail.m: Overhaul function and return logical true rather than double (1).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
97 warn = lastwarn (); |
6494 | 98 warning (state.state, "quiet"); |
19207
ffa20d287355
fail.m: Overhaul function and return logical true rather than double (1).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
99 if (isempty (warn)) |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11563
diff
changeset
|
100 msg = sprintf ("expected warning <%s> but got none", pattern); |
5589 | 101 else |
10549 | 102 ## Transform "warning: ...\n" to "...". |
19207
ffa20d287355
fail.m: Overhaul function and return logical true rather than double (1).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
103 warn([1:9, end]) = []; |
ffa20d287355
fail.m: Overhaul function and return logical true rather than double (1).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
104 if (! isempty (regexp (warn, pattern, "once"))) |
10549 | 105 return; |
106 endif | |
19207
ffa20d287355
fail.m: Overhaul function and return logical true rather than double (1).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
107 msg = sprintf ("expected warning <%s>\nbut got <%s>", pattern, warn); |
5589 | 108 endif |
109 catch | |
6494 | 110 warning (state.state, "quiet"); |
5589 | 111 err = lasterr; |
8506 | 112 ## Transform "error: ...\n", to "...". |
19207
ffa20d287355
fail.m: Overhaul function and return logical true rather than double (1).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
113 err([1:6, end]) = []; |
ffa20d287355
fail.m: Overhaul function and return logical true rather than double (1).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
114 msg = sprintf ("expected warning <%s>\nbut got error <%s>", pattern, err); |
7151 | 115 end_try_catch |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11563
diff
changeset
|
116 |
5589 | 117 else |
8506 | 118 ## Perform the error test. |
5589 | 119 try |
19207
ffa20d287355
fail.m: Overhaul function and return logical true rather than double (1).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
120 evalin ("caller", [code ";"]); |
6494 | 121 msg = sprintf ("expected error <%s> but got none", pattern); |
5589 | 122 catch |
6494 | 123 err = lasterr (); |
124 if (strcmp (err(1:7), "error:")) | |
125 err([1:6, end]) = []; # transform "error: ...\n", to "..." | |
5589 | 126 endif |
6494 | 127 if (! isempty (regexp (err, pattern, "once"))) |
10549 | 128 return; |
6494 | 129 endif |
130 msg = sprintf ("expected error <%s>\nbut got <%s>", pattern, err); | |
7151 | 131 end_try_catch |
5589 | 132 endif |
133 | |
8506 | 134 ## If we get here, then code didn't fail or error didn't match. |
6494 | 135 error (msg); |
8506 | 136 |
5589 | 137 endfunction |
138 | |
13066
756af8385e26
codesprint: Tests for fail.m
Rik <octave@nomad.inbox5.com>
parents:
12632
diff
changeset
|
139 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14359
diff
changeset
|
140 %!fail ("[1,2]*[2,3]", "nonconformant") |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
141 %!fail ("fail ('[1,2]*[2;3]', 'nonconformant')", "expected error <nonconformant> but got none") |
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
142 %!fail ("fail ('[1,2]*[2,3]', 'usage:')", "expected error <usage:>\nbut got.*nonconformant") |
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
143 %!fail ("warning ('test warning')", "warning", "test warning"); |
5589 | 144 |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
145 #%!fail ("warning ('next test')",'warning','next test'); # only allowed one warning test?!? |
5589 | 146 |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
147 ## Test that fail() itself will generate an error |
19207
ffa20d287355
fail.m: Overhaul function and return logical true rather than double (1).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
148 %!error <expected error> fail ("1") |
20144
2d5cbb6ac845
fail.m: Use internal variable name in BIST test (bug #44525).
Rik <rik@octave.org>
parents:
20038
diff
changeset
|
149 %!error <'__a__' undefined> fail ("__a__*[2;3]", "nonconformant") |
2d5cbb6ac845
fail.m: Use internal variable name in BIST test (bug #44525).
Rik <rik@octave.org>
parents:
20038
diff
changeset
|
150 %!error <expected error .usage:> fail ("__a__*[2,3]", "usage:") |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
151 %!error <warning failure> fail ("warning ('warning failure')", "warning", "success") |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14359
diff
changeset
|
152 |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
153 ## Test input validation |
19207
ffa20d287355
fail.m: Overhaul function and return logical true rather than double (1).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
154 %!error fail () |
ffa20d287355
fail.m: Overhaul function and return logical true rather than double (1).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
155 %!error fail (1,2,3,4) |
ffa20d287355
fail.m: Overhaul function and return logical true rather than double (1).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
156 |