test-org-num.el 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. ;; Do not choke on empty headlines.
  148. (should
  149. (equal '("1 ")
  150. (org-test-with-temp-text "* "
  151. (let ((org-num-skip-commented t)) (org-num-mode 1))
  152. (mapcar (lambda (o) (overlay-get o 'after-string))
  153. (overlays-in (point-min) (point-max))))))
  154. (should
  155. (equal '("1 ")
  156. (org-test-with-temp-text "* "
  157. (let ((org-num-skip-unnumbered t)) (org-num-mode 1))
  158. (mapcar (lambda (o) (overlay-get o 'after-string))
  159. (overlays-in (point-min) (point-max))))))
  160. (should
  161. (equal '("1 ")
  162. (org-test-with-temp-text "* "
  163. (let ((org-num-skip-footnotes t)) (org-num-mode 1))
  164. (mapcar (lambda (o) (overlay-get o 'after-string))
  165. (overlays-in (point-min) (point-max)))))))
  166. (ert-deftest test-org-num/update ()
  167. "Test numbering update after a buffer modification."
  168. ;; Headlines created at BEG.
  169. (should
  170. (equal "1 "
  171. (org-test-with-temp-text "X* H"
  172. (org-num-mode 1)
  173. (delete-char 1)
  174. (overlay-get (car (overlays-at (line-beginning-position)))
  175. 'after-string))))
  176. (should
  177. (equal "1 "
  178. (org-test-with-temp-text "*<point>\n H"
  179. (org-num-mode 1)
  180. (delete-char 1)
  181. (overlay-get (car (overlays-at (line-beginning-position)))
  182. 'after-string))))
  183. (should
  184. (equal "1 "
  185. (org-test-with-temp-text "*<point>bold*"
  186. (org-num-mode 1)
  187. (insert " ")
  188. (overlay-get (car (overlays-at (line-beginning-position)))
  189. 'after-string))))
  190. ;; Headlines created at END.
  191. (should
  192. (equal '("1 ")
  193. (org-test-with-temp-text "X<point> H"
  194. (org-num-mode 1)
  195. (insert "\n*")
  196. (mapcar (lambda (o) (overlay-get o 'after-string))
  197. (overlays-in (point-min) (point-max))))))
  198. (should
  199. (equal '("1 ")
  200. (org-test-with-temp-text "X<point>* H"
  201. (org-num-mode 1)
  202. (insert "\n")
  203. (mapcar (lambda (o) (overlay-get o 'after-string))
  204. (overlays-in (point-min) (point-max))))))
  205. ;; Headlines created between BEG and END.
  206. (should
  207. (equal '("1.1 " "1 ")
  208. (org-test-with-temp-text ""
  209. (org-num-mode 1)
  210. (insert "\n* H\n** H2")
  211. (mapcar (lambda (o) (overlay-get o 'after-string))
  212. (overlays-in (point-min) (point-max))))))
  213. ;; Change level of a headline.
  214. (should
  215. (equal '("0.1 ")
  216. (org-test-with-temp-text "* H"
  217. (org-num-mode 1)
  218. (insert "*")
  219. (mapcar (lambda (o) (overlay-get o 'after-string))
  220. (overlays-in (point-min) (point-max))))))
  221. (should
  222. (equal '("1 ")
  223. (org-test-with-temp-text "*<point>* H"
  224. (org-num-mode 1)
  225. (delete-char 1)
  226. (mapcar (lambda (o) (overlay-get o 'after-string))
  227. (overlays-in (point-min) (point-max))))))
  228. ;; Alter skip state.
  229. (should
  230. (equal '("1 ")
  231. (org-test-with-temp-text "* H :fo<point>o:"
  232. (let ((org-num-skip-tags '("foo")))
  233. (org-num-mode 1)
  234. (delete-char 1))
  235. (mapcar (lambda (o) (overlay-get o 'after-string))
  236. (overlays-in (point-min) (point-max))))))
  237. (should
  238. (equal '(nil)
  239. (org-test-with-temp-text "* H :fo<point>:"
  240. (let ((org-num-skip-tags '("foo")))
  241. (org-num-mode 1)
  242. (insert "o"))
  243. (mapcar (lambda (o) (overlay-get o 'after-string))
  244. (overlays-in (point-min) (point-max))))))
  245. ;; Invalidate an overlay and insert new headlines.
  246. (should
  247. (equal '("1.2 " "1.1 " "1 ")
  248. (org-test-with-temp-text
  249. "* H\n:PROPERTIES:\n:UNNUMBE<point>RED: t\n:END:"
  250. (let ((org-num-skip-unnumbered t))
  251. (org-num-mode 1)
  252. (insert "\n** H2\n** H3\n")
  253. (mapcar (lambda (o) (overlay-get o 'after-string))
  254. (overlays-in (point-min) (point-max)))))))
  255. ;; Invalidate two overlays: current headline and next one.
  256. (should
  257. (equal '("1 ")
  258. (org-test-with-temp-text
  259. "* H\n:PROPERTIES:\n:UNNUMBE<point>RED: t\n:END:\n** H2"
  260. (let ((org-num-skip-unnumbered t))
  261. (org-num-mode 1)
  262. (delete-region (point) (line-beginning-position 3))
  263. (mapcar (lambda (o) (overlay-get o 'after-string))
  264. (overlays-in (point-min) (point-max))))))))
  265. (provide 'test-org-num)
  266. ;;; org-test-num.el ends here