branch: elpa/lua-mode
commit 56691b8ffd790715dc82fbf21329483f105bc264
Author: immerrr <[email protected]>
Commit: immerrr <[email protected]>
Add tests for preserving point location when filling
---
test/test-fill.el | 101 +++++++++++++++++++++++++++++++++++++++++++++---------
test/utils.el | 15 ++++++++
2 files changed, 99 insertions(+), 17 deletions(-)
diff --git a/test/test-fill.el b/test/test-fill.el
index 8b93d10..75089f8 100644
--- a/test/test-fill.el
+++ b/test/test-fill.el
@@ -4,32 +4,35 @@
"utils.el") nil 'nomessage 'nosuffix)
-
-(defun expect-filled-as (strs etalon)
- (expect
- (lua-buffer-strs
- (let ((fill-column 10))
- (lua-insert-goto-<>
- strs)
- (execute-kbd-macro (kbd "M-q"))))
- :to-equal
- etalon))
+(defun expect-filled-as (strs etalon &optional look-at)
+ (let ((result-str (lua-buffer-strs
+ (let ((fill-column 10))
+ (lua-insert-goto-<> strs)
+ (execute-kbd-macro (kbd "M-q"))
+ (when look-at
+ (expect (point) :to-precede look-at))))))
+ (expect result-str :to-equal etalon)))
(describe "Test fill-paragraph"
- (it "filling single-line comment"
+ (it "fills single-line comment"
(expect-filled-as '("<>-- foo bar baz qux")
'("-- foo bar"
- "-- baz qux")))
- (it "filling comment after code"
+ "-- baz qux")
+ "-- foo bar"))
+ (it "fills comment after code"
(expect-filled-as '("<>foo -- bar baz")
'("foo -- bar"
" -- baz")))
- (xit "filling multiline comment"
- (expect-filled-as '("<>--[[ foo bar baz ]]")
- '("--[[ foo bar"
- " baz ]]")))
+ (xit "fills multiline comment"
+ ;; Right now it ends up with something like this:
+ ;;
+ ;; --[[ ab c
+ ;; --d ]]
+ (expect-filled-as '("<>--[[ ab c d ]]")
+ '("--[[ ab c"
+ " d ]]")))
(it "does not spill comments into code (issue #25)"
(expect-filled-as '("<>"
"-- foo bar baz qux"
@@ -38,3 +41,67 @@
"-- foo bar"
"-- baz qux"
"foo_func()"))))
+
+
+(describe "Test fill-paragraph preserves point position"
+ (it "doesn't move point if nothing has changed"
+ (expect-filled-as '("<>-- foo bar")
+ '("-- foo bar")
+ "-- foo bar")
+
+ (expect-filled-as '("-- <>foo bar")
+ '("-- foo bar")
+ "foo bar")
+
+ (expect-filled-as '("-- foo <>bar")
+ '("-- foo bar")
+ "bar"))
+
+ (it "doesn't move point in refilled region"
+ (expect-filled-as '("--<> foo bar baz qux")
+ '("-- foo bar"
+ "-- baz qux")
+ " foo bar\n")
+
+ (expect-filled-as '("-- <>foo bar baz qux")
+ '("-- foo bar"
+ "-- baz qux")
+ "foo bar\n")
+
+ (expect-filled-as '("-- <> foo bar baz qux")
+ '("-- foo"
+ "-- bar"
+ "-- baz"
+ "-- qux")
+ " foo\n")
+
+ (expect-filled-as '("-- foo bar <>baz qux")
+ '("-- foo bar"
+ "-- baz qux")
+ "baz qux")
+ (expect-filled-as '("-- foo bar<> baz qux")
+ '("-- foo bar"
+ "-- baz qux")
+ "\n-- baz qux")
+
+ (expect-filled-as '("-- foo bar baz qux<>")
+ '("-- foo bar"
+ "-- baz qux")
+ "$")
+ )
+
+ (it "doesn't move point if nothing has changed (multi-line)"
+ (expect-filled-as '("--[[ a <>b]]")
+ '("--[[ a b]]")
+ "b]]")
+
+ (expect-filled-as '("--[[ a<>"
+ " b"
+ "]]")
+ '("--[[ a"
+ " b"
+ "]]")
+ "\n b\n]]")
+
+ )
+ )
diff --git a/test/utils.el b/test/utils.el
index 65256f2..c2f4c8e 100644
--- a/test/utils.el
+++ b/test/utils.el
@@ -32,6 +32,21 @@ Fontification check failed on line %d for:
(to-be-fontified-as text faces))
+(buttercup-define-matcher :to-precede (pos regexp)
+ (save-excursion
+ (goto-char pos)
+ (let* ((precedes (looking-at regexp))
+ (substr-begin (min (point-max) pos))
+ (substr-end (min (point-max) (+ pos 100)))
+ (found-after (format "%S" (buffer-substring-no-properties
+ substr-begin substr-end ))))
+ (goto-char substr-end)
+ (when (eobp) (setq found-after (concat found-after " (end-of-buffer)")))
+ (cons precedes (format "Expected %s to see after point at %s: %S.
Found: %s"
+ (if precedes "NOT" "")
+ pos regexp found-after)))))
+
+
(defun get-str-faces (str)
"Find contiguous spans of non-default faces in STR.