test-org-footnote.el 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  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 " [[http://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. (ert-deftest test-org-footnote/delete ()
  110. "Test `org-footnote-delete' specifications."
  111. ;; Regular test.
  112. (should
  113. (equal "Paragraph"
  114. (org-test-with-temp-text "Paragraph<point>[fn:1]\n\n[fn:1] Definition"
  115. (org-footnote-delete)
  116. (org-trim (buffer-string)))))
  117. ;; Remove multiple definitions and references.
  118. (should
  119. (equal "Paragraph and another"
  120. (org-test-with-temp-text
  121. "Paragraph<point>[fn:1] and another[fn:1]
  122. \[fn:1] def
  123. \[fn:1] def"
  124. (org-footnote-delete)
  125. (org-trim (buffer-string)))))
  126. ;; Delete inline footnotes and all references.
  127. (should
  128. (equal "Para and"
  129. (org-test-with-temp-text "Para<point>[fn:label:def] and[fn:label]"
  130. (org-footnote-delete)
  131. (org-trim (buffer-string)))))
  132. ;; Delete anonymous footnotes.
  133. (should
  134. (equal "Para"
  135. (let ((org-footnote-section nil))
  136. (org-test-with-temp-text "Para<point>[fn::def]"
  137. (org-footnote-delete)
  138. (org-trim (buffer-string))))))
  139. ;; With an argument, delete footnote with specified label.
  140. (should
  141. (equal "Paragraph[fn:1] and another\n\n[fn:1] def"
  142. (let ((org-footnote-section nil))
  143. (org-test-with-temp-text
  144. "Paragraph[fn:1] and another[fn:2]\n\n[fn:1] def\n\n[fn:2] def2"
  145. (org-footnote-delete "2")
  146. (org-trim (buffer-string))))))
  147. ;; Error when no argument is specified at point is not at a footnote
  148. ;; reference.
  149. (should-error
  150. (org-test-with-temp-text "Para[fn:1]\n\n[fn:1] Def"
  151. (org-footnote-delete)))
  152. ;; Correctly delete footnotes with multiple paragraphs.
  153. (should
  154. (equal "Para\n\n\nOutside footnote."
  155. (let ((org-footnote-section nil))
  156. (org-test-with-temp-text
  157. "Para[fn:1]\n\n[fn:1] para1\n\npara2\n\n\nOutside footnote."
  158. (org-footnote-delete "1")
  159. (org-trim (buffer-string))))))
  160. ;; Remove blank lines above the footnote but preserve those after
  161. ;; it.
  162. (should
  163. (equal "Text\n\n\nOther text."
  164. (let ((org-footnote-section nil))
  165. (org-test-with-temp-text
  166. "Text[fn:1]\n\n[fn:1] Definition.\n\n\nOther text."
  167. (org-footnote-delete "1")
  168. (buffer-string))))))
  169. (ert-deftest test-org-footnote/goto-definition ()
  170. "Test `org-footnote-goto-definition' specifications."
  171. ;; Error on unknown definitions.
  172. (should-error
  173. (org-test-with-temp-text "No footnote definition"
  174. (org-footnote-goto-definition "1")))
  175. ;; Error when trying to reach a definition outside narrowed part of
  176. ;; buffer.
  177. (should-error
  178. (org-test-with-temp-text "Some text<point>\n[fn:1] Definition."
  179. (narrow-to-region (point-min) (point))
  180. (org-footnote-goto-definition "1")))
  181. (should-error
  182. (org-test-with-temp-text "[fn:1] Definition.\n<point>Some text"
  183. (narrow-to-region (point) (point-max))
  184. (org-footnote-goto-definition "1")))
  185. ;; Otherwise, move at the beginning of the definition, including
  186. ;; anonymous footnotes.
  187. (should
  188. (equal
  189. "Definition."
  190. (org-test-with-temp-text "Some text\n[fn:1] Definition."
  191. (org-footnote-goto-definition "1")
  192. (buffer-substring (point) (point-max)))))
  193. (should
  194. (equal
  195. "definition]"
  196. (org-test-with-temp-text "Some text[fn:label:definition]"
  197. (org-footnote-goto-definition "label")
  198. (buffer-substring (point) (point-max))))))
  199. (ert-deftest test-org-footnote/sort ()
  200. "Test `org-footnote-sort' specifications."
  201. ;; Reorder definitions with a nil `org-footnote-section'. In this
  202. ;; case each definition is written at the end of the section
  203. ;; containing its first reference.
  204. (should
  205. (equal
  206. "
  207. Text[fn:1][fn:2]
  208. \[fn:1] Def 1
  209. \[fn:2] Def 2
  210. "
  211. (org-test-with-temp-text "
  212. Text[fn:1][fn:2]
  213. \[fn:2] Def 2
  214. \[fn:1] Def 1"
  215. (let ((org-footnote-section nil)) (org-footnote-sort))
  216. (buffer-string))))
  217. (should
  218. (equal
  219. "
  220. * H1
  221. Text[fn:1]
  222. \[fn:1] Def 1
  223. * H2
  224. Text[fn:2]
  225. \[fn:2] Def 2
  226. "
  227. (org-test-with-temp-text "
  228. * H1
  229. Text[fn:1]
  230. * H2
  231. Text[fn:2]
  232. \[fn:1] Def 1
  233. \[fn:2] Def 2
  234. "
  235. (let ((org-footnote-section nil)) (org-footnote-sort))
  236. (buffer-string))))
  237. ;; Reorder definitions with a non-nil `org-footnote-section'.
  238. (should
  239. (equal
  240. "
  241. Text[fn:1][fn:2]
  242. * Footnotes
  243. \[fn:1] Def 1
  244. \[fn:2] Def 2
  245. "
  246. (org-test-with-temp-text "
  247. Text[fn:1][fn:2]
  248. \[fn:2] Def 2
  249. \[fn:1] Def 1"
  250. (let ((org-footnote-section "Footnotes")) (org-footnote-sort))
  251. (buffer-string))))
  252. ;; When `org-footnote-section' is non-nil, clear previous footnote
  253. ;; sections.
  254. (should
  255. (equal
  256. "
  257. Text[fn:1]
  258. * Headline
  259. * Other headline
  260. * Footnotes
  261. \[fn:1] Def 1
  262. "
  263. (org-test-with-temp-text "
  264. Text[fn:1]
  265. * Footnotes
  266. \[fn:1] Def 1
  267. * Headline
  268. ** Footnotes
  269. * Other headline"
  270. (let ((org-footnote-section "Footnotes")) (org-footnote-sort))
  271. (buffer-string))))
  272. ;; Ignore anonymous footnotes.
  273. (should
  274. (equal
  275. "
  276. Text[fn:1][fn::inline][fn:2]
  277. \[fn:1] Def 1
  278. \[fn:2] Def 2
  279. "
  280. (org-test-with-temp-text
  281. "
  282. Text[fn:1][fn::inline][fn:2]
  283. \[fn:2] Def 2
  284. \[fn:1] Def 1"
  285. (let ((org-footnote-section nil)) (org-footnote-sort))
  286. (buffer-string))))
  287. ;; Ignore inline footnotes.
  288. (should
  289. (equal
  290. "
  291. Text[fn:1][fn:label:inline][fn:2]
  292. \[fn:1] Def 1
  293. \[fn:2] Def 2
  294. "
  295. (org-test-with-temp-text
  296. "
  297. Text[fn:1][fn:label:inline][fn:2]
  298. \[fn:2] Def 2
  299. \[fn:1] Def 1"
  300. (let ((org-footnote-section nil)) (org-footnote-sort))
  301. (buffer-string))))
  302. ;; Handle (deeply) nested footnotes.
  303. (should
  304. (equal
  305. "
  306. Text[fn:1][fn:3]
  307. \[fn:1] Def 1[fn:2]
  308. \[fn:2] Def 2
  309. \[fn:3] Def 3
  310. "
  311. (org-test-with-temp-text "
  312. Text[fn:1][fn:3]
  313. \[fn:1] Def 1[fn:2]
  314. \[fn:3] Def 3
  315. \[fn:2] Def 2
  316. "
  317. (let ((org-footnote-section nil)) (org-footnote-sort))
  318. (buffer-string))))
  319. (should
  320. (equal
  321. "
  322. Text[fn:1][fn:4]
  323. \[fn:1] Def 1[fn:2]
  324. \[fn:2] Def 2[fn:3]
  325. \[fn:3] Def 3
  326. \[fn:4] Def 4
  327. "
  328. (org-test-with-temp-text "
  329. Text[fn:1][fn:4]
  330. \[fn:1] Def 1[fn:2]
  331. \[fn:3] Def 3
  332. \[fn:2] Def 2[fn:3]
  333. \[fn:4] Def 4
  334. "
  335. (let ((org-footnote-section nil)) (org-footnote-sort))
  336. (buffer-string))))
  337. ;; When multiple (nested) references are used, make sure to insert
  338. ;; definition only once.
  339. (should
  340. (equal
  341. "
  342. * Section 1
  343. Text[fn:1]
  344. \[fn:1] Def 1
  345. * Section 2
  346. Text[fn:1]"
  347. (org-test-with-temp-text
  348. "
  349. * Section 1
  350. Text[fn:1]
  351. \[fn:1] Def 1
  352. * Section 2
  353. Text[fn:1]"
  354. (let ((org-footnote-section nil)) (org-footnote-sort))
  355. (buffer-string))))
  356. (should
  357. (equal
  358. "
  359. Text[fn:1][fn:4]
  360. \[fn:1] Def 1[fn:2][fn:3]
  361. \[fn:2] Def 2[fn:3]
  362. \[fn:3] Def 3
  363. \[fn:4] Def 4
  364. "
  365. (org-test-with-temp-text "
  366. Text[fn:1][fn:4]
  367. \[fn:1] Def 1[fn:2][fn:3]
  368. \[fn:3] Def 3
  369. \[fn:2] Def 2[fn:3]
  370. \[fn:4] Def 4
  371. "
  372. (let ((org-footnote-section nil)) (org-footnote-sort))
  373. (buffer-string))))
  374. ;; Insert un-referenced definitions at the end.
  375. (should
  376. (equal
  377. "Text[fn:9]
  378. \[fn:9] B
  379. \[fn:1] A
  380. "
  381. (org-test-with-temp-text "Text[fn:9]\n\n[fn:1] A\n[fn:9] B"
  382. (let ((org-footnote-section nil)) (org-footnote-sort))
  383. (buffer-string)))))
  384. (ert-deftest test-org-footnote/renumber-fn:N ()
  385. "Test `org-footnote-renumber-fn:N' specifications."
  386. ;; Renumber (inline) references and definitions.
  387. (should
  388. (equal
  389. "Test[fn:1]"
  390. (org-test-with-temp-text "Test[fn:99]"
  391. (org-footnote-renumber-fn:N)
  392. (buffer-string))))
  393. (should
  394. (equal
  395. "Test[fn:1]\n\n[fn:1] 99"
  396. (org-test-with-temp-text "Test[fn:99]\n\n[fn:99] 99"
  397. (org-footnote-renumber-fn:N)
  398. (buffer-string))))
  399. (should
  400. (equal
  401. "Test[fn:1:99]"
  402. (org-test-with-temp-text "Test[fn:99:99]"
  403. (org-footnote-renumber-fn:N)
  404. (buffer-string))))
  405. ;; No-op if there's no numbered footnote.
  406. (should
  407. (equal
  408. "Test[fn:label]\n\n[fn:label] Def"
  409. (org-test-with-temp-text "Test[fn:label]\n\n[fn:label] Def"
  410. (org-footnote-renumber-fn:N)
  411. (buffer-string))))
  412. ;; Definitions without a reference get the highest numbers.
  413. (should
  414. (equal
  415. "Test[fn:1]\n[fn:1] 1\n[fn:2] 99"
  416. (org-test-with-temp-text "Test[fn:1]\n[fn:1] 1\n[fn:99] 99"
  417. (org-footnote-renumber-fn:N)
  418. (buffer-string))))
  419. ;; Sort labels in sequence. Anonymous footnotes are ignored.
  420. (should
  421. (equal
  422. "Test[fn:1][fn:2:def][fn:3]"
  423. (org-test-with-temp-text "Test[fn:4][fn:3:def][fn:2]"
  424. (org-footnote-renumber-fn:N)
  425. (buffer-string))))
  426. (should
  427. (equal
  428. "Test[fn:1][fn::def][fn:2]"
  429. (org-test-with-temp-text "Test[fn:4][fn::def][fn:2]"
  430. (org-footnote-renumber-fn:N)
  431. (buffer-string)))))
  432. (ert-deftest test-org-footnote/normalize ()
  433. "Test `org-footnote-normalize' specifications."
  434. ;; Normalize regular, inline and anonymous references.
  435. (should
  436. (equal
  437. "Test[fn:1]\n\n[fn:1] def\n"
  438. (org-test-with-temp-text "Test[fn:label]\n[fn:label] def"
  439. (let ((org-footnote-section nil)) (org-footnote-normalize))
  440. (buffer-string))))
  441. (should
  442. (equal
  443. "Test[fn:1]\n\n[fn:1] def\n"
  444. (org-test-with-temp-text "Test[fn:label:def]"
  445. (let ((org-footnote-section nil)) (org-footnote-normalize))
  446. (buffer-string))))
  447. (should
  448. (equal
  449. "Test[fn:1]\n\n[fn:1] def\n"
  450. (org-test-with-temp-text "Test[fn::def]"
  451. (let ((org-footnote-section nil)) (org-footnote-normalize))
  452. (buffer-string))))
  453. ;; Normalization includes sorting.
  454. (should
  455. (equal
  456. "Test[fn:1][fn:2]\n\n[fn:1] def2\n\n[fn:2] def\n"
  457. (org-test-with-temp-text "Test[fn:2][fn:1]\n\n[fn:2] def2\n[fn:1] def"
  458. (let ((org-footnote-section nil)) (org-footnote-normalize))
  459. (buffer-string))))
  460. (should
  461. (equal
  462. "Test[fn:1][fn:2]\n\n[fn:1] def\n\n[fn:2] inline\n"
  463. (org-test-with-temp-text "Test[fn:2][fn::inline]\n[fn:2] def\n"
  464. (let ((org-footnote-section nil)) (org-footnote-normalize))
  465. (buffer-string))))
  466. (should
  467. (equal
  468. "Test[fn:1][fn:3]
  469. \[fn:1] def[fn:2]
  470. \[fn:2] inline
  471. \[fn:3] last
  472. "
  473. (org-test-with-temp-text
  474. "Test[fn:lab1][fn:lab2]\n[fn:lab1] def[fn::inline]\n[fn:lab2] last"
  475. (let ((org-footnote-section nil)) (org-footnote-normalize))
  476. (buffer-string))))
  477. ;; When normalizing an inline reference, fill paragraph whenever the
  478. ;; `org-footnote-fill-after-inline-note-extraction' is non-nil.
  479. (should
  480. (equal
  481. "Test[fn:1] Next\n\n[fn:1] def\n"
  482. (org-test-with-temp-text "Test[fn::def]\nNext"
  483. (let ((org-footnote-section nil)
  484. (org-footnote-fill-after-inline-note-extraction t))
  485. (org-footnote-normalize))
  486. (buffer-string))))
  487. ;; Insert un-referenced definitions at the end.
  488. (should
  489. (equal
  490. "Test[fn:1]\nNext\n\n[fn:1] def\n\n[fn:2] A\n"
  491. (org-test-with-temp-text "Test[fn::def]\nNext\n[fn:unref] A"
  492. (let ((org-footnote-section nil)) (org-footnote-normalize))
  493. (buffer-string)))))
  494. (provide 'test-org-footnote)
  495. ;;; test-org-footnote.el ends here