]> git.eshelyaron.com Git - emacs.git/commitdiff
Add tests for replace-regexp-in-string
authorMattias Engdegård <mattiase@acm.org>
Wed, 25 Nov 2020 12:56:39 +0000 (13:56 +0100)
committerMattias Engdegård <mattiase@acm.org>
Wed, 25 Nov 2020 12:57:16 +0000 (13:57 +0100)
* test/lisp/subr-tests.el (subr-replace-regexp-in-string): New.

test/lisp/subr-tests.el

index 035c064d75c22e73fa195cce69da5a72d1fdfc7a..c77be511dc2f676e38eb69d71683e6b9ac1b66b2 100644 (file)
@@ -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