我喜欢 doc-tests 的 Python 特性,用于独立测试功能。Emacs Lisp 是否有类似的东西,或者我可以以某种方式模仿它吗?
例如,此函数从 Org 模式时钟段获取时间戳:
(defun org-get-timestamps (line)
"Parses a clock segment line and returns the first and last timestamps in a list."
(let* ((org-clock-regexp (concat "CLOCK: " org-ts-regexp3 "--" org-ts-regexp3))
(t1 (if (string-match org-clock-regexp line)
(match-string 1 line)
(user-error "The argument must have a valid CLOCK range")))
(t2 (match-string 9 line)))
(cons t1 (cons t2 '()))))
我想要一个文档测试,例如:
(org-get-timestamps "CLOCK: [2019-09-26 Thu 00:29]--[2019-09-26 Thu 01:11] => 0:42") ("2019-09-26 Thu 00: 29" "2019-09-26 周四 01:11")
对 的测试user-error
也很好。
我还想确保任何重构都通过了 doc-test,所以它也是一个回归测试。
那存在吗?
Python doctest 的一个重要特性是它的输入看起来像一个 Python 交互式 REPL 会话,如doctest 文档中所述:
doctest 模块搜索看起来像交互式 Python 会话的文本片段,然后执行这些会话以验证它们是否完全按照所示工作。
我不知道有任何与此完全相同的 elisp 设施,但我认为您可以使用Emacs Lisp 回归测试 (ERT) 框架实现您想要的功能,该框架支持交互式和批处理测试执行。
要测试该org-get-timestamps
功能,您可以像这样定义一个测试:
(require 'ert)
(ert-deftest org-timestamp-test ()
(should (equal
(org-get-timestamps "CLOCK: [2019-09-26 Thu 00:29]--[2019-09-26 Thu 01:11] => 0:42")
'("2019-09-26 Thu 00:29" "2019-09-26 Thu 01:11"))))
要以交互方式运行测试,您可以键入M-x ert,按回车键,然后再次按回车键选择使用默认t
参数的所有测试,或键入要运行的测试名称并按回车键,测试结果将显示在*ert*
缓冲区中:
Selector: org-timestamp-test
Passed: 1
Failed: 0
Skipped: 0
Total: 1/1
Started at: 2019-09-27 08:44:57-0400
Finished.
Finished at: 2019-09-27 08:44:57-0400
.
上面最后的点字符代表运行的测试。如果执行多个测试,就会有多个点。
要以批处理模式运行测试,请将其保存到 fileorg-timestamp-test.el
并假设org-get-timestamps
函数驻留在 file 中org-timestamps.el
,从您的 shell 命令行像这样运行它:
emacs -batch -l ert -l org-timestamps.el -l org-timestamp-test.el -f ert-run-tests-batch-and-exit
然后在 shell 输出中显示测试结果:
运行 1 个测试 (2019-09-27 06:03:09-0700) 通过 1/1 org-timestamp-test
按预期运行 1 个测试,1 个结果 (2019-09-27 06:03:09-0700)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句