summaryrefslogtreecommitdiff
path: root/test/lisp/emacs-lisp/rx-tests.el
diff options
context:
space:
mode:
authorNoam Postavsky <npostavs@gmail.com>2019-06-14 08:43:17 -0400
committerNoam Postavsky <npostavs@gmail.com>2019-06-25 22:00:03 -0400
commitb59ffd2290ff744ca4e7cc2748ba6b66fb2f99f1 (patch)
tree650ab12b77ba2cf9918ebc9bce586ce22ab7d52a /test/lisp/emacs-lisp/rx-tests.el
parent29babad7286bff235407e883a4ff61bae49a2e5e (diff)
downloademacs-b59ffd2290ff744ca4e7cc2748ba6b66fb2f99f1.tar.gz
emacs-b59ffd2290ff744ca4e7cc2748ba6b66fb2f99f1.tar.bz2
emacs-b59ffd2290ff744ca4e7cc2748ba6b66fb2f99f1.zip
Support (rx (and (regexp EXPR) (literal EXPR))) (Bug#36237)
* lisp/emacs-lisp/rx.el (rx-regexp): Allow non-string forms. (rx-constituents): Add literal constituent, which is like a plain STRING form, but allows arbitrary lisp expressions. (rx-literal): New function. (rx-compile-to-lisp): New variable. (rx--subforms): New helper function for handling subforms, including non-constant case. (rx-group-if, rx-and, rx-or, rx-=, rx->=, rx-repeat, rx-submatch) (rx-submatch-n, rx-kleene, rx-atomic-p): Use it to handle non-constant subforms. (rx): Document new form, wrap non-constant forms with concat call. * test/lisp/emacs-lisp/rx-tests.el (rx-tests--match): New macro. (rx-nonstring-expr, rx-nonstring-expr-non-greedy): New tests. * etc/NEWS: Announce changes.
Diffstat (limited to 'test/lisp/emacs-lisp/rx-tests.el')
-rw-r--r--test/lisp/emacs-lisp/rx-tests.el41
1 files changed, 41 insertions, 0 deletions
diff --git a/test/lisp/emacs-lisp/rx-tests.el b/test/lisp/emacs-lisp/rx-tests.el
index 6f392d616d1..bab71b522bb 100644
--- a/test/lisp/emacs-lisp/rx-tests.el
+++ b/test/lisp/emacs-lisp/rx-tests.el
@@ -115,5 +115,46 @@
;; Test zero-argument `seq'.
(should (equal (rx (seq)) "")))
+(defmacro rx-tests--match (regexp string &optional match)
+ (macroexp-let2 nil strexp string
+ `(ert-info ((format "Matching %S to %S" ',regexp ,strexp))
+ (should (string-match ,regexp ,strexp))
+ ,@(when match
+ `((should (equal (match-string 0 ,strexp) ,match)))))))
+
+(ert-deftest rx-nonstring-expr ()
+ (let ((bee "b")
+ (vowel "[aeiou]"))
+ (rx-tests--match (rx "a" (literal bee) "c") "abc")
+ (rx-tests--match (rx "a" (regexp bee) "c") "abc")
+ (rx-tests--match (rx "a" (or (regexp bee) "xy") "c") "abc")
+ (rx-tests--match (rx "a" (or "xy" (regexp bee)) "c") "abc")
+ (should-not (string-match (rx (or (regexp bee) "xy")) ""))
+ (rx-tests--match (rx "a" (= 3 (regexp bee)) "c") "abbbc")
+ (rx-tests--match (rx "x" (= 3 (regexp vowel)) "z") "xeoez")
+ (should-not (string-match (rx "x" (= 3 (regexp vowel)) "z") "xe[]z"))
+ (rx-tests--match (rx "x" (= 3 (literal vowel)) "z")
+ "x[aeiou][aeiou][aeiou]z")
+ (rx-tests--match (rx "x" (repeat 1 (regexp vowel)) "z") "xaz")
+ (rx-tests--match (rx "x" (repeat 1 2 (regexp vowel)) "z") "xaz")
+ (rx-tests--match (rx "x" (repeat 1 2 (regexp vowel)) "z") "xauz")
+ (rx-tests--match (rx "x" (>= 1 (regexp vowel)) "z") "xaiiz")
+ (rx-tests--match (rx "x" (** 1 2 (regexp vowel)) "z") "xaiz")
+ (rx-tests--match (rx "x" (group (regexp vowel)) "z") "xaz")
+ (rx-tests--match (rx "x" (group-n 1 (regexp vowel)) "z") "xaz")
+ (rx-tests--match (rx "x" (? (regexp vowel)) "z") "xz")))
+
+(ert-deftest rx-nonstring-expr-non-greedy ()
+ "`rx's greediness can't affect runtime regexp parts."
+ (let ((ad-min "[ad]*?")
+ (ad-max "[ad]*")
+ (ad "[ad]"))
+ (rx-tests--match (rx "c" (regexp ad-min) "a") "cdaaada" "cda")
+ (rx-tests--match (rx "c" (regexp ad-max) "a") "cdaaada" "cdaaada")
+ (rx-tests--match (rx "c" (minimal-match (regexp ad-max)) "a") "cdaaada" "cdaaada")
+ (rx-tests--match (rx "c" (maximal-match (regexp ad-min)) "a") "cdaaada" "cda")
+ (rx-tests--match (rx "c" (minimal-match (0+ (regexp ad))) "a") "cdaaada" "cda")
+ (rx-tests--match (rx "c" (maximal-match (0+ (regexp ad))) "a") "cdaaada" "cdaaada")))
+
(provide 'rx-tests)
;; rx-tests.el ends here.