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. (require 'org-num)
  17. (ert-deftest test-org-num/face ()
  18. "Test `org-num-face' parameter."
  19. (should
  20. (equal
  21. '(foo)
  22. (org-test-with-temp-text "* H1"
  23. (let ((org-num-face 'foo)) (org-num-mode 1))
  24. (mapcar (lambda (o)
  25. (get-text-property 0 'face (overlay-get o 'after-string)))
  26. (overlays-in (point-min) (point-max)))))))
  27. (ert-deftest test-org-num/format-function ()
  28. "Test `org-num-format-function' parameter."
  29. (should
  30. (equal '("foo" "foo")
  31. (org-test-with-temp-text "* H1\n** H2"
  32. (let ((org-num-format-function (lambda (_) "foo")))
  33. (org-num-mode 1))
  34. (mapcar (lambda (o) (overlay-get o 'after-string))
  35. (overlays-in (point-min) (point-max))))))
  36. ;; Preserve face, when set.
  37. (should
  38. (equal-including-properties
  39. '(#("foo" 0 3 (face bar)))
  40. (org-test-with-temp-text "* H1"
  41. (let ((org-num-format-function
  42. (lambda (_) (org-add-props "foo" nil 'face 'bar))))
  43. (org-num-mode 1))
  44. (mapcar (lambda (o) (overlay-get o 'after-string))
  45. (overlays-in (point-min) (point-max))))))
  46. ;; Set face override `org-num-face'.
  47. (should
  48. (equal-including-properties
  49. '(#("foo" 0 3 (face bar)))
  50. (org-test-with-temp-text "* H1"
  51. (let ((org-num-face 'baz)
  52. (org-num-format-function
  53. (lambda (_) (org-add-props "foo" nil 'face 'bar))))
  54. (org-num-mode 1))
  55. (mapcar (lambda (o) (overlay-get o 'after-string))
  56. (overlays-in (point-min) (point-max)))))))
  57. (ert-deftest test-org-num/max-level ()
  58. "Test `org-num-max-level' option."
  59. (should
  60. (equal '("1.1 " "1 ")
  61. (org-test-with-temp-text "* H1\n** H2\n*** H3"
  62. (let ((org-num-max-level 2)) (org-num-mode 1))
  63. (mapcar (lambda (o) (overlay-get o 'after-string))
  64. (overlays-in (point-min) (point-max)))))))
  65. (ert-deftest test-org-num/skip-numbering ()
  66. "Test various skip numbering parameters."
  67. ;; Skip commented headlines.
  68. (should
  69. (equal '(nil "1 ")
  70. (org-test-with-temp-text "* H1\n* COMMENT H2"
  71. (let ((org-num-skip-commented t)) (org-num-mode 1))
  72. (mapcar (lambda (o) (overlay-get o 'after-string))
  73. (overlays-in (point-min) (point-max))))))
  74. (should
  75. (equal '("2 " "1 ")
  76. (org-test-with-temp-text "* H1\n* COMMENT H2"
  77. (let ((org-num-skip-commented nil)) (org-num-mode 1))
  78. (mapcar (lambda (o) (overlay-get o 'after-string))
  79. (overlays-in (point-min) (point-max))))))
  80. ;; Skip commented sub-trees.
  81. (should
  82. (equal '(nil nil)
  83. (org-test-with-temp-text "* COMMENT H1\n** H2"
  84. (let ((org-num-skip-commented t)) (org-num-mode 1))
  85. (mapcar (lambda (o) (overlay-get o 'after-string))
  86. (overlays-in (point-min) (point-max))))))
  87. ;; Skip footnotes sections.
  88. (should
  89. (equal '(nil "1 ")
  90. (org-test-with-temp-text "* H1\n* FN"
  91. (let ((org-num-skip-footnotes t)
  92. (org-footnote-section "FN"))
  93. (org-num-mode 1))
  94. (mapcar (lambda (o) (overlay-get o 'after-string))
  95. (overlays-in (point-min) (point-max))))))
  96. (should
  97. (equal '("2 " "1 ")
  98. (org-test-with-temp-text "* H1\n* FN"
  99. (let ((org-num-skip-footnotes nil)
  100. (org-footnote-section "FN"))
  101. (org-num-mode 1))
  102. (mapcar (lambda (o) (overlay-get o 'after-string))
  103. (overlays-in (point-min) (point-max))))))
  104. ;; Skip tags, recursively.
  105. (should
  106. (equal '(nil "1 ")
  107. (org-test-with-temp-text "* H1\n* H2 :foo:"
  108. (let ((org-num-skip-tags '("foo"))) (org-num-mode 1))
  109. (mapcar (lambda (o) (overlay-get o 'after-string))
  110. (overlays-in (point-min) (point-max))))))
  111. (should
  112. (equal '(nil nil)
  113. (org-test-with-temp-text "* H1 :foo:\n** H2"
  114. (let ((org-num-skip-tags '("foo"))) (org-num-mode 1))
  115. (mapcar (lambda (o) (overlay-get o 'after-string))
  116. (overlays-in (point-min) (point-max))))))
  117. ;; Skip unnumbered sections.
  118. (should
  119. (equal '(nil "1 ")
  120. (org-test-with-temp-text
  121. "* H1\n* H2\n:PROPERTIES:\n:UNNUMBERED: t\n:END:"
  122. (let ((org-num-skip-unnumbered t)) (org-num-mode 1))
  123. (mapcar (lambda (o) (overlay-get o 'after-string))
  124. (overlays-in (point-min) (point-max))))))
  125. (should
  126. (equal '("2 " "1 ")
  127. (org-test-with-temp-text
  128. "* H1\n* H2\n:PROPERTIES:\n:UNNUMBERED: t\n:END:"
  129. (let ((org-num-skip-unnumbered nil)) (org-num-mode 1))
  130. (mapcar (lambda (o) (overlay-get o 'after-string))
  131. (overlays-in (point-min) (point-max))))))
  132. (should
  133. (equal '("2 " "1 ")
  134. (org-test-with-temp-text
  135. "* H1\n* H2\n:PROPERTIES:\n:UNNUMBERED: nil\n:END:"
  136. (let ((org-num-skip-unnumbered t)) (org-num-mode 1))
  137. (mapcar (lambda (o) (overlay-get o 'after-string))
  138. (overlays-in (point-min) (point-max))))))
  139. ;; Skip unnumbered sub-trees.
  140. (should
  141. (equal '(nil nil)
  142. (org-test-with-temp-text
  143. "* H1\n:PROPERTIES:\n:UNNUMBERED: t\n:END:\n** H2"
  144. (let ((org-num-skip-unnumbered t)) (org-num-mode 1))
  145. (mapcar (lambda (o) (overlay-get o 'after-string))
  146. (overlays-in (point-min) (point-max)))))))
  147. (ert-deftest test-org-num/update ()
  148. "Test numbering update after a buffer modification."
  149. ;; Headlines created at BEG.
  150. (should
  151. (equal "1 "
  152. (org-test-with-temp-text "X* H"
  153. (org-num-mode 1)
  154. (delete-char 1)
  155. (overlay-get (car (overlays-at (line-beginning-position)))
  156. 'after-string))))
  157. (should
  158. (equal "1 "
  159. (org-test-with-temp-text "*<point>\n H"
  160. (org-num-mode 1)
  161. (delete-char 1)
  162. (overlay-get (car (overlays-at (line-beginning-position)))
  163. 'after-string))))
  164. (should
  165. (equal "1 "
  166. (org-test-with-temp-text "*<point>bold*"
  167. (org-num-mode 1)
  168. (insert " ")
  169. (overlay-get (car (overlays-at (line-beginning-position)))
  170. 'after-string))))
  171. ;; Headlines created at END.
  172. (should
  173. (equal '("1 ")
  174. (org-test-with-temp-text "X<point> H"
  175. (org-num-mode 1)
  176. (insert "\n*")
  177. (mapcar (lambda (o) (overlay-get o 'after-string))
  178. (overlays-in (point-min) (point-max))))))
  179. (should
  180. (equal '("1 ")
  181. (org-test-with-temp-text "X<point>* H"
  182. (org-num-mode 1)
  183. (insert "\n")
  184. (mapcar (lambda (o) (overlay-get o 'after-string))
  185. (overlays-in (point-min) (point-max))))))
  186. ;; Headlines created between BEG and END.
  187. (should
  188. (equal '("1.1 " "1 ")
  189. (org-test-with-temp-text ""
  190. (org-num-mode 1)
  191. (insert "\n* H\n** H2")
  192. (mapcar (lambda (o) (overlay-get o 'after-string))
  193. (overlays-in (point-min) (point-max))))))
  194. ;; Change level of a headline.
  195. (should
  196. (equal '("0.1 ")
  197. (org-test-with-temp-text "* H"
  198. (org-num-mode 1)
  199. (insert "*")
  200. (mapcar (lambda (o) (overlay-get o 'after-string))
  201. (overlays-in (point-min) (point-max))))))
  202. (should
  203. (equal '("1 ")
  204. (org-test-with-temp-text "*<point>* H"
  205. (org-num-mode 1)
  206. (delete-char 1)
  207. (mapcar (lambda (o) (overlay-get o 'after-string))
  208. (overlays-in (point-min) (point-max))))))
  209. ;; Alter skip state.
  210. (should
  211. (equal '("1 ")
  212. (org-test-with-temp-text "* H :fo<point>o:"
  213. (let ((org-num-skip-tags '("foo")))
  214. (org-num-mode 1)
  215. (delete-char 1))
  216. (mapcar (lambda (o) (overlay-get o 'after-string))
  217. (overlays-in (point-min) (point-max))))))
  218. (should
  219. (equal '(nil)
  220. (org-test-with-temp-text "* H :fo<point>:"
  221. (let ((org-num-skip-tags '("foo")))
  222. (org-num-mode 1)
  223. (insert "o"))
  224. (mapcar (lambda (o) (overlay-get o 'after-string))
  225. (overlays-in (point-min) (point-max))))))
  226. ;; Invalidate an overlay and insert new headlines.
  227. (should
  228. (equal '("1.2 " "1.1 " "1 ")
  229. (org-test-with-temp-text
  230. "* H\n:PROPERTIES:\n:UNNUMBE<point>RED: t\n:END:"
  231. (let ((org-num-skip-unnumbered t))
  232. (org-num-mode 1)
  233. (insert "\n** H2\n** H3\n")
  234. (mapcar (lambda (o) (overlay-get o 'after-string))
  235. (overlays-in (point-min) (point-max)))))))
  236. ;; Invalidate two overlays: current headline and next one.
  237. (should
  238. (equal '("1 ")
  239. (org-test-with-temp-text
  240. "* H\n:PROPERTIES:\n:UNNUMBE<point>RED: t\n:END:\n** H2"
  241. (let ((org-num-skip-unnumbered t))
  242. (org-num-mode 1)
  243. (delete-region (point) (line-beginning-position 3))
  244. (mapcar (lambda (o) (overlay-get o 'after-string))
  245. (overlays-in (point-min) (point-max))))))))
  246. (provide 'test-org-num)
  247. ;;; org-test-num.el ends here