test-org-src.el 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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. (ert-deftest test-org-src/coderef-regexp ()
  223. "Test `org-src-coderef-regexp' specifications."
  224. ;; Regular test.
  225. (should
  226. (string-match-p (org-src-coderef-regexp "; ref:%s")
  227. "#+BEGIN_SRC emacs-lisp\n0; ref:label\n#+END_SRC"))
  228. ;; Ignore white space around the coderef.
  229. (should
  230. (string-match-p (org-src-coderef-regexp "; ref:%s")
  231. "#+BEGIN_SRC emacs-lisp\n0 ; ref:label\n#+END_SRC"))
  232. (should
  233. (string-match-p (org-src-coderef-regexp "; ref:%s")
  234. "#+BEGIN_SRC emacs-lisp\n0 ; ref:label \n#+END_SRC"))
  235. ;; Only match regexp at the end of the line.
  236. (should-not
  237. (string-match-p (org-src-coderef-regexp "; ref:%s")
  238. "#+BEGIN_SRC emacs-lisp\n0; ref:label (+ 1 2)\n#+END_SRC"))
  239. ;; Do not match an empty label.
  240. (should-not
  241. (string-match-p (org-src-coderef-regexp "; ref:%s")
  242. "#+BEGIN_SRC emacs-lisp\n0; ref:\n#+END_SRC"))
  243. ;; When optional argument LABEL is provided, match given label only.
  244. (should
  245. (string-match-p (org-src-coderef-regexp "; ref:%s" "label")
  246. "#+BEGIN_SRC emacs-lisp\n0; ref:label\n#+END_SRC"))
  247. (should-not
  248. (string-match-p (org-src-coderef-regexp "; ref:%s" "label2")
  249. "#+BEGIN_SRC emacs-lisp\n0; ref:label\n#+END_SRC")))
  250. (ert-deftest test-org-src/indented-blocks ()
  251. "Test editing indented blocks."
  252. ;; Editing a block should preserve its global indentation, unless
  253. ;; `org-src-preserve-indentation' is non-nil.
  254. (should
  255. (equal
  256. "- Item\n #+BEGIN_SRC emacs-lisp\n Foo\n #+END_SRC"
  257. (org-test-with-temp-text
  258. "- Item\n<point> #+BEGIN_SRC emacs-lisp\n (+ 1 1)\n #+END_SRC"
  259. (let ((org-edit-src-content-indentation 2)
  260. (org-src-preserve-indentation nil))
  261. (org-edit-special)
  262. (erase-buffer)
  263. (insert "Foo")
  264. (org-edit-src-exit)
  265. (buffer-string)))))
  266. (should
  267. (equal
  268. "- Item\n #+BEGIN_SRC emacs-lisp\n Foo\n #+END_SRC"
  269. (org-test-with-temp-text
  270. "- Item\n<point> #+BEGIN_SRC emacs-lisp\n (+ 1 1)\n #+END_SRC"
  271. (let ((org-src-preserve-indentation t))
  272. (org-edit-special)
  273. (erase-buffer)
  274. (insert " Foo")
  275. (org-edit-src-exit)
  276. (buffer-string)))))
  277. ;; Global indentation obeys `indent-tabs-mode' from the original
  278. ;; buffer.
  279. (should
  280. (string-match-p
  281. "^\t+\s*argument2"
  282. (org-test-with-temp-text
  283. "
  284. - Item
  285. #+BEGIN_SRC emacs-lisp<point>
  286. (progn
  287. (function argument1
  288. argument2))
  289. #+END_SRC"
  290. (setq-local indent-tabs-mode t)
  291. (let ((org-edit-src-content-indentation 2)
  292. (org-src-preserve-indentation nil))
  293. (org-edit-special)
  294. (org-edit-src-exit)
  295. (buffer-string)))))
  296. (should
  297. (string-match-p
  298. "^\s+argument2"
  299. (org-test-with-temp-text
  300. "
  301. - Item
  302. #+BEGIN_SRC emacs-lisp<point>
  303. (progn\n (function argument1\n\t\targument2))
  304. #+END_SRC"
  305. (setq-local indent-tabs-mode nil)
  306. (let ((org-edit-src-content-indentation 2)
  307. (org-src-preserve-indentation nil))
  308. (org-edit-special)
  309. (org-edit-src-exit)
  310. (buffer-string)))))
  311. ;; Global indentation also obeys `tab-width' from original buffer.
  312. (should
  313. (string-match-p
  314. "^\t\\{3\\}\s\\{2\\}argument2"
  315. (org-test-with-temp-text
  316. "
  317. - Item
  318. #+BEGIN_SRC emacs-lisp<point>
  319. (progn
  320. (function argument1
  321. argument2))
  322. #+END_SRC"
  323. (setq-local indent-tabs-mode t)
  324. (setq-local tab-width 4)
  325. (let ((org-edit-src-content-indentation 0)
  326. (org-src-preserve-indentation nil))
  327. (org-edit-special)
  328. (org-edit-src-exit)
  329. (buffer-string)))))
  330. (should
  331. (string-match-p
  332. "^\t\s\\{6\\}argument2"
  333. (org-test-with-temp-text
  334. "
  335. - Item
  336. #+BEGIN_SRC emacs-lisp<point>
  337. (progn
  338. (function argument1
  339. argument2))
  340. #+END_SRC"
  341. (setq-local indent-tabs-mode t)
  342. (setq-local tab-width 8)
  343. (let ((org-edit-src-content-indentation 0)
  344. (org-src-preserve-indentation nil))
  345. (org-edit-special)
  346. (org-edit-src-exit)
  347. (buffer-string))))))
  348. (ert-deftest test-org-src/footnote-references ()
  349. "Test editing footnote references."
  350. ;; Error when there is no definition to edit.
  351. (should-error
  352. (org-test-with-temp-text "A footnote<point>[fn:1]"
  353. (org-edit-special)))
  354. ;; Error when trying to edit an anonymous footnote.
  355. (should-error
  356. (org-test-with-temp-text "A footnote[fn::<point>edit me!]"
  357. (org-edit-special)))
  358. ;; Edit a regular definition.
  359. (should
  360. (equal "[fn:1] Definition"
  361. (org-test-with-temp-text "A footnote<point>[fn:1]\n[fn:1] Definition"
  362. (org-edit-special)
  363. (prog1 (buffer-string) (org-edit-src-exit)))))
  364. ;; Label should be protected against editing.
  365. (should
  366. (org-test-with-temp-text "A footnote<point>[fn:1]\n[fn:1] Definition"
  367. (org-edit-special)
  368. (prog1 (get-text-property 0 'read-only (buffer-string))
  369. (org-edit-src-exit))))
  370. (should
  371. (org-test-with-temp-text "A footnote<point>[fn:1]\n[fn:1] Definition"
  372. (org-edit-special)
  373. (prog1 (get-text-property 5 'read-only (buffer-string))
  374. (org-edit-src-exit))))
  375. ;; Edit a regular definition.
  376. (should
  377. (equal
  378. "A footnote[fn:1][fn:2]\n[fn:1] D1\n\n[fn:2] D2"
  379. (org-test-with-temp-text
  380. "A footnote<point>[fn:1][fn:2]\n[fn:1] D1\n\n[fn:2] D2"
  381. (org-edit-special)
  382. (org-edit-src-exit)
  383. (buffer-string))))
  384. ;; Edit an inline definition.
  385. (should
  386. (equal
  387. "[fn:1:definition]"
  388. (org-test-with-temp-text
  389. "An inline<point>[fn:1] footnote[fn:1:definition]"
  390. (org-edit-special)
  391. (prog1 (buffer-string) (org-edit-src-exit)))))
  392. ;; Label and closing square bracket should be protected against
  393. ;; editing.
  394. (should
  395. (org-test-with-temp-text "An inline<point>[fn:1] footnote[fn:1:definition]"
  396. (org-edit-special)
  397. (prog1 (get-text-property 0 'read-only (buffer-string))
  398. (org-edit-src-exit))))
  399. (should
  400. (org-test-with-temp-text "An inline<point>[fn:1] footnote[fn:1:definition]"
  401. (org-edit-special)
  402. (prog1 (get-text-property 5 'read-only (buffer-string))
  403. (org-edit-src-exit))))
  404. (should
  405. (org-test-with-temp-text "An inline<point>[fn:1] footnote[fn:1:definition]"
  406. (org-edit-special)
  407. (prog1 (get-text-property 16 'read-only (buffer-string))
  408. (org-edit-src-exit))))
  409. ;; Do not include trailing white spaces when displaying the inline
  410. ;; footnote definition.
  411. (should
  412. (equal
  413. "[fn:1:definition]"
  414. (org-test-with-temp-text
  415. "An inline<point>[fn:1] footnote[fn:1:definition] and some text"
  416. (org-edit-special)
  417. (prog1 (buffer-string) (org-edit-src-exit)))))
  418. ;; Preserve local variables when editing a footnote definition.
  419. (should
  420. (eq 'bar
  421. (org-test-with-temp-text "A footnote<point>[fn:1]\n[fn:1] Definition"
  422. (setq-local foo 'bar)
  423. (org-edit-special)
  424. (prog1 foo (org-edit-src-exit))))))
  425. ;;; Code escaping
  426. (ert-deftest test-org-src/escape-code-in-string ()
  427. "Test `org-escape-code-in-string' specifications."
  428. ;; Escape lines starting with "*" or "#+".
  429. (should (equal ",*" (org-escape-code-in-string "*")))
  430. (should (equal ",#+" (org-escape-code-in-string "#+")))
  431. ;; Escape lines starting with ",*" and ",#+". Number of leading
  432. ;; commas does not matter.
  433. (should (equal ",,*" (org-escape-code-in-string ",*")))
  434. (should (equal ",,#+" (org-escape-code-in-string ",#+")))
  435. (should (equal ",,,*" (org-escape-code-in-string ",,*")))
  436. (should (equal ",,,#+" (org-escape-code-in-string ",,#+")))
  437. ;; Indentation does not matter.
  438. (should (equal " ,*" (org-escape-code-in-string " *")))
  439. (should (equal " ,#+" (org-escape-code-in-string " #+")))
  440. ;; Do nothing on other cases.
  441. (should (equal "a" (org-escape-code-in-string "a")))
  442. (should (equal "#" (org-escape-code-in-string "#")))
  443. (should (equal "," (org-escape-code-in-string ","))))
  444. (ert-deftest test-org-src/unescape-code-in-string ()
  445. "Test `org-unescape-code-in-string' specifications."
  446. ;; Unescape lines starting with ",*" or ",#+". Number of leading
  447. ;; commas does not matter.
  448. (should (equal "*" (org-unescape-code-in-string ",*")))
  449. (should (equal "#+" (org-unescape-code-in-string ",#+")))
  450. (should (equal ",*" (org-unescape-code-in-string ",,*")))
  451. (should (equal ",#+" (org-unescape-code-in-string ",,#+")))
  452. ;; Indentation does not matter.
  453. (should (equal " *" (org-unescape-code-in-string " ,*")))
  454. (should (equal " #+" (org-unescape-code-in-string " ,#+")))
  455. ;; Do nothing on other cases.
  456. (should (equal "a" (org-unescape-code-in-string "a")))
  457. (should (equal "#" (org-unescape-code-in-string "#")))
  458. (should (equal "," (org-unescape-code-in-string ","))))
  459. (provide 'test-org-src)
  460. ;;; test-org-src.el ends here