changeset 8522:d65b33e55d40

nargchk.m: improve compatibility; new tests
author Peter L. Sondergaard <peter@sonderport.dk>
date Thu, 15 Jan 2009 01:06:06 -0500
parents 93cf10950334
children ad3afaaa19c1
files scripts/ChangeLog scripts/general/nargchk.m scripts/general/nargoutchk.m
diffstat 3 files changed, 59 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/ChangeLog	Thu Jan 15 00:50:20 2009 -0500
+++ b/scripts/ChangeLog	Thu Jan 15 01:06:06 2009 -0500
@@ -1,3 +1,8 @@
+2009-01-15  Peter L. Søndergaard  <peter@sonderport.dk>
+
+	* general/nargoutchk.m: Doc fix.
+	* general/nargchk.m: Improve compatibility.  New tests.
+
 2008-01-15  Rafael Laboissiere  <rafael@debian.org>
 
 	* gethelp.cc: Include <cstdio>.
--- a/scripts/general/nargchk.m	Thu Jan 15 00:50:20 2009 -0500
+++ b/scripts/general/nargchk.m	Thu Jan 15 01:06:06 2009 -0500
@@ -1,5 +1,4 @@
-## Copyright (C) 1994, 1995, 1996, 1997, 1999, 2000, 2005, 2006, 2007
-##               John W. Eaton
+## Copyright (C) 2008 Bill Denney
 ##
 ## This file is part of Octave.
 ##
@@ -18,34 +17,68 @@
 ## <http://www.gnu.org/licenses/>.
 
 ## -*- texinfo -*-
-## @deftypefn {Function File} {} nargchk (@var{nargin_min}, @var{nargin_max}, @var{n})
-## If @var{n} is in the range @var{nargin_min} through @var{nargin_max}
-## inclusive, return the empty matrix.  Otherwise, return a message
-## indicating whether @var{n} is too large or too small.
+## @deftypefn {Function File} {@var{msgstr} =} nargchk (@var{minargs}, @var{maxargs}, @var{nargs})
+## @deftypefnx {Function File} {@var{msgstr} =} nargchk (@var{minargs}, @var{maxargs}, @var{nargs}, "string")
+## @deftypefnx {Function File} {@var{msgstruct} =} nargchk (@var{minargs}, @var{maxargs}, @var{nargs}, "struct")
+## Return an appropriate error message string (or structure) if the
+## number of inputs requested is invalid.
 ##
-## This is useful for checking to see that the number of arguments supplied
-## to a function is within an acceptable range.
+## This is useful for checking to see that the number of input arguments
+## supplied to a function is within an acceptable range.
 ## @seealso{nargoutchk, error, nargin, nargout}
 ## @end deftypefn
 
-## Author: jwe
+## Author: Bill Denney <bill@denney.ws>
+
+function msg = nargchk (mina, maxa, narg, outtype)
 
-function retval = nargchk (nargin_min, nargin_max, n)
-
-  if (nargin != 3)
+  if (nargin < 3 || nargin > 4)
     print_usage ();
+  elseif (mina > maxa)
+    error ("nargchk: minargs must be <= maxargs");
+  elseif (nargin == 3)
+    outtype = "string";
+  elseif (! any (strcmpi (outtype, {"string" "struct"})))
+    error ("nargchk: output type must be either string or struct");
+  elseif (! (isscalar (mina) && isscalar (maxa) && isscalar (narg)))
+    error ("nargchk: mina, maxa, and narg must be scalars");
   endif
 
-  if (nargin_min > nargin_max)
-    error ("nargchk: nargin_min > nargin_max");
+  msg = struct ("message", "", "identifier", "");
+  if (narg < mina)
+    msg.message = "not enough input arguments";
+    msg.identifier = "Octave:nargchk:not-enough-inputs";
+  elseif (narg > maxa)
+    msg.message = "too many input arguments";
+    msg.identifier = "Octave:nargchk:too-many-inputs";
   endif
 
-  if (n < nargin_min)
-    retval = "nargchk: N is less than nargin_min";
-  elseif (n > nargin_max)
-    retval = "nargchk: N is greater than nargin_max";
+  if (strcmpi (outtype, "string"))
+    msg = msg.message;
   else
-    retval = [];
+    if (isempty (msg.message))
+      msg = struct ([]);
+    endif
+    ## FIXME: remove the error below if error is modified to accept
+    ## struct inputs
+    error ("nargchk: error does not yet support struct inputs")
   endif
 
 endfunction
+
+## Tests
+%!shared stmin, stmax
+%!  stmin = struct ("message", "not enough input arguments",
+%!                  "identifier", "Octave:nargchk:not-enough-inputs");
+%!  stmax = struct ("message", "too many input arguments",
+%!                  "identifier", "Octave:nargchk:too-many-inputs");
+%!assert (nargchk (0, 1, 0), "")
+%!assert (nargchk (0, 1, 1), "")
+%!assert (nargchk (1, 1, 0), "not enough input arguments")
+%!assert (nargchk (0, 1, 2), "too many input arguments")
+%!assert (nargchk (0, 1, 2, "string"), "too many input arguments")
+## Struct outputs
+#%!assert (nargchk (0, 1, 0, "struct"), struct([]))
+#%!assert (nargchk (0, 1, 1, "struct"), struct([]))
+#%!assert (nargchk (1, 1, 0, "struct"), stmin)
+#%!assert (nargchk (0, 1, 2, "struct"), stmax)
--- a/scripts/general/nargoutchk.m	Thu Jan 15 00:50:20 2009 -0500
+++ b/scripts/general/nargoutchk.m	Thu Jan 15 01:06:06 2009 -0500
@@ -23,8 +23,8 @@
 ## Return an appropriate error message string (or structure) if the
 ## number of outputs requested is invalid.
 ##
-## This is useful for checking to see that the number of arguments supplied
-## to a function is within an acceptable range.
+## This is useful for checking to see that the number of output
+## arguments supplied to a function is within an acceptable range.
 ## @seealso{nargchk, error, nargout, nargin}
 ## @end deftypefn