aboutsummaryrefslogtreecommitdiff
path: root/tests/test-config-utilities--benchmark-method.el
blob: aefc38ba8a94e5e8a21c8ac9171d6aef46ce8acf (plain)
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
;;; test-config-utilities--benchmark-method.el --- Tests for cj/--benchmark-method -*- lexical-binding: t; -*-

;;; Commentary:
;; Tests for `cj/--benchmark-method'.  The internal takes a title
;; string and a method symbol, validates fboundp, and runs the
;; symbol's funcall through `with-timer'.  Returns whatever the
;; funcall returns; signals `user-error' on invalid input.

;;; Code:

(require 'ert)
(require 'cl-lib)

(add-to-list 'load-path (expand-file-name "modules" user-emacs-directory))
(require 'config-utilities)

(ert-deftest test-config-utilities-benchmark-method-runs-valid-symbol ()
  "Normal: a valid fboundp symbol is called once and its return value
is propagated through the timer wrapper."
  (let ((called 0)
        (inhibit-message t))
    (defalias 'test-cu-benchmark-target (lambda () (cl-incf called) 42))
    (unwind-protect
        (should (= 42 (cj/--benchmark-method "x" 'test-cu-benchmark-target)))
      (fmakunbound 'test-cu-benchmark-target))
    (should (= 1 called))))

(ert-deftest test-config-utilities-benchmark-method-nil-symbol-raises ()
  "Error: a nil method symbol raises `user-error'."
  (let ((inhibit-message t))
    (should-error (cj/--benchmark-method "x" nil) :type 'user-error)))

(ert-deftest test-config-utilities-benchmark-method-unbound-symbol-raises ()
  "Error: a symbol that is not fboundp raises `user-error' naming the symbol."
  (let* ((sym (make-symbol "test-cu-not-bound"))
         (err (let ((inhibit-message t))
                (should-error (cj/--benchmark-method "x" sym)
                              :type 'user-error))))
    (should (string-match-p (symbol-name sym)
                            (error-message-string err)))))

(ert-deftest test-config-utilities-benchmark-method-emits-timer-messages ()
  "Boundary: the announce and done messages from with-timer fire."
  (let (messages)
    (defalias 'test-cu-benchmark-noisy (lambda () nil))
    (unwind-protect
        (cl-letf (((symbol-function 'message)
                   (lambda (fmt &rest args)
                     (push (apply #'format fmt args) messages))))
          (cj/--benchmark-method "ZZZ" 'test-cu-benchmark-noisy))
      (fmakunbound 'test-cu-benchmark-noisy))
    (let ((all (string-join (nreverse messages) "\n")))
      (should (string-match-p "^ZZZ\\.\\.\\.$" all))
      (should (string-match-p "^ZZZ\\.\\.\\. done (" all)))))

(provide 'test-config-utilities--benchmark-method)
;;; test-config-utilities--benchmark-method.el ends here