emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
blob d2179e1ec88afbec5b52d4106092b8981d4cf0bc 38322 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
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
 
;;; test-org-clock.el --- Tests for org-clock.el

;; Copyright (C) 2012, 2014, 2015, 2019  Nicolas Goaziou

;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>

;; Released under the GNU General Public License version 3
;; see: https://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 (options &optional initial)
  "Return contents of a Clock table for current buffer

OPTIONS is a string of Clock table options.  Optional argument
INITIAL is a string specifying initial contents within the Clock
table.

Caption is ignored in contents.  The clocktable doesn't appear in
the buffer."
  (declare (indent 2))
  (goto-char (point-min))
  (save-excursion
    (insert "#+BEGIN: clocktable " options "\n")
    (when initial (insert initial))
    (unless (string-suffix-p "\n" initial) (insert "\n"))
    (insert "#+END:\n"))
  (unwind-protect
      (save-excursion
        (let ((org-duration-format 'h:mm)) (org-update-dblock))
        (forward-line)
        ;; Skip caption.
        (when (looking-at "#\\+CAPTION:") (forward-line))
        (buffer-substring-no-properties
         (point) (progn (search-forward "#+END:") (line-end-position 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/insert ()
  "Test insert clocktable dynamic block with `org-dynamic-block-insert-dblock'."
  (should
   (equal
    "| Headline     | Time   |
|--------------+--------|
| *Total time* | *1:00* |
|--------------+--------|
| H1           | 1:00   |"
    (org-test-with-temp-text "* H1\n<point>"
      (insert (org-test-clock-create-clock ". 1:00" ". 2:00"))

      (goto-line 2)
      (require 'org-clock)
      (org-dynamic-block-insert-dblock "clocktable")

      (goto-line 1)
      (unwind-protect
	  (save-excursion
	    (when (search-forward "#+CAPTION:") (forward-line))
	    (buffer-substring-no-properties
	     (point) (progn (search-forward "#+END:") (line-end-position 0))))
	(delete-region (point) (search-forward "#+END:\n")))))))

(ert-deftest test-org-clock/clocktable/ranges ()
  "Test ranges in Clock table."
  ;; 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
       ":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
       ":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 ":block untilnow :indent nil")))))

(ert-deftest test-org-clock/clocktable/match ()
  "Test \":match\" parameter in Clock table."
  ;; Test match 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 ". 8:00" ". 9:00"))
      (goto-line 4)
      (insert (org-test-clock-create-clock ". 9:00" ". 11:00"))
      (test-org-clock-clocktable-contents ":match \"tag\" :indent nil")))))

(ert-deftest test-org-clock/clocktable/tags ()
  "Test \":tags\" parameter in Clock table."
  ;; Test tags column.
  (should
   (equal
    "| Tags | Headline     | Time   |      |
|------+--------------+--------+------|
|      | *Total time* | *1:00* |      |
|------+--------------+--------+------|
| tag  | H1           |        | 1:00 |"
    (org-test-with-temp-text "** H1 :tag:\n\n*** H2 \n<point>"
      (insert (org-test-clock-create-clock ". 1:00" ". 2:00"))
      (goto-line 4)
      (test-org-clock-clocktable-contents ":tags t :indent nil")))))

(ert-deftest test-org-clock/clocktable/scope ()
  "Test \":scope\" parameter in Clock table."
  ;; Test `file-with-archives' scope.  In particular, preserve "TBLFM"
  ;; line, and ignore "file" column.
  (should
   (equal
    "| Headline     | Time   |     |
|--------------+--------+-----|
| *Total time* | *8:40* | foo |
|--------------+--------+-----|
| Test         | 8:40   | foo |
#+TBLFM: $3=string(\"foo\")"
    (org-test-with-temp-text-in-file
        "* Test
CLOCK: [2012-03-29 Thu 8:00]--[2012-03-29 Thu 16:40] => 8:40"
      (test-org-clock-clocktable-contents ":scope file-with-archives"
                                          "#+TBLFM: $3=string(\"foo\")"))))
  ;; Test "function" scope.
  (should
   (string-match-p
    (regexp-quote "| ALL *Total time* | *1:00* |")
    (org-test-with-temp-text-in-file
        "* Test
CLOCK: [2012-03-29 Thu 16:00]--[2012-03-29 Thu 17:00] =>  1:00"
      (let ((the-file (buffer-file-name)))
        (org-test-with-temp-text-in-file ""
          (test-org-clock-clocktable-contents
           (format ":scope (lambda () (list %S))" the-file))))))))

(ert-deftest test-org-clock/clocktable/maxlevel ()
  "Test \":maxlevel\" parameter in Clock table."
  (should
   (equal "| Headline     | Time   |      |
|--------------+--------+------|
| *Total time* | *6:00* |      |
|--------------+--------+------|
| Foo          | 6:00   |      |
| \\_  Bar      |        | 2:00 |"
          (org-test-with-temp-text
              "* Foo
CLOCK: [2016-12-28 Wed 11:09]--[2016-12-28 Wed 15:09] =>  4:00
** Bar
CLOCK: [2016-12-28 Wed 13:09]--[2016-12-28 Wed 15:09] =>  2:00"
            (test-org-clock-clocktable-contents ":maxlevel 3"))))
  (should
   (equal "| Headline     | Time   |      |
|--------------+--------+------|
| *Total time* | *6:00* |      |
|--------------+--------+------|
| Foo          | 6:00   |      |
| \\_  Bar      |        | 2:00 |"
          (org-test-with-temp-text
              "* Foo
CLOCK: [2016-12-28 Wed 11:09]--[2016-12-28 Wed 15:09] =>  4:00
** Bar
CLOCK: [2016-12-28 Wed 13:09]--[2016-12-28 Wed 15:09] =>  2:00"
            (test-org-clock-clocktable-contents ":maxlevel 2"))))
  (should
   (equal "| Headline     | Time   |
|--------------+--------|
| *Total time* | *6:00* |
|--------------+--------|
| Foo          | 6:00   |"
          (org-test-with-temp-text
              "* Foo
CLOCK: [2016-12-28 Wed 11:09]--[2016-12-28 Wed 15:09] =>  4:00
** Bar
CLOCK: [2016-12-28 Wed 13:09]--[2016-12-28 Wed 15:09] =>  2:00"
            (test-org-clock-clocktable-contents ":maxlevel 1"))))
  ;; Special ":maxlevel 0" case: only report total file time.
  (should
   (equal "| Headline     | Time   |
|--------------+--------|
| *Total time* | *6:00* |
|--------------+--------|"
          (org-test-with-temp-text
              "* Foo
CLOCK: [2016-12-28 Wed 11:09]--[2016-12-28 Wed 15:09] =>  4:00
** Bar
CLOCK: [2016-12-28 Wed 13:09]--[2016-12-28 Wed 15:09] =>  2:00"
            (test-org-clock-clocktable-contents ":maxlevel 0")))))

(ert-deftest test-org-clock/clocktable/formula ()
  "Test \":formula\" parameter in Clock table."
  ;; Test ":formula %".  Handle various duration formats.
  (should
   (equal
    "| Headline     |   Time |     % |
|--------------+--------+-------|
| *Total time* | *6:00* | 100.0 |
|--------------+--------+-------|
| Foo          |   4:00 |  66.7 |
| Bar          |   2:00 |  33.3 |"
    (org-test-with-temp-text
        "* Foo
  CLOCK: [2016-12-28 Wed 11:09]--[2016-12-28 Wed 15:09] =>  4:00
* Bar
  CLOCK: [2016-12-28 Wed 13:09]--[2016-12-28 Wed 15:09] =>  2:00"
      (test-org-clock-clocktable-contents ":maxlevel 1 :formula %"))))
  (should
   (equal
    "| Headline     |    Time |     % |
|--------------+---------+-------|
| *Total time* | *28:00* | 100.0 |
|--------------+---------+-------|
| Foo          |   26:00 |  92.9 |
| Bar          |    2:00 |   7.1 |"
    (org-test-with-temp-text
        "* Foo
  CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00
* Bar
  CLOCK: [2016-12-28 Wed 13:09]--[2016-12-28 Wed 15:09] =>  2:00"
      (test-org-clock-clocktable-contents ":maxlevel 1 :formula %"))))
  ;; Properly align column with different depths.
  (should
   (equal "| Headline      | Time   |      |      |     % |
|---------------+--------+------+------+-------|
| *Total time*  | *1:00* |      |      | 100.0 |
|---------------+--------+------+------+-------|
| foo           | 1:00   |      |      | 100.0 |
| \\_  sub       |        | 0:15 |      |  25.0 |
| \\_  sub2      |        | 0:15 |      |  25.0 |
| \\_  sub3      |        | 0:30 |      |  50.0 |
| \\_    subsub1 |        |      | 0:15 |  25.0 |
| \\_    subsub1 |        |      | 0:15 |  25.0 |"
          (org-test-with-temp-text
              "* foo
** sub
   :LOGBOOK:
   CLOCK: [2017-03-18 Sat 15:00]--[2017-03-18 Sat 15:15] =>  0:15
   :END:
** sub2
   :LOGBOOK:
   CLOCK: [2017-03-18 Sat 15:15]--[2017-03-18 Sat 15:30] =>  0:15
   :END:
** sub3
*** subsub1
    :LOGBOOK:
    CLOCK: [2017-03-18 Sat 13:00]--[2017-03-18 Sat 13:15] =>  0:15
    :END:
*** subsub1
    :LOGBOOK:
    CLOCK: [2017-03-18 Sat 14:00]--[2017-03-18 Sat 14:15] =>  0:15
    :END:"
            (test-org-clock-clocktable-contents ":maxlevel 3 :formula %")))))

(ert-deftest test-org-clock/clocktable/lang ()
  "Test \":lang\" parameter in Clock table."
  ;; Test foreign translation
  (should
   (equal
    "| Headline     | Time    |
|--------------+---------|
| *Total time* | *26:00* |
|--------------+---------|
| Foo          | 26:00   |"
    (org-test-with-temp-text
        "* Foo
  CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
      (test-org-clock-clocktable-contents ":maxlevel 1 :lang en"))))
  (should
   (equal
    "| En-tête        | Durée   |
|----------------+---------|
| *Durée totale* | *26:00* |
|----------------+---------|
| Foo            | 26:00   |"
    (org-test-with-temp-text
        "* Foo
  CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
      (test-org-clock-clocktable-contents ":maxlevel 1 :lang fr"))))
  ;; No :lang parameter is equivalent to "en".
  (should
   (equal
    (org-test-with-temp-text
        "* Foo
  CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
      (test-org-clock-clocktable-contents ":maxlevel 1 :lang en"))
    (org-test-with-temp-text
        "* Foo
  CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
      (test-org-clock-clocktable-contents ":maxlevel 1"))))
  ;; Unknown translation fall backs to "en".
  (should
   (equal
    "| Headline     | Time    |
|--------------+---------|
| *Total time* | *26:00* |
|--------------+---------|
| Foo          | 26:00   |"
    (org-test-with-temp-text
        "* Foo
  CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
      (test-org-clock-clocktable-contents ":maxlevel 1 :lang foo")))))

(ert-deftest test-org-clock/clocktable/link ()
  "Test \":link\" parameter in Clock table."
  ;; If there is no file attached to the document, link directly to
  ;; the headline.
  (should
   (string-match-p "| +\\[\\[\\*Foo]\\[Foo]] +| 26:00 +|"
		   (org-test-with-temp-text
		       "* Foo
CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
		     (test-org-clock-clocktable-contents ":link t"))))
  ;; Otherwise, link to the headline in the current file.
  (should
   (string-match-p
    "| \\[\\[file:filename::\\*Foo]\\[Foo]] +| 26:00 +|"
    (org-test-with-temp-text
	(org-test-with-temp-text-in-file
	    "* Foo
CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
	  (let ((file (buffer-file-name)))
	    (replace-regexp-in-string
	     (regexp-quote file) "filename"
	     (test-org-clock-clocktable-contents ":link t :lang en"))))
      (org-table-align)
      (buffer-substring-no-properties (point-min) (point-max)))))
  ;; Ignore TODO keyword, priority cookie, COMMENT and tags in
  ;; headline.
  (should
   (string-match-p
    "| \\[\\[\\*Foo]\\[Foo]] +| 26:00 +|"
    (org-test-with-temp-text
	"* TODO Foo
CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
      (test-org-clock-clocktable-contents ":link t :lang en"))))
  (should
   (string-match-p
    "| \\[\\[\\*Foo]\\[Foo]] +| 26:00 +|"
    (org-test-with-temp-text
	"* [#A] Foo
CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
      (test-org-clock-clocktable-contents ":link t :lang en"))))
  (should
   (string-match-p
    "| \\[\\[\\*Foo]\\[Foo]] +| 26:00 +|"
    (org-test-with-temp-text
	"* COMMENT Foo
CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
      (test-org-clock-clocktable-contents ":link t"))))
  (should
   (string-match-p
    "| \\[\\[\\*Foo]\\[Foo]] +| 26:00 +|"
    (org-test-with-temp-text
	"* Foo :tag:
CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
      (test-org-clock-clocktable-contents ":link t :lang en"))))
  ;; Remove statistics cookie from headline description.
  (should
   (string-match-p
    "| \\[\\[\\*Foo]\\[Foo]] +| 26:00 +|"
    (org-test-with-temp-text
	"* Foo [50%]
CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
      (test-org-clock-clocktable-contents ":link t :lang en"))))
  (should
   (string-match-p
    "| \\[\\[\\*Foo]\\[Foo]] +| 26:00 +|"
    (org-test-with-temp-text
	"* Foo [1/2]
CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
      (test-org-clock-clocktable-contents ":link t :lang en"))))
  ;; Replace links with their description, or turn them into plain
  ;; links if there is no description.
  (should
   (string-match-p
    "| \\[\\[\\*Foo \\\\\\[\\\\\\[https://orgmode\\.org\\\\]\\\\\\[Org mode\\\\]\\\\]]\\[Foo Org mode]] +| 26:00 +|"
    (org-test-with-temp-text
	"* Foo [[https://orgmode.org][Org mode]]
CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
      (test-org-clock-clocktable-contents ":link t :lang en"))))
  (should
   (string-match-p
    "| \\[\\[\\*Foo \\\\\\[\\\\\\[https://orgmode\\.org\\\\]\\\\]]\\[Foo https://orgmode\\.org]] +| 26:00 +|"
    (org-test-with-temp-text
	"* Foo [[https://orgmode.org]]
CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
      (test-org-clock-clocktable-contents ":link t :lang en")))))

(ert-deftest test-org-clock/clocktable/compact ()
  "Test \":compact\" parameter in Clock table."
  ;; With :compact, all headlines are in the same column.
  (should
   (equal
    "| Headline     | Time    |
|--------------+---------|
| *Total time* | *26:00* |
|--------------+---------|
| Foo          | 26:00   |"
    (org-test-with-temp-text
        "* Foo
  CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
      (test-org-clock-clocktable-contents ":compact t"))))
  (should
   (equal
    "| Headline     |    Time |
|--------------+---------|
| *Total time* | *52:00* |
|--------------+---------|
| Foo          |   52:00 |
| \\_  Bar      |   26:00 |"
    (org-test-with-temp-text
        "* Foo
CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00
** Bar
CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
      (test-org-clock-clocktable-contents ":compact t"))))
  ;; :maxlevel does not affect :compact parameter.
  (should
   (equal
    "| Headline     |    Time |
|--------------+---------|
| *Total time* | *52:00* |
|--------------+---------|
| Foo          |   52:00 |
| \\_  Bar      |   26:00 |"
    (org-test-with-temp-text
        "* Foo
CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00
** Bar
CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
      (test-org-clock-clocktable-contents ":compact t :maxlevel 2"))))
  ;; :compact implies a non-nil :indent parameter.
  (should
   (equal
    "| Headline     |    Time |
|--------------+---------|
| *Total time* | *52:00* |
|--------------+---------|
| Foo          |   52:00 |
| \\_  Bar      |   26:00 |"
    (org-test-with-temp-text
        "* Foo
CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00
** Bar
CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
      (test-org-clock-clocktable-contents ":compact t :indent nil"))))
  ;; :compact implies a nil :level parameter.
  (should
   (equal
    "| Headline     |    Time |
|--------------+---------|
| *Total time* | *52:00* |
|--------------+---------|
| Foo          |   52:00 |
| \\_  Bar      |   26:00 |"
    (org-test-with-temp-text
        "* Foo
CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00
** Bar
CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
      (test-org-clock-clocktable-contents ":compact t :level t")))))

(ert-deftest test-org-clock/clocktable/properties ()
  "Test \":properties\" parameter in Clock table."
  ;; Include a new column with list properties.
  (should
   (equal
    "| A | Headline     | Time    |
|---+--------------+---------|
|   | *Total time* | *26:00* |
|---+--------------+---------|
| 1 | Foo          | 26:00   |"
    (org-test-with-temp-text
        "* Foo
:PROPERTIES:
:A: 1
:END:
CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
      (test-org-clock-clocktable-contents ":properties (\"A\")"))))
  (should
   (equal
    "| A | Headline     | Time    |       |
|---+--------------+---------+-------|
|   | *Total time* | *52:00* |       |
|---+--------------+---------+-------|
|   | Foo          | 52:00   |       |
| 1 | \\_  Bar      |         | 26:00 |"
    (org-test-with-temp-text
        "* Foo
CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00
** Bar
:PROPERTIES:
:A: 1
:END:
CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
      (test-org-clock-clocktable-contents ":properties (\"A\")"))))
  ;; Handle missing properties.
  (should
   (equal
    "| A | Headline     | Time    |
|---+--------------+---------|
|   | *Total time* | *26:00* |
|---+--------------+---------|
| 1 | Foo          | 26:00   |"
    (org-test-with-temp-text
        "* Foo
:PROPERTIES:
:A: 1
:END:
CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
      (test-org-clock-clocktable-contents ":properties (\"A\")")))))

(ert-deftest test-org-clock/clocktable/tcolumns ()
  "Test \":tcolumns\" parameter in Clock table."
  ;; When :tcolumns is smaller than the deepest headline level, lump
  ;; lower levels in the last column.
  (should
   (equal
    "| Headline     |    Time |
|--------------+---------|
| *Total time* | *52:00* |
|--------------+---------|
| Foo          |   52:00 |
| \\_  Bar      |   26:00 |"
    (org-test-with-temp-text
        "* Foo
CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00
** Bar
CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
      (test-org-clock-clocktable-contents ":tcolumns 1"))))
  ;; :tcolumns cannot create more columns than the deepest headline
  ;; level.
  (should
   (equal
    "| Headline     | Time    |       |
|--------------+---------+-------|
| *Total time* | *52:00* |       |
|--------------+---------+-------|
| Foo          | 52:00   |       |
| \\_  Bar      |         | 26:00 |"
    (org-test-with-temp-text
        "* Foo
CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00
** Bar
CLOCK: [2016-12-27 Wed 13:09]--[2016-12-28 Wed 15:09] => 26:00"
      (test-org-clock-clocktable-contents ":tcolumns 3"))))
  ;; Pathological case: when no headline contributes to the total
  ;; time, there is only one time column.
  (should
   (equal "| Headline     | Time   |
|--------------+--------|
| *Total time* | *0:00* |"
          (org-test-with-temp-text
              "* Foo
CLOCK: [2016-12-28 Wed 11:09]--[2016-12-28 Wed 11:09] =>  0:00
** Bar
CLOCK: [2016-12-28 Wed 13:09]--[2016-12-28 Wed 13:09] =>  0:00"
            (test-org-clock-clocktable-contents ":tcolumns 2")))))

(ert-deftest test-org-clock/clocktable/step ()
  "Test \":step\" parameter in Clock table."
  ;; Regression test: week crossing month boundary before :wstart
  ;; day-of-week.
  (should
   (string-match-p
    "
.*?\\[2017-09-25 .*
.*
.*
|.*?| \\*1:00\\* |
.*
| Foo +| 1:00 +|"
    (org-test-with-temp-text
	"* Foo
CLOCK: [2017-09-30 Sat 12:00]--[2017-09-30 Sat 13:00] =>  1:00
CLOCK: [2017-10-01 Sun 11:00]--[2017-10-01 Sun 13:00] =>  2:00
CLOCK: [2017-10-02 Mon 11:00]--[2017-10-02 Mon 14:00] =>  3:00"
      (let ((system-time-locale "en_US"))
	(test-org-clock-clocktable-contents
	    ":step week :block 2017-09 :stepskip0 t")))))
  (should
   (string-match-p
    "
.*?\\[2017-10-01 .*
.*
.*
|.*?| \\*2:00\\* |
.*
| Foo +| 2:00   |

.*?\\[2017-10-02 .*
.*
.*
|.*?| \\*7:00\\* |
.*
| Foo +| 7:00 +|

.*?\\[2017-10-09 .*
.*
.*
|.*?| \\*5:00\\* |
.*
| Foo +| 5:00 +|
"
    (org-test-with-temp-text
	"* Foo
CLOCK: [2017-09-30 Sat 12:00]--[2017-09-30 Sat 13:00] =>  1:00
CLOCK: [2017-10-01 Sun 11:00]--[2017-10-01 Sun 13:00] =>  2:00
CLOCK: [2017-10-02 Mon 11:00]--[2017-10-02 Mon 14:00] =>  3:00
CLOCK: [2017-10-08 Sun 09:00]--[2017-10-08 Sun 13:00] =>  4:00
CLOCK: [2017-10-09 Mon 09:00]--[2017-10-09 Mon 14:00] =>  5:00"
      (let ((system-time-locale "en_US"))
	(test-org-clock-clocktable-contents
	    ":step week :block 2017-10 :stepskip0 t")))))
  ;; :step day
  (should
   (string-match-p
    "
.*?\\[2017-10-02 .*
.*
.*
|.*?| \\*3:00\\* |
.*
| Foo +| 3:00 +|

.*?\\[2017-10-03 .*
.*
.*
|.*?| \\*0:00\\* |

.*?\\[2017-10-04 .*
.*
.*
|.*?| \\*0:00\\* |

.*?\\[2017-10-05 .*
.*
.*
|.*?| \\*0:00\\* |

.*?\\[2017-10-06 .*
.*
.*
|.*?| \\*0:00\\* |

.*?\\[2017-10-07 .*
.*
.*
|.*?| \\*0:00\\* |

.*?\\[2017-10-08 .*
.*
.*
|.*?| \\*4:00\\* |
.*
| Foo +| 4:00 +|"
    (org-test-with-temp-text
	"* Foo
CLOCK: [2017-09-30 Sat 12:00]--[2017-09-30 Sat 13:00] =>  1:00
CLOCK: [2017-10-01 Sun 11:00]--[2017-10-01 Sun 13:00] =>  2:00
CLOCK: [2017-10-02 Mon 11:00]--[2017-10-02 Mon 14:00] =>  3:00
CLOCK: [2017-10-08 Sun 09:00]--[2017-10-08 Sun 13:00] =>  4:00
CLOCK: [2017-10-09 Mon 09:00]--[2017-10-09 Mon 14:00] =>  5:00"
      (let ((system-time-locale "en_US"))
	(test-org-clock-clocktable-contents
	    ":step day :block 2017-W40")))))
  ;; Regression test: take :tstart and :tend hours into consideration.
  (should
   (string-match-p
    "
.*?\\[2017-12-25 .*
.*
.*
|.*?| \\*8:00\\* |
.*
| Foo +| 8:00 +|"
    (org-test-with-temp-text
	"* Foo
CLOCK: [2017-12-27 Wed 08:00]--[2017-12-27 Wed 16:00] =>  8:00"
      (let ((system-time-locale "en_US"))
	(test-org-clock-clocktable-contents
	    (concat ":step week :tstart \"<2017-12-25 Mon>\" "
		    ":tend \"<2017-12-27 Wed 23:59>\""))))))
  (should
   (string-match-p
    "
.*?\\[2017-12-27 .*
.*
.*
|.*?| \\*8:00\\* |
.*
| Foo +| 8:00 +|"
    (org-test-with-temp-text
	"* Foo
CLOCK: [2017-12-27 Wed 08:00]--[2017-12-27 Wed 16:00] =>  8:00"
      (let ((system-time-locale "en_US"))
	(test-org-clock-clocktable-contents
	    (concat ":step day :tstart \"<2017-12-25 Mon>\" "
		    ":tend \"<2017-12-27 Wed 23:59>\" :stepskip0 t"))))))
  ;; Test :step week", without or with ":wstart" parameter.
  (should
   (string-match-p
    "
.*?\\[2012-03-26 .*
.*
.*
|.*?| \\*8:00\\* |
.*
| Foo +| 8:00 +|

.*?\\[2012-04-02 .*
.*
.*
|.*?| \\*8:00\\* |
.*
| Foo +| 8:00 +|
"
    (org-test-with-temp-text
        "* Foo
CLOCK: [2012-03-29 Thu 08:00]--[2012-03-29 Thu 16:00] =>  8:00
CLOCK: [2012-04-03 Thu 08:00]--[2012-04-03 Thu 16:00] =>  8:00"
      (let ((system-time-locale "en_US"))
        (test-org-clock-clocktable-contents
	    ":step week :block 2012 :stepskip0 t")))))
  (should
   (string-match-p
    "
.*?\\[2012-03-29 .*
.*
.*
|.*?| \\*16:00\\* |
.*
| Foo +| 16:00 +|
"
    (org-test-with-temp-text
        "* Foo
CLOCK: [2012-03-29 Thu 08:00]--[2012-03-29 Thu 16:00] =>  8:00
CLOCK: [2012-04-03 Thu 08:00]--[2012-04-03 Thu 16:00] =>  8:00"
      (let ((system-time-locale "en_US"))
        (test-org-clock-clocktable-contents
	    ":step week :wstart 4 :block 2012 :stepskip0 t")))))
  ;; Test ":step month" without and with ":mstart".
  (should
   (string-match-p
    "
.*?\\[2014-03-01 .*
.*
.*
|.*?| \\*8:00\\* |
.*
| Foo +| 8:00 +|

.*?\\[2014-04-01 .*
.*
.*
|.*?| \\*8:00\\* |
.*
| Foo +| 8:00 +|
"
    (org-test-with-temp-text
        "* Foo
CLOCK: [2014-03-04 Tue 08:00]--[2014-03-04 Tue 16:00] =>  8:00
CLOCK: [2014-04-03 Thu 08:00]--[2014-04-03 Thu 16:00] =>  8:00"
      (let ((system-time-locale "en_US"))
        (test-org-clock-clocktable-contents
	    ":step month :block 2014 :stepskip0 t")))))
  (should
   (string-match-p
    "
.*?\\[2014-03-04 .*
.*
.*
|.*?| \\*16:00\\* |
.*
| Foo +| 16:00 +|
"
    (org-test-with-temp-text
        "* Foo
CLOCK: [2014-03-04 Tue 08:00]--[2014-03-04 Tue 16:00] =>  8:00
CLOCK: [2014-04-03 Thu 08:00]--[2014-04-03 Thu 16:00] =>  8:00"
      (let ((system-time-locale "en_US"))
        (test-org-clock-clocktable-contents
	    ":step month :mstart 4 :block 2014 :stepskip0 t")))))
  ;; Test ":step semimonth".
  (should
   (string-match-p
    "
.*?\\[2014-03-01 .*
.*
.*
|.*?| \\*8:00\\* |
.*
| Foo +| 8:00 +|

.*?\\[2014-03-16 .*
.*
.*
|.*?| \\*2:00\\* |
.*
| Foo +| 2:00 +|

.*?\\[2014-04-01 .*
.*
.*
|.*?| \\*7:00\\* |
.*
| Foo +| 7:00 +|
"
    (org-test-with-temp-text
     "* Foo
CLOCK: [2014-03-04 Tue 08:00]--[2014-03-04 Tue 16:00] =>  8:00
CLOCK: [2014-03-24 Mon 08:00]--[2014-03-24 Mon 10:00] =>  2:00
CLOCK: [2014-04-03 Thu 08:00]--[2014-04-03 Thu 15:00] =>  7:00"
     (let ((system-time-locale "en_US"))
       (test-org-clock-clocktable-contents
	":step semimonth :block 2014 :stepskip0 t")))))
  ;; Test ":step year".
  (should
   (string-match-p
    "
.*?\\[2012-01-01 .*
.*
.*
|.*?| \\*8:00\\* |
.*
| Foo +| 8:00 +|

.*?\\[2014-01-01 .*
.*
.*
|.*?| \\*8:00\\* |
.*
| Foo +| 8:00 +|
"
    (org-test-with-temp-text
        "* Foo
CLOCK: [2012-03-29 Thu 08:00]--[2012-03-29 Thu 16:00] =>  8:00
CLOCK: [2014-03-04 Tue 08:00]--[2014-03-04 Tue 16:00] =>  8:00"
      (let ((system-time-locale "en_US"))
        (test-org-clock-clocktable-contents
	    ":step year :block untilnow :stepskip0 t")))))
  ;; Regression test: Respect DST
  (should
   (string-match-p
    "
.*?\\[2018-10-29 .*
.*
.*
|.*?| \\*8:00\\* |
.*
| Foo +| 8:00 +|
"
    (org-test-with-temp-text
        "* Foo
CLOCK: [2018-10-29 Mon 08:00]--[2018-10-29 Mon 16:00] =>  8:00"
      (let ((system-time-locale "en_US"))
        (test-org-clock-clocktable-contents
	    (concat ":step day "
		    ":stepskip0 t "
		    ":tstart \"2018-10-01\" "
		    ":tend \"2018-11-01\"")))))))

(ert-deftest test-org-clock/clocktable/extend-today-until ()
  "Test assignment of clock time to days in presence of \"org-extend-today-until\"."
  ;; Basic test of :block with org-extend-today-until - the report for
  ;; 2017-09-30 should include the time clocked on 2017-10-01 before
  ;; 04:00.
  (should
   (equal "| Headline     | Time   |
|--------------+--------|
| *Total time* | *2:00* |
|--------------+--------|
| Foo          | 2:00   |"
          (org-test-with-temp-text
              "* Foo
CLOCK: [2017-09-30 Sat 12:00]--[2017-09-30 Sat 13:00] =>  1:00
CLOCK: [2017-10-01 Sun 02:00]--[2017-10-01 Sun 03:00] =>  1:00
CLOCK: [2017-10-01 Sun 11:00]--[2017-10-01 Sun 13:00] =>  2:00"
            (setq-local org-extend-today-until 4)
            (let ((system-time-locale "en_US"))
              (test-org-clock-clocktable-contents
               ":block 2017-09-30")))))

  ;; Week-length block - time on Monday before 04:00 should be
  ;; assigned to previous week.
  (should
   (string-match-p "
.*? \\[2017-10-01 .*
.*
.*
|.*?| \\*2:00\\* |
.*
| Foo +| 2:00   |

.*? \\[2017-10-02 .*
.*
.*
|.*?| \\*2:00\\* |
.*
| Foo +| 2:00   |
"
          (org-test-with-temp-text
              "* Foo
CLOCK: [2017-10-01 Sun 12:00]--[2017-10-01 Sun 13:00] =>  1:00
CLOCK: [2017-10-02 Mon 02:00]--[2017-10-02 Mon 03:00] =>  1:00
CLOCK: [2017-10-02 Mon 11:00]--[2017-10-02 Mon 13:00] =>  2:00"
            (setq-local org-extend-today-until 4)
            (let ((system-time-locale "en_US"))
              (test-org-clock-clocktable-contents
               ":step week :block 2017-10 :stepskip0 t"))))))

(ert-deftest test-org-clock/clocktable/hidefiles ()
  "Test \":hidefiles\" parameter in Clock table."
  ;; Test that hidefiles removes the file column.
  (should
   (equal
    "| Headline     | Time   |
|--------------+--------|
| *Total time* | *1:00* |
|--------------+--------|
| Test         | 1:00   |"
    (org-test-with-temp-text-in-file
        "* Test
CLOCK: [2012-03-29 Thu 16:00]--[2012-03-29 Thu 17:00] =>  1:00"
      (let ((the-file (buffer-file-name)))
        (org-test-with-temp-text-in-file ""
          (test-org-clock-clocktable-contents
           (format ":hidefiles t :scope (lambda () (list %S))" the-file))))))))

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

debug log:

solving d2179e1ec ...
found d2179e1ec 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).