comparison scripts/testfun/example.m @ 12643:55430618bd5f

Properly warn when demo or example called on a function without demos * test.m: Return empty matrix if no demos present * demo.m, example.m: Update documentation. Warn when no demos present and return from function immediately.
author Rik <octave@nomad.inbox5.com>
date Mon, 02 May 2011 11:26:27 -0700
parents 6a1fe83fe129
children e81ddf9cacd5
comparison
equal deleted inserted replaced
12641:9bfc37e699da 12643:55430618bd5f
15 ## You should have received a copy of the GNU General Public License 15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING. If not, see 16 ## along with Octave; see the file COPYING. If not, see
17 ## <http://www.gnu.org/licenses/>. 17 ## <http://www.gnu.org/licenses/>.
18 18
19 ## -*- texinfo -*- 19 ## -*- texinfo -*-
20 ## @deftypefn {Command} {} example @var{name} @var{n} 20 ## @deftypefn {Command} {} example @var{name}
21 ## @deftypefnx {Command} {} example @var{name} @var{n}
22 ## @deftypefnx {Function File} {} example ('@var{name}')
21 ## @deftypefnx {Function File} {} example ('@var{name}', @var{n}) 23 ## @deftypefnx {Function File} {} example ('@var{name}', @var{n})
22 ## @deftypefnx {Function File} {[@var{x}, @var{idx}] =} example ('@var{name}', @var{n}) 24 ## @deftypefnx {Function File} {[@var{s}, @var{idx}] =} example (@dots{})
23 ## 25 ##
24 ## Display the code for example @var{n} associated with the function 26 ## Display the code for example @var{n} associated with the function
25 ## '@var{name}', but do not run it. If @var{n} is not given, all examples 27 ## '@var{name}', but do not run it. If @var{n} is not specified, all examples
26 ## are displayed. 28 ## are displayed.
27 ## 29 ##
28 ## Called with output arguments, the examples are returned in the form of 30 ## When called with output arguments, the examples are returned in the form of
29 ## a string @var{x}, with @var{idx} indicating the ending position of the 31 ## a string @var{s}, with @var{idx} indicating the ending position of the
30 ## various examples. 32 ## various examples.
31 ## 33 ##
32 ## See @code{demo} for a complete explanation. 34 ## See @code{demo} for a complete explanation.
33 ## @seealso{demo, test} 35 ## @seealso{demo, test}
34 ## @end deftypefn 36 ## @end deftypefn
39 print_usage (); 41 print_usage ();
40 endif 42 endif
41 43
42 if (nargin < 2) 44 if (nargin < 2)
43 n = 0; 45 n = 0;
44 elseif (strcmp ("char", class (n))) 46 elseif (ischar (n))
45 n = str2double (n); 47 n = str2double (n);
46 endif 48 endif
47 49
48 [code, idx] = test (name, "grabdemo"); 50 [code, idx] = test (name, "grabdemo");
49 if (nargout > 0) 51 if (nargout > 0)
63 if (n > 0) 65 if (n > 0)
64 doidx = n; 66 doidx = n;
65 else 67 else
66 doidx = 1:length(idx)-1; 68 doidx = 1:length(idx)-1;
67 endif 69 endif
68 if (length (idx) == 0) 70 if (isempty (idx))
69 warning ("example not available for %s", name); 71 warning ("no example available for %s", name);
72 return;
70 elseif (n >= length(idx)) 73 elseif (n >= length(idx))
71 warning ("only %d examples available for %s", length(idx)-1, name); 74 warning ("only %d examples available for %s", length(idx)-1, name);
72 doidx = []; 75 return;
73 endif 76 endif
74 77
75 for i = 1:length (doidx) 78 for i = 1:length (doidx)
76 block = code (idx(doidx(i)):idx(doidx(i)+1)-1); 79 block = code(idx(doidx(i)):idx(doidx(i)+1)-1);
77 printf ("%s example %d:%s\n\n", name, doidx(i), block); 80 printf ("%s example %d:%s\n\n", name, doidx(i), block);
78 endfor 81 endfor
79 endif 82 endif
80 83
81 endfunction 84 endfunction
82 85
83 %!## warning: don't modify the demos without modifying the tests! 86 %!## warning: don't modify the demos without modifying the tests!
84 %!demo 87 %!demo
85 %! example('example'); 88 %! example ('example');
86 %!demo 89 %!demo
87 %! t=0:0.01:2*pi; x=sin(t); 90 %! t=0:0.01:2*pi; x = sin(t);
88 %! plot(t,x) 91 %! plot (t,x)
89 92
90 %!assert (example('example',1), "\n example('example');"); 93 %!assert (example('example',1), "\n example ('example');");
91 %!test 94 %!test
92 %! [code, idx] = example('example'); 95 %! [code, idx] = example ('example');
93 %! assert (code, ... 96 %! assert (code, ...
94 %! "\n example('example');\n t=0:0.01:2*pi; x=sin(t);\n plot(t,x)") 97 %! "\n example ('example');\n t=0:0.01:2*pi; x = sin(t);\n plot (t,x)")
95 %! assert (idx, [1, 22, 59]); 98 %! assert (idx, [1, 23, 63]);
96 99
100 %% Test input validation
97 %!error example; 101 %!error example;
98 %!error example('example',3,5) 102 %!error example('example', 3, 5)