diff options
| author | Mattias EngdegÄrd | 2020-11-25 13:56:39 +0100 |
|---|---|---|
| committer | Mattias EngdegÄrd | 2020-11-25 13:57:16 +0100 |
| commit | d148d0259a4d3ad3da2f5bdf121e1f5836b3522b (patch) | |
| tree | 7daf6f3a84367fd14c1b477e1b5cf21234a59f94 | |
| parent | be53fe0263492a124c9cf01aec50b329287465f9 (diff) | |
| download | emacs-d148d0259a4d3ad3da2f5bdf121e1f5836b3522b.tar.gz emacs-d148d0259a4d3ad3da2f5bdf121e1f5836b3522b.zip | |
Add tests for replace-regexp-in-string
* test/lisp/subr-tests.el (subr-replace-regexp-in-string): New.
| -rw-r--r-- | test/lisp/subr-tests.el | 63 |
1 files changed, 63 insertions, 0 deletions
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." | |||
| 484 | 484 | ||
| 485 | (should-error (string-replace "" "x" "abc"))) | 485 | (should-error (string-replace "" "x" "abc"))) |
| 486 | 486 | ||
| 487 | (ert-deftest subr-replace-regexp-in-string () | ||
| 488 | (should (equal (replace-regexp-in-string "a+" "xy" "abaabbabaaba") | ||
| 489 | "xybxybbxybxybxy")) | ||
| 490 | ;; FIXEDCASE | ||
| 491 | (let ((case-fold-search t)) | ||
| 492 | (should (equal (replace-regexp-in-string "a+" "xy" "ABAABBABAABA") | ||
| 493 | "XYBXYBBXYBXYBXY")) | ||
| 494 | (should (equal (replace-regexp-in-string "a+" "xy" "ABAABBABAABA" t) | ||
| 495 | "xyBxyBBxyBxyBxy")) | ||
| 496 | (should (equal (replace-regexp-in-string | ||
| 497 | "a[bc]*" "xyz" | ||
| 498 | "a A ab AB Ab aB abc ABC Abc AbC aBc") | ||
| 499 | "xyz XYZ xyz XYZ Xyz xyz xyz XYZ Xyz Xyz xyz")) | ||
| 500 | (should (equal (replace-regexp-in-string | ||
| 501 | "a[bc]*" "xyz" | ||
| 502 | "a A ab AB Ab aB abc ABC Abc AbC aBc" t) | ||
| 503 | "xyz xyz xyz xyz xyz xyz xyz xyz xyz xyz xyz"))) | ||
| 504 | (let ((case-fold-search nil)) | ||
| 505 | (should (equal (replace-regexp-in-string "a+" "xy" "ABAABBABAABA") | ||
| 506 | "ABAABBABAABA"))) | ||
| 507 | ;; group substitution | ||
| 508 | (should (equal (replace-regexp-in-string | ||
| 509 | "a\\(b*\\)" "<\\1,\\&>" "babbcaabacbab") | ||
| 510 | "b<bb,abb>c<,a><b,ab><,a>cb<b,ab>")) | ||
| 511 | (should (equal (replace-regexp-in-string | ||
| 512 | "x\\(?2:..\\)\\(?1:..\\)\\(..\\)\\(..\\)\\(..\\)" | ||
| 513 | "<\\3,\\5,\\4,\\1,\\2>" "yxabcdefghijkl") | ||
| 514 | "y<ef,ij,gh,cd,ab>kl")) | ||
| 515 | ;; LITERAL | ||
| 516 | (should (equal (replace-regexp-in-string | ||
| 517 | "a\\(b*\\)" "<\\1,\\&>" "babbcaabacbab" nil t) | ||
| 518 | "b<\\1,\\&>c<\\1,\\&><\\1,\\&><\\1,\\&>cb<\\1,\\&>")) | ||
| 519 | (should (equal (replace-regexp-in-string | ||
| 520 | "a" "\\\\,\\?" "aba") | ||
| 521 | "\\,\\?b\\,\\?")) | ||
| 522 | (should (equal (replace-regexp-in-string | ||
| 523 | "a" "\\\\,\\?" "aba" nil t) | ||
| 524 | "\\\\,\\?b\\\\,\\?")) | ||
| 525 | ;; SUBEXP | ||
| 526 | (should (equal (replace-regexp-in-string | ||
| 527 | "\\(a\\)\\(b*\\)c" "xy" "babbcdacd" nil nil 2) | ||
| 528 | "baxycdaxycd")) | ||
| 529 | ;; START | ||
| 530 | (should (equal (replace-regexp-in-string | ||
| 531 | "ab" "x" "abcabdabeabf" nil nil nil 4) | ||
| 532 | "bdxexf")) | ||
| 533 | ;; An empty pattern matches once before every character. | ||
| 534 | (should (equal (replace-regexp-in-string "" "x" "abc") | ||
| 535 | "xaxbxc")) | ||
| 536 | (should (equal (replace-regexp-in-string "y*" "x" "abc") | ||
| 537 | "xaxbxc")) | ||
| 538 | ;; replacement function | ||
| 539 | (should (equal (replace-regexp-in-string | ||
| 540 | "a\\(b*\\)c" | ||
| 541 | (lambda (s) | ||
| 542 | (format "<%s,%s,%s,%s,%s>" | ||
| 543 | s | ||
| 544 | (match-beginning 0) (match-end 0) | ||
| 545 | (match-beginning 1) (match-end 1))) | ||
| 546 | "babbcaacabc") | ||
| 547 | "b<abbc,0,4,1,3>a<ac,0,2,1,1><abc,0,3,1,2>")) | ||
| 548 | ) | ||
| 549 | |||
| 487 | (provide 'subr-tests) | 550 | (provide 'subr-tests) |
| 488 | ;;; subr-tests.el ends here | 551 | ;;; subr-tests.el ends here |