Mercurial > hg > octave-nkf
annotate scripts/testfun/test.m @ 19599:5cd83b466a3e
test.m: Avoid num-to-str warning.
* test.m (__extract_test_code): Initialize body of code with "" rather than [].
author | Rik <rik@octave.org> |
---|---|
date | Mon, 15 Dec 2014 22:16:25 -0800 |
parents | 64f034147e9a |
children | 3f29b433bd5d |
rev | line source |
---|---|
17744
d63878346099
maint: Update copyright notices for release.
John W. Eaton <jwe@octave.org>
parents:
17336
diff
changeset
|
1 ## Copyright (C) 2005-2013 Paul Kienzle |
5589 | 2 ## |
7016 | 3 ## This file is part of Octave. |
5589 | 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. | |
5589 | 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/>. | |
5589 | 18 |
19 ## -*- texinfo -*- | |
11412
a8a9f062d0ef
test.m: Use 'Command' for deftype in docstring.
Rik <octave@nomad.inbox5.com>
parents:
11032
diff
changeset
|
20 ## @deftypefn {Command} {} test @var{name} |
a8a9f062d0ef
test.m: Use 'Command' for deftype in docstring.
Rik <octave@nomad.inbox5.com>
parents:
11032
diff
changeset
|
21 ## @deftypefnx {Command} {} test @var{name} quiet|normal|verbose |
14359
7277fe922e99
doc: Use Octave preference for double quote in docstrings in scripts/
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
22 ## @deftypefnx {Function File} {} test ("@var{name}", "quiet|normal|verbose", @var{fid}) |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
23 ## @deftypefnx {Function File} {} test ("@var{name}", "quiet|normal|verbose", @var{fname}) |
5589 | 24 ## @deftypefnx {Function File} {@var{success} =} test (@dots{}) |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
25 ## @deftypefnx {Function File} {[@var{n}, @var{nmax}, @var{nxfail}, @var{nskip}] =} test (@dots{}) |
14359
7277fe922e99
doc: Use Octave preference for double quote in docstrings in scripts/
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
26 ## @deftypefnx {Function File} {[@var{code}, @var{idx}] =} test ("@var{name}", "grabdemo") |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
27 ## @deftypefnx {Function File} {} test ([], "explain", @var{fid}) |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
28 ## @deftypefnx {Function File} {} test ([], "explain", @var{fname}) |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
29 ## |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
30 ## Perform built-in self-tests from the first file in the loadpath matching |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
31 ## @var{name}. |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
32 ## |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
33 ## @code{test} can be called in either command or functional form. The exact |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
34 ## operation of test is determined by a combination of mode (interactive or |
19344
0f9c5a15c8fa
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
19329
diff
changeset
|
35 ## batch), reporting level (@qcode{"quiet"}, @qcode{"normal"}, |
0f9c5a15c8fa
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
19329
diff
changeset
|
36 ## @qcode{"verbose"}), and whether a logfile or summary output variable is |
0f9c5a15c8fa
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
19329
diff
changeset
|
37 ## used. |
5589 | 38 ## |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
39 ## The default mode when @code{test} is called from the command line is |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
40 ## interactive. In this mode, tests will be run until the first error is |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
41 ## encountered, or all tests complete successfully. In batch mode, all tests |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
42 ## are run regardless of any failures, and the results are collected for |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
43 ## reporting. Tests which require user interaction, i.e., demo blocks, |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
44 ## are never run in batch mode. |
5589 | 45 ## |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
46 ## Batch mode is enabled by either 1) specifying a logfile using the third |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
47 ## argument @var{fname} or @var{fid}, or 2) requesting an output argument |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
48 ## such as @var{success}, @var{n}, etc. |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
49 ## |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
50 ## The optional second argument determines the amount of output to generate and |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
51 ## which types of tests to run. The default value is @qcode{"normal"}. |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
52 ## Requesting an output argument will suppress printing the final summary |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
53 ## message and any intermediate warnings, unless verbose reporting is |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
54 ## enabled. |
5589 | 55 ## |
56 ## @table @asis | |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
17245
diff
changeset
|
57 ## @item @qcode{"quiet"} |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
58 ## Print a summary message when all tests pass, or print an error with the |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
59 ## results of the first bad test when a failure occurs. Don't run tests which |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
60 ## require user interaction. |
5589 | 61 ## |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
17245
diff
changeset
|
62 ## @item @qcode{"normal"} |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
63 ## Display warning messages about skipped tests or failing xtests during test |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
64 ## execution. |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
65 ## Print a summary message when all tests pass, or print an error with the |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
66 ## results of the first bad test when a failure occurs. Don't run tests which |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
67 ## require user interaction. |
5589 | 68 ## |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
17245
diff
changeset
|
69 ## @item @qcode{"verbose"} |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
70 ## Display tests before execution. Print all warning messages. In interactive |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
71 ## mode, run all tests including those which require user interaction. |
5589 | 72 ## @end table |
73 ## | |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
74 ## The optional third input argument specifies a logfile where results of the |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
75 ## tests should be written. The logfile may be a character string |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
76 ## (@var{fname}) or an open file descriptor ID (@var{fid}). To enable batch |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
77 ## processing, but still print the results to the screen, use @code{stdout} for |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
78 ## @var{fid}. |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
79 ## |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
80 ## When called with just a single output argument @var{success}, @code{test} |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
81 ## returns true if all of the tests were successful. If called with more |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
82 ## than one output argument then the number of successful tests (@var{n}), |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
83 ## the total number of tests in the file (@var{nmax}), the number of xtest |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
84 ## failures (@var{nxfail}), and the number of skipped tests (@var{nskip} are |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
85 ## returned. |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
86 ## |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
87 ## Example |
5589 | 88 ## |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
89 ## @example |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
90 ## @group |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
91 ## test sind |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
92 ## @result{} |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
93 ## PASSES 5 out of 5 tests |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
94 ## |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
95 ## [n, nmax] = test ("sind") |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
96 ## @result{} |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
97 ## n = 5 |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
98 ## nmax = 5 |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
99 ## @end group |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
100 ## @end example |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
101 ## |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
102 ## Additional Calling Syntaxes |
5589 | 103 ## |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
17245
diff
changeset
|
104 ## If the second argument is the string @qcode{"grabdemo"}, the contents of |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
105 ## any built-in demo blocks are extracted but not executed. The text for all |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
106 ## code blocks is concatenated and returned as @var{code} with @var{idx} being |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
107 ## a vector of positions of the ends of each demo block. For an easier way to |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
108 ## extract demo blocks from files, @xref{XREFexample,,example}. |
5589 | 109 ## |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
110 ## If the second argument is @qcode{"explain"} then @var{name} is ignored and |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
111 ## an explanation of the line markers used in @code{test} output reports is |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
112 ## written to the file specified by @var{fname} or @var{fid}. |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
113 ## |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
114 ## @seealso{assert, fail, demo, example, error} |
5589 | 115 ## @end deftypefn |
116 | |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
117 ## Programming Note: All variables for test() must use the internal prefix "__". |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
118 ## Shared variables are eval'ed into the current workspace and therefore might |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
119 ## collide with the names used in the test.m function itself. |
5589 | 120 |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
121 function [__n, __nmax, __nxfail, __nskip] = test (__name, __flag = "normal", __fid = []) |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
122 |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
123 ## Output from test is prefixed by a "key" to quickly understand the issue. |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
124 persistent __signal_fail = "!!!!! "; |
5589 | 125 persistent __signal_empty = "????? "; |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
126 persistent __signal_block = "***** "; |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
127 persistent __signal_file = ">>>>> "; |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
128 persistent __signal_skip = "----- "; |
6730 | 129 |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
130 if (nargin < 1 || nargin > 3) |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
131 print_usage (); |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
132 elseif (! isempty (__name) && ! ischar (__name)) |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
133 error ("test: NAME must be a string"); |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
134 elseif (! ischar (__flag)) |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
135 error ("test: second argument must be a string"); |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
136 elseif (isempty (__name) && (nargin != 3 || ! strcmp (__flag, "explain"))) |
6046 | 137 print_usage (); |
5589 | 138 endif |
139 | |
8506 | 140 ## Decide if error messages should be collected. |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
141 __logfile = ! isempty (__fid); |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
142 __batch = __logfile || nargout > 0; |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
143 __close_fid = false; |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
144 if (__logfile) |
6494 | 145 if (ischar (__fid)) |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
146 __fname = __fid; |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
147 __fid = fopen (__fname, "wt"); |
6494 | 148 if (__fid < 0) |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
149 error ("test: could not open log file %s", __fname); |
6494 | 150 endif |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
151 __close_fid = true; |
5589 | 152 endif |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
153 if (! strcmp (__flag, "explain")) |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
154 fprintf (__fid, "%sprocessing %s\n", __signal_file, __name); |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
155 fflush (__fid); |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
156 endif |
5589 | 157 else |
158 __fid = stdout; | |
159 endif | |
160 | |
6494 | 161 if (strcmp (__flag, "normal")) |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
162 __grabdemo = false; |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
163 __rundemo = false; |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
164 if (__logfile) |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
165 __verbose = 1; |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
166 elseif (__batch) |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
167 __verbose = -1; |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
168 else |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
169 __verbose = 0; |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
170 endif |
6494 | 171 elseif (strcmp (__flag, "quiet")) |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
172 __grabdemo = false; |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
173 __rundemo = false; |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
174 __verbose = -1; |
6494 | 175 elseif (strcmp (__flag, "verbose")) |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
176 __grabdemo = false; |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
177 __rundemo = true; |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
178 __verbose = 1; |
6494 | 179 elseif (strcmp (__flag, "grabdemo")) |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
180 __grabdemo = true; |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
181 __rundemo = false; |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
182 __verbose = -1; |
5589 | 183 __demo_code = ""; |
12643
55430618bd5f
Properly warn when demo or example called on a function without demos
Rik <octave@nomad.inbox5.com>
parents:
12447
diff
changeset
|
184 __demo_idx = []; |
6494 | 185 elseif (strcmp (__flag, "explain")) |
186 fprintf (__fid, "# %s new test file\n", __signal_file); | |
187 fprintf (__fid, "# %s no tests in file\n", __signal_empty); | |
188 fprintf (__fid, "# %s test had an unexpected result\n", __signal_fail); | |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
189 fprintf (__fid, "# %s test was skipped\n", __signal_skip); |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
190 fprintf (__fid, "# %s code for the test\n\n", __signal_block); |
5589 | 191 fprintf (__fid, "# Search for the unexpected results in the file\n"); |
192 fprintf (__fid, "# then page back to find the file name which caused it.\n"); | |
193 fprintf (__fid, "# The result may be an unexpected failure (in which\n"); | |
194 fprintf (__fid, "# case an error will be reported) or an unexpected\n"); | |
195 fprintf (__fid, "# success (in which case no error will be reported).\n"); | |
5908 | 196 fflush (__fid); |
6494 | 197 if (__close_fid) |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
198 fclose (__fid); |
6494 | 199 endif |
5589 | 200 return; |
201 else | |
10635
d1978e7364ad
Print name of function in error() string messages.
Rik <octave@nomad.inbox5.com>
parents:
10615
diff
changeset
|
202 error ("test: unknown flag '%s'", __flag); |
5589 | 203 endif |
204 | |
8506 | 205 ## Locate the file to test. |
6249 | 206 __file = file_in_loadpath (__name, "all"); |
207 if (isempty (__file)) | |
16994
333243133364
Use matrix concatenation for strings, rather than cstrcat(), for clarity and performance.
Rik <rik@octave.org>
parents:
16992
diff
changeset
|
208 __file = file_in_loadpath ([__name ".m"], "all"); |
5589 | 209 endif |
210 if (isempty (__file)) | |
16994
333243133364
Use matrix concatenation for strings, rather than cstrcat(), for clarity and performance.
Rik <rik@octave.org>
parents:
16992
diff
changeset
|
211 __file = file_in_loadpath ([__name ".cc"], "all"); |
6249 | 212 endif |
213 if (iscell (__file)) | |
6365 | 214 if (isempty (__file)) |
215 __file = ""; | |
216 else | |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
217 __file = __file{1}; # If repeats, return first in path. |
6365 | 218 endif |
5589 | 219 endif |
220 if (isempty (__file)) | |
221 if (__grabdemo) | |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
222 __n = ""; |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
223 __nmax = -1; |
5589 | 224 else |
15762
a5475ba0d199
test.m: Better error reporting for arguments that can't be tested (bug #37592)
Mike Miller <mtmiller@ieee.org>
parents:
15520
diff
changeset
|
225 ftype = exist (__name); |
a5475ba0d199
test.m: Better error reporting for arguments that can't be tested (bug #37592)
Mike Miller <mtmiller@ieee.org>
parents:
15520
diff
changeset
|
226 if (ftype == 3) |
12447
890e89c3dfeb
More explicit error message when source with tests for dynamically linked functions not found (#30341)
David Bateman <dbateman@free.fr>
parents:
11587
diff
changeset
|
227 fprintf (__fid, "%s%s source code with tests for dynamically linked function not found\n", __signal_empty, __name); |
15762
a5475ba0d199
test.m: Better error reporting for arguments that can't be tested (bug #37592)
Mike Miller <mtmiller@ieee.org>
parents:
15520
diff
changeset
|
228 elseif (ftype == 5) |
a5475ba0d199
test.m: Better error reporting for arguments that can't be tested (bug #37592)
Mike Miller <mtmiller@ieee.org>
parents:
15520
diff
changeset
|
229 fprintf (__fid, "%s%s is a built-in function\n", __signal_empty, __name); |
a5475ba0d199
test.m: Better error reporting for arguments that can't be tested (bug #37592)
Mike Miller <mtmiller@ieee.org>
parents:
15520
diff
changeset
|
230 elseif (any (strcmp (__operators__ (), __name))) |
a5475ba0d199
test.m: Better error reporting for arguments that can't be tested (bug #37592)
Mike Miller <mtmiller@ieee.org>
parents:
15520
diff
changeset
|
231 fprintf (__fid, "%s%s is an operator\n", __signal_empty, __name); |
a5475ba0d199
test.m: Better error reporting for arguments that can't be tested (bug #37592)
Mike Miller <mtmiller@ieee.org>
parents:
15520
diff
changeset
|
232 elseif (any (strcmp (__keywords__ (), __name))) |
a5475ba0d199
test.m: Better error reporting for arguments that can't be tested (bug #37592)
Mike Miller <mtmiller@ieee.org>
parents:
15520
diff
changeset
|
233 fprintf (__fid, "%s%s is a keyword\n", __signal_empty, __name); |
12447
890e89c3dfeb
More explicit error message when source with tests for dynamically linked functions not found (#30341)
David Bateman <dbateman@free.fr>
parents:
11587
diff
changeset
|
234 else |
890e89c3dfeb
More explicit error message when source with tests for dynamically linked functions not found (#30341)
David Bateman <dbateman@free.fr>
parents:
11587
diff
changeset
|
235 fprintf (__fid, "%s%s does not exist in path\n", __signal_empty, __name); |
890e89c3dfeb
More explicit error message when source with tests for dynamically linked functions not found (#30341)
David Bateman <dbateman@free.fr>
parents:
11587
diff
changeset
|
236 endif |
5908 | 237 fflush (__fid); |
6494 | 238 if (nargout > 0) |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
239 if (nargout == 1) |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
240 __n = false; |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
241 else |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
242 __n = __nmax = 0; |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
243 endif |
6494 | 244 endif |
5589 | 245 endif |
6494 | 246 if (__close_fid) |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
247 fclose (__fid); |
6494 | 248 endif |
5589 | 249 return; |
250 endif | |
251 | |
8506 | 252 ## Grab the test code from the file. |
5589 | 253 __body = __extract_test_code (__file); |
254 | |
255 if (isempty (__body)) | |
256 if (__grabdemo) | |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
257 __n = ""; |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
258 __nmax = []; |
5589 | 259 else |
6494 | 260 fprintf (__fid, "%s%s has no tests available\n", __signal_empty, __file); |
5908 | 261 fflush (__fid); |
6494 | 262 if (nargout > 0) |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
263 if (nargout == 1) |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
264 __n = false; |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
265 else |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
266 __n = __nmax = 0; |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
267 endif |
6494 | 268 endif |
5589 | 269 endif |
6494 | 270 if (__close_fid) |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
271 fclose (__fid); |
6494 | 272 endif |
5589 | 273 return; |
274 else | |
8506 | 275 ## Add a dummy comment block to the end for ease of indexing. |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
276 if (__body(end) == "\n") |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
277 __body = ["\n" __body "#"]; |
5589 | 278 else |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
279 __body = ["\n" __body "\n#"]; |
5589 | 280 endif |
281 endif | |
282 | |
8506 | 283 ## Chop it up into blocks for evaluation. |
6494 | 284 __lineidx = find (__body == "\n"); |
285 __blockidx = __lineidx(find (! isspace (__body(__lineidx+1))))+1; | |
5589 | 286 |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
287 ## Ready to start tests. |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
288 ## If in batch mode, with a logfile, report what is happening. |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
289 if (__verbose > 0) |
16994
333243133364
Use matrix concatenation for strings, rather than cstrcat(), for clarity and performance.
Rik <rik@octave.org>
parents:
16992
diff
changeset
|
290 disp ([__signal_file, __file]); |
5589 | 291 endif |
292 | |
8506 | 293 ## Assume all tests will pass. |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
294 __all_success = true; |
5589 | 295 |
8506 | 296 ## Process each block separately, initially with no shared variables. |
5589 | 297 __tests = __successes = 0; |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
298 __xfail = __xskip = 0; |
5589 | 299 __shared = " "; |
300 __shared_r = " "; | |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
301 __clearfcn = ""; |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
302 for __i = 1:numel (__blockidx)-1 |
5589 | 303 |
8506 | 304 ## Extract the block. |
5589 | 305 __block = __body(__blockidx(__i):__blockidx(__i+1)-2); |
306 | |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
307 ## Print the code block before execution if in verbose mode. |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
308 if (__verbose > 0) |
5589 | 309 fprintf (__fid, "%s%s\n", __signal_block, __block); |
5908 | 310 fflush (__fid); |
5589 | 311 endif |
312 | |
8506 | 313 ## Split __block into __type and __code. |
6494 | 314 __idx = find (! isletter (__block)); |
315 if (isempty (__idx)) | |
5589 | 316 __type = __block; |
317 __code = ""; | |
318 else | |
319 __type = __block(1:__idx(1)-1); | |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
320 __code = __block(__idx(1):length (__block)); |
5589 | 321 endif |
322 | |
8506 | 323 ## Assume the block will succeed. |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
324 __success = true; |
5589 | 325 __msg = []; |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
326 __isxtest = false; |
5589 | 327 |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
328 ### DEMO |
8506 | 329 |
5589 | 330 ## If in __grabdemo mode, then don't process any other block type. |
331 ## So that the other block types don't have to worry about | |
332 ## this __grabdemo mode, the demo block processor grabs all block | |
333 ## types and skips those which aren't demo blocks. | |
8506 | 334 |
5589 | 335 __isdemo = strcmp (__type, "demo"); |
336 if (__grabdemo || __isdemo) | |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
337 __istest = false; |
5589 | 338 |
339 if (__grabdemo && __isdemo) | |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
340 if (isempty (__demo_code)) |
10549 | 341 __demo_code = __code; |
342 __demo_idx = [1, length(__demo_code)+1]; | |
343 else | |
16994
333243133364
Use matrix concatenation for strings, rather than cstrcat(), for clarity and performance.
Rik <rik@octave.org>
parents:
16992
diff
changeset
|
344 __demo_code = [__demo_code, __code]; |
10549 | 345 __demo_idx = [__demo_idx, length(__demo_code)+1]; |
346 endif | |
5589 | 347 |
348 elseif (__rundemo && __isdemo) | |
10549 | 349 try |
350 ## process the code in an environment without variables | |
14846
460a3c6d8bf1
maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents:
14359
diff
changeset
|
351 eval (sprintf ("function __test__ ()\n%s\nendfunction", __code)); |
10549 | 352 __test__; |
353 input ("Press <enter> to continue: ", "s"); | |
354 catch | |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
355 __success = false; |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
356 __msg = [__signal_fail "demo failed\n" lasterr()]; |
10549 | 357 end_try_catch |
358 clear __test__; | |
5589 | 359 |
360 endif | |
8506 | 361 ## Code already processed. |
362 __code = ""; | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11563
diff
changeset
|
363 |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
364 ### SHARED |
8506 | 365 |
366 elseif (strcmp (__type, "shared")) | |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
367 __istest = false; |
5589 | 368 |
8506 | 369 ## Separate initialization code from variables. |
6494 | 370 __idx = find (__code == "\n"); |
371 if (isempty (__idx)) | |
10549 | 372 __vars = __code; |
373 __code = ""; | |
5589 | 374 else |
10549 | 375 __vars = __code (1:__idx(1)-1); |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
376 __code = __code (__idx(1):length (__code)); |
5589 | 377 endif |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11563
diff
changeset
|
378 |
8506 | 379 ## Strip comments off the variables. |
6730 | 380 __idx = find (__vars == "%" | __vars == "#"); |
6494 | 381 if (! isempty (__idx)) |
10549 | 382 __vars = __vars(1:__idx(1)-1); |
5589 | 383 endif |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11563
diff
changeset
|
384 |
8506 | 385 ## Assign default values to variables. |
5589 | 386 try |
10549 | 387 __vars = deblank (__vars); |
388 if (! isempty (__vars)) | |
16994
333243133364
Use matrix concatenation for strings, rather than cstrcat(), for clarity and performance.
Rik <rik@octave.org>
parents:
16992
diff
changeset
|
389 eval ([strrep(__vars, ",", "=[];"), "=[];"]); |
10549 | 390 __shared = __vars; |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
391 __shared_r = ["[ " __vars "] = "]; |
10549 | 392 else |
393 __shared = " "; | |
394 __shared_r = " "; | |
395 endif | |
5589 | 396 catch |
10549 | 397 ## Couldn't declare, so don't initialize. |
398 __code = ""; | |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
399 __success = false; |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
400 __msg = [__signal_fail "shared variable initialization failed\n"]; |
5589 | 401 end_try_catch |
402 | |
8506 | 403 ## Initialization code will be evaluated below. |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11563
diff
changeset
|
404 |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
405 ### FUNCTION |
8506 | 406 |
6494 | 407 elseif (strcmp (__type, "function")) |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
408 __istest = false; |
5589 | 409 persistent __fn = 0; |
6494 | 410 __name_position = function_name (__block); |
411 if (isempty (__name_position)) | |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
412 __success = false; |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
413 __msg = [__signal_fail "test failed: missing function name\n"]; |
5589 | 414 else |
415 __name = __block(__name_position(1):__name_position(2)); | |
10130
0c3609dd34cf
oct-parse.yy (eval_string): save and set reading_{script,fcn,classdef}_file vars
John W. Eaton <jwe@octave.org>
parents:
10128
diff
changeset
|
416 __code = __block; |
5589 | 417 try |
17336
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
418 eval (__code); # Define the function |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
419 __clearfcn = sprintf ("%sclear %s;\n", __clearfcn, __name); |
5589 | 420 catch |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
421 __success = false; |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
422 __msg = [__signal_fail "test failed: syntax error\n" lasterr()]; |
5589 | 423 end_try_catch |
424 endif | |
425 __code = ""; | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11563
diff
changeset
|
426 |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
427 ### ENDFUNCTION |
13305
63463570d9fe
Add %!endfunction block keyword to test.m
Rik <octave@nomad.inbox5.com>
parents:
12687
diff
changeset
|
428 |
63463570d9fe
Add %!endfunction block keyword to test.m
Rik <octave@nomad.inbox5.com>
parents:
12687
diff
changeset
|
429 elseif (strcmp (__type, "endfunction")) |
63463570d9fe
Add %!endfunction block keyword to test.m
Rik <octave@nomad.inbox5.com>
parents:
12687
diff
changeset
|
430 ## endfunction simply declares the end of a previous function block. |
63463570d9fe
Add %!endfunction block keyword to test.m
Rik <octave@nomad.inbox5.com>
parents:
12687
diff
changeset
|
431 ## There is no processing to be done here, just skip to next block. |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
432 __istest = false; |
13305
63463570d9fe
Add %!endfunction block keyword to test.m
Rik <octave@nomad.inbox5.com>
parents:
12687
diff
changeset
|
433 __code = ""; |
63463570d9fe
Add %!endfunction block keyword to test.m
Rik <octave@nomad.inbox5.com>
parents:
12687
diff
changeset
|
434 |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
435 ### ASSERT/FAIL |
5589 | 436 |
6494 | 437 elseif (strcmp (__type, "assert") || strcmp (__type, "fail")) |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
438 __istest = true; |
8506 | 439 ## Put the keyword back on the code. |
440 __code = __block; | |
441 ## The code will be evaluated below as a test block. | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11563
diff
changeset
|
442 |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
443 ### ERROR/WARNING |
8506 | 444 |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
445 elseif (strcmp (__type, "error") || strcmp (__type, "warning")) |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
446 __istest = true; |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
447 __iswarning = strcmp (__type, "warning"); |
10398
f7cf1a53e1c5
support ID test for errors and warningsin test suites
Jaroslav Hajek <highegg@gmail.com>
parents:
10130
diff
changeset
|
448 [__pattern, __id, __code] = getpattern (__code); |
f7cf1a53e1c5
support ID test for errors and warningsin test suites
Jaroslav Hajek <highegg@gmail.com>
parents:
10130
diff
changeset
|
449 if (__id) |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
450 __patstr = ["id=" __id]; |
10398
f7cf1a53e1c5
support ID test for errors and warningsin test suites
Jaroslav Hajek <highegg@gmail.com>
parents:
10130
diff
changeset
|
451 else |
16976
13affad7347c
test.m: Use the syntax "BLOCK_NAME failed" for error/warnings blocks.
Rik <rik@octave.org>
parents:
16170
diff
changeset
|
452 if (! strcmp (__pattern, '.')) |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
453 __patstr = ["<" __pattern ">"]; |
16976
13affad7347c
test.m: Use the syntax "BLOCK_NAME failed" for error/warnings blocks.
Rik <rik@octave.org>
parents:
16170
diff
changeset
|
454 else |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
455 __patstr = ifelse (__iswarning, "a warning", "an error"); |
16976
13affad7347c
test.m: Use the syntax "BLOCK_NAME failed" for error/warnings blocks.
Rik <rik@octave.org>
parents:
16170
diff
changeset
|
456 endif |
10398
f7cf1a53e1c5
support ID test for errors and warningsin test suites
Jaroslav Hajek <highegg@gmail.com>
parents:
10130
diff
changeset
|
457 endif |
5589 | 458 try |
10549 | 459 eval (sprintf ("function __test__(%s)\n%s\nendfunction", |
460 __shared, __code)); | |
5589 | 461 catch |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
462 __success = false; |
19258
f7c524595a71
test.m: Fix typo in __signal_fail name.
Rik <rik@octave.org>
parents:
19240
diff
changeset
|
463 __msg = [__signal_fail "test failed: syntax error\n" lasterr()]; |
5589 | 464 end_try_catch |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11563
diff
changeset
|
465 |
5589 | 466 if (__success) |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
467 __success = false; |
10549 | 468 __warnstate = warning ("query", "quiet"); |
469 warning ("on", "quiet"); | |
16977
0d5d3e53255f
test.m: Fix randomly failing %!warning test blocks (bug #35850, bug #38679).
Rik <rik@octave.org>
parents:
16976
diff
changeset
|
470 ## Clear error and warning strings before starting |
0d5d3e53255f
test.m: Fix randomly failing %!warning test blocks (bug #35850, bug #38679).
Rik <rik@octave.org>
parents:
16976
diff
changeset
|
471 lasterr (""); |
0d5d3e53255f
test.m: Fix randomly failing %!warning test blocks (bug #35850, bug #38679).
Rik <rik@octave.org>
parents:
16976
diff
changeset
|
472 lastwarn (""); |
10549 | 473 try |
16977
0d5d3e53255f
test.m: Fix randomly failing %!warning test blocks (bug #35850, bug #38679).
Rik <rik@octave.org>
parents:
16976
diff
changeset
|
474 ## FIXME: lastwarn () must be called once from *WITHIN* the try block |
0d5d3e53255f
test.m: Fix randomly failing %!warning test blocks (bug #35850, bug #38679).
Rik <rik@octave.org>
parents:
16976
diff
changeset
|
475 ## or subsequent warning/lastwarn statements may fail. |
0d5d3e53255f
test.m: Fix randomly failing %!warning test blocks (bug #35850, bug #38679).
Rik <rik@octave.org>
parents:
16976
diff
changeset
|
476 ## Likely this is something to do with the specialness of |
0d5d3e53255f
test.m: Fix randomly failing %!warning test blocks (bug #35850, bug #38679).
Rik <rik@octave.org>
parents:
16976
diff
changeset
|
477 ## the try block which is disabling normal errors. |
0d5d3e53255f
test.m: Fix randomly failing %!warning test blocks (bug #35850, bug #38679).
Rik <rik@octave.org>
parents:
16976
diff
changeset
|
478 lastwarn (); |
10549 | 479 eval (sprintf ("__test__(%s);", __shared)); |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
480 if (! __iswarning) |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
481 __msg = [__signal_fail "error failed.\n" ... |
19329
cfe8ec7650a1
test.m: use comma to separate observed and expected
Andreas Weber <andy.weber.aw@gmail.com>
parents:
19258
diff
changeset
|
482 "Expected " __patstr ", but got no error\n"]; |
10549 | 483 else |
10398
f7cf1a53e1c5
support ID test for errors and warningsin test suites
Jaroslav Hajek <highegg@gmail.com>
parents:
10130
diff
changeset
|
484 if (! isempty (__id)) |
16977
0d5d3e53255f
test.m: Fix randomly failing %!warning test blocks (bug #35850, bug #38679).
Rik <rik@octave.org>
parents:
16976
diff
changeset
|
485 [~, __err] = lastwarn (); |
10398
f7cf1a53e1c5
support ID test for errors and warningsin test suites
Jaroslav Hajek <highegg@gmail.com>
parents:
10130
diff
changeset
|
486 __mismatch = ! strcmp (__err, __id); |
f7cf1a53e1c5
support ID test for errors and warningsin test suites
Jaroslav Hajek <highegg@gmail.com>
parents:
10130
diff
changeset
|
487 else |
16977
0d5d3e53255f
test.m: Fix randomly failing %!warning test blocks (bug #35850, bug #38679).
Rik <rik@octave.org>
parents:
16976
diff
changeset
|
488 __err = trimerr (lastwarn (), "warning"); |
10398
f7cf1a53e1c5
support ID test for errors and warningsin test suites
Jaroslav Hajek <highegg@gmail.com>
parents:
10130
diff
changeset
|
489 __mismatch = isempty (regexp (__err, __pattern, "once")); |
f7cf1a53e1c5
support ID test for errors and warningsin test suites
Jaroslav Hajek <highegg@gmail.com>
parents:
10130
diff
changeset
|
490 endif |
7028 | 491 warning (__warnstate.state, "quiet"); |
492 if (isempty (__err)) | |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
493 __msg = [__signal_fail "warning failed.\n" ... |
19329
cfe8ec7650a1
test.m: use comma to separate observed and expected
Andreas Weber <andy.weber.aw@gmail.com>
parents:
19258
diff
changeset
|
494 "Expected " __patstr ", but got no warning\n"]; |
10398
f7cf1a53e1c5
support ID test for errors and warningsin test suites
Jaroslav Hajek <highegg@gmail.com>
parents:
10130
diff
changeset
|
495 elseif (__mismatch) |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
496 __msg = [__signal_fail "warning failed.\n" ... |
19329
cfe8ec7650a1
test.m: use comma to separate observed and expected
Andreas Weber <andy.weber.aw@gmail.com>
parents:
19258
diff
changeset
|
497 "Expected " __patstr ", but got <" __err ">\n"]; |
7028 | 498 else |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
499 __success = true; |
7028 | 500 endif |
10549 | 501 endif |
5589 | 502 |
10549 | 503 catch |
10398
f7cf1a53e1c5
support ID test for errors and warningsin test suites
Jaroslav Hajek <highegg@gmail.com>
parents:
10130
diff
changeset
|
504 if (! isempty (__id)) |
16977
0d5d3e53255f
test.m: Fix randomly failing %!warning test blocks (bug #35850, bug #38679).
Rik <rik@octave.org>
parents:
16976
diff
changeset
|
505 [~, __err] = lasterr (); |
10398
f7cf1a53e1c5
support ID test for errors and warningsin test suites
Jaroslav Hajek <highegg@gmail.com>
parents:
10130
diff
changeset
|
506 __mismatch = ! strcmp (__err, __id); |
f7cf1a53e1c5
support ID test for errors and warningsin test suites
Jaroslav Hajek <highegg@gmail.com>
parents:
10130
diff
changeset
|
507 else |
16977
0d5d3e53255f
test.m: Fix randomly failing %!warning test blocks (bug #35850, bug #38679).
Rik <rik@octave.org>
parents:
16976
diff
changeset
|
508 __err = trimerr (lasterr (), "error"); |
10398
f7cf1a53e1c5
support ID test for errors and warningsin test suites
Jaroslav Hajek <highegg@gmail.com>
parents:
10130
diff
changeset
|
509 __mismatch = isempty (regexp (__err, __pattern, "once")); |
f7cf1a53e1c5
support ID test for errors and warningsin test suites
Jaroslav Hajek <highegg@gmail.com>
parents:
10130
diff
changeset
|
510 endif |
6494 | 511 warning (__warnstate.state, "quiet"); |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
512 if (__iswarning) |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
513 __msg = [__signal_fail "warning failed.\n" ... |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
514 "Expected warning " __patstr ... |
19329
cfe8ec7650a1
test.m: use comma to separate observed and expected
Andreas Weber <andy.weber.aw@gmail.com>
parents:
19258
diff
changeset
|
515 ", but got error <" __err ">\n"]; |
10549 | 516 elseif (__mismatch) |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
517 __msg = [__signal_fail "error failed.\n" ... |
19329
cfe8ec7650a1
test.m: use comma to separate observed and expected
Andreas Weber <andy.weber.aw@gmail.com>
parents:
19258
diff
changeset
|
518 "Expected " __patstr ", but got <" __err ">\n"]; |
5589 | 519 else |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
520 __success = true; |
5589 | 521 endif |
10549 | 522 end_try_catch |
523 clear __test__; | |
5589 | 524 endif |
8506 | 525 ## Code already processed. |
526 __code = ""; | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11563
diff
changeset
|
527 |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
528 ### TESTIF |
8506 | 529 |
7242 | 530 elseif (strcmp (__type, "testif")) |
14182
f8d99761244c
test: Expand %!testif functionality to multiple conditions
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
531 __e = regexp (__code, '.$', 'lineanchors', 'once'); |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
532 ## Strip any comment from testif line before looking for features |
14190
68fd9f95e1f9
Don't process comments in %!testif specifications.
Rik <octave@nomad.inbox5.com>
parents:
14182
diff
changeset
|
533 __feat_line = strtok (__code(1:__e), '#%'); |
68fd9f95e1f9
Don't process comments in %!testif specifications.
Rik <octave@nomad.inbox5.com>
parents:
14182
diff
changeset
|
534 __feat = regexp (__feat_line, '\w+', 'match'); |
16170
2a4f83826024
new way to test for features in Octave scripts
John W. Eaton <jwe@octave.org>
parents:
16019
diff
changeset
|
535 __feat = strrep (__feat, "HAVE_", ""); |
2a4f83826024
new way to test for features in Octave scripts
John W. Eaton <jwe@octave.org>
parents:
16019
diff
changeset
|
536 __have_feat = __have_feature__ (__feat); |
2a4f83826024
new way to test for features in Octave scripts
John W. Eaton <jwe@octave.org>
parents:
16019
diff
changeset
|
537 if (__have_feat) |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
538 __istest = true; |
16170
2a4f83826024
new way to test for features in Octave scripts
John W. Eaton <jwe@octave.org>
parents:
16019
diff
changeset
|
539 __code = __code(__e + 1 : end); |
2a4f83826024
new way to test for features in Octave scripts
John W. Eaton <jwe@octave.org>
parents:
16019
diff
changeset
|
540 else |
7242 | 541 __xskip++; |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
542 __istest = false; |
10615
08050f37ba49
Don't abort testing when merely skipping a testif block.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
543 __code = ""; # Skip the code. |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
544 __msg = [__signal_skip "skipped test\n"]; |
7242 | 545 endif |
546 | |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
547 ### TEST |
8506 | 548 |
18166
c9b4d3177de3
Don't count expected failures as passed tests.
Carlo de Falco <cdf@users.sourceforge.net>
parents:
17744
diff
changeset
|
549 elseif (strcmp (__type, "test")) |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
550 __istest = true; |
8506 | 551 ## Code will be evaluated below. |
552 | |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
553 ### XTEST |
18166
c9b4d3177de3
Don't count expected failures as passed tests.
Carlo de Falco <cdf@users.sourceforge.net>
parents:
17744
diff
changeset
|
554 |
c9b4d3177de3
Don't count expected failures as passed tests.
Carlo de Falco <cdf@users.sourceforge.net>
parents:
17744
diff
changeset
|
555 elseif (strcmp (__type, "xtest")) |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
556 __istest = false; |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
557 __isxtest = true; |
18166
c9b4d3177de3
Don't count expected failures as passed tests.
Carlo de Falco <cdf@users.sourceforge.net>
parents:
17744
diff
changeset
|
558 ## Code will be evaluated below. |
c9b4d3177de3
Don't count expected failures as passed tests.
Carlo de Falco <cdf@users.sourceforge.net>
parents:
17744
diff
changeset
|
559 |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
560 ### Comment block. |
8506 | 561 |
6494 | 562 elseif (strcmp (__block(1:1), "#")) |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
563 __istest = false; |
5589 | 564 __code = ""; # skip the code |
565 | |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
566 ### Unknown block. |
8506 | 567 |
5589 | 568 else |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
569 __istest = true; |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
570 __success = false; |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
571 __msg = [__signal_fail "unknown test type!\n"]; |
5589 | 572 __code = ""; # skip the code |
573 endif | |
574 | |
575 ## evaluate code for test, shared, and assert. | |
6494 | 576 if (! isempty(__code)) |
5589 | 577 try |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
578 ## FIXME: Must check for embedded test functions, which cause |
14066
80bda7c4f012
Avoid segfault in test() and demo() blocks with embedded functions (Bugs #35068, #32843, #33981)
Rik <octave@nomad.inbox5.com>
parents:
13914
diff
changeset
|
579 ## segfaults, until issues with subfunctions in functions are resolved. |
80bda7c4f012
Avoid segfault in test() and demo() blocks with embedded functions (Bugs #35068, #32843, #33981)
Rik <octave@nomad.inbox5.com>
parents:
13914
diff
changeset
|
580 embed_func = regexp (__code, '^\s*function ', 'once', 'lineanchors'); |
80bda7c4f012
Avoid segfault in test() and demo() blocks with embedded functions (Bugs #35068, #32843, #33981)
Rik <octave@nomad.inbox5.com>
parents:
13914
diff
changeset
|
581 if (isempty (embed_func)) |
80bda7c4f012
Avoid segfault in test() and demo() blocks with embedded functions (Bugs #35068, #32843, #33981)
Rik <octave@nomad.inbox5.com>
parents:
13914
diff
changeset
|
582 eval (sprintf ("function %s__test__(%s)\n%s\nendfunction", |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
583 __shared_r, __shared, __code)); |
14066
80bda7c4f012
Avoid segfault in test() and demo() blocks with embedded functions (Bugs #35068, #32843, #33981)
Rik <octave@nomad.inbox5.com>
parents:
13914
diff
changeset
|
584 eval (sprintf ("%s__test__(%s);", __shared_r, __shared)); |
80bda7c4f012
Avoid segfault in test() and demo() blocks with embedded functions (Bugs #35068, #32843, #33981)
Rik <octave@nomad.inbox5.com>
parents:
13914
diff
changeset
|
585 else |
80bda7c4f012
Avoid segfault in test() and demo() blocks with embedded functions (Bugs #35068, #32843, #33981)
Rik <octave@nomad.inbox5.com>
parents:
13914
diff
changeset
|
586 error (["Functions embedded in %!test blocks are not allowed.\n", ... |
80bda7c4f012
Avoid segfault in test() and demo() blocks with embedded functions (Bugs #35068, #32843, #33981)
Rik <octave@nomad.inbox5.com>
parents:
13914
diff
changeset
|
587 "Use the %!function/%!endfunction syntax instead to define shared functions for testing.\n"]); |
80bda7c4f012
Avoid segfault in test() and demo() blocks with embedded functions (Bugs #35068, #32843, #33981)
Rik <octave@nomad.inbox5.com>
parents:
13914
diff
changeset
|
588 endif |
5589 | 589 catch |
6728 | 590 if (strcmp (__type, "xtest")) |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
591 __msg = [__signal_fail "known failure\n" lasterr()]; |
10549 | 592 __xfail++; |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
593 __success = false; |
6728 | 594 else |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
595 __msg = [__signal_fail "test failed\n" lasterr()]; |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
596 __success = false; |
6728 | 597 endif |
12687
6d4c18565de1
Deprecate error_text(), __error_text__()
Rik <octave@nomad.inbox5.com>
parents:
12643
diff
changeset
|
598 if (isempty (lasterr ())) |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11563
diff
changeset
|
599 error ("empty error text, probably Ctrl-C --- aborting"); |
10549 | 600 endif |
5589 | 601 end_try_catch |
602 clear __test__; | |
603 endif | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11563
diff
changeset
|
604 |
8506 | 605 ## All done. Remember if we were successful and print any messages. |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
606 if (! isempty (__msg) && (__verbose >= 0 || __logfile)) |
8506 | 607 ## Make sure the user knows what caused the error. |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
608 if (__verbose < 1) |
10549 | 609 fprintf (__fid, "%s%s\n", __signal_block, __block); |
610 fflush (__fid); | |
5589 | 611 endif |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
612 fprintf (__fid, "%s\n", __msg); |
5908 | 613 fflush (__fid); |
8506 | 614 ## Show the variable context. |
7244 | 615 if (! strcmp (__type, "error") && ! strcmp (__type, "testif") |
10549 | 616 && ! all (__shared == " ")) |
617 fputs (__fid, "shared variables "); | |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
618 eval (sprintf ("fdisp(__fid,var2struct(%s));", __shared)); |
10549 | 619 fflush (__fid); |
5589 | 620 endif |
621 endif | |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
622 if (! __success && ! __isxtest) |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
623 __all_success = false; |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
624 ## Stop after 1 error if not in batch mode or only pass/fail requested. |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
625 if (! __batch || nargout == 1) |
10549 | 626 if (nargout > 0) |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
627 if (nargout == 1) |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
628 __n = false; |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
629 else |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
630 __n = __nmax = 0; |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
631 endif |
10549 | 632 endif |
633 if (__close_fid) | |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
634 fclose (__fid); |
10549 | 635 endif |
636 return; | |
5589 | 637 endif |
638 endif | |
18166
c9b4d3177de3
Don't count expected failures as passed tests.
Carlo de Falco <cdf@users.sourceforge.net>
parents:
17744
diff
changeset
|
639 __tests += (__istest || __isxtest); |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
640 __successes += __success && (__istest || __isxtest); |
5589 | 641 endfor |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
642 |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
643 ## Clear any functions created during test run |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
644 eval (__clearfcn, ""); |
5589 | 645 |
646 if (nargout == 0) | |
8422
20cbb0fdab48
test.m: print "has no tests" message if there are demos but no tests
John W. Eaton <jwe@octave.org>
parents:
8202
diff
changeset
|
647 if (__tests || __xfail || __xskip) |
20cbb0fdab48
test.m: print "has no tests" message if there are demos but no tests
John W. Eaton <jwe@octave.org>
parents:
8202
diff
changeset
|
648 if (__xfail) |
16992
4e8f49304059
test.m: Use correct English singular/plural in reporting messages (bug #39164).
Rafael Laboissiere <rafael@laboissiere.net>
parents:
16977
diff
changeset
|
649 printf ("PASSES %d out of %d test%s (%d expected failure%s)\n", |
4e8f49304059
test.m: Use correct English singular/plural in reporting messages (bug #39164).
Rafael Laboissiere <rafael@laboissiere.net>
parents:
16977
diff
changeset
|
650 __successes, __tests, ifelse (__tests > 1, "s", ""), |
4e8f49304059
test.m: Use correct English singular/plural in reporting messages (bug #39164).
Rafael Laboissiere <rafael@laboissiere.net>
parents:
16977
diff
changeset
|
651 __xfail, ifelse (__xfail > 1, "s", "")); |
8422
20cbb0fdab48
test.m: print "has no tests" message if there are demos but no tests
John W. Eaton <jwe@octave.org>
parents:
8202
diff
changeset
|
652 else |
16992
4e8f49304059
test.m: Use correct English singular/plural in reporting messages (bug #39164).
Rafael Laboissiere <rafael@laboissiere.net>
parents:
16977
diff
changeset
|
653 printf ("PASSES %d out of %d test%s\n", __successes, __tests, |
4e8f49304059
test.m: Use correct English singular/plural in reporting messages (bug #39164).
Rafael Laboissiere <rafael@laboissiere.net>
parents:
16977
diff
changeset
|
654 ifelse (__tests > 1, "s", "")); |
8422
20cbb0fdab48
test.m: print "has no tests" message if there are demos but no tests
John W. Eaton <jwe@octave.org>
parents:
8202
diff
changeset
|
655 endif |
20cbb0fdab48
test.m: print "has no tests" message if there are demos but no tests
John W. Eaton <jwe@octave.org>
parents:
8202
diff
changeset
|
656 if (__xskip) |
16992
4e8f49304059
test.m: Use correct English singular/plural in reporting messages (bug #39164).
Rafael Laboissiere <rafael@laboissiere.net>
parents:
16977
diff
changeset
|
657 printf ("Skipped %d test%s due to missing features\n", __xskip, |
4e8f49304059
test.m: Use correct English singular/plural in reporting messages (bug #39164).
Rafael Laboissiere <rafael@laboissiere.net>
parents:
16977
diff
changeset
|
658 ifelse (__xskip > 1, "s", "")); |
8422
20cbb0fdab48
test.m: print "has no tests" message if there are demos but no tests
John W. Eaton <jwe@octave.org>
parents:
8202
diff
changeset
|
659 endif |
6730 | 660 else |
8422
20cbb0fdab48
test.m: print "has no tests" message if there are demos but no tests
John W. Eaton <jwe@octave.org>
parents:
8202
diff
changeset
|
661 printf ("%s%s has no tests available\n", __signal_empty, __file); |
7242 | 662 endif |
5589 | 663 elseif (__grabdemo) |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
664 __n = __demo_code; |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
665 __nmax = __demo_idx; |
6494 | 666 elseif (nargout == 1) |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
667 __n = __all_success; |
5589 | 668 else |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
669 __n = __successes; |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
670 __nmax = __tests; |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
671 __nxfail = __xfail; |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
672 __nskip = __xskip; |
5589 | 673 endif |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
674 |
5589 | 675 endfunction |
676 | |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
677 |
8506 | 678 ## Create structure with fieldnames the name of the input variables. |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
679 function s = var2struct (varargin) |
6494 | 680 for i = 1:nargin |
681 s.(deblank (argn(i,:))) = varargin{i}; | |
5589 | 682 endfor |
683 endfunction | |
684 | |
8506 | 685 ## Find [start,end] of fn in 'function [a,b] = fn'. |
6494 | 686 function pos = function_name (def) |
5589 | 687 pos = []; |
688 | |
8506 | 689 ## Find the end of the name. |
6494 | 690 right = find (def == "(", 1); |
691 if (isempty (right)) | |
692 return; | |
693 endif | |
694 right = find (def(1:right-1) != " ", 1, "last"); | |
5589 | 695 |
8506 | 696 ## Find the beginning of the name. |
6494 | 697 left = max ([find(def(1:right)==" ", 1, "last"), ... |
10549 | 698 find(def(1:right)=="=", 1, "last")]); |
6494 | 699 if (isempty (left)) |
700 return; | |
701 endif | |
5589 | 702 left++; |
703 | |
8506 | 704 ## Return the end points of the name. |
6494 | 705 pos = [left, right]; |
5589 | 706 endfunction |
707 | |
8506 | 708 ## Strip <pattern> from '<pattern> code'. |
10398
f7cf1a53e1c5
support ID test for errors and warningsin test suites
Jaroslav Hajek <highegg@gmail.com>
parents:
10130
diff
changeset
|
709 ## Also handles 'id=ID code' |
f7cf1a53e1c5
support ID test for errors and warningsin test suites
Jaroslav Hajek <highegg@gmail.com>
parents:
10130
diff
changeset
|
710 function [pattern, id, rest] = getpattern (str) |
6494 | 711 pattern = "."; |
10398
f7cf1a53e1c5
support ID test for errors and warningsin test suites
Jaroslav Hajek <highegg@gmail.com>
parents:
10130
diff
changeset
|
712 id = []; |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11563
diff
changeset
|
713 rest = str; |
6494 | 714 str = trimleft (str); |
715 if (! isempty (str) && str(1) == "<") | |
716 close = index (str, ">"); | |
717 if (close) | |
5589 | 718 pattern = str(2:close-1); |
719 rest = str(close+1:end); | |
720 endif | |
10398
f7cf1a53e1c5
support ID test for errors and warningsin test suites
Jaroslav Hajek <highegg@gmail.com>
parents:
10130
diff
changeset
|
721 elseif (strncmp (str, "id=", 3)) |
f7cf1a53e1c5
support ID test for errors and warningsin test suites
Jaroslav Hajek <highegg@gmail.com>
parents:
10130
diff
changeset
|
722 [id, rest] = strtok (str(4:end)); |
5589 | 723 endif |
724 endfunction | |
725 | |
8506 | 726 ## Strip '.*prefix:' from '.*prefix: msg\n' and strip trailing blanks. |
6494 | 727 function msg = trimerr (msg, prefix) |
16977
0d5d3e53255f
test.m: Fix randomly failing %!warning test blocks (bug #35850, bug #38679).
Rik <rik@octave.org>
parents:
16976
diff
changeset
|
728 idx = index (msg, [prefix ":"]); |
6494 | 729 if (idx > 0) |
730 msg(1:idx+length(prefix)) = []; | |
731 endif | |
16977
0d5d3e53255f
test.m: Fix randomly failing %!warning test blocks (bug #35850, bug #38679).
Rik <rik@octave.org>
parents:
16976
diff
changeset
|
732 msg = strtrim (msg); |
5589 | 733 endfunction |
734 | |
8506 | 735 ## Strip leading blanks from string. |
6494 | 736 function str = trimleft (str) |
16977
0d5d3e53255f
test.m: Fix randomly failing %!warning test blocks (bug #35850, bug #38679).
Rik <rik@octave.org>
parents:
16976
diff
changeset
|
737 idx = find (! isspace (str), 1); |
0d5d3e53255f
test.m: Fix randomly failing %!warning test blocks (bug #35850, bug #38679).
Rik <rik@octave.org>
parents:
16976
diff
changeset
|
738 str = str(idx:end); |
5589 | 739 endfunction |
740 | |
741 function body = __extract_test_code (nm) | |
742 fid = fopen (nm, "rt"); | |
19599 | 743 body = ""; |
5589 | 744 if (fid >= 0) |
19483
64f034147e9a
test.m: Eliminate needless use of feof().
Rik <rik@octave.org>
parents:
19344
diff
changeset
|
745 while (ischar (ln = fgets (fid))) |
64f034147e9a
test.m: Eliminate needless use of feof().
Rik <rik@octave.org>
parents:
19344
diff
changeset
|
746 if (strncmp (ln, "%!", 2)) |
64f034147e9a
test.m: Eliminate needless use of feof().
Rik <rik@octave.org>
parents:
19344
diff
changeset
|
747 body = [body, ln(3:end)]; |
5589 | 748 endif |
749 endwhile | |
750 fclose (fid); | |
751 endif | |
752 endfunction | |
753 | |
16977
0d5d3e53255f
test.m: Fix randomly failing %!warning test blocks (bug #35850, bug #38679).
Rik <rik@octave.org>
parents:
16976
diff
changeset
|
754 |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
755 ## example from toeplitz |
13914
4b1ce071f504
test.m: Update tests which depend on error messages changed in toeplitz.m
Rik <octave@nomad.inbox5.com>
parents:
13305
diff
changeset
|
756 %!shared msg1,msg2 |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
757 %! msg1 = "C must be a vector"; |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
758 %! msg2 = "C and R must be vectors"; |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
759 %!fail ("toeplitz ([])", msg1) |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
760 %!fail ("toeplitz ([1,2;3,4])", msg1) |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
761 %!fail ("toeplitz ([1,2],[])", msg2) |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
762 %!fail ("toeplitz ([1,2],[1,2;3,4])", msg2) |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
763 %!fail ("toeplitz ([1,2;3,4],[1,2])", msg2) |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
764 %!test fail ("toeplitz", "Invalid call to toeplitz") |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
765 %!fail ("toeplitz (1, 2, 3)", "Invalid call to toeplitz") |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
766 %!test assert (toeplitz ([1,2,3], [1,4]), [1,4; 2,1; 3,2]) |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
767 %!assert (toeplitz ([1,2,3], [1,4]), [1,4; 2,1; 3,2]) |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
768 %!demo toeplitz ([1,2,3,4],[1,5,6]) |
5589 | 769 |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
770 ## example from kron |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
771 %!error <Invalid call to kron> kron () |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
772 %!error <Invalid call to kron> kron (1) |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
773 %!test assert (isempty (kron ([], rand (3, 4)))) |
5589 | 774 %!test assert (isempty (kron (rand (3, 4), []))) |
775 %!test assert (isempty (kron ([], []))) | |
776 %!shared A, B | |
777 %!test | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11563
diff
changeset
|
778 %! A = [1, 2, 3; 4, 5, 6]; |
5589 | 779 %! B = [1, -1; 2, -2]; |
780 %!assert (size (kron (zeros (3, 0), A)), [ 3*rows(A), 0 ]) | |
781 %!assert (size (kron (zeros (0, 3), A)), [ 0, 3*columns(A) ]) | |
782 %!assert (size (kron (A, zeros (3, 0))), [ 3*rows(A), 0 ]) | |
783 %!assert (size (kron (A, zeros (0, 3))), [ 0, 3*columns(A) ]) | |
784 %!assert (kron (pi, e), pi*e) | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11563
diff
changeset
|
785 %!assert (kron (pi, A), pi*A) |
5589 | 786 %!assert (kron (A, e), e*A) |
787 %!assert (kron ([1, 2, 3], A), [ A, 2*A, 3*A ]) | |
788 %!assert (kron ([1; 2; 3], A), [ A; 2*A; 3*A ]) | |
789 %!assert (kron ([1, 2; 3, 4], A), [ A, 2*A; 3*A, 4*A ]) | |
790 %!test | |
791 %! res = [1,-1,2,-2,3,-3; 2,-2,4,-4,6,-6; 4,-4,5,-5,6,-6; 8,-8,10,-10,12,-12]; | |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
792 %! assert (kron (A, B), res); |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
793 %!shared # clear out shared variables |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
794 |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
795 ## Now verify test() itself |
5589 | 796 |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
797 ## Test 'fail' keyword |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
798 %!fail ("test", "Invalid call to test") # no args, generates usage() |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
799 %!fail ("test (1,2,3,4)", "usage.*test") # too many args, generates usage() |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
800 %!fail ('test ("test", "bogus")', "unknown flag") # incorrect args |
5589 | 801 %!fail ('garbage','garbage.*undefined') # usage on nonexistent function should be |
802 | |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
803 ## Test 'error' keyword |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
804 %!error test # no args, generates usage() |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
805 %!error test (1,2,3,4) # too many args, generates usage() |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
806 %!error <unknown flag> test ("test", "bogus"); # incorrect args |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
807 %!error test ("test", "bogus"); # test without pattern |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
808 %!error <'garbage' undefined> garbage; # usage on nonexistent function is error |
5589 | 809 |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
810 ## Test 'warning' keyword |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
811 %!warning warning ("warning message"); # no pattern |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
812 %!warning <warning message> warning ("warning message"); # with pattern |
5589 | 813 |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
814 ## Test 'shared' keyword |
5589 | 815 %!shared a # create a shared variable |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
816 %!test a = 3; # assign to a shared variable |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
817 %!test assert (a, 3) # variable should equal 3 |
5589 | 818 %!shared b,c # replace shared variables |
16019
5b2126a8c84f
install tests and functions to run them
John W. Eaton <jwe@octave.org>
parents:
15762
diff
changeset
|
819 %!test assert (!exist ("a", "var")); # a no longer exists |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
820 %!test assert (isempty (b)); # variables start off empty |
5589 | 821 %!shared a,b,c # recreate a shared variable |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
822 %!test assert (isempty (a)); # value is empty even if it had a previous value |
5589 | 823 %!test a=1; b=2; c=3; # give values to all variables |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
824 %!test assert ([a,b,c], [1,2,3]); # test all of them together |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
825 %!test c=6; # update a value |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
826 %!test assert ([a,b,c], [1,2,6]); # show that the update sticks |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
827 %!shared # clear all shared variables |
16019
5b2126a8c84f
install tests and functions to run them
John W. Eaton <jwe@octave.org>
parents:
15762
diff
changeset
|
828 %!test assert (!exist ("a", "var")) # show that they are cleared |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
829 %!shared a,b,c # support for initializer shorthand |
5589 | 830 %! a=1; b=2; c=4; |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
831 %!shared # clear all shared variables for remainder of tests |
5589 | 832 |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
833 ## Test 'function' keyword |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
834 %!function x = __test_a (y) |
5589 | 835 %! x = 2*y; |
13305
63463570d9fe
Add %!endfunction block keyword to test.m
Rik <octave@nomad.inbox5.com>
parents:
12687
diff
changeset
|
836 %!endfunction |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
837 %!assert (__test_a (2), 4) # Test a test function |
5589 | 838 |
839 %!function __test_a (y) | |
840 %! x = 2*y; | |
13305
63463570d9fe
Add %!endfunction block keyword to test.m
Rik <octave@nomad.inbox5.com>
parents:
12687
diff
changeset
|
841 %!endfunction |
5589 | 842 %!test |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
843 %! __test_a (2); # Test a test function with no return value |
5589 | 844 |
845 %!function [x,z] = __test_a (y) | |
846 %! x = 2*y; | |
847 %! z = 3*y; | |
13305
63463570d9fe
Add %!endfunction block keyword to test.m
Rik <octave@nomad.inbox5.com>
parents:
12687
diff
changeset
|
848 %!endfunction |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
849 %!test |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
850 %! [x,z] = __test_a (3); # Test a test function with multiple returns |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
851 %! assert (x,6); |
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
852 %! assert (z,9); |
5589 | 853 |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
854 ## Test 'assert' keyword |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
855 %!assert (isempty ([])) # support for test assert shorthand |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
856 %!assert (size (ones (1,2,3)), [1 2 3]) |
5589 | 857 |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
858 ## Test 'demo' keyword |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
859 %!demo # multiline demo block |
14237
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14190
diff
changeset
|
860 %! t = [0:0.01:2*pi]; x = sin (t); |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14190
diff
changeset
|
861 %! plot (t,x); |
5589 | 862 %! % you should now see a sine wave in your figure window |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
863 |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
864 %!demo a=3 # single line demo blocks work too |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
865 |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
866 %!test |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
867 %! [code, idx] = test ("test", "grabdemo"); |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
868 %! assert (numel (idx), 4); |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
869 %! assert (code(idx(3):end), |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
870 %! " a=3 # single line demo blocks work too"); |
5589 | 871 |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
872 ## Test 'testif' keyword |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
873 %!testif HAVE_BOGUS_FEATURE |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
874 %! error ("testif executed code despite not having feature"); |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
875 |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
876 ## Test 'xtest' keyword |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
877 %!xtest |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
878 %! assert (1, 1); # Test passes |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
879 %!xtest |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
880 %! assert (0, 1); # Test fails |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
881 |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
882 ## Test comment block. it can contain anything. |
5589 | 883 %!## |
884 %! it is the "#" as the block type that makes it a comment | |
14237
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14190
diff
changeset
|
885 %! and it stays as a comment even through continuation lines |
5589 | 886 %! which means that it works well with commenting out whole tests |
887 | |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
888 %% Test test() input validation |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
889 %!error <NAME must be a string> test (1) |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
890 %!error <second argument must be a string> test ("ls", 1) |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
891 %!error test ([], "normal") |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
892 |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
893 ## All of the following tests should fail. These tests should |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
894 ## be disabled unless you are developing test() since users don't |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
895 ## like to be presented with expected failures. I use '% !' to disable. |
5589 | 896 % !test error("---------Failure tests. Use test('test','verbose',1)"); |
897 % !test assert([a,b,c],[1,3,6]); # variables have wrong values | |
898 % !bogus # unknown block type | |
899 % !error toeplitz([1,2,3]); # correct usage | |
900 % !test syntax errors) # syntax errors fail properly | |
901 % !shared garbage in # variables must be comma separated | |
902 % !error syntax++error # error test fails on syntax errors | |
903 % !error "succeeds."; # error test fails if code succeeds | |
904 % !error <wrong pattern> error("message") # error pattern must match | |
905 % !demo with syntax error # syntax errors in demo fail properly | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11563
diff
changeset
|
906 % !shared a,b,c |
5589 | 907 % !demo # shared variables not available in demo |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
908 % ! assert (exist ("a", "var")) |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11563
diff
changeset
|
909 % !error |
19240
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
910 % ! test ('/etc/passwd'); |
ec28b7216501
test.m: Overhaul function (also fixes bug #39703).
Rik <rik@octave.org>
parents:
19047
diff
changeset
|
911 % ! test ("nonexistent file"); |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11563
diff
changeset
|
912 % ! ## These don't signal an error, so the test for an error fails. Note |
5589 | 913 % ! ## that the call doesn't reference the current fid (it is unavailable), |
914 % ! ## so of course the informational message is not printed in the log. | |
14237
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14190
diff
changeset
|
915 |