home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / elisp / functions / test-fun.el < prev    next >
Encoding:
Text File  |  1990-07-22  |  2.7 KB  |  72 lines

  1. ;From ark1!uakari.primate.wisc.edu!samsung!cs.utexas.edu!tut.cis.ohio-state.edu!VENERA.ISI.EDU!katz Mon Feb 12 10:18:27 1990
  2. ;Article 1127 of gnu.emacs:
  3. ;Path: ark1!uakari.primate.wisc.edu!samsung!cs.utexas.edu!tut.cis.ohio-state.edu!VENERA.ISI.EDU!katz
  4. ;From katz@VENERA.ISI.EDU
  5. ;Newsgroups: gnu.emacs
  6. ;Subject: Nifty function to test things
  7. ;Message-ID: <9002100125.AA05511@tardis.isi.edu>
  8. ;Date: 10 Feb 90 01:25:35 GMT
  9. ;Sender: daemon@tut.cis.ohio-state.edu
  10. ;Distribution: gnu
  11. ;Organization: GNUs Not Usenet
  12. ;Lines: 56
  13. ;
  14. ;
  15. ;I wrote the following to make testing new functions easier and have
  16. ;found it useful.  Perhaps others will also.
  17. ;
  18. ;Suppose you have written a lower level function, foo, which is
  19. ;supposed to return some value and you want to test it.  However,
  20. ;suppose that you can't test it while in lisp-interaction-mode (for
  21. ;example, a lower level rmail function, where you must be in
  22. ;rmail-mode in order for the function to make sense).  I found myself
  23. ;writing test interactive functions which ask the user for arguments, 
  24. ;run foo, and then do a (message) to tell what happened.  I finally 
  25. ;decided to write a general purpose function.
  26. ;
  27. ;This is also handy for running built-in functions which happen to be
  28. ;non-interactive (such as (current-time-string)).  Also, its handy for
  29. ;finding out what interactive functions actually return.
  30. ;
  31. ;It can run all interactive functions and non-interactive functions
  32. ;that take no arguments.  It cannot run non-interactive functions that
  33. ;take arguments, since we have no way of knowing what types those 
  34. ;arguments should be.
  35. ;
  36. ;
  37. ;You might want to bind it to a key.
  38. ;
  39. ;                Alan
  40. ;
  41. ;--------------------------------------------------------
  42.  
  43. (defvar test-last-test-fun nil
  44.   "The last function we called with test-function")
  45.  
  46. (defun test-function(fun)
  47.   "For testing, calls the function FUN interactively and reports what it
  48. returns.  Also works on non-interactive functions IF they have no arguments.
  49. If a null line is entered for FUN, calls the function we used when this
  50. was last called."
  51.   (interactive "aTest the function: ")
  52.   (let* ((is-real (> (length (symbol-name fun)) 0))
  53.      (real-fun (if is-real fun test-last-test-fun))
  54.      (fname (symbol-name real-fun))
  55.      (len (if (subrp (symbol-function real-fun)) 0
  56.         (length (car (cdr (symbol-function real-fun)))))))
  57.     (if is-real (setq test-last-test-fun fun))
  58.     (if (commandp real-fun)
  59.     (message "The interactive function %s returned: %s."
  60.          fname
  61.          (prin1-to-string (call-interactively real-fun)))
  62.       (if (zerop len)
  63.       (message "The non-interactive function %s returned: %s."
  64.            fname
  65.            (prin1-to-string (apply real-fun nil)))
  66.     (message "Unable to run, %s is non-interactive with %d arg%s."
  67.          fname
  68.          len
  69.          (if (= len 1) "" "s"))))))
  70.  
  71.  
  72.