test-org-colview.el 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. ;;; test-org-colview.el --- Tests for org-colview.el -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2016 Nicolas Goaziou
  3. ;; Author: Nicolas Goaziou <mail@nicolasgoaziou.fr>
  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. ;;; Code:
  15. ;;; Column view
  16. (ert-deftest test-org-colview/columns-scope ()
  17. "Test `org-columns' scope."
  18. ;; Before the first headline, view all document.
  19. (should
  20. (equal
  21. '("H1" "H2" "H3")
  22. (org-test-with-temp-text "Top\n* H1\n** H2\n* H3"
  23. (let ((org-columns-default-format "%ITEM")) (org-columns))
  24. (org-map-entries
  25. (lambda () (get-char-property (point) 'org-columns-value))))))
  26. ;; When :COLUMNS: is set up in the hierarchy, view tree starting
  27. ;; there.
  28. (should
  29. (equal
  30. '(nil "H2" "H3" nil)
  31. (org-test-with-temp-text
  32. "* H1\n** H2\n:PROPERTIES:\n:COLUMNS: %ITEM\n:END:\n*** <point>H3\n* H4"
  33. (let ((org-columns-default-format "%ITEM")) (org-columns))
  34. (org-map-entries
  35. (lambda () (get-char-property (point) 'org-columns-value))))))
  36. ;; Otherwise, view tree starting at the current headline.
  37. (should
  38. (equal
  39. '(nil "H2" "H3" nil)
  40. (org-test-with-temp-text "Top\n* H1\n** <point>H2\n*** H3\n* H4"
  41. (let ((org-columns-default-format "%ITEM")) (org-columns))
  42. (org-map-entries
  43. (lambda () (get-char-property (point) 'org-columns-value))))))
  44. ;; With a non-nil prefix argument, always view all document.
  45. (should
  46. (equal
  47. '("H1" "H2" "H3" "H4")
  48. (org-test-with-temp-text
  49. "* H1\n** H2\n:PROPERTIES:\n:COLUMNS: %ITEM\n:END:\n*** <point>H3\n* H4"
  50. (let ((org-columns-default-format "%ITEM")) (org-columns t))
  51. (org-map-entries
  52. (lambda () (get-char-property (point) 'org-columns-value))))))
  53. (should
  54. (equal
  55. '("H1" "H2" "H3" "H4")
  56. (org-test-with-temp-text "Top\n* H1\n** <point>H2\n*** H3\n* H4"
  57. (let ((org-columns-default-format "%ITEM")) (org-columns t))
  58. (org-map-entries
  59. (lambda () (get-char-property (point) 'org-columns-value)))))))
  60. (ert-deftest test-org-colview/columns-width ()
  61. "Test `org-columns' column widths."
  62. ;; When a width is specified in the format, use it.
  63. (should
  64. (= 9
  65. (org-test-with-temp-text "* H"
  66. (let ((org-columns-default-format "%9ITEM")) (org-columns))
  67. (cdar org-columns-current-maxwidths))))
  68. ;; Otherwise, use the width of the largest value in the column.
  69. (should
  70. (= 2
  71. (org-test-with-temp-text
  72. "* H\n:PROPERTIES:\n:P: X\n:END:\n** H2\n:PROPERTIES:\n:P: XX\n:END:"
  73. (let ((org-columns-default-format "%P")) (org-columns))
  74. (cdar org-columns-current-maxwidths))))
  75. ;; If the title is wider than the widest value, use title width
  76. ;; instead.
  77. (should
  78. (= 4
  79. (org-test-with-temp-text "* H"
  80. (let ((org-columns-default-format "%ITEM")) (org-columns))
  81. (cdar org-columns-current-maxwidths))))
  82. ;; Special case: stars do count for ITEM.
  83. (should
  84. (= 6
  85. (org-test-with-temp-text "* Head"
  86. (let ((org-columns-default-format "%ITEM")) (org-columns))
  87. (cdar org-columns-current-maxwidths))))
  88. ;; Special case: width takes into account link narrowing in ITEM.
  89. (should
  90. (equal
  91. '("* [123]" . 7)
  92. (org-test-with-temp-text "* [[http://orgmode.org][123]]"
  93. (let ((org-columns-default-format "%ITEM")) (org-columns))
  94. (cons (get-char-property (point) 'org-columns-value-modified)
  95. (cdar org-columns-current-maxwidths)))))
  96. ;; When a value is too wide for the current column, add ellipses.
  97. ;; Take into consideration length of `org-columns-ellipses'.
  98. (should
  99. (equal "123.. |"
  100. (org-test-with-temp-text "* H\n:PROPERTIES:\n:P: 123456\n:END:"
  101. (let ((org-columns-default-format "%5P")
  102. (org-columns-ellipses ".."))
  103. (org-columns))
  104. (org-trim (get-char-property (point) 'display)))))
  105. (should
  106. (equal "1234… |"
  107. (org-test-with-temp-text "* H\n:PROPERTIES:\n:P: 123456\n:END:"
  108. (let ((org-columns-default-format "%5P")
  109. (org-columns-ellipses "…"))
  110. (org-columns))
  111. (org-trim (get-char-property (point) 'display))))))
  112. ;;; Dynamic block
  113. (ert-deftest test-org-colview/dblock ()
  114. "Test the column view table."
  115. (should
  116. (equal
  117. "#+BEGIN: columnview
  118. | ITEM |
  119. |------|
  120. | H |
  121. #+END:"
  122. (org-test-with-temp-text
  123. "* H\n<point>#+BEGIN: columnview\n#+END:"
  124. (let ((org-columns-default-format "%ITEM")) (org-update-dblock))
  125. (buffer-substring-no-properties (point) (point-max)))))
  126. (should
  127. (equal
  128. "#+BEGIN: columnview
  129. | ITEM | A |
  130. |------+---|
  131. | H | 1 |
  132. #+END:"
  133. (org-test-with-temp-text
  134. "* H\n:PROPERTIES:\n:A: 1\n:END:\n<point>#+BEGIN: columnview\n#+END:"
  135. (let ((org-columns-default-format "%ITEM %A")) (org-update-dblock))
  136. (buffer-substring-no-properties (point) (point-max)))))
  137. ;; Properties are case insensitive.
  138. (should
  139. (equal
  140. "#+BEGIN: columnview
  141. | a |
  142. |---|
  143. | 1 |
  144. #+END:"
  145. (org-test-with-temp-text
  146. "* H\n:PROPERTIES:\n:A: 1\n:END:\n<point>#+BEGIN: columnview\n#+END:"
  147. (let ((org-columns-default-format "%a")) (org-update-dblock))
  148. (buffer-substring-no-properties (point) (point-max)))))
  149. ;; Test titles given to columns.
  150. (should
  151. (equal
  152. "#+BEGIN: columnview
  153. | Name | Prop |
  154. |------+------|
  155. | H | 1 |
  156. #+END:"
  157. (org-test-with-temp-text
  158. "* H\n:PROPERTIES:\n:A: 1\n:END:\n<point>#+BEGIN: columnview\n#+END:"
  159. (let ((org-columns-default-format "%ITEM(Name) %A(Prop)"))
  160. (org-update-dblock))
  161. (buffer-substring-no-properties (point) (point-max)))))
  162. ;; Test `:id' parameter
  163. (should
  164. (equal
  165. "#+BEGIN: columnview :id local
  166. | ITEM |
  167. |------|
  168. | H1 |
  169. | H1.1 |
  170. #+END:
  171. "
  172. (org-test-with-temp-text
  173. "* H1\n<point>#+BEGIN: columnview :id local\n#+END:\n** H1.1\n* H2"
  174. (let ((org-columns-default-format "%ITEM")) (org-update-dblock))
  175. (buffer-substring-no-properties (point) (outline-next-heading)))))
  176. (should
  177. (equal
  178. "#+BEGIN: columnview :id global
  179. | ITEM |
  180. |------|
  181. | H1 |
  182. | H1.1 |
  183. | H2 |
  184. #+END:
  185. "
  186. (org-test-with-temp-text
  187. "\n* H1\n<point>#+BEGIN: columnview :id global\n#+END:\n** H1.1\n* H2"
  188. (let ((org-columns-default-format "%ITEM")) (org-update-dblock))
  189. (buffer-substring-no-properties (point) (outline-next-heading)))))
  190. ;; Test `:hlines' parameter.
  191. (should
  192. (equal
  193. "#+BEGIN: columnview :hlines t :id global
  194. | ITEM |
  195. |------|
  196. | H |
  197. |------|
  198. | H2 |
  199. |------|
  200. | H2.1 |
  201. #+END:\n"
  202. (org-test-with-temp-text
  203. "
  204. * H
  205. <point>#+BEGIN: columnview :hlines t :id global
  206. #+END:
  207. * H2
  208. ** H2.1"
  209. (let ((org-columns-default-format "%ITEM")) (org-update-dblock))
  210. (buffer-substring-no-properties (point) (outline-next-heading)))))
  211. (should
  212. (equal
  213. "#+BEGIN: columnview :hlines 1 :id global
  214. | ITEM |
  215. |------|
  216. | H |
  217. |------|
  218. | H2 |
  219. | H2.1 |
  220. #+END:\n"
  221. (org-test-with-temp-text
  222. "
  223. * H
  224. <point>#+BEGIN: columnview :hlines 1 :id global
  225. #+END:
  226. * H2
  227. ** H2.1"
  228. (let ((org-columns-default-format "%ITEM")) (org-update-dblock))
  229. (buffer-substring-no-properties (point) (outline-next-heading)))))
  230. ;; Test `:indent' parameter.
  231. (should
  232. (equal
  233. "#+BEGIN: columnview :indent t
  234. | ITEM |
  235. |----------|
  236. | H1 |
  237. | \\_ H1.1 |
  238. #+END:
  239. "
  240. (org-test-with-temp-text
  241. "* H1\n<point>#+BEGIN: columnview :indent t\n#+END:\n** H1.1"
  242. (let ((org-columns-default-format "%ITEM")) (org-update-dblock))
  243. (buffer-substring-no-properties (point) (outline-next-heading)))))
  244. (should
  245. (equal
  246. "#+BEGIN: columnview :indent t
  247. | Prop | Name |
  248. |------+----------|
  249. | | H1 |
  250. | | \\_ H1.1 |
  251. #+END:
  252. "
  253. (org-test-with-temp-text
  254. "* H1\n<point>#+BEGIN: columnview :indent t\n#+END:\n** H1.1"
  255. (let ((org-columns-default-format "%A(Prop) %ITEM(Name)"))
  256. (org-update-dblock))
  257. (buffer-substring-no-properties (point) (outline-next-heading)))))
  258. ;; Test `:vlines' parameter.
  259. (should
  260. (equal
  261. "#+BEGIN: columnview :vlines t
  262. | | ITEM | A |
  263. |---+------+----|
  264. | | H | 1 |
  265. | / | <> | <> |
  266. #+END:"
  267. (org-test-with-temp-text
  268. "* H\n:PROPERTIES:\n:A: 1\n:END:\n<point>#+BEGIN: columnview :vlines t\n#+END:"
  269. (let ((org-columns-default-format "%ITEM %A")) (org-update-dblock))
  270. (buffer-substring-no-properties (point) (point-max)))))
  271. ;; Test `:skip-empty-rows' parameter.
  272. (should
  273. (equal
  274. "#+BEGIN: columnview :skip-empty-rows t
  275. | ITEM | A |
  276. |------+---|
  277. | H1.1 | 1 |
  278. #+END:
  279. "
  280. (org-test-with-temp-text
  281. "
  282. * H1
  283. <point>#+BEGIN: columnview :skip-empty-rows t
  284. #+END:
  285. ** H1.1
  286. :PROPERTIES:
  287. :A: 1
  288. :END:"
  289. (let ((org-columns-default-format "%ITEM %A")) (org-update-dblock))
  290. (buffer-substring-no-properties (point) (outline-next-heading)))))
  291. ;; Test `:format' parameter.
  292. (should
  293. (equal
  294. "#+BEGIN: columnview :format \"%ITEM(Name)\"
  295. | Name |
  296. |------|
  297. | H |
  298. #+END:"
  299. (org-test-with-temp-text
  300. "* H\n<point>#+BEGIN: columnview :format \"%ITEM(Name)\"\n#+END:"
  301. (let ((org-columns-default-format "%ITEM")) (org-update-dblock))
  302. (buffer-substring-no-properties (point) (point-max)))))
  303. ;; Test `:width' parameter
  304. (should
  305. (equal
  306. "#+BEGIN: columnview :width t
  307. | ITEM | A |
  308. |------------+---|
  309. | H | |
  310. | <10> | |
  311. #+END:"
  312. (org-test-with-temp-text
  313. "* H\n<point>#+BEGIN: columnview :width t\n#+END:"
  314. (let ((org-columns-default-format "%10ITEM %A")) (org-update-dblock))
  315. (buffer-substring-no-properties (point) (point-max)))))
  316. ;; When inserting ITEM values, make sure to clean sensitive
  317. ;; contents, like unique targets or forbidden inline src-blocks.
  318. (should
  319. (equal
  320. "#+BEGIN: columnview
  321. | ITEM |
  322. |------|
  323. | H 1 |
  324. #+END:"
  325. (org-test-with-temp-text
  326. "* H <<target>> 1\n<point>#+BEGIN: columnview\n#+END:"
  327. (let ((org-columns-default-format "%ITEM")) (org-update-dblock))
  328. (buffer-substring-no-properties (point) (point-max)))))
  329. (should
  330. (equal
  331. "#+BEGIN: columnview
  332. | ITEM |
  333. |------|
  334. | H 1 |
  335. #+END:"
  336. (org-test-with-temp-text
  337. "* H src_emacs-lisp{(+ 1 1)} 1\n<point>#+BEGIN: columnview\n#+END:"
  338. (let ((org-columns-default-format "%ITEM")) (org-update-dblock))
  339. (buffer-substring-no-properties (point) (point-max))))))
  340. (provide 'test-org-colview)
  341. ;;; test-org-colview.el ends here