test-ol.el 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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 TODO and COMMENT keywords, priority cookie,
  118. ;; and tags.
  119. (should
  120. (let (org-store-link-props org-stored-links)
  121. (org-test-with-temp-text-in-file "* TODO H1"
  122. (let ((file (buffer-file-name)))
  123. (equal (format "[[file:%s::*H1][H1]]" file)
  124. (org-store-link nil))))))
  125. (should
  126. (let (org-store-link-props org-stored-links)
  127. (org-test-with-temp-text-in-file "* COMMENT H1"
  128. (let ((file (buffer-file-name)))
  129. (equal (format "[[file:%s::*H1][H1]]" file)
  130. (org-store-link nil))))))
  131. (should
  132. (let (org-store-link-props org-stored-links)
  133. (org-test-with-temp-text-in-file "* [#A] H1"
  134. (let ((file (buffer-file-name)))
  135. (equal (format "[[file:%s::*H1][H1]]" file)
  136. (org-store-link nil))))))
  137. (should
  138. (let (org-store-link-props org-stored-links)
  139. (org-test-with-temp-text-in-file "* H1 :tag:"
  140. (let ((file (buffer-file-name)))
  141. (equal (format "[[file:%s::*H1][H1]]" file)
  142. (org-store-link nil))))))
  143. ;; On a headline, remove any link from description.
  144. (should
  145. (let (org-store-link-props org-stored-links)
  146. (org-test-with-temp-text-in-file "* [[#l][d]]"
  147. (let ((file (buffer-file-name)))
  148. (equal (format "[[file:%s::*%s][d]]"
  149. file
  150. (org-link-escape "[[#l][d]]"))
  151. (org-store-link nil))))))
  152. (should
  153. (let (org-store-link-props org-stored-links)
  154. (org-test-with-temp-text-in-file "* [[l]]"
  155. (let ((file (buffer-file-name)))
  156. (equal (format "[[file:%s::*%s][l]]" file (org-link-escape "[[l]]"))
  157. (org-store-link nil))))))
  158. (should
  159. (let (org-store-link-props org-stored-links)
  160. (org-test-with-temp-text-in-file "* [[l1][d1]] [[l2][d2]]"
  161. (let ((file (buffer-file-name)))
  162. (equal (format "[[file:%s::*%s][d1 d2]]"
  163. file
  164. (org-link-escape "[[l1][d1]] [[l2][d2]]"))
  165. (org-store-link nil))))))
  166. ;; On a named element, link to that element.
  167. (should
  168. (let (org-store-link-props org-stored-links)
  169. (org-test-with-temp-text-in-file "#+NAME: foo\nParagraph"
  170. (let ((file (buffer-file-name)))
  171. (equal (format "[[file:%s::foo][foo]]" file)
  172. (org-store-link nil))))))
  173. ;; Store link to Org buffer, with context.
  174. (should
  175. (let ((org-stored-links nil)
  176. (org-id-link-to-org-use-id nil)
  177. (org-context-in-file-links t))
  178. (org-test-with-temp-text-in-file "* h1"
  179. (let ((file (buffer-file-name)))
  180. (equal (format "[[file:%s::*h1][h1]]" file)
  181. (org-store-link nil))))))
  182. ;; Store link to Org buffer, without context.
  183. (should
  184. (let ((org-stored-links nil)
  185. (org-id-link-to-org-use-id nil)
  186. (org-context-in-file-links nil))
  187. (org-test-with-temp-text-in-file "* h1"
  188. (let ((file (buffer-file-name)))
  189. (equal (format "[[file:%s][file:%s]]" file file)
  190. (org-store-link nil))))))
  191. ;; C-u prefix reverses `org-context-in-file-links' in Org buffer.
  192. (should
  193. (let ((org-stored-links nil)
  194. (org-id-link-to-org-use-id nil)
  195. (org-context-in-file-links nil))
  196. (org-test-with-temp-text-in-file "* h1"
  197. (let ((file (buffer-file-name)))
  198. (equal (format "[[file:%s::*h1][h1]]" file)
  199. (org-store-link '(4)))))))
  200. ;; A C-u C-u does *not* reverse `org-context-in-file-links' in Org
  201. ;; buffer.
  202. (should
  203. (let ((org-stored-links nil)
  204. (org-id-link-to-org-use-id nil)
  205. (org-context-in-file-links nil))
  206. (org-test-with-temp-text-in-file "* h1"
  207. (let ((file (buffer-file-name)))
  208. (equal (format "[[file:%s][file:%s]]" file file)
  209. (org-store-link '(16)))))))
  210. ;; Store file link to non-Org buffer, with context.
  211. (should
  212. (let ((org-stored-links nil)
  213. (org-link-context-for-files t))
  214. (org-test-with-temp-text-in-file "one\n<point>two"
  215. (fundamental-mode)
  216. (let ((file (buffer-file-name)))
  217. (equal (format "[[file:%s::two]]" file)
  218. (org-store-link nil))))))
  219. ;; Store file link to non-Org buffer, without context.
  220. (should
  221. (let ((org-stored-links nil)
  222. (org-context-in-file-links nil))
  223. (org-test-with-temp-text-in-file "one\n<point>two"
  224. (fundamental-mode)
  225. (let ((file (buffer-file-name)))
  226. (equal (format "[[file:%s][file:%s]]" file file)
  227. (org-store-link nil))))))
  228. ;; C-u prefix reverses `org-context-in-file-links' in non-Org
  229. ;; buffer.
  230. (should
  231. (let ((org-stored-links nil)
  232. (org-link-context-for-files nil))
  233. (org-test-with-temp-text-in-file "one\n<point>two"
  234. (fundamental-mode)
  235. (let ((file (buffer-file-name)))
  236. (equal (format "[[file:%s::two]]" file)
  237. (org-store-link '(4)))))))
  238. ;; A C-u C-u does *not* reverse `org-context-in-file-links' in
  239. ;; non-Org buffer.
  240. (should
  241. (let ((org-stored-links nil)
  242. (org-context-in-file-links nil))
  243. (org-test-with-temp-text-in-file "one\n<point>two"
  244. (fundamental-mode)
  245. (let ((file (buffer-file-name)))
  246. (equal (format "[[file:%s][file:%s]]" file file)
  247. (org-store-link '(16))))))))
  248. ;;; Radio Targets
  249. (ert-deftest test-ol/update-radio-target-regexp ()
  250. "Test `org-update-radio-target-regexp' specifications."
  251. ;; Properly update cache with no previous radio target regexp.
  252. (should
  253. (eq 'link
  254. (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
  255. (save-excursion (goto-char (point-max)) (org-element-context))
  256. (insert "<<<")
  257. (search-forward "o")
  258. (insert ">>>")
  259. (org-update-radio-target-regexp)
  260. (goto-char (point-max))
  261. (org-element-type (org-element-context)))))
  262. ;; Properly update cache with previous radio target regexp.
  263. (should
  264. (eq 'link
  265. (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
  266. (save-excursion (goto-char (point-max)) (org-element-context))
  267. (insert "<<<")
  268. (search-forward "o")
  269. (insert ">>>")
  270. (org-update-radio-target-regexp)
  271. (search-backward "r")
  272. (delete-char 5)
  273. (insert "new")
  274. (org-update-radio-target-regexp)
  275. (goto-char (point-max))
  276. (delete-region (line-beginning-position) (point))
  277. (insert "new")
  278. (org-element-type (org-element-context))))))
  279. ;;; Navigation
  280. (ert-deftest test-ol/next-link ()
  281. "Test `org-next-link' specifications."
  282. ;; Move to any type of link.
  283. (should
  284. (equal "[[link]]"
  285. (org-test-with-temp-text "foo [[link]]"
  286. (org-next-link)
  287. (buffer-substring (point) (line-end-position)))))
  288. (should
  289. (equal "http://link"
  290. (org-test-with-temp-text "foo http://link"
  291. (org-next-link)
  292. (buffer-substring (point) (line-end-position)))))
  293. (should
  294. (equal "<http://link>"
  295. (org-test-with-temp-text "foo <http://link>"
  296. (org-next-link)
  297. (buffer-substring (point) (line-end-position)))))
  298. ;; Ignore link at point.
  299. (should
  300. (equal "[[link2]]"
  301. (org-test-with-temp-text "[[link1]] [[link2]]"
  302. (org-next-link)
  303. (buffer-substring (point) (line-end-position)))))
  304. ;; Ignore fake links.
  305. (should
  306. (equal "[[truelink]]"
  307. (org-test-with-temp-text "foo\n: [[link]]\n[[truelink]]"
  308. (org-next-link)
  309. (buffer-substring (point) (line-end-position)))))
  310. ;; Do not move point when there is no link.
  311. (should
  312. (org-test-with-temp-text "foo bar"
  313. (org-next-link)
  314. (bobp)))
  315. ;; Wrap around after a failed search.
  316. (should
  317. (equal "[[link]]"
  318. (org-test-with-temp-text "[[link]]\n<point>foo"
  319. (org-next-link)
  320. (let* ((this-command 'org-next-link)
  321. (last-command this-command))
  322. (org-next-link))
  323. (buffer-substring (point) (line-end-position)))))
  324. ;; Find links with item tags.
  325. (should
  326. (equal "[[link1]]"
  327. (org-test-with-temp-text "- tag [[link1]] :: description"
  328. (org-next-link)
  329. (buffer-substring (point) (search-forward "]]" nil t))))))
  330. (ert-deftest test-ol/previous-link ()
  331. "Test `org-previous-link' specifications."
  332. ;; Move to any type of link.
  333. (should
  334. (equal "[[link]]"
  335. (org-test-with-temp-text "[[link]]\nfoo<point>"
  336. (org-previous-link)
  337. (buffer-substring (point) (line-end-position)))))
  338. (should
  339. (equal "http://link"
  340. (org-test-with-temp-text "http://link\nfoo<point>"
  341. (org-previous-link)
  342. (buffer-substring (point) (line-end-position)))))
  343. (should
  344. (equal "<http://link>"
  345. (org-test-with-temp-text "<http://link>\nfoo<point>"
  346. (org-previous-link)
  347. (buffer-substring (point) (line-end-position)))))
  348. ;; Ignore link at point.
  349. (should
  350. (equal "[[link1]]"
  351. (org-test-with-temp-text "[[link1]]\n[[link2<point>]]"
  352. (org-previous-link)
  353. (buffer-substring (point) (line-end-position)))))
  354. (should
  355. (equal "[[link1]]"
  356. (org-test-with-temp-text "line\n[[link1]]\n[[link2<point>]]"
  357. (org-previous-link)
  358. (buffer-substring (point) (line-end-position)))))
  359. ;; Ignore fake links.
  360. (should
  361. (equal "[[truelink]]"
  362. (org-test-with-temp-text "[[truelink]]\n: [[link]]\n<point>"
  363. (org-previous-link)
  364. (buffer-substring (point) (line-end-position)))))
  365. ;; Do not move point when there is no link.
  366. (should
  367. (org-test-with-temp-text "foo bar<point>"
  368. (org-previous-link)
  369. (eobp)))
  370. ;; Wrap around after a failed search.
  371. (should
  372. (equal "[[link]]"
  373. (org-test-with-temp-text "foo\n[[link]]"
  374. (org-previous-link)
  375. (let* ((this-command 'org-previous-link)
  376. (last-command this-command))
  377. (org-previous-link))
  378. (buffer-substring (point) (line-end-position))))))
  379. (provide 'test-ol)
  380. ;;; test-ol.el ends here