Mercurial > hg > octave-lyh
annotate scripts/testfun/example.m @ 14444:245963d3d628 stable
pkg: bug fix - accessing non-existent variable for error message
author | Miguel Bazdresch <lmb@2pif.info> |
---|---|
date | Thu, 01 Mar 2012 14:58:59 +0000 |
parents | 72c96de7a403 |
children | 11949c9795a0 |
rev | line source |
---|---|
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
14039
diff
changeset
|
1 ## Copyright (C) 2000-2012 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 -*- | |
12643
55430618bd5f
Properly warn when demo or example called on a function without demos
Rik <octave@nomad.inbox5.com>
parents:
12505
diff
changeset
|
20 ## @deftypefn {Command} {} example @var{name} |
55430618bd5f
Properly warn when demo or example called on a function without demos
Rik <octave@nomad.inbox5.com>
parents:
12505
diff
changeset
|
21 ## @deftypefnx {Command} {} example @var{name} @var{n} |
55430618bd5f
Properly warn when demo or example called on a function without demos
Rik <octave@nomad.inbox5.com>
parents:
12505
diff
changeset
|
22 ## @deftypefnx {Function File} {} example ('@var{name}') |
12505
6a1fe83fe129
Allow command forms of example and demo.
Ben Abbott <bpabbott@mac.com>
parents:
11587
diff
changeset
|
23 ## @deftypefnx {Function File} {} example ('@var{name}', @var{n}) |
12643
55430618bd5f
Properly warn when demo or example called on a function without demos
Rik <octave@nomad.inbox5.com>
parents:
12505
diff
changeset
|
24 ## @deftypefnx {Function File} {[@var{s}, @var{idx}] =} example (@dots{}) |
5589 | 25 ## |
12643
55430618bd5f
Properly warn when demo or example called on a function without demos
Rik <octave@nomad.inbox5.com>
parents:
12505
diff
changeset
|
26 ## Display the code for example @var{n} associated with the function |
55430618bd5f
Properly warn when demo or example called on a function without demos
Rik <octave@nomad.inbox5.com>
parents:
12505
diff
changeset
|
27 ## '@var{name}', but do not run it. If @var{n} is not specified, all examples |
5589 | 28 ## are displayed. |
29 ## | |
12643
55430618bd5f
Properly warn when demo or example called on a function without demos
Rik <octave@nomad.inbox5.com>
parents:
12505
diff
changeset
|
30 ## When called with output arguments, the examples are returned in the form of |
55430618bd5f
Properly warn when demo or example called on a function without demos
Rik <octave@nomad.inbox5.com>
parents:
12505
diff
changeset
|
31 ## a string @var{s}, with @var{idx} indicating the ending position of the |
5589 | 32 ## various examples. |
33 ## | |
34 ## See @code{demo} for a complete explanation. | |
5642 | 35 ## @seealso{demo, test} |
5589 | 36 ## @end deftypefn |
37 | |
6494 | 38 function [code_r, idx_r] = example (name, n) |
5589 | 39 |
40 if (nargin < 1 || nargin > 2) | |
6046 | 41 print_usage (); |
5589 | 42 endif |
12505
6a1fe83fe129
Allow command forms of example and demo.
Ben Abbott <bpabbott@mac.com>
parents:
11587
diff
changeset
|
43 |
5589 | 44 if (nargin < 2) |
45 n = 0; | |
12643
55430618bd5f
Properly warn when demo or example called on a function without demos
Rik <octave@nomad.inbox5.com>
parents:
12505
diff
changeset
|
46 elseif (ischar (n)) |
12505
6a1fe83fe129
Allow command forms of example and demo.
Ben Abbott <bpabbott@mac.com>
parents:
11587
diff
changeset
|
47 n = str2double (n); |
13141
e81ddf9cacd5
maint: untabify and remove trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
12643
diff
changeset
|
48 endif |
5589 | 49 |
6494 | 50 [code, idx] = test (name, "grabdemo"); |
5589 | 51 if (nargout > 0) |
52 if (n > 0) | |
6494 | 53 if (n <= length (idx)) |
10549 | 54 code_r = code(idx(n):idx(n+1)-1); |
55 idx_r = [1, length(code_r)+1]; | |
5589 | 56 else |
10549 | 57 code_r = ""; |
58 idx_r = []; | |
5589 | 59 endif |
60 else | |
61 code_r = code; | |
62 idx_r = idx; | |
63 endif | |
64 else | |
65 if (n > 0) | |
66 doidx = n; | |
67 else | |
6494 | 68 doidx = 1:length(idx)-1; |
5589 | 69 endif |
12643
55430618bd5f
Properly warn when demo or example called on a function without demos
Rik <octave@nomad.inbox5.com>
parents:
12505
diff
changeset
|
70 if (isempty (idx)) |
55430618bd5f
Properly warn when demo or example called on a function without demos
Rik <octave@nomad.inbox5.com>
parents:
12505
diff
changeset
|
71 warning ("no example available for %s", name); |
55430618bd5f
Properly warn when demo or example called on a function without demos
Rik <octave@nomad.inbox5.com>
parents:
12505
diff
changeset
|
72 return; |
5589 | 73 elseif (n >= length(idx)) |
6494 | 74 warning ("only %d examples available for %s", length(idx)-1, name); |
12643
55430618bd5f
Properly warn when demo or example called on a function without demos
Rik <octave@nomad.inbox5.com>
parents:
12505
diff
changeset
|
75 return; |
5589 | 76 endif |
77 | |
8507 | 78 for i = 1:length (doidx) |
12643
55430618bd5f
Properly warn when demo or example called on a function without demos
Rik <octave@nomad.inbox5.com>
parents:
12505
diff
changeset
|
79 block = code(idx(doidx(i)):idx(doidx(i)+1)-1); |
6494 | 80 printf ("%s example %d:%s\n\n", name, doidx(i), block); |
5589 | 81 endfor |
82 endif | |
83 | |
84 endfunction | |
85 | |
14039
e98140f84ae0
test: Rewrite %!tests to preserve warning state.
Rik <octave@nomad.inbox5.com>
parents:
13141
diff
changeset
|
86 |
5589 | 87 %!## warning: don't modify the demos without modifying the tests! |
88 %!demo | |
12643
55430618bd5f
Properly warn when demo or example called on a function without demos
Rik <octave@nomad.inbox5.com>
parents:
12505
diff
changeset
|
89 %! example ('example'); |
5589 | 90 %!demo |
12643
55430618bd5f
Properly warn when demo or example called on a function without demos
Rik <octave@nomad.inbox5.com>
parents:
12505
diff
changeset
|
91 %! t=0:0.01:2*pi; x = sin(t); |
55430618bd5f
Properly warn when demo or example called on a function without demos
Rik <octave@nomad.inbox5.com>
parents:
12505
diff
changeset
|
92 %! plot (t,x) |
5589 | 93 |
12643
55430618bd5f
Properly warn when demo or example called on a function without demos
Rik <octave@nomad.inbox5.com>
parents:
12505
diff
changeset
|
94 %!assert (example('example',1), "\n example ('example');"); |
5589 | 95 %!test |
12643
55430618bd5f
Properly warn when demo or example called on a function without demos
Rik <octave@nomad.inbox5.com>
parents:
12505
diff
changeset
|
96 %! [code, idx] = example ('example'); |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11563
diff
changeset
|
97 %! assert (code, ... |
12643
55430618bd5f
Properly warn when demo or example called on a function without demos
Rik <octave@nomad.inbox5.com>
parents:
12505
diff
changeset
|
98 %! "\n example ('example');\n t=0:0.01:2*pi; x = sin(t);\n plot (t,x)") |
55430618bd5f
Properly warn when demo or example called on a function without demos
Rik <octave@nomad.inbox5.com>
parents:
12505
diff
changeset
|
99 %! assert (idx, [1, 23, 63]); |
5589 | 100 |
12643
55430618bd5f
Properly warn when demo or example called on a function without demos
Rik <octave@nomad.inbox5.com>
parents:
12505
diff
changeset
|
101 %% Test input validation |
14039
e98140f84ae0
test: Rewrite %!tests to preserve warning state.
Rik <octave@nomad.inbox5.com>
parents:
13141
diff
changeset
|
102 %!error example |
e98140f84ae0
test: Rewrite %!tests to preserve warning state.
Rik <octave@nomad.inbox5.com>
parents:
13141
diff
changeset
|
103 %!error example ('example', 3, 5) |
e98140f84ae0
test: Rewrite %!tests to preserve warning state.
Rik <octave@nomad.inbox5.com>
parents:
13141
diff
changeset
|
104 |