Mercurial > hg > octave-nkf
annotate scripts/testfun/fail.m @ 20830:b65888ec820e draft default tip gccjit
dmalcom gcc jit import
author | Stefan Mahr <dac922@gmx.de> |
---|---|
date | Fri, 27 Feb 2015 16:59:36 +0100 |
parents | 2645f9ef8c88 |
children |
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 ## | |
20372
2645f9ef8c88
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20350
diff
changeset
|
56 ## When called with the @qcode{"warning"} option @code{fail} will produce an |
2645f9ef8c88
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20350
diff
changeset
|
57 ## error if executing the code produces no warning. |
19207
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 |
20350
64a2d4c87ecb
Don't save "warning: " prefix in lastwarn() message.
Rik <rik@octave.org>
parents:
20144
diff
changeset
|
70 test_warning = false; |
64a2d4c87ecb
Don't save "warning: " prefix in lastwarn() message.
Rik <rik@octave.org>
parents:
20144
diff
changeset
|
71 if (nargin == 1) |
5589 | 72 pattern = ""; |
20350
64a2d4c87ecb
Don't save "warning: " prefix in lastwarn() message.
Rik <rik@octave.org>
parents:
20144
diff
changeset
|
73 elseif (nargin == 2 && ! strcmp (pattern, "warning")) |
64a2d4c87ecb
Don't save "warning: " prefix in lastwarn() message.
Rik <rik@octave.org>
parents:
20144
diff
changeset
|
74 ## Normal error test |
64a2d4c87ecb
Don't save "warning: " prefix in lastwarn() message.
Rik <rik@octave.org>
parents:
20144
diff
changeset
|
75 elseif (nargin >= 2 && strcmp (pattern, "warning")) |
64a2d4c87ecb
Don't save "warning: " prefix in lastwarn() message.
Rik <rik@octave.org>
parents:
20144
diff
changeset
|
76 test_warning = true; |
64a2d4c87ecb
Don't save "warning: " prefix in lastwarn() message.
Rik <rik@octave.org>
parents:
20144
diff
changeset
|
77 if (nargin == 2) |
64a2d4c87ecb
Don't save "warning: " prefix in lastwarn() message.
Rik <rik@octave.org>
parents:
20144
diff
changeset
|
78 pattern = ""; |
64a2d4c87ecb
Don't save "warning: " prefix in lastwarn() message.
Rik <rik@octave.org>
parents:
20144
diff
changeset
|
79 else |
64a2d4c87ecb
Don't save "warning: " prefix in lastwarn() message.
Rik <rik@octave.org>
parents:
20144
diff
changeset
|
80 pattern = warning_pattern; |
64a2d4c87ecb
Don't save "warning: " prefix in lastwarn() message.
Rik <rik@octave.org>
parents:
20144
diff
changeset
|
81 endif |
64a2d4c87ecb
Don't save "warning: " prefix in lastwarn() message.
Rik <rik@octave.org>
parents:
20144
diff
changeset
|
82 else |
64a2d4c87ecb
Don't save "warning: " prefix in lastwarn() message.
Rik <rik@octave.org>
parents:
20144
diff
changeset
|
83 print_usage (); |
5589 | 84 endif |
6494 | 85 |
19207
ffa20d287355
fail.m: Overhaul function and return logical true rather than double (1).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
86 ## Match any nonempty message |
6494 | 87 if (isempty (pattern)) |
88 pattern = "."; | |
89 endif | |
5589 | 90 |
19207
ffa20d287355
fail.m: Overhaul function and return logical true rather than double (1).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
91 ## Allow assert (fail ()) |
6494 | 92 if (nargout) |
19207
ffa20d287355
fail.m: Overhaul function and return logical true rather than double (1).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
93 retval = true; |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11563
diff
changeset
|
94 endif |
5589 | 95 |
6494 | 96 if (test_warning) |
8506 | 97 ## Perform the warning test. |
98 ## 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
|
99 lastwarn (""); |
8506 | 100 ## Make sure warnings are turned on. |
20350
64a2d4c87ecb
Don't save "warning: " prefix in lastwarn() message.
Rik <rik@octave.org>
parents:
20144
diff
changeset
|
101 wstate = warning ("query", "quiet"); |
6494 | 102 warning ("on", "quiet"); |
5589 | 103 try |
19207
ffa20d287355
fail.m: Overhaul function and return logical true rather than double (1).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
104 evalin ("caller", [code ";"]); |
8506 | 105 ## 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
|
106 warn = lastwarn (); |
20350
64a2d4c87ecb
Don't save "warning: " prefix in lastwarn() message.
Rik <rik@octave.org>
parents:
20144
diff
changeset
|
107 warning (wstate.state, "quiet"); |
19207
ffa20d287355
fail.m: Overhaul function and return logical true rather than double (1).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
108 if (isempty (warn)) |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11563
diff
changeset
|
109 msg = sprintf ("expected warning <%s> but got none", pattern); |
5589 | 110 else |
19207
ffa20d287355
fail.m: Overhaul function and return logical true rather than double (1).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
111 if (! isempty (regexp (warn, pattern, "once"))) |
10549 | 112 return; |
113 endif | |
19207
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 <%s>", pattern, warn); |
5589 | 115 endif |
116 catch | |
20350
64a2d4c87ecb
Don't save "warning: " prefix in lastwarn() message.
Rik <rik@octave.org>
parents:
20144
diff
changeset
|
117 warning (wstate.state, "quiet"); |
64a2d4c87ecb
Don't save "warning: " prefix in lastwarn() message.
Rik <rik@octave.org>
parents:
20144
diff
changeset
|
118 err = lasterr (); |
19207
ffa20d287355
fail.m: Overhaul function and return logical true rather than double (1).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
119 msg = sprintf ("expected warning <%s>\nbut got error <%s>", pattern, err); |
7151 | 120 end_try_catch |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11563
diff
changeset
|
121 |
5589 | 122 else |
8506 | 123 ## Perform the error test. |
5589 | 124 try |
19207
ffa20d287355
fail.m: Overhaul function and return logical true rather than double (1).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
125 evalin ("caller", [code ";"]); |
6494 | 126 msg = sprintf ("expected error <%s> but got none", pattern); |
5589 | 127 catch |
6494 | 128 err = lasterr (); |
129 if (! isempty (regexp (err, pattern, "once"))) | |
10549 | 130 return; |
6494 | 131 endif |
132 msg = sprintf ("expected error <%s>\nbut got <%s>", pattern, err); | |
7151 | 133 end_try_catch |
5589 | 134 endif |
135 | |
8506 | 136 ## If we get here, then code didn't fail or error didn't match. |
6494 | 137 error (msg); |
8506 | 138 |
5589 | 139 endfunction |
140 | |
13066
756af8385e26
codesprint: Tests for fail.m
Rik <octave@nomad.inbox5.com>
parents:
12632
diff
changeset
|
141 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14359
diff
changeset
|
142 %!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
|
143 %!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
|
144 %!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
|
145 %!fail ("warning ('test warning')", "warning", "test warning"); |
5589 | 146 |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
147 #%!fail ("warning ('next test')",'warning','next test'); # only allowed one warning test?!? |
5589 | 148 |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
149 ## 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
|
150 %!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
|
151 %!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
|
152 %!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
|
153 %!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
|
154 |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
155 ## 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
|
156 %!error fail () |
ffa20d287355
fail.m: Overhaul function and return logical true rather than double (1).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
157 %!error fail (1,2,3,4) |
20350
64a2d4c87ecb
Don't save "warning: " prefix in lastwarn() message.
Rik <rik@octave.org>
parents:
20144
diff
changeset
|
158 %!error fail (1, "nowarning", "foo") |
19207
ffa20d287355
fail.m: Overhaul function and return logical true rather than double (1).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
159 |