test-ol.el 25 KB

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