summaryrefslogtreecommitdiff
path: root/test/lisp/emacs-lisp/check-declare-tests.el
blob: 9552bf0e3979ba80ded3fdf5b0ec934237355ab3 (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
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
;;; check-declare-tests.el --- Tests for check-declare.el  -*- lexical-binding: t; -*-

;; Copyright (C) 2020-2021 Free Software Foundation, Inc.

;; Author: Simen Heggestøyl <simenheg@gmail.com>
;; Keywords:

;; This file is part of GNU Emacs.

;; GNU Emacs 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.

;; GNU Emacs 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 GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:

;;

;;; Code:

(require 'check-declare)
(require 'ert)
(eval-when-compile (require 'subr-x))

(ert-deftest check-declare-tests-locate ()
  (should (file-exists-p (check-declare-locate "check-declare" "")))
  (should
   (string-prefix-p "ext:" (check-declare-locate "ext:foo" ""))))

(ert-deftest check-declare-tests-scan ()
  (let ((file (make-temp-file "check-declare-tests-")))
    (unwind-protect
        (progn
          (with-temp-file file
            (insert
             (string-join
              '(";; foo comment"
                "(declare-function ring-insert \"ring\" (ring item))"
                "(let ((foo 'code)) foo)")
              "\n")))
          (let ((res (check-declare-scan file)))
            (should (= (length res) 1))
            (pcase-let ((`((,fnfile ,fn ,arglist ,fileonly)) res))
              (should (string-match-p "ring" fnfile))
              (should (equal "ring-insert" fn))
              (should (equal '(ring item) arglist))
              (should-not fileonly))))
      (delete-file file))))

(ert-deftest check-declare-tests-verify ()
  (let ((file (make-temp-file "check-declare-tests-")))
    (unwind-protect
        (progn
          (with-temp-file file
            (insert
             (string-join
              '(";; foo comment"
                "(defun foo-fun ())"
                "(defun ring-insert (ring item)"
                "\"Insert onto ring RING the item ITEM.\""
                "nil)")
              "\n")))
          (should-not
           (check-declare-verify
            file '(("foo.el" "ring-insert" (ring item))))))
      (delete-file file))))

(ert-deftest check-declare-tests-verify-mismatch ()
  (let ((file (make-temp-file "check-declare-tests-")))
    (unwind-protect
        (progn
          (with-temp-file file
            (insert
             (string-join
              '(";; foo comment"
                "(defun foo-fun ())"
                "(defun ring-insert (ring)"
                "\"Insert onto ring RING the item ITEM.\""
                "nil)")
              "\n")))
          (should
           (equal
            (check-declare-verify
             file '(("foo.el" "ring-insert" (ring item))))
            '(("foo.el" "ring-insert" "arglist mismatch")))))
      (delete-file file))))

(ert-deftest check-declare-tests-sort ()
  (should-not (check-declare-sort '()))
  (should (equal (check-declare-sort '((a (1 a)) (b (2)) (d (1 d))))
                 '((2 (b)) (1 (a a) (d d))))))

(ert-deftest check-declare-tests-warn ()
  (with-temp-buffer
    (let ((check-declare-warning-buffer (buffer-name)))
      (check-declare-warn
       "foo-file" "foo-fun" "bar-file" "it wasn't" 999)
      (let ((res (buffer-string)))
        ;; Don't care too much about the format of the output, but
        ;; check that key information is present.
        (should (string-match-p "foo-file" res))
        (should (string-match-p "foo-fun" res))
        (should (string-match-p "bar-file" res))
        (should (string-match-p "it wasn't" res))
        (should (string-match-p "999" res))))))

(provide 'check-declare-tests)
;;; check-declare-tests.el ends here