Skip to content

Commit

Permalink
[REF] Trigger roll event by execute-kbd-macro
Browse files Browse the repository at this point in the history
Real user behavior instead of simulation.
  • Loading branch information
KeyWeeUsr committed Aug 17, 2024
1 parent 82f8622 commit 7fdb983
Showing 1 changed file with 61 additions and 62 deletions.
123 changes: 61 additions & 62 deletions typewriter-roll-mode-tests.el
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@
(let* ((old-win-start (window-start))
(old-win-end (window-end))
(old-text "text text")
(expected-text (format "%s\ntext" old-text)))
(expected-text (format "%s\ntext " old-text)))
;; initial text
(insert old-text)

;; default state
(should (string= (buffer-string) old-text))
(should (eq (typewriter-roll--cursor-line) 1))
Expand All @@ -40,26 +41,29 @@
(let ((fill-column 10))
(typewriter-roll-mode)

(typewriter-roll--scroll-main (current-column))
;; default state, not enough chars to fill `fill-column'
(should (string= (buffer-string) old-text))
(execute-kbd-macro (kbd "SPC"))
;; default state, SPC does not trigger the roll
;; keep cursor on line 1, keep top on line 1
(should (string= (buffer-string) (format "%s " old-text)))
(should (eq (typewriter-roll--cursor-line) 1))
(should (eq (typewriter-roll--top-line) 1))

(insert " text")
;; text is inserted, --scroll-main not called yet
(insert "text")
(should (string= (buffer-string) (format "%s text" old-text)))
(should (eq (typewriter-roll--cursor-line) 1))
(should (eq (typewriter-roll--top-line) 1))

;; only after
(typewriter-roll--scroll-main (current-column))
;; after --scroll-main call
;; move cursor to line 2, move top to line 2
(execute-kbd-macro (kbd "SPC"))
(should (string= (buffer-string) expected-text))
(should (eq (typewriter-roll--cursor-line) 2))
(should (eq (typewriter-roll--top-line) 2))

(insert " ") ; will be removed by filling
(typewriter-roll--scroll-main (current-column))
(should (string= (buffer-string) expected-text))
;; another space does not trigger the roll
(execute-kbd-macro (kbd "SPC"))
(should (string= (buffer-string) (format "%s " expected-text)))
(should (eq (typewriter-roll--cursor-line) 2))
(should (eq (typewriter-roll--top-line) 2)))))))

Expand All @@ -77,7 +81,7 @@ that line should be the only one visible on top."
(let* ((old-win-start (window-start))
(old-win-end (window-end))
(old-text "text text")
(expected-text (format "%s\ntext" old-text)))
(expected-text (format "%s\ntext " old-text)))
;; initial text
(insert old-text)

Expand All @@ -89,30 +93,29 @@ that line should be the only one visible on top."
(let ((fill-column 10))
(typewriter-roll-mode)

(typewriter-roll--scroll-main (current-column))
;; default state, not enough chars to fill `fill-column'
(execute-kbd-macro (kbd "SPC"))
;; default state, SPC does not trigger the roll
;; keep cursor on line 1, keep top on line 1
(should (string= (buffer-string) old-text))
(should (string= (buffer-string) (format "%s " old-text)))
(should (eq (typewriter-roll--cursor-line) 1))
(should (eq (typewriter-roll--top-line) 1))

;; text is inserted, --scroll-main not called yet
(insert " text")
(insert "text")
(should (string= (buffer-string) (format "%s text" old-text)))
(should (eq (typewriter-roll--cursor-line) 1))
(should (eq (typewriter-roll--top-line) 1))

;; after --scroll-main call
;; move cursor to line 2, move top to line 2
(typewriter-roll--scroll-main (current-column))
(execute-kbd-macro (kbd "SPC"))
(should (string= (buffer-string) expected-text))
(should (eq (typewriter-roll--cursor-line) 2))
(should (eq (typewriter-roll--top-line) 2))

;; space will be removed by filling, keep previous state
(insert " ")
(typewriter-roll--scroll-main (current-column))
(should (string= (buffer-string) expected-text))
;; another space does not trigger the roll
(execute-kbd-macro (kbd "SPC"))
(should (string= (buffer-string) (format "%s " expected-text)))
(should (eq (typewriter-roll--cursor-line) 2))
(should (eq (typewriter-roll--top-line) 2)))))))

Expand All @@ -130,7 +133,7 @@ keep one more line visible above."
(let* ((old-win-start (window-start))
(old-win-end (window-end))
(old-text "text text")
(expected-text (format "%s\ntext" old-text)))
(expected-text (format "%s\ntext " old-text)))
;; initial text
(insert old-text)

Expand All @@ -142,31 +145,30 @@ keep one more line visible above."
(let ((fill-column 10))
(typewriter-roll-mode)

(typewriter-roll--scroll-main (current-column))
;; default state, not enough chars to fill `fill-column'
(execute-kbd-macro (kbd "SPC"))
;; default state, SPC does not trigger the roll
;; keep cursor on line 1, keep top on line 1
(should (string= (buffer-string) old-text))
(should (string= (buffer-string) (format "%s " old-text)))
(should (eq (typewriter-roll--cursor-line) 1))
(should (eq (typewriter-roll--top-line) 1))

;; text is inserted, --scroll-main not called yet
(insert " text")
(insert "text")
(should (string= (buffer-string) (format "%s text" old-text)))
(should (eq (typewriter-roll--cursor-line) 1))
(should (eq (typewriter-roll--top-line) 1))

;; after --scroll-main call
;; move cursor to line 2, keep top on line 1
;; (i.e. +1 previous in focus)
(typewriter-roll--scroll-main (current-column))
(execute-kbd-macro (kbd "SPC"))
(should (string= (buffer-string) expected-text))
(should (eq (typewriter-roll--cursor-line) 2))
(should (eq (typewriter-roll--top-line) 1))

;; space will be removed by filling, keep previous state
(insert " ")
(typewriter-roll--scroll-main (current-column))
(should (string= (buffer-string) expected-text))
;; another space does not trigger the roll
(execute-kbd-macro (kbd "SPC"))
(should (string= (buffer-string) (format "%s " expected-text)))
(should (eq (typewriter-roll--cursor-line) 2))
(should (eq (typewriter-roll--top-line) 1)))))))

Expand All @@ -184,7 +186,7 @@ but keep one more line visible above."
(let* ((old-win-start (window-start))
(old-win-end (window-end))
(old-text "text text")
(expected-text (format "%s\ntext" old-text)))
(expected-text (format "%s\ntext " old-text)))
;; initial text
(insert old-text)

Expand All @@ -196,31 +198,30 @@ but keep one more line visible above."
(let ((fill-column 10))
(typewriter-roll-mode)

(typewriter-roll--scroll-main (current-column))
;; default state, not enough chars to fill `fill-column'
(execute-kbd-macro (kbd "SPC"))
;; default state, SPC does not trigger the roll
;; keep cursor on line 1, keep top on line 1
(should (string= (buffer-string) old-text))
(should (string= (buffer-string) (format "%s " old-text)))
(should (eq (typewriter-roll--cursor-line) 1))
(should (eq (typewriter-roll--top-line) 1))

;; text is inserted, --scroll-main not called yet
(insert " text")
(insert "text")
(should (string= (buffer-string) (format "%s text" old-text)))
(should (eq (typewriter-roll--cursor-line) 1))
(should (eq (typewriter-roll--top-line) 1))

;; after --scroll-main call
;; move cursor to line 2, keep top on line 1
;; (i.e. +1 previous in focus)
(typewriter-roll--scroll-main (current-column))
(execute-kbd-macro (kbd "SPC"))
(should (string= (buffer-string) expected-text))
(should (eq (typewriter-roll--cursor-line) 2))
(should (eq (typewriter-roll--top-line) 1))

;; space will be removed by filling, keep previous state
(insert " ")
(typewriter-roll--scroll-main (current-column))
(should (string= (buffer-string) expected-text))
;; another space does not trigger the roll
(execute-kbd-macro (kbd "SPC"))
(should (string= (buffer-string) (format "%s " expected-text)))
(should (eq (typewriter-roll--cursor-line) 2))
(should (eq (typewriter-roll--top-line) 1)))))))

Expand All @@ -238,7 +239,7 @@ that line should be the only one visible on top."
(let* ((old-win-start (window-start))
(old-win-end (window-end))
(old-text "text text")
(expected-text (format "%s\ntext" old-text)))
(expected-text (format "%s\ntext " old-text)))
;; initial text
(insert old-text)

Expand All @@ -250,30 +251,29 @@ that line should be the only one visible on top."
(let ((fill-column 10))
(typewriter-roll-mode)

(typewriter-roll--scroll-main (current-column))
;; default state, not enough chars to fill `fill-column'
(execute-kbd-macro (kbd "SPC"))
;; default state, SPC does not trigger the roll
;; keep cursor on line 1, keep top on line 1
(should (string= (buffer-string) old-text))
(should (string= (buffer-string) (format "%s " old-text)))
(should (eq (typewriter-roll--cursor-line) 1))
(should (eq (typewriter-roll--top-line) 1))

;; text is inserted, --scroll-main not called yet
(insert " text")
(insert "text")
(should (string= (buffer-string) (format "%s text" old-text)))
(should (eq (typewriter-roll--cursor-line) 1))
(should (eq (typewriter-roll--top-line) 1))

;; after --scroll-main call
;; move cursor to line 2, move top to line 2
(typewriter-roll--scroll-main (current-column))
(execute-kbd-macro (kbd "SPC"))
(should (string= (buffer-string) expected-text))
(should (eq (typewriter-roll--cursor-line) 2))
(should (eq (typewriter-roll--top-line) 2))

;; space will be removed by filling, keep previous state
(insert " ")
(typewriter-roll--scroll-main (current-column))
(should (string= (buffer-string) expected-text))
;; another space does not trigger the roll
(execute-kbd-macro (kbd "SPC"))
(should (string= (buffer-string) (format "%s " expected-text)))
(should (eq (typewriter-roll--cursor-line) 2))
(should (eq (typewriter-roll--top-line) 2)))))))

Expand All @@ -288,19 +288,18 @@ Test scrolling value used based on `typewriter-roll-prefer-scroll-margin'."
(progn
(advice-add 'recenter-top-bottom
:override
(lambda (arg) (setq result arg)))
(let ((typewriter-roll-prefer-scroll-margin nil))
(with-temp-buffer
(insert "1\n2") ; move cursor to trigger scrolling
(typewriter-roll--scroll-main (current-column)))
(should (eq result typewriter-roll-keep-in-focus)))
(let ((typewriter-roll-prefer-scroll-margin t))
(with-temp-buffer
(insert "1\n2") ; move cursor to trigger scrolling
(typewriter-roll--scroll-main (current-column)))
(should (eq result scroll-margin))))
(lambda (arg) (push arg result)))
(dolist (item '(nil t))
(let ((typewriter-roll-prefer-scroll-margin item))
(with-temp-buffer
(typewriter-roll--fix-ert-window)
(typewriter-roll-mode)
(insert "1\n2")
(typewriter-roll--scroll-main (current-column)))))
(should (equal (reverse result)
`(,typewriter-roll-keep-in-focus ,scroll-margin))))
(advice-remove 'recenter-top-bottom
(lambda (arg) (setq result arg))))))
(lambda (arg) (push arg result))))))

(provide 'typewriter-roll-mode-tests)

Expand Down

0 comments on commit 7fdb983

Please sign in to comment.