aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorNoam Postavsky2019-06-14 08:43:17 -0400
committerNoam Postavsky2019-06-25 22:00:03 -0400
commitb59ffd2290ff744ca4e7cc2748ba6b66fb2f99f1 (patch)
tree650ab12b77ba2cf9918ebc9bce586ce22ab7d52a /test
parent29babad7286bff235407e883a4ff61bae49a2e5e (diff)
downloademacs-b59ffd2290ff744ca4e7cc2748ba6b66fb2f99f1.tar.gz
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')
-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 @@
115 ;; Test zero-argument `seq'. 115 ;; Test zero-argument `seq'.
116 (should (equal (rx (seq)) ""))) 116 (should (equal (rx (seq)) "")))
117 117
118(defmacro rx-tests--match (regexp string &optional match)
119 (macroexp-let2 nil strexp string
120 `(ert-info ((format "Matching %S to %S" ',regexp ,strexp))
121 (should (string-match ,regexp ,strexp))
122 ,@(when match
123 `((should (equal (match-string 0 ,strexp) ,match)))))))
124
125(ert-deftest rx-nonstring-expr ()
126 (let ((bee "b")
127 (vowel "[aeiou]"))
128 (rx-tests--match (rx "a" (literal bee) "c") "abc")
129 (rx-tests--match (rx "a" (regexp bee) "c") "abc")
130 (rx-tests--match (rx "a" (or (regexp bee) "xy") "c") "abc")
131 (rx-tests--match (rx "a" (or "xy" (regexp bee)) "c") "abc")
132 (should-not (string-match (rx (or (regexp bee) "xy")) ""))
133 (rx-tests--match (rx "a" (= 3 (regexp bee)) "c") "abbbc")
134 (rx-tests--match (rx "x" (= 3 (regexp vowel)) "z") "xeoez")
135 (should-not (string-match (rx "x" (= 3 (regexp vowel)) "z") "xe[]z"))
136 (rx-tests--match (rx "x" (= 3 (literal vowel)) "z")
137 "x[aeiou][aeiou][aeiou]z")
138 (rx-tests--match (rx "x" (repeat 1 (regexp vowel)) "z") "xaz")
139 (rx-tests--match (rx "x" (repeat 1 2 (regexp vowel)) "z") "xaz")
140 (rx-tests--match (rx "x" (repeat 1 2 (regexp vowel)) "z") "xauz")
141 (rx-tests--match (rx "x" (>= 1 (regexp vowel)) "z") "xaiiz")
142 (rx-tests--match (rx "x" (** 1 2 (regexp vowel)) "z") "xaiz")
143 (rx-tests--match (rx "x" (group (regexp vowel)) "z") "xaz")
144 (rx-tests--match (rx "x" (group-n 1 (regexp vowel)) "z") "xaz")
145 (rx-tests--match (rx "x" (? (regexp vowel)) "z") "xz")))
146
147(ert-deftest rx-nonstring-expr-non-greedy ()
148 "`rx's greediness can't affect runtime regexp parts."
149 (let ((ad-min "[ad]*?")
150 (ad-max "[ad]*")
151 (ad "[ad]"))
152 (rx-tests--match (rx "c" (regexp ad-min) "a") "cdaaada" "cda")
153 (rx-tests--match (rx "c" (regexp ad-max) "a") "cdaaada" "cdaaada")
154 (rx-tests--match (rx "c" (minimal-match (regexp ad-max)) "a") "cdaaada" "cdaaada")
155 (rx-tests--match (rx "c" (maximal-match (regexp ad-min)) "a") "cdaaada" "cda")
156 (rx-tests--match (rx "c" (minimal-match (0+ (regexp ad))) "a") "cdaaada" "cda")
157 (rx-tests--match (rx "c" (maximal-match (0+ (regexp ad))) "a") "cdaaada" "cdaaada")))
158
118(provide 'rx-tests) 159(provide 'rx-tests)
119;; rx-tests.el ends here. 160;; rx-tests.el ends here.