test-org-src.el 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. ;;; test-org-src.el --- tests for org-src.el
  2. ;; Copyright (C) 2012-2015 Le Wang
  3. ;; Author: Le Wang <l26wang at gmail dot com>
  4. ;; This file is not part of GNU Emacs.
  5. ;; This program is free software; you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; This program 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
  12. ;; GNU General Public License for more details.
  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. ;;; Code:
  16. (require 'org-test)
  17. (ert-deftest test-org-src/basic ()
  18. "Editing regular block works, with point on source block."
  19. (org-test-with-temp-text
  20. "
  21. <point>#+begin_src emacs-lisp
  22. (message hello)
  23. #+end_src
  24. "
  25. (let ((org-edit-src-content-indentation 2)
  26. (org-src-preserve-indentation nil))
  27. (org-edit-special)
  28. (insert "blah")
  29. (org-edit-src-exit)
  30. (should (equal (buffer-string) "
  31. #+begin_src emacs-lisp
  32. blah(message hello)
  33. #+end_src
  34. "))
  35. (should (looking-at-p "(message hello)")))))
  36. (ert-deftest test-org-src/point-outside-block ()
  37. "Editing with point before/after block signals expected error."
  38. (org-test-with-temp-text
  39. "
  40. #+begin_src emacs-lisp
  41. (message hello)
  42. #+end_src
  43. "
  44. (goto-line 1)
  45. (should-error (org-edit-special))
  46. (goto-char (point-max))
  47. (should-error (org-edit-special))))
  48. (ert-deftest test-org-src/empty-block ()
  49. "Editing empty block."
  50. (org-test-with-temp-text
  51. "
  52. <point>#+begin_src emacs-lisp
  53. #+end_src
  54. "
  55. (let ((org-edit-src-content-indentation 0)
  56. (org-src-preserve-indentation nil))
  57. (org-edit-special)
  58. (insert "blah")
  59. (org-edit-src-exit)
  60. (should (equal (buffer-string) "
  61. #+begin_src emacs-lisp
  62. blah
  63. #+end_src
  64. "))
  65. (should
  66. (equal (buffer-substring (line-beginning-position) (point)) "blah")))))
  67. (ert-deftest test-org-src/blank-line-block ()
  68. "Editing block with just a blank line."
  69. (org-test-with-temp-text-in-file
  70. "
  71. #+begin_src emacs-lisp
  72. #+end_src
  73. "
  74. (let ((org-edit-src-content-indentation 2)
  75. (org-src-preserve-indentation nil))
  76. (goto-line 2)
  77. (org-edit-special)
  78. (insert "blah")
  79. (org-edit-src-exit)
  80. (should (equal (buffer-string) "
  81. #+begin_src emacs-lisp
  82. blah
  83. #+end_src
  84. ")))))
  85. (ert-deftest test-org-src/preserve-tabs ()
  86. "Editing block preserve tab characters."
  87. ;; With `org-src-preserve-indentation' set to nil.
  88. (should
  89. (equal "
  90. #+begin_src emacs-lisp
  91. This is a tab:\t.
  92. #+end_src"
  93. (org-test-with-temp-text
  94. "
  95. #+begin_src emacs-lisp
  96. <point>This is a tab:\t.
  97. #+end_src"
  98. (let ((org-edit-src-content-indentation 2)
  99. (org-src-preserve-indentation nil))
  100. (org-edit-special)
  101. (org-edit-src-exit)
  102. (buffer-string)))))
  103. ;; With `org-src-preserve-indentation' set to t.
  104. (should
  105. (equal "
  106. #+begin_src emacs-lisp
  107. This is a tab:\t.
  108. #+end_src"
  109. (org-test-with-temp-text
  110. "
  111. #+begin_src emacs-lisp
  112. <point>This is a tab:\t.
  113. #+end_src"
  114. (let ((org-edit-src-content-indentation 2)
  115. (org-src-preserve-indentation t))
  116. (org-edit-special)
  117. (org-edit-src-exit)
  118. (buffer-string))))))
  119. (ert-deftest test-org-src/coderef-format ()
  120. "Test `org-src-coderef-format' specifications."
  121. ;; Regular tests in a src block, an example block and an edit
  122. ;; buffer.
  123. (should
  124. (equal "foo"
  125. (let ((org-coderef-label-format "foo"))
  126. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n0\n#+END_SRC"
  127. (org-src-coderef-format)))))
  128. (should
  129. (equal "foo"
  130. (let ((org-coderef-label-format "foo"))
  131. (org-test-with-temp-text "#+BEGIN_EXAMPLE\n0\n#+END_EXAMPLE"
  132. (org-src-coderef-format)))))
  133. (should
  134. (equal "foo"
  135. (let ((org-coderef-label-format "foo") result)
  136. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n0\n#+END_SRC"
  137. (org-edit-special)
  138. (setq result (org-src-coderef-format))
  139. (org-edit-src-exit)
  140. result))))
  141. ;; When a local variable in the source buffer is available, use it.
  142. (should
  143. (equal "bar"
  144. (let ((org-coderef-label-format "foo"))
  145. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n0\n#+END_SRC"
  146. (setq-local org-coderef-label-format "bar")
  147. (org-src-coderef-format)))))
  148. (should
  149. (equal "bar"
  150. (let ((org-coderef-label-format "foo") result)
  151. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n0\n#+END_SRC"
  152. (setq-local org-coderef-label-format "bar")
  153. (org-edit-special)
  154. (setq result (org-src-coderef-format))
  155. (org-edit-src-exit)
  156. result))))
  157. ;; Use provided local format even if in an edit buffer.
  158. (should
  159. (equal "bar"
  160. (let ((org-coderef-label-format "foo"))
  161. (org-test-with-temp-text
  162. "#+BEGIN_SRC emacs-lisp -l \"bar\"\n0\n#+END_SRC"
  163. (org-src-coderef-format)))))
  164. (should
  165. (equal "bar"
  166. (let ((org-coderef-label-format "foo") result)
  167. (org-test-with-temp-text
  168. "#+BEGIN_SRC emacs-lisp -l \"bar\"\n0\n#+END_SRC"
  169. (org-edit-special)
  170. (setq result (org-src-coderef-format))
  171. (org-edit-src-exit)
  172. result))))
  173. ;; Local format has precedence over local variables.
  174. (should
  175. (equal "bar"
  176. (let ((org-coderef-label-format "foo"))
  177. (org-test-with-temp-text
  178. "#+BEGIN_SRC emacs-lisp -l \"bar\"\n0\n#+END_SRC"
  179. (setq-local org-coderef-label-format "foo")
  180. (org-src-coderef-format)))))
  181. (should
  182. (equal "bar"
  183. (let ((org-coderef-label-format "foo") result)
  184. (org-test-with-temp-text
  185. "#+BEGIN_SRC emacs-lisp -l \"bar\"\n0\n#+END_SRC"
  186. (setq-local org-coderef-label-format "foo")
  187. (org-edit-special)
  188. (setq result (org-src-coderef-format))
  189. (org-edit-src-exit)
  190. result))))
  191. ;; When optional argument provides a coderef format string, use it.
  192. (should
  193. (equal "bar"
  194. (let ((org-coderef-label-format "foo")
  195. (element (org-element-create 'src-block '(:label-fmt "bar"))))
  196. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n0\n#+END_SRC"
  197. (org-src-coderef-format element)))))
  198. (should
  199. (equal "baz"
  200. (let ((org-coderef-label-format "foo")
  201. (element (org-element-create 'src-block '(:label-fmt "baz"))))
  202. (org-test-with-temp-text
  203. "#+BEGIN_SRC emacs-lisp -l \"bar\"\n0\n#+END_SRC"
  204. (setq-local org-coderef-label-format "foo")
  205. (org-src-coderef-format element)))))
  206. ;; If it doesn't provide any label format string, fall back to
  207. ;; regular checks.
  208. (should
  209. (equal "foo"
  210. (let ((org-coderef-label-format "foo")
  211. (element (org-element-create 'src-block)))
  212. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n0\n#+END_SRC"
  213. (org-src-coderef-format element)))))
  214. (should
  215. (equal "bar"
  216. (let ((org-coderef-label-format "foo")
  217. (element (org-element-create 'src-block)))
  218. (org-test-with-temp-text
  219. "#+BEGIN_SRC emacs-lisp -l \"bar\"\n0\n#+END_SRC"
  220. (setq-local org-coderef-label-format "foo")
  221. (org-src-coderef-format element))))))
  222. (provide 'test-org-src)
  223. ;;; test-org-src.el ends here