test-ol.el 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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. ;; Context does not include special search syntax.
  249. (should
  250. (let ((org-stored-links nil)
  251. (org-context-in-file-links t))
  252. (org-test-with-temp-text-in-file "(two)"
  253. (fundamental-mode)
  254. (let ((file (buffer-file-name)))
  255. (equal (format "[[file:%s::two]]" file file)
  256. (org-store-link nil))))))
  257. (should
  258. (let ((org-stored-links nil)
  259. (org-context-in-file-links t))
  260. (org-test-with-temp-text-in-file "# two"
  261. (fundamental-mode)
  262. (let ((file (buffer-file-name)))
  263. (equal (format "[[file:%s::two]]" file file)
  264. (org-store-link nil))))))
  265. (should
  266. (let ((org-stored-links nil)
  267. (org-context-in-file-links t))
  268. (org-test-with-temp-text-in-file "*two"
  269. (fundamental-mode)
  270. (let ((file (buffer-file-name)))
  271. (equal (format "[[file:%s::two]]" file file)
  272. (org-store-link nil))))))
  273. (should
  274. (let ((org-stored-links nil)
  275. (org-context-in-file-links t))
  276. (org-test-with-temp-text-in-file "( two )"
  277. (fundamental-mode)
  278. (let ((file (buffer-file-name)))
  279. (equal (format "[[file:%s::two]]" file file)
  280. (org-store-link nil))))))
  281. (should
  282. (let ((org-stored-links nil)
  283. (org-context-in-file-links t))
  284. (org-test-with-temp-text-in-file "# two"
  285. (fundamental-mode)
  286. (let ((file (buffer-file-name)))
  287. (equal (format "[[file:%s::two]]" file file)
  288. (org-store-link nil))))))
  289. (should
  290. (let ((org-stored-links nil)
  291. (org-context-in-file-links t))
  292. (org-test-with-temp-text-in-file "#( two )"
  293. (fundamental-mode)
  294. (let ((file (buffer-file-name)))
  295. (equal (format "[[file:%s::two]]" file file)
  296. (org-store-link nil))))))
  297. (should
  298. (let ((org-stored-links nil)
  299. (org-context-in-file-links t))
  300. (org-test-with-temp-text-in-file "#** ((## two) )"
  301. (fundamental-mode)
  302. (let ((file (buffer-file-name)))
  303. (equal (format "[[file:%s::two]]" file file)
  304. (org-store-link nil))))))
  305. (should-not
  306. (let ((org-stored-links nil)
  307. (org-context-in-file-links t))
  308. (org-test-with-temp-text-in-file "(two"
  309. (fundamental-mode)
  310. (let ((file (buffer-file-name)))
  311. (equal (format "[[file:%s::two]]" file file)
  312. (org-store-link nil))))))
  313. ;; Context also ignore statistics cookies and special headlines
  314. ;; data.
  315. (should
  316. (let ((org-stored-links nil)
  317. (org-context-in-file-links t))
  318. (org-test-with-temp-text-in-file "* TODO [#A] COMMENT foo :bar:"
  319. (let ((file (buffer-file-name)))
  320. (equal (format "[[file:%s::*foo][foo]]" file file)
  321. (org-store-link nil))))))
  322. (should
  323. (let ((org-stored-links nil)
  324. (org-context-in-file-links t))
  325. (org-test-with-temp-text-in-file "* foo[33%]bar"
  326. (let ((file (buffer-file-name)))
  327. (equal (format "[[file:%s::*foo bar][foo bar]]" file file)
  328. (org-store-link nil))))))
  329. (should
  330. (let ((org-stored-links nil)
  331. (org-context-in-file-links t))
  332. (org-test-with-temp-text-in-file "* [%][/] foo [35%] bar[3/5]"
  333. (let ((file (buffer-file-name)))
  334. (equal (format "[[file:%s::*foo bar][foo bar]]" file file)
  335. (org-store-link nil)))))))
  336. ;;; Radio Targets
  337. (ert-deftest test-ol/update-radio-target-regexp ()
  338. "Test `org-update-radio-target-regexp' specifications."
  339. ;; Properly update cache with no previous radio target regexp.
  340. (should
  341. (eq 'link
  342. (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
  343. (save-excursion (goto-char (point-max)) (org-element-context))
  344. (insert "<<<")
  345. (search-forward "o")
  346. (insert ">>>")
  347. (org-update-radio-target-regexp)
  348. (goto-char (point-max))
  349. (org-element-type (org-element-context)))))
  350. ;; Properly update cache with previous radio target regexp.
  351. (should
  352. (eq 'link
  353. (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
  354. (save-excursion (goto-char (point-max)) (org-element-context))
  355. (insert "<<<")
  356. (search-forward "o")
  357. (insert ">>>")
  358. (org-update-radio-target-regexp)
  359. (search-backward "r")
  360. (delete-char 5)
  361. (insert "new")
  362. (org-update-radio-target-regexp)
  363. (goto-char (point-max))
  364. (delete-region (line-beginning-position) (point))
  365. (insert "new")
  366. (org-element-type (org-element-context))))))
  367. ;;; Navigation
  368. (ert-deftest test-ol/next-link ()
  369. "Test `org-next-link' specifications."
  370. ;; Move to any type of link.
  371. (should
  372. (equal "[[link]]"
  373. (org-test-with-temp-text "foo [[link]]"
  374. (org-next-link)
  375. (buffer-substring (point) (line-end-position)))))
  376. (should
  377. (equal "http://link"
  378. (org-test-with-temp-text "foo http://link"
  379. (org-next-link)
  380. (buffer-substring (point) (line-end-position)))))
  381. (should
  382. (equal "<http://link>"
  383. (org-test-with-temp-text "foo <http://link>"
  384. (org-next-link)
  385. (buffer-substring (point) (line-end-position)))))
  386. ;; Ignore link at point.
  387. (should
  388. (equal "[[link2]]"
  389. (org-test-with-temp-text "[[link1]] [[link2]]"
  390. (org-next-link)
  391. (buffer-substring (point) (line-end-position)))))
  392. ;; Ignore fake links.
  393. (should
  394. (equal "[[truelink]]"
  395. (org-test-with-temp-text "foo\n: [[link]]\n[[truelink]]"
  396. (org-next-link)
  397. (buffer-substring (point) (line-end-position)))))
  398. ;; Do not move point when there is no link.
  399. (should
  400. (org-test-with-temp-text "foo bar"
  401. (org-next-link)
  402. (bobp)))
  403. ;; Wrap around after a failed search.
  404. (should
  405. (equal "[[link]]"
  406. (org-test-with-temp-text "[[link]]\n<point>foo"
  407. (org-next-link)
  408. (let* ((this-command 'org-next-link)
  409. (last-command this-command))
  410. (org-next-link))
  411. (buffer-substring (point) (line-end-position)))))
  412. ;; Find links with item tags.
  413. (should
  414. (equal "[[link1]]"
  415. (org-test-with-temp-text "- tag [[link1]] :: description"
  416. (org-next-link)
  417. (buffer-substring (point) (search-forward "]]" nil t))))))
  418. (ert-deftest test-ol/previous-link ()
  419. "Test `org-previous-link' specifications."
  420. ;; Move to any type of link.
  421. (should
  422. (equal "[[link]]"
  423. (org-test-with-temp-text "[[link]]\nfoo<point>"
  424. (org-previous-link)
  425. (buffer-substring (point) (line-end-position)))))
  426. (should
  427. (equal "http://link"
  428. (org-test-with-temp-text "http://link\nfoo<point>"
  429. (org-previous-link)
  430. (buffer-substring (point) (line-end-position)))))
  431. (should
  432. (equal "<http://link>"
  433. (org-test-with-temp-text "<http://link>\nfoo<point>"
  434. (org-previous-link)
  435. (buffer-substring (point) (line-end-position)))))
  436. ;; Ignore link at point.
  437. (should
  438. (equal "[[link1]]"
  439. (org-test-with-temp-text "[[link1]]\n[[link2<point>]]"
  440. (org-previous-link)
  441. (buffer-substring (point) (line-end-position)))))
  442. (should
  443. (equal "[[link1]]"
  444. (org-test-with-temp-text "line\n[[link1]]\n[[link2<point>]]"
  445. (org-previous-link)
  446. (buffer-substring (point) (line-end-position)))))
  447. ;; Ignore fake links.
  448. (should
  449. (equal "[[truelink]]"
  450. (org-test-with-temp-text "[[truelink]]\n: [[link]]\n<point>"
  451. (org-previous-link)
  452. (buffer-substring (point) (line-end-position)))))
  453. ;; Do not move point when there is no link.
  454. (should
  455. (org-test-with-temp-text "foo bar<point>"
  456. (org-previous-link)
  457. (eobp)))
  458. ;; Wrap around after a failed search.
  459. (should
  460. (equal "[[link]]"
  461. (org-test-with-temp-text "foo\n[[link]]"
  462. (org-previous-link)
  463. (let* ((this-command 'org-previous-link)
  464. (last-command this-command))
  465. (org-previous-link))
  466. (buffer-substring (point) (line-end-position))))))
  467. (provide 'test-ol)
  468. ;;; test-ol.el ends here