emacs-diffs
[Top][All Lists]
Advanced

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

master c5c545e 2/2: Add tests for incomplete escape sequences in ansi-co


From: Lars Ingebrigtsen
Subject: master c5c545e 2/2: Add tests for incomplete escape sequences in ansi-color-tests
Date: Wed, 6 Oct 2021 04:51:31 -0400 (EDT)

branch: master
commit c5c545e45eba866ca7c5931a6f4a86891035222b
Author: Miha Rihtaršič <miha@kamnitnik.top>
Commit: Lars Ingebrigtsen <larsi@gnus.org>

    Add tests for incomplete escape sequences in ansi-color-tests
    
    * test/lisp/ansi-color-tests.el
    (ansi-color-tests-equal-props): New function.
    (ansi-color-incomplete-sequences-test): New ert test (bug#50806).
---
 test/lisp/ansi-color-tests.el | 82 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 82 insertions(+)

diff --git a/test/lisp/ansi-color-tests.el b/test/lisp/ansi-color-tests.el
index 16a1ba4..14a14ca 100644
--- a/test/lisp/ansi-color-tests.el
+++ b/test/lisp/ansi-color-tests.el
@@ -24,6 +24,7 @@
 ;;; Code:
 
 (require 'ansi-color)
+(eval-when-compile (require 'cl-lib))
 
 (defvar ansi-color-tests--strings
   (let ((bright-yellow (face-foreground 'ansi-color-bright-yellow nil 
'default))
@@ -61,6 +62,17 @@
       ("\e[48;2;135;255;255;1mHello World\e[0m" "Hello World"
        (ansi-color-bold (:background ,custom-color))))))
 
+(defun ansi-color-tests-equal-props (o1 o2)
+  "Return t if two Lisp objects have similar structure and contents.
+While `equal-including-properties' compares text properties of
+strings with `eq', this function compares them with `equal'."
+  (or (equal-including-properties o1 o2)
+      (and (stringp o1)
+           (equal o1 o2)
+           (cl-loop for i below (length o1)
+                    always (equal (text-properties-at i o1)
+                                  (text-properties-at i o2))))))
+
 (ert-deftest ansi-color-apply-on-region-test ()
   (pcase-dolist (`(,input ,text ,face) ansi-color-tests--strings)
     (with-temp-buffer
@@ -91,6 +103,76 @@
       (ansi-color-apply-on-region (point-min) (point-max) t)
       (should (equal (buffer-string) (car pair))))))
 
+(ert-deftest ansi-color-incomplete-sequences-test ()
+  (let* ((strs (list "\e[" "2;31m Hello World "
+                     "\e" "[108;5;12" "3m" "Greetings"
+                     "\e[0m\e[35;6m" "Hello"))
+         (complete-str (apply #'concat strs))
+         (filtered-str)
+         (propertized-str)
+         (ansi-color-apply-face-function
+          #'ansi-color-apply-text-property-face)
+         (ansi-filt (lambda (str) (ansi-color-filter-apply
+                                   (copy-sequence str))))
+         (ansi-app (lambda (str) (ansi-color-apply
+                                  (copy-sequence str)))))
+
+    (with-temp-buffer
+      (setq filtered-str
+            (replace-regexp-in-string "\e\\[.*?m" "" complete-str))
+      (setq propertized-str (funcall ansi-app complete-str))
+
+      (should-not (ansi-color-tests-equal-props
+                   filtered-str propertized-str))
+      (should (equal filtered-str propertized-str)))
+
+    ;; Tests for `ansi-color-filter-apply'
+    (with-temp-buffer
+      (should (equal-including-properties
+               filtered-str
+               (funcall ansi-filt complete-str))))
+
+    (with-temp-buffer
+      (should (equal-including-properties
+               filtered-str
+               (mapconcat ansi-filt strs ""))))
+
+    ;; Tests for `ansi-color-filter-region'
+    (with-temp-buffer
+      (insert complete-str)
+      (ansi-color-filter-region (point-min) (point-max))
+      (should (equal-including-properties
+               filtered-str (buffer-string))))
+
+    (with-temp-buffer
+      (dolist (str strs)
+        (let ((opoint (point)))
+          (insert str)
+          (ansi-color-filter-region opoint (point))))
+      (should (equal-including-properties
+               filtered-str (buffer-string))))
+
+    ;; Test for `ansi-color-apply'
+    (with-temp-buffer
+      (should (ansi-color-tests-equal-props
+               propertized-str
+               (mapconcat ansi-app strs ""))))
+
+    ;; Tests for `ansi-color-apply-on-region'
+    (with-temp-buffer
+      (insert complete-str)
+      (ansi-color-apply-on-region (point-min) (point-max))
+      (should (ansi-color-tests-equal-props
+               propertized-str (buffer-string))))
+
+    (with-temp-buffer
+      (dolist (str strs)
+        (let ((opoint (point)))
+          (insert str)
+          (ansi-color-apply-on-region opoint (point))))
+      (should (ansi-color-tests-equal-props
+               propertized-str (buffer-string))))))
+
 (provide 'ansi-color-tests)
 
 ;;; ansi-color-tests.el ends here



reply via email to

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