test-org-clock.el 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. (defun test-org-clock/clocktable (string)
  69. (let ((org-clock-total-time-cell-format "*%s*"))
  70. ;; Install Clock lines in "Foo".
  71. (search-forward "** Foo")
  72. (forward-line)
  73. (insert (org-test-clock-create-clock "-2d 8:00" "-2d 13:00"))
  74. (insert (org-test-clock-create-clock ". 8:00" "13:00"))
  75. ;; Install Clock lines in "Bar".
  76. (search-forward "** Bar")
  77. (forward-line)
  78. (insert (org-test-clock-create-clock "-10y 8:00" "-10y 12:00"))
  79. (insert (org-test-clock-create-clock "-2d 15:00" "-2d 18:00"))
  80. (insert (org-test-clock-create-clock "-1d 8:00" "-1d 13:00"))
  81. (insert (org-test-clock-create-clock "-1d 15:00" "-1d 18:00"))
  82. (insert (org-test-clock-create-clock
  83. (let ((time (decode-time (current-time))))
  84. (format ". %d:%d" (1- (nth 2 time)) (nth 1 time)))))
  85. ;; Previous two days.
  86. (goto-char (point-min))
  87. (forward-line)
  88. (test-org-clock-clocktable-contents-at-point string)))
  89. (ert-deftest test-org-clock/clocktable1 ()
  90. "Test clocktable specifications."
  91. ;; Relative time: Previous two days.
  92. (should
  93. (equal
  94. "| Headline | Time | |
  95. |------------------------------+---------+-------|
  96. | *Total time* | *16:00* | |
  97. |------------------------------+---------+-------|
  98. | Relative times in clocktable | 16:00 | |
  99. | Foo | | 5:00 |
  100. | Bar | | 11:00 |
  101. "
  102. (org-test-with-temp-text
  103. "* Relative times in clocktable\n** Foo\n** Bar\n"
  104. (test-org-clock/clocktable
  105. ":tstart \"<-2d>\" :tend \"<today>\" :indent nil")))))
  106. (ert-deftest test-org-clock/clocktable2 ()
  107. "Test clocktable specifications."
  108. ;; Relative time: Yesterday until now.
  109. (should
  110. (equal
  111. "| Headline | Time | |
  112. |------------------------------+---------+------|
  113. | *Total time* | *13:00* | |
  114. |------------------------------+---------+------|
  115. | Relative times in clocktable | 13:00 | |
  116. | Foo | | 5:00 |
  117. | Bar | | 8:00 |
  118. "
  119. (org-test-with-temp-text
  120. "* Relative times in clocktable\n** Foo\n** Bar\n"
  121. (test-org-clock/clocktable
  122. ":tstart \"<yesterday>\" :tend \"<tomorrow>\" :indent nil")))))
  123. (ert-deftest test-org-clock/clocktable-until-now ()
  124. "Test clocktable specifications using `untilnow' range."
  125. ;; Relative time: Yesterday until now.
  126. (should
  127. (equal
  128. "| Headline | Time | |
  129. |------------------------------+---------+-------|
  130. | *Total time* | *25:00* | |
  131. |------------------------------+---------+-------|
  132. | Relative times in clocktable | 25:00 | |
  133. | Foo | | 10:00 |
  134. | Bar | | 15:00 |
  135. "
  136. (org-test-with-temp-text
  137. "* Relative times in clocktable\n** Foo\n** Bar\n"
  138. (test-org-clock/clocktable ":block untilnow :indent nil")))))
  139. (provide 'test-org-clock)
  140. ;;; test-org-clock.el end here