org-mode/testing/lisp/test-org-duration.el

173 lines
6.7 KiB
EmacsLisp
Raw Normal View History

;;; test-org-duration.el --- Tests for org-duration.el -*- lexical-binding: t; -*-
2019-01-01 05:50:56 -05:00
;; Copyright (C) 2017, 2019 Nicolas Goaziou
;; Author: Nicolas Goaziou <mail@nicolasgoaziou.fr>
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Code:
testing: Make all files use `lexical-binding` Mainly, add the corresponding cookie, but also add various `require`s so that the compiler knows which vars should be trated as dynbound. This does not fix all the warnings, but does try to eliminate all those about "unused" variables. For the variables truly unused, the patch usually adds an underscore to their name to silence the warning. Some of the fixes affect files which already used `lexical-binding`. Not sure why the test worked before: maybe because the tests were run without compiling them first (which could cause some of the missing `require`d packages to be autoloaded before we got to the problematic code, thus hiding the problem)? I found some suspicious code, for which I added FIXMEs. There are also a few changes to the main files. * lisp/org-protocol.el (org-protocol-check-filename-for-protocol): Don't call `server-edit` if it's not yet defined. [ Needed to get the tests to pass. ] * lisp/ob-core.el (org-babel-temporary-directory) (org-babel-temporary-stable-directory): Always define (and use nil if we don't want to create a directory for it). Simplify the code based on the fact that (defvar V E) only evaluates E if V is not yet `boundp`. (org-babel-temp-file, org-babel-temp-stable-file) (org-babel-remove-temporary-directory) (org-babel-remove-temporary-stable-directory): Adjust accordingly. * lisp/org.el (org-log-beginning): Add FIXME. * testing/org-test.el: Require `org` and `org-id`. (org-id-locations-file): Don't `defconst` it. (org-test-at-id, org-test-in-example-file, org-test-at-marker) (org-test-with-temp-text, org-test-with-temp-text-in-file): Move edebug specs into `declare` (and simplify them). (org-test-with-tramp-remote-dir--worker): Declare dynbound tramp vars. (org--compile-when): Fix quoting of `exp`. (org-test-load): Tweak regexps. * testing/org-batch-test-init.el: Tweak regexp, remove dead code and add a FIXME about it. * testing/lisp/test-ox.el: Require `ox` instead of erroring out if it's not already loaded. Also require `org-inlinetask`. (org-test-with-parsed-data): Silence warnings when `info` is not used. (test-org-export/bind-keyword): Add FIXME. * testing/lisp/test-ox-publish.el: Require `org-test` and `ox-publish`. (test-org-publish/resolve-external-link): Expose lambdas to the compiler. Remove unused var `ids`. (test-org-publish/get-project-from-filename): Remove unused var `file`. * testing/lisp/test-org.el: Require `org-macs`, `org`, `org-inlinetask`, `org-refile`, and `org-agenda`. (test-org/org-read-date): Declare `org-time-was-given` as dynbound. (test-org/set-regexps-and-options): Add FIXME. * testing/lisp/test-org-timer.el: Require `org-timer`. * testing/lisp/test-org-table.el: Require `ox`. * testing/lisp/test-org-protocol.el: Require `org-protocol` instead of erroring out if it's not already loaded. Also require `capture`, and add missing `provide` statement. * testing/lisp/test-org-pcomplete.el: Require `org`. * testing/lisp/test-org-list.el: Require `org-list` and `org`. * testing/lisp/test-org-lint.el: Require `org-footnote` and `org-lint`. * testing/lisp/test-org-footnote.el: Require `org-footnote`. * testing/lisp/test-org-element.el: Require `org-element` instead of erroring out if it's not already loaded. Also require `org` and `org-inlinetask`. * testing/lisp/test-org-duration.el: Require `org-duration`. * testing/lisp/test-org-datetree.el: Require `org-datetree`. * testing/lisp/test-org-colview.el: Require `org-colview`, `org-duration`, and `org-inlinetask`. * testing/lisp/test-org-clock.el: Require `org-duration` and `org-clock`. * testing/lisp/test-org-archive.el: Require `org-archive`. * testing/lisp/test-org-agenda.el (test-org-agenda/bulk-custom-arg-func): Add FIXME. * testing/lisp/test-ol.el: Require `ol` and `org-id`. (test-org-link/store-link): Declare `org-store-link-props` and add FIXME. * testing/lisp/test-oc.el (test-org-cite/export-capability): Add FIXME. * testing/lisp/test-ob.el: Require `ob-core`, `org-src`, `ob-ref`, and `org-table`. (test-ob/eval-header-argument): Rename `foo` to `test-ob--foo` and declare it as dynbound. (test-ob/blocks-with-spaces, test-ob/specific-colnames): Add FIXME. (test-ob/noweb-expansions-in-cache): Declare `noweb-expansions-in-cache-var` as dynbound. * testing/lisp/test-ob-tangle.el: Require `org` and `ob-tangle`. * testing/lisp/test-ob-shell.el: * testing/lisp/test-ob-python.el: Require `ob-core`. * testing/lisp/test-ob-lob.el: Require `ob-lob`. (temporary-value-for-test): Declare as dynbound. * testing/lisp/test-ob-plantuml.el: Require `ob-plantuml` instead of erroring out if it's not already loaded. * testing/lisp/test-ob-lilypond.el: Require `ob-lilypond` instead of erroring out if it's not already loaded. Use `with-current-buffer`. * testing/lisp/test-ob-julia.el: Require `ob-core`. * testing/lisp/test-ob-java.el (org-babel-temporary-directory): Remove dead code now that `org-babel-temporary-directory` is always bound. * testing/lisp/test-ob-exp.el: Require `ob-exp`, `org-src`, and `org-test`. (ob-exp/evaluate-all-executables-in-order): Declare `*evaluation-collector*` as dynbound. * testing/lisp/test-ob-emacs-lisp.el (ob-emacs-lisp/dynamic-lexical-edit) (ob-emacs-lisp/dynamic-lexical-execute): Rename dynbound var to `ob-emacs--x` and declare it as such. * testing/lisp/test-ob-R.el: Require `ob-core`. (ess-ask-for-ess-directory, ess-history-file): Declare vars.
2022-09-14 17:21:37 -04:00
(require 'org-duration)
(ert-deftest test-org-duration/to-minutes ()
"Test `org-duration-to-minutes' specifications."
;; Raise an error for unknown duration format.
(should-error (org-duration-to-minutes "1:2"))
;; Return number of minutes, as a float.
(should (= (org-duration-to-minutes "1:01") 61))
(should (floatp (org-duration-to-minutes "1:01")))
;; Handle various duration formats.
(should (= (org-duration-to-minutes "1:20:30") 80.5))
(should (= (org-duration-to-minutes "2h 10min") 130))
(should (= (org-duration-to-minutes "1d 1:02") 1502))
(should (= (org-duration-to-minutes "2.5h") 150))
;; Special case: a bare number is treated as minutes.
(should (= (org-duration-to-minutes "2") 2))
(should (= (org-duration-to-minutes "2.5") 2.5))
(should (= (org-duration-to-minutes 1) 1))
;; Special case: the empty string is 0.0.
(should (= (org-duration-to-minutes "") 0.0))
;; Support custom units.
(should (= 4
(let ((org-duration-units '(("longmin" . 2)))
org-duration--unit-re
org-duration--full-re
org-duration--mixed-re)
(org-duration-set-regexps)
(org-duration-to-minutes "2longmin"))))
(should (= 61
(let ((org-duration-units '(("h" . 61)))
org-duration--unit-re
org-duration--full-re
org-duration--mixed-re)
(org-duration-set-regexps)
(org-duration-to-minutes "1h"))))
;; When CANONICAL is non-nil, ignore custom units and only recognize
;; units defined in `org-duration-canonical-units'.
(should (= 60
(let ((org-duration-units '(("h" . 61)))
org-duration--unit-re
org-duration--full-re
org-duration--mixed-re)
(org-duration-set-regexps)
(org-duration-to-minutes "1h" t))))
(should-error (let ((org-duration-units '(("longmin" . 2)))
org-duration--unit-re
org-duration--full-re
org-duration--mixed-re)
(org-duration-set-regexps)
(org-duration-to-minutes "2longmin" t))))
(ert-deftest test-org-duration/from-minutes ()
"Test `org-duration-from-minutes' specifications."
;; Format number of minutes according to `org-duration-format'.
(should (equal "1:00"
(let ((org-duration-format 'h:mm))
(org-duration-from-minutes 60))))
(should (equal "1:01:30"
(let ((org-duration-format 'h:mm:ss))
(org-duration-from-minutes 61.5))))
(should (equal "1:01"
(let ((org-duration-format 'h:mm))
(org-duration-from-minutes 61.5))))
;; Handle required parameter in advanced format specifications.
(should (equal "1h"
(let ((org-duration-format '(("h" . nil) ("min" . nil))))
(org-duration-from-minutes 60))))
(should (equal "1h 0min"
(let ((org-duration-format '(("h" . nil) ("min" . t))))
(org-duration-from-minutes 60))))
(should (equal "50min"
(let ((org-duration-format '(("h" . nil) ("min" . nil))))
(org-duration-from-minutes 50))))
(should (equal "0h 50min"
(let ((org-duration-format '(("h" . t) ("min" . t))))
(org-duration-from-minutes 50))))
;; Handle mixed mode.
(should (equal "1d 0:10"
(let ((org-duration-format '(("d" . nil) (special . h:mm))))
(org-duration-from-minutes (+ (* 24 60) 10)))))
(should (equal "1d 0:12:30"
(let ((org-duration-format '(("d" . nil) (special . h:mm:ss))))
(org-duration-from-minutes (+ (* 24 60) 12.5)))))
;; Handle fractional duration. Parameter is the precision.
(should (equal "1.5h"
(let ((org-duration-format '(("h" . nil) (special . 1))))
(org-duration-from-minutes 90))))
(should (equal "1.50h"
(let ((org-duration-format '(("h" . nil) (special . 2))))
(org-duration-from-minutes 90))))
;; When using fractional duration, use first required unit or the
;; first with a non-zero integer part. If none is found, refer to
;; smallest unit specified in format.
(should (equal "0.7h"
(let ((org-duration-format
'(("h" . t) ("min" . nil) (special . 1))))
(org-duration-from-minutes 40))))
(should (equal "40.0min"
(let ((org-duration-format
'(("h" . nil) ("min" . nil) (special . 1))))
(org-duration-from-minutes 40))))
(should (equal "0.5min"
(let ((org-duration-format
'(("h" . nil) ("min" . nil) (special . 1))))
(org-duration-from-minutes 0.5))))
;; Handle compact form.
(should (equal "0h50min"
(let ((org-duration-format '(("h" . t) ("min" . t) compact)))
(org-duration-from-minutes 50))))
(should (equal "1d0:10"
(let ((org-duration-format
'(("d" . nil) (special . h:mm) compact)))
(org-duration-from-minutes (+ (* 24 60) 10))))))
(ert-deftest test-org-duration/p ()
"Test `org-duration-p' specifications."
;; Test all duration formats.
(should (org-duration-p "3:12"))
(should (org-duration-p "123:12"))
(should (org-duration-p "1:23:45"))
(should (org-duration-p "3d 3h 4min"))
(should (org-duration-p "3d3h4min"))
(should (org-duration-p "3d 13:35"))
(should (org-duration-p "3d13:35"))
(should (org-duration-p "2.35h"))
;; Handle custom units, but return nil for unknown units.
(should-not (org-duration-p "1minute"))
(should (let ((org-duration-units '(("minute" . 1)))
org-duration--unit-re
org-duration--full-re
org-duration--mixed-re)
(org-duration-set-regexps)
(org-duration-p "2minute")))
;; Tolerate white space between the number and the unit.
(should (org-duration-p "2 h"))
;; Return nil for ill-formed H:MM:SS strings.
(should-not (org-duration-p "3::12"))
(should-not (org-duration-p "3:2"))
(should-not (org-duration-p "3:12:4"))
;; Return nil in mixed mode if H:MM:SS part is not the last one.
(should-not (org-duration-p "3d 13:35 13h")))
(ert-deftest test-org-duration/h:mm-only-p ()
"Test `org-duration-h:mm-only-p' specifications."
(should (org-duration-h:mm-only-p '("123:31" "1:00")))
(should-not (org-duration-h:mm-only-p '("123:32" "1h")))
(should (eq 'h:mm:ss (org-duration-h:mm-only-p '("3:33" "1:23:45")))))
(provide 'test-org-duration)
;;; test-org-duration.el ends here