From: Alan Mackenzie Date: Fri, 2 Oct 2020 21:21:06 +0000 (+0000) Subject: Enhance syntax-tests.el to test comments in parse-partial-sexp X-Git-Tag: emacs-28.0.90~5757 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=d22fe0ec3a321ea222ad2f25c52c7eddea4375df;p=emacs.git Enhance syntax-tests.el to test comments in parse-partial-sexp This now tests the interface between parse-partial-sexp and the low level comment function in syntax.c * test/src/syntax-tests.el (syntax-comments-midpoint): New function. (syntax-pps-comments): New macro. (top level): Two new syntax-br-comments tests and five new syntax-pps-comments tests. * test/data/syntax-comments.txt (top level): Amend some test fragments and add some more. --- diff --git a/test/data/syntax-comments.txt b/test/data/syntax-comments.txt index e10c1450e46..6f595e4d8dc 100644 --- a/test/data/syntax-comments.txt +++ b/test/data/syntax-comments.txt @@ -25,18 +25,19 @@ /* C Comments within lists */ 59}59 -50{ /* comment */ }50 +50{ /*70 comment */71 }50 51{ /**/ }51 -52{ // comment -}52 +52{ //72 comment +73}52 53{ // }53 -54{ // \ +54{ //74 \ }54 55{/* */}55 -56{ /* \*/ }56 -57*/57 +56{ /*76 \*/ }56 +57*/77 58}58 +60{ /*78 \\*/79}60 /* Straight Pascal comments (not nested) */ diff --git a/test/src/syntax-tests.el b/test/src/syntax-tests.el index 3b3521eaab0..56e03380579 100644 --- a/test/src/syntax-tests.el +++ b/test/src/syntax-tests.el @@ -85,11 +85,11 @@ also has open paren syntax (see Bug#24870)." ;;; Commentary: ;; The next bit tests the handling of comments in syntax.c, in -;; particular the functions `forward-comment' and `scan-lists' (in so -;; far as it relates to comments). +;; particular the functions `forward-comment' and `scan-lists' and +;; `parse-partial-sexp' (in so far as they relate to comments). -;; It is intended to enhance this bit to test nested comments and also -;; the interaction of `parse-partial-sexp' with comments (2020-10-01). +;; It is intended to enhance this bit to test nested comments +;; (2020-10-01). ;; This bit uses the data file test/data/syntax-comments.txt. @@ -128,6 +128,23 @@ line that the -N label is on. When it is zero, we return POINT." (progn (end-of-line) (point)) (match-beginning 2))))))) +(defun syntax-comments-midpoint (n) + "Return the buffer offset corresponding to the \"label\" N. +N is a positive decimal number which should appear in the buffer +exactly once. The label need not be at the beginning or end of a +line. + +The return value is the position just before the label. + +If the label N doesn't exist in the current buffer, an exception +is thrown." + (let ((str (format "%d" n))) + (save-excursion + (goto-char (point-min)) + (re-search-forward + (concat "\\(^\\|[^0-9]\\)\\(" str "\\)\\([^0-9\n]\\|$\\)")) + (match-beginning 2)))) + (eval-and-compile (defvar syntax-comments-section)) @@ -228,6 +245,64 @@ missing or nil, the value of -START- is assumed for it." :type 'scan-error))) (,(intern (concat (symbol-name type) "-out"))))))) +(defmacro syntax-pps-comments (-type- -start- open close &optional -stop-) + "Create an ERT test to test `parse-partial-sexp' with comments. +This is to test the interface between `parse-partial-sexp' and +the internal comment routines in syntax.c. + +The test uses a fixed name data file, which it visits. It calls +entry and exit functions to set up and tear down syntax entries +for comment and paren characters. The test is given a name based +on the global variable `syntax-comments-section', and the value +of -START-. + +The generated test calls `parse-partial-sexp' three times, the +first two with COMMENTSTOP set to `syntax-table' so as to stop +after the start and end of the comment. The third call is +expected to stop at the brace/paren matching the one where the +test started. + +-TYPE- (unquoted) is a symbol from whose name the entry and exit +function names are derived by appending \"-in\" and \"-out\". + +-START- and -STOP- are decimal numbers corresponding to labels in +the data file marking the start and expected stop positions. See +`syntax-comments-point' for a precise specification. If -STOP- +is missing or nil, the value of -START- is assumed for it. + +OPEN and CLOSE are decimal numbers corresponding to labels in the +data file marking just after the comment opener and closer where +the `parse-partial-sexp's are expected to stop. See +`syntax-comments-midpoint' for a precise specification." + (declare (debug t)) + (let* ((type -type-) + (start -start-) + (start-str (format "%d" start)) + (stop (or -stop- start))) + `(ert-deftest ,(intern (concat "syntax-pps-comments-" + syntax-comments-section + "-" start-str)) + () + (with-current-buffer + (find-file + ,(expand-file-name "data/syntax-comments.txt" + (getenv "EMACS_TEST_DIRECTORY"))) + (,(intern (concat (symbol-name type) "-in"))) + (let ((start-pos (syntax-comments-point ,start t)) + (open-pos (syntax-comments-midpoint ,open)) + (close-pos (syntax-comments-midpoint ,close)) + (stop-pos (syntax-comments-point ,stop nil)) + s) + (setq s (parse-partial-sexp + start-pos (point-max) 0 nil nil 'syntax-table)) + (should (eq (point) open-pos)) + (setq s (parse-partial-sexp + (point) (point-max) 0 nil s 'syntax-table)) + (should (eq (point) close-pos)) + (setq s (parse-partial-sexp (point) (point-max) 0 nil s)) + (should (eq (point) stop-pos))) + (,(intern (concat (symbol-name type) "-out"))))))) + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; "Pascal" style comments - single character delimiters, the closing ;; delimiter not being newline. @@ -346,5 +421,14 @@ missing or nil, the value of -START- is assumed for it." (syntax-br-comments /* forward t 56 58) (syntax-br-comments /* backward t 58 56) (syntax-br-comments /* backward nil 59) +(syntax-br-comments /* forward t 60) +(syntax-br-comments /* backward t 60) + +;; Emacs 27 "C" style comments parsed by `parse-partial-sexp'. +(syntax-pps-comments /* 50 70 71) +(syntax-pps-comments /* 52 72 73) +(syntax-pps-comments /* 54 74 55 20) +(syntax-pps-comments /* 56 76 77 58) +(syntax-pps-comments /* 60 78 79) ;;; syntax-tests.el ends here