123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324 |
- ;;; test-org-clock.el --- Tests for org-clock.el
- ;; Copyright (C) 2012, 2014, 2015 Nicolas Goaziou
- ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
- ;; Released under the GNU General Public License version 3
- ;; see: http://www.gnu.org/licenses/gpl-3.0.html
- ;;;; Comments
- ;;; Code:
- (defun org-test-clock-create-timestamp (input &optional inactive with-time)
- "Create a timestamp out of a date/time prompt string.
- INPUT is a string as expected in a date/time prompt, i.e \"+2d\"
- or \"2/5\".
- When optional argument INACTIVE is non-nil, return an inactive
- timestamp. When optional argument WITH-TIME is non-nil, also
- insert hours and minutes.
- Return the timestamp as a string."
- (org-element-interpret-data
- (let ((time (decode-time
- (apply #'encode-time
- (mapcar (lambda (el) (or el 0))
- (org-read-date-analyze
- input nil (decode-time (current-time))))))))
- (list 'timestamp
- (list :type (if inactive 'inactive 'active)
- :minute-start (and with-time (nth 1 time))
- :hour-start (and with-time (nth 2 time))
- :day-start (nth 3 time)
- :month-start (nth 4 time)
- :year-start (nth 5 time))))))
- (defun org-test-clock-create-clock (input1 &optional input2)
- "Create a clock line out of two date/time prompts.
- INPUT1 and INPUT2 are strings as expected in a date/time prompt,
- i.e \"+2d\" or \"2/5\". They respectively refer to start and end
- range. INPUT2 can be omitted if clock hasn't finished yet.
- Return the clock line as a string."
- (let* ((beg (org-test-clock-create-timestamp input1 t t))
- (end (and input2 (org-test-clock-create-timestamp input2 t t)))
- (sec-diff (and input2 (floor (- (org-time-string-to-seconds end)
- (org-time-string-to-seconds beg))))))
- (concat org-clock-string " " beg
- (when end
- (concat "--" end " => "
- (format "%2d:%02d"
- (/ sec-diff 3600)
- (/ (mod sec-diff 3600) 60))))
- "\n")))
- (defun test-org-clock-clocktable-contents-at-point (options)
- "Return contents of a clocktable at point.
- OPTIONS is a string of clocktable options. Caption is ignored in
- contents. The clocktable doesn't appear in the buffer."
- (save-excursion
- (insert "#+BEGIN: clocktable " options "\n")
- (insert "#+END:\n"))
- (unwind-protect
- (save-excursion
- (let ((org-time-clocksum-format
- '(:hours "%d" :require-hours t :minutes ":%02d"
- :require-minutes t)))
- (org-update-dblock))
- (forward-line)
- ;; Skip caption.
- (when (looking-at "#\\+CAPTION:") (forward-line))
- (buffer-substring (point)
- (progn (search-forward "#+END:")
- (match-beginning 0))))
- ;; Remove clocktable.
- (delete-region (point) (search-forward "#+END:\n"))))
- ;;; Clock drawer
- (ert-deftest test-org-clock/into-drawer ()
- "Test `org-clock-into-drawer' specifications."
- ;; When `org-clock-into-drawer' is nil, do not use a clock drawer.
- (should-not
- (org-test-with-temp-text "* H"
- (let ((org-clock-into-drawer nil)
- (org-log-into-drawer nil))
- (org-clock-into-drawer))))
- (should-not
- (org-test-with-temp-text "* H"
- (let ((org-clock-into-drawer nil)
- (org-log-into-drawer t))
- (org-clock-into-drawer))))
- (should-not
- (org-test-with-temp-text "* H"
- (let ((org-clock-into-drawer nil)
- (org-log-into-drawer "BAR"))
- (org-clock-into-drawer))))
- ;; When `org-clock-into-drawer' is a string, use it
- ;; unconditionally.
- (should
- (equal "FOO"
- (org-test-with-temp-text "* H"
- (let ((org-clock-into-drawer "FOO")
- (org-log-into-drawer nil))
- (org-clock-into-drawer)))))
- (should
- (equal "FOO"
- (org-test-with-temp-text "* H"
- (let ((org-clock-into-drawer "FOO")
- (org-log-into-drawer t))
- (org-clock-into-drawer)))))
- (should
- (equal "FOO"
- (org-test-with-temp-text "* H"
- (let ((org-clock-into-drawer "FOO")
- (org-log-into-drawer "BAR"))
- (org-clock-into-drawer)))))
- ;; When `org-clock-into-drawer' is an integer, return it.
- (should
- (= 1
- (org-test-with-temp-text "* H"
- (let ((org-clock-into-drawer 1)
- (org-log-into-drawer nil))
- (org-clock-into-drawer)))))
- (should
- (= 1
- (org-test-with-temp-text "* H"
- (let ((org-clock-into-drawer 1)
- (org-log-into-drawer t))
- (org-clock-into-drawer)))))
- (should
- (= 1
- (org-test-with-temp-text "* H"
- (let ((org-clock-into-drawer 1)
- (org-log-into-drawer "BAR"))
- (org-clock-into-drawer)))))
- ;; Otherwise, any non-nil value defaults to `org-log-into-drawer' or
- ;; "LOGBOOK" if it is nil.
- (should
- (equal "LOGBOOK"
- (org-test-with-temp-text "* H"
- (let ((org-clock-into-drawer t)
- (org-log-into-drawer nil))
- (org-clock-into-drawer)))))
- (should
- (equal "LOGBOOK"
- (org-test-with-temp-text "* H"
- (let ((org-clock-into-drawer t)
- (org-log-into-drawer t))
- (org-clock-into-drawer)))))
- (should
- (equal "FOO"
- (org-test-with-temp-text "* H"
- (let ((org-clock-into-drawer t)
- (org-log-into-drawer "FOO"))
- (org-clock-into-drawer)))))
- ;; A non-nil "CLOCK_INTO_DRAWER" property overrides
- ;; `org-clock-into-drawer' value.
- (should
- (equal "LOGBOOK"
- (org-test-with-temp-text
- "* H\n:PROPERTIES:\n:CLOCK_INTO_DRAWER: t\n:END:"
- (let ((org-clock-into-drawer nil)
- (org-log-into-drawer nil))
- (org-clock-into-drawer)))))
- (should
- (equal "FOO"
- (org-test-with-temp-text
- "* H\n:PROPERTIES:\n:CLOCK_INTO_DRAWER: FOO\n:END:"
- (let ((org-clock-into-drawer nil)
- (org-log-into-drawer nil))
- (org-clock-into-drawer)))))
- (should-not
- (org-test-with-temp-text
- "* H\n:PROPERTIES:\n:CLOCK_INTO_DRAWER: nil\n:END:"
- (let ((org-clock-into-drawer t)
- (org-log-into-drawer nil))
- (org-clock-into-drawer))))
- ;; "CLOCK_INTO_DRAWER" can be inherited.
- (should
- (equal "LOGBOOK"
- (org-test-with-temp-text
- "* H\n:PROPERTIES:\n:CLOCK_INTO_DRAWER: t\n:END:\n** H2<point>"
- (let ((org-clock-into-drawer nil)
- (org-log-into-drawer nil))
- (org-clock-into-drawer)))))
- (should
- (equal "FOO"
- (org-test-with-temp-text
- "* H\n:PROPERTIES:\n:CLOCK_INTO_DRAWER: FOO\n:END:\n** H2<point>"
- (let ((org-clock-into-drawer nil)
- (org-log-into-drawer nil))
- (org-clock-into-drawer)))))
- (should-not
- (org-test-with-temp-text
- "* H\n:PROPERTIES:\n:CLOCK_INTO_DRAWER: nil\n:END:\n** H2<point>"
- (let ((org-clock-into-drawer t)
- (org-log-into-drawer nil))
- (org-clock-into-drawer)))))
- (ert-deftest test-org-clock/drawer-name ()
- "Test `org-clock-drawer-name' specifications."
- ;; A nil value for `org-clock-into-drawer' means no drawer is
- ;; expected whatsoever.
- (should-not
- (org-test-with-temp-text "* H"
- (let ((org-clock-into-drawer nil)
- (org-log-into-drawer nil))
- (org-clock-drawer-name))))
- (should-not
- (org-test-with-temp-text "* H"
- (let ((org-clock-into-drawer nil)
- (org-log-into-drawer t))
- (org-clock-drawer-name))))
- (should-not
- (org-test-with-temp-text "* H"
- (let ((org-clock-into-drawer nil)
- (org-log-into-drawer "FOO"))
- (org-clock-drawer-name))))
- ;; A string value for `org-clock-into-drawer' means to use it
- ;; unconditionally.
- (should
- (equal "FOO"
- (org-test-with-temp-text "* H"
- (let ((org-clock-into-drawer "FOO")
- (org-log-into-drawer nil))
- (org-clock-drawer-name)))))
- (should
- (equal "FOO"
- (org-test-with-temp-text "* H"
- (let ((org-clock-into-drawer "FOO")
- (org-log-into-drawer t))
- (org-clock-drawer-name)))))
- (should
- (equal "FOO"
- (org-test-with-temp-text "* H"
- (let ((org-clock-into-drawer "FOO")
- (org-log-into-drawer "BAR"))
- (org-clock-drawer-name)))))
- ;; When the value in `org-clock-into-drawer' is a number, re-use
- ;; `org-log-into-drawer' or use default "LOGBOOK" value.
- (should
- (equal "FOO"
- (org-test-with-temp-text "* H"
- (let ((org-clock-into-drawer 1)
- (org-log-into-drawer "FOO"))
- (org-clock-drawer-name)))))
- (should
- (equal "LOGBOOK"
- (org-test-with-temp-text "* H"
- (let ((org-clock-into-drawer 1)
- (org-log-into-drawer t))
- (org-clock-drawer-name)))))
- (should
- (equal "LOGBOOK"
- (org-test-with-temp-text "* H"
- (let ((org-clock-into-drawer 1)
- (org-log-into-drawer nil))
- (org-clock-drawer-name))))))
- ;;; Clocktable
- (ert-deftest test-org-clock/clocktable ()
- "Test clocktable specifications."
- ;; Relative time: Previous two days.
- (should
- (equal
- "| Headline | Time | |
- |------------------------------+--------+------|
- | *Total time* | *8:00* | |
- |------------------------------+--------+------|
- | Relative times in clocktable | 8:00 | |
- | Foo | | 8:00 |
- "
- (org-test-with-temp-text
- "* Relative times in clocktable\n** Foo\n<point>"
- (insert (org-test-clock-create-clock "-3d 8:00" "-3d 12:00"))
- (insert (org-test-clock-create-clock "-2d 15:00" "-2d 18:00"))
- (insert (org-test-clock-create-clock "-1d 8:00" "-1d 13:00"))
- (test-org-clock-clocktable-contents-at-point
- ":tstart \"<-2d>\" :tend \"<today>\" :indent nil"))))
- ;; Relative time: Yesterday until now.
- (should
- (equal
- "| Headline | Time | |
- |------------------------------+--------+------|
- | *Total time* | *6:00* | |
- |------------------------------+--------+------|
- | Relative times in clocktable | 6:00 | |
- | Foo | | 6:00 |
- "
- (org-test-with-temp-text
- "* Relative times in clocktable\n** Foo\n<point>"
- (insert (org-test-clock-create-clock "-2d 15:00" "-2d 18:00"))
- (insert (org-test-clock-create-clock "-1d 8:00" "-1d 13:00"))
- (insert (org-test-clock-create-clock ". 1:00" ". 2:00"))
- (test-org-clock-clocktable-contents-at-point
- ":tstart \"<yesterday>\" :tend \"<tomorrow>\" :indent nil"))))
- ;; Test `untilnow' block.
- (should
- (equal
- "| Headline | Time | |
- |------------------------------+--------+------|
- | *Total time* | *6:00* | |
- |------------------------------+--------+------|
- | Relative times in clocktable | 6:00 | |
- | Foo | | 6:00 |
- "
- (org-test-with-temp-text
- "* Relative times in clocktable\n** Foo\n<point>"
- (insert (org-test-clock-create-clock "-10y 15:00" "-10y 18:00"))
- (insert (org-test-clock-create-clock "-2d 15:00" "-2d 18:00"))
- (test-org-clock-clocktable-contents-at-point
- ":block untilnow :indent nil")))))
- (provide 'test-org-clock)
- ;;; test-org-clock.el end here
|