test-org-footnote.el 19 KB

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