test-org-clock.el 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. ;;; test-org-clock.el --- Tests for org-clock.el
  2. ;; Copyright (C) 2012, 2014, 2015 Nicolas Goaziou
  3. ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
  4. ;; Released under the GNU General Public License version 3
  5. ;; see: http://www.gnu.org/licenses/gpl-3.0.html
  6. ;;;; Comments
  7. ;;; Code:
  8. (defun org-test-clock-create-timestamp (input &optional inactive with-time)
  9. "Create a timestamp out of a date/time prompt string.
  10. INPUT is a string as expected in a date/time prompt, i.e \"+2d\"
  11. or \"2/5\".
  12. When optional argument INACTIVE is non-nil, return an inactive
  13. timestamp. When optional argument WITH-TIME is non-nil, also
  14. insert hours and minutes.
  15. Return the timestamp as a string."
  16. (org-element-interpret-data
  17. (let ((time (decode-time
  18. (apply #'encode-time
  19. (mapcar (lambda (el) (or el 0))
  20. (org-read-date-analyze
  21. input nil (decode-time (current-time))))))))
  22. (list 'timestamp
  23. (list :type (if inactive 'inactive 'active)
  24. :minute-start (and with-time (nth 1 time))
  25. :hour-start (and with-time (nth 2 time))
  26. :day-start (nth 3 time)
  27. :month-start (nth 4 time)
  28. :year-start (nth 5 time))))))
  29. (defun org-test-clock-create-clock (input1 &optional input2)
  30. "Create a clock line out of two date/time prompts.
  31. INPUT1 and INPUT2 are strings as expected in a date/time prompt,
  32. i.e \"+2d\" or \"2/5\". They respectively refer to start and end
  33. range. INPUT2 can be omitted if clock hasn't finished yet.
  34. Return the clock line as a string."
  35. (let* ((beg (org-test-clock-create-timestamp input1 t t))
  36. (end (and input2 (org-test-clock-create-timestamp input2 t t)))
  37. (sec-diff (and input2 (floor (- (org-time-string-to-seconds end)
  38. (org-time-string-to-seconds beg))))))
  39. (concat org-clock-string " " beg
  40. (when end
  41. (concat "--" end " => "
  42. (format "%2d:%02d"
  43. (/ sec-diff 3600)
  44. (/ (mod sec-diff 3600) 60))))
  45. "\n")))
  46. (defun test-org-clock-clocktable-contents-at-point (options)
  47. "Return contents of a clocktable at point.
  48. OPTIONS is a string of clocktable options. Caption is ignored in
  49. contents. The clocktable doesn't appear in the buffer."
  50. (save-excursion
  51. (insert "#+BEGIN: clocktable " options "\n")
  52. (insert "#+END:\n"))
  53. (unwind-protect
  54. (save-excursion
  55. (let ((org-time-clocksum-format
  56. '(:hours "%d" :require-hours t :minutes ":%02d"
  57. :require-minutes t)))
  58. (org-update-dblock))
  59. (forward-line)
  60. ;; Skip caption.
  61. (when (looking-at "#\\+CAPTION:") (forward-line))
  62. (buffer-substring (point)
  63. (progn (search-forward "#+END:")
  64. (match-beginning 0))))
  65. ;; Remove clocktable.
  66. (delete-region (point) (search-forward "#+END:\n"))))
  67. ;;; Clocktable
  68. (ert-deftest test-org-clock/clocktable ()
  69. "Test clocktable specifications."
  70. ;; Relative time: Previous two days.
  71. (should
  72. (equal
  73. "| Headline | Time | |
  74. |------------------------------+--------+------|
  75. | *Total time* | *8:00* | |
  76. |------------------------------+--------+------|
  77. | Relative times in clocktable | 8:00 | |
  78. | Foo | | 8:00 |
  79. "
  80. (org-test-with-temp-text
  81. "* Relative times in clocktable\n** Foo\n<point>"
  82. (insert (org-test-clock-create-clock "-3d 8:00" "-3d 12:00"))
  83. (insert (org-test-clock-create-clock "-2d 15:00" "-2d 18:00"))
  84. (insert (org-test-clock-create-clock "-1d 8:00" "-1d 13:00"))
  85. (test-org-clock-clocktable-contents-at-point
  86. ":tstart \"<-2d>\" :tend \"<today>\" :indent nil"))))
  87. ;; Relative time: Yesterday until now.
  88. (should
  89. (equal
  90. "| Headline | Time | |
  91. |------------------------------+--------+------|
  92. | *Total time* | *6:00* | |
  93. |------------------------------+--------+------|
  94. | Relative times in clocktable | 6:00 | |
  95. | Foo | | 6:00 |
  96. "
  97. (org-test-with-temp-text
  98. "* Relative times in clocktable\n** Foo\n<point>"
  99. (insert (org-test-clock-create-clock "-2d 15:00" "-2d 18:00"))
  100. (insert (org-test-clock-create-clock "-1d 8:00" "-1d 13:00"))
  101. (insert (org-test-clock-create-clock ". 1:00" ". 2:00"))
  102. (test-org-clock-clocktable-contents-at-point
  103. ":tstart \"<yesterday>\" :tend \"<tomorrow>\" :indent nil"))))
  104. ;; Test `untilnow' block.
  105. (should
  106. (equal
  107. "| Headline | Time | |
  108. |------------------------------+--------+------|
  109. | *Total time* | *6:00* | |
  110. |------------------------------+--------+------|
  111. | Relative times in clocktable | 6:00 | |
  112. | Foo | | 6:00 |
  113. "
  114. (org-test-with-temp-text
  115. "* Relative times in clocktable\n** Foo\n<point>"
  116. (insert (org-test-clock-create-clock "-10y 15:00" "-10y 18:00"))
  117. (insert (org-test-clock-create-clock "-2d 15:00" "-2d 18:00"))
  118. (test-org-clock-clocktable-contents-at-point
  119. ":block untilnow :indent nil")))))
  120. (provide 'test-org-clock)
  121. ;;; test-org-clock.el end here