unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#70939: [PATCH] Add commands to run unit tests in go-ts-mode
@ 2024-05-14 14:04 Ankit Gadiya via Bug reports for GNU Emacs, the Swiss army knife of text editors
  2024-05-14 16:52 ` Eli Zaretskii
  2024-05-15  2:36 ` Randy Taylor
  0 siblings, 2 replies; 36+ messages in thread
From: Ankit Gadiya via Bug reports for GNU Emacs, the Swiss army knife of text editors @ 2024-05-14 14:04 UTC (permalink / raw)
  To: 70939


[-- Attachment #1.1: Type: text/plain, Size: 616 bytes --]

Hello folks,

This patch adds two new commands for go-ts-mode to run unit test cases.

The go-ts-mode-test-package command can run all the tests under the package
of
the current buffer. I've tested it to work with both Go module packages and
non-module packages. This command is bound to C-c C-p in the go-ts-mode-map.

The go-ts-mode-test-function-at-point command runs the current test
function. If
region is active then it runs all the test functions under the region. I'm
attaching a sample_test.go file as well for reviewers to test the patch.
This
command is bound to C-c C-t in the go-ts-mode-map.

-- 
Ankit

[-- Attachment #1.2: Type: text/html, Size: 688 bytes --]

[-- Attachment #2: sample_test.go --]
[-- Type: text/x-go, Size: 121 bytes --]

package sample_test

import "testing"

func TestIndividual(t *testing.T) {

}

func TestOverlapRegion(t *testing.T) {

}

[-- Attachment #3: 0001-Add-commands-to-run-unit-tests-in-go-ts-mode.patch --]
[-- Type: text/x-patch, Size: 4225 bytes --]

From 4988ef4962c8f3fde0ca0be7a1485488c7dca923 Mon Sep 17 00:00:00 2001
From: Ankit R Gadiya <git@argp.in>
Date: Tue, 14 May 2024 00:14:03 +0530
Subject: [PATCH] Add commands to run unit tests in go-ts-mode

---
 etc/NEWS                     | 10 ++++++
 lisp/progmodes/go-ts-mode.el | 63 +++++++++++++++++++++++++++++++++++-
 2 files changed, 72 insertions(+), 1 deletion(-)

diff --git a/etc/NEWS b/etc/NEWS
index 34052764f5f..b9df7fdcaf3 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -1236,6 +1236,16 @@ This command adds a docstring comment to the current defun.  If a
 comment already exists, point is only moved to the comment.  It is
 bound to 'C-c C-d' in 'go-ts-mode'.
 
+*** New unit test commands.
+Two new commands are now available to run unit tests.
+
+The 'go-ts-mode-test-function-at-point' command runs unit test at
+point. If a region is active, it runs all the unit tests under the
+region. It is bound to 'C-c C-t' in 'go-ts-mode'.
+
+The 'go-ts-mode-test-package' command runs all unit tests under the
+package of the current buffer. It is bound to 'C-c C-p' in 'go-ts-mode'.
+
 ** Man mode
 
 +++
diff --git a/lisp/progmodes/go-ts-mode.el b/lisp/progmodes/go-ts-mode.el
index aef224ab3fa..5540ddb61e6 100644
--- a/lisp/progmodes/go-ts-mode.el
+++ b/lisp/progmodes/go-ts-mode.el
@@ -237,7 +237,9 @@ go-ts-mode--font-lock-settings
 (defvar-keymap go-ts-mode-map
   :doc "Keymap used in Go mode, powered by tree-sitter"
   :parent prog-mode-map
-  "C-c C-d" #'go-ts-mode-docstring)
+  "C-c C-d" #'go-ts-mode-docstring
+  "C-c C-t" #'go-ts-mode-test-function-at-point
+  "C-c C-p" #'go-ts-mode-test-package)
 
 ;;;###autoload
 (define-derived-mode go-ts-mode prog-mode "Go"
@@ -370,6 +372,65 @@ go-ts-mode--comment-on-previous-line-p
      (<= (treesit-node-start node) point (treesit-node-end node))
      (string-equal "comment" (treesit-node-type node)))))
 
+(defun go-ts-mode--get-build-tags-flag ()
+  "Return compile flag for build tags.
+This function respects `go-build-tags' buffer-local variable for
+specifying build tags."
+  (if (local-variable-p 'go-build-tags)
+      (format "-tags %s" go-build-tags)
+    ""))
+
+(defun go-ts-mode--compile-test (regexp)
+  "Compiles the tests matching REGEXP.
+This function respects `go-build-tags' buffer-local variable for
+specifying build tags."
+  (compile (format "go test -v %s -run '%s'"
+                   (go-ts-mode--get-build-tags-flag)
+                   regexp)))
+
+(defun go-ts-mode--find-defun-at (start)
+  "Return the first defun node from START."
+  (let ((thing (or treesit-defun-type-regexp 'defun)))
+    (or (treesit-thing-at start thing)
+        (treesit-thing-next start thing))))
+
+(defun go-ts-mode--get-functions-in-range (start end)
+  "Return a list with names of all defuns in the range."
+  (let* ((node (go-ts-mode--find-defun-at start))
+	 (name (treesit-defun-name node))
+         (node-start (treesit-node-start node))
+         (node-end (treesit-node-end node)))
+    (if (or (not node)
+            (> start node-end)
+	    (< end node-start))
+	nil
+      (cons name (go-ts-mode--get-functions-in-range (treesit-node-end node) end)))))
+
+(defun go-ts-mode--get-test-regexp-at-point ()
+  "Return a regular expression for tests at point.
+If region is active, the regexp will include all the functions under the
+region."
+  (if (region-active-p)
+      (string-join (go-ts-mode--get-functions-in-range (region-beginning)
+                                                       (region-end))
+                   "|")
+    (treesit-defun-name (treesit-defun-at-point))))
+
+(defun go-ts-mode-test-function-at-point ()
+  "Run the unit test at point.
+If the point is anywhere in the test function, that function will be
+tested.  If the region is selected, all the functions under the region
+will be run."
+    (interactive)
+    (go-ts-mode--compile-test (go-ts-mode--get-test-regexp-at-point)))
+
+(defun go-ts-mode-test-package ()
+  "Run all the unit tests under current package."
+  (interactive)
+  (compile (format "go test -v %s -run %s"
+                   (go-ts-mode--get-build-tags-flag)
+                   default-directory)))
+
 ;; go.mod support.
 
 (defvar go-mod-ts-mode--syntax-table
-- 
2.39.2


^ permalink raw reply related	[flat|nested] 36+ messages in thread

end of thread, other threads:[~2024-07-21  6:05 UTC | newest]

Thread overview: 36+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-05-14 14:04 bug#70939: [PATCH] Add commands to run unit tests in go-ts-mode Ankit Gadiya via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-05-14 16:52 ` Eli Zaretskii
2024-05-14 17:24   ` Ankit Gadiya via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-05-14 17:59     ` Eli Zaretskii
2024-05-15  2:36 ` Randy Taylor
2024-05-15  4:55   ` Ankit Gadiya via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-05-16  2:32     ` Randy Taylor
2024-05-16  8:27       ` Eli Zaretskii
2024-05-16 15:03         ` Ankit Gadiya via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-05-16 16:01           ` Eli Zaretskii
2024-05-18  9:54             ` Ankit Gadiya via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-05-25  2:35               ` Randy Taylor
2024-05-28  2:30                 ` Randy Taylor
2024-05-28 19:58                   ` Ankit Gadiya via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-06-19 18:17                     ` Ankit Gadiya via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-06-21  2:40                       ` Randy Taylor
2024-06-23 14:46                         ` Ankit Gadiya via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-06-26  2:26                           ` Randy Taylor
2024-06-26 11:27                             ` Eli Zaretskii
2024-06-26 12:31                               ` Randy Taylor
2024-07-06 19:44                             ` Ankit Gadiya via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-07-06 22:08                               ` Stefan Kangas
2024-07-06 22:30                                 ` Dmitry Gutov
2024-07-07  7:26                                   ` Ankit Gadiya via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-07-10 23:43                               ` Randy Taylor
2024-07-11  7:33                                 ` Ankit Gadiya via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-07-11 14:21                                   ` Randy Taylor
2024-07-12  6:23                                     ` Eli Zaretskii
2024-07-12 11:10                                       ` Ankit Gadiya via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-07-12 11:21                                         ` Eli Zaretskii
2024-07-21  6:05                                   ` Eli Zaretskii
2024-06-23 14:56                         ` Eli Zaretskii
2024-05-17  2:27       ` Randy Taylor
2024-05-18  8:55         ` Ankit Gadiya via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-05-15 11:21   ` Eli Zaretskii
2024-05-16  1:24     ` Randy Taylor

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).