hooks.test 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. ;;;; hooks.test --- tests guile's hooks implementation -*- scheme -*-
  2. ;;;; Copyright (C) 1999, 2001, 2006, 2009, 2010 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 3 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. (define-module (test-suite test-hooks)
  18. #:use-module (test-suite lib))
  19. ;;;
  20. ;;; miscellaneous
  21. ;;;
  22. ;; FIXME: Maybe a standard wrong-num-arg exception should be thrown instead
  23. ;; of a misc-error? If so, the tests should be changed to expect failure.
  24. (define exception:wrong-num-hook-args
  25. (cons 'misc-error "Hook .* requires .* arguments"))
  26. ;;;
  27. ;;; {The tests}
  28. ;;;
  29. (let ((proc1 (lambda (x) (+ x 1)))
  30. (proc2 (lambda (x) (- x 1)))
  31. (bad-proc (lambda (x y) #t)))
  32. (with-test-prefix "hooks"
  33. (pass-if "make-hook"
  34. (make-hook 1)
  35. #t)
  36. (pass-if "add-hook!"
  37. (let ((x (make-hook 1)))
  38. (add-hook! x proc1)
  39. (add-hook! x proc2)
  40. #t))
  41. (with-test-prefix "add-hook!"
  42. (pass-if "append"
  43. (let ((x (make-hook 1)))
  44. (add-hook! x proc1)
  45. (add-hook! x proc2 #t)
  46. (eq? (cadr (hook->list x))
  47. proc2)))
  48. (pass-if-exception "illegal proc"
  49. exception:wrong-type-arg
  50. (let ((x (make-hook 1)))
  51. (add-hook! x bad-proc)))
  52. (pass-if-exception "illegal hook"
  53. exception:wrong-type-arg
  54. (add-hook! '(foo) proc1)))
  55. (pass-if "run-hook"
  56. (let ((x (make-hook 1)))
  57. (add-hook! x proc1)
  58. (add-hook! x proc2)
  59. (run-hook x 1)
  60. #t))
  61. (with-test-prefix "run-hook"
  62. (pass-if-exception "bad hook"
  63. exception:wrong-type-arg
  64. (let ((x (cons 'a 'b)))
  65. (run-hook x 1)))
  66. (pass-if-exception "too many args"
  67. exception:wrong-num-hook-args
  68. (let ((x (make-hook 1)))
  69. (add-hook! x proc1)
  70. (add-hook! x proc2)
  71. (run-hook x 1 2)))
  72. (pass-if
  73. "destructive procs"
  74. (let ((x (make-hook 1))
  75. (dest-proc1 (lambda (x)
  76. (set-car! x
  77. 'i-sunk-your-battleship)))
  78. (dest-proc2 (lambda (x) (set-cdr! x 'no-way!)))
  79. (val '(a-game-of battleship)))
  80. (add-hook! x dest-proc1)
  81. (add-hook! x dest-proc2 #t)
  82. (run-hook x val)
  83. (and (eq? (car val) 'i-sunk-your-battleship)
  84. (eq? (cdr val) 'no-way!)))))
  85. (with-test-prefix "remove-hook!"
  86. (pass-if ""
  87. (let ((x (make-hook 1)))
  88. (add-hook! x proc1)
  89. (add-hook! x proc2)
  90. (remove-hook! x proc1)
  91. (not (memq proc1 (hook->list x)))))
  92. ; Maybe it should error, but this is probably
  93. ; more convienient
  94. (pass-if "empty hook"
  95. (let ((x (make-hook 1)))
  96. (remove-hook! x proc1)
  97. #t)))
  98. (pass-if "hook->list"
  99. (let ((x (make-hook 1)))
  100. (add-hook! x proc1)
  101. (add-hook! x proc2)
  102. (and (memq proc1 (hook->list x))
  103. (memq proc2 (hook->list x))
  104. #t)))
  105. (pass-if "reset-hook!"
  106. (let ((x (make-hook 1)))
  107. (add-hook! x proc1)
  108. (add-hook! x proc2)
  109. (reset-hook! x)
  110. (null? (hook->list x))))
  111. (with-test-prefix "reset-hook!"
  112. (pass-if "empty hook"
  113. (let ((x (make-hook 1)))
  114. (reset-hook! x)
  115. #t))
  116. (pass-if-exception "bad hook"
  117. exception:wrong-type-arg
  118. (reset-hook! '(a b))))))