mwim-tests.el 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. ;;; mwim-tests.el --- Tests for MWIM package
  2. ;; Copyright © 2016 Alex Kost <alezost@gmail.com>
  3. ;; This program is free software; you can redistribute it and/or modify
  4. ;; it under the terms of the GNU General Public License as published by
  5. ;; the Free Software Foundation, either version 3 of the License, or
  6. ;; (at your option) any later version.
  7. ;;
  8. ;; This program 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. ;;
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;; This file provides some tests to check that the point appears on the
  17. ;; right positions after calling MWIM commands.
  18. ;;
  19. ;; These tests can be run like this:
  20. ;;
  21. ;; cd /path/to/mwim
  22. ;; emacs -Q -nw -L . --batch --eval '(progn (load-file "tests/mwim-tests.el") (ert-run-tests-batch-and-exit))'
  23. ;;; Code:
  24. (require 'ert)
  25. (require 'mwim)
  26. (defvar mwim-test-elisp-sample
  27. '(emacs-lisp-mode . "\
  28. hello ; comment
  29. ;; another comment
  30. something
  31. "))
  32. (defvar mwim-test-c-sample
  33. '(c-mode . "\
  34. int foo; /* comment */
  35. // foo
  36. // foo
  37. "))
  38. (defmacro mwim-test-with-sample (sample &rest body)
  39. (declare (debug t) (indent 1))
  40. `(with-temp-buffer
  41. (funcall (car ,sample))
  42. (insert (cdr ,sample))
  43. (goto-char (point-min))
  44. ,@body))
  45. (defun mwim-test-fancy-move ()
  46. (mwim-goto-next-position
  47. (mwim-line-comment-beginning)
  48. (mwim-line-end)
  49. (+ 7 (line-beginning-position))
  50. (mwim-code-beginning)
  51. (mwim-code-end)))
  52. (ert-deftest mwim-test-beginning-of-line-or-code ()
  53. (mwim-test-with-sample mwim-test-elisp-sample
  54. (mwim-beginning-of-line-or-code)
  55. (should (= (point) 3))
  56. (mwim-beginning-of-line-or-code 1)
  57. (should (= (point) 22))
  58. (mwim-beginning-of-line-or-code)
  59. (should (= (point) 28))
  60. (mwim-beginning-of-line-or-code)
  61. (should (= (point) 22))))
  62. (ert-deftest mwim-test-beginning-of-code-or-line-or-comment ()
  63. (mwim-test-with-sample mwim-test-elisp-sample
  64. (mwim-beginning-of-code-or-line-or-comment)
  65. (should (= (point) 14))
  66. (mwim-beginning-of-code-or-line-or-comment)
  67. (should (= (point) 3))
  68. (mwim-beginning-of-code-or-line-or-comment)
  69. (should (= (point) 1))
  70. (mwim-beginning-of-code-or-line-or-comment 2)
  71. (should (= (point) 47))
  72. (mwim-beginning-of-code-or-line-or-comment -1)
  73. (should (= (point) 28))
  74. (mwim-beginning-of-code-or-line-or-comment)
  75. (should (= (point) 22))
  76. (mwim-beginning-of-code-or-line-or-comment)
  77. (should (= (point) 31)))
  78. (mwim-test-with-sample mwim-test-c-sample
  79. (mwim-beginning-of-code-or-line-or-comment)
  80. (should (= (point) 14))
  81. (mwim-beginning-of-code-or-line-or-comment)
  82. (should (= (point) 1))
  83. (mwim-beginning-of-code-or-line-or-comment 1)
  84. (should (= (point) 27))
  85. (mwim-beginning-of-code-or-line-or-comment)
  86. (should (= (point) 25))
  87. (mwim-beginning-of-code-or-line-or-comment)
  88. (should (= (point) 30))))
  89. (ert-deftest mwim-test-end-of-code-or-line ()
  90. (mwim-test-with-sample mwim-test-elisp-sample
  91. (mwim-end-of-code-or-line)
  92. (should (= (point) 8))
  93. (mwim-end-of-code-or-line)
  94. (should (= (point) 21))
  95. (mwim-end-of-code-or-line 2)
  96. (should (= (point) 56))
  97. (mwim-end-of-code-or-line)
  98. (should (= (point) 58))
  99. (mwim-end-of-code-or-line -1)
  100. (should (= (point) 46))
  101. (mwim-end-of-code-or-line)
  102. (should (= (point) 46)))
  103. (mwim-test-with-sample mwim-test-c-sample
  104. (mwim-end-of-code-or-line)
  105. (should (= (point) 9))
  106. (mwim-end-of-code-or-line)
  107. (should (= (point) 24))
  108. (mwim-end-of-code-or-line 1)
  109. (should (= (point) 33))
  110. (mwim-end-of-code-or-line)
  111. (should (= (point) 35))
  112. (mwim-end-of-code-or-line 1)
  113. (should (= (point) 42))
  114. (mwim-end-of-code-or-line)
  115. (should (= (point) 45))))
  116. (ert-deftest mwim-test-mwim ()
  117. (mwim-test-with-sample mwim-test-elisp-sample
  118. (mwim)
  119. (should (= (point) 3))
  120. (mwim)
  121. (should (= (point) 8))
  122. (mwim)
  123. (should (= (point) 14))
  124. (mwim t)
  125. (should (= (point) 8))
  126. (mwim)
  127. (should (= (point) 14))
  128. (mwim)
  129. (should (= (point) 21))
  130. (mwim)
  131. (should (= (point) 1))
  132. (mwim t)
  133. (should (= (point) 21))
  134. (forward-line)
  135. (forward-char)
  136. (mwim)
  137. (should (= (point) 22))
  138. (mwim)
  139. (should (= (point) 28))
  140. (mwim)
  141. (should (= (point) 31))
  142. (mwim)
  143. (should (= (point) 46))))
  144. (ert-deftest mwim-test-fancy ()
  145. (mwim-test-with-sample mwim-test-elisp-sample
  146. (mwim-test-fancy-move)
  147. (should (= (point) 12))
  148. (mwim-test-fancy-move)
  149. (should (= (point) 21))
  150. (mwim-test-fancy-move)
  151. (should (= (point) 8))
  152. (mwim-test-fancy-move)
  153. (should (= (point) 3))
  154. (mwim-test-fancy-move)
  155. (should (= (point) 12))
  156. (forward-line)
  157. (mwim-test-fancy-move)
  158. (should (= (point) 28))
  159. (mwim-test-fancy-move)
  160. (should (= (point) 46))
  161. (mwim-test-fancy-move)
  162. (should (= (point) 29))
  163. (mwim-test-fancy-move)
  164. (should (= (point) 28))
  165. (forward-line)
  166. (mwim-test-fancy-move)
  167. (should (= (point) 56))
  168. (mwim-test-fancy-move)
  169. (should (= (point) 58))
  170. (mwim-test-fancy-move)
  171. (should (= (point) 54))
  172. (mwim-test-fancy-move)
  173. (should (= (point) 47)))
  174. (mwim-test-with-sample mwim-test-c-sample
  175. (mwim-test-fancy-move)
  176. (should (= (point) 9))
  177. (mwim-test-fancy-move)
  178. (should (= (point) 11))
  179. (mwim-test-fancy-move)
  180. (should (= (point) 24))
  181. (mwim-test-fancy-move)
  182. (should (= (point) 8))
  183. (mwim-test-fancy-move)
  184. (should (= (point) 1))
  185. (forward-line)
  186. (mwim-test-fancy-move)
  187. (should (= (point) 27))
  188. (mwim-test-fancy-move)
  189. (should (= (point) 35))
  190. (mwim-test-fancy-move)
  191. (should (= (point) 32))
  192. (mwim-test-fancy-move)
  193. (should (= (point) 33))))
  194. (provide 'mwim-tests)
  195. ;;; mwim-tests.el ends here