test-org-footnote.el 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. ;;; test-org-footnote.el --- Tests for org-footnote.el
  2. ;; Copyright (C) 2012 Nicolas Goaziou
  3. ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
  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/normalize-in-org ()
  16. "Test specifications for `org-footnote-normalize' in an Org buffer."
  17. ;; 1. With a non-nil `org-footnote-section'.
  18. (let ((org-footnote-section "Footnotes")
  19. (org-blank-before-new-entry '((heading . auto))))
  20. ;; 1.1. Normalize each type of footnote: standard, labelled,
  21. ;; numbered, inline, anonymous.
  22. (org-test-with-temp-text
  23. "Paragraph[fn:1][fn:label][1][fn:inline:Inline][fn::Anonymous]
  24. * Footnotes
  25. \[fn:1] Standard
  26. \[fn:label] Labelled
  27. \[1] Numbered"
  28. (org-footnote-normalize)
  29. (should
  30. (equal (buffer-string)
  31. "Paragraph[1][2][3][4][5]
  32. * Footnotes
  33. \[1] Standard
  34. \[2] Labelled
  35. \[3] Numbered
  36. \[4] Inline
  37. \[5] Anonymous
  38. ")))
  39. ;; 1.2. When no footnote section is present, create it. Follow
  40. ;; `org-blank-before-new-entry' specifications when doing so.
  41. (org-test-with-temp-text "Paragraph[fn:1]\n\n[fn:1] Definition"
  42. (org-footnote-normalize)
  43. (should (equal (buffer-string)
  44. "Paragraph[1]\n\n* Footnotes\n\n[1] Definition")))
  45. (org-test-with-temp-text "Paragraph[fn:1]\n* Head1\n[fn:1] Definition"
  46. (let ((org-blank-before-new-entry '((heading))))
  47. (org-footnote-normalize))
  48. (should (equal (buffer-string)
  49. "Paragraph[1]\n* Head1\n* Footnotes\n\n[1] Definition")))
  50. ;; 1.3. When the footnote section is misplaced, move it at the end
  51. ;; of the buffer.
  52. (org-test-with-temp-text "* Head1
  53. Body[fn:1]
  54. * Footnotes
  55. \[fn:1] Definition 1
  56. * Head2"
  57. (org-footnote-normalize)
  58. (should
  59. (equal (buffer-string)
  60. "* Head1
  61. Body[1]
  62. * Head2
  63. * Footnotes
  64. \[1] Definition 1"))))
  65. ;; 2. With a nil `org-footnote-section'.
  66. (let ((org-footnote-section nil))
  67. ;; 2.1. Normalize each type of footnote: standard, labelled,
  68. ;; numbered, inline, anonymous.
  69. (org-test-with-temp-text
  70. "Paragraph[fn:1][fn:label][1][fn:inline:Inline][fn::Anonymous]
  71. \[fn:1] Standard
  72. \[fn:label] Labelled
  73. \[1] Numbered"
  74. (org-footnote-normalize)
  75. (should
  76. (equal (buffer-string)
  77. "Paragraph[1][2][3][4][5]
  78. \[1] Standard
  79. \[2] Labelled
  80. \[3] Numbered
  81. \[4] Inline
  82. \[5] Anonymous
  83. ")))
  84. ;; 2.2. Put each footnote definition at the end of the section
  85. ;; containing its first reference.
  86. (org-test-with-temp-text
  87. "* Head 1
  88. Text[fn:1:Def1]
  89. * Head 2
  90. Text[fn:1]
  91. * Head 3
  92. Text[fn:2:Def2]"
  93. (org-footnote-normalize)
  94. (should
  95. (equal (buffer-string)
  96. "* Head 1
  97. Text[1]
  98. \[1] Def1
  99. * Head 2
  100. Text[1]
  101. * Head 3
  102. Text[2]
  103. \[2] Def2
  104. ")))))
  105. (ert-deftest test-org-footnote/normalize-outside-org ()
  106. "Test `org-footnote-normalize' specifications for buffers not in Org mode."
  107. ;; 1. In a non-Org buffer, footnotes definitions are always put at
  108. ;; its end.
  109. (let ((org-footnote-tag-for-non-org-mode-files nil))
  110. (with-temp-buffer
  111. (insert "Paragraph[fn:1][fn:label][1][fn:inline:Inline][fn::Anonymous]
  112. \[fn:1] Standard
  113. \[fn:label] Labelled
  114. \[1] Numbered
  115. Some additional text.")
  116. (org-footnote-normalize)
  117. (should
  118. (equal (buffer-string)
  119. "Paragraph[1][2][3][4][5]
  120. Some additional text.
  121. \[1] Standard
  122. \[2] Labelled
  123. \[3] Numbered
  124. \[4] Inline
  125. \[5] Anonymous"))))
  126. ;; 2. With a special tag.
  127. (let ((org-footnote-tag-for-non-org-mode-files "Footnotes:"))
  128. ;; 2.1. The tag must be inserted before the footnotes, separated
  129. ;; from the rest of the text with a blank line.
  130. (with-temp-buffer
  131. (insert "Paragraph[fn:1][fn::Anonymous]
  132. \[fn:1] Standard
  133. Some additional text.")
  134. (org-footnote-normalize)
  135. (should
  136. (equal (buffer-string)
  137. "Paragraph[1][2]
  138. Some additional text.
  139. Footnotes:
  140. \[1] Standard
  141. \[2] Anonymous")))
  142. ;; 2.2. Any tag already inserted in the buffer should be removed
  143. ;; prior to footnotes insertion.
  144. (with-temp-buffer
  145. (insert "Text[fn:1]
  146. Footnotes:
  147. Additional text.
  148. Footnotes:
  149. \[fn:1] Definition")
  150. (org-footnote-normalize)
  151. (should
  152. (equal (buffer-string)
  153. "Text[1]
  154. Additional text.
  155. Footnotes:
  156. \[1] Definition"))))
  157. ;; 3. As an exception, in `message-mode' buffer, if a signature is
  158. ;; present, insert footnotes before it.n
  159. (let ((org-footnote-tag-for-non-org-mode-files nil))
  160. (with-temp-buffer
  161. (insert "Body[fn::def]
  162. --
  163. Fake signature
  164. --
  165. Signature")
  166. ;; Mimic `message-mode'.
  167. (let ((major-mode 'message-mode)
  168. (message-cite-prefix-regexp "\\([ ]*[_.[:word:]]+>+\\|[ ]*[]>|]\\)+")
  169. (message-signature-separator "^-- $"))
  170. (flet ((message-point-in-header-p nil nil))
  171. (org-footnote-normalize)))
  172. (should
  173. (equal (buffer-string)
  174. "Body[1]
  175. --
  176. Fake signature
  177. \[1] def
  178. --
  179. Signature")))))
  180. (ert-deftest test-org-footnote/sort ()
  181. "Test footnotes definitions sorting."
  182. (let ((org-footnote-section nil))
  183. (org-test-with-temp-text
  184. "Text[fn:1][fn::inline][fn:2][fn:label]
  185. \[fn:label] C
  186. \[fn:1] A
  187. \[fn:2] B"
  188. (org-footnote-normalize 'sort)
  189. (should
  190. (equal (buffer-string)
  191. "Text[fn:1][fn::inline][fn:2][fn:label]
  192. \[fn:1] A
  193. \[fn:2] B
  194. \[fn:label] C
  195. ")))))
  196. (provide 'test-org-footnote)
  197. ;;; test-org-footnote.el ends here