test-org-element.el 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. ;;; test-org-element.el --- Tests for org-element.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. ;;; Commentary:
  15. ;;; Code:
  16. (let ((load-path (cons (expand-file-name
  17. ".." (file-name-directory
  18. (or load-file-name buffer-file-name)))
  19. load-path)))
  20. (require 'org-test)
  21. (require 'org-test-ob-consts)
  22. (require 'org-element))
  23. ;;; Tests:
  24. ;;;; Headlines
  25. (ert-deftest test-org-element/headline-quote-keyword ()
  26. "Test QUOTE keyword recognition."
  27. ;; Reference test.
  28. (org-test-with-temp-text "* Headline"
  29. (let ((org-quote-string "QUOTE"))
  30. (should-not (org-element-property :quotedp (org-element-at-point)))))
  31. ;; Standard position.
  32. (org-test-with-temp-text "* QUOTE Headline"
  33. (let ((org-quote-string "QUOTE"))
  34. (let ((headline (org-element-at-point)))
  35. (should (org-element-property :quotedp headline))
  36. ;; Test removal from raw value.
  37. (should (equal (org-element-property :raw-value headline) "Headline"))))
  38. ;; Case sensitivity.
  39. (let ((org-quote-string "Quote"))
  40. (should-not (org-element-property :quotedp (org-element-at-point)))))
  41. ;; With another keyword.
  42. (org-test-with-temp-text "* TODO QUOTE Headline"
  43. (let ((org-quote-string "QUOTE")
  44. (org-todo-keywords '((sequence "TODO" "DONE"))))
  45. (should (org-element-property :quotedp (org-element-at-point))))))
  46. (ert-deftest test-org-element/headline-comment-keyword ()
  47. "Test COMMENT keyword recognition."
  48. ;; Reference test.
  49. (org-test-with-temp-text "* Headline"
  50. (let ((org-comment-string "COMMENT"))
  51. (should-not (org-element-property :commentedp (org-element-at-point)))))
  52. ;; Standard position.
  53. (org-test-with-temp-text "* COMMENT Headline"
  54. (let ((org-comment-string "COMMENT"))
  55. (let ((headline (org-element-at-point)))
  56. (should (org-element-property :commentedp headline))
  57. ;; Test removal from raw value.
  58. (should (equal (org-element-property :raw-value headline) "Headline"))))
  59. ;; Case sensitivity.
  60. (let ((org-comment-string "Comment"))
  61. (should-not (org-element-property :commentedp (org-element-at-point)))))
  62. ;; With another keyword.
  63. (org-test-with-temp-text "* TODO COMMENT Headline"
  64. (let ((org-comment-string "COMMENT")
  65. (org-todo-keywords '((sequence "TODO" "DONE"))))
  66. (should (org-element-property :commentedp (org-element-at-point))))))
  67. (ert-deftest test-org-element/headline-archive-tag ()
  68. "Test ARCHIVE tag recognition."
  69. ;; Reference test.
  70. (org-test-with-temp-text "* Headline"
  71. (let ((org-archive-tag "ARCHIVE"))
  72. (should-not (org-element-property :archivedp (org-element-at-point)))))
  73. ;; Single tag.
  74. (org-test-with-temp-text "* Headline :ARCHIVE:"
  75. (let ((org-archive-tag "ARCHIVE"))
  76. (let ((headline (org-element-at-point)))
  77. (should (org-element-property :archivedp headline))
  78. ;; Test tag removal.
  79. (should-not (org-element-property :tags headline))))
  80. (let ((org-archive-tag "Archive"))
  81. (should-not (org-element-property :archivedp (org-element-at-point)))))
  82. ;; Multiple tags.
  83. (org-test-with-temp-text "* Headline :test:ARCHIVE:"
  84. (let ((org-archive-tag "ARCHIVE"))
  85. (let ((headline (org-element-at-point)))
  86. (should (org-element-property :archivedp headline))
  87. ;; Test tag removal.
  88. (should (equal (org-element-property :tags headline) ":test:"))))))
  89. ;;; Navigation tools.
  90. (ert-deftest test-org-element/forward-element ()
  91. "Test `org-element-forward' specifications."
  92. ;; 1. At EOB: should error.
  93. (org-test-with-temp-text "Some text\n"
  94. (goto-char (point-max))
  95. (should-error (org-element-forward)))
  96. ;; 2. Standard move: expected to ignore blank lines.
  97. (org-test-with-temp-text "First paragraph.\n\n\nSecond paragraph."
  98. (org-element-forward)
  99. (should (looking-at "Second paragraph.")))
  100. ;; 3. Headline tests.
  101. (org-test-with-temp-text "
  102. * Head 1
  103. ** Head 1.1
  104. *** Head 1.1.1
  105. ** Head 1.2"
  106. ;; 3.1. At an headline beginning: move to next headline at the
  107. ;; same level.
  108. (goto-line 3)
  109. (org-element-forward)
  110. (should (looking-at "** Head 1.2"))
  111. ;; 3.2. At an headline beginning: move to parent headline if no
  112. ;; headline at the same level.
  113. (goto-line 3)
  114. (org-element-forward)
  115. (should (looking-at "** Head 1.2")))
  116. ;; 4. Greater element tests.
  117. (org-test-with-temp-text
  118. "#+BEGIN_CENTER\nInside.\n#+END_CENTER\n\nOutside."
  119. ;; 4.1. At a greater element: expected to skip contents.
  120. (org-element-forward)
  121. (should (looking-at "Outside."))
  122. ;; 4.2. At the end of greater element contents: expected to skip
  123. ;; to the end of the greater element.
  124. (goto-line 2)
  125. (org-element-forward)
  126. (should (looking-at "Outside.")))
  127. ;; 5. List tests.
  128. (org-test-with-temp-text "
  129. - item1
  130. - sub1
  131. - sub2
  132. - sub3
  133. Inner paragraph.
  134. - item2
  135. Outside."
  136. ;; 5.1. At list top point: expected to move to the element after
  137. ;; the list.
  138. (goto-line 2)
  139. (org-element-forward)
  140. (should (looking-at "Outside."))
  141. ;; 5.2. Special case: at the first line of a sub-list, but not at
  142. ;; beginning of line, move to next item.
  143. (goto-line 2)
  144. (forward-char)
  145. (org-element-forward)
  146. (should (looking-at "- item2"))
  147. (goto-line 4)
  148. (forward-char)
  149. (org-element-forward)
  150. (should (looking-at " - sub2"))
  151. ;; 5.3 At sub-list beginning: expected to move after the sub-list.
  152. (goto-line 4)
  153. (org-element-forward)
  154. (should (looking-at " Inner paragraph."))
  155. ;; 5.4. At sub-list end: expected to move outside the sub-list.
  156. (goto-line 8)
  157. (org-element-forward)
  158. (should (looking-at " Inner paragraph."))
  159. ;; 5.5. At an item: expected to move to next item, if any.
  160. (goto-line 6)
  161. (org-element-forward)
  162. (should (looking-at " - sub3"))))
  163. (ert-deftest test-org-element/backward-element ()
  164. "Test `org-element-backward' specifications."
  165. ;; 1. At BOB (modulo some white spaces): should error.
  166. (org-test-with-temp-text " \nParagraph."
  167. (org-skip-whitespace)
  168. (should-error (org-element-backward)))
  169. ;; 2. Not at the beginning of an element: move at its beginning.
  170. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  171. (goto-line 3)
  172. (end-of-line)
  173. (org-element-backward)
  174. (should (looking-at "Paragraph2.")))
  175. ;; 3. Headline tests.
  176. (org-test-with-temp-text "
  177. * Head 1
  178. ** Head 1.1
  179. *** Head 1.1.1
  180. ** Head 1.2"
  181. ;; 3.1. At an headline beginning: move to previous headline at the
  182. ;; same level.
  183. (goto-line 5)
  184. (org-element-backward)
  185. (should (looking-at "** Head 1.1"))
  186. ;; 3.2. At an headline beginning: move to parent headline if no
  187. ;; headline at the same level.
  188. (goto-line 3)
  189. (org-element-backward)
  190. (should (looking-at "* Head 1"))
  191. ;; 3.3. At the first top-level headline: should error.
  192. (goto-line 2)
  193. (should-error (org-element-backward)))
  194. ;; 4. At beginning of first element inside a greater element:
  195. ;; expected to move to greater element's beginning.
  196. (org-test-with-temp-text "Before.\n#+BEGIN_CENTER\nInside.\n#+END_CENTER."
  197. (goto-line 3)
  198. (org-element-backward)
  199. (should (looking-at "#\\+BEGIN_CENTER")))
  200. ;; 5. List tests.
  201. (org-test-with-temp-text "
  202. - item1
  203. - sub1
  204. - sub2
  205. - sub3
  206. Inner paragraph.
  207. - item2
  208. Outside."
  209. ;; 5.1. At beginning of sub-list: expected to move to the
  210. ;; paragraph before it.
  211. (goto-line 4)
  212. (org-element-backward)
  213. (should (looking-at "item1"))
  214. ;; 5.2. At an item in a list: expected to move at previous item.
  215. (goto-line 8)
  216. (org-element-backward)
  217. (should (looking-at " - sub2"))
  218. (goto-line 12)
  219. (org-element-backward)
  220. (should (looking-at "- item1"))
  221. ;; 5.3. At end of list/sub-list: expected to move to list/sub-list
  222. ;; beginning.
  223. (goto-line 10)
  224. (org-element-backward)
  225. (should (looking-at " - sub1"))
  226. (goto-line 15)
  227. (org-element-backward)
  228. (should (looking-at "- item1"))
  229. ;; 5.4. At blank-lines before list end: expected to move to top
  230. ;; item.
  231. (goto-line 14)
  232. (org-element-backward)
  233. (should (looking-at "- item1"))))
  234. (ert-deftest test-org-element/up-element ()
  235. "Test `org-element-up' specifications."
  236. ;; 1. At BOB or with no surrounding element: should error.
  237. (org-test-with-temp-text "Paragraph."
  238. (should-error (org-element-up)))
  239. (org-test-with-temp-text "* Head1\n* Head2"
  240. (goto-line 2)
  241. (should-error (org-element-up)))
  242. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  243. (goto-line 3)
  244. (should-error (org-element-up)))
  245. ;; 2. At an headline: move to parent headline.
  246. (org-test-with-temp-text "* Head1\n** Sub-Head1\n** Sub-Head2"
  247. (goto-line 3)
  248. (org-element-up)
  249. (should (looking-at "\\* Head1")))
  250. ;; 3. Inside a greater element: move to greater element beginning.
  251. (org-test-with-temp-text
  252. "Before.\n#+BEGIN_CENTER\nParagraph1\nParagraph2\n#+END_CENTER\n"
  253. (goto-line 3)
  254. (org-element-up)
  255. (should (looking-at "#\\+BEGIN_CENTER")))
  256. ;; 4. List tests.
  257. (org-test-with-temp-text "* Top
  258. - item1
  259. - sub1
  260. - sub2
  261. Paragraph within sub2.
  262. - item2"
  263. ;; 4.1. Within an item: move to the item beginning.
  264. (goto-line 8)
  265. (org-element-up)
  266. (should (looking-at " - sub2"))
  267. ;; 4.2. At an item in a sub-list: move to parent item.
  268. (goto-line 4)
  269. (org-element-up)
  270. (should (looking-at "- item1"))
  271. ;; 4.3. At an item in top list: move to beginning of whole list.
  272. (goto-line 10)
  273. (org-element-up)
  274. (should (looking-at "- item1"))
  275. ;; 4.4. Special case. At very top point: should move to parent of
  276. ;; list.
  277. (goto-line 2)
  278. (org-element-up)
  279. (should (looking-at "\\* Top"))))
  280. (provide 'test-org-element)
  281. ;;; test-org-element.el ends here