ido-ubiquitous-test.el 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. ;;; ido-ubiquitous-test.el --- -*- "lexical-binding": t -*-
  2. ;; Copyright (C) 2015 Ryan C. Thompson
  3. ;; Filename: ido-ubiquitous-test.el
  4. ;; Author: Ryan C. Thompson
  5. ;; Created: Tue Oct 6 20:52:45 2015 (-0700)
  6. ;; This file is NOT part of GNU Emacs.
  7. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  8. ;;
  9. ;; This program is free software: you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation, either version 3 of the License, or (at
  12. ;; your option) any later version.
  13. ;;
  14. ;; This program is distributed in the hope that it will be useful, but
  15. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. ;; General Public License for more details.
  18. ;;
  19. ;; You should have received a copy of the GNU General Public License
  20. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  21. ;;
  22. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  23. ;;
  24. ;;; Code:
  25. ;; This is a series of macros to facilitate the testing of completion
  26. ;; non-interactively by simulating input.
  27. (require 'ido-ubiquitous)
  28. (defmacro keyboard-quit-to-error (&rest body)
  29. "Evaluate BODY but signal an error on `keyboard-quit'."
  30. `(condition-case nil
  31. (progn ,@body)
  32. (quit
  33. (error "Caught `keyboard-quit'"))))
  34. (defmacro with-simulated-input (keys &rest body)
  35. "Eval body with KEYS as simulated input.
  36. This macro is intended for testing normally interactive functions
  37. by simulating input. If BODY tries to read more input events than
  38. KEYS provides, `keyboard-quit' is invoked (by means of appending
  39. multple C-g keys to KEYS). This is to ensure that BODY will never
  40. block waiting for input, since this macro is intended for
  41. noninteractive use. As such, BODY should not invoke
  42. `keyboard-quit' under normal operation, and KEYS should not
  43. include C-g, or this macro will interpret it as reading past the
  44. end of input."
  45. ;; It would be better to detect end-of-input by overriding
  46. ;; `read-event' to throw an error, since theoretically C-g could be
  47. ;; rebound to something other than `keyboard-quit'. But apparently
  48. ;; some functions read input directly in C code, and redefining
  49. ;; `read-event' has no effect on those. So the suboptimal solution
  50. ;; is to rely on C-g.
  51. (declare (indent 1))
  52. `(let* ((key-sequence (listify-key-sequence (kbd ,keys)))
  53. (C-g-key-sequence
  54. (listify-key-sequence
  55. ;; We *really* want to trigger `keyboard-quit' if we reach
  56. ;; the end of the input.
  57. (kbd "C-g C-g C-g C-g C-g C-g C-g")))
  58. (unread-command-events
  59. (append key-sequence C-g-key-sequence)))
  60. (when (member (car C-g-key-sequence) key-sequence)
  61. (error "KEYS must include C-g"))
  62. (condition-case nil
  63. ,@body
  64. (quit
  65. (error "Reached end of simulated input while evaluating body")))))
  66. (defmacro with-mode (mode arg &rest body)
  67. "Eval (MODE ARG), then body, then restore previous status of MODE.
  68. This will only work on modes that respect the normal conventions
  69. for activation and deactivation."
  70. (declare (indent 2))
  71. (let* ((orig-status (eval mode))
  72. (restore-arg (if orig-status 1 0)))
  73. `(unwind-protect
  74. (progn
  75. (,mode ,arg)
  76. ,@body)
  77. (,mode ,restore-arg))))
  78. (ert-deftest ido-ubiquitous-test ()
  79. "Do some ido-ubiquitous tests"
  80. (with-mode ido-ubiquitous-mode 1
  81. (should
  82. (string=
  83. "green"
  84. (with-simulated-input "g RET"
  85. (completing-read "Prompt: " '("blue" "yellow" "green")))))
  86. (should
  87. (string=
  88. "g"
  89. (with-simulated-input "g C-j"
  90. (completing-read "Prompt: " '("blue" "yellow" "green"))))))
  91. ;; Normal completing-read
  92. (with-mode ido-ubiquitous-mode 0
  93. ;; No match required
  94. (should
  95. (string=
  96. "g"
  97. (with-simulated-input "g RET"
  98. (completing-read "Prompt: " '("blue" "yellow" "green")))))
  99. ;; Match required, so input should be incomplete
  100. (should-error
  101. (with-simulated-input "g RET"
  102. (completing-read "Prompt: " '("blue" "yellow" "green") nil 'require-match))
  103. :type 'error))))
  104. (provide 'ido-ubiquitous-test)
  105. ;;; ido-ubiquitous-test.el ends here