test-ol.el 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  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. (require 'cl-lib)
  16. (require 'ol)
  17. (require 'org-id)
  18. ;;; Decode and Encode Links
  19. (ert-deftest test-org-link/encode ()
  20. "Test `org-link-encode' specifications."
  21. ;; Regural test.
  22. (should (string= "Foo%3A%42ar" (org-link-encode "Foo:Bar" '(?\: ?\B))))
  23. ;; Encode an ASCII character.
  24. (should (string= "%5B" (org-link-encode "[" '(?\[))))
  25. ;; Encode an ASCII control character.
  26. (should (string= "%09" (org-link-encode "\t" '(9))))
  27. ;; Encode a Unicode multibyte character.
  28. (should (string= "%E2%82%AC" (org-link-encode "€" '(?\€)))))
  29. (ert-deftest test-org-link/decode ()
  30. "Test `org-link-decode' specifications."
  31. ;; Decode an ASCII character.
  32. (should (string= "[" (org-link-decode "%5B")))
  33. ;; Decode an ASCII control character.
  34. (should (string= "\n" (org-link-decode "%0A")))
  35. ;; Decode a Unicode multibyte character.
  36. (should (string= "€" (org-link-decode "%E2%82%AC"))))
  37. (ert-deftest test-org-link/encode-url-with-escaped-char ()
  38. "Encode and decode a URL that includes an encoded char."
  39. (should
  40. (string= "http://some.host.com/form?&id=blah%2Bblah25"
  41. (org-link-decode
  42. (org-link-encode "http://some.host.com/form?&id=blah%2Bblah25"
  43. '(?\s ?\[ ?\] ?%))))))
  44. (ert-deftest test-org-link/toggle-link-display ()
  45. "Make sure that `org-toggle-link-display' is working.
  46. See https://github.com/yantar92/org/issues/4."
  47. (dolist (org-link-descriptive '(nil t))
  48. (org-test-with-temp-text "* Org link test
  49. [[https://example.com][A link to a site]]"
  50. (dotimes (_ 2)
  51. (goto-char 1)
  52. (re-search-forward "\\[")
  53. (should-not (org-xor org-link-descriptive (org-invisible-p)))
  54. (re-search-forward "example")
  55. (should-not (org-xor org-link-descriptive (org-invisible-p)))
  56. (re-search-forward "com")
  57. (should-not (org-xor org-link-descriptive (org-invisible-p)))
  58. (re-search-forward "]")
  59. (should-not (org-xor org-link-descriptive (org-invisible-p)))
  60. (re-search-forward "\\[")
  61. (should-not (org-invisible-p))
  62. (re-search-forward "link")
  63. (should-not (org-invisible-p))
  64. (re-search-forward "]")
  65. (should-not (org-xor org-link-descriptive (org-invisible-p)))
  66. (org-toggle-link-display)))))
  67. ;;; Escape and Unescape Links
  68. (ert-deftest test-org-link/escape ()
  69. "Test `org-link-escape' specifications."
  70. ;; No-op when there is no backslash or square bracket.
  71. (should (string= "foo" (org-link-escape "foo")))
  72. ;; Escape square brackets at boundaries of the link.
  73. (should (string= "\\[foo\\]" (org-link-escape "[foo]")))
  74. ;; Escape square brackets followed by another square bracket.
  75. (should (string= "foo\\]\\[bar" (org-link-escape "foo][bar")))
  76. (should (string= "foo\\]\\]bar" (org-link-escape "foo]]bar")))
  77. (should (string= "foo\\[\\[bar" (org-link-escape "foo[[bar")))
  78. (should (string= "foo\\[\\]bar" (org-link-escape "foo[]bar")))
  79. ;; Escape backslashes at the end of the link.
  80. (should (string= "foo\\\\" (org-link-escape "foo\\")))
  81. ;; Escape backslashes that could be confused with escaping
  82. ;; characters.
  83. (should (string= "foo\\\\\\]" (org-link-escape "foo\\]")))
  84. (should (string= "foo\\\\\\]\\[" (org-link-escape "foo\\][")))
  85. (should (string= "foo\\\\\\]\\]bar" (org-link-escape "foo\\]]bar")))
  86. ;; Do not escape backslash characters when unnecessary.
  87. (should (string= "foo\\bar" (org-link-escape "foo\\bar")))
  88. ;; Pathological cases: consecutive closing square brackets.
  89. (should (string= "\\[\\[\\[foo\\]\\]\\]" (org-link-escape "[[[foo]]]")))
  90. (should (string= "\\[\\[foo\\]\\] bar" (org-link-escape "[[foo]] bar"))))
  91. (ert-deftest test-org-link/unescape ()
  92. "Test `org-link-unescape' specifications."
  93. ;; No-op if there is no backslash.
  94. (should (string= "foo" (org-link-unescape "foo")))
  95. ;; No-op if backslashes are not escaping backslashes.
  96. (should (string= "foo\\bar" (org-link-unescape "foo\\bar")))
  97. ;; Unescape backslashes before square brackets.
  98. (should (string= "foo]bar" (org-link-unescape "foo\\]bar")))
  99. (should (string= "foo\\]" (org-link-unescape "foo\\\\\\]")))
  100. (should (string= "foo\\][" (org-link-unescape "foo\\\\\\][")))
  101. (should (string= "foo\\]]bar" (org-link-unescape "foo\\\\\\]\\]bar")))
  102. (should (string= "foo\\[[bar" (org-link-unescape "foo\\\\\\[\\[bar")))
  103. (should (string= "foo\\[]bar" (org-link-unescape "foo\\\\\\[\\]bar")))
  104. ;; Unescape backslashes at the end of the link.
  105. (should (string= "foo\\" (org-link-unescape "foo\\\\")))
  106. ;; Unescape closing square bracket at boundaries of the link.
  107. (should (string= "[foo]" (org-link-unescape "\\[foo\\]")))
  108. ;; Pathological cases: consecutive closing square brackets.
  109. (should (string= "[[[foo]]]" (org-link-unescape "\\[\\[\\[foo\\]\\]\\]")))
  110. (should (string= "[[foo]] bar" (org-link-unescape "\\[\\[foo\\]\\] bar"))))
  111. (ert-deftest test-org-link/make-string ()
  112. "Test `org-link-make-string' specifications."
  113. ;; Throw an error on empty URI.
  114. (should-error (org-link-make-string ""))
  115. ;; Empty description returns a [[URI]] construct.
  116. (should (string= "[[uri]]"(org-link-make-string "uri")))
  117. ;; Non-empty description returns a [[URI][DESCRIPTION]] construct.
  118. (should
  119. (string= "[[uri][description]]"
  120. (org-link-make-string "uri" "description")))
  121. ;; Escape "]]" strings in the description with zero-width spaces.
  122. (should
  123. (let ((zws (string ?\x200B)))
  124. (string= (format "[[uri][foo]%s]bar]]" zws)
  125. (org-link-make-string "uri" "foo]]bar"))))
  126. ;; Prevent description from ending with a closing square bracket
  127. ;; with a zero-width space.
  128. (should
  129. (let ((zws (string ?\x200B)))
  130. (string= (format "[[uri][foo]%s]]" zws)
  131. (org-link-make-string "uri" "foo]")))))
  132. ;;; Store links
  133. (ert-deftest test-org-link/store-link ()
  134. "Test `org-store-link' specifications."
  135. ;; On a headline, link to that headline. Use heading as the
  136. ;; description of the link.
  137. (should
  138. (let (org-store-link-props org-stored-links)
  139. (org-test-with-temp-text-in-file "* H1"
  140. (let ((file (buffer-file-name)))
  141. (equal (format "[[file:%s::*H1][H1]]" file)
  142. (org-store-link nil))))))
  143. ;; On a headline, remove TODO and COMMENT keywords, priority cookie,
  144. ;; and tags.
  145. (should
  146. (let (org-store-link-props org-stored-links)
  147. (org-test-with-temp-text-in-file "* TODO H1"
  148. (let ((file (buffer-file-name)))
  149. (equal (format "[[file:%s::*H1][H1]]" file)
  150. (org-store-link nil))))))
  151. (should
  152. (let (org-store-link-props org-stored-links)
  153. (org-test-with-temp-text-in-file "* COMMENT H1"
  154. (let ((file (buffer-file-name)))
  155. (equal (format "[[file:%s::*H1][H1]]" file)
  156. (org-store-link nil))))))
  157. (should
  158. (let (org-store-link-props org-stored-links)
  159. (org-test-with-temp-text-in-file "* [#A] H1"
  160. (let ((file (buffer-file-name)))
  161. (equal (format "[[file:%s::*H1][H1]]" file)
  162. (org-store-link nil))))))
  163. (should
  164. (let (org-store-link-props org-stored-links)
  165. (org-test-with-temp-text-in-file "* H1 :tag:"
  166. (let ((file (buffer-file-name)))
  167. (equal (format "[[file:%s::*H1][H1]]" file)
  168. (org-store-link nil))))))
  169. ;; On a headline, remove any link from description.
  170. (should
  171. (let (org-store-link-props org-stored-links)
  172. (org-test-with-temp-text-in-file "* [[#l][d]]"
  173. (let ((file (buffer-file-name)))
  174. (equal (format "[[file:%s::*%s][d]]"
  175. file
  176. (org-link-escape "[[#l][d]]"))
  177. (org-store-link nil))))))
  178. (should
  179. (let (org-store-link-props org-stored-links)
  180. (org-test-with-temp-text-in-file "* [[l]]"
  181. (let ((file (buffer-file-name)))
  182. (equal (format "[[file:%s::*%s][l]]" file (org-link-escape "[[l]]"))
  183. (org-store-link nil))))))
  184. (should
  185. (let (org-store-link-props org-stored-links)
  186. (org-test-with-temp-text-in-file "* [[l1][d1]] [[l2][d2]]"
  187. (let ((file (buffer-file-name)))
  188. (equal (format "[[file:%s::*%s][d1 d2]]"
  189. file
  190. (org-link-escape "[[l1][d1]] [[l2][d2]]"))
  191. (org-store-link nil))))))
  192. ;; On a named element, link to that element.
  193. (should
  194. (let (org-store-link-props org-stored-links)
  195. (org-test-with-temp-text-in-file "#+NAME: foo\nParagraph"
  196. (let ((file (buffer-file-name)))
  197. (equal (format "[[file:%s::foo][foo]]" file)
  198. (org-store-link nil))))))
  199. ;; Store link to Org buffer, with context.
  200. (should
  201. (let ((org-stored-links nil)
  202. (org-id-link-to-org-use-id nil)
  203. (org-context-in-file-links t))
  204. (org-test-with-temp-text-in-file "* h1"
  205. (let ((file (buffer-file-name)))
  206. (equal (format "[[file:%s::*h1][h1]]" file)
  207. (org-store-link nil))))))
  208. ;; Store link to Org buffer, without context.
  209. (should
  210. (let ((org-stored-links nil)
  211. (org-id-link-to-org-use-id nil)
  212. (org-context-in-file-links nil))
  213. (org-test-with-temp-text-in-file "* h1"
  214. (let ((file (buffer-file-name)))
  215. (equal (format "[[file:%s]]" file file)
  216. (org-store-link nil))))))
  217. ;; C-u prefix reverses `org-context-in-file-links' in Org buffer.
  218. (should
  219. (let ((org-stored-links nil)
  220. (org-id-link-to-org-use-id nil)
  221. (org-context-in-file-links nil))
  222. (org-test-with-temp-text-in-file "* h1"
  223. (let ((file (buffer-file-name)))
  224. (equal (format "[[file:%s::*h1][h1]]" file)
  225. (org-store-link '(4)))))))
  226. ;; A C-u C-u does *not* reverse `org-context-in-file-links' in Org
  227. ;; buffer.
  228. (should
  229. (let ((org-stored-links nil)
  230. (org-id-link-to-org-use-id nil)
  231. (org-context-in-file-links nil))
  232. (org-test-with-temp-text-in-file "* h1"
  233. (let ((file (buffer-file-name)))
  234. (equal (format "[[file:%s]]" file file)
  235. (org-store-link '(16)))))))
  236. ;; Store file link to non-Org buffer, with context.
  237. (should
  238. (let ((org-stored-links nil)
  239. (org-link-context-for-files t))
  240. (org-test-with-temp-text-in-file "one\n<point>two"
  241. (fundamental-mode)
  242. (let ((file (buffer-file-name)))
  243. (equal (format "[[file:%s::two]]" file)
  244. (org-store-link nil))))))
  245. ;; Store file link to non-Org buffer, without context.
  246. (should
  247. (let ((org-stored-links nil)
  248. (org-context-in-file-links nil))
  249. (org-test-with-temp-text-in-file "one\n<point>two"
  250. (fundamental-mode)
  251. (let ((file (buffer-file-name)))
  252. (equal (format "[[file:%s]]" file file)
  253. (org-store-link nil))))))
  254. ;; C-u prefix reverses `org-context-in-file-links' in non-Org
  255. ;; buffer.
  256. (should
  257. (let ((org-stored-links nil)
  258. (org-link-context-for-files nil))
  259. (org-test-with-temp-text-in-file "one\n<point>two"
  260. (fundamental-mode)
  261. (let ((file (buffer-file-name)))
  262. (equal (format "[[file:%s::two]]" file)
  263. (org-store-link '(4)))))))
  264. ;; A C-u C-u does *not* reverse `org-context-in-file-links' in
  265. ;; non-Org buffer.
  266. (should
  267. (let ((org-stored-links nil)
  268. (org-context-in-file-links nil))
  269. (org-test-with-temp-text-in-file "one\n<point>two"
  270. (fundamental-mode)
  271. (let ((file (buffer-file-name)))
  272. (equal (format "[[file:%s]]" file file)
  273. (org-store-link '(16)))))))
  274. ;; Context does not include special search syntax.
  275. (should
  276. (let ((org-stored-links nil)
  277. (org-context-in-file-links t))
  278. (org-test-with-temp-text-in-file "(two)"
  279. (fundamental-mode)
  280. (let ((file (buffer-file-name)))
  281. (equal (format "[[file:%s::two]]" file file)
  282. (org-store-link nil))))))
  283. (should
  284. (let ((org-stored-links nil)
  285. (org-context-in-file-links t))
  286. (org-test-with-temp-text-in-file "# two"
  287. (fundamental-mode)
  288. (let ((file (buffer-file-name)))
  289. (equal (format "[[file:%s::two]]" file file)
  290. (org-store-link nil))))))
  291. (should
  292. (let ((org-stored-links nil)
  293. (org-context-in-file-links t))
  294. (org-test-with-temp-text-in-file "*two"
  295. (fundamental-mode)
  296. (let ((file (buffer-file-name)))
  297. (equal (format "[[file:%s::two]]" file file)
  298. (org-store-link nil))))))
  299. (should
  300. (let ((org-stored-links nil)
  301. (org-context-in-file-links t))
  302. (org-test-with-temp-text-in-file "( two )"
  303. (fundamental-mode)
  304. (let ((file (buffer-file-name)))
  305. (equal (format "[[file:%s::two]]" file file)
  306. (org-store-link nil))))))
  307. (should
  308. (let ((org-stored-links nil)
  309. (org-context-in-file-links t))
  310. (org-test-with-temp-text-in-file "# two"
  311. (fundamental-mode)
  312. (let ((file (buffer-file-name)))
  313. (equal (format "[[file:%s::two]]" file file)
  314. (org-store-link nil))))))
  315. (should
  316. (let ((org-stored-links nil)
  317. (org-context-in-file-links t))
  318. (org-test-with-temp-text-in-file "#( two )"
  319. (fundamental-mode)
  320. (let ((file (buffer-file-name)))
  321. (equal (format "[[file:%s::two]]" file file)
  322. (org-store-link nil))))))
  323. (should
  324. (let ((org-stored-links nil)
  325. (org-context-in-file-links t))
  326. (org-test-with-temp-text-in-file "#** ((## two) )"
  327. (fundamental-mode)
  328. (let ((file (buffer-file-name)))
  329. (equal (format "[[file:%s::two]]" file file)
  330. (org-store-link nil))))))
  331. (should-not
  332. (let ((org-stored-links nil)
  333. (org-context-in-file-links t))
  334. (org-test-with-temp-text-in-file "(two"
  335. (fundamental-mode)
  336. (let ((file (buffer-file-name)))
  337. (equal (format "[[file:%s::two]]" file file)
  338. (org-store-link nil))))))
  339. ;; Context also ignore statistics cookies and special headlines
  340. ;; data.
  341. (should
  342. (let ((org-stored-links nil)
  343. (org-context-in-file-links t))
  344. (org-test-with-temp-text-in-file "* TODO [#A] COMMENT foo :bar:"
  345. (let ((file (buffer-file-name)))
  346. (equal (format "[[file:%s::*foo][foo]]" file file)
  347. (org-store-link nil))))))
  348. (should
  349. (let ((org-stored-links nil)
  350. (org-context-in-file-links t))
  351. (org-test-with-temp-text-in-file "* foo[33%]bar"
  352. (let ((file (buffer-file-name)))
  353. (equal (format "[[file:%s::*foo bar][foo bar]]" file file)
  354. (org-store-link nil))))))
  355. (should
  356. (let ((org-stored-links nil)
  357. (org-context-in-file-links t))
  358. (org-test-with-temp-text-in-file "* [%][/] foo [35%] bar[3/5]"
  359. (let ((file (buffer-file-name)))
  360. (equal (format "[[file:%s::*foo bar][foo bar]]" file file)
  361. (org-store-link nil)))))))
  362. ;;; Radio Targets
  363. (ert-deftest test-org-link/update-radio-target-regexp ()
  364. "Test `org-update-radio-target-regexp' specifications."
  365. ;; Properly update cache with no previous radio target regexp.
  366. (should
  367. (eq 'link
  368. (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
  369. (save-excursion (goto-char (point-max)) (org-element-context))
  370. (insert "<<<")
  371. (search-forward "o")
  372. (insert ">>>")
  373. (org-update-radio-target-regexp)
  374. (goto-char (point-max))
  375. (org-element-type (org-element-context)))))
  376. ;; Properly update cache with previous radio target regexp.
  377. (should
  378. (eq 'link
  379. (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
  380. (save-excursion (goto-char (point-max)) (org-element-context))
  381. (insert "<<<")
  382. (search-forward "o")
  383. (insert ">>>")
  384. (org-update-radio-target-regexp)
  385. (search-backward "r")
  386. (delete-char 5)
  387. (insert "new")
  388. (org-update-radio-target-regexp)
  389. (goto-char (point-max))
  390. (delete-region (line-beginning-position) (point))
  391. (insert "new")
  392. (org-element-type (org-element-context))))))
  393. ;;; Navigation
  394. (ert-deftest test-org-link/next-link ()
  395. "Test `org-next-link' specifications."
  396. ;; Move to any type of link.
  397. (should
  398. (equal "[[link]]"
  399. (org-test-with-temp-text "foo [[link]]"
  400. (org-next-link)
  401. (buffer-substring (point) (line-end-position)))))
  402. (should
  403. (equal "http://link"
  404. (org-test-with-temp-text "foo http://link"
  405. (org-next-link)
  406. (buffer-substring (point) (line-end-position)))))
  407. (should
  408. (equal "<http://link>"
  409. (org-test-with-temp-text "foo <http://link>"
  410. (org-next-link)
  411. (buffer-substring (point) (line-end-position)))))
  412. ;; Ignore link at point.
  413. (should
  414. (equal "[[link2]]"
  415. (org-test-with-temp-text "[[link1]] [[link2]]"
  416. (org-next-link)
  417. (buffer-substring (point) (line-end-position)))))
  418. ;; Ignore fake links.
  419. (should
  420. (equal "[[truelink]]"
  421. (org-test-with-temp-text "foo\n: [[link]]\n[[truelink]]"
  422. (org-next-link)
  423. (buffer-substring (point) (line-end-position)))))
  424. ;; Do not move point when there is no link.
  425. (should
  426. (org-test-with-temp-text "foo bar"
  427. (org-next-link)
  428. (bobp)))
  429. ;; Wrap around after a failed search.
  430. (should
  431. (equal "[[link]]"
  432. (org-test-with-temp-text "[[link]]\n<point>foo"
  433. (org-next-link)
  434. (let* ((this-command 'org-next-link)
  435. (last-command this-command))
  436. (org-next-link))
  437. (buffer-substring (point) (line-end-position)))))
  438. ;; Find links with item tags.
  439. (should
  440. (equal "[[link1]]"
  441. (org-test-with-temp-text "- tag [[link1]] :: description"
  442. (org-next-link)
  443. (buffer-substring (point) (search-forward "]]" nil t))))))
  444. (ert-deftest test-org-link/previous-link ()
  445. "Test `org-previous-link' specifications."
  446. ;; Move to any type of link.
  447. (should
  448. (equal "[[link]]"
  449. (org-test-with-temp-text "[[link]]\nfoo<point>"
  450. (org-previous-link)
  451. (buffer-substring (point) (line-end-position)))))
  452. (should
  453. (equal "http://link"
  454. (org-test-with-temp-text "http://link\nfoo<point>"
  455. (org-previous-link)
  456. (buffer-substring (point) (line-end-position)))))
  457. (should
  458. (equal "<http://link>"
  459. (org-test-with-temp-text "<http://link>\nfoo<point>"
  460. (org-previous-link)
  461. (buffer-substring (point) (line-end-position)))))
  462. ;; Ignore link at point.
  463. (should
  464. (equal "[[link1]]"
  465. (org-test-with-temp-text "[[link1]]\n[[link2<point>]]"
  466. (org-previous-link)
  467. (buffer-substring (point) (line-end-position)))))
  468. (should
  469. (equal "[[link1]]"
  470. (org-test-with-temp-text "line\n[[link1]]\n[[link2<point>]]"
  471. (org-previous-link)
  472. (buffer-substring (point) (line-end-position)))))
  473. ;; Ignore fake links.
  474. (should
  475. (equal "[[truelink]]"
  476. (org-test-with-temp-text "[[truelink]]\n: [[link]]\n<point>"
  477. (org-previous-link)
  478. (buffer-substring (point) (line-end-position)))))
  479. ;; Do not move point when there is no link.
  480. (should
  481. (org-test-with-temp-text "foo bar<point>"
  482. (org-previous-link)
  483. (eobp)))
  484. ;; Wrap around after a failed search.
  485. (should
  486. (equal "[[link]]"
  487. (org-test-with-temp-text "foo\n[[link]]"
  488. (org-previous-link)
  489. (let* ((this-command 'org-previous-link)
  490. (last-command this-command))
  491. (org-previous-link))
  492. (buffer-substring (point) (line-end-position))))))
  493. ;;; Link regexps
  494. (defmacro test-ol-parse-link-in-text (text)
  495. "Return list of :type and :path of link parsed in TEXT.
  496. \"<point>\" string must be at the beginning of the link to be parsed."
  497. (declare (indent 1))
  498. `(org-test-with-temp-text ,text
  499. (list (org-element-property :type (org-element-link-parser))
  500. (org-element-property :path (org-element-link-parser)))))
  501. (ert-deftest test-org-link/plain-link-re ()
  502. "Test `org-link-plain-re'."
  503. (should
  504. (equal
  505. '("https" "//example.com")
  506. (test-ol-parse-link-in-text
  507. "(<point>https://example.com)")))
  508. (should
  509. (equal
  510. '("https" "//example.com/qwe()")
  511. (test-ol-parse-link-in-text
  512. "(Some text <point>https://example.com/qwe())")))
  513. (should
  514. (equal
  515. '("https" "//doi.org/10.1016/0160-791x(79)90023-x")
  516. (test-ol-parse-link-in-text
  517. "<point>https://doi.org/10.1016/0160-791x(79)90023-x")))
  518. (should
  519. (equal
  520. '("file" "aa")
  521. (test-ol-parse-link-in-text
  522. "The <point>file:aa link")))
  523. (should
  524. (equal
  525. '("file" "a(b)c")
  526. (test-ol-parse-link-in-text
  527. "The <point>file:a(b)c link")))
  528. (should
  529. (equal
  530. '("file" "a()")
  531. (test-ol-parse-link-in-text
  532. "The <point>file:a() link")))
  533. (should
  534. (equal
  535. '("file" "aa((a))")
  536. (test-ol-parse-link-in-text
  537. "The <point>file:aa((a)) link")))
  538. (should
  539. (equal
  540. '("file" "aa(())")
  541. (test-ol-parse-link-in-text
  542. "The <point>file:aa(()) link")))
  543. (should
  544. (equal
  545. '("file" "/a")
  546. (test-ol-parse-link-in-text
  547. "The <point>file:/a link")))
  548. (should
  549. (equal
  550. '("file" "/a/")
  551. (test-ol-parse-link-in-text
  552. "The <point>file:/a/ link")))
  553. (should
  554. (equal
  555. '("http" "//")
  556. (test-ol-parse-link-in-text
  557. "The <point>http:// link")))
  558. (should
  559. (equal
  560. '("file" "ab")
  561. (test-ol-parse-link-in-text
  562. "The (some <point>file:ab) link")))
  563. (should
  564. (equal
  565. '("file" "aa")
  566. (test-ol-parse-link-in-text
  567. "The <point>file:aa) link")))
  568. (should
  569. (equal
  570. '("file" "aa")
  571. (test-ol-parse-link-in-text
  572. "The <point>file:aa( link")))
  573. (should
  574. (equal
  575. '("http" "//foo.com/more_(than)_one_(parens)")
  576. (test-ol-parse-link-in-text
  577. "The <point>http://foo.com/more_(than)_one_(parens) link")))
  578. (should
  579. (equal
  580. '("http" "//foo.com/blah_(wikipedia)#cite-1")
  581. (test-ol-parse-link-in-text
  582. "The <point>http://foo.com/blah_(wikipedia)#cite-1 link")))
  583. (should
  584. (equal
  585. '("http" "//foo.com/blah_(wikipedia)_blah#cite-1")
  586. (test-ol-parse-link-in-text
  587. "The <point>http://foo.com/blah_(wikipedia)_blah#cite-1 link")))
  588. (should
  589. (equal
  590. '("http" "//foo.com/unicode_(✪)_in_parens")
  591. (test-ol-parse-link-in-text
  592. "The <point>http://foo.com/unicode_(✪)_in_parens link")))
  593. (should
  594. (equal
  595. '("http" "//foo.com/(something)?after=parens")
  596. (test-ol-parse-link-in-text
  597. "The <point>http://foo.com/(something)?after=parens link"))))
  598. ;;; Insert Links
  599. (defmacro test-ol-with-link-parameters-as (type parameters &rest body)
  600. "Pass TYPE/PARAMETERS to `org-link-parameters' and execute BODY.
  601. Save the original value of `org-link-parameters', execute
  602. `org-link-set-parameters' with the relevant args, execute BODY
  603. and restore `org-link-parameters'.
  604. TYPE is as in `org-link-set-parameters'. PARAMETERS is a plist to
  605. be passed to `org-link-set-parameters'."
  606. (declare (indent 2))
  607. (let (orig-parameters)
  608. ;; Copy all keys in `parameters' and their original values to
  609. ;; `orig-parameters'.
  610. (cl-loop for param in parameters by 'cddr
  611. do (setq orig-parameters
  612. (plist-put orig-parameters param (org-link-get-parameter type param))))
  613. `(unwind-protect
  614. ;; Set `parameters' values and execute body.
  615. (progn (org-link-set-parameters ,type ,@parameters) ,@body)
  616. ;; Restore original values.
  617. (apply 'org-link-set-parameters ,type ',orig-parameters))))
  618. (defun test-ol-insert-link-get-desc (&optional link-location description)
  619. "Insert link in temp buffer, return description.
  620. LINK-LOCATION and DESCRIPTION are passed to
  621. `org-insert-link' (COMPLETE-FILE is always nil)."
  622. (org-test-with-temp-text ""
  623. (org-insert-link nil link-location description)
  624. (save-match-data
  625. (when (and
  626. (org-in-regexp org-link-bracket-re 1)
  627. (match-end 2))
  628. (match-string-no-properties 2)))))
  629. (defun test-ol/return-foobar (_link-test _desc)
  630. "Return string \"foobar\".
  631. Take (and ignore) arguments conforming to `:insert-description'
  632. API in `org-link-parameters'. Used in test
  633. `test-ol/insert-link-insert-description', for the case where
  634. `:insert-description' is a function symbol."
  635. "foobar-from-function")
  636. (ert-deftest test-org-link/insert-link-insert-description ()
  637. "Test `:insert-description' parameter handling."
  638. ;; String case.
  639. (should
  640. (string=
  641. "foobar-string"
  642. (test-ol-with-link-parameters-as
  643. "id" (:insert-description "foobar-string")
  644. (test-ol-insert-link-get-desc "id:foo-bar"))))
  645. ;; Lambda case.
  646. (should
  647. (string=
  648. "foobar-lambda"
  649. (test-ol-with-link-parameters-as
  650. "id" (:insert-description (lambda (_link-test _desc) "foobar-lambda"))
  651. (test-ol-insert-link-get-desc "id:foo-bar"))))
  652. ;; Function symbol case.
  653. (should
  654. (string=
  655. "foobar-from-function"
  656. (test-ol-with-link-parameters-as
  657. "id" (:insert-description #'test-ol/return-foobar)
  658. (test-ol-insert-link-get-desc "id:foo-bar"))))
  659. ;; `:insert-description' parameter is defined, but doesn't return a
  660. ;; string.
  661. (should
  662. (null
  663. (test-ol-with-link-parameters-as
  664. "id" (:insert-description #'ignore)
  665. (test-ol-insert-link-get-desc "id:foo-bar"))))
  666. ;; Description argument should override `:insert-description'.
  667. (should
  668. (string=
  669. "foobar-desc-arg"
  670. (test-ol-with-link-parameters-as
  671. "id" (:insert-description "foobar")
  672. (test-ol-insert-link-get-desc "id:foo-bar" "foobar-desc-arg"))))
  673. ;; When neither `:insert-description' nor
  674. ;; `org-link-make-description-function' is defined, there should be
  675. ;; no description
  676. (should
  677. (null
  678. (let ((org-link-make-description-function nil))
  679. (test-ol-insert-link-get-desc "fake-link-type:foo-bar")))))
  680. (provide 'test-ol)
  681. ;;; test-ol.el ends here