diff options
Diffstat (limited to 'test/lisp/emacs-lisp/rx-tests.el')
-rw-r--r-- | test/lisp/emacs-lisp/rx-tests.el | 114 |
1 files changed, 102 insertions, 12 deletions
diff --git a/test/lisp/emacs-lisp/rx-tests.el b/test/lisp/emacs-lisp/rx-tests.el index 9c8628a8f26..e773ddf158e 100644 --- a/test/lisp/emacs-lisp/rx-tests.el +++ b/test/lisp/emacs-lisp/rx-tests.el @@ -41,19 +41,31 @@ (should (equal (rx "" (or "ab" nonl) "") "ab\\|."))) +;; FIXME: Extend tests for `or', `not' etc to cover char pattern combination, +;; including (syntax whitespace) and (syntax word). + (ert-deftest rx-or () - (should (equal (rx (or "ab" (| "c" nonl) "de")) - "ab\\|c\\|.\\|de")) + (should (equal (rx (or "ab" (| "cd" nonl) "de")) + "ab\\|cd\\|.\\|de")) (should (equal (rx (or "ab" "abc" ?a)) "\\(?:a\\(?:bc?\\)?\\)")) (should (equal (rx (or "ab" (| (or "abcd" "abcde")) (or "a" "abc"))) "\\(?:a\\(?:b\\(?:c\\(?:de?\\)?\\)?\\)?\\)")) (should (equal (rx (or "a" (eval (string ?a ?b)))) "\\(?:ab?\\)")) + (should (equal (rx (| nonl "ac") (| "bd" blank)) + "\\(?:.\\|ac\\)\\(?:bd\\|[[:blank:]]\\)")) (should (equal (rx (| nonl "a") (| "b" blank)) - "\\(?:.\\|a\\)\\(?:b\\|[[:blank:]]\\)")) + ".[b[:blank:]]")) (should (equal (rx (|)) - "\\`a\\`"))) + "\\`a\\`")) + (should (equal (rx (or "a" (not anychar) punct ?c "b" (not (not ?d)))) + "[a-d[:punct:]]")) + (should (equal (rx (or nonl ?\n)) + "[^z-a]")) + (should (equal (rx (or "ab" "a" "b" blank (syntax whitespace) word "z")) + "ab\\|[ab[:blank:]]\\|\\s-\\|[z[:word:]]")) + ) (ert-deftest rx-def-in-or () (rx-let ((a b) @@ -98,7 +110,21 @@ "[\177Å\211\326-\377]")) ;; Split range; \177-\377ÿ should not be optimized to \177-\377. (should (equal (rx (any "\177-\377" ?ÿ)) - "[\177ÿ\200-\377]"))) + "[\177ÿ\200-\377]")) + ;; Range between normal chars and raw bytes: must be split to be parsed + ;; correctly by the Emacs regexp engine. + (should (equal (rx (any (0 . #x3fffff) word) (any (?G . #x3fff9a) word) + (any (?Ü . #x3ffff2) word)) + (concat "[\0-\x3fff7f\x80-\xff[:word:]]" + "[G-\x3fff7f\x80-\x9a[:word:]]" + "[Ü-\x3fff7f\x80-\xf2[:word:]]"))) + ;; As above but with ranges in string form. For historical reasons, + ;; we special-case ASCII-to-raw ranges to exclude non-ASCII unicode. + (should (equal (rx (any "\x00-\xff" alpha) (any "G-\x9a" alpha) + (any "Ü-\xf2" alpha)) + (concat "[\0-\x7f\x80-\xff[:alpha:]]" + "[G-\x7f\x80-\x9a[:alpha:]]" + "[Ü-\x3fff7f\x80-\xf2[:alpha:]]")))) (ert-deftest rx-any () (should (equal (rx (any ?A (?C . ?D) "F-H" "J-L" "M" "N-P" "Q" "RS")) @@ -138,7 +164,7 @@ (should (equal (rx (any "-]^" ascii) (not (any "-]^" ascii))) "[]^[:ascii:]-][^]^[:ascii:]-]")) (should (equal (rx (any "^" lower upper) (not (any "^" lower upper))) - "[[:lower:]^[:upper:]][^^[:lower:][:upper:]]")) + "[[:lower:][:upper:]^][^^[:lower:][:upper:]]")) (should (equal (rx (any "-" lower upper) (not (any "-" lower upper))) "[[:lower:][:upper:]-][^[:lower:][:upper:]-]")) (should (equal (rx (any "]" lower upper) (not (any "]" lower upper))) @@ -165,7 +191,10 @@ "[a[:space:][:digit:]]")) (should (equal (rx (not "\n") (not ?\n) (not (any "\n")) (not-char ?\n) (| (not (in "a\n")) (not (char ?\n (?b . ?b))))) - "....."))) + ".....")) + (should (equal (rx (or (in "g-k") (in "a-f") (or ?r (in "i-m" "n-q")))) + "[a-r]")) + ) (ert-deftest rx-pcase () (should (equal (pcase "i18n" ((rx (let x (+ digit))) (list 'ok x))) @@ -274,7 +303,7 @@ "^\\`\\'\\`\\'\\`\\'\\`\\'$")) (should (equal (rx point word-start word-end bow eow symbol-start symbol-end word-boundary not-word-boundary not-wordchar) - "\\=\\<\\>\\<\\>\\_<\\_>\\b\\B\\W")) + "\\=\\<\\>\\<\\>\\_<\\_>\\b\\B[^[:word:]]")) (should (equal (rx digit numeric num control cntrl) "[[:digit:]][[:digit:]][[:digit:]][[:cntrl:]][[:cntrl:]]")) (should (equal (rx hex-digit hex xdigit blank) @@ -296,7 +325,7 @@ (should (equal (rx (syntax whitespace) (syntax punctuation) (syntax word) (syntax symbol) (syntax open-parenthesis) (syntax close-parenthesis)) - "\\s-\\s.\\sw\\s_\\s(\\s)")) + "\\s-\\s.\\w\\s_\\s(\\s)")) (should (equal (rx (syntax string-quote) (syntax paired-delimiter) (syntax escape) (syntax character-quote) (syntax comment-start) (syntax comment-end) @@ -344,8 +373,9 @@ "\\B")) (should (equal (rx (not ascii) (not lower-case) (not wordchar)) "[^[:ascii:]][^[:lower:]][^[:word:]]")) - (should (equal (rx (not (syntax punctuation)) (not (syntax escape))) - "\\S.\\S\\")) + (should (equal (rx (not (syntax punctuation)) (not (syntax escape)) + (not (syntax word))) + "\\S.\\S\\\\W")) (should (equal (rx (not (category tone-mark)) (not (category lao))) "\\C4\\Co")) (should (equal (rx (not (not ascii)) (not (not (not (any "a-z"))))) @@ -381,7 +411,16 @@ (should (equal (rx (or (not (in "abc")) (not (char "bcd")))) "[^bc]")) (should (equal (rx (or "x" (? "yz"))) - "x\\|\\(?:yz\\)?"))) + "x\\|\\(?:yz\\)?")) + (should (equal (rx (or anychar (not anychar))) + "[^z-a]")) + (should (equal (rx (or (not (in "a-p")) (not (in "k-u")))) + "[^k-p]")) + (should (equal (rx (or (not (in "a-p")) word (not (in "k-u")))) + "[\0-jq-\x3fff7f\x80-\xff[:word:]]")) + (should (equal (rx (or (in "a-f" blank) (in "c-z") blank)) + "[a-z[:blank:]]")) + ) (ert-deftest rx-def-in-charset-or () (rx-let ((a (any "badc")) @@ -600,6 +639,57 @@ (rx-submatch-n '(group-n 3 (+ nonl) eol))) "\\(?3:.+$\\)"))) +;;; unit tests for internal functions + +(ert-deftest rx--interval-set-complement () + (should (equal (rx--interval-set-complement '()) + '((0 . #x3fffff)))) + (should (equal (rx--interval-set-complement '((10 . 20) (30 . 40))) + '((0 . 9) (21 . 29) (41 . #x3fffff)))) + (should (equal (rx--interval-set-complement '((0 . #x3fffff))) + '())) + (should (equal (rx--interval-set-complement + '((0 . 10) (20 . 20) (30 . #x3fffff))) + '((11 . 19) (21 . 29))))) + +(ert-deftest rx--interval-set-union () + (should (equal (rx--interval-set-union '() '()) '())) + (should (equal (rx--interval-set-union '() '((10 . 20) (30 . 40))) + '((10 . 20) (30 . 40)))) + (should (equal (rx--interval-set-union '((10 . 20) (30 . 40)) '()) + '((10 . 20) (30 . 40)))) + (should (equal (rx--interval-set-union '((5 . 15) (18 . 24) (32 . 40)) + '((10 . 20) (30 . 40) (50 . 60))) + '((5 . 24) (30 . 40) (50 . 60)))) + (should (equal (rx--interval-set-union '((10 . 20) (30 . 40) (50 . 60)) + '((0 . 9) (21 . 29) (41 . 50))) + '((0 . 60)))) + (should (equal (rx--interval-set-union '((10 . 20) (30 . 40)) + '((12 . 18) (28 . 42))) + '((10 . 20) (28 . 42)))) + (should (equal (rx--interval-set-union '((10 . 20) (30 . 40)) + '((0 . #x3fffff))) + '((0 . #x3fffff))))) + +(ert-deftest rx--interval-set-intersection () + (should (equal (rx--interval-set-intersection '() '()) '())) + (should (equal (rx--interval-set-intersection '() '((10 . 20) (30 . 40))) + '())) + (should (equal (rx--interval-set-intersection '((10 . 20) (30 . 40)) '()) + '())) + (should (equal (rx--interval-set-intersection '((5 . 15) (18 . 24) (32 . 40)) + '((10 . 20) (30 . 40) (50 . 60))) + '((10 . 15) (18 . 20) (32 . 40)))) + (should (equal (rx--interval-set-intersection '((10 . 20) (30 . 40) (50 . 60)) + '((0 . 9) (21 . 29) (41 . 50))) + '((50 . 50)))) + (should (equal (rx--interval-set-intersection '((10 . 20) (30 . 40)) + '((12 . 18) (28 . 42))) + '((12 . 18) (30 . 40)))) + (should (equal (rx--interval-set-intersection '((10 . 20) (30 . 40)) + '((0 . #x3fffff))) + '((10 . 20) (30 . 40))))) + (provide 'rx-tests) ;;; rx-tests.el ends here |