Mercurial > hg > octave-lyh
annotate scripts/testfun/example.m @ 14611:f2ed09ae8d3f
doc: Produce .texi from .txi files using Perl rather than C++.
* munge-texi.pl: New Perl file to generate .texi files from .txi files.
* munge-texi.cc: Remove C++ file for generating .texi files.
* Makefile.am: Change build system to use munge-texi.pl
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Tue, 08 May 2012 21:03:42 -0700 |
parents | f3d52523cde1 |
children | 5d3a684236b0 |
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} |
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} {} example ("@var{name}") |
7277fe922e99
doc: Use Octave preference for double quote in docstrings in scripts/
Rik <octave@nomad.inbox5.com>
parents:
14237
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 |
14359
7277fe922e99
doc: Use Octave preference for double quote in docstrings in scripts/
Rik <octave@nomad.inbox5.com>
parents:
14237
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 |
14237
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
87 ## WARNING: don't modify the demos without modifying the tests! |
5589 | 88 %!demo |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14359
diff
changeset
|
89 %! example ("example"); |
14237
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
90 |
5589 | 91 %!demo |
14237
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
92 %! clf; |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14359
diff
changeset
|
93 %! t = 0:0.01:2*pi; x = sin (t); |
12643
55430618bd5f
Properly warn when demo or example called on a function without demos
Rik <octave@nomad.inbox5.com>
parents:
12505
diff
changeset
|
94 %! plot (t,x) |
5589 | 95 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14359
diff
changeset
|
96 %!assert (example ("example",1), "\n example (\"example\");"); |
5589 | 97 %!test |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14359
diff
changeset
|
98 %! [code, idx] = example ("example"); |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11563
diff
changeset
|
99 %! assert (code, ... |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14359
diff
changeset
|
100 %! "\n example (\"example\");\n clf;\n t = 0:0.01:2*pi; x = sin (t);\n plot (t,x)") |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14359
diff
changeset
|
101 %! assert (idx, [1, 23, 73]); |
5589 | 102 |
12643
55430618bd5f
Properly warn when demo or example called on a function without demos
Rik <octave@nomad.inbox5.com>
parents:
12505
diff
changeset
|
103 %% Test input validation |
14039
e98140f84ae0
test: Rewrite %!tests to preserve warning state.
Rik <octave@nomad.inbox5.com>
parents:
13141
diff
changeset
|
104 %!error example |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14359
diff
changeset
|
105 %!error example ("example", 3, 5) |
14039
e98140f84ae0
test: Rewrite %!tests to preserve warning state.
Rik <octave@nomad.inbox5.com>
parents:
13141
diff
changeset
|
106 |