0

I am refactoring some code and adding doc-tests as suggested here. The previous iteration passed the tests. Now running ERT in batch mode with

eemacs -batch -l ert -l org-clock-split.el -f ert-run-tests-batch-and-exit

returns an error referring to commands that I cannot find in my code. I would like to know the offending line. I don't have a setq value-32 in my code so I cannot find it. And if I had several occurrences, I wouldn't know which one had the bug. What's more confusing is that the same code works as I expect when I run it interactively.

How can I find offending line in doc-tests?

details

The error is:

Running 2 tests (2019-12-14 15:19:24+0000)
   passed  1/2  org-clock-split-get-timestrings-test
Test org-clock-split-split-line-test backtrace:
  signal(wrong-type-argument (sequencep 11))
  apply(signal (wrong-type-argument (sequencep 11)))
  (setq value-32 (apply fn-30 args-31))
  (unwind-protect (setq value-32 (apply fn-30 args-31)) (setq form-des
  (if (unwind-protect (setq value-32 (apply fn-30 args-31)) (setq form
  (let (form-description-34) (if (unwind-protect (setq value-32 (apply
  (let ((value-32 (quote ert-form-evaluation-aborted-33))) (let (form-
  (let* ((fn-30 (function equal)) (args-31 (condition-case err (let ((
  (closure (t) nil (let* ((fn-5 (function equal)) (args-6 (condition-c
  ert--run-test-internal(#s(ert--test-execution-info :test #s(ert-test
  ert-run-test(#s(ert-test :name org-clock-split-split-line-test :docu
  ert-run-or-rerun-test(#s(ert--stats :selector t :tests [#s(ert-test 
  ert-run-tests(t #f(compiled-function (event-type &rest event-args) #
  ert-run-tests-batch(nil)
  ert-run-tests-batch-and-exit()
  command-line-1(("-l" "ert" "-l" "org-clock-split.el" "-f" "ert-run-t
  command-line()
  normal-top-level()
Test org-clock-split-split-line-test condition:
    (wrong-type-argument sequencep 11)
   FAILED  2/2  org-clock-split-split-line-test

Ran 2 tests, 1 results as expected, 1 unexpected (2019-12-14 15:19:24+0000)

1 unexpected results:
   FAILED  org-clock-split-split-line-test

The code additions to the previous commit, which passed, are:

(defun org-clock-split-absolute-timestring-to-hours-and-minutes (splitter-string)
  "Return internal time from the timestring.

    SPLITTER-STRING - Absolute time to split record at (Ex
    '9:20') (see `org-clock-split-absolute-time-regexp`)."

  ;; TODO: consolidate this code so only the regular exression is used
  (unless (string-match "[0-9]?[0-9]:[0-9][0-9]" splitter-string)
    (error "Input must be a valid absolute time, e.g. 9:20."))

  (seq-map #'string-to-number (split-string splitter-string ":")))

(defun org-clock-split-split-line-into-timestamps (original-line splitter-string from-end)
  "Splits the clock range in ORIGINAL-LINE by SPLITTER-STRING, from the end or the start of the clock range.

   ORIGINAL-LINE: a clock range from an Org buffer, such as 'CLOCK: [2019-12-14 Sat 08:20]--[2019-12-14 Sat 08:44] =>  0:24'

   SPLITTER-STRING: either a relative duration such as 1h02m or
    an absolute time such as 09:20. If the absolute time is
    within the range in ORIGINAL-LINE, then FROM-END is
    irrelevant. If it falls outside the range, the splitting
    point will be the latest time before the end of the clock
    range if FROM-END is t, and the first time after the
    beginning of the clock range if FROM-END is nil.

   FROM-END: whether to split from the end of the clock range or the start."

  (let* ((timestring-pair (org-clock-split-get-timestrings original-line))
     (t0-string (pop timestring-pair))
     (t0-float (org-timestring-to-time t0-string))
     (t2-string (pop timestring-pair))
     (t2-float (org-timestring-to-time t2-string))
     (absolute (cl-search ":" splitter-string))
     ;; assign splitting time provisionally, will be updated in the logic
     (t1-string (if from-end t2-string t0-string))
     t1-float)
    (if absolute
    (let* ((pair (org-clock-split-absolute-timestring-to-hours-and-minutes splitter-string))
           (hours (pop pair))
           (minutes (pop pair))
           (t1-tuple (org-parse-time-string t1-string))
           (t1-tuple (append (list 0 minutes hours) (seq-subseq t1-tuple 3)))
           (t1-float (float-time (apply #'encode-time t1-tuple))))
      ;; update the splitting time so it's later than t0 or earlier than t2, depending on FROM-END
      (setq temp t1-tuple)
      (if from-end
          (when (> t1-float t2-float)
        (setq t1-tuple (append (seq-subseq t1-tuple 0 3) (1+ (nth 4 t1-tuple)) (seq-subseq t1-tuple 5))))
        (when (< t1-float t0-float)
          (setq t1-tuple (append (seq-subseq t1-tuple 0 3) (1- (nth 4 t1-tuple)) (seq-subseq t1-tuple 5)))))
      (setq temp2 t1-tuple)

      ;; convert to float
      (setq t1-float (apply #'encode-time t1-tuple))
      (setq t1-string (format-time-string org-clock-split-inactive-timestamp-hm t1-float)))


      ;; Handle relative duration
      (let* ((parsed-seconds (org-clock-split-relative-string-to-seconds splitter-string))
         (t1-float (org-timestring-to-time t1-string))
         (t1-float (if from-end
           (- t1-float parsed-seconds)
         (+ t1-float parsed-seconds))))
    (setq t1-string (format-time-string org-clock-split-inactive-timestamp-hm t1-float))))
    (list t0-string t1-string t2-string)))

(ert-deftest org-clock-split-split-line-test ()
  (should (equal
       (org-clock-split-split-line-into-timestamps "CLOCK: [2019-12-14 Sat 08:20]--[2019-12-14 Sat 08:44] =>  0:24" "4m" t)
       '("[2019-12-14 Sat 08:20]" "[2019-12-14 Sat 08:40]" "[2019-12-14 Sat 08:44]")))
  (should (equal
       (org-clock-split-split-line-into-timestamps "CLOCK: [2019-12-14 Sat 08:20]--[2019-12-14 Sat 08:44] =>  0:24" "20m" nil)
       '("[2019-12-14 Sat 08:20]" "[2019-12-14 Sat 08:40]" "[2019-12-14 Sat 08:44]")))
  (should (equal
       (org-clock-split-split-line-into-timestamps "CLOCK: [2019-12-14 Sat 08:20]--[2019-12-14 Sat 08:44] =>  0:24" "08:40" t)
       '("[2019-12-14 Sat 08:20]" "[2019-12-14 Sat 08:40]" "[2019-12-14 Sat 08:44]")))
  (should (equal
       (org-clock-split-split-line-into-timestamps "CLOCK: [2019-12-14 Sat 08:20]--[2019-12-14 Sat 08:44] =>  0:24" "08:40" t)
       '("[2019-12-14 Sat 08:20]" "[2019-12-14 Sat 08:40]" "[2019-12-14 Sat 08:44]")))
  (should (equal
       (org-clock-split-split-line-into-timestamps "CLOCK: [2019-12-14 Sat 08:20]--[2019-12-14 Sat 08:44] =>  0:24" "07:00" t)
       '("[2019-12-14 Sat 08:20]" "[2019-12-14 Sat 07:00]" "[2019-12-14 Sat 08:44]")))
  (should (equal
       (org-clock-split-split-line-into-timestamps "CLOCK: [2019-12-14 Sat 08:20]--[2019-12-14 Sat 08:44] =>  0:24" "07:00" nil)
       '("[2019-12-14 Sat 08:20]" "[2019-12-15 Sun 07:00]" "[2019-12-14 Sat 08:44]")))
  )

The full code is on Github, commit eed7f.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.