Subject: bug#12169: Acknowledgement ([PATCH] Merge changes from upstream ruby-mode)

From: Dmitry Gutov <[email-masked]>
Date: Fri, 10 Aug 2012 18:52:30 +0400

Here's part 2, in which I'm skipping the next 3 commits:

https://github.com/ruby/ruby/commit/85df40e9ab4559cd4ddde3f106ab56c76a808cc8
https://github.com/ruby/ruby/commit/6b0dc7fd81b25bc66681548b8b82f38258f7e08c
https://github.com/ruby/ruby/commit/862048e28d26112af1e67205cfd1c16564590bce

Because as far as I can tell, they hijack the `ruby-deep-indent-paren' 
behavior. IOW, with these applied, the indentation works as though the 
above variable is always nil (with a small improvement in the nested 
case, to be fair), so they don't make sense.

Instead, I added two tests, for `ruby-deep-indent-paren' enabled and 
disabled, and also ported all examples from test/misc/test_ruby_mode.rb.
diff --git a/test/ChangeLog b/test/ChangeLog
index 86f3019..c59c3cb 100644
--- a/test/ChangeLog
+++ b/test/ChangeLog
@@ -1,3 +1,11 @@
+2012-08-10  Dmitry Gutov  <[email-masked]>
+
+	* automated/ruby-mode-tests.el (ruby-should-indent): Add
+	docstring, check (current-indentation) instead of (current-column).
+	(ruby-should-indent-buffer): New function.
+	Add tests for `ruby-deep-indent-paren' behavior.
+	Port all tests from test/misc/test_ruby_mode.rb in Ruby repo.
+
 2012-08-09  Dmitry Gutov  <[email-masked]>
 
 	* automated/ruby-mode-tests.el (ruby-should-indent)
diff --git a/test/automated/ruby-mode-tests.el b/test/automated/ruby-mode-tests.el
index fbe1b8d..f91b6e4 100644
--- a/test/automated/ruby-mode-tests.el
+++ b/test/automated/ruby-mode-tests.el
@@ -24,11 +24,24 @@
 (require 'ruby-mode)
 
 (defun ruby-should-indent (content column)
+  "Assert indentation COLUMN on the last line of CONTENT."
   (with-temp-buffer
     (insert content)
     (ruby-mode)
     (ruby-indent-line)
-    (should (= (current-column) column))))
+    (should (= (current-indentation) column))))
+
+(defun ruby-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."
+  (with-temp-buffer
+    (cl-flet ((fix-indent (s) (replace-regexp-in-string "^[ \t]*|" "" s)))
+      (insert (fix-indent content))
+      (ruby-mode)
+      (indent-region (point-min) (point-max))
+      (should (string= (fix-indent expected) (buffer-substring-no-properties
+                                              (point-min) (point-max)))))))
 
 (defun ruby-assert-state (content &rest values-plist)
   "Assert syntax state values at the end of CONTENT.
@@ -57,6 +70,127 @@ VALUES-PLIST is a list with alternating index and value elements."
   (ruby-assert-state "foo <<asd\n" 3 ?\n)
   (ruby-assert-state "class <<asd\n" 3 nil))
 
+(ert-deftest ruby-deep-indent ()
+  (let ((ruby-deep-arglist nil)
+        (ruby-deep-indent-paren '(?\( ?\{ ?\[ ?\] t)))
+    (ruby-should-indent "foo = [1,\n2" 7)
+    (ruby-should-indent "foo = {a: b,\nc: d" 7)
+    (ruby-should-indent "foo(a,\nb" 4)))
+
+(ert-deftest ruby-deep-indent-disabled ()
+  (let ((ruby-deep-arglist nil)
+        (ruby-deep-indent-paren nil))
+    (ruby-should-indent "foo = [\n1" ruby-indent-level)
+    (ruby-should-indent "foo = {\na: b" ruby-indent-level)
+    (ruby-should-indent "foo(\na" ruby-indent-level)))
+
+(ert-deftest ruby-indent-simple ()
+  (ruby-should-indent-buffer
+   "if foo
+   |  bar
+   |end
+   |zot
+   |"
+   "if foo
+   |bar
+   |  end
+   |    zot
+   |"))
+
+(ert-deftest ruby-indent-keyword-label ()
+  (ruby-should-indent-buffer
+   "bar(class: XXX) do
+   |  foo
+   |end
+   |bar
+   |"
+   "bar(class: XXX) do
+   |     foo
+   |  end
+   |    bar
+   |"))
+
+(ert-deftest ruby-indent-method-with-question-mark ()
+  (ruby-should-indent-buffer
+   "if x.is_a?(XXX)
+   |  foo
+   |end
+   |"
+   "if x.is_a?(XXX)
+   | foo
+   |   end
+   |"))
+
+(ert-deftest ruby-indent-expr-in-regexp ()
+  (ruby-should-indent-buffer
+   "if /#{foo}/ =~ s
+   |  x = 1
+   |end
+   |"
+   "if /#{foo}/ =~ s
+   | x = 1
+   |  end
+   |"))
+
+(ert-deftest ruby-indent-singleton-class ()
+  :expected-result :failed   ; Doesn't work yet, when no space before "<<".
+  (ruby-should-indent-buffer
+   "class<<bar
+   |  foo
+   |end
+   |"
+   "class<<bar
+   |foo
+   |   end
+   |"))
+
+(ert-deftest ruby-indent-array-literal ()
+  (let ((ruby-deep-indent-paren nil))
+    (ruby-should-indent-buffer
+     "foo = [
+     |  bar
+     |]
+     |"
+     "foo = [
+     | bar
+     |  ]
+     |"))
+  (ruby-should-indent-buffer
+   "foo do
+   |  [bar]
+   |end
+   |"
+   "foo do
+   |[bar]
+   |  end
+   |"))
+
+(ert-deftest ruby-indent-begin-end ()
+  (ruby-should-indent-buffer
+   "begin
+   |  a[b]
+   |end
+   |"
+   "begin
+   | a[b]
+   |  end
+   |"))
+
+(ert-deftest ruby-indent-array-after-paren-and-space ()
+  (ruby-should-indent-buffer
+   "class A
+   |  def foo
+   |    foo( [])
+   |  end
+   |end
+   |"
+   "class A
+   | def foo
+   |foo( [])
+   |end
+   |  end
+   |"))
+
 (provide 'ruby-mode-tests)
 
 ;;; ruby-mode-tests.el ends here