Mercurial > hg > octave-terminal
changeset 14232:3e94663fc3e9
news.m: Revamp implementation of function.
* news.m: Put input checking first. Close file handle when done.
Add tests. Update docstring.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Wed, 18 Jan 2012 21:27:13 -0800 |
parents | 6ec182c207af |
children | 358b59706656 |
files | scripts/miscellaneous/news.m |
diffstat | 1 files changed, 26 insertions(+), 19 deletions(-) [+] |
line wrap: on
line diff
--- a/scripts/miscellaneous/news.m +++ b/scripts/miscellaneous/news.m @@ -17,20 +17,27 @@ ## <http://www.gnu.org/licenses/>. ## -*- texinfo -*- -## @deftypefn {Function File} {} news (@var{package}) -## Display the current NEWS file for Octave or installed package. +## @deftypefn {Function File} {} news +## @deftypefnx {Function File} {} news (@var{package}) +## Display the current NEWS file for Octave or an installed package. ## -## If @var{package} is the name of an installed package, display the current -## NEWS file for that package. +## When called without an argument, display the NEWS file for Octave. +## When given a package name @var{package}, display the current NEWS file for +## that package. ## @end deftypefn function news (package = "octave") - if (ischar (package) && strcmpi (package, "octave")) + if (nargin > 1) + print_usage (); + elseif (! ischar (package)) + error ("news: PACKAGE must be a string"); + endif + + if (strcmpi (package, "octave")) octetcdir = octave_config_info ("octetcdir"); newsfile = fullfile (octetcdir, "NEWS"); - - elseif (nargin == 1 && ischar (package)) + else installed = pkg ("list"); names = cellfun (@(x) x.name, installed, "UniformOutput", false); ## we are nice and let the user use any case on the package name @@ -39,25 +46,25 @@ error ("Package '%s' is not installed.", package); endif newsfile = fullfile (installed{pos}.dir, "packinfo", "NEWS"); - - else - print_usage; endif - if (exist (newsfile, "file")) - f = fopen (newsfile, "r"); - while (ischar (line = fgets (f))) - puts (line); - endwhile - else + if (! exist (newsfile, "file")) if (strcmpi (package, "octave")) error ("news: unable to locate NEWS file"); else - error ("news: unable to locate NEWS file of %s package", package); + error ("news: unable to locate NEWS file for package %s", package); endif endif + fid = fopen (newsfile, "r"); + while (ischar (line = fgets (fid))) + puts (line); + endwhile + fclose (fid); + endfunction -## Remove from test statistics. No real tests possible -%!assert (1) + +%!error news (1, 2) +%!error <PACKAGE must be a string> news (1) +%!error <Package .* is not installed> news ("__NOT_A_VALID_PKG_NAME__")