test-org-clock.el 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. ;;; test-org-clock.el --- Tests for org-clock.el
  2. ;; Copyright (C) 2012 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: clocktable\n"))
  53. (unwind-protect
  54. (save-excursion
  55. (org-update-dblock)
  56. (forward-line)
  57. ;; Skip caption.
  58. (when (looking-at "#\\+CAPTION:") (forward-line))
  59. (buffer-substring (point)
  60. (progn (search-forward "#+END: clocktable")
  61. (match-beginning 0))))
  62. ;; Remove clocktable.
  63. (delete-region (point)
  64. (progn (search-forward "#+END: clocktable")
  65. (forward-line)
  66. (point)))))
  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* | *16:00* | |
  76. |------------------------------+---------+-------|
  77. | Relative times in clocktable | 16:00 | |
  78. | Foo | | 5:00 |
  79. | Bar | | 11:00 |
  80. "
  81. (org-test-with-temp-text "* Relative times in clocktable\n** Foo\n** Bar\n"
  82. (progn
  83. ;; Install Clock lines in "Foo".
  84. (search-forward "** Foo")
  85. (forward-line)
  86. (insert (org-test-clock-create-clock "-2d 8:00" "-2d 13:00"))
  87. (insert (org-test-clock-create-clock ". 8:00" "13:00"))
  88. ;; Install Clock lines in "Bar".
  89. (search-forward "** Bar")
  90. (forward-line)
  91. (insert (org-test-clock-create-clock "-2d 15:00" "-2d 18:00"))
  92. (insert (org-test-clock-create-clock "-1d 8:00" "-1d 13:00"))
  93. (insert (org-test-clock-create-clock "-1d 15:00" "-1d 18:00"))
  94. (insert (org-test-clock-create-clock ". 15:00"))
  95. ;; Previous two days.
  96. (goto-char (point-min))
  97. (forward-line)
  98. (test-org-clock-clocktable-contents-at-point
  99. ":tstart \"<today-2>\" :tend \"<today>\" :indent nil")))))
  100. ;; Relative time: Yesterday until now.
  101. (should
  102. (equal
  103. "| Headline | Time | |
  104. |------------------------------+---------+------|
  105. | *Total time* | *13:00* | |
  106. |------------------------------+---------+------|
  107. | Relative times in clocktable | 13:00 | |
  108. | Foo | | 5:00 |
  109. | Bar | | 8:00 |
  110. "
  111. (org-test-with-temp-text "* Relative times in clocktable\n** Foo\n** Bar\n"
  112. (progn
  113. ;; Install Clock lines in "Foo".
  114. (search-forward "** Foo")
  115. (forward-line)
  116. (insert (org-test-clock-create-clock "-2d 8:00" "-2d 13:00"))
  117. (insert (org-test-clock-create-clock ". 8:00" "13:00"))
  118. ;; Install Clock lines in "Bar".
  119. (search-forward "** Bar")
  120. (forward-line)
  121. (insert (org-test-clock-create-clock "-2d 15:00" "-2d 18:00"))
  122. (insert (org-test-clock-create-clock "-1d 8:00" "-1d 13:00"))
  123. (insert (org-test-clock-create-clock "-1d 15:00" "-1d 18:00"))
  124. (insert (org-test-clock-create-clock ". 15:00"))
  125. ;; Previous two days.
  126. (goto-char (point-min))
  127. (forward-line)
  128. (test-org-clock-clocktable-contents-at-point
  129. ":tstart \"<yesterday>\" :tend \"<tomorrow>\" :indent nil"))))))
  130. (provide 'test-org-clock)
  131. ;;; test-org-clock.el end here