test-org-footnote.el 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  1. ;;; test-org-footnote.el --- Tests for org-footnote.el
  2. ;; Copyright (C) 2012-2015 Nicolas Goaziou
  3. ;; Author: Nicolas Goaziou <mail at nicolasgoaziou dot 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 <http://www.gnu.org/licenses/>.
  14. ;;; Code:
  15. (ert-deftest test-org-footnote/new ()
  16. "Test `org-footnote-new' specifications."
  17. ;; `org-footnote-auto-label' is t.
  18. (should
  19. (string-match-p
  20. "Test\\[fn:1\\]\n+\\[fn:1\\]"
  21. (org-test-with-temp-text "Test<point>"
  22. (let ((org-footnote-auto-label t)
  23. (org-footnote-section nil))
  24. (org-footnote-new))
  25. (buffer-string))))
  26. ;; `org-footnote-auto-label' is `random'.
  27. (should
  28. (string-match-p
  29. "Test\\[fn:\\(.+?\\)\\]\n+\\[fn:\\1\\]"
  30. (org-test-with-temp-text "Test<point>"
  31. (let ((org-footnote-auto-label 'random)
  32. (org-footnote-section nil))
  33. (org-footnote-new))
  34. (buffer-string))))
  35. ;; Error at beginning of line.
  36. (should-error
  37. (org-test-with-temp-text "<point>Test"
  38. (org-footnote-new)))
  39. ;; Error at keywords.
  40. (should-error
  41. (org-test-with-temp-text "#+TIT<point>LE: value"
  42. (org-footnote-new)))
  43. (should-error
  44. (org-test-with-temp-text "#+CAPTION: <point>\nParagraph"
  45. (org-footnote-new)))
  46. ;; Allow new footnotes in blank lines at the beginning of the
  47. ;; document.
  48. (should
  49. (string-match-p
  50. " \\[fn:1\\]"
  51. (org-test-with-temp-text " <point>"
  52. (let ((org-footnote-auto-label t)) (org-footnote-new))
  53. (buffer-string))))
  54. ;; In an headline or inlinetask, point must be either on the
  55. ;; heading itself or on the blank lines below.
  56. (should (org-test-with-temp-text "* H<point>" (org-footnote-new) t))
  57. (should
  58. (org-test-with-temp-text "* H\n <point>\nParagraph" (org-footnote-new) t))
  59. (should-error (org-test-with-temp-text "*<point> H" (org-footnote-new) t))
  60. (should-error
  61. (org-test-with-temp-text "* H <point>:tag:" (org-footnote-new) t))
  62. ;; Allow new footnotes within recursive objects, but not in links.
  63. (should
  64. (string-match-p
  65. " \\*bold\\[fn:1\\]\\*"
  66. (org-test-with-temp-text " *bold<point>*"
  67. (let ((org-footnote-auto-label t)) (org-footnote-new))
  68. (buffer-string))))
  69. (should-error
  70. (org-test-with-temp-text " [[https://orgmode.org][Org mode<point>]]"
  71. (org-footnote-new)))
  72. ;; Allow new footnotes in blank lines after an element or white
  73. ;; spaces after an object.
  74. (should
  75. (string-match-p
  76. " \\[fn:1\\]"
  77. (org-test-with-temp-text "#+BEGIN_EXAMPLE\nA\n#+END_EXAMPLE\n <point>"
  78. (let ((org-footnote-auto-label t)) (org-footnote-new))
  79. (buffer-string))))
  80. (should
  81. (string-match-p
  82. " \\*bold\\*\\[fn:1\\]"
  83. (org-test-with-temp-text " *bold*<point>"
  84. (let ((org-footnote-auto-label t)) (org-footnote-new))
  85. (buffer-string))))
  86. ;; When creating a new footnote, move to its definition.
  87. (should
  88. (string=
  89. "[fn:1] "
  90. (org-test-with-temp-text "Text<point>"
  91. (let ((org-footnote-auto-label t)
  92. (org-footnote-auto-adjust nil))
  93. (org-footnote-new))
  94. (buffer-substring-no-properties (line-beginning-position) (point)))))
  95. ;; Re-order and re-label footnotes properly when
  96. ;; `org-footnote-auto-adjust' is non-nil.
  97. (should
  98. (string=
  99. "[fn:1] 1\n\n[fn:2] \n\n[fn:3] 2\n"
  100. (org-test-with-temp-text
  101. "Text[fn:1]Text<point>Text[fn:2]\n\n[fn:1] 1\n\n[fn:2] 2"
  102. (let ((org-footnote-auto-label t)
  103. (org-footnote-auto-adjust t)
  104. (org-footnote-section nil))
  105. (org-footnote-new))
  106. (buffer-substring-no-properties
  107. (line-beginning-position -1)
  108. (line-beginning-position 4)))))
  109. ;; Do not alter file local variables when inserting new definition
  110. ;; label.
  111. (should
  112. (equal "Paragraph[fn:1]
  113. \[fn:1]
  114. # Local Variables:
  115. # foo: t
  116. # End:"
  117. (org-test-with-temp-text
  118. "Paragraph<point>\n# Local Variables:\n# foo: t\n# End:"
  119. (let ((org-footnote-section nil)) (org-footnote-new))
  120. (buffer-string))))
  121. (should
  122. (equal "Paragraph[fn:1]
  123. * Footnotes
  124. \[fn:1]
  125. # Local Variables:
  126. # foo: t
  127. # End:"
  128. (org-test-with-temp-text
  129. "Paragraph<point>\n# Local Variables:\n# foo: t\n# End:"
  130. (let ((org-footnote-section "Footnotes")) (org-footnote-new))
  131. (buffer-string)))))
  132. (ert-deftest test-org-footnote/delete ()
  133. "Test `org-footnote-delete' specifications."
  134. ;; Regular test.
  135. (should
  136. (equal "Paragraph"
  137. (org-test-with-temp-text "Paragraph<point>[fn:1]\n\n[fn:1] Definition"
  138. (org-footnote-delete)
  139. (org-trim (buffer-string)))))
  140. ;; Remove multiple definitions and references.
  141. (should
  142. (equal "Paragraph and another"
  143. (org-test-with-temp-text
  144. "Paragraph<point>[fn:1] and another[fn:1]
  145. \[fn:1] def
  146. \[fn:1] def"
  147. (org-footnote-delete)
  148. (org-trim (buffer-string)))))
  149. ;; Delete inline footnotes and all references.
  150. (should
  151. (equal "Para and"
  152. (org-test-with-temp-text "Para<point>[fn:label:def] and[fn:label]"
  153. (org-footnote-delete)
  154. (org-trim (buffer-string)))))
  155. ;; Delete anonymous footnotes.
  156. (should
  157. (equal "Para"
  158. (let ((org-footnote-section nil))
  159. (org-test-with-temp-text "Para<point>[fn::def]"
  160. (org-footnote-delete)
  161. (org-trim (buffer-string))))))
  162. ;; With an argument, delete footnote with specified label.
  163. (should
  164. (equal "Paragraph[fn:1] and another\n\n[fn:1] def"
  165. (let ((org-footnote-section nil))
  166. (org-test-with-temp-text
  167. "Paragraph[fn:1] and another[fn:2]\n\n[fn:1] def\n\n[fn:2] def2"
  168. (org-footnote-delete "2")
  169. (org-trim (buffer-string))))))
  170. ;; Error when no argument is specified at point is not at a footnote
  171. ;; reference.
  172. (should-error
  173. (org-test-with-temp-text "Para[fn:1]\n\n[fn:1] Def"
  174. (org-footnote-delete)))
  175. ;; Correctly delete footnotes with multiple paragraphs.
  176. (should
  177. (equal "Para\n\n\nOutside footnote."
  178. (let ((org-footnote-section nil))
  179. (org-test-with-temp-text
  180. "Para[fn:1]\n\n[fn:1] para1\n\npara2\n\n\nOutside footnote."
  181. (org-footnote-delete "1")
  182. (org-trim (buffer-string))))))
  183. ;; Remove blank lines above the footnote but preserve those after
  184. ;; it.
  185. (should
  186. (equal "Text\n\n\nOther text."
  187. (let ((org-footnote-section nil))
  188. (org-test-with-temp-text
  189. "Text[fn:1]\n\n[fn:1] Definition.\n\n\nOther text."
  190. (org-footnote-delete "1")
  191. (buffer-string)))))
  192. ;; Preserve file local variables when deleting a footnote.
  193. (should
  194. (equal
  195. "Paragraph\n# Local Variables:\n# foo: t\n# End:"
  196. (org-test-with-temp-text
  197. "Paragraph[fn:1]\n[fn:1] Def 1\n# Local Variables:\n# foo: t\n# End:"
  198. (let ((org-footnote-section nil)) (org-footnote-delete "1"))
  199. (buffer-string)))))
  200. (ert-deftest test-org-footnote/goto-definition ()
  201. "Test `org-footnote-goto-definition' specifications."
  202. ;; Error on unknown definitions.
  203. (should-error
  204. (org-test-with-temp-text "No footnote definition"
  205. (org-footnote-goto-definition "1")))
  206. ;; Error when trying to reach a definition outside narrowed part of
  207. ;; buffer.
  208. (should-error
  209. (org-test-with-temp-text "Some text<point>\n[fn:1] Definition."
  210. (narrow-to-region (point-min) (point))
  211. (org-footnote-goto-definition "1")))
  212. (should-error
  213. (org-test-with-temp-text "[fn:1] Definition.\n<point>Some text"
  214. (narrow-to-region (point) (point-max))
  215. (org-footnote-goto-definition "1")))
  216. ;; Otherwise, move at the beginning of the definition, including
  217. ;; anonymous footnotes.
  218. (should
  219. (equal
  220. "Definition."
  221. (org-test-with-temp-text "Some text\n[fn:1] Definition."
  222. (org-footnote-goto-definition "1")
  223. (buffer-substring (point) (point-max)))))
  224. (should
  225. (equal
  226. "definition]"
  227. (org-test-with-temp-text "Some text[fn:label:definition]"
  228. (org-footnote-goto-definition "label")
  229. (buffer-substring (point) (point-max))))))
  230. (ert-deftest test-org-footnote/sort ()
  231. "Test `org-footnote-sort' specifications."
  232. ;; Reorder definitions with a nil `org-footnote-section'. In this
  233. ;; case each definition is written at the end of the section
  234. ;; containing its first reference.
  235. (should
  236. (equal
  237. "
  238. Text[fn:1][fn:2]
  239. \[fn:1] Def 1
  240. \[fn:2] Def 2
  241. "
  242. (org-test-with-temp-text "
  243. Text[fn:1][fn:2]
  244. \[fn:2] Def 2
  245. \[fn:1] Def 1"
  246. (let ((org-footnote-section nil)) (org-footnote-sort))
  247. (buffer-string))))
  248. (should
  249. (equal
  250. "
  251. * H1
  252. Text[fn:1]
  253. \[fn:1] Def 1
  254. * H2
  255. Text[fn:2]
  256. \[fn:2] Def 2
  257. "
  258. (org-test-with-temp-text "
  259. * H1
  260. Text[fn:1]
  261. * H2
  262. Text[fn:2]
  263. \[fn:1] Def 1
  264. \[fn:2] Def 2
  265. "
  266. (let ((org-footnote-section nil)) (org-footnote-sort))
  267. (buffer-string))))
  268. ;; Reorder definitions with a non-nil `org-footnote-section'.
  269. (should
  270. (equal
  271. "
  272. Text[fn:1][fn:2]
  273. * Footnotes
  274. \[fn:1] Def 1
  275. \[fn:2] Def 2
  276. "
  277. (org-test-with-temp-text "
  278. Text[fn:1][fn:2]
  279. \[fn:2] Def 2
  280. \[fn:1] Def 1"
  281. (let ((org-footnote-section "Footnotes")) (org-footnote-sort))
  282. (buffer-string))))
  283. ;; When `org-footnote-section' is non-nil, clear previous footnote
  284. ;; sections.
  285. (should
  286. (equal
  287. "
  288. Text[fn:1]
  289. * Headline
  290. * Other headline
  291. * Footnotes
  292. \[fn:1] Def 1
  293. "
  294. (org-test-with-temp-text "
  295. Text[fn:1]
  296. * Footnotes
  297. \[fn:1] Def 1
  298. * Headline
  299. ** Footnotes
  300. * Other headline"
  301. (let ((org-footnote-section "Footnotes")) (org-footnote-sort))
  302. (buffer-string))))
  303. ;; Ignore anonymous footnotes.
  304. (should
  305. (equal
  306. "
  307. Text[fn:1][fn::inline][fn:2]
  308. \[fn:1] Def 1
  309. \[fn:2] Def 2
  310. "
  311. (org-test-with-temp-text
  312. "
  313. Text[fn:1][fn::inline][fn:2]
  314. \[fn:2] Def 2
  315. \[fn:1] Def 1"
  316. (let ((org-footnote-section nil)) (org-footnote-sort))
  317. (buffer-string))))
  318. ;; Ignore inline footnotes.
  319. (should
  320. (equal
  321. "
  322. Text[fn:1][fn:label:inline][fn:2]
  323. \[fn:1] Def 1
  324. \[fn:2] Def 2
  325. "
  326. (org-test-with-temp-text
  327. "
  328. Text[fn:1][fn:label:inline][fn:2]
  329. \[fn:2] Def 2
  330. \[fn:1] Def 1"
  331. (let ((org-footnote-section nil)) (org-footnote-sort))
  332. (buffer-string))))
  333. ;; Handle (deeply) nested footnotes.
  334. (should
  335. (equal
  336. "
  337. Text[fn:1][fn:3]
  338. \[fn:1] Def 1[fn:2]
  339. \[fn:2] Def 2
  340. \[fn:3] Def 3
  341. "
  342. (org-test-with-temp-text "
  343. Text[fn:1][fn:3]
  344. \[fn:1] Def 1[fn:2]
  345. \[fn:3] Def 3
  346. \[fn:2] Def 2
  347. "
  348. (let ((org-footnote-section nil)) (org-footnote-sort))
  349. (buffer-string))))
  350. (should
  351. (equal
  352. "
  353. Text[fn:1][fn:4]
  354. \[fn:1] Def 1[fn:2]
  355. \[fn:2] Def 2[fn:3]
  356. \[fn:3] Def 3
  357. \[fn:4] Def 4
  358. "
  359. (org-test-with-temp-text "
  360. Text[fn:1][fn:4]
  361. \[fn:1] Def 1[fn:2]
  362. \[fn:3] Def 3
  363. \[fn:2] Def 2[fn:3]
  364. \[fn:4] Def 4
  365. "
  366. (let ((org-footnote-section nil)) (org-footnote-sort))
  367. (buffer-string))))
  368. ;; When multiple (nested) references are used, make sure to insert
  369. ;; definition only once.
  370. (should
  371. (equal
  372. "
  373. * Section 1
  374. Text[fn:1]
  375. \[fn:1] Def 1
  376. * Section 2
  377. Text[fn:1]"
  378. (org-test-with-temp-text
  379. "
  380. * Section 1
  381. Text[fn:1]
  382. \[fn:1] Def 1
  383. * Section 2
  384. Text[fn:1]"
  385. (let ((org-footnote-section nil)) (org-footnote-sort))
  386. (buffer-string))))
  387. (should
  388. (equal
  389. "
  390. Text[fn:1][fn:4]
  391. \[fn:1] Def 1[fn:2][fn:3]
  392. \[fn:2] Def 2[fn:3]
  393. \[fn:3] Def 3
  394. \[fn:4] Def 4
  395. "
  396. (org-test-with-temp-text "
  397. Text[fn:1][fn:4]
  398. \[fn:1] Def 1[fn:2][fn:3]
  399. \[fn:3] Def 3
  400. \[fn:2] Def 2[fn:3]
  401. \[fn:4] Def 4
  402. "
  403. (let ((org-footnote-section nil)) (org-footnote-sort))
  404. (buffer-string))))
  405. ;; Insert un-referenced definitions at the end.
  406. (should
  407. (equal
  408. "Text[fn:9]
  409. \[fn:9] B
  410. \[fn:1] A
  411. "
  412. (org-test-with-temp-text "Text[fn:9]\n\n[fn:1] A\n[fn:9] B"
  413. (let ((org-footnote-section nil)) (org-footnote-sort))
  414. (buffer-string))))
  415. ;; When sorting, preserve file local variables.
  416. (should
  417. (equal "
  418. Paragraph[fn:1][fn:2]
  419. \[fn:1] Def 1
  420. \[fn:2] Def 2
  421. # Local Variables:
  422. # foo: t
  423. # End:"
  424. (org-test-with-temp-text
  425. "
  426. Paragraph[fn:1][fn:2]
  427. \[fn:2] Def 2
  428. \[fn:1] Def 1
  429. # Local Variables:
  430. # foo: t
  431. # End:"
  432. (let ((org-footnote-section nil)) (org-footnote-sort))
  433. (buffer-string)))))
  434. (ert-deftest test-org-footnote/renumber-fn:N ()
  435. "Test `org-footnote-renumber-fn:N' specifications."
  436. ;; Renumber (inline) references and definitions.
  437. (should
  438. (equal
  439. "Test[fn:1]"
  440. (org-test-with-temp-text "Test[fn:99]"
  441. (org-footnote-renumber-fn:N)
  442. (buffer-string))))
  443. (should
  444. (equal
  445. "Test[fn:1]\n\n[fn:1] 99"
  446. (org-test-with-temp-text "Test[fn:99]\n\n[fn:99] 99"
  447. (org-footnote-renumber-fn:N)
  448. (buffer-string))))
  449. (should
  450. (equal
  451. "Test[fn:1:99]"
  452. (org-test-with-temp-text "Test[fn:99:99]"
  453. (org-footnote-renumber-fn:N)
  454. (buffer-string))))
  455. ;; No-op if there's no numbered footnote.
  456. (should
  457. (equal
  458. "Test[fn:label]\n\n[fn:label] Def"
  459. (org-test-with-temp-text "Test[fn:label]\n\n[fn:label] Def"
  460. (org-footnote-renumber-fn:N)
  461. (buffer-string))))
  462. ;; Definitions without a reference get the highest numbers.
  463. (should
  464. (equal
  465. "Test[fn:1]\n[fn:1] 1\n[fn:2] 99"
  466. (org-test-with-temp-text "Test[fn:1]\n[fn:1] 1\n[fn:99] 99"
  467. (org-footnote-renumber-fn:N)
  468. (buffer-string))))
  469. ;; Sort labels in sequence. Anonymous footnotes are ignored.
  470. (should
  471. (equal
  472. "Test[fn:1][fn:2:def][fn:3]"
  473. (org-test-with-temp-text "Test[fn:4][fn:3:def][fn:2]"
  474. (org-footnote-renumber-fn:N)
  475. (buffer-string))))
  476. (should
  477. (equal
  478. "Test[fn:1][fn::def][fn:2]"
  479. (org-test-with-temp-text "Test[fn:4][fn::def][fn:2]"
  480. (org-footnote-renumber-fn:N)
  481. (buffer-string)))))
  482. (ert-deftest test-org-footnote/normalize ()
  483. "Test `org-footnote-normalize' specifications."
  484. ;; Normalize regular, inline and anonymous references.
  485. (should
  486. (equal
  487. "Test[fn:1]\n\n[fn:1] def\n"
  488. (org-test-with-temp-text "Test[fn:label]\n[fn:label] def"
  489. (let ((org-footnote-section nil)) (org-footnote-normalize))
  490. (buffer-string))))
  491. (should
  492. (equal
  493. "Test[fn:1]\n\n[fn:1] def\n"
  494. (org-test-with-temp-text "Test[fn:label:def]"
  495. (let ((org-footnote-section nil)) (org-footnote-normalize))
  496. (buffer-string))))
  497. (should
  498. (equal
  499. "Test[fn:1]\n\n[fn:1] def\n"
  500. (org-test-with-temp-text "Test[fn::def]"
  501. (let ((org-footnote-section nil)) (org-footnote-normalize))
  502. (buffer-string))))
  503. ;; Normalization includes sorting.
  504. (should
  505. (equal
  506. "Test[fn:1][fn:2]\n\n[fn:1] def2\n\n[fn:2] def\n"
  507. (org-test-with-temp-text "Test[fn:2][fn:1]\n\n[fn:2] def2\n[fn:1] def"
  508. (let ((org-footnote-section nil)) (org-footnote-normalize))
  509. (buffer-string))))
  510. (should
  511. (equal
  512. "Test[fn:1][fn:2]\n\n[fn:1] def\n\n[fn:2] inline\n"
  513. (org-test-with-temp-text "Test[fn:2][fn::inline]\n[fn:2] def\n"
  514. (let ((org-footnote-section nil)) (org-footnote-normalize))
  515. (buffer-string))))
  516. (should
  517. (equal
  518. "Test[fn:1][fn:3]
  519. \[fn:1] def[fn:2]
  520. \[fn:2] inline
  521. \[fn:3] last
  522. "
  523. (org-test-with-temp-text
  524. "Test[fn:lab1][fn:lab2]\n[fn:lab1] def[fn::inline]\n[fn:lab2] last"
  525. (let ((org-footnote-section nil)) (org-footnote-normalize))
  526. (buffer-string))))
  527. ;; When normalizing an inline reference, fill paragraph whenever the
  528. ;; `org-footnote-fill-after-inline-note-extraction' is non-nil.
  529. (should
  530. (equal
  531. "Test[fn:1] Next\n\n[fn:1] def\n"
  532. (org-test-with-temp-text "Test[fn::def]\nNext"
  533. (let ((org-footnote-section nil)
  534. (org-footnote-fill-after-inline-note-extraction t))
  535. (org-footnote-normalize))
  536. (buffer-string))))
  537. ;; Insert un-referenced definitions at the end.
  538. (should
  539. (equal
  540. "Test[fn:1]\nNext\n\n[fn:1] def\n\n[fn:2] A\n"
  541. (org-test-with-temp-text "Test[fn::def]\nNext\n[fn:unref] A"
  542. (let ((org-footnote-section nil)) (org-footnote-normalize))
  543. (buffer-string))))
  544. ;; Preserve file local variables when normalizing.
  545. (should
  546. (equal "
  547. Paragraph[fn:1][fn:2]
  548. \[fn:1] Def 1
  549. \[fn:2] Def 2
  550. # Local Variables:
  551. # foo: t
  552. # End:"
  553. (org-test-with-temp-text
  554. "
  555. Paragraph[fn:foo][fn:bar]
  556. \[fn:bar] Def 2
  557. \[fn:foo] Def 1
  558. # Local Variables:
  559. # foo: t
  560. # End:"
  561. (let ((org-footnote-section nil)) (org-footnote-normalize))
  562. (buffer-string)))))
  563. (provide 'test-org-footnote)
  564. ;;; test-org-footnote.el ends here