-
Notifications
You must be signed in to change notification settings - Fork 3
/
gotests.el
52 lines (42 loc) · 1.64 KB
/
gotests.el
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
;;; gotests.el --- Emacs package for https://github.com/cweill/gotests
;; Copyright (C) 2016 Damien Levin
;; Author: Damien Levin
;; Keywords: go gotests languages
;; Version: 0.0.2
;; URL: https://github.com/damienlevin/GoTests-Emacs/blob/master/gotests.el
;; Apache License (version 2.0).
;;; Code:
(defconst func-regexp "\\_<\\(func\\s-([^\)]*\)\s\\(\\w*\\)\\|func\\s-\\(\\w*\\)\\)\\_>")
;; Return all go functions found in the selected region.
(defun go-functions (string)
(save-match-data
(let ((pos 0) functions)
(while (string-match func-regexp string pos)
(let ((f (match-string 2 string))
(g (match-string 3 string)))
(cond
((not(null f))(push f functions))
((not(null g))(push g functions))))
(setq pos (match-end 0)))
functions)))
;; Execute `gen` if we are not in a test file.
(defun gen-if-not-test(gen)
(if (string-match "_test.go\\'" buffer-file-name)
(message "Cannot generate gotests from test file.")
(funcall gen)))
;; Generate all missing go tests.
(defun gotests()
(interactive)
(gen-if-not-test
(lambda() (progn
(call-process "gotests" nil nil nil "-all" "-w" buffer-file-name)
(message "Generated all of test codes.")))))
;; Generate all missing go tests in region.
(defun gotests-region()
(interactive)
(gen-if-not-test
(lambda() (progn
(call-process "gotests" nil nil nil "-w" "-only" (mapconcat 'identity (go-functions (buffer-substring (region-beginning) (region-end))) "|") buffer-file-name)
(message "Generated a test code. See *_test.go file.")))))
(provide 'gotests)
;;; gotests.el ends here