blob: 6947f358b85b6f261c9f2682b800b2174ef239da (
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
;;; test-prog-go-commands.el --- Tests for prog-go setup + commands -*- lexical-binding: t; -*-
;;; Commentary:
;; Covers the four functions in prog-go.el:
;;
;; cj/go-setup
;; cj/go-staticcheck
;; cj/go-debug
;; cj/go-mode-keybindings
;;
;; External modes and process primitives are stubbed.
;;; Code:
(require 'ert)
(require 'cl-lib)
(add-to-list 'load-path (expand-file-name "modules" user-emacs-directory))
(add-to-list 'load-path (expand-file-name "tests" user-emacs-directory))
(require 'testutil-format-wiring)
(format-test--ensure-packages-init)
(require 'prog-go)
;;; cj/go-setup
(ert-deftest test-prog-go-setup-sets-buffer-local-preferences ()
"Normal: tab-width 4, standard-indent 4, indent-tabs-mode t (Go convention)."
(with-temp-buffer
(cl-letf (((symbol-function 'company-mode) #'ignore)
((symbol-function 'electric-pair-mode) #'ignore))
(cj/go-setup)
(should (= tab-width 4))
(should (= standard-indent 4))
(should indent-tabs-mode))))
(ert-deftest test-prog-go-setup-enables-mode-helpers ()
"Normal: company-mode and electric-pair-mode are both called."
(with-temp-buffer
(let ((called nil))
(cl-letf (((symbol-function 'company-mode)
(lambda (&rest _) (push 'company called)))
((symbol-function 'electric-pair-mode)
(lambda (arg) (push (cons 'pair arg) called))))
(cj/go-setup))
(should (memq 'company called))
(should (assq 'pair called)))))
(ert-deftest test-prog-go-setup-starts-lsp-when-gopls-on-path ()
"Normal: with gopls on PATH, `lsp-deferred' is called."
(with-temp-buffer
(let ((started nil))
(cl-letf (((symbol-function 'company-mode) #'ignore)
((symbol-function 'electric-pair-mode) #'ignore)
((symbol-function 'lsp-deferred)
(lambda (&rest _) (setq started t)))
((symbol-function 'executable-find)
(lambda (path) (when (equal path gopls-path) "/usr/bin/gopls"))))
(cj/go-setup))
(should started))))
(ert-deftest test-prog-go-setup-skips-lsp-without-gopls ()
"Boundary: without gopls on PATH, `lsp-deferred' is NOT called."
(with-temp-buffer
(let ((started nil))
(cl-letf (((symbol-function 'company-mode) #'ignore)
((symbol-function 'electric-pair-mode) #'ignore)
((symbol-function 'lsp-deferred)
(lambda (&rest _) (setq started t)))
((symbol-function 'executable-find) (lambda (_) nil)))
(cj/go-setup))
(should-not started))))
;;; cj/go-staticcheck
(ert-deftest test-prog-go-staticcheck-runs-compile-when-binary-present ()
"Normal: with staticcheck on disk, `compile' is called with `./...'."
(let (compiled)
(cl-letf (((symbol-function 'file-executable-p) (lambda (_) t))
((symbol-function 'compile)
(lambda (cmd &rest _) (setq compiled cmd))))
(with-temp-buffer
(cj/go-staticcheck)))
(should (stringp compiled))
(should (string-match-p "./\\.\\.\\." compiled))))
(ert-deftest test-prog-go-staticcheck-messages-when-missing ()
"Error: missing staticcheck -> message + no compile."
(let ((compiled nil)
(msg nil))
(cl-letf (((symbol-function 'file-executable-p) (lambda (_) nil))
((symbol-function 'compile)
(lambda (&rest _) (setq compiled t)))
((symbol-function 'message)
(lambda (fmt &rest args)
(setq msg (apply #'format fmt args)))))
(with-temp-buffer
(cj/go-staticcheck)))
(should-not compiled)
(should (string-match-p "staticcheck not found" msg))))
;;; cj/go-debug
(ert-deftest test-prog-go-debug-runs-gud-gdb-when-delve-on-path ()
"Normal: with delve on PATH, `gud-gdb' is called with `dlv debug'."
(let (started)
(cl-letf (((symbol-function 'executable-find)
(lambda (path) (when (equal path dlv-path) "/usr/bin/dlv")))
((symbol-function 'file-executable-p) (lambda (_) nil))
((symbol-function 'gud-gdb)
(lambda (cmd &rest _) (setq started cmd))))
(with-temp-buffer
(cj/go-debug)))
(should (stringp started))
(should (string-match-p "dlv debug" started))))
(ert-deftest test-prog-go-debug-messages-when-delve-missing ()
"Error: delve missing -> message + no gud-gdb call."
(let ((started nil)
(msg nil))
(cl-letf (((symbol-function 'executable-find) (lambda (_) nil))
((symbol-function 'file-executable-p) (lambda (_) nil))
((symbol-function 'gud-gdb)
(lambda (&rest _) (setq started t)))
((symbol-function 'message)
(lambda (fmt &rest args)
(setq msg (apply #'format fmt args)))))
(with-temp-buffer
(cj/go-debug)))
(should-not started)
(should (string-match-p "Delve not found" msg))))
;;; cj/go-mode-keybindings
(ert-deftest test-prog-go-mode-keybindings-binds-the-keys ()
"Normal: C-; f -> gofmt, S-<f5> -> staticcheck, S-<f6> -> debug."
(with-temp-buffer
(use-local-map (make-sparse-keymap))
(cj/go-mode-keybindings)
(should (eq #'gofmt (local-key-binding (kbd "C-; f"))))
(should (eq #'cj/go-staticcheck (local-key-binding (kbd "S-<f5>"))))
(should (eq #'cj/go-debug (local-key-binding (kbd "S-<f6>"))))))
(provide 'test-prog-go-commands)
;;; test-prog-go-commands.el ends here
|