test-ol.el 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. ;;; test-ol.el --- Tests for Org Links library -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2019 Nicolas Goaziou
  3. ;; Author: Nicolas Goaziou <mail@nicolasgoaziou.fr>
  4. ;; This program 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. ;; 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. ;; You should have received a copy of the GNU General Public License
  13. ;; along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. ;;; Code:
  15. ;;; (Un)Escape links
  16. (ert-deftest test-ol/escape-ascii-character ()
  17. "Escape an ascii character."
  18. (should
  19. (string=
  20. "%5B"
  21. (org-link-escape "["))))
  22. (ert-deftest test-ol/escape-ascii-ctrl-character ()
  23. "Escape an ascii control character."
  24. (should
  25. (string=
  26. "%09"
  27. (org-link-escape "\t"))))
  28. (ert-deftest test-ol/escape-multibyte-character ()
  29. "Escape an unicode multibyte character."
  30. (should
  31. (string=
  32. "%E2%82%AC"
  33. (org-link-escape "€"))))
  34. (ert-deftest test-ol/escape-custom-table ()
  35. "Escape string with custom character table."
  36. (should
  37. (string=
  38. "Foo%3A%42ar%0A"
  39. (org-link-escape "Foo:Bar\n" '(?\: ?\B)))))
  40. (ert-deftest test-ol/escape-custom-table-merge ()
  41. "Escape string with custom table merged with default table."
  42. (should
  43. (string=
  44. "%5BF%6F%6F%3A%42ar%0A%5D"
  45. (org-link-escape "[Foo:Bar\n]" '(?\: ?\B ?\o) t))))
  46. (ert-deftest test-ol/unescape-ascii-character ()
  47. "Unescape an ascii character."
  48. (should
  49. (string=
  50. "["
  51. (org-link-unescape "%5B"))))
  52. (ert-deftest test-ol/unescape-ascii-ctrl-character ()
  53. "Unescpae an ascii control character."
  54. (should
  55. (string=
  56. "\n"
  57. (org-link-unescape "%0A"))))
  58. (ert-deftest test-ol/unescape-multibyte-character ()
  59. "Unescape unicode multibyte character."
  60. (should
  61. (string=
  62. "€"
  63. (org-link-unescape "%E2%82%AC"))))
  64. (ert-deftest test-ol/unescape-ascii-extended-char ()
  65. "Unescape old style percent escaped character."
  66. (should
  67. (string=
  68. "àâçèéêîôùû"
  69. (decode-coding-string
  70. (org-link-unescape "%E0%E2%E7%E8%E9%EA%EE%F4%F9%FB") 'latin-1))))
  71. (ert-deftest test-ol/escape-url-with-escaped-char ()
  72. "Escape and unescape a URL that includes an escaped char.
  73. http://article.gmane.org/gmane.emacs.orgmode/21459/"
  74. (should
  75. (string=
  76. "http://some.host.com/form?&id=blah%2Bblah25"
  77. (org-link-unescape
  78. (org-link-escape "http://some.host.com/form?&id=blah%2Bblah25")))))
  79. ;;; Store links
  80. (ert-deftest test-ol/store-link ()
  81. "Test `org-store-link' specifications."
  82. ;; On a headline, link to that headline. Use heading as the
  83. ;; description of the link.
  84. (should
  85. (let (org-store-link-props org-stored-links)
  86. (org-test-with-temp-text-in-file "* H1"
  87. (let ((file (buffer-file-name)))
  88. (equal (format "[[file:%s::*H1][H1]]" file)
  89. (org-store-link nil))))))
  90. ;; On a headline, remove any link from description.
  91. (should
  92. (let (org-store-link-props org-stored-links)
  93. (org-test-with-temp-text-in-file "* [[#l][d]]"
  94. (let ((file (buffer-file-name)))
  95. (equal (format "[[file:%s::*%s][d]]"
  96. file
  97. (org-link-escape "[[#l][d]]"))
  98. (org-store-link nil))))))
  99. (should
  100. (let (org-store-link-props org-stored-links)
  101. (org-test-with-temp-text-in-file "* [[l]]"
  102. (let ((file (buffer-file-name)))
  103. (equal (format "[[file:%s::*%s][l]]" file (org-link-escape "[[l]]"))
  104. (org-store-link nil))))))
  105. (should
  106. (let (org-store-link-props org-stored-links)
  107. (org-test-with-temp-text-in-file "* [[l1][d1]] [[l2][d2]]"
  108. (let ((file (buffer-file-name)))
  109. (equal (format "[[file:%s::*%s][d1 d2]]"
  110. file
  111. (org-link-escape "[[l1][d1]] [[l2][d2]]"))
  112. (org-store-link nil))))))
  113. ;; On a named element, link to that element.
  114. (should
  115. (let (org-store-link-props org-stored-links)
  116. (org-test-with-temp-text-in-file "#+NAME: foo\nParagraph"
  117. (let ((file (buffer-file-name)))
  118. (equal (format "[[file:%s::foo][foo]]" file)
  119. (org-store-link nil))))))
  120. ;; Store link to Org buffer, with context.
  121. (should
  122. (let ((org-stored-links nil)
  123. (org-id-link-to-org-use-id nil)
  124. (org-context-in-file-links t))
  125. (org-test-with-temp-text-in-file "* h1"
  126. (let ((file (buffer-file-name)))
  127. (equal (format "[[file:%s::*h1][h1]]" file)
  128. (org-store-link nil))))))
  129. ;; Store link to Org buffer, without context.
  130. (should
  131. (let ((org-stored-links nil)
  132. (org-id-link-to-org-use-id nil)
  133. (org-context-in-file-links nil))
  134. (org-test-with-temp-text-in-file "* h1"
  135. (let ((file (buffer-file-name)))
  136. (equal (format "[[file:%s][file:%s]]" file file)
  137. (org-store-link nil))))))
  138. ;; C-u prefix reverses `org-context-in-file-links' in Org buffer.
  139. (should
  140. (let ((org-stored-links nil)
  141. (org-id-link-to-org-use-id nil)
  142. (org-context-in-file-links nil))
  143. (org-test-with-temp-text-in-file "* h1"
  144. (let ((file (buffer-file-name)))
  145. (equal (format "[[file:%s::*h1][h1]]" file)
  146. (org-store-link '(4)))))))
  147. ;; A C-u C-u does *not* reverse `org-context-in-file-links' in Org
  148. ;; buffer.
  149. (should
  150. (let ((org-stored-links nil)
  151. (org-id-link-to-org-use-id nil)
  152. (org-context-in-file-links nil))
  153. (org-test-with-temp-text-in-file "* h1"
  154. (let ((file (buffer-file-name)))
  155. (equal (format "[[file:%s][file:%s]]" file file)
  156. (org-store-link '(16)))))))
  157. ;; Store file link to non-Org buffer, with context.
  158. (should
  159. (let ((org-stored-links nil)
  160. (org-context-in-file-links t))
  161. (org-test-with-temp-text-in-file "one\n<point>two"
  162. (fundamental-mode)
  163. (let ((file (buffer-file-name)))
  164. (equal (format "[[file:%s::one]]" file)
  165. (org-store-link nil))))))
  166. ;; Store file link to non-Org buffer, without context.
  167. (should
  168. (let ((org-stored-links nil)
  169. (org-context-in-file-links nil))
  170. (org-test-with-temp-text-in-file "one\n<point>two"
  171. (fundamental-mode)
  172. (let ((file (buffer-file-name)))
  173. (equal (format "[[file:%s][file:%s]]" file file)
  174. (org-store-link nil))))))
  175. ;; C-u prefix reverses `org-context-in-file-links' in non-Org
  176. ;; buffer.
  177. (should
  178. (let ((org-stored-links nil)
  179. (org-context-in-file-links nil))
  180. (org-test-with-temp-text-in-file "one\n<point>two"
  181. (fundamental-mode)
  182. (let ((file (buffer-file-name)))
  183. (equal (format "[[file:%s::one]]" file)
  184. (org-store-link '(4)))))))
  185. ;; A C-u C-u does *not* reverse `org-context-in-file-links' in
  186. ;; non-Org buffer.
  187. (should
  188. (let ((org-stored-links nil)
  189. (org-context-in-file-links nil))
  190. (org-test-with-temp-text-in-file "one\n<point>two"
  191. (fundamental-mode)
  192. (let ((file (buffer-file-name)))
  193. (equal (format "[[file:%s][file:%s]]" file file)
  194. (org-store-link '(16))))))))
  195. ;;; Radio Targets
  196. (ert-deftest test-ol/update-radio-target-regexp ()
  197. "Test `org-update-radio-target-regexp' specifications."
  198. ;; Properly update cache with no previous radio target regexp.
  199. (should
  200. (eq 'link
  201. (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
  202. (save-excursion (goto-char (point-max)) (org-element-context))
  203. (insert "<<<")
  204. (search-forward "o")
  205. (insert ">>>")
  206. (org-update-radio-target-regexp)
  207. (goto-char (point-max))
  208. (org-element-type (org-element-context)))))
  209. ;; Properly update cache with previous radio target regexp.
  210. (should
  211. (eq 'link
  212. (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
  213. (save-excursion (goto-char (point-max)) (org-element-context))
  214. (insert "<<<")
  215. (search-forward "o")
  216. (insert ">>>")
  217. (org-update-radio-target-regexp)
  218. (search-backward "r")
  219. (delete-char 5)
  220. (insert "new")
  221. (org-update-radio-target-regexp)
  222. (goto-char (point-max))
  223. (delete-region (line-beginning-position) (point))
  224. (insert "new")
  225. (org-element-type (org-element-context))))))
  226. ;;; Navigation
  227. (ert-deftest test-ol/next-link ()
  228. "Test `org-next-link' specifications."
  229. ;; Move to any type of link.
  230. (should
  231. (equal "[[link]]"
  232. (org-test-with-temp-text "foo [[link]]"
  233. (org-next-link)
  234. (buffer-substring (point) (line-end-position)))))
  235. (should
  236. (equal "http://link"
  237. (org-test-with-temp-text "foo http://link"
  238. (org-next-link)
  239. (buffer-substring (point) (line-end-position)))))
  240. (should
  241. (equal "<http://link>"
  242. (org-test-with-temp-text "foo <http://link>"
  243. (org-next-link)
  244. (buffer-substring (point) (line-end-position)))))
  245. ;; Ignore link at point.
  246. (should
  247. (equal "[[link2]]"
  248. (org-test-with-temp-text "[[link1]] [[link2]]"
  249. (org-next-link)
  250. (buffer-substring (point) (line-end-position)))))
  251. ;; Ignore fake links.
  252. (should
  253. (equal "[[truelink]]"
  254. (org-test-with-temp-text "foo\n: [[link]]\n[[truelink]]"
  255. (org-next-link)
  256. (buffer-substring (point) (line-end-position)))))
  257. ;; Do not move point when there is no link.
  258. (should
  259. (org-test-with-temp-text "foo bar"
  260. (org-next-link)
  261. (bobp)))
  262. ;; Wrap around after a failed search.
  263. (should
  264. (equal "[[link]]"
  265. (org-test-with-temp-text "[[link]]\n<point>foo"
  266. (org-next-link)
  267. (let* ((this-command 'org-next-link)
  268. (last-command this-command))
  269. (org-next-link))
  270. (buffer-substring (point) (line-end-position))))))
  271. (ert-deftest test-ol/previous-link ()
  272. "Test `org-previous-link' specifications."
  273. ;; Move to any type of link.
  274. (should
  275. (equal "[[link]]"
  276. (org-test-with-temp-text "[[link]]\nfoo<point>"
  277. (org-previous-link)
  278. (buffer-substring (point) (line-end-position)))))
  279. (should
  280. (equal "http://link"
  281. (org-test-with-temp-text "http://link\nfoo<point>"
  282. (org-previous-link)
  283. (buffer-substring (point) (line-end-position)))))
  284. (should
  285. (equal "<http://link>"
  286. (org-test-with-temp-text "<http://link>\nfoo<point>"
  287. (org-previous-link)
  288. (buffer-substring (point) (line-end-position)))))
  289. ;; Ignore link at point.
  290. (should
  291. (equal "[[link1]]"
  292. (org-test-with-temp-text "[[link1]]\n[[link2<point>]]"
  293. (org-previous-link)
  294. (buffer-substring (point) (line-end-position)))))
  295. ;; Ignore fake links.
  296. (should
  297. (equal "[[truelink]]"
  298. (org-test-with-temp-text "[[truelink]]\n: [[link]]\n<point>"
  299. (org-previous-link)
  300. (buffer-substring (point) (line-end-position)))))
  301. ;; Do not move point when there is no link.
  302. (should
  303. (org-test-with-temp-text "foo bar<point>"
  304. (org-previous-link)
  305. (eobp)))
  306. ;; Wrap around after a failed search.
  307. (should
  308. (equal "[[link]]"
  309. (org-test-with-temp-text "foo\n[[link]]"
  310. (org-previous-link)
  311. (let* ((this-command 'org-previous-link)
  312. (last-command this-command))
  313. (org-previous-link))
  314. (buffer-substring (point) (line-end-position))))))
  315. (provide 'test-ol)
  316. ;;; test-ol.el ends here