hooks.test 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. ;;;; hooks.test --- tests guile's hooks implementation -*- scheme -*-
  2. ;;;; Copyright (C) 1999 Free Software Foundation, Inc.
  3. ;;;;
  4. ;;;; This program is free software; you can redistribute it and/or modify
  5. ;;;; it under the terms of the GNU General Public License as published by
  6. ;;;; the Free Software Foundation; either version 2, or (at your option)
  7. ;;;; any later version.
  8. ;;;;
  9. ;;;; This program is distributed in the hope that it will be useful,
  10. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;;;; GNU General Public License for more details.
  13. ;;;;
  14. ;;;; You should have received a copy of the GNU General Public License
  15. ;;;; along with this software; see the file COPYING. If not, write to
  16. ;;;; the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  17. ;;;; Boston, MA 02111-1307 USA
  18. ;;;;
  19. ;;;; As a special exception, the Free Software Foundation gives permission
  20. ;;;; for additional uses of the text contained in its release of GUILE.
  21. ;;;;
  22. ;;;; The exception is that, if you link the GUILE library with other files
  23. ;;;; to produce an executable, this does not by itself cause the
  24. ;;;; resulting executable to be covered by the GNU General Public License.
  25. ;;;; Your use of that executable is in no way restricted on account of
  26. ;;;; linking the GUILE library code into it.
  27. ;;;;
  28. ;;;; This exception does not however invalidate any other reasons why
  29. ;;;; the executable file might be covered by the GNU General Public License.
  30. ;;;;
  31. ;;;; This exception applies only to the code released by the
  32. ;;;; Free Software Foundation under the name GUILE. If you copy
  33. ;;;; code from other Free Software Foundation releases into a copy of
  34. ;;;; GUILE, as the General Public License permits, the exception does
  35. ;;;; not apply to the code that you add in this way. To avoid misleading
  36. ;;;; anyone as to the status of such modified files, you must delete
  37. ;;;; this exception notice from them.
  38. ;;;;
  39. ;;;; If you write modifications of your own for GUILE, it is your choice
  40. ;;;; whether to permit this exception to apply to your modifications.
  41. ;;;; If you do not wish that, delete this exception notice.
  42. ;;; {Description}
  43. ;;;
  44. ;;; A test suite for hooks. I maybe should've split off some of the
  45. ;;; stuff (like with alists), but this is small enough that it
  46. ;;; probably isn't worth the hassle. A little note: in some places it
  47. ;;; catches all errors when it probably shouldn't, since there's only
  48. ;;; one error we consider correct. This is mostly because the
  49. ;;; add-hook! error in released guiles isn't really accurate
  50. ;;; This should be changed once a released version returns
  51. ;;; wrong-type-arg from add-hook!
  52. ;; {Utility stuff}
  53. ;; Evaluate form inside a catch; if it throws an error, return true
  54. ;; This is good for checking that errors are not ignored
  55. (define-macro (catch-error-returning-true error . form)
  56. `(catch ,error (lambda () (begin ,@form #f)) (lambda (key . args) #t)))
  57. ;; Evaluate form inside a catch; if it throws an error, return false
  58. ;; Good for making sure that errors don't occur
  59. (define-macro (catch-error-returning-false error . form)
  60. `(catch ,error (lambda () (begin ,@form #t)) (lambda (key . args) #f)))
  61. ;; pass-if-not: syntactic sugar
  62. (define-macro (pass-if-not string form)
  63. `(pass-if ,string (not ,form)))
  64. ;; {The tests}
  65. (let ((proc1 (lambda (x) (+ x 1)))
  66. (proc2 (lambda (x) (- x 1)))
  67. (bad-proc (lambda (x y) #t)))
  68. (with-test-prefix "hooks"
  69. (pass-if "make-hook"
  70. (catch-error-returning-false
  71. #t
  72. (define x (make-hook 1))))
  73. (pass-if "add-hook!"
  74. (catch-error-returning-false
  75. #t
  76. (let ((x (make-hook 1)))
  77. (add-hook! x proc1)
  78. (add-hook! x proc2))))
  79. (with-test-prefix "add-hook!"
  80. (pass-if "append"
  81. (let ((x (make-hook 1)))
  82. (add-hook! x proc1)
  83. (add-hook! x proc2 #t)
  84. (eq? (cadr (hook->list x))
  85. proc2)))
  86. (pass-if "illegal proc"
  87. (catch-error-returning-true
  88. #t
  89. (let ((x (make-hook 1)))
  90. (add-hook! x bad-proc))))
  91. (pass-if "illegal hook"
  92. (catch-error-returning-true
  93. 'wrong-type-arg
  94. (add-hook! '(foo) proc1))))
  95. (pass-if "run-hook"
  96. (let ((x (make-hook 1)))
  97. (catch-error-returning-false #t
  98. (add-hook! x proc1)
  99. (add-hook! x proc2)
  100. (run-hook x 1))))
  101. (with-test-prefix "run-hook"
  102. (pass-if "bad hook"
  103. (catch-error-returning-true
  104. #t
  105. (let ((x (cons 'a 'b)))
  106. (run-hook x 1))))
  107. (pass-if "too many args"
  108. (let ((x (make-hook 1)))
  109. (catch-error-returning-true
  110. #t
  111. (add-hook! x proc1)
  112. (add-hook! x proc2)
  113. (run-hook x 1 2))))
  114. (pass-if
  115. "destructive procs"
  116. (let ((x (make-hook 1))
  117. (dest-proc1 (lambda (x)
  118. (set-car! x
  119. 'i-sunk-your-battleship)))
  120. (dest-proc2 (lambda (x) (set-cdr! x 'no-way!)))
  121. (val '(a-game-of battleship)))
  122. (add-hook! x dest-proc1)
  123. (add-hook! x dest-proc2 #t)
  124. (run-hook x val)
  125. (and (eq? (car val) 'i-sunk-your-battleship)
  126. (eq? (cdr val) 'no-way!)))))
  127. (with-test-prefix "remove-hook!"
  128. (pass-if ""
  129. (let ((x (make-hook 1)))
  130. (add-hook! x proc1)
  131. (add-hook! x proc2)
  132. (remove-hook! x proc1)
  133. (not (memq proc1 (hook->list x)))))
  134. ; Maybe it should error, but this is probably
  135. ; more convienient
  136. (pass-if "empty hook"
  137. (catch-error-returning-false
  138. #t
  139. (let ((x (make-hook 1)))
  140. (remove-hook! x proc1)))))
  141. (pass-if "hook->list"
  142. (let ((x (make-hook 1)))
  143. (add-hook! x proc1)
  144. (add-hook! x proc2)
  145. (and (memq proc1 (hook->list x) )
  146. (memq proc2 (hook->list x)))))
  147. (pass-if "reset-hook!"
  148. (let ((x (make-hook 1)))
  149. (add-hook! x proc1)
  150. (add-hook! x proc2)
  151. (reset-hook! x)
  152. (null? (hook->list x))))
  153. (with-test-prefix "reset-hook!"
  154. (pass-if "empty hook"
  155. (let ((x (make-hook 1)))
  156. (reset-hook! x)))
  157. (pass-if "bad hook"
  158. (catch-error-returning-true
  159. #t
  160. (reset-hook! '(a b)))))))