test-ol.el 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. ;;; Decode and Encode Links
  16. (ert-deftest test-ol/encode ()
  17. "Test `org-link-encode' specifications."
  18. ;; Regural test.
  19. (should (string= "Foo%3A%42ar" (org-link-encode "Foo:Bar" '(?\: ?\B))))
  20. ;; Encode an ASCII character.
  21. (should (string= "%5B" (org-link-encode "[" '(?\[))))
  22. ;; Encode an ASCII control character.
  23. (should (string= "%09" (org-link-encode "\t" '(9))))
  24. ;; Encode a Unicode multibyte character.
  25. (should (string= "%E2%82%AC" (org-link-encode "€" '(?\€)))))
  26. (ert-deftest test-ol/decode ()
  27. "Test `org-link-decode' specifications."
  28. ;; Decode an ASCII character.
  29. (should (string= "[" (org-link-decode "%5B")))
  30. ;; Decode an ASCII control character.
  31. (should (string= "\n" (org-link-decode "%0A")))
  32. ;; Decode a Unicode multibyte character.
  33. (should (string= "€" (org-link-decode "%E2%82%AC"))))
  34. (ert-deftest test-ol/encode-url-with-escaped-char ()
  35. "Encode and decode a URL that includes an encoded char."
  36. (should
  37. (string= "http://some.host.com/form?&id=blah%2Bblah25"
  38. (org-link-decode
  39. (org-link-encode "http://some.host.com/form?&id=blah%2Bblah25"
  40. '(?\s ?\[ ?\] ?%))))))
  41. ;;; Escape and Unescape Links
  42. (ert-deftest test-ol/escape ()
  43. "Test `org-link-escape' specifications."
  44. ;; No-op when there is no backslash or square bracket.
  45. (should (string= "foo" (org-link-escape "foo")))
  46. ;; Escape square brackets at boundaries of the link.
  47. (should (string= "\\[foo\\]" (org-link-escape "[foo]")))
  48. ;; Escape square brackets followed by another square bracket.
  49. (should (string= "foo\\]\\[bar" (org-link-escape "foo][bar")))
  50. (should (string= "foo\\]\\]bar" (org-link-escape "foo]]bar")))
  51. (should (string= "foo\\[\\[bar" (org-link-escape "foo[[bar")))
  52. (should (string= "foo\\[\\]bar" (org-link-escape "foo[]bar")))
  53. ;; Escape backslashes at the end of the link.
  54. (should (string= "foo\\\\" (org-link-escape "foo\\")))
  55. ;; Escape backslashes that could be confused with escaping
  56. ;; characters.
  57. (should (string= "foo\\\\\\]" (org-link-escape "foo\\]")))
  58. (should (string= "foo\\\\\\]\\[" (org-link-escape "foo\\][")))
  59. (should (string= "foo\\\\\\]\\]bar" (org-link-escape "foo\\]]bar")))
  60. ;; Do not escape backslash characters when unnecessary.
  61. (should (string= "foo\\bar" (org-link-escape "foo\\bar")))
  62. ;; Pathological cases: consecutive closing square brackets.
  63. (should (string= "\\[\\[\\[foo\\]\\]\\]" (org-link-escape "[[[foo]]]")))
  64. (should (string= "\\[\\[foo\\]\\] bar" (org-link-escape "[[foo]] bar"))))
  65. (ert-deftest test-ol/unescape ()
  66. "Test `org-link-unescape' specifications."
  67. ;; No-op if there is no backslash.
  68. (should (string= "foo" (org-link-unescape "foo")))
  69. ;; No-op if backslashes are not escaping backslashes.
  70. (should (string= "foo\\bar" (org-link-unescape "foo\\bar")))
  71. ;; Unescape backslashes before square brackets.
  72. (should (string= "foo]bar" (org-link-unescape "foo\\]bar")))
  73. (should (string= "foo\\]" (org-link-unescape "foo\\\\\\]")))
  74. (should (string= "foo\\][" (org-link-unescape "foo\\\\\\][")))
  75. (should (string= "foo\\]]bar" (org-link-unescape "foo\\\\\\]\\]bar")))
  76. (should (string= "foo\\[[bar" (org-link-unescape "foo\\\\\\[\\[bar")))
  77. (should (string= "foo\\[]bar" (org-link-unescape "foo\\\\\\[\\]bar")))
  78. ;; Unescape backslashes at the end of the link.
  79. (should (string= "foo\\" (org-link-unescape "foo\\\\")))
  80. ;; Unescape closing square bracket at boundaries of the link.
  81. (should (string= "[foo]" (org-link-unescape "\\[foo\\]")))
  82. ;; Pathological cases: consecutive closing square brackets.
  83. (should (string= "[[[foo]]]" (org-link-unescape "\\[\\[\\[foo\\]\\]\\]")))
  84. (should (string= "[[foo]] bar" (org-link-unescape "\\[\\[foo\\]\\] bar"))))
  85. (ert-deftest test-ol/make-string ()
  86. "Test `org-link-make-string' specifications."
  87. ;; Throw an error on empty URI.
  88. (should-error (org-link-make-string ""))
  89. ;; Empty description returns a [[URI]] construct.
  90. (should (string= "[[uri]]"(org-link-make-string "uri")))
  91. ;; Non-empty description returns a [[URI][DESCRIPTION]] construct.
  92. (should
  93. (string= "[[uri][description]]"
  94. (org-link-make-string "uri" "description")))
  95. ;; Escape "]]" strings in the description with zero-width spaces.
  96. (should
  97. (let ((zws (string ?\x200B)))
  98. (string= (format "[[uri][foo]%s]bar]]" zws)
  99. (org-link-make-string "uri" "foo]]bar"))))
  100. ;; Prevent description from ending with a closing square bracket
  101. ;; with a zero-width space.
  102. (should
  103. (let ((zws (string ?\x200B)))
  104. (string= (format "[[uri][foo]%s]]" zws)
  105. (org-link-make-string "uri" "foo]")))))
  106. ;;; Store links
  107. (ert-deftest test-ol/store-link ()
  108. "Test `org-store-link' specifications."
  109. ;; On a headline, link to that headline. Use heading as the
  110. ;; description of the link.
  111. (should
  112. (let (org-store-link-props org-stored-links)
  113. (org-test-with-temp-text-in-file "* H1"
  114. (let ((file (buffer-file-name)))
  115. (equal (format "[[file:%s::*H1][H1]]" file)
  116. (org-store-link nil))))))
  117. ;; On a headline, remove any link from description.
  118. (should
  119. (let (org-store-link-props org-stored-links)
  120. (org-test-with-temp-text-in-file "* [[#l][d]]"
  121. (let ((file (buffer-file-name)))
  122. (equal (format "[[file:%s::*%s][d]]"
  123. file
  124. (org-link-escape "[[#l][d]]"))
  125. (org-store-link nil))))))
  126. (should
  127. (let (org-store-link-props org-stored-links)
  128. (org-test-with-temp-text-in-file "* [[l]]"
  129. (let ((file (buffer-file-name)))
  130. (equal (format "[[file:%s::*%s][l]]" file (org-link-escape "[[l]]"))
  131. (org-store-link nil))))))
  132. (should
  133. (let (org-store-link-props org-stored-links)
  134. (org-test-with-temp-text-in-file "* [[l1][d1]] [[l2][d2]]"
  135. (let ((file (buffer-file-name)))
  136. (equal (format "[[file:%s::*%s][d1 d2]]"
  137. file
  138. (org-link-escape "[[l1][d1]] [[l2][d2]]"))
  139. (org-store-link nil))))))
  140. ;; On a named element, link to that element.
  141. (should
  142. (let (org-store-link-props org-stored-links)
  143. (org-test-with-temp-text-in-file "#+NAME: foo\nParagraph"
  144. (let ((file (buffer-file-name)))
  145. (equal (format "[[file:%s::foo][foo]]" file)
  146. (org-store-link nil))))))
  147. ;; Store link to Org buffer, with context.
  148. (should
  149. (let ((org-stored-links nil)
  150. (org-id-link-to-org-use-id nil)
  151. (org-context-in-file-links t))
  152. (org-test-with-temp-text-in-file "* h1"
  153. (let ((file (buffer-file-name)))
  154. (equal (format "[[file:%s::*h1][h1]]" file)
  155. (org-store-link nil))))))
  156. ;; Store link to Org buffer, without context.
  157. (should
  158. (let ((org-stored-links nil)
  159. (org-id-link-to-org-use-id nil)
  160. (org-context-in-file-links nil))
  161. (org-test-with-temp-text-in-file "* h1"
  162. (let ((file (buffer-file-name)))
  163. (equal (format "[[file:%s][file:%s]]" file file)
  164. (org-store-link nil))))))
  165. ;; C-u prefix reverses `org-context-in-file-links' in Org buffer.
  166. (should
  167. (let ((org-stored-links nil)
  168. (org-id-link-to-org-use-id nil)
  169. (org-context-in-file-links nil))
  170. (org-test-with-temp-text-in-file "* h1"
  171. (let ((file (buffer-file-name)))
  172. (equal (format "[[file:%s::*h1][h1]]" file)
  173. (org-store-link '(4)))))))
  174. ;; A C-u C-u does *not* reverse `org-context-in-file-links' in Org
  175. ;; buffer.
  176. (should
  177. (let ((org-stored-links nil)
  178. (org-id-link-to-org-use-id nil)
  179. (org-context-in-file-links nil))
  180. (org-test-with-temp-text-in-file "* h1"
  181. (let ((file (buffer-file-name)))
  182. (equal (format "[[file:%s][file:%s]]" file file)
  183. (org-store-link '(16)))))))
  184. ;; Store file link to non-Org buffer, with context.
  185. (should
  186. (let ((org-stored-links nil)
  187. (org-link-context-for-files t))
  188. (org-test-with-temp-text-in-file "one\n<point>two"
  189. (fundamental-mode)
  190. (let ((file (buffer-file-name)))
  191. (equal (format "[[file:%s::two]]" file)
  192. (org-store-link nil))))))
  193. ;; Store file link to non-Org buffer, without context.
  194. (should
  195. (let ((org-stored-links nil)
  196. (org-context-in-file-links nil))
  197. (org-test-with-temp-text-in-file "one\n<point>two"
  198. (fundamental-mode)
  199. (let ((file (buffer-file-name)))
  200. (equal (format "[[file:%s][file:%s]]" file file)
  201. (org-store-link nil))))))
  202. ;; C-u prefix reverses `org-context-in-file-links' in non-Org
  203. ;; buffer.
  204. (should
  205. (let ((org-stored-links nil)
  206. (org-link-context-for-files nil))
  207. (org-test-with-temp-text-in-file "one\n<point>two"
  208. (fundamental-mode)
  209. (let ((file (buffer-file-name)))
  210. (equal (format "[[file:%s::two]]" file)
  211. (org-store-link '(4)))))))
  212. ;; A C-u C-u does *not* reverse `org-context-in-file-links' in
  213. ;; non-Org buffer.
  214. (should
  215. (let ((org-stored-links nil)
  216. (org-context-in-file-links nil))
  217. (org-test-with-temp-text-in-file "one\n<point>two"
  218. (fundamental-mode)
  219. (let ((file (buffer-file-name)))
  220. (equal (format "[[file:%s][file:%s]]" file file)
  221. (org-store-link '(16))))))))
  222. ;;; Radio Targets
  223. (ert-deftest test-ol/update-radio-target-regexp ()
  224. "Test `org-update-radio-target-regexp' specifications."
  225. ;; Properly update cache with no previous radio target regexp.
  226. (should
  227. (eq 'link
  228. (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
  229. (save-excursion (goto-char (point-max)) (org-element-context))
  230. (insert "<<<")
  231. (search-forward "o")
  232. (insert ">>>")
  233. (org-update-radio-target-regexp)
  234. (goto-char (point-max))
  235. (org-element-type (org-element-context)))))
  236. ;; Properly update cache with previous radio target regexp.
  237. (should
  238. (eq 'link
  239. (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
  240. (save-excursion (goto-char (point-max)) (org-element-context))
  241. (insert "<<<")
  242. (search-forward "o")
  243. (insert ">>>")
  244. (org-update-radio-target-regexp)
  245. (search-backward "r")
  246. (delete-char 5)
  247. (insert "new")
  248. (org-update-radio-target-regexp)
  249. (goto-char (point-max))
  250. (delete-region (line-beginning-position) (point))
  251. (insert "new")
  252. (org-element-type (org-element-context))))))
  253. ;;; Navigation
  254. (ert-deftest test-ol/next-link ()
  255. "Test `org-next-link' specifications."
  256. ;; Move to any type of link.
  257. (should
  258. (equal "[[link]]"
  259. (org-test-with-temp-text "foo [[link]]"
  260. (org-next-link)
  261. (buffer-substring (point) (line-end-position)))))
  262. (should
  263. (equal "http://link"
  264. (org-test-with-temp-text "foo http://link"
  265. (org-next-link)
  266. (buffer-substring (point) (line-end-position)))))
  267. (should
  268. (equal "<http://link>"
  269. (org-test-with-temp-text "foo <http://link>"
  270. (org-next-link)
  271. (buffer-substring (point) (line-end-position)))))
  272. ;; Ignore link at point.
  273. (should
  274. (equal "[[link2]]"
  275. (org-test-with-temp-text "[[link1]] [[link2]]"
  276. (org-next-link)
  277. (buffer-substring (point) (line-end-position)))))
  278. ;; Ignore fake links.
  279. (should
  280. (equal "[[truelink]]"
  281. (org-test-with-temp-text "foo\n: [[link]]\n[[truelink]]"
  282. (org-next-link)
  283. (buffer-substring (point) (line-end-position)))))
  284. ;; Do not move point when there is no link.
  285. (should
  286. (org-test-with-temp-text "foo bar"
  287. (org-next-link)
  288. (bobp)))
  289. ;; Wrap around after a failed search.
  290. (should
  291. (equal "[[link]]"
  292. (org-test-with-temp-text "[[link]]\n<point>foo"
  293. (org-next-link)
  294. (let* ((this-command 'org-next-link)
  295. (last-command this-command))
  296. (org-next-link))
  297. (buffer-substring (point) (line-end-position)))))
  298. ;; Find links with item tags.
  299. (should
  300. (equal "[[link1]]"
  301. (org-test-with-temp-text "- tag [[link1]] :: description"
  302. (org-next-link)
  303. (buffer-substring (point) (search-forward "]]" nil t))))))
  304. (ert-deftest test-ol/previous-link ()
  305. "Test `org-previous-link' specifications."
  306. ;; Move to any type of link.
  307. (should
  308. (equal "[[link]]"
  309. (org-test-with-temp-text "[[link]]\nfoo<point>"
  310. (org-previous-link)
  311. (buffer-substring (point) (line-end-position)))))
  312. (should
  313. (equal "http://link"
  314. (org-test-with-temp-text "http://link\nfoo<point>"
  315. (org-previous-link)
  316. (buffer-substring (point) (line-end-position)))))
  317. (should
  318. (equal "<http://link>"
  319. (org-test-with-temp-text "<http://link>\nfoo<point>"
  320. (org-previous-link)
  321. (buffer-substring (point) (line-end-position)))))
  322. ;; Ignore link at point.
  323. (should
  324. (equal "[[link1]]"
  325. (org-test-with-temp-text "[[link1]]\n[[link2<point>]]"
  326. (org-previous-link)
  327. (buffer-substring (point) (line-end-position)))))
  328. (should
  329. (equal "[[link1]]"
  330. (org-test-with-temp-text "line\n[[link1]]\n[[link2<point>]]"
  331. (org-previous-link)
  332. (buffer-substring (point) (line-end-position)))))
  333. ;; Ignore fake links.
  334. (should
  335. (equal "[[truelink]]"
  336. (org-test-with-temp-text "[[truelink]]\n: [[link]]\n<point>"
  337. (org-previous-link)
  338. (buffer-substring (point) (line-end-position)))))
  339. ;; Do not move point when there is no link.
  340. (should
  341. (org-test-with-temp-text "foo bar<point>"
  342. (org-previous-link)
  343. (eobp)))
  344. ;; Wrap around after a failed search.
  345. (should
  346. (equal "[[link]]"
  347. (org-test-with-temp-text "foo\n[[link]]"
  348. (org-previous-link)
  349. (let* ((this-command 'org-previous-link)
  350. (last-command this-command))
  351. (org-previous-link))
  352. (buffer-substring (point) (line-end-position))))))
  353. (provide 'test-ol)
  354. ;;; test-ol.el ends here