emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
blob f99affc1298d2cb9ea2ef9bb785998785ddf2f54 12106 bytes (raw)
name: testing/lisp/test-org-clock.el 	 # note: path name is non-authoritative(*)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
 
;;; 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


\f
;;; 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"))))

\f
;;; 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))))))

\f
;;; 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"))))
  ;; Test tag filtering.
  (should
   (equal
    "| Headline     | Time   |      |
|--------------+--------+------|
| *Total time* | *2:00* |      |
|--------------+--------+------|
| H1           |        | 2:00 |
"
    (org-test-with-temp-text "** H1\n\n*** H2 :tag:\n\n*** H3\n<point>"
      (insert (org-test-clock-create-clock ". 1:00" ". 2:00"))
      (goto-line 4)
      (insert (org-test-clock-create-clock ". 2:00" ". 4:00"))
      (goto-line 2)
      (test-org-clock-clocktable-contents-at-point
       ":tags \"tag\" :indent nil"))))
  ;; Test `file-with-archives' scope.  In particular, preserve "TBLFM"
  ;; line, and ignore "file" column.
  (should
   (equal
    "| Headline     | Time        |     |
|--------------+-------------+-----|
| *Total time* | *704d 9:01* | foo |
|--------------+-------------+-----|
| Test         | 704d 9:01   | foo |
"
    (org-test-with-temp-text-in-file
	"* Test
CLOCK: [2012-03-29 Thu 16:40]--[2014-03-04 Thu 00:41] => 16905:01

#+BEGIN: clocktable :scope file-with-archives
#+TBLFM: $3=string(\"foo\")
#+END:
"
      (search-forward "#+begin:")
      (beginning-of-line)
      (org-update-dblock)
      (forward-line 2)
      (buffer-substring-no-properties
       (point) (progn (goto-char (point-max))
		      (line-beginning-position -1)))))))

(provide 'test-org-clock)
;;; test-org-clock.el end here

debug log:

solving f99affc ...
found f99affc in https://git.savannah.gnu.org/cgit/emacs/org-mode.git

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).