test-org-clock.el 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983
  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
  38. (floor (- (org-time-string-to-seconds end)
  39. (org-time-string-to-seconds beg))))))
  40. (concat org-clock-string " " beg
  41. (when end
  42. (concat "--" end " => "
  43. (format "%2d:%02d"
  44. (/ sec-diff 3600)
  45. (/ (mod sec-diff 3600) 60))))
  46. "\n")))
  47. (defun test-org-clock-clocktable-contents (options &optional initial)
  48. "Return contents of a Clock table for current buffer
  49. OPTIONS is a string of Clock table options. Optional argument
  50. INITIAL is a string specifying initial contents within the Clock
  51. table.
  52. Caption is ignored in contents. The clocktable doesn't appear in
  53. the buffer."
  54. (declare (indent 2))
  55. (goto-char (point-min))
  56. (save-excursion
  57. (insert "#+BEGIN: clocktable " options "\n")
  58. (when initial (insert initial))
  59. (unless (string-suffix-p "\n" initial) (insert "\n"))
  60. (insert "#+END:\n"))
  61. (unwind-protect
  62. (save-excursion
  63. (let ((org-duration-format 'h:mm)) (org-update-dblock))
  64. (forward-line)
  65. ;; Skip caption.
  66. (when (looking-at "#\\+CAPTION:") (forward-line))
  67. (buffer-substring-no-properties
  68. (point) (progn (search-forward "#+END:") (line-end-position 0))))
  69. ;; Remove clocktable.
  70. (delete-region (point) (search-forward "#+END:\n"))))
  71. ;;; Clock drawer
  72. (ert-deftest test-org-clock/into-drawer ()
  73. "Test `org-clock-into-drawer' specifications."
  74. ;; When `org-clock-into-drawer' is nil, do not use a clock drawer.
  75. (should-not
  76. (org-test-with-temp-text "* H"
  77. (let ((org-clock-into-drawer nil)
  78. (org-log-into-drawer nil))
  79. (org-clock-into-drawer))))
  80. (should-not
  81. (org-test-with-temp-text "* H"
  82. (let ((org-clock-into-drawer nil)
  83. (org-log-into-drawer t))
  84. (org-clock-into-drawer))))
  85. (should-not
  86. (org-test-with-temp-text "* H"
  87. (let ((org-clock-into-drawer nil)
  88. (org-log-into-drawer "BAR"))
  89. (org-clock-into-drawer))))
  90. ;; When `org-clock-into-drawer' is a string, use it
  91. ;; unconditionally.
  92. (should
  93. (equal "FOO"
  94. (org-test-with-temp-text "* H"
  95. (let ((org-clock-into-drawer "FOO")
  96. (org-log-into-drawer nil))
  97. (org-clock-into-drawer)))))
  98. (should
  99. (equal "FOO"
  100. (org-test-with-temp-text "* H"
  101. (let ((org-clock-into-drawer "FOO")
  102. (org-log-into-drawer t))
  103. (org-clock-into-drawer)))))
  104. (should
  105. (equal "FOO"
  106. (org-test-with-temp-text "* H"
  107. (let ((org-clock-into-drawer "FOO")
  108. (org-log-into-drawer "BAR"))
  109. (org-clock-into-drawer)))))
  110. ;; When `org-clock-into-drawer' is an integer, return it.
  111. (should
  112. (= 1
  113. (org-test-with-temp-text "* H"
  114. (let ((org-clock-into-drawer 1)
  115. (org-log-into-drawer nil))
  116. (org-clock-into-drawer)))))
  117. (should
  118. (= 1
  119. (org-test-with-temp-text "* H"
  120. (let ((org-clock-into-drawer 1)
  121. (org-log-into-drawer t))
  122. (org-clock-into-drawer)))))
  123. (should
  124. (= 1
  125. (org-test-with-temp-text "* H"
  126. (let ((org-clock-into-drawer 1)
  127. (org-log-into-drawer "BAR"))
  128. (org-clock-into-drawer)))))
  129. ;; Otherwise, any non-nil value defaults to `org-log-into-drawer' or
  130. ;; "LOGBOOK" if it is nil.
  131. (should
  132. (equal "LOGBOOK"
  133. (org-test-with-temp-text "* H"
  134. (let ((org-clock-into-drawer t)
  135. (org-log-into-drawer nil))
  136. (org-clock-into-drawer)))))
  137. (should
  138. (equal "LOGBOOK"
  139. (org-test-with-temp-text "* H"
  140. (let ((org-clock-into-drawer t)
  141. (org-log-into-drawer t))
  142. (org-clock-into-drawer)))))
  143. (should
  144. (equal "FOO"
  145. (org-test-with-temp-text "* H"
  146. (let ((org-clock-into-drawer t)
  147. (org-log-into-drawer "FOO"))
  148. (org-clock-into-drawer)))))
  149. ;; A non-nil "CLOCK_INTO_DRAWER" property overrides
  150. ;; `org-clock-into-drawer' value.
  151. (should
  152. (equal "LOGBOOK"
  153. (org-test-with-temp-text
  154. "* H\n:PROPERTIES:\n:CLOCK_INTO_DRAWER: t\n:END:"
  155. (let ((org-clock-into-drawer nil)
  156. (org-log-into-drawer nil))
  157. (org-clock-into-drawer)))))
  158. (should
  159. (equal "FOO"
  160. (org-test-with-temp-text
  161. "* H\n:PROPERTIES:\n:CLOCK_INTO_DRAWER: FOO\n:END:"
  162. (let ((org-clock-into-drawer nil)
  163. (org-log-into-drawer nil))
  164. (org-clock-into-drawer)))))
  165. (should-not
  166. (org-test-with-temp-text
  167. "* H\n:PROPERTIES:\n:CLOCK_INTO_DRAWER: nil\n:END:"
  168. (let ((org-clock-into-drawer t)
  169. (org-log-into-drawer nil))
  170. (org-clock-into-drawer))))
  171. ;; "CLOCK_INTO_DRAWER" can be inherited.
  172. (should
  173. (equal "LOGBOOK"
  174. (org-test-with-temp-text
  175. "* H\n:PROPERTIES:\n:CLOCK_INTO_DRAWER: t\n:END:\n** H2<point>"
  176. (let ((org-clock-into-drawer nil)
  177. (org-log-into-drawer nil))
  178. (org-clock-into-drawer)))))
  179. (should
  180. (equal "FOO"
  181. (org-test-with-temp-text
  182. "* H\n:PROPERTIES:\n:CLOCK_INTO_DRAWER: FOO\n:END:\n** H2<point>"
  183. (let ((org-clock-into-drawer nil)
  184. (org-log-into-drawer nil))
  185. (org-clock-into-drawer)))))
  186. (should-not
  187. (org-test-with-temp-text
  188. "* H\n:PROPERTIES:\n:CLOCK_INTO_DRAWER: nil\n:END:\n** H2<point>"
  189. (let ((org-clock-into-drawer t)
  190. (org-log-into-drawer nil))
  191. (org-clock-into-drawer)))))
  192. (ert-deftest test-org-clock/drawer-name ()
  193. "Test `org-clock-drawer-name' specifications."
  194. ;; A nil value for `org-clock-into-drawer' means no drawer is
  195. ;; expected whatsoever.
  196. (should-not
  197. (org-test-with-temp-text "* H"
  198. (let ((org-clock-into-drawer nil)
  199. (org-log-into-drawer nil))
  200. (org-clock-drawer-name))))
  201. (should-not
  202. (org-test-with-temp-text "* H"
  203. (let ((org-clock-into-drawer nil)
  204. (org-log-into-drawer t))
  205. (org-clock-drawer-name))))
  206. (should-not
  207. (org-test-with-temp-text "* H"
  208. (let ((org-clock-into-drawer nil)
  209. (org-log-into-drawer "FOO"))
  210. (org-clock-drawer-name))))
  211. ;; A string value for `org-clock-into-drawer' means to use it
  212. ;; unconditionally.
  213. (should
  214. (equal "FOO"
  215. (org-test-with-temp-text "* H"
  216. (let ((org-clock-into-drawer "FOO")
  217. (org-log-into-drawer nil))
  218. (org-clock-drawer-name)))))
  219. (should
  220. (equal "FOO"
  221. (org-test-with-temp-text "* H"
  222. (let ((org-clock-into-drawer "FOO")
  223. (org-log-into-drawer t))
  224. (org-clock-drawer-name)))))
  225. (should
  226. (equal "FOO"
  227. (org-test-with-temp-text "* H"
  228. (let ((org-clock-into-drawer "FOO")
  229. (org-log-into-drawer "BAR"))
  230. (org-clock-drawer-name)))))
  231. ;; When the value in `org-clock-into-drawer' is a number, re-use
  232. ;; `org-log-into-drawer' or use default "LOGBOOK" value.
  233. (should
  234. (equal "FOO"
  235. (org-test-with-temp-text "* H"
  236. (let ((org-clock-into-drawer 1)
  237. (org-log-into-drawer "FOO"))
  238. (org-clock-drawer-name)))))
  239. (should
  240. (equal "LOGBOOK"
  241. (org-test-with-temp-text "* H"
  242. (let ((org-clock-into-drawer 1)
  243. (org-log-into-drawer t))
  244. (org-clock-drawer-name)))))
  245. (should
  246. (equal "LOGBOOK"
  247. (org-test-with-temp-text "* H"
  248. (let ((org-clock-into-drawer 1)
  249. (org-log-into-drawer nil))
  250. (org-clock-drawer-name))))))
  251. ;;; Clocktable
  252. (ert-deftest test-org-clock/clocktable/ranges ()
  253. "Test ranges in Clock table."
  254. ;; Relative time: Previous two days.
  255. (should
  256. (equal
  257. "| Headline | Time | |
  258. |------------------------------+--------+------|
  259. | *Total time* | *8:00* | |
  260. |------------------------------+--------+------|
  261. | Relative times in clocktable | 8:00 | |
  262. | Foo | | 8:00 |"
  263. (org-test-with-temp-text
  264. "* Relative times in clocktable\n** Foo\n<point>"
  265. (insert (org-test-clock-create-clock "-3d 8:00" "-3d 12:00"))
  266. (insert (org-test-clock-create-clock "-2d 15:00" "-2d 18:00"))
  267. (insert (org-test-clock-create-clock "-1d 8:00" "-1d 13:00"))
  268. (test-org-clock-clocktable-contents
  269. ":tstart \"<-2d>\" :tend \"<today>\" :indent nil"))))
  270. ;; Relative time: Yesterday until now.
  271. (should
  272. (equal
  273. "| Headline | Time | |
  274. |------------------------------+--------+------|
  275. | *Total time* | *6:00* | |
  276. |------------------------------+--------+------|
  277. | Relative times in clocktable | 6:00 | |
  278. | Foo | | 6:00 |"
  279. (org-test-with-temp-text
  280. "* Relative times in clocktable\n** Foo\n<point>"
  281. (insert (org-test-clock-create-clock "-2d 15:00" "-2d 18:00"))
  282. (insert (org-test-clock-create-clock "-1d 8:00" "-1d 13:00"))
  283. (insert (org-test-clock-create-clock ". 1:00" ". 2:00"))
  284. (test-org-clock-clocktable-contents
  285. ":tstart \"<yesterday>\" :tend \"<tomorrow>\" :indent nil"))))
  286. ;; Test `untilnow' block.
  287. (should
  288. (equal
  289. "| Headline | Time | |
  290. |------------------------------+--------+------|
  291. | *Total time* | *6:00* | |
  292. |------------------------------+--------+------|
  293. | Relative times in clocktable | 6:00 | |
  294. | Foo | | 6:00 |"
  295. (org-test-with-temp-text
  296. "* Relative times in clocktable\n** Foo\n<point>"
  297. (insert (org-test-clock-create-clock "-10y 15:00" "-10y 18:00"))
  298. (insert (org-test-clock-create-clock "-2d 15:00" "-2d 18:00"))
  299. (test-org-clock-clocktable-contents ":block untilnow :indent nil")))))
  300. (ert-deftest test-org-clock/clocktable/match ()
  301. "Test \":match\" parameter in Clock table."
  302. ;; Test match filtering.
  303. (should
  304. (equal
  305. "| Headline | Time | |
  306. |--------------+--------+------|
  307. | *Total time* | *2:00* | |
  308. |--------------+--------+------|
  309. | H1 | | 2:00 |"
  310. (org-test-with-temp-text "** H1\n\n*** H2 :tag:\n\n*** H3\n<point>"
  311. (insert (org-test-clock-create-clock ". 1:00" ". 2:00"))
  312. (goto-line 4)
  313. (insert (org-test-clock-create-clock ". 2:00" ". 4:00"))
  314. (test-org-clock-clocktable-contents ":match \"tag\" :indent nil")))))
  315. (ert-deftest test-org-clock/clocktable/tags ()
  316. "Test \":tags\" parameter in Clock table."
  317. ;; Test tags column.
  318. (should
  319. (equal
  320. "| Tags | Headline | Time | |
  321. |------+--------------+--------+------|
  322. | | *Total time* | *1:00* | |
  323. |------+--------------+--------+------|
  324. | tag | H1 | | 1:00 |"
  325. (org-test-with-temp-text "** H1 :tag:\n\n*** H2 \n<point>"
  326. (insert (org-test-clock-create-clock ". 1:00" ". 2:00"))
  327. (goto-line 4)
  328. (test-org-clock-clocktable-contents ":tags t :indent nil")))))
  329. (ert-deftest test-org-clock/clocktable/scope ()
  330. "Test \":scope\" parameter in Clock table."
  331. ;; Test `file-with-archives' scope. In particular, preserve "TBLFM"
  332. ;; line, and ignore "file" column.
  333. (should
  334. (equal
  335. "| Headline | Time | |
  336. |--------------+--------+-----|
  337. | *Total time* | *8:40* | foo |
  338. |--------------+--------+-----|
  339. | Test | 8:40 | foo |
  340. #+TBLFM: $3=string(\"foo\")"
  341. (org-test-with-temp-text-in-file
  342. "* Test
  343. CLOCK: [2012-03-29 Thu 8:00]--[2012-03-29 Thu 16:40] => 8:40"
  344. (test-org-clock-clocktable-contents ":scope file-with-archives"
  345. "#+TBLFM: $3=string(\"foo\")"))))
  346. ;; Test "function" scope.
  347. (should
  348. (string-match-p
  349. (regexp-quote "| ALL *Total time* | *1:00* |")
  350. (org-test-with-temp-text-in-file
  351. "* Test
  352. CLOCK: [2012-03-29 Thu 16:00]--[2012-03-29 Thu 17:00] => 1:00"
  353. (let ((the-file (buffer-file-name)))
  354. (org-test-with-temp-text-in-file ""
  355. (test-org-clock-clocktable-contents
  356. (format ":scope (lambda () (list %S))" the-file))))))))
  357. (ert-deftest test-org-clock/clocktable/maxlevel ()
  358. "Test \":maxlevel\" parameter in Clock table."
  359. (should
  360. (equal "| Headline | Time | |
  361. |--------------+--------+------|
  362. | *Total time* | *6:00* | |
  363. |--------------+--------+------|
  364. | Foo | 6:00 | |
  365. | \\_ Bar | | 2:00 |"
  366. (org-test-with-temp-text
  367. "* Foo
  368. CLOCK: [2016-12-28 Wed 11:09]--[2016-12-28 Wed 15:09] => 4:00
  369. ** Bar
  370. CLOCK: [2016-12-28 Wed 13:09]--[2016-12-28 Wed 15:09] => 2:00"
  371. (test-org-clock-clocktable-contents ":maxlevel 3"))))
  372. (should
  373. (equal "| Headline | Time | |
  374. |--------------+--------+------|
  375. | *Total time* | *6:00* | |
  376. |--------------+--------+------|
  377. | Foo | 6:00 | |
  378. | \\_ Bar | | 2:00 |"
  379. (org-test-with-temp-text
  380. "* Foo
  381. CLOCK: [2016-12-28 Wed 11:09]--[2016-12-28 Wed 15:09] => 4:00
  382. ** Bar
  383. CLOCK: [2016-12-28 Wed 13:09]--[2016-12-28 Wed 15:09] => 2:00"
  384. (test-org-clock-clocktable-contents ":maxlevel 2"))))
  385. (should
  386. (equal "| Headline | Time |
  387. |--------------+--------|
  388. | *Total time* | *6:00* |
  389. |--------------+--------|
  390. | Foo | 6:00 |"
  391. (org-test-with-temp-text
  392. "* Foo
  393. CLOCK: [2016-12-28 Wed 11:09]--[2016-12-28 Wed 15:09] => 4:00
  394. ** Bar
  395. CLOCK: [2016-12-28 Wed 13:09]--[2016-12-28 Wed 15:09] => 2:00"
  396. (test-org-clock-clocktable-contents ":maxlevel 1"))))
  397. ;; Special ":maxlevel 0" case: only report total file time.
  398. (should
  399. (equal "| Headline | Time |
  400. |--------------+--------|
  401. | *Total time* | *6:00* |
  402. |--------------+--------|"
  403. (org-test-with-temp-text
  404. "* Foo
  405. CLOCK: [2016-12-28 Wed 11:09]--[2016-12-28 Wed 15:09] => 4:00
  406. ** Bar
  407. CLOCK: [2016-12-28 Wed 13:09]--[2016-12-28 Wed 15:09] => 2:00"
  408. (test-org-clock-clocktable-contents ":maxlevel 0")))))
  409. (ert-deftest test-org-clock/clocktable/formula ()
  410. "Test \":formula\" parameter in Clock table."
  411. ;; Test ":formula %". Handle various duration formats.
  412. (should
  413. (equal
  414. "| Headline | Time | % |
  415. |--------------+--------+-------|
  416. | *Total time* | *6:00* | 100.0 |
  417. |--------------+--------+-------|
  418. | Foo | 4:00 | 66.7 |
  419. | Bar | 2:00 | 33.3 |"
  420. (org-test-with-temp-text
  421. "* Foo
  422. CLOCK: [2016-12-28 Wed 11:09]--[2016-12-28 Wed 15:09] => 4:00
  423. * Bar
  424. CLOCK: [2016-12-28 Wed 13:09]--[2016-12-28 Wed 15:09] => 2:00"
  425. (test-org-clock-clocktable-contents ":maxlevel 1 :formula %"))))
  426. (should
  427. (equal
  428. "| Headline | Time | % |
  429. |--------------+---------+-------|
  430. | *Total time* | *28:00* | 100.0 |
  431. |--------------+---------+-------|
  432. | Foo | 26:00 | 92.9 |
  433. | Bar | 2:00 | 7.1 |"
  434. (org-test-with-temp-text
  435. "* Foo
  436. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00
  437. * Bar
  438. CLOCK: [2016-12-28 Wed 13:09]--[2016-12-28 Wed 15:09] => 2:00"
  439. (test-org-clock-clocktable-contents ":maxlevel 1 :formula %"))))
  440. ;; Properly align column with different depths.
  441. (should
  442. (equal "| Headline | Time | | | % |
  443. |---------------+--------+------+------+-------|
  444. | *Total time* | *1:00* | | | 100.0 |
  445. |---------------+--------+------+------+-------|
  446. | foo | 1:00 | | | 100.0 |
  447. | \\_ sub | | 0:15 | | 25.0 |
  448. | \\_ sub2 | | 0:15 | | 25.0 |
  449. | \\_ sub3 | | 0:30 | | 50.0 |
  450. | \\_ subsub1 | | | 0:15 | 25.0 |
  451. | \\_ subsub1 | | | 0:15 | 25.0 |"
  452. (org-test-with-temp-text
  453. "* foo
  454. ** sub
  455. :LOGBOOK:
  456. CLOCK: [2017-03-18 Sat 15:00]--[2017-03-18 Sat 15:15] => 0:15
  457. :END:
  458. ** sub2
  459. :LOGBOOK:
  460. CLOCK: [2017-03-18 Sat 15:15]--[2017-03-18 Sat 15:30] => 0:15
  461. :END:
  462. ** sub3
  463. *** subsub1
  464. :LOGBOOK:
  465. CLOCK: [2017-03-18 Sat 13:00]--[2017-03-18 Sat 13:15] => 0:15
  466. :END:
  467. *** subsub1
  468. :LOGBOOK:
  469. CLOCK: [2017-03-18 Sat 14:00]--[2017-03-18 Sat 14:15] => 0:15
  470. :END:"
  471. (test-org-clock-clocktable-contents ":maxlevel 3 :formula %")))))
  472. (ert-deftest test-org-clock/clocktable/lang ()
  473. "Test \":lang\" parameter in Clock table."
  474. ;; Test foreign translation
  475. (should
  476. (equal
  477. "| Headline | Time |
  478. |--------------+---------|
  479. | *Total time* | *26:00* |
  480. |--------------+---------|
  481. | Foo | 26:00 |"
  482. (org-test-with-temp-text
  483. "* Foo
  484. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
  485. (test-org-clock-clocktable-contents ":maxlevel 1 :lang en"))))
  486. (should
  487. (equal
  488. "| En-tête | Durée |
  489. |----------------+---------|
  490. | *Durée totale* | *26:00* |
  491. |----------------+---------|
  492. | Foo | 26:00 |"
  493. (org-test-with-temp-text
  494. "* Foo
  495. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
  496. (test-org-clock-clocktable-contents ":maxlevel 1 :lang fr"))))
  497. ;; No :lang parameter is equivalent to "en".
  498. (should
  499. (equal
  500. (org-test-with-temp-text
  501. "* Foo
  502. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
  503. (test-org-clock-clocktable-contents ":maxlevel 1 :lang en"))
  504. (org-test-with-temp-text
  505. "* Foo
  506. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
  507. (test-org-clock-clocktable-contents ":maxlevel 1"))))
  508. ;; Unknown translation fall backs to "en".
  509. (should
  510. (equal
  511. "| Headline | Time |
  512. |--------------+---------|
  513. | *Total time* | *26:00* |
  514. |--------------+---------|
  515. | Foo | 26:00 |"
  516. (org-test-with-temp-text
  517. "* Foo
  518. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
  519. (test-org-clock-clocktable-contents ":maxlevel 1 :lang foo")))))
  520. (ert-deftest test-org-clock/clocktable/link ()
  521. "Test \":link\" parameter in Clock table."
  522. ;; If there is no file attached to the document, link directly to
  523. ;; the headline.
  524. (should
  525. (equal
  526. "| Headline | Time |
  527. |--------------+---------|
  528. | *Total time* | *26:00* |
  529. |--------------+---------|
  530. | [[Foo][Foo]] | 26:00 |"
  531. (org-test-with-temp-text
  532. "* Foo
  533. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
  534. (test-org-clock-clocktable-contents ":link t"))))
  535. ;; Otherwise, link to the headline in the current file.
  536. (should
  537. (equal
  538. "| Headline | Time |
  539. |--------------+---------|
  540. | *Total time* | *26:00* |
  541. |--------------+---------|
  542. | [[file:filename::Foo][Foo]] | 26:00 |"
  543. (org-test-with-temp-text-in-file
  544. "* Foo
  545. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
  546. (let ((file (buffer-file-name)))
  547. (replace-regexp-in-string
  548. (regexp-quote file) "filename"
  549. (test-org-clock-clocktable-contents ":link t"))))))
  550. ;; Ignore TODO keyword, priority cookie, COMMENT and tags in
  551. ;; headline.
  552. (should
  553. (equal
  554. "| Headline | Time |
  555. |--------------+---------|
  556. | *Total time* | *26:00* |
  557. |--------------+---------|
  558. | [[Foo][Foo]] | 26:00 |"
  559. (org-test-with-temp-text
  560. "* TODO Foo
  561. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
  562. (test-org-clock-clocktable-contents ":link t"))))
  563. (should
  564. (equal
  565. "| Headline | Time |
  566. |--------------+---------|
  567. | *Total time* | *26:00* |
  568. |--------------+---------|
  569. | [[Foo][Foo]] | 26:00 |"
  570. (org-test-with-temp-text
  571. "* [#A] Foo
  572. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
  573. (test-org-clock-clocktable-contents ":link t"))))
  574. (should
  575. (equal
  576. "| Headline | Time |
  577. |--------------+---------|
  578. | *Total time* | *26:00* |
  579. |--------------+---------|
  580. | [[Foo][Foo]] | 26:00 |"
  581. (org-test-with-temp-text
  582. "* COMMENT Foo
  583. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
  584. (test-org-clock-clocktable-contents ":link t"))))
  585. (should
  586. (equal
  587. "| Headline | Time |
  588. |--------------+---------|
  589. | *Total time* | *26:00* |
  590. |--------------+---------|
  591. | [[Foo][Foo]] | 26:00 |"
  592. (org-test-with-temp-text
  593. "* Foo :tag:
  594. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
  595. (test-org-clock-clocktable-contents ":link t"))))
  596. ;; Remove statistics cookie from headline description.
  597. (should
  598. (equal
  599. "| Headline | Time |
  600. |--------------+---------|
  601. | *Total time* | *26:00* |
  602. |--------------+---------|
  603. | [[Foo][Foo]] | 26:00 |"
  604. (org-test-with-temp-text
  605. "* Foo [50%]
  606. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
  607. (test-org-clock-clocktable-contents ":link t"))))
  608. (should
  609. (equal
  610. "| Headline | Time |
  611. |--------------+---------|
  612. | *Total time* | *26:00* |
  613. |--------------+---------|
  614. | [[Foo][Foo]] | 26:00 |"
  615. (org-test-with-temp-text
  616. "* Foo [1/2]
  617. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
  618. (test-org-clock-clocktable-contents ":link t"))))
  619. ;; Replace links with their description, or turn them into plain
  620. ;; links if there is no description.
  621. (should
  622. (equal
  623. "| Headline | Time |
  624. |--------------+---------|
  625. | *Total time* | *26:00* |
  626. |--------------+---------|
  627. | [[Foo %5B%5Bhttps://orgmode.org%5D%5BOrg mode%5D%5D][Foo Org mode]] | 26:00 |"
  628. (org-test-with-temp-text
  629. "* Foo [[https://orgmode.org][Org mode]]
  630. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
  631. (test-org-clock-clocktable-contents ":link t"))))
  632. (should
  633. (equal
  634. "| Headline | Time |
  635. |-------------------------+---------|
  636. | *Total time* | *26:00* |
  637. |-------------------------+---------|
  638. | [[Foo %5B%5Bhttps://orgmode.org%5D%5D][Foo https://orgmode.org]] | 26:00 |"
  639. (org-test-with-temp-text
  640. "* Foo [[https://orgmode.org]]
  641. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
  642. (test-org-clock-clocktable-contents ":link t")))))
  643. (ert-deftest test-org-clock/clocktable/compact ()
  644. "Test \":compact\" parameter in Clock table."
  645. ;; With :compact, all headlines are in the same column.
  646. (should
  647. (equal
  648. "| Headline | Time |
  649. |--------------+---------|
  650. | *Total time* | *26:00* |
  651. |--------------+---------|
  652. | Foo | 26:00 |"
  653. (org-test-with-temp-text
  654. "* Foo
  655. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
  656. (test-org-clock-clocktable-contents ":compact t"))))
  657. (should
  658. (equal
  659. "| Headline | Time |
  660. |--------------+---------|
  661. | *Total time* | *52:00* |
  662. |--------------+---------|
  663. | Foo | 52:00 |
  664. | \\_ Bar | 26:00 |"
  665. (org-test-with-temp-text
  666. "* Foo
  667. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00
  668. ** Bar
  669. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
  670. (test-org-clock-clocktable-contents ":compact t"))))
  671. ;; :maxlevel does not affect :compact parameter.
  672. (should
  673. (equal
  674. "| Headline | Time |
  675. |--------------+---------|
  676. | *Total time* | *52:00* |
  677. |--------------+---------|
  678. | Foo | 52:00 |
  679. | \\_ Bar | 26:00 |"
  680. (org-test-with-temp-text
  681. "* Foo
  682. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00
  683. ** Bar
  684. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
  685. (test-org-clock-clocktable-contents ":compact t :maxlevel 2"))))
  686. ;; :compact implies a non-nil :indent parameter.
  687. (should
  688. (equal
  689. "| Headline | Time |
  690. |--------------+---------|
  691. | *Total time* | *52:00* |
  692. |--------------+---------|
  693. | Foo | 52:00 |
  694. | \\_ Bar | 26:00 |"
  695. (org-test-with-temp-text
  696. "* Foo
  697. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00
  698. ** Bar
  699. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
  700. (test-org-clock-clocktable-contents ":compact t :indent nil"))))
  701. ;; :compact implies a nil :level parameter.
  702. (should
  703. (equal
  704. "| Headline | Time |
  705. |--------------+---------|
  706. | *Total time* | *52:00* |
  707. |--------------+---------|
  708. | Foo | 52:00 |
  709. | \\_ Bar | 26:00 |"
  710. (org-test-with-temp-text
  711. "* Foo
  712. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00
  713. ** Bar
  714. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
  715. (test-org-clock-clocktable-contents ":compact t :level t")))))
  716. (ert-deftest test-org-clock/clocktable/properties ()
  717. "Test \":properties\" parameter in Clock table."
  718. ;; Include a new column with list properties.
  719. (should
  720. (equal
  721. "| A | Headline | Time |
  722. |---+--------------+---------|
  723. | | *Total time* | *26:00* |
  724. |---+--------------+---------|
  725. | 1 | Foo | 26:00 |"
  726. (org-test-with-temp-text
  727. "* Foo
  728. :PROPERTIES:
  729. :A: 1
  730. :END:
  731. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
  732. (test-org-clock-clocktable-contents ":properties (\"A\")"))))
  733. (should
  734. (equal
  735. "| A | Headline | Time | |
  736. |---+--------------+---------+-------|
  737. | | *Total time* | *52:00* | |
  738. |---+--------------+---------+-------|
  739. | | Foo | 52:00 | |
  740. | 1 | \\_ Bar | | 26:00 |"
  741. (org-test-with-temp-text
  742. "* Foo
  743. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00
  744. ** Bar
  745. :PROPERTIES:
  746. :A: 1
  747. :END:
  748. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
  749. (test-org-clock-clocktable-contents ":properties (\"A\")"))))
  750. ;; Handle missing properties.
  751. (should
  752. (equal
  753. "| A | Headline | Time |
  754. |---+--------------+---------|
  755. | | *Total time* | *26:00* |
  756. |---+--------------+---------|
  757. | 1 | Foo | 26:00 |"
  758. (org-test-with-temp-text
  759. "* Foo
  760. :PROPERTIES:
  761. :A: 1
  762. :END:
  763. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
  764. (test-org-clock-clocktable-contents ":properties (\"A\")")))))
  765. (ert-deftest test-org-clock/clocktable/tcolumns ()
  766. "Test \":tcolumns\" parameter in Clock table."
  767. ;; When :tcolumns is smaller than the deepest headline level, lump
  768. ;; lower levels in the last column.
  769. (should
  770. (equal
  771. "| Headline | Time |
  772. |--------------+---------|
  773. | *Total time* | *52:00* |
  774. |--------------+---------|
  775. | Foo | 52:00 |
  776. | \\_ Bar | 26:00 |"
  777. (org-test-with-temp-text
  778. "* Foo
  779. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00
  780. ** Bar
  781. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
  782. (test-org-clock-clocktable-contents ":tcolumns 1"))))
  783. ;; :tcolumns cannot create more columns than the deepest headline
  784. ;; level.
  785. (should
  786. (equal
  787. "| Headline | Time | |
  788. |--------------+---------+-------|
  789. | *Total time* | *52:00* | |
  790. |--------------+---------+-------|
  791. | Foo | 52:00 | |
  792. | \\_ Bar | | 26:00 |"
  793. (org-test-with-temp-text
  794. "* Foo
  795. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00
  796. ** Bar
  797. CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
  798. (test-org-clock-clocktable-contents ":tcolumns 3"))))
  799. ;; Pathological case: when no headline contributes to the total
  800. ;; time, there is only one time column.
  801. (should
  802. (equal "| Headline | Time |
  803. |--------------+--------|
  804. | *Total time* | *0:00* |"
  805. (org-test-with-temp-text
  806. "* Foo
  807. CLOCK: [2016-12-28 Wed 11:09]--[2016-12-28 Wed 11:09] => 0:00
  808. ** Bar
  809. CLOCK: [2016-12-28 Wed 13:09]--[2016-12-28 Wed 13:09] => 0:00"
  810. (test-org-clock-clocktable-contents ":tcolumns 2")))))
  811. (ert-deftest test-org-clock/clocktable/step ()
  812. "Test \":step\" parameter in Clock table."
  813. ;; Regression test: week crossing month boundary before :wstart
  814. ;; day-of-week.
  815. (should
  816. (equal "
  817. Weekly report starting on: [2017-09-25 Mon]
  818. | Headline | Time |
  819. |--------------+--------|
  820. | *Total time* | *1:00* |
  821. |--------------+--------|
  822. | Foo | 1:00 |"
  823. (org-test-with-temp-text
  824. "* Foo
  825. CLOCK: [2017-09-30 Sat 12:00]--[2017-09-30 Sat 13:00] => 1:00
  826. CLOCK: [2017-10-01 Sun 11:00]--[2017-10-01 Sun 13:00] => 2:00
  827. CLOCK: [2017-10-02 Mon 11:00]--[2017-10-02 Mon 14:00] => 3:00"
  828. (let ((system-time-locale "en_US"))
  829. (test-org-clock-clocktable-contents
  830. ":step week :block 2017-09 :stepskip0 t")))))
  831. (should
  832. (equal "
  833. Weekly report starting on: [2017-10-01 Sun]
  834. | Headline | Time |
  835. |--------------+--------|
  836. | *Total time* | *2:00* |
  837. |--------------+--------|
  838. | Foo | 2:00 |
  839. Weekly report starting on: [2017-10-02 Mon]
  840. | Headline | Time |
  841. |--------------+--------|
  842. | *Total time* | *7:00* |
  843. |--------------+--------|
  844. | Foo | 7:00 |
  845. Weekly report starting on: [2017-10-09 Mon]
  846. | Headline | Time |
  847. |--------------+--------|
  848. | *Total time* | *5:00* |
  849. |--------------+--------|
  850. | Foo | 5:00 |
  851. "
  852. (org-test-with-temp-text
  853. "* Foo
  854. CLOCK: [2017-09-30 Sat 12:00]--[2017-09-30 Sat 13:00] => 1:00
  855. CLOCK: [2017-10-01 Sun 11:00]--[2017-10-01 Sun 13:00] => 2:00
  856. CLOCK: [2017-10-02 Mon 11:00]--[2017-10-02 Mon 14:00] => 3:00
  857. CLOCK: [2017-10-08 Sun 09:00]--[2017-10-08 Sun 13:00] => 4:00
  858. CLOCK: [2017-10-09 Mon 09:00]--[2017-10-09 Mon 14:00] => 5:00"
  859. (let ((system-time-locale "en_US"))
  860. (test-org-clock-clocktable-contents
  861. ":step week :block 2017-10 :stepskip0 t")))))
  862. ;; :step day
  863. (should
  864. (equal "
  865. Daily report: [2017-10-02 Mon]
  866. | Headline | Time |
  867. |--------------+--------|
  868. | *Total time* | *3:00* |
  869. |--------------+--------|
  870. | Foo | 3:00 |
  871. Daily report: [2017-10-03 Tue]
  872. | Headline | Time |
  873. |--------------+--------|
  874. | *Total time* | *0:00* |
  875. Daily report: [2017-10-04 Wed]
  876. | Headline | Time |
  877. |--------------+--------|
  878. | *Total time* | *0:00* |
  879. Daily report: [2017-10-05 Thu]
  880. | Headline | Time |
  881. |--------------+--------|
  882. | *Total time* | *0:00* |
  883. Daily report: [2017-10-06 Fri]
  884. | Headline | Time |
  885. |--------------+--------|
  886. | *Total time* | *0:00* |
  887. Daily report: [2017-10-07 Sat]
  888. | Headline | Time |
  889. |--------------+--------|
  890. | *Total time* | *0:00* |
  891. Daily report: [2017-10-08 Sun]
  892. | Headline | Time |
  893. |--------------+--------|
  894. | *Total time* | *4:00* |
  895. |--------------+--------|
  896. | Foo | 4:00 |"
  897. (org-test-with-temp-text
  898. "* Foo
  899. CLOCK: [2017-09-30 Sat 12:00]--[2017-09-30 Sat 13:00] => 1:00
  900. CLOCK: [2017-10-01 Sun 11:00]--[2017-10-01 Sun 13:00] => 2:00
  901. CLOCK: [2017-10-02 Mon 11:00]--[2017-10-02 Mon 14:00] => 3:00
  902. CLOCK: [2017-10-08 Sun 09:00]--[2017-10-08 Sun 13:00] => 4:00
  903. CLOCK: [2017-10-09 Mon 09:00]--[2017-10-09 Mon 14:00] => 5:00"
  904. (let ((system-time-locale "en_US"))
  905. (test-org-clock-clocktable-contents
  906. ":step day :block 2017-W40")))))
  907. ;; Regression test: take :tstart and :tend hours into consideration.
  908. (should
  909. (equal "
  910. Weekly report starting on: [2017-12-25 Mon]
  911. | Headline | Time |
  912. |--------------+--------|
  913. | *Total time* | *8:00* |
  914. |--------------+--------|
  915. | Foo | 8:00 |"
  916. (org-test-with-temp-text
  917. "* Foo
  918. CLOCK: [2017-12-27 Wed 08:00]--[2017-12-27 Wed 16:00] => 8:00"
  919. (let ((system-time-locale "en_US"))
  920. (test-org-clock-clocktable-contents
  921. (concat ":step week :tstart \"<2017-12-25 Mon>\" "
  922. ":tend \"<2017-12-27 Wed 23:59>\""))))))
  923. (should
  924. (equal "
  925. Daily report: [2017-12-27 Wed]
  926. | Headline | Time |
  927. |--------------+--------|
  928. | *Total time* | *8:00* |
  929. |--------------+--------|
  930. | Foo | 8:00 |"
  931. (org-test-with-temp-text
  932. "* Foo
  933. CLOCK: [2017-12-27 Wed 08:00]--[2017-12-27 Wed 16:00] => 8:00"
  934. (let ((system-time-locale "en_US"))
  935. (test-org-clock-clocktable-contents
  936. (concat ":step day :tstart \"<2017-12-25 Mon>\" "
  937. ":tend \"<2017-12-27 Wed 23:59>\" :stepskip0 t")))))))
  938. (provide 'test-org-clock)
  939. ;;; test-org-clock.el end here