viper-tests.el 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. ;;; viper-tests.el --- tests for viper.
  2. ;; Copyright (C) 2016-2017 Free Software Foundation, Inc.
  3. ;; This file is part of GNU Emacs.
  4. ;; GNU Emacs 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 3 of the License, or
  7. ;; (at your option) any later version.
  8. ;; GNU Emacs is distributed in the hope that it will be useful,
  9. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. ;; GNU General Public License for more details.
  12. ;; You should have received a copy of the GNU General Public License
  13. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  14. ;;; Commentary:
  15. ;;; Code:
  16. (require 'viper)
  17. (defun viper-test-undo-kmacro (kmacro)
  18. "In a clean viper buffer, run KMACRO and return `buffer-string'.
  19. This function makes as many attempts as possible to clean up
  20. after itself, although it will leave a buffer called
  21. *viper-test-buffer* if it fails (this is deliberate!)."
  22. (let (
  23. ;; Viper just turns itself off during batch use.
  24. (noninteractive nil)
  25. ;; Switch off start up message or it will chew the key presses.
  26. (viper-inhibit-startup-message 't)
  27. ;; Select an expert-level for the same reason.
  28. (viper-expert-level 5)
  29. ;; viper loads this even with -q so make sure it's empty!
  30. (viper-custom-file-name (make-temp-file "viper-tests" nil ".elc"))
  31. (before-buffer (current-buffer)))
  32. (unwind-protect
  33. (progn
  34. ;; viper-mode is essentially global, so set it here.
  35. (viper-mode)
  36. ;; We must switch to buffer because we are using a keyboard macro
  37. ;; which appears to not go to the current-buffer but what ever is
  38. ;; currently taking keyboard events. We use a named buffer because
  39. ;; then we can see what it in it if it all goes wrong.
  40. (switch-to-buffer
  41. (get-buffer-create
  42. "*viper-test-buffer*"))
  43. (erase-buffer)
  44. ;; The new buffer fails to enter vi state so set it.
  45. (viper-change-state-to-vi)
  46. ;; Run the macro.
  47. (execute-kbd-macro kmacro)
  48. (let ((rtn
  49. (buffer-substring-no-properties
  50. (point-min)
  51. (point-max))))
  52. ;; Kill the buffer iff the macro succeeds.
  53. (kill-buffer)
  54. rtn))
  55. ;; Switch everything off and restore the buffer.
  56. (toggle-viper-mode)
  57. (delete-file viper-custom-file-name)
  58. (switch-to-buffer before-buffer))))
  59. (ert-deftest viper-test-go ()
  60. "Test that this file is running."
  61. (should t))
  62. (ert-deftest viper-test-fix ()
  63. "Test that the viper kmacro fixture is working."
  64. (should
  65. (viper-test-undo-kmacro [])))
  66. (ert-deftest viper-test-undo-1 ()
  67. "Test for VI like undo behavior.
  68. Insert 1, then 2 on consecutive lines, followed by undo. This
  69. should leave just 1 in the buffer.
  70. Test for Bug #22295"
  71. (should
  72. (equal
  73. "1\n"
  74. (viper-test-undo-kmacro
  75. [
  76. ?a
  77. ?1
  78. escape
  79. ?o
  80. ?2
  81. escape
  82. ?u
  83. ]
  84. ))))
  85. (ert-deftest viper-test-undo-2 ()
  86. "Test for VI like undo behavior.
  87. Insert \"1 2 3 4 5\" then delete the 2, then the 4, and undo.
  88. Should restore the 4, but leave the 2 deleted.
  89. Test for Bug #22295"
  90. (should
  91. (equal
  92. "1 3 4 5\n"
  93. (viper-test-undo-kmacro
  94. [
  95. ?i
  96. ?1 ? ?2 ? ?3 ? ?4 ? ?5
  97. escape
  98. ?F ?2 ?d ?w
  99. ?w ?d ?w
  100. ?u
  101. ]))))
  102. (ert-deftest viper-test-undo-3 ()
  103. "Test for VI like undo behavior.
  104. Insert \"1 2 3 4 5 6\", delete the 2, then the 3 4 and 5.
  105. Should restore the 3 4 and 5 but not the 2.
  106. Test for Bug #22295"
  107. (should
  108. (equal
  109. "1 3 4 5 6\n"
  110. (viper-test-undo-kmacro
  111. [
  112. ;; Insert this lot.
  113. ?i ?1 ? ?2 ? ?3 ? ?4 ? ?5 ? ?6
  114. escape
  115. ;; Start of line.
  116. ?0
  117. ;; Move to 2, delete
  118. ?w ?d ?w
  119. ;; Delete 3 4 5
  120. ?. ?. ?.
  121. ;; Undo del 5, then
  122. ?u ?. ?.
  123. ]))))
  124. (ert-deftest viper-test-undo-4()
  125. (should
  126. (equal
  127. ""
  128. (viper-test-undo-kmacro
  129. [
  130. ?i ?1 escape
  131. ?o ?2 escape
  132. ?o ?3 escape
  133. ?u ?. ?.
  134. ])
  135. )))
  136. ;;; viper-tests.el ends here