emacs-diffs
[Top][All Lists]
Advanced

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

master d148d02 2/2: Add tests for replace-regexp-in-string


From: Mattias Engdegård
Subject: master d148d02 2/2: Add tests for replace-regexp-in-string
Date: Wed, 25 Nov 2020 09:46:17 -0500 (EST)

branch: master
commit d148d0259a4d3ad3da2f5bdf121e1f5836b3522b
Author: Mattias Engdegård <mattiase@acm.org>
Commit: Mattias Engdegård <mattiase@acm.org>

    Add tests for replace-regexp-in-string
    
    * test/lisp/subr-tests.el (subr-replace-regexp-in-string): New.
---
 test/lisp/subr-tests.el | 63 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 63 insertions(+)

diff --git a/test/lisp/subr-tests.el b/test/lisp/subr-tests.el
index 035c064..c77be51 100644
--- a/test/lisp/subr-tests.el
+++ b/test/lisp/subr-tests.el
@@ -484,5 +484,68 @@ See https://debbugs.gnu.org/cgi/bugreport.cgi?bug=19350.";
 
   (should-error (string-replace "" "x" "abc")))
 
+(ert-deftest subr-replace-regexp-in-string ()
+  (should (equal (replace-regexp-in-string "a+" "xy" "abaabbabaaba")
+                 "xybxybbxybxybxy"))
+  ;; FIXEDCASE
+  (let ((case-fold-search t))
+    (should (equal (replace-regexp-in-string "a+" "xy" "ABAABBABAABA")
+                   "XYBXYBBXYBXYBXY"))
+    (should (equal (replace-regexp-in-string "a+" "xy" "ABAABBABAABA" t)
+                   "xyBxyBBxyBxyBxy"))
+    (should (equal (replace-regexp-in-string
+                    "a[bc]*" "xyz"
+                    "a A ab AB Ab aB abc ABC Abc AbC aBc")
+                   "xyz XYZ xyz XYZ Xyz xyz xyz XYZ Xyz Xyz xyz"))
+    (should (equal (replace-regexp-in-string
+                    "a[bc]*" "xyz"
+                    "a A ab AB Ab aB abc ABC Abc AbC aBc" t)
+                   "xyz xyz xyz xyz xyz xyz xyz xyz xyz xyz xyz")))
+  (let ((case-fold-search nil))
+    (should (equal (replace-regexp-in-string "a+" "xy" "ABAABBABAABA")
+                   "ABAABBABAABA")))
+  ;; group substitution
+  (should (equal (replace-regexp-in-string
+                  "a\\(b*\\)" "<\\1,\\&>" "babbcaabacbab")
+                 "b<bb,abb>c<,a><b,ab><,a>cb<b,ab>"))
+  (should (equal (replace-regexp-in-string
+                  "x\\(?2:..\\)\\(?1:..\\)\\(..\\)\\(..\\)\\(..\\)"
+                  "<\\3,\\5,\\4,\\1,\\2>" "yxabcdefghijkl")
+                 "y<ef,ij,gh,cd,ab>kl"))
+  ;; LITERAL
+  (should (equal (replace-regexp-in-string
+                  "a\\(b*\\)" "<\\1,\\&>" "babbcaabacbab" nil t)
+                 "b<\\1,\\&>c<\\1,\\&><\\1,\\&><\\1,\\&>cb<\\1,\\&>"))
+  (should (equal (replace-regexp-in-string
+                  "a" "\\\\,\\?" "aba")
+                 "\\,\\?b\\,\\?"))
+  (should (equal (replace-regexp-in-string
+                  "a" "\\\\,\\?" "aba" nil t)
+                 "\\\\,\\?b\\\\,\\?"))
+  ;; SUBEXP
+  (should (equal (replace-regexp-in-string
+                  "\\(a\\)\\(b*\\)c" "xy" "babbcdacd" nil nil 2)
+                 "baxycdaxycd"))
+  ;; START
+  (should (equal (replace-regexp-in-string
+                  "ab" "x" "abcabdabeabf" nil nil nil 4)
+                 "bdxexf"))
+  ;; An empty pattern matches once before every character.
+  (should (equal (replace-regexp-in-string "" "x" "abc")
+                 "xaxbxc"))
+  (should (equal (replace-regexp-in-string "y*" "x" "abc")
+                 "xaxbxc"))
+  ;; replacement function
+  (should (equal (replace-regexp-in-string
+                  "a\\(b*\\)c"
+                  (lambda (s)
+                    (format "<%s,%s,%s,%s,%s>"
+                            s
+                            (match-beginning 0) (match-end 0)
+                            (match-beginning 1) (match-end 1)))
+                  "babbcaacabc")
+                 "b<abbc,0,4,1,3>a<ac,0,2,1,1><abc,0,3,1,2>"))
+  )
+
 (provide 'subr-tests)
 ;;; subr-tests.el ends here



reply via email to

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