Allow 'browse-url-emacs' to fetch URL in the selected window
[emacs.git] / test / lisp / thingatpt-tests.el
blobcfb57de6189fa067ce04a6cc3ca555773a9ffea6
1 ;;; thingatpt.el --- tests for thing-at-point.
3 ;; Copyright (C) 2013-2018 Free Software Foundation, Inc.
5 ;; This file is part of GNU Emacs.
7 ;; GNU Emacs is free software: you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation, either version 3 of the License, or
10 ;; (at your option) any later version.
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
20 ;;; Code:
22 (require 'ert)
24 (defvar thing-at-point-test-data
25 '(("https://1.gnu.org" 1 url "https://1.gnu.org")
26 ("https://2.gnu.org" 6 url "https://2.gnu.org")
27 ("https://3.gnu.org" 19 url "https://3.gnu.org")
28 ("https://4.gnu.org" 1 url "https://4.gnu.org")
29 ("A geo URI (geo:3.14159,-2.71828)." 12 url "geo:3.14159,-2.71828")
30 ("Visit https://5.gnu.org now." 5 url nil)
31 ("Visit https://6.gnu.org now." 7 url "https://6.gnu.org")
32 ("Visit https://7.gnu.org now." 22 url "https://7.gnu.org")
33 ("Visit https://8.gnu.org now." 22 url "https://8.gnu.org")
34 ("Visit https://9.gnu.org now." 25 url nil)
35 ;; Invalid URIs
36 ("<<<<" 2 url nil)
37 ("<>" 1 url nil)
38 ("<url:>" 1 url nil)
39 ("http://" 1 url nil)
40 ;; Invalid schema
41 ("foo://www.gnu.org" 1 url nil)
42 ("foohttp://www.gnu.org" 1 url nil)
43 ;; Non alphanumeric characters can be found in URIs
44 ("ftp://example.net/~foo!;#bar=baz&goo=bob" 3 url "ftp://example.net/~foo!;#bar=baz&goo=bob")
45 ("bzr+ssh://user@example.net:5/a%20d,5" 34 url "bzr+ssh://user@example.net:5/a%20d,5")
46 ;; <url:...> markup
47 ("Url: <url:foo://1.example.com>..." 8 url "foo://1.example.com")
48 ("Url: <url:foo://2.example.com>..." 30 url "foo://2.example.com")
49 ("Url: <url:foo://www.gnu.org/a bc>..." 20 url "foo://www.gnu.org/a bc")
50 ;; Hack used by thing-at-point: drop punctuation at end of URI.
51 ("Go to https://www.gnu.org, for details" 7 url "https://www.gnu.org")
52 ("Go to https://www.gnu.org." 24 url "https://www.gnu.org")
53 ;; Standard URI delimiters
54 ("Go to \"https://10.gnu.org\"." 8 url "https://10.gnu.org")
55 ("Go to \"https://11.gnu.org/\"." 26 url "https://11.gnu.org/")
56 ("Go to <https://12.gnu.org> now." 8 url "https://12.gnu.org")
57 ("Go to <https://13.gnu.org> now." 24 url "https://13.gnu.org")
58 ;; Parenthesis handling (non-standard)
59 ("http://example.com/a(b)c" 21 url "http://example.com/a(b)c")
60 ("http://example.com/a(b)" 21 url "http://example.com/a(b)")
61 ("(http://example.com/abc)" 2 url "http://example.com/abc")
62 ("This (http://example.com/a(b))" 7 url "http://example.com/a(b)")
63 ("This (http://example.com/a(b))" 30 url "http://example.com/a(b)")
64 ("This (http://example.com/a(b))" 5 url nil)
65 ("http://example.com/ab)c" 4 url "http://example.com/ab)c")
66 ;; URL markup, lacking schema
67 ("<url:foo@example.com>" 1 url "mailto:foo@example.com")
68 ("<url:ftp.example.net/abc/>" 1 url "ftp://ftp.example.net/abc/"))
69 "List of thing-at-point tests.
70 Each list element should have the form
72 (STRING POS THING RESULT)
74 where STRING is a string of buffer contents, POS is the value of
75 point, THING is a symbol argument for `thing-at-point', and
76 RESULT should be the result of calling `thing-at-point' from that
77 position to retrieve THING.")
79 (ert-deftest thing-at-point-tests ()
80 "Test the file-local variables implementation."
81 (dolist (test thing-at-point-test-data)
82 (with-temp-buffer
83 (insert (nth 0 test))
84 (goto-char (nth 1 test))
85 (should (equal (thing-at-point (nth 2 test)) (nth 3 test))))))
87 ;; These tests reflect the actual behavior of
88 ;; `thing-at-point-bounds-of-list-at-point'.
89 (ert-deftest thing-at-point-bug24627 ()
90 "Test for https://debbugs.gnu.org/24627 ."
91 (let ((string-result '(("(a \"b\" c)" . (a "b" c))
92 (";(a \"b\" c)")
93 ("(a \"b\" c\n)" . (a "b" c))
94 ("\"(a b c)\"")
95 ("(a ;(b c d)\ne)" . (a e))
96 ("(foo\n(a ;(b c d)\ne) bar)" . (a e))
97 ("(foo\na ;(b c d)\ne bar)" . (foo a e bar))
98 ("(foo\n(a \"(b c d)\"\ne) bar)" . (a "(b c d)" e))
99 ("(b\n(a ;(foo c d)\ne) bar)" . (a e))
100 ("(princ \"(a b c)\")" . (princ "(a b c)"))
101 ("(defun foo ()\n \"Test function.\"\n ;;(a b)\n nil)" . (defun foo nil "Test function." nil))))
102 (file
103 (expand-file-name "lisp/thingatpt.el" source-directory))
104 buf)
105 ;; Test for `thing-at-point'.
106 (when (file-exists-p file)
107 (unwind-protect
108 (progn
109 (setq buf (find-file file))
110 (goto-char (point-max))
111 (forward-line -1)
112 (should-not (thing-at-point 'list)))
113 (kill-buffer buf)))
114 ;; Tests for `list-at-point'.
115 (dolist (str-res string-result)
116 (with-temp-buffer
117 (emacs-lisp-mode)
118 (insert (car str-res))
119 (re-search-backward "\\((a\\|^a\\)")
120 (should (equal (list-at-point)
121 (cdr str-res)))))))
123 (ert-deftest thing-at-point-url-in-comment ()
124 (with-temp-buffer
125 (c-mode)
126 (insert "/* (http://foo/bar)\n(http://foo/bar(baz)) */\n")
127 (goto-char 6)
128 (should (equal (thing-at-point 'url) "http://foo/bar"))
129 (goto-char 23)
130 (should (equal (thing-at-point 'url) "http://foo/bar(baz)"))))
132 ;;; thingatpt.el ends here