Browse Source

Add tests about filling and auto-filling

* lisp/org.el (org-fill-context-prefix): Small refactoring.
(org-fill-paragraph): Add code comments.
* testing/lisp/test-org.el: Add tests
Nicolas Goaziou 12 years ago
parent
commit
5cb9beea9b
2 changed files with 99 additions and 10 deletions
  1. 11 10
      lisp/org.el
  2. 88 0
      testing/lisp/test-org.el

+ 11 - 10
lisp/org.el

@@ -20820,8 +20820,9 @@ If point is in an inline task, mark that task instead."
 Return fill prefix, as a string, or nil if current line isn't
 meant to be filled."
   (save-excursion
-    (let* ((elements (org-element-at-point t))
-	   (element (car elements))
+    (goto-char p)
+    (beginning-of-line)
+    (let* ((element (org-element-at-point))
 	   (type (org-element-type element))
 	   (post-affiliated
 	    (progn
@@ -20829,8 +20830,6 @@ meant to be filled."
 	      (while (looking-at org-element--affiliated-re) (forward-line))
 	      (point))))
       (unless (< p post-affiliated)
-	(goto-char p)
-	(beginning-of-line)
 	(case type
 	  (comment (looking-at "[ \t]*#\\+? ?") (match-string 0))
 	  ((item plain-list)
@@ -20840,12 +20839,13 @@ meant to be filled."
 	  (paragraph
 	   ;; Fill prefix is usually the same as the current line,
 	   ;; except if the paragraph is at the beginning of an item.
-	   (let ((parent (cadr elements)))
-	     (if (eq (org-element-type parent) 'item)
-		 (make-string (org-list-item-body-column
-			       (org-element-property :begin parent))
-			      ? )
-	       (if (looking-at "\\s-+") (match-string 0) ""))))
+	   (let ((parent (org-element-property :parent element)))
+	     (cond ((eq (org-element-type parent) 'item)
+		    (make-string (org-list-item-body-column
+				  (org-element-property :begin parent))
+				 ? ))
+		   ((looking-at "\\s-+") (match-string 0))
+		   (t  ""))))
 	  ((comment-block verse-block)
 	   ;; Only fill contents if P is within block boundaries.
 	   (let* ((cbeg (save-excursion (goto-char post-affiliated)
@@ -20939,6 +20939,7 @@ width for filling."
 		(skip-chars-backward " \r\t\n")
 		(line-beginning-position))
 	      justify))) t)
+	;; Fill comments, indented or not.
 	(comment
 	 (let ((fill-prefix (org-fill-context-prefix (point))))
 	   (save-excursion

+ 88 - 0
testing/lisp/test-org.el

@@ -166,12 +166,100 @@ http://article.gmane.org/gmane.emacs.orgmode/21459/"
 	(org-fill-paragraph)
 	(buffer-string)))
     "#+BEGIN_COMMENT\nSome text\n#+END_COMMENT"))
+  ;; Fill `comment' elements, indented or not.
+  (should
+   (equal "# A B"
+	  (org-test-with-temp-text "# A\n#B"
+	    (let ((fill-column 20))
+	      (org-fill-paragraph)
+	      (buffer-string)))))
+  (should
+   (equal "  #+ A B"
+	  (org-test-with-temp-text "  #+ A\n  #+ B"
+	    (let ((fill-column 20))
+	      (org-fill-paragraph)
+	      (buffer-string)))))
   ;; Do nothing at affiliated keywords.
   (org-test-with-temp-text "#+NAME: para\nSome\ntext."
     (let ((fill-column 20))
       (org-fill-paragraph)
       (should (equal (buffer-string) "#+NAME: para\nSome\ntext.")))))
 
+(ert-deftest test-org/auto-fill-function ()
+  "Test auto-filling features."
+  ;; Auto fill paragraph.
+  (should
+   (equal "12345\n7890"
+	  (org-test-with-temp-text "12345 7890"
+	    (let ((fill-column 5))
+	      (end-of-line)
+	      (org-auto-fill-function)
+	      (buffer-string)))))
+  ;; Auto fill first paragraph in an item.
+  (should
+   (equal "- 12345\n  7890"
+	  (org-test-with-temp-text "- 12345 7890"
+	    (let ((fill-column 7))
+	      (end-of-line)
+	      (org-auto-fill-function)
+	      (buffer-string)))))
+  ;; Auto fill comments, indented or not.
+  (should
+   (equal "# 12345\n# 7890"
+	  (org-test-with-temp-text "# 12345 7890"
+	    (let ((fill-column 7))
+	      (end-of-line)
+	      (org-auto-fill-function)
+	      (buffer-string)))))
+  (should
+   (equal "  #+ 12345\n  #+ 7890"
+	  (org-test-with-temp-text "  #+ 12345 7890"
+	    (let ((fill-column 10))
+	      (end-of-line)
+	      (org-auto-fill-function)
+	      (buffer-string)))))
+  ;; Verse and comment block: auto fill contents.
+  (should
+   (equal "#+BEGIN_VERSE\n12345\n7890\n#+END_VERSE"
+	  (org-test-with-temp-text "#+BEGIN_VERSE\n12345 7890\n#+END_VERSE"
+	    (let ((fill-column 5))
+	      (forward-line)
+	      (end-of-line)
+	      (org-auto-fill-function)
+	      (buffer-string)))))
+  (should
+   (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
+	  (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
+	    (let ((fill-column 5))
+	      (forward-line)
+	      (end-of-line)
+	      (org-auto-fill-function)
+	      (buffer-string)))))
+  ;; Do not fill if a new item could be created.
+  (should-not
+   (equal "12345\n- 90"
+	  (org-test-with-temp-text "12345 - 90"
+	    (let ((fill-column 5))
+	      (end-of-line)
+	      (org-auto-fill-function)
+	      (buffer-string)))))
+  ;; Do not fill if a line break could be introduced.
+  (should-not
+   (equal "123\\\\\n7890"
+	  (org-test-with-temp-text "123\\\\ 7890"
+	    (let ((fill-column 6))
+	      (end-of-line)
+	      (org-auto-fill-function)
+	      (buffer-string)))))
+  ;; Do not fill affiliated keywords.
+  (should-not
+   (equal "#+ATTR_LATEX: ABC\nDEFGHIJKL"
+	  (org-test-with-temp-text "#+ATTR_LATEX: ABC DEFGHIJKL"
+	    (let ((fill-column 20))
+	      (end-of-line)
+	      (org-auto-fill-function)
+	      (buffer-string))))))
+
 
 (provide 'test-org)