test-org-element.el 10 KB

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