Mercurial > hg > octave-lyh
annotate scripts/testfun/fail.m @ 17384:f0e3d3de5c22
build: Enable subdir-objects Automake option project-wide
* configure.ac (AM_INIT_AUTOMAKE): Add subdir-objects option to enable it
project-wide to simplify and anticipate future Automake defaults.
* libgui/Makefile.am, libinterp/Makefile.am, liboctave/Makefile.am,
liboctave/cruft/Makefile.am, scripts/Makefile.am (AUTOMAKE_OPTIONS): Delete.
author | Mike Miller <mtmiller@ieee.org> |
---|---|
date | Thu, 05 Sep 2013 01:49:29 -0400 |
parents | 70c47da7e02b |
children |
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}) |
14359
7277fe922e99
doc: Use Octave preference for double quote in docstrings in scripts/
Rik <octave@nomad.inbox5.com>
parents:
14138
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 | |
16190
7fe4ea72ba4d
doc: Update 'test' and "fail" documentation
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
32 ## expected error <.> but got none |
5589 | 33 ## @end example |
34 ## | |
16190
7fe4ea72ba4d
doc: Update 'test' and "fail" documentation
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
35 ## |
7fe4ea72ba4d
doc: Update 'test' and "fail" documentation
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
36 ## Code must be in the form of a string that may be passed by |
7fe4ea72ba4d
doc: Update 'test' and "fail" documentation
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
37 ## @code{fail} to the Octave interpreter via the @code{evalin} function, |
7fe4ea72ba4d
doc: Update 'test' and "fail" documentation
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
38 ## that is, a (quoted) string constant or a string variable. |
7fe4ea72ba4d
doc: Update 'test' and "fail" documentation
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
39 ## |
7fe4ea72ba4d
doc: Update 'test' and "fail" documentation
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
40 ## If called with two arguments, the behavior is similar to |
7fe4ea72ba4d
doc: Update 'test' and "fail" documentation
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
41 ## @code{fail (@var{code})}, except the return value will only be true if |
7fe4ea72ba4d
doc: Update 'test' and "fail" documentation
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
42 ## code fails with an error message containing pattern (case sensitive). |
7fe4ea72ba4d
doc: Update 'test' and "fail" documentation
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
43 ## If the code fails with a different error to that given in pattern, |
7fe4ea72ba4d
doc: Update 'test' and "fail" documentation
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
44 ## the message produced is: |
5589 | 45 ## |
46 ## @example | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
47 ## @group |
5589 | 48 ## expected <pattern> |
49 ## but got <text of actual error> | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
50 ## @end group |
5589 | 51 ## @end example |
52 ## | |
53 ## The angle brackets are not part of the output. | |
54 ## | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11563
diff
changeset
|
55 ## Called with three arguments, the behavior is similar to |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
56 ## @code{fail (@var{code}, @var{pattern})}, but produces an error if no |
5589 | 57 ## 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
|
58 ## @seealso{assert} |
5589 | 59 ## @end deftypefn |
60 | |
61 ## Author: Paul Kienzle <pkienzle@users.sf.net> | |
62 | |
6494 | 63 function ret = fail (code, pattern, warning_pattern) |
64 | |
65 if (nargin < 1 || nargin > 3) | |
6046 | 66 print_usage (); |
5589 | 67 endif |
68 | |
69 ## sort out 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 |
77 ## match any nonempty message | |
78 if (isempty (pattern)) | |
79 pattern = "."; | |
80 endif | |
5589 | 81 |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
82 ## allow assert (fail ()) |
6494 | 83 if (nargout) |
84 ret = 1; | |
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. | |
90 lastwarn (); | |
91 ## Make sure warnings are turned on. | |
92 state = warning ("query", "quiet"); | |
6494 | 93 warning ("on", "quiet"); |
5589 | 94 try |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
95 ## printf ("lastwarn before %s: %s\n",code,lastwarn); |
6494 | 96 evalin ("caller", sprintf ("%s;", code)); |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
97 ## printf ("lastwarn after %s: %s\n",code,lastwarn); |
8506 | 98 ## Retrieve new warnings. |
99 err = lastwarn (); | |
6494 | 100 warning (state.state, "quiet"); |
101 if (isempty (err)) | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11563
diff
changeset
|
102 msg = sprintf ("expected warning <%s> but got none", pattern); |
5589 | 103 else |
10549 | 104 ## Transform "warning: ...\n" to "...". |
8506 | 105 err([1:9, end]) = []; |
6494 | 106 if (! isempty (regexp (err, pattern, "once"))) |
10549 | 107 return; |
108 endif | |
6494 | 109 msg = sprintf ("expected warning <%s>\nbut got <%s>", pattern, err); |
5589 | 110 endif |
111 catch | |
6494 | 112 warning (state.state, "quiet"); |
5589 | 113 err = lasterr; |
8506 | 114 ## Transform "error: ...\n", to "...". |
115 err([1:7, end]) = []; | |
6494 | 116 msg = sprintf ("expected warning <%s> but got error <%s>", pattern, err); |
7151 | 117 end_try_catch |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11563
diff
changeset
|
118 |
5589 | 119 else |
8506 | 120 ## Perform the error test. |
5589 | 121 try |
6494 | 122 evalin ("caller", sprintf ("%s;", code)); |
123 msg = sprintf ("expected error <%s> but got none", pattern); | |
5589 | 124 catch |
6494 | 125 err = lasterr (); |
126 if (strcmp (err(1:7), "error:")) | |
127 err([1:6, end]) = []; # transform "error: ...\n", to "..." | |
5589 | 128 endif |
6494 | 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 |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
147 ##% !fail ("warning ('next test')",'warning','next test'); ## only allowed one warning test?!? |
5589 | 148 |
13066
756af8385e26
codesprint: Tests for fail.m
Rik <octave@nomad.inbox5.com>
parents:
12632
diff
changeset
|
149 %% Test that fail() itself will generate an error |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14359
diff
changeset
|
150 %!error fail ("1") |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14359
diff
changeset
|
151 %!error <undefined> fail ("a*[2;3]", "nonconformant") |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14359
diff
changeset
|
152 %!error <expected error> 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 |