summaryrefslogtreecommitdiff
path: root/test/lisp/progmodes/ruby-ts-mode-tests.el
diff options
context:
space:
mode:
authorPerry Smith <pedz@easesoftware.com>2023-01-02 02:57:38 +0200
committerDmitry Gutov <dgutov@yandex.ru>2023-01-02 02:57:53 +0200
commit0562006da3b6f0147069a9aea75c08a9a0a4e6d8 (patch)
tree83d359d52d0ff25fd3cdaeb1a7f5981a2318234e /test/lisp/progmodes/ruby-ts-mode-tests.el
parent84e7c2fbc85e5b7fbc5f9c225c2d1ba4cba2689b (diff)
downloademacs-0562006da3b6f0147069a9aea75c08a9a0a4e6d8.tar.gz
emacs-0562006da3b6f0147069a9aea75c08a9a0a4e6d8.tar.bz2
emacs-0562006da3b6f0147069a9aea75c08a9a0a4e6d8.zip
Add ruby-ts-mode
* etc/NEWS: Mention the new mode. * lisp/progmodes/ruby-ts-mode.el: New file. * test/lisp/progmodes/ruby-ts-mode-tests.el: New file. * lisp/progmodes/eglot.el (eglot-server-programs): Add ruby-ts-mode to the Ruby entry. Co-authored-by: Dmitry Gutov <dgutov@yandex.ru>
Diffstat (limited to 'test/lisp/progmodes/ruby-ts-mode-tests.el')
-rw-r--r--test/lisp/progmodes/ruby-ts-mode-tests.el254
1 files changed, 254 insertions, 0 deletions
diff --git a/test/lisp/progmodes/ruby-ts-mode-tests.el b/test/lisp/progmodes/ruby-ts-mode-tests.el
new file mode 100644
index 00000000000..f48d0bf6330
--- /dev/null
+++ b/test/lisp/progmodes/ruby-ts-mode-tests.el
@@ -0,0 +1,254 @@
+;;; ruby-mode-tests.el --- Test suite for ruby-mode -*- lexical-binding:t -*-
+
+;; Copyright (C) 2023 Free Software Foundation, Inc.
+
+;; 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 'ert)
+(require 'ert-x)
+(require 'ruby-ts-mode)
+
+(defmacro ruby-ts-with-temp-buffer (contents &rest body)
+ (declare (indent 1) (debug t))
+ `(with-temp-buffer
+ (insert ,contents)
+ (ruby-ts-mode)
+ ,@body))
+
+(defun ruby-ts-should-indent-buffer (expected content)
+ "Assert that CONTENT turns into EXPECTED after the buffer is re-indented.
+
+The whitespace before and including \"|\" on each line is removed."
+ (ruby-ts-with-temp-buffer (ruby-ts-test-string content)
+ (indent-region (point-min) (point-max))
+ (should (string= (ruby-ts-test-string expected) (buffer-string)))))
+
+(defun ruby-ts-test-string (s &rest args)
+ (apply 'format (replace-regexp-in-string "^[ \t]*|" "" s) args))
+
+(ert-deftest ruby-ts-indent-simple ()
+ (skip-unless (treesit-available-p))
+ (ruby-ts-should-indent-buffer
+ "if foo
+ | bar
+ |end
+ |zot
+ |"
+ "if foo
+ |bar
+ | end
+ | zot
+ |"))
+
+(ert-deftest ruby-ts-align-to-stmt-keywords-t ()
+ (skip-unless (treesit-available-p))
+ (let ((ruby-align-to-stmt-keywords t))
+ (ruby-ts-should-indent-buffer
+ "foo = if bar?
+ | 1
+ |else
+ | 2
+ |end
+ |
+ |foo || begin
+ | bar
+ |end
+ |
+ |foo ||
+ | begin
+ | bar
+ | end
+ |"
+ "foo = if bar?
+ | 1
+ |else
+ | 2
+ | end
+ |
+ | foo || begin
+ | bar
+ |end
+ |
+ | foo ||
+ | begin
+ |bar
+ | end
+ |")
+ ))
+
+(ert-deftest ruby-ts-align-to-stmt-keywords-case ()
+ (skip-unless (treesit-available-p))
+ (let ((ruby-align-to-stmt-keywords '(case)))
+ (ruby-ts-should-indent-buffer
+ "b = case a
+ |when 13
+ | 6
+ |else
+ | 42
+ |end"
+ "b = case a
+ | when 13
+ | 6
+ | else
+ | 42
+ | end")))
+
+(ert-deftest ruby-ts-add-log-current-method-examples ()
+ (skip-unless (treesit-available-p))
+ (let ((pairs '(("foo" . "#foo")
+ ("C.foo" . ".foo")
+ ("self.foo" . ".foo")
+ ("<<" . "#<<"))))
+ (dolist (pair pairs)
+ (let ((name (car pair))
+ (value (cdr pair)))
+ (ruby-ts-with-temp-buffer (ruby-ts-test-string
+ "module M
+ | class C
+ | def %s
+ | _
+ | end
+ | end
+ |end"
+ name)
+ (search-backward "_")
+ (forward-line)
+ (should (string= (ruby-ts-add-log-current-function)
+ (format "M::C%s" value))))))))
+
+(ert-deftest ruby-ts-add-log-current-method-outside-of-method ()
+ (skip-unless (treesit-available-p))
+ (ruby-ts-with-temp-buffer (ruby-ts-test-string
+ "module M
+ | class C
+ | def foo
+ | end
+ | _
+ | end
+ |end")
+ (search-backward "_")
+ (should (string= (ruby-ts-add-log-current-function) "M::C"))))
+
+(ert-deftest ruby-ts-add-log-current-method-in-singleton-class ()
+ (skip-unless (treesit-available-p))
+ (ruby-ts-with-temp-buffer (ruby-ts-test-string
+ "class C
+ | class << self
+ | def foo
+ | _
+ | end
+ | end
+ |end")
+ (search-backward "_")
+ (should (string= (ruby-ts-add-log-current-function) "C.foo"))))
+
+(ert-deftest ruby-ts-add-log-current-method-namespace-shorthand ()
+ (skip-unless (treesit-available-p))
+ (ruby-ts-with-temp-buffer (ruby-ts-test-string
+ "class C::D
+ | def foo
+ | _
+ | end
+ |end")
+ (search-backward "_")
+ (should (string= (ruby-ts-add-log-current-function) "C::D#foo"))))
+
+(ert-deftest ruby-ts-add-log-current-method-after-inner-class ()
+ (skip-unless (treesit-available-p))
+ (ruby-ts-with-temp-buffer (ruby-ts-test-string
+ "module M
+ | class C
+ | class D
+ | end
+ | def foo
+ | _
+ | end
+ | end
+ |end")
+ (search-backward "_")
+ (should (string= (ruby-ts-add-log-current-function) "M::C#foo"))))
+
+(ert-deftest ruby-ts-add-log-current-method-after-inner-class-outside-methods ()
+ (skip-unless (treesit-available-p))
+ (ruby-ts-with-temp-buffer (ruby-ts-test-string
+ "module M
+ | class C
+ | class D
+ | end
+ |
+ |_
+ | end
+ |end")
+ (search-backward "_")
+ (delete-char 1)
+ (should (string= (ruby-ts-add-log-current-function) "M::C"))))
+
+(ert-deftest ruby-ts-add-log-current-method-after-inner-class-outside-methods-with-text ()
+ (skip-unless (treesit-available-p))
+ (ruby-ts-with-temp-buffer (ruby-ts-test-string
+ "module M
+ | class C
+ | class D
+ | end
+ |
+ | FOO = 5
+ | end
+ |end")
+ (search-backward "FOO")
+ (should (string= (ruby-ts-add-log-current-function) "M::C"))))
+
+(ert-deftest ruby-ts-add-log-current-method-after-endless-method ()
+ (skip-unless (treesit-available-p))
+ (ruby-ts-with-temp-buffer (ruby-ts-test-string
+ "module M
+ | class C
+ | def foo =
+ | 4_
+ | end
+ |end")
+ (search-backward "_")
+ (delete-char 1)
+ (should (string= (ruby-ts-add-log-current-function) "M::C#foo"))))
+
+(defmacro ruby-ts-resource-file (file)
+ `(when-let ((testfile ,(or (macroexp-file-name)
+ buffer-file-name)))
+ (let ((default-directory (file-name-directory testfile)))
+ (file-truename
+ (expand-file-name (format "ruby-mode-resources/%s" ,file))))))
+
+(defmacro ruby-ts-deftest-indent (file)
+ `(ert-deftest ,(intern (format "ruby-ts-indent-test/%s" file)) ()
+ ;; :tags '(:expensive-test)
+ (skip-unless (treesit-available-p))
+ (let ((buf (find-file-noselect (ruby-ts-resource-file ,file))))
+ (unwind-protect
+ (with-current-buffer buf
+ (let ((orig (buffer-string)))
+ ;; Indent and check that we get the original text.
+ (indent-region (point-min) (point-max))
+ (should (equal (buffer-string) orig))))
+ (kill-buffer buf)))))
+
+(ruby-ts-deftest-indent "ruby-method-params-indent.rb")
+
+(provide 'ruby-ts-mode-tests)
+
+;;; ruby-ts-mode-tests.el ends here