test-ol.el 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  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. (ert-deftest test-ol/org-toggle-link-display ()
  42. "Make sure that `org-toggle-link-display' is working.
  43. See https://github.com/yantar92/org/issues/4."
  44. (dolist (org-link-descriptive '(nil t))
  45. (org-test-with-temp-text "* Org link test
  46. [[https://example.com][A link to a site]]"
  47. (dotimes (_ 2)
  48. (goto-char 1)
  49. (re-search-forward "\\[")
  50. (should-not (xor org-link-descriptive (org-invisible-p)))
  51. (re-search-forward "example")
  52. (should-not (xor org-link-descriptive (org-invisible-p)))
  53. (re-search-forward "com")
  54. (should-not (xor org-link-descriptive (org-invisible-p)))
  55. (re-search-forward "]")
  56. (should-not (xor org-link-descriptive (org-invisible-p)))
  57. (re-search-forward "\\[")
  58. (should-not (org-invisible-p))
  59. (re-search-forward "link")
  60. (should-not (org-invisible-p))
  61. (re-search-forward "]")
  62. (should-not (xor org-link-descriptive (org-invisible-p)))
  63. (org-toggle-link-display)))))
  64. ;;; Escape and Unescape Links
  65. (ert-deftest test-ol/escape ()
  66. "Test `org-link-escape' specifications."
  67. ;; No-op when there is no backslash or square bracket.
  68. (should (string= "foo" (org-link-escape "foo")))
  69. ;; Escape square brackets at boundaries of the link.
  70. (should (string= "\\[foo\\]" (org-link-escape "[foo]")))
  71. ;; Escape square brackets followed by another square bracket.
  72. (should (string= "foo\\]\\[bar" (org-link-escape "foo][bar")))
  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. ;; Escape backslashes at the end of the link.
  77. (should (string= "foo\\\\" (org-link-escape "foo\\")))
  78. ;; Escape backslashes that could be confused with escaping
  79. ;; characters.
  80. (should (string= "foo\\\\\\]" (org-link-escape "foo\\]")))
  81. (should (string= "foo\\\\\\]\\[" (org-link-escape "foo\\][")))
  82. (should (string= "foo\\\\\\]\\]bar" (org-link-escape "foo\\]]bar")))
  83. ;; Do not escape backslash characters when unnecessary.
  84. (should (string= "foo\\bar" (org-link-escape "foo\\bar")))
  85. ;; Pathological cases: consecutive closing square brackets.
  86. (should (string= "\\[\\[\\[foo\\]\\]\\]" (org-link-escape "[[[foo]]]")))
  87. (should (string= "\\[\\[foo\\]\\] bar" (org-link-escape "[[foo]] bar"))))
  88. (ert-deftest test-ol/unescape ()
  89. "Test `org-link-unescape' specifications."
  90. ;; No-op if there is no backslash.
  91. (should (string= "foo" (org-link-unescape "foo")))
  92. ;; No-op if backslashes are not escaping backslashes.
  93. (should (string= "foo\\bar" (org-link-unescape "foo\\bar")))
  94. ;; Unescape backslashes before square brackets.
  95. (should (string= "foo]bar" (org-link-unescape "foo\\]bar")))
  96. (should (string= "foo\\]" (org-link-unescape "foo\\\\\\]")))
  97. (should (string= "foo\\][" (org-link-unescape "foo\\\\\\][")))
  98. (should (string= "foo\\]]bar" (org-link-unescape "foo\\\\\\]\\]bar")))
  99. (should (string= "foo\\[[bar" (org-link-unescape "foo\\\\\\[\\[bar")))
  100. (should (string= "foo\\[]bar" (org-link-unescape "foo\\\\\\[\\]bar")))
  101. ;; Unescape backslashes at the end of the link.
  102. (should (string= "foo\\" (org-link-unescape "foo\\\\")))
  103. ;; Unescape closing square bracket at boundaries of the link.
  104. (should (string= "[foo]" (org-link-unescape "\\[foo\\]")))
  105. ;; Pathological cases: consecutive closing square brackets.
  106. (should (string= "[[[foo]]]" (org-link-unescape "\\[\\[\\[foo\\]\\]\\]")))
  107. (should (string= "[[foo]] bar" (org-link-unescape "\\[\\[foo\\]\\] bar"))))
  108. (ert-deftest test-ol/make-string ()
  109. "Test `org-link-make-string' specifications."
  110. ;; Throw an error on empty URI.
  111. (should-error (org-link-make-string ""))
  112. ;; Empty description returns a [[URI]] construct.
  113. (should (string= "[[uri]]"(org-link-make-string "uri")))
  114. ;; Non-empty description returns a [[URI][DESCRIPTION]] construct.
  115. (should
  116. (string= "[[uri][description]]"
  117. (org-link-make-string "uri" "description")))
  118. ;; Escape "]]" strings in the description with zero-width spaces.
  119. (should
  120. (let ((zws (string ?\x200B)))
  121. (string= (format "[[uri][foo]%s]bar]]" zws)
  122. (org-link-make-string "uri" "foo]]bar"))))
  123. ;; Prevent description from ending with a closing square bracket
  124. ;; with a zero-width space.
  125. (should
  126. (let ((zws (string ?\x200B)))
  127. (string= (format "[[uri][foo]%s]]" zws)
  128. (org-link-make-string "uri" "foo]")))))
  129. ;;; Store links
  130. (ert-deftest test-ol/store-link ()
  131. "Test `org-store-link' specifications."
  132. ;; On a headline, link to that headline. Use heading as the
  133. ;; description of the link.
  134. (should
  135. (let (org-store-link-props org-stored-links)
  136. (org-test-with-temp-text-in-file "* H1"
  137. (let ((file (buffer-file-name)))
  138. (equal (format "[[file:%s::*H1][H1]]" file)
  139. (org-store-link nil))))))
  140. ;; On a headline, remove TODO and COMMENT keywords, priority cookie,
  141. ;; and tags.
  142. (should
  143. (let (org-store-link-props org-stored-links)
  144. (org-test-with-temp-text-in-file "* TODO H1"
  145. (let ((file (buffer-file-name)))
  146. (equal (format "[[file:%s::*H1][H1]]" file)
  147. (org-store-link nil))))))
  148. (should
  149. (let (org-store-link-props org-stored-links)
  150. (org-test-with-temp-text-in-file "* COMMENT H1"
  151. (let ((file (buffer-file-name)))
  152. (equal (format "[[file:%s::*H1][H1]]" file)
  153. (org-store-link nil))))))
  154. (should
  155. (let (org-store-link-props org-stored-links)
  156. (org-test-with-temp-text-in-file "* [#A] H1"
  157. (let ((file (buffer-file-name)))
  158. (equal (format "[[file:%s::*H1][H1]]" file)
  159. (org-store-link nil))))))
  160. (should
  161. (let (org-store-link-props org-stored-links)
  162. (org-test-with-temp-text-in-file "* H1 :tag:"
  163. (let ((file (buffer-file-name)))
  164. (equal (format "[[file:%s::*H1][H1]]" file)
  165. (org-store-link nil))))))
  166. ;; On a headline, remove any link from description.
  167. (should
  168. (let (org-store-link-props org-stored-links)
  169. (org-test-with-temp-text-in-file "* [[#l][d]]"
  170. (let ((file (buffer-file-name)))
  171. (equal (format "[[file:%s::*%s][d]]"
  172. file
  173. (org-link-escape "[[#l][d]]"))
  174. (org-store-link nil))))))
  175. (should
  176. (let (org-store-link-props org-stored-links)
  177. (org-test-with-temp-text-in-file "* [[l]]"
  178. (let ((file (buffer-file-name)))
  179. (equal (format "[[file:%s::*%s][l]]" file (org-link-escape "[[l]]"))
  180. (org-store-link nil))))))
  181. (should
  182. (let (org-store-link-props org-stored-links)
  183. (org-test-with-temp-text-in-file "* [[l1][d1]] [[l2][d2]]"
  184. (let ((file (buffer-file-name)))
  185. (equal (format "[[file:%s::*%s][d1 d2]]"
  186. file
  187. (org-link-escape "[[l1][d1]] [[l2][d2]]"))
  188. (org-store-link nil))))))
  189. ;; On a named element, link to that element.
  190. (should
  191. (let (org-store-link-props org-stored-links)
  192. (org-test-with-temp-text-in-file "#+NAME: foo\nParagraph"
  193. (let ((file (buffer-file-name)))
  194. (equal (format "[[file:%s::foo][foo]]" file)
  195. (org-store-link nil))))))
  196. ;; Store link to Org buffer, with context.
  197. (should
  198. (let ((org-stored-links nil)
  199. (org-id-link-to-org-use-id nil)
  200. (org-context-in-file-links t))
  201. (org-test-with-temp-text-in-file "* h1"
  202. (let ((file (buffer-file-name)))
  203. (equal (format "[[file:%s::*h1][h1]]" file)
  204. (org-store-link nil))))))
  205. ;; Store link to Org buffer, without context.
  206. (should
  207. (let ((org-stored-links nil)
  208. (org-id-link-to-org-use-id nil)
  209. (org-context-in-file-links nil))
  210. (org-test-with-temp-text-in-file "* h1"
  211. (let ((file (buffer-file-name)))
  212. (equal (format "[[file:%s][file:%s]]" file file)
  213. (org-store-link nil))))))
  214. ;; C-u prefix reverses `org-context-in-file-links' in Org buffer.
  215. (should
  216. (let ((org-stored-links nil)
  217. (org-id-link-to-org-use-id nil)
  218. (org-context-in-file-links nil))
  219. (org-test-with-temp-text-in-file "* h1"
  220. (let ((file (buffer-file-name)))
  221. (equal (format "[[file:%s::*h1][h1]]" file)
  222. (org-store-link '(4)))))))
  223. ;; A C-u C-u does *not* reverse `org-context-in-file-links' in Org
  224. ;; buffer.
  225. (should
  226. (let ((org-stored-links nil)
  227. (org-id-link-to-org-use-id nil)
  228. (org-context-in-file-links nil))
  229. (org-test-with-temp-text-in-file "* h1"
  230. (let ((file (buffer-file-name)))
  231. (equal (format "[[file:%s][file:%s]]" file file)
  232. (org-store-link '(16)))))))
  233. ;; Store file link to non-Org buffer, with context.
  234. (should
  235. (let ((org-stored-links nil)
  236. (org-link-context-for-files t))
  237. (org-test-with-temp-text-in-file "one\n<point>two"
  238. (fundamental-mode)
  239. (let ((file (buffer-file-name)))
  240. (equal (format "[[file:%s::two]]" file)
  241. (org-store-link nil))))))
  242. ;; Store file link to non-Org buffer, without context.
  243. (should
  244. (let ((org-stored-links nil)
  245. (org-context-in-file-links nil))
  246. (org-test-with-temp-text-in-file "one\n<point>two"
  247. (fundamental-mode)
  248. (let ((file (buffer-file-name)))
  249. (equal (format "[[file:%s][file:%s]]" file file)
  250. (org-store-link nil))))))
  251. ;; C-u prefix reverses `org-context-in-file-links' in non-Org
  252. ;; buffer.
  253. (should
  254. (let ((org-stored-links nil)
  255. (org-link-context-for-files nil))
  256. (org-test-with-temp-text-in-file "one\n<point>two"
  257. (fundamental-mode)
  258. (let ((file (buffer-file-name)))
  259. (equal (format "[[file:%s::two]]" file)
  260. (org-store-link '(4)))))))
  261. ;; A C-u C-u does *not* reverse `org-context-in-file-links' in
  262. ;; non-Org buffer.
  263. (should
  264. (let ((org-stored-links nil)
  265. (org-context-in-file-links nil))
  266. (org-test-with-temp-text-in-file "one\n<point>two"
  267. (fundamental-mode)
  268. (let ((file (buffer-file-name)))
  269. (equal (format "[[file:%s][file:%s]]" file file)
  270. (org-store-link '(16)))))))
  271. ;; Context does not include special search syntax.
  272. (should
  273. (let ((org-stored-links nil)
  274. (org-context-in-file-links t))
  275. (org-test-with-temp-text-in-file "(two)"
  276. (fundamental-mode)
  277. (let ((file (buffer-file-name)))
  278. (equal (format "[[file:%s::two]]" file file)
  279. (org-store-link nil))))))
  280. (should
  281. (let ((org-stored-links nil)
  282. (org-context-in-file-links t))
  283. (org-test-with-temp-text-in-file "# two"
  284. (fundamental-mode)
  285. (let ((file (buffer-file-name)))
  286. (equal (format "[[file:%s::two]]" file file)
  287. (org-store-link nil))))))
  288. (should
  289. (let ((org-stored-links nil)
  290. (org-context-in-file-links t))
  291. (org-test-with-temp-text-in-file "*two"
  292. (fundamental-mode)
  293. (let ((file (buffer-file-name)))
  294. (equal (format "[[file:%s::two]]" file file)
  295. (org-store-link nil))))))
  296. (should
  297. (let ((org-stored-links nil)
  298. (org-context-in-file-links t))
  299. (org-test-with-temp-text-in-file "( two )"
  300. (fundamental-mode)
  301. (let ((file (buffer-file-name)))
  302. (equal (format "[[file:%s::two]]" file file)
  303. (org-store-link nil))))))
  304. (should
  305. (let ((org-stored-links nil)
  306. (org-context-in-file-links t))
  307. (org-test-with-temp-text-in-file "# two"
  308. (fundamental-mode)
  309. (let ((file (buffer-file-name)))
  310. (equal (format "[[file:%s::two]]" file file)
  311. (org-store-link nil))))))
  312. (should
  313. (let ((org-stored-links nil)
  314. (org-context-in-file-links t))
  315. (org-test-with-temp-text-in-file "#( two )"
  316. (fundamental-mode)
  317. (let ((file (buffer-file-name)))
  318. (equal (format "[[file:%s::two]]" file file)
  319. (org-store-link nil))))))
  320. (should
  321. (let ((org-stored-links nil)
  322. (org-context-in-file-links t))
  323. (org-test-with-temp-text-in-file "#** ((## two) )"
  324. (fundamental-mode)
  325. (let ((file (buffer-file-name)))
  326. (equal (format "[[file:%s::two]]" file file)
  327. (org-store-link nil))))))
  328. (should-not
  329. (let ((org-stored-links nil)
  330. (org-context-in-file-links t))
  331. (org-test-with-temp-text-in-file "(two"
  332. (fundamental-mode)
  333. (let ((file (buffer-file-name)))
  334. (equal (format "[[file:%s::two]]" file file)
  335. (org-store-link nil))))))
  336. ;; Context also ignore statistics cookies and special headlines
  337. ;; data.
  338. (should
  339. (let ((org-stored-links nil)
  340. (org-context-in-file-links t))
  341. (org-test-with-temp-text-in-file "* TODO [#A] COMMENT foo :bar:"
  342. (let ((file (buffer-file-name)))
  343. (equal (format "[[file:%s::*foo][foo]]" file file)
  344. (org-store-link nil))))))
  345. (should
  346. (let ((org-stored-links nil)
  347. (org-context-in-file-links t))
  348. (org-test-with-temp-text-in-file "* foo[33%]bar"
  349. (let ((file (buffer-file-name)))
  350. (equal (format "[[file:%s::*foo bar][foo bar]]" file file)
  351. (org-store-link nil))))))
  352. (should
  353. (let ((org-stored-links nil)
  354. (org-context-in-file-links t))
  355. (org-test-with-temp-text-in-file "* [%][/] foo [35%] bar[3/5]"
  356. (let ((file (buffer-file-name)))
  357. (equal (format "[[file:%s::*foo bar][foo bar]]" file file)
  358. (org-store-link nil)))))))
  359. ;;; Radio Targets
  360. (ert-deftest test-ol/update-radio-target-regexp ()
  361. "Test `org-update-radio-target-regexp' specifications."
  362. ;; Properly update cache with no previous radio target regexp.
  363. (should
  364. (eq 'link
  365. (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
  366. (save-excursion (goto-char (point-max)) (org-element-context))
  367. (insert "<<<")
  368. (search-forward "o")
  369. (insert ">>>")
  370. (org-update-radio-target-regexp)
  371. (goto-char (point-max))
  372. (org-element-type (org-element-context)))))
  373. ;; Properly update cache with previous radio target regexp.
  374. (should
  375. (eq 'link
  376. (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
  377. (save-excursion (goto-char (point-max)) (org-element-context))
  378. (insert "<<<")
  379. (search-forward "o")
  380. (insert ">>>")
  381. (org-update-radio-target-regexp)
  382. (search-backward "r")
  383. (delete-char 5)
  384. (insert "new")
  385. (org-update-radio-target-regexp)
  386. (goto-char (point-max))
  387. (delete-region (line-beginning-position) (point))
  388. (insert "new")
  389. (org-element-type (org-element-context))))))
  390. ;;; Navigation
  391. (ert-deftest test-ol/next-link ()
  392. "Test `org-next-link' specifications."
  393. ;; Move to any type of link.
  394. (should
  395. (equal "[[link]]"
  396. (org-test-with-temp-text "foo [[link]]"
  397. (org-next-link)
  398. (buffer-substring (point) (line-end-position)))))
  399. (should
  400. (equal "http://link"
  401. (org-test-with-temp-text "foo http://link"
  402. (org-next-link)
  403. (buffer-substring (point) (line-end-position)))))
  404. (should
  405. (equal "<http://link>"
  406. (org-test-with-temp-text "foo <http://link>"
  407. (org-next-link)
  408. (buffer-substring (point) (line-end-position)))))
  409. ;; Ignore link at point.
  410. (should
  411. (equal "[[link2]]"
  412. (org-test-with-temp-text "[[link1]] [[link2]]"
  413. (org-next-link)
  414. (buffer-substring (point) (line-end-position)))))
  415. ;; Ignore fake links.
  416. (should
  417. (equal "[[truelink]]"
  418. (org-test-with-temp-text "foo\n: [[link]]\n[[truelink]]"
  419. (org-next-link)
  420. (buffer-substring (point) (line-end-position)))))
  421. ;; Do not move point when there is no link.
  422. (should
  423. (org-test-with-temp-text "foo bar"
  424. (org-next-link)
  425. (bobp)))
  426. ;; Wrap around after a failed search.
  427. (should
  428. (equal "[[link]]"
  429. (org-test-with-temp-text "[[link]]\n<point>foo"
  430. (org-next-link)
  431. (let* ((this-command 'org-next-link)
  432. (last-command this-command))
  433. (org-next-link))
  434. (buffer-substring (point) (line-end-position)))))
  435. ;; Find links with item tags.
  436. (should
  437. (equal "[[link1]]"
  438. (org-test-with-temp-text "- tag [[link1]] :: description"
  439. (org-next-link)
  440. (buffer-substring (point) (search-forward "]]" nil t))))))
  441. (ert-deftest test-ol/previous-link ()
  442. "Test `org-previous-link' specifications."
  443. ;; Move to any type of link.
  444. (should
  445. (equal "[[link]]"
  446. (org-test-with-temp-text "[[link]]\nfoo<point>"
  447. (org-previous-link)
  448. (buffer-substring (point) (line-end-position)))))
  449. (should
  450. (equal "http://link"
  451. (org-test-with-temp-text "http://link\nfoo<point>"
  452. (org-previous-link)
  453. (buffer-substring (point) (line-end-position)))))
  454. (should
  455. (equal "<http://link>"
  456. (org-test-with-temp-text "<http://link>\nfoo<point>"
  457. (org-previous-link)
  458. (buffer-substring (point) (line-end-position)))))
  459. ;; Ignore link at point.
  460. (should
  461. (equal "[[link1]]"
  462. (org-test-with-temp-text "[[link1]]\n[[link2<point>]]"
  463. (org-previous-link)
  464. (buffer-substring (point) (line-end-position)))))
  465. (should
  466. (equal "[[link1]]"
  467. (org-test-with-temp-text "line\n[[link1]]\n[[link2<point>]]"
  468. (org-previous-link)
  469. (buffer-substring (point) (line-end-position)))))
  470. ;; Ignore fake links.
  471. (should
  472. (equal "[[truelink]]"
  473. (org-test-with-temp-text "[[truelink]]\n: [[link]]\n<point>"
  474. (org-previous-link)
  475. (buffer-substring (point) (line-end-position)))))
  476. ;; Do not move point when there is no link.
  477. (should
  478. (org-test-with-temp-text "foo bar<point>"
  479. (org-previous-link)
  480. (eobp)))
  481. ;; Wrap around after a failed search.
  482. (should
  483. (equal "[[link]]"
  484. (org-test-with-temp-text "foo\n[[link]]"
  485. (org-previous-link)
  486. (let* ((this-command 'org-previous-link)
  487. (last-command this-command))
  488. (org-previous-link))
  489. (buffer-substring (point) (line-end-position))))))
  490. ;;; Link regexps
  491. (defmacro test-ol-parse-link-in-text (text)
  492. "Return list of :type and :path of link parsed in TEXT.
  493. \"<point>\" string must be at the beginning of the link to be parsed."
  494. (declare (indent 1))
  495. `(org-test-with-temp-text ,text
  496. (list (org-element-property :type (org-element-link-parser))
  497. (org-element-property :path (org-element-link-parser)))))
  498. (ert-deftest test-ol/plain-link-re ()
  499. "Test `org-link-plain-re'."
  500. (should
  501. (equal
  502. '("https" "//example.com")
  503. (test-ol-parse-link-in-text
  504. "(<point>https://example.com)")))
  505. (should
  506. (equal
  507. '("https" "//example.com/qwe()")
  508. (test-ol-parse-link-in-text
  509. "(Some text <point>https://example.com/qwe())")))
  510. (should
  511. (equal
  512. '("https" "//doi.org/10.1016/0160-791x(79)90023-x")
  513. (test-ol-parse-link-in-text
  514. "<point>https://doi.org/10.1016/0160-791x(79)90023-x")))
  515. (should
  516. (equal
  517. '("file" "aa")
  518. (test-ol-parse-link-in-text
  519. "The <point>file:aa link")))
  520. (should
  521. (equal
  522. '("file" "a(b)c")
  523. (test-ol-parse-link-in-text
  524. "The <point>file:a(b)c link")))
  525. (should
  526. (equal
  527. '("file" "a()")
  528. (test-ol-parse-link-in-text
  529. "The <point>file:a() link")))
  530. (should
  531. (equal
  532. '("file" "aa((a))")
  533. (test-ol-parse-link-in-text
  534. "The <point>file:aa((a)) link")))
  535. (should
  536. (equal
  537. '("file" "aa(())")
  538. (test-ol-parse-link-in-text
  539. "The <point>file:aa(()) link")))
  540. (should
  541. (equal
  542. '("file" "/a")
  543. (test-ol-parse-link-in-text
  544. "The <point>file:/a link")))
  545. (should
  546. (equal
  547. '("file" "/a/")
  548. (test-ol-parse-link-in-text
  549. "The <point>file:/a/ link")))
  550. (should
  551. (equal
  552. '("http" "//")
  553. (test-ol-parse-link-in-text
  554. "The <point>http:// link")))
  555. (should
  556. (equal
  557. '("file" "ab")
  558. (test-ol-parse-link-in-text
  559. "The (some <point>file:ab) link")))
  560. (should
  561. (equal
  562. '("file" "aa")
  563. (test-ol-parse-link-in-text
  564. "The <point>file:aa) link")))
  565. (should
  566. (equal
  567. '("file" "aa")
  568. (test-ol-parse-link-in-text
  569. "The <point>file:aa( link")))
  570. (should
  571. (equal
  572. '("http" "//foo.com/more_(than)_one_(parens)")
  573. (test-ol-parse-link-in-text
  574. "The <point>http://foo.com/more_(than)_one_(parens) link")))
  575. (should
  576. (equal
  577. '("http" "//foo.com/blah_(wikipedia)#cite-1")
  578. (test-ol-parse-link-in-text
  579. "The <point>http://foo.com/blah_(wikipedia)#cite-1 link")))
  580. (should
  581. (equal
  582. '("http" "//foo.com/blah_(wikipedia)_blah#cite-1")
  583. (test-ol-parse-link-in-text
  584. "The <point>http://foo.com/blah_(wikipedia)_blah#cite-1 link")))
  585. (should
  586. (equal
  587. '("http" "//foo.com/unicode_(✪)_in_parens")
  588. (test-ol-parse-link-in-text
  589. "The <point>http://foo.com/unicode_(✪)_in_parens link")))
  590. (should
  591. (equal
  592. '("http" "//foo.com/(something)?after=parens")
  593. (test-ol-parse-link-in-text
  594. "The <point>http://foo.com/(something)?after=parens link"))))
  595. (provide 'test-ol)
  596. ;;; test-ol.el ends here