hooks.test 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. ;;;; hooks.test --- tests guile's hooks implementation -*- scheme -*-
  2. ;;;; Copyright (C) 1999, 2001, 2006 Free Software Foundation, Inc.
  3. ;;;;
  4. ;;;; This library is free software; you can redistribute it and/or
  5. ;;;; modify it under the terms of the GNU Lesser General Public
  6. ;;;; License as published by the Free Software Foundation; either
  7. ;;;; version 2.1 of the License, or (at your option) any later version.
  8. ;;;;
  9. ;;;; This library 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 GNU
  12. ;;;; Lesser General Public License for more details.
  13. ;;;;
  14. ;;;; You should have received a copy of the GNU Lesser General Public
  15. ;;;; License along with this library; if not, write to the Free Software
  16. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. ;;;
  18. ;;; miscellaneous
  19. ;;;
  20. ;; FIXME: Maybe a standard wrong-num-arg exception should be thrown instead
  21. ;; of a misc-error? If so, the tests should be changed to expect failure.
  22. (define exception:wrong-num-hook-args
  23. (cons 'misc-error "Hook .* requires .* arguments"))
  24. ;;;
  25. ;;; {The tests}
  26. ;;;
  27. (let ((proc1 (lambda (x) (+ x 1)))
  28. (proc2 (lambda (x) (- x 1)))
  29. (bad-proc (lambda (x y) #t)))
  30. (with-test-prefix "hooks"
  31. (pass-if "make-hook"
  32. (make-hook 1)
  33. #t)
  34. (pass-if "add-hook!"
  35. (let ((x (make-hook 1)))
  36. (add-hook! x proc1)
  37. (add-hook! x proc2)
  38. #t))
  39. (with-test-prefix "add-hook!"
  40. (pass-if "append"
  41. (let ((x (make-hook 1)))
  42. (add-hook! x proc1)
  43. (add-hook! x proc2 #t)
  44. (eq? (cadr (hook->list x))
  45. proc2)))
  46. (pass-if-exception "illegal proc"
  47. exception:wrong-type-arg
  48. (let ((x (make-hook 1)))
  49. (add-hook! x bad-proc)))
  50. (pass-if-exception "illegal hook"
  51. exception:wrong-type-arg
  52. (add-hook! '(foo) proc1)))
  53. (pass-if "run-hook"
  54. (let ((x (make-hook 1)))
  55. (add-hook! x proc1)
  56. (add-hook! x proc2)
  57. (run-hook x 1)
  58. #t))
  59. (with-test-prefix "run-hook"
  60. (pass-if-exception "bad hook"
  61. exception:wrong-type-arg
  62. (let ((x (cons 'a 'b)))
  63. (run-hook x 1)))
  64. (pass-if-exception "too many args"
  65. exception:wrong-num-hook-args
  66. (let ((x (make-hook 1)))
  67. (add-hook! x proc1)
  68. (add-hook! x proc2)
  69. (run-hook x 1 2)))
  70. (pass-if
  71. "destructive procs"
  72. (let ((x (make-hook 1))
  73. (dest-proc1 (lambda (x)
  74. (set-car! x
  75. 'i-sunk-your-battleship)))
  76. (dest-proc2 (lambda (x) (set-cdr! x 'no-way!)))
  77. (val '(a-game-of battleship)))
  78. (add-hook! x dest-proc1)
  79. (add-hook! x dest-proc2 #t)
  80. (run-hook x val)
  81. (and (eq? (car val) 'i-sunk-your-battleship)
  82. (eq? (cdr val) 'no-way!)))))
  83. (with-test-prefix "remove-hook!"
  84. (pass-if ""
  85. (let ((x (make-hook 1)))
  86. (add-hook! x proc1)
  87. (add-hook! x proc2)
  88. (remove-hook! x proc1)
  89. (not (memq proc1 (hook->list x)))))
  90. ; Maybe it should error, but this is probably
  91. ; more convienient
  92. (pass-if "empty hook"
  93. (let ((x (make-hook 1)))
  94. (remove-hook! x proc1)
  95. #t)))
  96. (pass-if "hook->list"
  97. (let ((x (make-hook 1)))
  98. (add-hook! x proc1)
  99. (add-hook! x proc2)
  100. (and (memq proc1 (hook->list x))
  101. (memq proc2 (hook->list x))
  102. #t)))
  103. (pass-if "reset-hook!"
  104. (let ((x (make-hook 1)))
  105. (add-hook! x proc1)
  106. (add-hook! x proc2)
  107. (reset-hook! x)
  108. (null? (hook->list x))))
  109. (with-test-prefix "reset-hook!"
  110. (pass-if "empty hook"
  111. (let ((x (make-hook 1)))
  112. (reset-hook! x)
  113. #t))
  114. (pass-if-exception "bad hook"
  115. exception:wrong-type-arg
  116. (reset-hook! '(a b))))))