emacs-diffs
[Top][All Lists]
Advanced

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

[Emacs-diffs] master abf7d0d: Split up and add tests for two page.el fun


From: Simen Heggestøyl
Subject: [Emacs-diffs] master abf7d0d: Split up and add tests for two page.el functions
Date: Sun, 23 Jun 2019 01:27:55 -0400 (EDT)

branch: master
commit abf7d0d802728e0ae8e064683eb5fb411bad911e
Author: Simen Heggestøyl <address@hidden>
Commit: Simen Heggestøyl <address@hidden>

    Split up and add tests for two page.el functions
    
    * lisp/textmodes/page.el (page--count-lines-page): New function
    extracted from `count-lines-page'.
    (count-lines-page): Extract main logic into `page--count-lines-page'.
    (page--what-page); New function extracted from `what-page'.
    (what-page): Extract main logic into `page--what-page'.
    
    * test/lisp/textmodes/page-tests.el (page-tests-count-lines-page)
    (page-tests-what-page): New tests for `page--count-lines-page' and
    `page--what-page'.  (Bug#36009)
---
 lisp/textmodes/page.el            | 59 ++++++++++++++++++++++-----------------
 test/lisp/textmodes/page-tests.el | 19 ++++++++++++-
 2 files changed, 52 insertions(+), 26 deletions(-)

diff --git a/lisp/textmodes/page.el b/lisp/textmodes/page.el
index 8921b69..a42fc6e 100644
--- a/lisp/textmodes/page.el
+++ b/lisp/textmodes/page.el
@@ -125,41 +125,50 @@ thus showing a page other than the one point was 
originally in."
                        (point)))))
 (put 'narrow-to-page 'disabled t)
 
-(defun count-lines-page ()
-  "Report number of lines on current page, and how many are before or after 
point."
-  (interactive)
+(defun page--count-lines-page ()
+  "Return a list of line counts on the current page.
+The list is on the form (TOTAL BEFORE AFTER), where TOTAL is the
+total number of lines on the current page, while BEFORE and AFTER
+are the number of lines on the current page before and after
+point, respectively."
   (save-excursion
-    (let ((opoint (point)) beg end
-         total before after)
+    (let ((opoint (point)))
       (forward-page)
       (beginning-of-line)
-      (or (looking-at page-delimiter)
-         (end-of-line))
-      (setq end (point))
-      (backward-page)
-      (setq beg (point))
-      (setq total (count-lines beg end)
-           before (count-lines beg opoint)
-           after (count-lines opoint end))
-      (message (ngettext "Page has %d line (%d + %d)"
-                        "Page has %d lines (%d + %d)" total)
-              total before after))))
+      (unless (looking-at page-delimiter)
+        (end-of-line))
+      (let ((end (point)))
+        (backward-page)
+        (list (count-lines (point) end)
+              (count-lines (point) opoint)
+              (count-lines opoint end))))))
 
-(defun what-page ()
-  "Print page and line number of point."
+(defun count-lines-page ()
+  "Report number of lines on current page, and how many are before or after 
point."
   (interactive)
+  (pcase-let ((`(,total ,before ,after) (page--count-lines-page)))
+    (message (ngettext "Page has %d line (%d + %d)"
+                       "Page has %d lines (%d + %d)" total)
+             total before after)))
+
+(defun page--what-page ()
+  "Return a list of the page and line number of point."
   (save-restriction
     (widen)
     (save-excursion
       (let ((count 1)
-           (opoint (point)))
-       (goto-char (point-min))
-       (while (re-search-forward page-delimiter opoint t)
-          (if (= (match-beginning 0) (match-end 0))
-              (forward-char 1))
-         (setq count (1+ count)))
-       (message "Page %d, line %d" count (line-number-at-pos opoint))))))
+            (opoint (point)))
+        (goto-char (point-min))
+        (while (re-search-forward page-delimiter opoint t)
+          (when (= (match-beginning 0) (match-end 0))
+            (forward-char))
+          (setq count (1+ count)))
+        (list count (line-number-at-pos opoint))))))
 
+(defun what-page ()
+  "Print page and line number of point."
+  (interactive)
+  (apply #'message (cons "Page %d, line %d" (page--what-page))))
 
 
 ;;; Place `provide' at end of file.
diff --git a/test/lisp/textmodes/page-tests.el 
b/test/lisp/textmodes/page-tests.el
index 0834d65..517f1d5 100644
--- a/test/lisp/textmodes/page-tests.el
+++ b/test/lisp/textmodes/page-tests.el
@@ -82,5 +82,22 @@
     (narrow-to-page -1)
     (should (equal (buffer-string) "bar\n"))))
 
-(provide 'page-tests)
+(ert-deftest page-tests-count-lines-page ()
+  (with-temp-buffer
+    (insert "foo\n\nbar\n\nbaz")
+    (goto-char (point-min))
+    (should (equal (page--count-lines-page) '(1 0 1)))
+    (goto-char (point-max))
+    (should (equal (page--count-lines-page) '(2 2 0)))))
+
+(ert-deftest page-tests-what-page ()
+  (with-temp-buffer
+    (insert "foo\n\nbar\n\nbaz")
+    (goto-char (point-min))
+    (should (equal (page--what-page) '(1 1)))
+    (forward-page)
+    (should (equal (page--what-page) '(2 2)))
+    (forward-page)
+    (should (equal (page--what-page) '(3 4)))))
+
 ;;; page-tests.el ends here



reply via email to

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