Skip to content

Commit

Permalink
Added more features for testing in golang layer
Browse files Browse the repository at this point in the history
  • Loading branch information
bogdanteleaga committed Oct 13, 2015
1 parent e72dd8a commit fca552e
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 23 deletions.
31 changes: 18 additions & 13 deletions layers/+lang/go/README.org
Original file line number Diff line number Diff line change
Expand Up @@ -54,23 +54,28 @@ formatter, set the value of =gofmt-command=, e.g.
#+begin_src emacs-lisp
(setq gofmt-command "goimports")
#+end_src
#
If you're using =gocheck= in your project you can use the =go-use-gocheck-for-testing= variable to enable suite testing and to get single function testing to work.

* Working with Go

** Go commands (start with =m=):
| Key Binding | Description |
|-------------+-----------------------------------------------------------|
| ~SPC m h h~ | godoc at point |
| ~SPC m i g~ | goto imports |
| ~SPC m i a~ | add import |
| ~SPC m i r~ | remove unused import |
| ~SPC m e b~ | go-play buffer |
| ~SPC m e r~ | go-play region |
| ~SPC m e d~ | download go-play snippet |
| ~SPC m t p~ | run "go test" for the current package |
| ~SPC m g a~ | jump to matching test file or back from test to code file |
| ~SPC m g g~ | go jump to definition |
| ~SPC m r n~ | go rename |
| Key Binding | Description |
|-------------+---------------------------------------------------------------------------------------|
| ~SPC m h h~ | godoc at point |
| ~SPC m i g~ | goto imports |
| ~SPC m i a~ | add import |
| ~SPC m i r~ | remove unused import |
| ~SPC m e b~ | go-play buffer |
| ~SPC m e r~ | go-play region |
| ~SPC m e d~ | download go-play snippet |
| ~SPC m g a~ | jump to matching test file or back from test to code file |
| ~SPC m g g~ | go jump to definition |
| ~SPC m r n~ | go rename |
| ~SPC m t p~ | run "go test" for the current package |
| ~SPC m t P~ | run "go test" for the current package and all packages under it |
| ~SPC m t t~ | run "go test" for the function you're currently in (while you're in a _.test.go file) |
| ~SPC m t s~ | run "go test" for the suite you're currently in (requires gocheck) |


** Go Oracle
Expand Down
3 changes: 3 additions & 0 deletions layers/+lang/go/config.el
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@
;; variables

(spacemacs|defvar-company-backends go-mode)

(defvar go-use-gocheck-for-testing nil
"If using gocheck for testing when running the tests -check.f will be used instead of -run to specify the test that will be ran. Gocheck is mandatory for testing suites.")
53 changes: 43 additions & 10 deletions layers/+lang/go/packages.el
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
(setq go-packages
'(
company
company-go
flycheck
go-mode
go-eldoc
))
'(
company
company-go
flycheck
go-mode
go-eldoc
))

(defun go/post-init-flycheck ()
(spacemacs/add-flycheck-hook 'go-mode))
Expand All @@ -20,9 +20,39 @@
(progn
(add-hook 'before-save-hook 'gofmt-before-save)

(defun spacemacs/go-run-tests (args)
(interactive)
(save-selected-window
(async-shell-command (concat "go test " args))))

(defun spacemacs/go-run-package-tests ()
(interactive)
(shell-command "go test"))
(spacemacs/go-run-tests ""))

(defun spacemacs/go-run-package-tests-nested ()
(interactive)
(spacemacs/go-run-tests "./..."))

(defun spacemacs/go-run-test-current-function ()
(interactive)
(if (string-match "_test\\.go" buffer-file-name)
(let ((test-method (if go-use-gocheck-for-testing
"-check.f"
"-run")))
(save-excursion
(re-search-backward "^func[ ]+([[:alnum:]]*?[ ]?[*]?\\([[:alnum:]]+\\))[ ]+\\(Test[[:alnum:]]+\\)(.*)")
(spacemacs/go-run-tests (concat test-method "='" (match-string-no-properties 2) "'"))))
(message "Must be in a _test.go file to run go-run-test-current-function")))

(defun spacemacs/go-run-test-current-suite ()
(interactive)
(if (string-match "_test\.go" buffer-file-name)
(if go-use-gocheck-for-testing
(save-excursion
(re-search-backward "^func[ ]+([[:alnum:]]*?[ ]?[*]?\\([[:alnum:]]+\\))[ ]+\\(Test[[:alnum:]]+\\)(.*)")
(spacemacs/go-run-tests (concat "-check.f='" (match-string-no-properties 1) "'")))
(message "Gocheck is needed to test the current suite"))
(message "Must be in a _test.go file to run go-test-current-suite")))

(evil-leader/set-key-for-mode 'go-mode
"mhh" 'godoc-at-point
Expand All @@ -34,10 +64,13 @@
"med" 'go-download-play
"mga" 'ff-find-other-file
"mgg" 'godef-jump
"mtp" 'spacemacs/go-run-package-tests))))
"mtt" 'spacemacs/go-run-test-current-function
"mts" 'spacemacs/go-run-test-current-suite
"mtp" 'spacemacs/go-run-package-tests
"mtP" 'spacemacs/go-run-package-tests-nested))))

(defun go/init-go-eldoc()
(add-hook 'go-mode-hook 'go-eldoc-setup))
(add-hook 'go-mode-hook 'go-eldoc-setup))

(when (configuration-layer/layer-usedp 'auto-completion)
(defun go/post-init-company ()
Expand Down

0 comments on commit fca552e

Please sign in to comment.