From d148d0259a4d3ad3da2f5bdf121e1f5836b3522b Mon Sep 17 00:00:00 2001 From: =?utf8?q?Mattias=20Engdeg=C3=A5rd?= Date: Wed, 25 Nov 2020 13:56:39 +0100 Subject: [PATCH] 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 035c064d75c..c77be511dc2 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") + "bc<,a><,a>cb")) + (should (equal (replace-regexp-in-string + "x\\(?2:..\\)\\(?1:..\\)\\(..\\)\\(..\\)\\(..\\)" + "<\\3,\\5,\\4,\\1,\\2>" "yxabcdefghijkl") + "ykl")) + ;; 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") + "ba")) + ) + (provide 'subr-tests) ;;; subr-tests.el ends here -- 2.39.2