test-org-num.el 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. ;;; test-org-num.el --- Tests for Org Num library -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2018 Nicolas Goaziou
  3. ;; Author: Nicolas Goaziou <mail@nicolasgoaziou.fr>
  4. ;; This file is not part of GNU Emacs.
  5. ;; This program is free software; you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; This program is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with this program. If not, see <https://www.gnu.org/licenses/>.
  15. ;;; Code:
  16. ;; FIXME: this test fails in batch mode.
  17. ;;
  18. ;; (ert-deftest test-org-num/face ()
  19. ;; "Test `org-num-face' parameter."
  20. ;; (should
  21. ;; (equal
  22. ;; '(foo)
  23. ;; (org-test-with-temp-text "* H1"
  24. ;; (let ((org-num-face 'foo)) (org-num-mode 1))
  25. ;; (mapcar (lambda (o)
  26. ;; (get-text-property 0 'face (overlay-get o 'after-string)))
  27. ;; (overlays-in (point-min) (point-max)))))))
  28. (ert-deftest test-org-num/format-function ()
  29. "Test `org-num-format-function' parameter."
  30. (should
  31. (equal '("foo" "foo")
  32. (org-test-with-temp-text "* H1\n** H2"
  33. (let ((org-num-format-function (lambda (_) "foo")))
  34. (org-num-mode 1))
  35. (mapcar (lambda (o) (overlay-get o 'after-string))
  36. (overlays-in (point-min) (point-max))))))
  37. ;; Preserve face, when set.
  38. (should
  39. (equal-including-properties
  40. '(#("foo" 0 3 (face bar)))
  41. (org-test-with-temp-text "* H1"
  42. (let ((org-num-format-function
  43. (lambda (_) (org-add-props "foo" nil 'face 'bar))))
  44. (org-num-mode 1))
  45. (mapcar (lambda (o) (overlay-get o 'after-string))
  46. (overlays-in (point-min) (point-max))))))
  47. ;; Set face override `org-num-face'.
  48. (should
  49. (equal-including-properties
  50. '(#("foo" 0 3 (face bar)))
  51. (org-test-with-temp-text "* H1"
  52. (let ((org-num-face 'baz)
  53. (org-num-format-function
  54. (lambda (_) (org-add-props "foo" nil 'face 'bar))))
  55. (org-num-mode 1))
  56. (mapcar (lambda (o) (overlay-get o 'after-string))
  57. (overlays-in (point-min) (point-max)))))))
  58. (ert-deftest test-org-num/max-level ()
  59. "Test `org-num-max-level' option."
  60. (should
  61. (equal '("1.1 " "1 ")
  62. (org-test-with-temp-text "* H1\n** H2\n*** H3"
  63. (let ((org-num-max-level 2)) (org-num-mode 1))
  64. (mapcar (lambda (o) (overlay-get o 'after-string))
  65. (overlays-in (point-min) (point-max)))))))
  66. (ert-deftest test-org-num/skip-numbering ()
  67. "Test various skip numbering parameters."
  68. ;; Skip commented headlines.
  69. (should
  70. (equal '(nil "1 ")
  71. (org-test-with-temp-text "* H1\n* COMMENT H2"
  72. (let ((org-num-skip-commented t)) (org-num-mode 1))
  73. (mapcar (lambda (o) (overlay-get o 'after-string))
  74. (overlays-in (point-min) (point-max))))))
  75. (should
  76. (equal '("2 " "1 ")
  77. (org-test-with-temp-text "* H1\n* COMMENT H2"
  78. (let ((org-num-skip-commented nil)) (org-num-mode 1))
  79. (mapcar (lambda (o) (overlay-get o 'after-string))
  80. (overlays-in (point-min) (point-max))))))
  81. ;; Skip commented sub-trees.
  82. (should
  83. (equal '(nil nil)
  84. (org-test-with-temp-text "* COMMENT H1\n** H2"
  85. (let ((org-num-skip-commented t)) (org-num-mode 1))
  86. (mapcar (lambda (o) (overlay-get o 'after-string))
  87. (overlays-in (point-min) (point-max))))))
  88. ;; Skip footnotes sections.
  89. (should
  90. (equal '(nil "1 ")
  91. (org-test-with-temp-text "* H1\n* FN"
  92. (let ((org-num-skip-footnotes t)
  93. (org-footnote-section "FN"))
  94. (org-num-mode 1))
  95. (mapcar (lambda (o) (overlay-get o 'after-string))
  96. (overlays-in (point-min) (point-max))))))
  97. (should
  98. (equal '("2 " "1 ")
  99. (org-test-with-temp-text "* H1\n* FN"
  100. (let ((org-num-skip-footnotes nil)
  101. (org-footnote-section "FN"))
  102. (org-num-mode 1))
  103. (mapcar (lambda (o) (overlay-get o 'after-string))
  104. (overlays-in (point-min) (point-max))))))
  105. ;; Skip tags, recursively.
  106. (should
  107. (equal '(nil "1 ")
  108. (org-test-with-temp-text "* H1\n* H2 :foo:"
  109. (let ((org-num-skip-tags '("foo"))) (org-num-mode 1))
  110. (mapcar (lambda (o) (overlay-get o 'after-string))
  111. (overlays-in (point-min) (point-max))))))
  112. (should
  113. (equal '(nil nil)
  114. (org-test-with-temp-text "* H1 :foo:\n** H2"
  115. (let ((org-num-skip-tags '("foo"))) (org-num-mode 1))
  116. (mapcar (lambda (o) (overlay-get o 'after-string))
  117. (overlays-in (point-min) (point-max))))))
  118. ;; Skip unnumbered sections.
  119. (should
  120. (equal '(nil "1 ")
  121. (org-test-with-temp-text
  122. "* H1\n* H2\n:PROPERTIES:\n:UNNUMBERED: t\n:END:"
  123. (let ((org-num-skip-unnumbered t)) (org-num-mode 1))
  124. (mapcar (lambda (o) (overlay-get o 'after-string))
  125. (overlays-in (point-min) (point-max))))))
  126. (should
  127. (equal '("2 " "1 ")
  128. (org-test-with-temp-text
  129. "* H1\n* H2\n:PROPERTIES:\n:UNNUMBERED: t\n:END:"
  130. (let ((org-num-skip-unnumbered nil)) (org-num-mode 1))
  131. (mapcar (lambda (o) (overlay-get o 'after-string))
  132. (overlays-in (point-min) (point-max))))))
  133. (should
  134. (equal '("2 " "1 ")
  135. (org-test-with-temp-text
  136. "* H1\n* H2\n:PROPERTIES:\n:UNNUMBERED: nil\n:END:"
  137. (let ((org-num-skip-unnumbered t)) (org-num-mode 1))
  138. (mapcar (lambda (o) (overlay-get o 'after-string))
  139. (overlays-in (point-min) (point-max))))))
  140. ;; Skip unnumbered sub-trees.
  141. (should
  142. (equal '(nil nil)
  143. (org-test-with-temp-text
  144. "* H1\n:PROPERTIES:\n:UNNUMBERED: t\n:END:\n** H2"
  145. (let ((org-num-skip-unnumbered t)) (org-num-mode 1))
  146. (mapcar (lambda (o) (overlay-get o 'after-string))
  147. (overlays-in (point-min) (point-max)))))))
  148. (ert-deftest test-org-num/update ()
  149. "Test numbering update after a buffer modification."
  150. ;; Headlines created at BEG.
  151. (should
  152. (equal "1 "
  153. (org-test-with-temp-text "X* H"
  154. (org-num-mode 1)
  155. (delete-char 1)
  156. (overlay-get (car (overlays-at (line-beginning-position)))
  157. 'after-string))))
  158. (should
  159. (equal "1 "
  160. (org-test-with-temp-text "*<point>\n H"
  161. (org-num-mode 1)
  162. (delete-char 1)
  163. (overlay-get (car (overlays-at (line-beginning-position)))
  164. 'after-string))))
  165. (should
  166. (equal "1 "
  167. (org-test-with-temp-text "*<point>bold*"
  168. (org-num-mode 1)
  169. (insert " ")
  170. (overlay-get (car (overlays-at (line-beginning-position)))
  171. 'after-string))))
  172. ;; Headlines created at END.
  173. (should
  174. (equal '("1 ")
  175. (org-test-with-temp-text "X<point> H"
  176. (org-num-mode 1)
  177. (insert "\n*")
  178. (mapcar (lambda (o) (overlay-get o 'after-string))
  179. (overlays-in (point-min) (point-max))))))
  180. (should
  181. (equal '("1 ")
  182. (org-test-with-temp-text "X<point>* H"
  183. (org-num-mode 1)
  184. (insert "\n")
  185. (mapcar (lambda (o) (overlay-get o 'after-string))
  186. (overlays-in (point-min) (point-max))))))
  187. ;; Headlines created between BEG and END.
  188. (should
  189. (equal '("1.1 " "1 ")
  190. (org-test-with-temp-text ""
  191. (org-num-mode 1)
  192. (insert "\n* H\n** H2")
  193. (mapcar (lambda (o) (overlay-get o 'after-string))
  194. (overlays-in (point-min) (point-max))))))
  195. ;; Change level of a headline.
  196. (should
  197. (equal '("0.1 ")
  198. (org-test-with-temp-text "* H"
  199. (org-num-mode 1)
  200. (insert "*")
  201. (mapcar (lambda (o) (overlay-get o 'after-string))
  202. (overlays-in (point-min) (point-max))))))
  203. (should
  204. (equal '("1 ")
  205. (org-test-with-temp-text "*<point>* H"
  206. (org-num-mode 1)
  207. (delete-char 1)
  208. (mapcar (lambda (o) (overlay-get o 'after-string))
  209. (overlays-in (point-min) (point-max))))))
  210. ;; Alter skip state.
  211. (should
  212. (equal '("1 ")
  213. (org-test-with-temp-text "* H :fo<point>o:"
  214. (let ((org-num-skip-tags '("foo")))
  215. (org-num-mode 1)
  216. (delete-char 1))
  217. (mapcar (lambda (o) (overlay-get o 'after-string))
  218. (overlays-in (point-min) (point-max))))))
  219. (should
  220. (equal '(nil)
  221. (org-test-with-temp-text "* H :fo<point>:"
  222. (let ((org-num-skip-tags '("foo")))
  223. (org-num-mode 1)
  224. (insert "o"))
  225. (mapcar (lambda (o) (overlay-get o 'after-string))
  226. (overlays-in (point-min) (point-max))))))
  227. ;; Invalidate an overlay and insert new headlines.
  228. (should
  229. (equal '("1.2 " "1.1 " "1 ")
  230. (org-test-with-temp-text
  231. "* H\n:PROPERTIES:\n:UNNUMBE<point>RED: t\n:END:"
  232. (let ((org-num-skip-unnumbered t))
  233. (org-num-mode 1)
  234. (insert "\n** H2\n** H3\n")
  235. (mapcar (lambda (o) (overlay-get o 'after-string))
  236. (overlays-in (point-min) (point-max)))))))
  237. ;; Invalidate two overlays: current headline and next one.
  238. (should
  239. (equal '("1 ")
  240. (org-test-with-temp-text
  241. "* H\n:PROPERTIES:\n:UNNUMBE<point>RED: t\n:END:\n** H2"
  242. (let ((org-num-skip-unnumbered t))
  243. (org-num-mode 1)
  244. (delete-region (point) (line-beginning-position 3))
  245. (mapcar (lambda (o) (overlay-get o 'after-string))
  246. (overlays-in (point-min) (point-max))))))))
  247. (provide 'test-org-num)
  248. ;;; org-test-num.el ends here