emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[nongnu] elpa/lua-mode 0e6813a 405/468: Refactor indentation tests to us


From: Philip Kaludercic
Subject: [nongnu] elpa/lua-mode 0e6813a 405/468: Refactor indentation tests to use test cases defined in *.lua files
Date: Thu, 5 Aug 2021 04:59:18 -0400 (EDT)

branch: elpa/lua-mode
commit 0e6813a4b30737295074fb6c8442e05a377696fc
Author: immerrr <immerrr+lua@gmail.com>
Commit: immerrr <immerrr+lua@gmail.com>

    Refactor indentation tests to use test cases defined in *.lua files
---
 lua-mode.el                                       |  33 +-
 test/indentation-tests/README.md                  |  63 +++
 test/indentation-tests/assignment-indentation.lua |  71 ++++
 test/indentation-tests/continuation-lines.lua     | 111 +++++
 test/indentation-tests/goto-label.lua             | 126 ++++++
 test/indentation-tests/issue-33.lua               |  42 ++
 test/indentation-tests/only-use-last-opener.lua   |  97 +++++
 test/indentation-tests/smoke.lua                  |   3 +
 test/test-indentation.el                          | 471 ++++------------------
 test/utils.el                                     |  53 ++-
 10 files changed, 642 insertions(+), 428 deletions(-)

diff --git a/lua-mode.el b/lua-mode.el
index f7b1513..e275cdb 100644
--- a/lua-mode.el
+++ b/lua-mode.el
@@ -132,8 +132,8 @@
                          (seq (* digit) (opt ".") (+ digit)))
                      (opt (regexp "[eE][+-]?[0-9]+"))))
            (lua-assignment-op (seq "=" (or buffer-end (not (any "=")))))
-           (lua-token (or "+" "-" "*" "/" "%" "^" "#" "==" "~=" "<=" ">=" "<"
-                       ">" "=" ";" ":" "," "." ".." "..."))
+           (lua-operator (or "+" "-" "*" "/" "%" "^" "#" "==" "~=" "<=" ">=" 
"<"
+                          ">" "=" ";" ":" "," "." ".." "..."))
            (lua-keyword
             (symbol "and" "break" "do" "else" "elseif" "end"  "for" "function"
                     "goto" "if" "in" "local" "not" "or" "repeat" "return"
@@ -214,14 +214,13 @@ element is itself expanded with `lua-rx-to-string'. "
                         (opt (regexp "[eE][+-]?[0-9]+"))))
               (lua-assignment-op
                :rx (seq "=" (or buffer-end (not (any "=")))))
-              (lua-token
+              (lua-operator
                :rx (or "+" "-" "*" "/" "%" "^" "#" "==" "~=" "<=" ">=" "<"
                        ">" "=" ";" ":" "," "." ".." "..."))
               (lua-keyword
                :rx (symbol "and" "break" "do" "else" "elseif" "end"  "for" 
"function"
                            "goto" "if" "in" "local" "not" "or" "repeat" 
"return"
-                           "then" "until" "while")))
-            ))))
+                           "then" "until" "while")))))))
 
 
 ;; Local variables
@@ -1502,19 +1501,29 @@ and relative each, and the shift/column to indent to."
     indentation-info))
 
 
-(defun lua-accumulate-indentation-info (info)
+(defun lua-accumulate-indentation-info (reversed-indentation-info)
   "Accumulates the indentation information previously calculated by
 lua-calculate-indentation-info. Returns either the relative indentation
 shift, or the absolute column to indent to."
-  (let ((info-list (reverse info))
+  (let (indentation-info
         (type 'relative)
         (accu 0))
+    ;; Aggregate all neighbouring relative offsets, reversing the INFO list.
+    (cl-dolist (elt reversed-indentation-info)
+      (if (and (eq (car elt) 'relative)
+               (eq (caar indentation-info) 'relative))
+          (setcdr (car indentation-info) (+ (cdar indentation-info) (cdr elt)))
+        (push elt indentation-info)))
+
+    ;; Aggregate indentation info, taking 'absolute modifiers into account.
     (mapc (lambda (x)
-            (setq accu (if (eq 'absolute (car x))
-                           (progn (setq type 'absolute)
-                                  (cdr x))
-                         (+ accu (cdr x)))))
-          info-list)
+            (let ((new-val (cdr x)))
+              (if (eq 'absolute (car x))
+                  (progn (setq type 'absolute
+                               accu new-val))
+                (setq accu (+ accu new-val)))))
+          indentation-info)
+
     (cons type accu)))
 
 (defun lua-calculate-indentation-block-modifier (&optional parse-end)
diff --git a/test/indentation-tests/README.md b/test/indentation-tests/README.md
new file mode 100644
index 0000000..0ae4edc
--- /dev/null
+++ b/test/indentation-tests/README.md
@@ -0,0 +1,63 @@
+## Indentation Tests
+
+This directory contains indentation tests for `lua-mode`.
+
+Each `*.lua` file will be processed by `test-indentation.el` as follows:
+
+- the file itself will be added as a buttercup `describe` block
+
+- each span of code between comments will be added as `it` block that checks 
that the code is reindented as given in the file
+
+- last line of comment before Lua code span will be used as the name of `it` 
block
+
+- spans of code that have empty name will be called `section 1`, `section 2` 
and so on
+
+- spans of code that contain only whitespace will be skipped
+
+- if the name of the code span contains `XFAIL`, the test will be created as 
an expected failure (`xit` in buttercup)
+
+### Example
+
+Here's an example:
+
+```lua
+
+-- function call indentation
+
+function(
+   1,
+   2,
+   3,
+)
+
+-- XXX: this is commented out for now
+-- while block indentation
+--
+-- while true do
+--    print(123)
+-- end
+--
+-- function literal indentation
+
+local x = function()
+   return 1, 2, 3
+end
+
+```
+
+It will create two tests, "function call indentation" and "function literal" 
indentation. The test called "while block indentation" will be ignored 
completely.
+
+### Adding Configuration Parameters
+
+To add configuration parameters use Emacs syntax for Local Variables:
+
+```
+
+-- Local Variables:
+-- lua-indent-close-paren-align: nil
+-- lua-indent-only-use-last-opener: t
+-- End:
+
+```
+
+This can go anywhere in the file, but make sure that the code span after the 
local variables section has a name comment, otherwise it will use `End:` line 
as a name.
diff --git a/test/indentation-tests/assignment-indentation.lua 
b/test/indentation-tests/assignment-indentation.lua
new file mode 100644
index 0000000..fdf31a8
--- /dev/null
+++ b/test/indentation-tests/assignment-indentation.lua
@@ -0,0 +1,71 @@
+-- ensure is sane
+
+foo = 10
+
+bar = 20
+
+-- add continuation before =
+
+foo
+   = 10
+
+bar = 20
+
+-- add continuation after =
+
+foo =
+   10
+bar = 20
+
+-- XFAIL: continuation after comma: 1
+foo,
+   baz = 10, 20
+
+bar = 20
+
+-- continuation after comma: 2
+
+foo, baz
+   = 10, 20
+
+bar = 20
+
+-- XFAIL: continuation after comma: 3
+
+foo, baz = 10,
+   20
+
+bar = 20
+
+-- XFAIL: continuation after comma: 4
+
+foo,
+   baz =
+   10, 20
+
+-- XFAIL: continuation after comma: 5
+
+foo, baz =
+   10,
+   20
+
+bar = 20
+
+-- continuation after "local": 1
+
+local
+   x = 5
+
+-- XFAIL: continuation after "local": 2
+
+local
+   x,
+   y = 10, 20
+
+-- XFAIL: continuation after "local": 3
+
+local
+   x,
+   y =
+   10,
+   20
diff --git a/test/indentation-tests/continuation-lines.lua 
b/test/indentation-tests/continuation-lines.lua
new file mode 100644
index 0000000..e329d36
--- /dev/null
+++ b/test/indentation-tests/continuation-lines.lua
@@ -0,0 +1,111 @@
+-- indentation if broken in the middle of \"foo.bar\" and \"qux:quux\"
+foo123
+   .bar:baz(xyz)
+
+foo123.
+   bar:baz(xyz)
+
+foo123.bar
+   :baz(xyz)
+
+foo123.bar:
+   baz(xyz)
+
+foo123.bar
+   .baz
+   .qux
+   :quux(xyz)
+
+-- indentation after return
+
+function foo()
+   return
+      123
+end
+
+-- indentation after return: blocks
+
+do
+   return
+      123
+end
+
+do
+   return
+      x +
+      y
+end
+
+do
+   return
+end
+
+foo = bar
+
+-- indentation after return: f1
+
+function f1()
+   if foo == bar then
+      return
+   else
+      foo = bar
+   end
+end
+
+-- indentation after return: f2
+
+function f2()
+   if foo == bar then
+      return
+   elseif foo != bar then
+      foo = bar
+   end
+end
+
+-- indentation after return: f3
+
+function f3()
+   repeat
+      return
+   until foo == bar
+end
+
+-- indentation after ellipsis
+
+function x(...)
+   a, b = 1, ...
+   return b
+end
+
+
+-- indentation of function call arguments in continuation part
+
+x = foo(123,
+        456)
+   + bar(
+      qux,
+      quux)
+
+-- XFAIL: indentation in block-intros: while
+while
+   foo do
+   a = a + 1
+end
+
+a = 0
+
+-- XFAIL: indentation in block-intros: for1
+
+for k, v
+   in pairs(bar) do
+   a = a + 1
+end
+
+a = 0
+
+-- XFAIL: indentation in block-intros: for2
+
+for k, v
+   in pairs(bar) do a = a + 1 end
+
+a = 0
diff --git a/test/indentation-tests/goto-label.lua 
b/test/indentation-tests/goto-label.lua
new file mode 100644
index 0000000..2920ea9
--- /dev/null
+++ b/test/indentation-tests/goto-label.lua
@@ -0,0 +1,126 @@
+-- is sane
+::foo::
+::bar::
+
+::baz::
+
+a = 0
+
+-- does not affect indentation when put on a separate line
+
+for z=1,10 do
+   ::foo::
+   bar
+end
+
+a = 0
+
+-- XFAIL: does not affect indentation before block modifiers
+
+::foo:: for z=1,10 do
+   bar
+end
+
+a = 0
+
+-- does not affect indentation after block modifiers
+
+for z=1,10 do  ::foo::
+   bar
+end
+
+a = 0
+
+-- reindents according to luawiki examples: 1
+
+for z=1,10 do
+   ::foo::
+   for y=1,10 do  ::bar
+      for x=1,10 do
+         if x^2 + y^2 == z^2 then
+            print('found a Pythagorean triple:', x, y, z)
+            goto done
+            goto done2
+         end
+      end
+      ::done2::
+   end
+end
+
+::done::
+
+-- reindents according to luawiki examples: 2
+for z=1,10 do
+   for y=1,10 do
+      for x=1,10 do
+         if x^2 + y^2 == z^2 then
+            print('found a Pythagorean triple:', x, y, z)
+            print('now trying next z...')
+            goto zcontinue
+         end
+      end
+   end
+   ::zcontinue::
+end
+
+-- reindents according to luawiki examples: 3
+
+for x=1,5 do ::redo::
+   print(x .. ' + 1 = ?')
+   local y = tonumber(io.read'*l')
+   if y ~= x + 1 then goto redo end
+end
+
+-- reindents according to luawiki examples: 4
+
+::a::
+print 'A'
+if math.random() < 0.3 then goto c end
+::b::
+print 'B'
+if math.random() < 0.5 then goto a end
+::c::
+print 'C'
+if math.random() < 0.1 then goto a else goto b end
+
+-- reindents according to luawiki examples: 5
+
+function fact_(n, ans)
+   ::call::
+   if n == 0 then
+      return ans
+   else
+      n, ans = n - 1, ans * n
+      goto call
+   end
+end
+print(fact_(5, 1)) --> 120
+
+-- reindents according to luawiki examples: 6
+
+function f()
+   if not g() then goto fail end
+   if not h() then goto cleanup_g end
+   if not i() then goto cleanup_h end
+   do return true end    -- need do/end?
+
+   ::cleanup_h::
+   undo_h()
+   ::cleanup_g::
+   undo_g()
+   ::fail::
+   return false
+end
+
+-- reindents according to luawiki examples: 7
+
+::redo::
+for x=1,10 do
+   for y=1,10 do
+      if not f(x,y) then goto continue end
+      if not g(x,y) then goto skip end
+      if not h(x,y) then goto redo end
+      ::continue::
+   end
+end ::skip::
+print('foo')
diff --git a/test/indentation-tests/issue-33.lua 
b/test/indentation-tests/issue-33.lua
new file mode 100644
index 0000000..14014b2
--- /dev/null
+++ b/test/indentation-tests/issue-33.lua
@@ -0,0 +1,42 @@
+-- don't accumulate indentation after the expression
+a =
+   {
+   }
+
+b =
+   {
+   },
+
+
+a = {
+   table_elt_indented
+}
+
+a = a +
+   5 +
+   10
+
+this_should_be_unindented()
+
+-- here foobar should be indented as simple continuation statement
+a = a +
+   dosmth(
+   ) +
+   foobar
+
+a =
+   do_smth(
+      do_smth_arg
+   )
+
+b =
+   {
+      table_elt0_indented,
+      table_elt1_indented
+   }
+
+this_should_be_unindented_too =
+   {
+   }
+
+this_should_be_unindented_three = etc
diff --git a/test/indentation-tests/only-use-last-opener.lua 
b/test/indentation-tests/only-use-last-opener.lua
new file mode 100644
index 0000000..e89413b
--- /dev/null
+++ b/test/indentation-tests/only-use-last-opener.lua
@@ -0,0 +1,97 @@
+-- Local Variables:
+-- lua-indent-close-paren-align: nil
+-- lua-indent-only-use-last-opener: t
+-- End:
+
+-- XFAIL: one param, nested table on same line as opener
+
+foobar({
+   a, b, c
+})
+
+-- XFAIL: two params, nested table on same line as opener
+
+foobar(a, {
+   b,
+   c
+})
+
+foobar({}, {
+   b,
+   c
+})
+
+-- XFAIL: two aligned params, nested table on next line
+
+foobar({},
+       {1, 2, 3})
+
+-- XFAIL: two aligned table params, first has nested tables
+
+foobar({{},
+        {1, 2, 3}},
+       {
+          4,5,6
+       })
+
+foobar({{},
+        {1, 2, 3}},
+       {
+          4,5,6
+       }
+)
+
+-- XFAIL: one nested table containing another table
+
+foobar({
+   {4, 5, 6}
+})
+
+-- XFAIL: nested table with indentation: nested table on separate line
+
+foobar(
+   a,
+   {
+      b,
+      c
+   })
+
+foobar(
+   a,
+   {
+      b,
+      c
+   }
+)
+
+-- XFAIL: nested table with alignment: nested table on separate line
+
+foobar(a,
+       {
+          b,
+          c
+       })
+
+foobar(a,
+       {
+          b,
+          c
+       }
+)
+
+-- nested table with indentation: params after nested table
+
+foobar(
+   {
+      a,
+      b
+   },
+   c, d)
+
+foobar(
+   {
+      a,
+      b
+   },
+   c, d
+)
diff --git a/test/indentation-tests/smoke.lua b/test/indentation-tests/smoke.lua
new file mode 100644
index 0000000..d83a8d7
--- /dev/null
+++ b/test/indentation-tests/smoke.lua
@@ -0,0 +1,3 @@
+function hello()
+   print("Hello, world")
+end
diff --git a/test/test-indentation.el b/test/test-indentation.el
index 6c7c59d..33248d9 100644
--- a/test/test-indentation.el
+++ b/test/test-indentation.el
@@ -1,182 +1,61 @@
 ;; -*- flycheck-disabled-checkers: (emacs-lisp-checkdoc) -*-
+;; -*- lexical-binding: t -*-
 (load (concat (file-name-directory (or load-file-name (buffer-file-name)
                                        default-directory))
               "utils.el") nil 'nomessage 'nosuffix)
 
-(describe "Assignment indentation"
-  (it "is sane"
-    (lua--reindent-like "\
-foo = 10
-
-bar = 20"))
-  (it "adds continuation before ="
-   (lua--reindent-like "\
-foo
-   = 10
-
-bar = 20"))
-
-  (it "adds continuation after ="
-    (lua--reindent-like "\
-foo =
-   10
-bar = 20"))
-
-
-  ;; (ert-deftest lua-indentation-assignment-with-commas ()
-  ;;   (lua--reindent-like "\
-  ;; foo,
-  ;;    baz = 10, 20
-
-  ;; bar = 20")
-  ;;    (lua--reindent-like "\
-  ;; foo, baz
-  ;;    = 10, 20
-
-  ;; bar = 20")
-
-  ;;    (lua--reindent-like "\
-  ;; foo, baz = 10,
-  ;;    20
-
-  ;; bar = 20")
-
-  ;;    (lua--reindent-like "\
-  ;; foo,
-  ;;    baz =
-  ;;    10, 20")
-
-  ;;    (lua--reindent-like "\
-  ;; foo, baz =
-  ;;    10,
-  ;;    20
-
-  ;; bar = 20")
-
-  ;;    (lua--reindent-like "\
-  ;; local
-  ;;    x = 5")
-
-  ;;    (lua--reindent-like "\
-  ;; local
-  ;;    x,
-  ;;    y = 10, 20")
-
-  ;;    (lua--reindent-like "\
-  ;; local
-  ;;    x,
-  ;;    y =
-  ;;    10,
-  ;;    20"))
-
-  (it "does not accumulate indentation after the expression (issue #33)"
-    (lua--reindent-like "\
-a =
-   {
-   }
-
-b =
-   {
-   },
-
-
-a = {
-   table_elt_indented
-}
-
-a = a +
-   5 +
-   10
-
-this_should_be_unindented()
-
--- here foobar should be indented as simple continuation statement
-a = a +
-   dosmth(
-   ) +
-   foobar
-
-a =
-   do_smth(
-      do_smth_arg
-   )
-
-b =
-   {
-      table_elt0_indented,
-      table_elt1_indented
-   }
-
-this_should_be_unindented_too =
-   {
-   }
-
-this_should_be_unindented_three = etc")))
+(require 'buttercup)
+(require 'subr-x)
+
+(defun file-contents (path)
+  (with-temp-buffer
+    (insert-file-contents-literally path)
+    (buffer-substring-no-properties (point-min) (point-max))))
+
+(defun string-trim-safe (str)
+  (save-match-data (string-trim str)))
+
+(defun indentation-test-sections (file-path)
+  (with-temp-buffer
+     (insert-file-contents-literally file-path)
+     (let (results
+           section-name
+           (begin (point-min))
+           end
+           cur-str
+           (next-section-name "start"))
+       (goto-char (point-min))
+       (while next-section-name
+         (setq next-section-name
+               (when (re-search-forward "^--\\(.*\\)" nil 'noerror) 
(string-trim-safe (match-string-no-properties 1))))
+         (setq end (if next-section-name (match-beginning 0) (point-max)))
+         (setq cur-str (string-trim-safe (buffer-substring-no-properties begin 
end)))
+         (if (> (length cur-str) 0)
+             (push (cons (or section-name (format "section %d" (1+ (length 
results))))
+                         cur-str)
+                   results))
+         (setq section-name next-section-name)
+         (setq begin (point)))
+       (nreverse results))))
+
+(defun make-indentation-it-or-xit-clause (x)
+  (let ((it-or-xit (if (string-match "XFAIL" (car x)) 'xit 'it)))
+    (eval `(,it-or-xit ,(format "%s" (car x))
+                       (let ((lua-code ,(cdr x)))
+                         (expect lua-code :to-be-reindented-the-same-way))))))
+
+(let* ((current-path (or load-file-name (buffer-file-name) default-directory))
+       (indentation-tests-dir (concat (file-name-directory current-path) 
"indentation-tests"))
+       (indentation-tests (directory-files indentation-tests-dir nil 
".*\.lua$" 'nosort)))
+  (mapcar (lambda (test-file)
+            (let ((file-path (expand-file-name test-file 
indentation-tests-dir)))
+              (describe (format "Indentation test `%s'" test-file)
+                (mapcar #'make-indentation-it-or-xit-clause
+                        (indentation-test-sections file-path)))))
+          indentation-tests))
 
 
 (describe "Continuation lines"
-  (it "are indented if broken in the middle of \"foo.bar\" and \"qux:quux\""
-    (lua--reindent-like "\
-foo123
-   .bar:baz(xyz)")
-    (lua--reindent-like "\
-foo123.
-   bar:baz(xyz)")
-    (lua--reindent-like "\
-foo123.bar
-   :baz(xyz)")
-    (lua--reindent-like "\
-foo123.bar:
-   baz(xyz)")
-    (lua--reindent-like "\
-foo123.bar
-   .baz
-   .qux
-   :quux(xyz)"))
-
-  (it "are indented after return"
-    (expect (lua--reindent-like "\
-return
-   123"))
-    (expect (lua--reindent-like "\
-do
-   return
-      123
-end"))
-    (expect (lua--reindent-like "\
-do
-   return
-      x +
-      y
-end")))
-
-  (it "are not indented if \"return\" returns no values"
-    (expect (lua--reindent-like "\
-do
-   return
-end
-
-foo = bar"))
-
-    (expect (lua--reindent-like "\
-if foo == bar then
-   return
-else
-   foo = bar
-end"))
-
-    (expect (lua--reindent-like "\
-if foo == bar then
-   return
-elseif foo != bar then
-   foo = bar
-end"))
-
-    (expect (lua--reindent-like "\
-repeat
-   return
-until foo == bar")))
-
   (it "are indented before/after binary operators"
     (let ((binops '("+"  "-"  "*"  "/"  "^"  "%"  ".."
                     "<"  "<="  ">"  ">="  "=="  "~="
@@ -190,12 +69,6 @@ a = foo
    BINOP bar" 'fixedcase)))))
 
 
-  (it "are not indented after ellipsis"
-    (lua--reindent-like "\
-function x(...)
-   a, b = 1, ...
-   return b
-end"))
 
   (xit "are indented before/after unary operators"
     (expect (lua--reindent-like "\
@@ -224,38 +97,8 @@ x = {qux, xyzzy
       (expect (lua--reindent-like (replace-regexp-in-string "<>" unop "\
 x = {
    <>quux
-}")))))
-
-  (it "indents function call arguments in continuation part"
-    (expect (lua--reindent-like "\
-x = foo(123,
-        456)
-   + bar(
-      qux,
-      quux)")))
+}"))))))
 
-  (xit "are indented in block-intros"
-    (expect (lua--reindent-like "\
-while
-   foo do
-   a = a + 1
-end
-
-a = 0"))
-
-    (expect (lua--reindent-like "\
-for k, v
-   in pairs(bar) do
-   a = a + 1
-end
-
-a = 0"))
-
-    (expect (lua--reindent-like "\
-for k, v
-   in pairs(bar) do a = a + 1 end
-
-a = 0"))))
 
 
 (describe "Block indentation"
@@ -458,59 +301,14 @@ foobar{
    a, b, c
 }")))
 
-
-  (xit "indents nested tables"
-    (expect (lua--reindent-like "\
-foobar({
-   a, b, c
-})"))
-
-    (expect (lua--reindent-like "\
-foobar(a, {
-   b,
-   c
-})"))
-
-    (expect (lua--reindent-like "\
-foobar(
-   a,
-   {
-      b,
-      c
-   })"))
-
-    (expect (lua--reindent-like "\
-foobar(a,
-       {
-          b,
-          c
-       })"))
-
-    (expect (lua--reindent-like "\
-foobar(a,
-       {
-          b,
-          c
-       }
-)"))
-
-    (expect (lua--reindent-like "\
-foobar(
-   {
-      a,
-      b
-   },
-   c, d
-)")))
-
   (it "indent blocks with lua-indent-nested-block-content-align"
-       (let ((lua-indent-nested-block-content-align nil))
-         (expect (lua--reindent-like "\
+    (let ((lua-indent-nested-block-content-align nil))
+      (expect (lua--reindent-like "\
 call_some_fn( something, {
       val = 5,
       another = 6,
 } )"))
-         (expect (lua--reindent-like "\
+      (expect (lua--reindent-like "\
 local def = {
    some_very_long_name = { fn =
          function()
@@ -518,34 +316,34 @@ local def = {
          end
    }
 }"))
-         ))
+      ))
 
   (it "indent blocks with lua-indent-close-paren-align"
-       (let ((lua-indent-close-paren-align nil))
-         (expect (lua--reindent-like "\
+    (let ((lua-indent-close-paren-align nil))
+      (expect (lua--reindent-like "\
 local foo = setmetatable( {
       a = 4,
       b = 5,
 }, {
       __index = some_func,
 } )"))
-         ))
+      ))
 
   (it "indents nested tables with alternative block indenting"
-       (let ((lua-indent-nested-block-content-align nil)
-                 (lua-indent-close-paren-align nil))
-         (expect (lua--reindent-like "\
+    (let ((lua-indent-nested-block-content-align nil)
+         (lua-indent-close-paren-align nil))
+      (expect (lua--reindent-like "\
 foobar({
       a, b, c
 })"))
 
-         (expect (lua--reindent-like "\
+      (expect (lua--reindent-like "\
 foobar(a, {
       b,
       c
 })"))
 
-         (expect (lua--reindent-like "\
+      (expect (lua--reindent-like "\
 foobar(
    a,
    {
@@ -553,7 +351,7 @@ foobar(
       c
 })"))
 
-         (expect (lua--reindent-like "\
+      (expect (lua--reindent-like "\
 foobar(
    a,
    {
@@ -562,14 +360,14 @@ foobar(
    }
 )"))
 
-         (expect (lua--reindent-like "\
+      (expect (lua--reindent-like "\
 foobar(a,
    {
       b,
       c
 })"))
 
-         (expect (lua--reindent-like "\
+      (expect (lua--reindent-like "\
 foobar(a,
    {
       b,
@@ -577,7 +375,7 @@ foobar(a,
    }
 )"))
 
-         (expect (lua--reindent-like "\
+      (expect (lua--reindent-like "\
 foobar(
    {
       a,
@@ -585,7 +383,7 @@ foobar(
    },
    c, d
 )"))
-         )))
+      )))
 
 (ert-deftest lua-indentation-defun ()
   ;;    [local] function funcname funcbody
@@ -611,136 +409,3 @@ foobar(
 do
    foobar = _do
 end")))
-
-
-(describe "Goto label indentation"
-  (it "is sane"
-   (expect (lua--reindent-like "\
-::foo::
-::bar::
-
-::baz::
-
-a = 0")))
-
-  (it "does not affect indentation when put on a separate line"
-   (expect (lua--reindent-like "\
-for z=1,10 do
-   ::foo::
-   bar
-end
-
-a = 0")))
-
-  (xit "does not affect indentation before block modifiers"
-   (expect (lua--reindent-like "\
-::foo:: for z=1,10 do
-   bar
-end
-
-a = 0")))
-
-  (it "does not affect indentation after block modifiers"
-   (expect (lua--reindent-like "\
-for z=1,10 do  ::foo::
-   bar
-end
-
-a = 0")))
-
-  (it "reindents according to luawiki examples"
-    (expect (lua--reindent-like "\
-for z=1,10 do
-   ::foo::
-   for y=1,10 do  ::bar
-      for x=1,10 do
-         if x^2 + y^2 == z^2 then
-            print('found a Pythagorean triple:', x, y, z)
-            goto done
-            goto done2
-         end
-      end
-      ::done2::
-   end
-end
-
-::done::"))
-
-    (expect (lua--reindent-like "\
--- 5.2.0-beta-rc2
-for z=1,10 do
-   for y=1,10 do
-      for x=1,10 do
-         if x^2 + y^2 == z^2 then
-            print('found a Pythagorean triple:', x, y, z)
-            print('now trying next z...')
-            goto zcontinue
-         end
-      end
-   end
-   ::zcontinue::
-end"))
-
-    (expect (lua--reindent-like "\
--- Lua 5.2.0-beta-rc2
-for x=1,5 do ::redo::
-   print(x .. ' + 1 = ?')
-   local y = tonumber(io.read'*l')
-   if y ~= x + 1 then goto redo end
-end"))
-
-    (expect (lua--reindent-like "\
--- 5.2.0-beta-rc1
-::a::
-print 'A'
-if math.random() < 0.3 then goto c end
-::b::
-print 'B'
-if math.random() < 0.5 then goto a end
-::c::
-print 'C'
-if math.random() < 0.1 then goto a else goto b end
-"))
-
-    (expect (lua--reindent-like "\
--- 5.2.0-beta-rc2 - factorial with tail recursion simulated with goto's
--- (warning: there's no need to do this)
-function fact_(n, ans)
-   ::call::
-   if n == 0 then
-      return ans
-   else
-      n, ans = n - 1, ans * n
-      goto call
-   end
-end
-print(fact_(5, 1)) --> 120"))
-
-    (expect (lua--reindent-like "\
--- 5.2.0-beta-rc2
-function f()
-   if not g() then goto fail end
-   if not h() then goto cleanup_g end
-   if not i() then goto cleanup_h end
-   do return true end    -- need do/end?
-
-   ::cleanup_h::
-   undo_h()
-   ::cleanup_g::
-   undo_g()
-   ::fail::
-   return false
-end"))
-
-    (expect (lua--reindent-like "\
--- 5.2.0-beta-rc2
-::redo::
-for x=1,10 do
-   for y=1,10 do
-      if not f(x,y) then goto continue end
-      if not g(x,y) then goto skip end
-      if not h(x,y) then goto redo end
-      ::continue::
-   end
-end ::skip::
-print('foo')"))))
diff --git a/test/utils.el b/test/utils.el
index d641cc8..8636108 100644
--- a/test/utils.el
+++ b/test/utils.el
@@ -136,16 +136,18 @@ This is a mere typing/reading aid for lua-mode's 
font-lock tests."
         (lua-kill-process)))))
 
 (defun lua-get-indented-strs (strs)
-  (butlast
-   (split-string
-    (with-lua-buffer
-     (let ((inhibit-message t))
-       (insert (replace-regexp-in-string "^\\s *" "" (lua-join-lines strs)))
-       (font-lock-fontify-buffer)
-       (indent-region (point-min) (point-max))
-       (buffer-substring-no-properties
-        (point-min) (point-max))))
-    "\n" nil)))
+  (let ((indent-tabs-mode nil)
+        (font-lock-verbose nil))
+   (butlast
+    (split-string
+     (with-lua-buffer
+      (let ((inhibit-message t))
+        (insert (replace-regexp-in-string "^\\s *" "" (lua-join-lines strs)))
+        (font-lock-fontify-buffer)
+        (indent-region (point-min) (point-max))
+        (buffer-substring-no-properties
+         (point-min) (point-max))))
+     "\n" nil))))
 
 (defun lua-insert-goto-<> (strs)
   "Insert sequence of strings and put point in place of \"<>\"."
@@ -166,9 +168,7 @@ This is a mere typing/reading aid for lua-mode's font-lock 
tests."
      "\n" nil)))
 
 (defun lua--reindent-like (str)
-  (let ((strs (split-string str "\n"))
-        (indent-tabs-mode nil)
-        (font-lock-verbose nil))
+  (let ((strs (split-string str "\n")))
     (equal strs (lua-get-indented-strs strs))))
 
 (defun with-point-at-matcher (&rest args)
@@ -228,3 +228,30 @@ This is a mere typing/reading aid for lua-mode's font-lock 
tests."
 
 (buttercup-define-matcher :with-point-at (&rest args)
   (apply #'with-point-at-matcher `(:lua-code ,(car args) :with-point-at ,@(cdr 
args))))
+
+
+(require 'subr-x)
+
+;; (describe "foo"
+;;   (it "runs hello"
+;;     (expect "function foo()\nreturn 123\nend" 
:to-be-reindented-the-same-way)))
+
+;; (defun lua--explain-indentation-mismatch (strs indented-strs)
+;;   (cl-loop for i in (number-sequence 1 (length strs))
+;;            for s1 in strs
+;;            for s2 in indented-strs
+;;            if (not (string-equal s1 s2))
+;;            collect (format "Mismatch on line %s:\nExpected: %S\nActual  : 
%S" i s1 s2)))
+
+
+
+
+(buttercup-define-matcher :to-be-reindented-the-same-way (str)
+  (let* ((lines (split-string (funcall str) "\n"))
+         (indented-lines (lua-get-indented-strs lines)))
+    (buttercup--test-expectation (equal lines indented-lines)
+      :expect-match-phrase (format "Indentation check 
failed:\n=========\nExpected:\n---------\n%s\n---------\nActual:\n---------\n%s\n========="
+                                   (string-trim (mapconcat 'identity lines 
"\n"))
+                                   (string-trim (mapconcat 'identity 
indented-lines "\n")))
+      :expect-mismatch-phrase (format "Expected `%S' to not be reindented like 
that"
+                                      lines))))



reply via email to

[Prev in Thread] Current Thread [Next in Thread]