test-org-footnote.el 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  1. ;;; test-org-footnote.el --- Tests for org-footnote.el
  2. ;; Copyright (C) 2012-2015, 2019 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 <https://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. ;; Allow new footnotes at the start of a footnote definition.
  87. (should
  88. (string-match-p
  89. "\\[fn:1\\]\\[fn:2\\]"
  90. (org-test-with-temp-text "[fn:1]<point>"
  91. (let ((org-footnote-auto-label t)) (org-footnote-new))
  92. (buffer-string))))
  93. (should
  94. (string-match-p
  95. "\\[fn:1\\] \\[fn:2\\]"
  96. (org-test-with-temp-text "[fn:1] <point>"
  97. (let ((org-footnote-auto-label t)) (org-footnote-new))
  98. (buffer-string))))
  99. (should
  100. (string-match-p
  101. "\\[fn:1\\]\\[fn:2\\]"
  102. (org-test-with-temp-text "[fn:1]<point> \nParagraph"
  103. (let ((org-footnote-auto-label t)) (org-footnote-new))
  104. (buffer-string))))
  105. (should-error
  106. (org-test-with-temp-text "[fn:<point>1]"
  107. (let ((org-footnote-auto-label t)) (org-footnote-new))
  108. (buffer-string)))
  109. (should-error
  110. (org-test-with-temp-text "<point>[fn:1]"
  111. (let ((org-footnote-auto-label t)) (org-footnote-new))
  112. (buffer-string)))
  113. ;; Allow new footnotes in table cells.
  114. (should
  115. (string-match-p
  116. " \\[fn:1\\]"
  117. (org-test-with-temp-text "| <point> |"
  118. (let ((org-footnote-auto-label t)) (org-footnote-new))
  119. (buffer-string))))
  120. (should
  121. (string-match-p
  122. "|\\[fn:1\\]"
  123. (org-test-with-temp-text "|<point> |"
  124. (let ((org-footnote-auto-label t)) (org-footnote-new))
  125. (buffer-string))))
  126. (should
  127. (string-match-p
  128. " \\[fn:1\\]"
  129. (org-test-with-temp-text "| <point>|"
  130. (let ((org-footnote-auto-label t)) (org-footnote-new))
  131. (buffer-string))))
  132. (should
  133. (string-match-p
  134. " \\[fn:1\\]"
  135. (org-test-with-temp-text "| contents <point>|"
  136. (let ((org-footnote-auto-label t)) (org-footnote-new))
  137. (buffer-string))))
  138. ;; When creating a new footnote, move to its definition.
  139. (should
  140. (string=
  141. "[fn:1]"
  142. (org-test-with-temp-text "Text<point>"
  143. (let ((org-footnote-auto-label t)
  144. (org-footnote-auto-adjust nil))
  145. (org-footnote-new))
  146. (buffer-substring-no-properties (line-beginning-position) (point)))))
  147. ;; Re-order and re-label footnotes properly when
  148. ;; `org-footnote-auto-adjust' is non-nil.
  149. (should
  150. (string=
  151. "[fn:1] 1\n\n[fn:2] \n\n[fn:3] 2\n"
  152. (org-test-with-temp-text
  153. "Text[fn:1]Text<point>Text[fn:2]\n\n[fn:1] 1\n\n[fn:2] 2"
  154. (let ((org-footnote-auto-label t)
  155. (org-footnote-auto-adjust t)
  156. (org-footnote-section nil))
  157. (org-footnote-new))
  158. (buffer-substring-no-properties
  159. (line-beginning-position -1)
  160. (line-beginning-position 4)))))
  161. ;; Do not alter file local variables when inserting new definition
  162. ;; label.
  163. (should
  164. (equal "Paragraph[fn:1]
  165. \[fn:1]
  166. # Local Variables:
  167. # foo: t
  168. # End:"
  169. (org-test-with-temp-text
  170. "Paragraph<point>\n# Local Variables:\n# foo: t\n# End:"
  171. (let ((org-footnote-section nil)) (org-footnote-new))
  172. (buffer-string))))
  173. (should
  174. (equal "Paragraph[fn:1]
  175. * Footnotes
  176. \[fn:1]
  177. # Local Variables:
  178. # foo: t
  179. # End:"
  180. (org-test-with-temp-text
  181. "Paragraph<point>\n# Local Variables:\n# foo: t\n# End:"
  182. (let ((org-footnote-section "Footnotes")) (org-footnote-new))
  183. (buffer-string))))
  184. (should
  185. (equal "Para[fn:1]
  186. * Footnotes
  187. :properties:
  188. :custom_id: id
  189. :end:
  190. \[fn:1]"
  191. (org-test-with-temp-text
  192. "Para<point>\n* Footnotes\n:properties:\n:custom_id: id\n:end:"
  193. (let ((org-footnote-section "Footnotes"))
  194. (org-footnote-new))
  195. (org-trim (buffer-string))))))
  196. (ert-deftest test-org-footnote/delete ()
  197. "Test `org-footnote-delete' specifications."
  198. ;; Regular test.
  199. (should
  200. (equal "Paragraph"
  201. (org-test-with-temp-text "Paragraph<point>[fn:1]\n\n[fn:1] Definition"
  202. (org-footnote-delete)
  203. (org-trim (buffer-string)))))
  204. ;; Remove multiple definitions and references.
  205. (should
  206. (equal "Paragraph and another"
  207. (org-test-with-temp-text
  208. "Paragraph<point>[fn:1] and another[fn:1]
  209. \[fn:1] def
  210. \[fn:1] def"
  211. (org-footnote-delete)
  212. (org-trim (buffer-string)))))
  213. ;; Delete inline footnotes and all references.
  214. (should
  215. (equal "Para and"
  216. (org-test-with-temp-text "Para<point>[fn:label:def] and[fn:label]"
  217. (org-footnote-delete)
  218. (org-trim (buffer-string)))))
  219. ;; Delete anonymous footnotes.
  220. (should
  221. (equal "Para"
  222. (let ((org-footnote-section nil))
  223. (org-test-with-temp-text "Para<point>[fn::def]"
  224. (org-footnote-delete)
  225. (org-trim (buffer-string))))))
  226. ;; With an argument, delete footnote with specified label.
  227. (should
  228. (equal "Paragraph[fn:1] and another\n\n[fn:1] def"
  229. (let ((org-footnote-section nil))
  230. (org-test-with-temp-text
  231. "Paragraph[fn:1] and another[fn:2]\n\n[fn:1] def\n\n[fn:2] def2"
  232. (org-footnote-delete "2")
  233. (org-trim (buffer-string))))))
  234. ;; Error when no argument is specified at point is not at a footnote
  235. ;; reference.
  236. (should-error
  237. (org-test-with-temp-text "Para[fn:1]\n\n[fn:1] Def"
  238. (org-footnote-delete)))
  239. ;; Correctly delete footnotes with multiple paragraphs.
  240. (should
  241. (equal "Para\n\n\nOutside footnote."
  242. (let ((org-footnote-section nil))
  243. (org-test-with-temp-text
  244. "Para[fn:1]\n\n[fn:1] para1\n\npara2\n\n\nOutside footnote."
  245. (org-footnote-delete "1")
  246. (org-trim (buffer-string))))))
  247. ;; Remove blank lines above the footnote but preserve those after
  248. ;; it.
  249. (should
  250. (equal "Text\n\n\nOther text."
  251. (let ((org-footnote-section nil))
  252. (org-test-with-temp-text
  253. "Text[fn:1]\n\n[fn:1] Definition.\n\n\nOther text."
  254. (org-footnote-delete "1")
  255. (buffer-string)))))
  256. ;; Preserve file local variables when deleting a footnote.
  257. (should
  258. (equal
  259. "Paragraph\n# Local Variables:\n# foo: t\n# End:"
  260. (org-test-with-temp-text
  261. "Paragraph[fn:1]\n[fn:1] Def 1\n# Local Variables:\n# foo: t\n# End:"
  262. (let ((org-footnote-section nil)) (org-footnote-delete "1"))
  263. (buffer-string)))))
  264. (ert-deftest test-org-footnote/goto-definition ()
  265. "Test `org-footnote-goto-definition' specifications."
  266. ;; Error on unknown definitions.
  267. (should-error
  268. (org-test-with-temp-text "No footnote definition"
  269. (org-footnote-goto-definition "1")))
  270. ;; Error when trying to reach a definition outside narrowed part of
  271. ;; buffer.
  272. (should-error
  273. (org-test-with-temp-text "Some text<point>\n[fn:1] Definition."
  274. (narrow-to-region (point-min) (point))
  275. (org-footnote-goto-definition "1")))
  276. (should-error
  277. (org-test-with-temp-text "[fn:1] Definition.\n<point>Some text"
  278. (narrow-to-region (point) (point-max))
  279. (org-footnote-goto-definition "1")))
  280. ;; Otherwise, move at the beginning of the definition, including
  281. ;; anonymous footnotes.
  282. (should
  283. (equal
  284. " Definition."
  285. (org-test-with-temp-text "Some text\n[fn:1] Definition."
  286. (org-footnote-goto-definition "1")
  287. (buffer-substring (point) (point-max)))))
  288. (should
  289. (equal
  290. "definition]"
  291. (org-test-with-temp-text "Some text[fn:label:definition]"
  292. (org-footnote-goto-definition "label")
  293. (buffer-substring (point) (point-max))))))
  294. (ert-deftest test-org-footnote/goto-previous-reference ()
  295. "Test `org-footnote-goto-previous-reference' specifications."
  296. ;; Error on unknown reference.
  297. (should-error
  298. (org-test-with-temp-text "No footnote reference"
  299. (org-footnote-goto-previous-reference "1")))
  300. ;; Error when trying to reach a reference outside narrowed part of
  301. ;; buffer.
  302. (should-error
  303. (org-test-with-temp-text "Some text<point>\nReference[fn:1]."
  304. (narrow-to-region (point-min) (point))
  305. (org-footnote-goto-previous-reference "1")))
  306. ;; Otherwise, move to closest reference from point.
  307. (should
  308. (org-test-with-temp-text "First reference[fn:1]\nReference[fn:1].<point>"
  309. (org-footnote-goto-previous-reference "1")
  310. (= (line-end-position) (point-max))))
  311. (should
  312. (org-test-with-temp-text "First reference[fn:1]\nReference[fn:1]."
  313. (org-footnote-goto-previous-reference "1")
  314. (= (line-beginning-position) (point-min)))))
  315. (ert-deftest test-org-footnote/sort ()
  316. "Test `org-footnote-sort' specifications."
  317. ;; Reorder definitions with a nil `org-footnote-section'. In this
  318. ;; case each definition is written at the end of the section
  319. ;; containing its first reference.
  320. (should
  321. (equal
  322. "
  323. Text[fn:1][fn:2]
  324. \[fn:1] Def 1
  325. \[fn:2] Def 2
  326. "
  327. (org-test-with-temp-text "
  328. Text[fn:1][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. (should
  334. (equal
  335. "
  336. * H1
  337. Text[fn:1]
  338. \[fn:1] Def 1
  339. * H2
  340. Text[fn:2]
  341. \[fn:2] Def 2
  342. "
  343. (org-test-with-temp-text "
  344. * H1
  345. Text[fn:1]
  346. * H2
  347. Text[fn:2]
  348. \[fn:1] Def 1
  349. \[fn:2] Def 2
  350. "
  351. (let ((org-footnote-section nil)) (org-footnote-sort))
  352. (buffer-string))))
  353. ;; Reorder definitions with a non-nil `org-footnote-section'.
  354. (should
  355. (equal
  356. "
  357. Text[fn:1][fn:2]
  358. * Footnotes
  359. \[fn:1] Def 1
  360. \[fn:2] Def 2
  361. "
  362. (org-test-with-temp-text "
  363. Text[fn:1][fn:2]
  364. \[fn:2] Def 2
  365. \[fn:1] Def 1"
  366. (let ((org-footnote-section "Footnotes")) (org-footnote-sort))
  367. (buffer-string))))
  368. ;; When `org-footnote-section' is non-nil, clear previous footnote
  369. ;; sections.
  370. (should
  371. (equal
  372. "
  373. Text[fn:1]
  374. * Headline
  375. * Other headline
  376. * Footnotes
  377. \[fn:1] Def 1
  378. "
  379. (org-test-with-temp-text "
  380. Text[fn:1]
  381. * Footnotes
  382. \[fn:1] Def 1
  383. * Headline
  384. ** Footnotes
  385. * Other headline"
  386. (let ((org-footnote-section "Footnotes")) (org-footnote-sort))
  387. (buffer-string))))
  388. ;; Ignore anonymous footnotes.
  389. (should
  390. (equal
  391. "
  392. Text[fn:1][fn::inline][fn:2]
  393. \[fn:1] Def 1
  394. \[fn:2] Def 2
  395. "
  396. (org-test-with-temp-text
  397. "
  398. Text[fn:1][fn::inline][fn:2]
  399. \[fn:2] Def 2
  400. \[fn:1] Def 1"
  401. (let ((org-footnote-section nil)) (org-footnote-sort))
  402. (buffer-string))))
  403. ;; Ignore inline footnotes.
  404. (should
  405. (equal
  406. "
  407. Text[fn:1][fn:label:inline][fn:2]
  408. \[fn:1] Def 1
  409. \[fn:2] Def 2
  410. "
  411. (org-test-with-temp-text
  412. "
  413. Text[fn:1][fn:label:inline][fn:2]
  414. \[fn:2] Def 2
  415. \[fn:1] Def 1"
  416. (let ((org-footnote-section nil)) (org-footnote-sort))
  417. (buffer-string))))
  418. ;; Handle (deeply) nested footnotes.
  419. (should
  420. (equal
  421. "
  422. Text[fn:1][fn:3]
  423. \[fn:1] Def 1[fn:2]
  424. \[fn:2] Def 2
  425. \[fn:3] Def 3
  426. "
  427. (org-test-with-temp-text "
  428. Text[fn:1][fn:3]
  429. \[fn:1] Def 1[fn:2]
  430. \[fn:3] Def 3
  431. \[fn:2] Def 2
  432. "
  433. (let ((org-footnote-section nil)) (org-footnote-sort))
  434. (buffer-string))))
  435. (should
  436. (equal
  437. "
  438. Text[fn:1][fn:4]
  439. \[fn:1] Def 1[fn:2]
  440. \[fn:2] Def 2[fn:3]
  441. \[fn:3] Def 3
  442. \[fn:4] Def 4
  443. "
  444. (org-test-with-temp-text "
  445. Text[fn:1][fn:4]
  446. \[fn:1] Def 1[fn:2]
  447. \[fn:3] Def 3
  448. \[fn:2] Def 2[fn:3]
  449. \[fn:4] Def 4
  450. "
  451. (let ((org-footnote-section nil)) (org-footnote-sort))
  452. (buffer-string))))
  453. ;; When multiple (nested) references are used, make sure to insert
  454. ;; definition only once.
  455. (should
  456. (equal
  457. "
  458. * Section 1
  459. Text[fn:1]
  460. \[fn:1] Def 1
  461. * Section 2
  462. Text[fn:1]"
  463. (org-test-with-temp-text
  464. "
  465. * Section 1
  466. Text[fn:1]
  467. \[fn:1] Def 1
  468. * Section 2
  469. Text[fn:1]"
  470. (let ((org-footnote-section nil)) (org-footnote-sort))
  471. (buffer-string))))
  472. (should
  473. (equal
  474. "
  475. Text[fn:1][fn:4]
  476. \[fn:1] Def 1[fn:2][fn:3]
  477. \[fn:2] Def 2[fn:3]
  478. \[fn:3] Def 3
  479. \[fn:4] Def 4
  480. "
  481. (org-test-with-temp-text "
  482. Text[fn:1][fn:4]
  483. \[fn:1] Def 1[fn:2][fn:3]
  484. \[fn:3] Def 3
  485. \[fn:2] Def 2[fn:3]
  486. \[fn:4] Def 4
  487. "
  488. (let ((org-footnote-section nil)) (org-footnote-sort))
  489. (buffer-string))))
  490. ;; Insert un-referenced definitions at the end.
  491. (should
  492. (equal
  493. "Text[fn:9]
  494. \[fn:9] B
  495. \[fn:1] A
  496. "
  497. (org-test-with-temp-text "Text[fn:9]\n\n[fn:1] A\n[fn:9] B"
  498. (let ((org-footnote-section nil)) (org-footnote-sort))
  499. (buffer-string))))
  500. ;; When sorting, preserve file local variables.
  501. (should
  502. (equal "
  503. Paragraph[fn:1][fn:2]
  504. \[fn:1] Def 1
  505. \[fn:2] Def 2
  506. # Local Variables:
  507. # foo: t
  508. # End:"
  509. (org-test-with-temp-text
  510. "
  511. Paragraph[fn:1][fn:2]
  512. \[fn:2] Def 2
  513. \[fn:1] Def 1
  514. # Local Variables:
  515. # foo: t
  516. # End:"
  517. (let ((org-footnote-section nil)) (org-footnote-sort))
  518. (buffer-string)))))
  519. (ert-deftest test-org-footnote/renumber-fn:N ()
  520. "Test `org-footnote-renumber-fn:N' specifications."
  521. ;; Renumber (inline) references and definitions.
  522. (should
  523. (equal
  524. "Test[fn:1]"
  525. (org-test-with-temp-text "Test[fn:99]"
  526. (org-footnote-renumber-fn:N)
  527. (buffer-string))))
  528. (should
  529. (equal
  530. "Test[fn:1]\n\n[fn:1] 99"
  531. (org-test-with-temp-text "Test[fn:99]\n\n[fn:99] 99"
  532. (org-footnote-renumber-fn:N)
  533. (buffer-string))))
  534. (should
  535. (equal
  536. "Test[fn:1:99]"
  537. (org-test-with-temp-text "Test[fn:99:99]"
  538. (org-footnote-renumber-fn:N)
  539. (buffer-string))))
  540. ;; No-op if there's no numbered footnote.
  541. (should
  542. (equal
  543. "Test[fn:label]\n\n[fn:label] Def"
  544. (org-test-with-temp-text "Test[fn:label]\n\n[fn:label] Def"
  545. (org-footnote-renumber-fn:N)
  546. (buffer-string))))
  547. ;; Definitions without a reference get the highest numbers.
  548. (should
  549. (equal
  550. "Test[fn:1]\n[fn:1] 1\n[fn:2] 99"
  551. (org-test-with-temp-text "Test[fn:1]\n[fn:1] 1\n[fn:99] 99"
  552. (org-footnote-renumber-fn:N)
  553. (buffer-string))))
  554. ;; Sort labels in sequence. Anonymous footnotes are ignored.
  555. (should
  556. (equal
  557. "Test[fn:1][fn:2:def][fn:3]"
  558. (org-test-with-temp-text "Test[fn:4][fn:3:def][fn:2]"
  559. (org-footnote-renumber-fn:N)
  560. (buffer-string))))
  561. (should
  562. (equal
  563. "Test[fn:1][fn::def][fn:2]"
  564. (org-test-with-temp-text "Test[fn:4][fn::def][fn:2]"
  565. (org-footnote-renumber-fn:N)
  566. (buffer-string)))))
  567. (ert-deftest test-org-footnote/normalize ()
  568. "Test `org-footnote-normalize' specifications."
  569. ;; Normalize regular, inline and anonymous references.
  570. (should
  571. (equal
  572. "Test[fn:1]\n\n[fn:1] def\n"
  573. (org-test-with-temp-text "Test[fn:label]\n[fn:label] def"
  574. (let ((org-footnote-section nil)) (org-footnote-normalize))
  575. (buffer-string))))
  576. (should
  577. (equal
  578. "Test[fn:1]\n\n[fn:1] def\n"
  579. (org-test-with-temp-text "Test[fn:label:def]"
  580. (let ((org-footnote-section nil)) (org-footnote-normalize))
  581. (buffer-string))))
  582. (should
  583. (equal
  584. "Test[fn:1]\n\n[fn:1] def\n"
  585. (org-test-with-temp-text "Test[fn::def]"
  586. (let ((org-footnote-section nil)) (org-footnote-normalize))
  587. (buffer-string))))
  588. ;; Normalization includes sorting.
  589. (should
  590. (equal
  591. "Test[fn:1][fn:2]\n\n[fn:1] def2\n\n[fn:2] def\n"
  592. (org-test-with-temp-text "Test[fn:2][fn:1]\n\n[fn:2] def2\n[fn:1] def"
  593. (let ((org-footnote-section nil)) (org-footnote-normalize))
  594. (buffer-string))))
  595. (should
  596. (equal
  597. "Test[fn:1][fn:2]\n\n[fn:1] def\n\n[fn:2] inline\n"
  598. (org-test-with-temp-text "Test[fn:2][fn::inline]\n[fn:2] def\n"
  599. (let ((org-footnote-section nil)) (org-footnote-normalize))
  600. (buffer-string))))
  601. (should
  602. (equal
  603. "Test[fn:1][fn:3]
  604. \[fn:1] def[fn:2]
  605. \[fn:2] inline
  606. \[fn:3] last
  607. "
  608. (org-test-with-temp-text
  609. "Test[fn:lab1][fn:lab2]\n[fn:lab1] def[fn::inline]\n[fn:lab2] last"
  610. (let ((org-footnote-section nil)) (org-footnote-normalize))
  611. (buffer-string))))
  612. ;; When normalizing an inline reference, fill paragraph whenever the
  613. ;; `org-footnote-fill-after-inline-note-extraction' is non-nil.
  614. (should
  615. (equal
  616. "Test[fn:1] Next\n\n[fn:1] def\n"
  617. (org-test-with-temp-text "Test[fn::def]\nNext"
  618. (let ((org-footnote-section nil)
  619. (org-footnote-fill-after-inline-note-extraction t))
  620. (org-footnote-normalize))
  621. (buffer-string))))
  622. ;; Insert un-referenced definitions at the end.
  623. (should
  624. (equal
  625. "Test[fn:1]\nNext\n\n[fn:1] def\n\n[fn:2] A\n"
  626. (org-test-with-temp-text "Test[fn::def]\nNext\n[fn:unref] A"
  627. (let ((org-footnote-section nil)) (org-footnote-normalize))
  628. (buffer-string))))
  629. ;; Preserve file local variables when normalizing.
  630. (should
  631. (equal "
  632. Paragraph[fn:1][fn:2]
  633. \[fn:1] Def 1
  634. \[fn:2] Def 2
  635. # Local Variables:
  636. # foo: t
  637. # End:"
  638. (org-test-with-temp-text
  639. "
  640. Paragraph[fn:foo][fn:bar]
  641. \[fn:bar] Def 2
  642. \[fn:foo] Def 1
  643. # Local Variables:
  644. # foo: t
  645. # End:"
  646. (let ((org-footnote-section nil)) (org-footnote-normalize))
  647. (buffer-string)))))
  648. (provide 'test-org-footnote)
  649. ;;; test-org-footnote.el ends here