unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#16078: Extensive docs and tests for `ruby-forward-string' (PATCH)
@ 2013-12-06 17:15 Cameron Desautels
  2013-12-07  3:22 ` Dmitry Gutov
  0 siblings, 1 reply; 7+ messages in thread
From: Cameron Desautels @ 2013-12-06 17:15 UTC (permalink / raw)
  To: 16078

[-- Attachment #1: Type: text/plain, Size: 610 bytes --]

The attached patch adds extensive documentation and tests to the
`ruby-forward-string' function.

This may seem an odd function to document thoroughly, but I spent
quite a while wrapping my head around the exact behavior and I want to
spare the next person.  It also underlies some important parsing
functionality in ruby-mode.

Note the one test which is expected to fail: this represents an
outstanding bug in `ruby-forward-string`.  I'll be (immediately)
following this report with a patch which fixes *that* issue, but it
didn't seem prudent to combine the commits.
--
Cameron Desautels <camdez@gmail.com>

[-- Attachment #2: ruby-forward-string-docs-and-tests.diff --]
[-- Type: text/plain, Size: 4148 bytes --]

*** lisp/progmodes/ruby-mode.el.orig	2013-12-06 10:56:52.000000000 -0600
--- lisp/progmodes/ruby-mode.el     	2013-12-06 10:54:07.000000000 -0600
***************
*** 791,797 ****
                    (t nil)))))))))

  (defun ruby-forward-string (term &optional end no-error expand)
!   "TODO: document."
    (let ((n 1) (c (string-to-char term))
          (re (if expand
                  (concat "[^\\]\\(\\\\\\\\\\)*\\([" term "]\\|\\(#{\\)\\)")
--- 791,811 ----
                    (t nil)))))))))

  (defun ruby-forward-string (term &optional end no-error expand)
!   "Move forward across one balanced pair of string delimiters.
! Skips escaped delimiters. If EXPAND is non-nil, also ignores
! delimiters in interpolated strings.
!
! TERM should be a string containing either a single, self-matching
! delimiter (e.g. \"/\"), or a pair of matching delimiters with the
! close delimiter first (e.g. \"][\").
!
! When non-nil, search is bounded by position END.
!
! Throws an error if a balanced match is not found, unless NO-ERROR
! is non-nil, in which case nil will be returned.
!
! This command assumes the character after point is an opening
! delimiter."
    (let ((n 1) (c (string-to-char term))
          (re (if expand
                  (concat "[^\\]\\(\\\\\\\\\\)*\\([" term "]\\|\\(#{\\)\\)")
*** test/automated/ruby-mode-tests.el.orig	2013-12-06 10:56:52.000000000 -0600
--- test/automated/ruby-mode-tests.el     	2013-12-06 10:54:07.000000000 -0600
***************
*** 639,644 ****
--- 639,695 ----
        (ruby--insert-coding-comment "utf-8")
        (should (string= "# encoding: utf-8\n\n" (buffer-string))))))

+ (defun ruby-forward-string-should-move-to (content term index &optional expand)
+   "Assert that `ruby-forward-string', called on buffer containing
+ CONTENT, passing TERM and EXPAND, leaves point at INDEX.
+
+ Pass nil for INDEX if an error should be expected."
+   (ruby-with-temp-buffer content
+     (goto-char (point-min))
+     (if (ruby-forward-string term nil t expand)
+         (should (= (point) index))
+       (should (null index)))))
+
+ (ert-deftest ruby-forward-string-accepts-paired-delimiters ()
+   (ruby-forward-string-should-move-to "<foo>bar" "><" 6)
+   (ruby-forward-string-should-move-to "[foo]bar" "][" 6)
+   (ruby-forward-string-should-move-to "(foo)bar" ")(" 6))
+
+ (ert-deftest ruby-forward-string-accepts-single-delimiters ()
+   (ruby-forward-string-should-move-to "/foo/bar" "/" 6)
+   (ruby-forward-string-should-move-to "|foo|bar" "|" 6)
+   (ruby-forward-string-should-move-to "-foo-bar" "-" 6))
+
+ (ert-deftest ruby-forward-string-accepts-carets ()
+   :expected-result :failed
+   (ruby-forward-string-should-move-to "^foo^bar" "^" 6))
+
+ (ert-deftest ruby-forward-string-scans-the-shortest-match ()
+   (ruby-forward-string-should-move-to "<foo>"   "><" 6)
+   (ruby-forward-string-should-move-to "<foo>>"  "><" 6)
+   (ruby-forward-string-should-move-to "<foo><>" "><" 6))
+
+ (ert-deftest ruby-forward-string-skips-escaped-delimiters ()
+   (ruby-forward-string-should-move-to "<foo\\>"   "><" nil)
+   (ruby-forward-string-should-move-to "<foo\\>>"  "><" 8)
+   (ruby-forward-string-should-move-to "/foo\\/"   "/"  nil)
+   (ruby-forward-string-should-move-to "/foo\\//"  "/"  8)
+   (ruby-forward-string-should-move-to "/foo\\\\/" "/"  8))
+
+ (ert-deftest ruby-forward-string-requires-matched-delimiters ()
+   (ruby-forward-string-should-move-to "<foo"    "><" nil)
+   (ruby-forward-string-should-move-to "<foo<"   "><" nil)
+   (ruby-forward-string-should-move-to "<foo<>"  "><" nil)
+   (ruby-forward-string-should-move-to "<foo<>>" "><" 8)
+   (ruby-forward-string-should-move-to "<<><>>"  "><" 7)
+   (ruby-forward-string-should-move-to "><"      "><" nil)
+   (ruby-forward-string-should-move-to "/foo"    "/"  nil))
+
+ (ert-deftest ruby-forward-string-can-skip-interpolations ()
+   (ruby-forward-string-should-move-to "<f#{>}o>" "><" 6 nil)
+   (ruby-forward-string-should-move-to "<f#{>}o>" "><" 9 t)
+   (ruby-forward-string-should-move-to "/f#{/}o/" "/"  6 nil)
+   (ruby-forward-string-should-move-to "/f#{/}o/" "/"  9 t))

  (provide 'ruby-mode-tests)

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2015-11-15 14:55 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-12-06 17:15 bug#16078: Extensive docs and tests for `ruby-forward-string' (PATCH) Cameron Desautels
2013-12-07  3:22 ` Dmitry Gutov
2013-12-08  2:29   ` Cameron Desautels
2013-12-09  4:07     ` Dmitry Gutov
2013-12-11 22:48       ` Cameron Desautels
2013-12-15  3:36         ` Dmitry Gutov
2015-11-15 14:55           ` Dmitry Gutov

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).