aboutsummaryrefslogtreecommitdiffstats
path: root/test/automated/python-tests.el
diff options
context:
space:
mode:
authorRichard Stallman2015-04-05 08:36:56 -0400
committerRichard Stallman2015-04-05 08:42:43 -0400
commit4e23cd0ccde4ad1e14fe2870ccf140487af649b2 (patch)
treeb709ac1e92a892f6ec1faa85eb59a9e5960c25dd /test/automated/python-tests.el
parentdca743f0941909a80e3f28c023977120b6203e20 (diff)
parent16eec6fc55dcc05d1d819f18998e84a9580b2521 (diff)
downloademacs-4e23cd0ccde4ad1e14fe2870ccf140487af649b2.tar.gz
emacs-4e23cd0ccde4ad1e14fe2870ccf140487af649b2.zip
* mail/rmail.el (rmail-show-message-1): When displaying a mime message,
indicate start and finish in the echo area. * mail/rmail.el (rmail-epa-decrypt): Disregard <pre> before armor. Ignore more kinds of whitespace in mime headers. Modify the decrypted mime part's mime type so it will be displayed by default when visiting this message again. * net/browse-url.el (browse-url-firefox-program): Prefer IceCat, doc. (browse-url-firefox-arguments) (browse-url-firefox-startup-arguments): Doc fix.
Diffstat (limited to 'test/automated/python-tests.el')
-rw-r--r--test/automated/python-tests.el359
1 files changed, 297 insertions, 62 deletions
diff --git a/test/automated/python-tests.el b/test/automated/python-tests.el
index 672b05c39de..b377a26f77a 100644
--- a/test/automated/python-tests.el
+++ b/test/automated/python-tests.el
@@ -24,6 +24,11 @@
24(require 'ert) 24(require 'ert)
25(require 'python) 25(require 'python)
26 26
27;; Dependencies for testing:
28(require 'electric)
29(require 'hideshow)
30
31
27(defmacro python-tests-with-temp-buffer (contents &rest body) 32(defmacro python-tests-with-temp-buffer (contents &rest body)
28 "Create a `python-mode' enabled temp buffer with CONTENTS. 33 "Create a `python-mode' enabled temp buffer with CONTENTS.
29BODY is code to be executed within the temp buffer. Point is 34BODY is code to be executed within the temp buffer. Point is
@@ -104,6 +109,28 @@ STRING, it is skipped so the next STRING occurrence is selected."
104 (call-interactively 'self-insert-command))) 109 (call-interactively 'self-insert-command)))
105 chars))) 110 chars)))
106 111
112(defun python-tests-visible-string (&optional min max)
113 "Return the buffer string excluding invisible overlays.
114Argument MIN and MAX delimit the region to be returned and
115default to `point-min' and `point-max' respectively."
116 (let* ((min (or min (point-min)))
117 (max (or max (point-max)))
118 (buffer (current-buffer))
119 (buffer-contents (buffer-substring-no-properties min max))
120 (overlays
121 (sort (overlays-in min max)
122 (lambda (a b)
123 (let ((overlay-end-a (overlay-end a))
124 (overlay-end-b (overlay-end b)))
125 (> overlay-end-a overlay-end-b))))))
126 (with-temp-buffer
127 (insert buffer-contents)
128 (dolist (overlay overlays)
129 (if (overlay-get overlay 'invisible)
130 (delete-region (overlay-start overlay)
131 (overlay-end overlay))))
132 (buffer-substring-no-properties (point-min) (point-max)))))
133
107 134
108;;; Tests for your tests, so you can test while you test. 135;;; Tests for your tests, so you can test while you test.
109 136
@@ -177,7 +204,7 @@ foo = long_function_name(var_one, var_two,
177 (should (eq (car (python-indent-context)) :no-indent)) 204 (should (eq (car (python-indent-context)) :no-indent))
178 (should (= (python-indent-calculate-indentation) 0)) 205 (should (= (python-indent-calculate-indentation) 0))
179 (python-tests-look-at "foo = long_function_name(var_one, var_two,") 206 (python-tests-look-at "foo = long_function_name(var_one, var_two,")
180 (should (eq (car (python-indent-context)) :after-line)) 207 (should (eq (car (python-indent-context)) :after-comment))
181 (should (= (python-indent-calculate-indentation) 0)) 208 (should (= (python-indent-calculate-indentation) 0))
182 (python-tests-look-at "var_three, var_four)") 209 (python-tests-look-at "var_three, var_four)")
183 (should (eq (car (python-indent-context)) :inside-paren)) 210 (should (eq (car (python-indent-context)) :inside-paren))
@@ -195,7 +222,7 @@ def long_function_name(
195 (should (eq (car (python-indent-context)) :no-indent)) 222 (should (eq (car (python-indent-context)) :no-indent))
196 (should (= (python-indent-calculate-indentation) 0)) 223 (should (= (python-indent-calculate-indentation) 0))
197 (python-tests-look-at "def long_function_name(") 224 (python-tests-look-at "def long_function_name(")
198 (should (eq (car (python-indent-context)) :after-line)) 225 (should (eq (car (python-indent-context)) :after-comment))
199 (should (= (python-indent-calculate-indentation) 0)) 226 (should (= (python-indent-calculate-indentation) 0))
200 (python-tests-look-at "var_one, var_two, var_three,") 227 (python-tests-look-at "var_one, var_two, var_three,")
201 (should (eq (car (python-indent-context)) 228 (should (eq (car (python-indent-context))
@@ -221,7 +248,7 @@ foo = long_function_name(
221 (should (eq (car (python-indent-context)) :no-indent)) 248 (should (eq (car (python-indent-context)) :no-indent))
222 (should (= (python-indent-calculate-indentation) 0)) 249 (should (= (python-indent-calculate-indentation) 0))
223 (python-tests-look-at "foo = long_function_name(") 250 (python-tests-look-at "foo = long_function_name(")
224 (should (eq (car (python-indent-context)) :after-line)) 251 (should (eq (car (python-indent-context)) :after-comment))
225 (should (= (python-indent-calculate-indentation) 0)) 252 (should (= (python-indent-calculate-indentation) 0))
226 (python-tests-look-at "var_one, var_two,") 253 (python-tests-look-at "var_one, var_two,")
227 (should (eq (car (python-indent-context)) :inside-paren-newline-start)) 254 (should (eq (car (python-indent-context)) :inside-paren-newline-start))
@@ -286,10 +313,10 @@ class Blag(object):
286def func(arg): 313def func(arg):
287 # I don't do much 314 # I don't do much
288 return arg 315 return arg
289 # This comment is badly indented just because. 316 # This comment is badly indented because the user forced so.
290 # But we won't mess with the user in this line. 317 # At this line python.el wont dedent, user is always right.
291 318
292now_we_do_mess_cause_this_is_not_a_comment = 1 319comment_wins_over_ender = True
293 320
294# yeah, that. 321# yeah, that.
295" 322"
@@ -301,28 +328,49 @@ now_we_do_mess_cause_this_is_not_a_comment = 1
301 ;; the rules won't apply here. 328 ;; the rules won't apply here.
302 (should (eq (car (python-indent-context)) :after-block-start)) 329 (should (eq (car (python-indent-context)) :after-block-start))
303 (should (= (python-indent-calculate-indentation) 4)) 330 (should (= (python-indent-calculate-indentation) 4))
304 (python-tests-look-at "# This comment is badly") 331 (python-tests-look-at "# This comment is badly indented")
305 (should (eq (car (python-indent-context)) :after-block-end)) 332 (should (eq (car (python-indent-context)) :after-block-end))
306 ;; The return keyword moves indentation backwards 4 spaces, but 333 ;; The return keyword do make indentation lose a level...
307 ;; let's assume this comment was placed there because the user
308 ;; wanted to (manually adding spaces or whatever).
309 (should (= (python-indent-calculate-indentation) 0)) 334 (should (= (python-indent-calculate-indentation) 0))
310 (python-tests-look-at "# but we won't mess") 335 ;; ...but the current indentation was forced by the user.
336 (python-tests-look-at "# At this line python.el wont dedent")
311 (should (eq (car (python-indent-context)) :after-comment)) 337 (should (eq (car (python-indent-context)) :after-comment))
312 (should (= (python-indent-calculate-indentation) 4)) 338 (should (= (python-indent-calculate-indentation) 4))
313 ;; Behave the same for blank lines: potentially a comment. 339 ;; Should behave the same for blank lines: potentially a comment.
314 (forward-line 1) 340 (forward-line 1)
315 (should (eq (car (python-indent-context)) :after-comment)) 341 (should (eq (car (python-indent-context)) :after-comment))
316 (should (= (python-indent-calculate-indentation) 4)) 342 (should (= (python-indent-calculate-indentation) 4))
317 (python-tests-look-at "now_we_do_mess") 343 (python-tests-look-at "comment_wins_over_ender")
318 ;; Here is where comment indentation starts to get ignored and 344 ;; The comment won over the ender because the user said so.
319 ;; where the user can't freely indent anymore. 345 (should (eq (car (python-indent-context)) :after-comment))
320 (should (eq (car (python-indent-context)) :after-block-end)) 346 (should (= (python-indent-calculate-indentation) 4))
321 (should (= (python-indent-calculate-indentation) 0)) 347 ;; The indentation calculated fine for the assignment, but the user
348 ;; choose to force it back to the first column. Next line should
349 ;; be aware of that.
322 (python-tests-look-at "# yeah, that.") 350 (python-tests-look-at "# yeah, that.")
323 (should (eq (car (python-indent-context)) :after-line)) 351 (should (eq (car (python-indent-context)) :after-line))
324 (should (= (python-indent-calculate-indentation) 0)))) 352 (should (= (python-indent-calculate-indentation) 0))))
325 353
354(ert-deftest python-indent-after-comment-3 ()
355 "Test after-comment in buggy case."
356 (python-tests-with-temp-buffer
357 "
358class A(object):
359
360 def something(self, arg):
361 if True:
362 return arg
363
364 # A comment
365
366 @adecorator
367 def method(self, a, b):
368 pass
369"
370 (python-tests-look-at "@adecorator")
371 (should (eq (car (python-indent-context)) :after-comment))
372 (should (= (python-indent-calculate-indentation) 4))))
373
326(ert-deftest python-indent-inside-paren-1 () 374(ert-deftest python-indent-inside-paren-1 ()
327 "The most simple inside-paren case that shouldn't fail." 375 "The most simple inside-paren case that shouldn't fail."
328 (python-tests-with-temp-buffer 376 (python-tests-with-temp-buffer
@@ -2106,6 +2154,55 @@ if True:
2106 (call-interactively #'python-indent-dedent-line-backspace) 2154 (call-interactively #'python-indent-dedent-line-backspace)
2107 (should (zerop (current-indentation))))) 2155 (should (zerop (current-indentation)))))
2108 2156
2157(ert-deftest python-indent-dedent-line-backspace-2 ()
2158 "Check de-indentation with tabs. Bug#19730."
2159 (let ((tab-width 8))
2160 (python-tests-with-temp-buffer
2161 "
2162if x:
2163\tabcdefg
2164"
2165 (python-tests-look-at "abcdefg")
2166 (goto-char (line-end-position))
2167 (call-interactively #'python-indent-dedent-line-backspace)
2168 (should
2169 (string= (buffer-substring-no-properties
2170 (line-beginning-position) (line-end-position))
2171 "\tabcdef")))))
2172
2173(ert-deftest python-indent-dedent-line-backspace-3 ()
2174 "Paranoid check of de-indentation with tabs. Bug#19730."
2175 (let ((tab-width 8))
2176 (python-tests-with-temp-buffer
2177 "
2178if x:
2179\tif y:
2180\t abcdefg
2181"
2182 (python-tests-look-at "abcdefg")
2183 (goto-char (line-end-position))
2184 (call-interactively #'python-indent-dedent-line-backspace)
2185 (should
2186 (string= (buffer-substring-no-properties
2187 (line-beginning-position) (line-end-position))
2188 "\t abcdef"))
2189 (back-to-indentation)
2190 (call-interactively #'python-indent-dedent-line-backspace)
2191 (should
2192 (string= (buffer-substring-no-properties
2193 (line-beginning-position) (line-end-position))
2194 "\tabcdef"))
2195 (call-interactively #'python-indent-dedent-line-backspace)
2196 (should
2197 (string= (buffer-substring-no-properties
2198 (line-beginning-position) (line-end-position))
2199 " abcdef"))
2200 (call-interactively #'python-indent-dedent-line-backspace)
2201 (should
2202 (string= (buffer-substring-no-properties
2203 (line-beginning-position) (line-end-position))
2204 "abcdef")))))
2205
2109 2206
2110;;; Shell integration 2207;;; Shell integration
2111 2208
@@ -2916,6 +3013,63 @@ class Foo(models.Model):
2916 3013
2917;;; Eldoc 3014;;; Eldoc
2918 3015
3016(ert-deftest python-eldoc--get-symbol-at-point-1 ()
3017 "Test paren handling."
3018 (python-tests-with-temp-buffer
3019 "
3020map(xx
3021map(codecs.open('somefile'
3022"
3023 (python-tests-look-at "ap(xx")
3024 (should (string= (python-eldoc--get-symbol-at-point) "map"))
3025 (goto-char (line-end-position))
3026 (should (string= (python-eldoc--get-symbol-at-point) "map"))
3027 (python-tests-look-at "('somefile'")
3028 (should (string= (python-eldoc--get-symbol-at-point) "map"))
3029 (goto-char (line-end-position))
3030 (should (string= (python-eldoc--get-symbol-at-point) "codecs.open"))))
3031
3032(ert-deftest python-eldoc--get-symbol-at-point-2 ()
3033 "Ensure self is replaced with the class name."
3034 (python-tests-with-temp-buffer
3035 "
3036class TheClass:
3037
3038 def some_method(self, n):
3039 return n
3040
3041 def other(self):
3042 return self.some_method(1234)
3043
3044"
3045 (python-tests-look-at "self.some_method")
3046 (should (string= (python-eldoc--get-symbol-at-point)
3047 "TheClass.some_method"))
3048 (python-tests-look-at "1234)")
3049 (should (string= (python-eldoc--get-symbol-at-point)
3050 "TheClass.some_method"))))
3051
3052(ert-deftest python-eldoc--get-symbol-at-point-3 ()
3053 "Ensure symbol is found when point is at end of buffer."
3054 (python-tests-with-temp-buffer
3055 "
3056some_symbol
3057
3058"
3059 (goto-char (point-max))
3060 (should (string= (python-eldoc--get-symbol-at-point)
3061 "some_symbol"))))
3062
3063(ert-deftest python-eldoc--get-symbol-at-point-4 ()
3064 "Ensure symbol is found when point is at whitespace."
3065 (python-tests-with-temp-buffer
3066 "
3067some_symbol some_other_symbol
3068"
3069 (python-tests-look-at " some_other_symbol")
3070 (should (string= (python-eldoc--get-symbol-at-point)
3071 "some_symbol"))))
3072
2919 3073
2920;;; Imenu 3074;;; Imenu
2921 3075
@@ -4358,12 +4512,11 @@ def foo(a, b, c):
4358;;; Electricity 4512;;; Electricity
4359 4513
4360(ert-deftest python-parens-electric-indent-1 () 4514(ert-deftest python-parens-electric-indent-1 ()
4361 (require 'electric)
4362 (let ((eim electric-indent-mode)) 4515 (let ((eim electric-indent-mode))
4363 (unwind-protect 4516 (unwind-protect
4364 (progn 4517 (progn
4365 (python-tests-with-temp-buffer 4518 (python-tests-with-temp-buffer
4366 " 4519 "
4367from django.conf.urls import patterns, include, url 4520from django.conf.urls import patterns, include, url
4368 4521
4369from django.contrib import admin 4522from django.contrib import admin
@@ -4375,66 +4528,148 @@ urlpatterns = patterns('',
4375 url(r'^$', views.index 4528 url(r'^$', views.index
4376) 4529)
4377" 4530"
4378 (electric-indent-mode 1) 4531 (electric-indent-mode 1)
4379 (python-tests-look-at "views.index") 4532 (python-tests-look-at "views.index")
4380 (end-of-line) 4533 (end-of-line)
4381 4534
4382 ;; Inserting commas within the same line should leave 4535 ;; Inserting commas within the same line should leave
4383 ;; indentation unchanged. 4536 ;; indentation unchanged.
4384 (python-tests-self-insert ",") 4537 (python-tests-self-insert ",")
4385 (should (= (current-indentation) 4)) 4538 (should (= (current-indentation) 4))
4386 4539
4387 ;; As well as any other input happening within the same 4540 ;; As well as any other input happening within the same
4388 ;; set of parens. 4541 ;; set of parens.
4389 (python-tests-self-insert " name='index')") 4542 (python-tests-self-insert " name='index')")
4390 (should (= (current-indentation) 4)) 4543 (should (= (current-indentation) 4))
4391 4544
4392 ;; But a comma outside it, should trigger indentation. 4545 ;; But a comma outside it, should trigger indentation.
4393 (python-tests-self-insert ",") 4546 (python-tests-self-insert ",")
4394 (should (= (current-indentation) 23)) 4547 (should (= (current-indentation) 23))
4395 4548
4396 ;; Newline indents to the first argument column 4549 ;; Newline indents to the first argument column
4397 (python-tests-self-insert "\n") 4550 (python-tests-self-insert "\n")
4398 (should (= (current-indentation) 23)) 4551 (should (= (current-indentation) 23))
4399 4552
4400 ;; All this input must not change indentation 4553 ;; All this input must not change indentation
4401 (indent-line-to 4) 4554 (indent-line-to 4)
4402 (python-tests-self-insert "url(r'^/login$', views.login)") 4555 (python-tests-self-insert "url(r'^/login$', views.login)")
4403 (should (= (current-indentation) 4)) 4556 (should (= (current-indentation) 4))
4404 4557
4405 ;; But this comma does 4558 ;; But this comma does
4406 (python-tests-self-insert ",") 4559 (python-tests-self-insert ",")
4407 (should (= (current-indentation) 23)))) 4560 (should (= (current-indentation) 23))))
4408 (or eim (electric-indent-mode -1))))) 4561 (or eim (electric-indent-mode -1)))))
4409 4562
4410(ert-deftest python-triple-quote-pairing () 4563(ert-deftest python-triple-quote-pairing ()
4411 (require 'electric)
4412 (let ((epm electric-pair-mode)) 4564 (let ((epm electric-pair-mode))
4413 (unwind-protect 4565 (unwind-protect
4414 (progn 4566 (progn
4415 (python-tests-with-temp-buffer 4567 (python-tests-with-temp-buffer
4416 "\"\"\n" 4568 "\"\"\n"
4417 (or epm (electric-pair-mode 1)) 4569 (or epm (electric-pair-mode 1))
4418 (goto-char (1- (point-max))) 4570 (goto-char (1- (point-max)))
4419 (python-tests-self-insert ?\") 4571 (python-tests-self-insert ?\")
4420 (should (string= (buffer-string) 4572 (should (string= (buffer-string)
4421 "\"\"\"\"\"\"\n")) 4573 "\"\"\"\"\"\"\n"))
4422 (should (= (point) 4))) 4574 (should (= (point) 4)))
4423 (python-tests-with-temp-buffer 4575 (python-tests-with-temp-buffer
4424 "\n" 4576 "\n"
4425 (python-tests-self-insert (list ?\" ?\" ?\")) 4577 (python-tests-self-insert (list ?\" ?\" ?\"))
4426 (should (string= (buffer-string) 4578 (should (string= (buffer-string)
4427 "\"\"\"\"\"\"\n")) 4579 "\"\"\"\"\"\"\n"))
4428 (should (= (point) 4))) 4580 (should (= (point) 4)))
4429 (python-tests-with-temp-buffer 4581 (python-tests-with-temp-buffer
4430 "\"\n\"\"\n" 4582 "\"\n\"\"\n"
4431 (goto-char (1- (point-max))) 4583 (goto-char (1- (point-max)))
4432 (python-tests-self-insert ?\") 4584 (python-tests-self-insert ?\")
4433 (should (= (point) (1- (point-max)))) 4585 (should (= (point) (1- (point-max))))
4434 (should (string= (buffer-string) 4586 (should (string= (buffer-string)
4435 "\"\n\"\"\"\n")))) 4587 "\"\n\"\"\"\n"))))
4436 (or epm (electric-pair-mode -1))))) 4588 (or epm (electric-pair-mode -1)))))
4437 4589
4590
4591;;; Hideshow support
4592
4593(ert-deftest python-hideshow-hide-levels-1 ()
4594 "Should hide all methods when called after class start."
4595 (let ((enabled hs-minor-mode))
4596 (unwind-protect
4597 (progn
4598 (python-tests-with-temp-buffer
4599 "
4600class SomeClass:
4601
4602 def __init__(self, arg, kwarg=1):
4603 self.arg = arg
4604 self.kwarg = kwarg
4605
4606 def filter(self, nums):
4607 def fn(item):
4608 return item in [self.arg, self.kwarg]
4609 return filter(fn, nums)
4610
4611 def __str__(self):
4612 return '%s-%s' % (self.arg, self.kwarg)
4613"
4614 (hs-minor-mode 1)
4615 (python-tests-look-at "class SomeClass:")
4616 (forward-line)
4617 (hs-hide-level 1)
4618 (should
4619 (string=
4620 (python-tests-visible-string)
4621 "
4622class SomeClass:
4623
4624 def __init__(self, arg, kwarg=1):
4625 def filter(self, nums):
4626 def __str__(self):"))))
4627 (or enabled (hs-minor-mode -1)))))
4628
4629(ert-deftest python-hideshow-hide-levels-2 ()
4630 "Should hide nested methods and parens at end of defun."
4631 (let ((enabled hs-minor-mode))
4632 (unwind-protect
4633 (progn
4634 (python-tests-with-temp-buffer
4635 "
4636class SomeClass:
4637
4638 def __init__(self, arg, kwarg=1):
4639 self.arg = arg
4640 self.kwarg = kwarg
4641
4642 def filter(self, nums):
4643 def fn(item):
4644 return item in [self.arg, self.kwarg]
4645 return filter(fn, nums)
4646
4647 def __str__(self):
4648 return '%s-%s' % (self.arg, self.kwarg)
4649"
4650 (hs-minor-mode 1)
4651 (python-tests-look-at "def fn(item):")
4652 (hs-hide-block)
4653 (should
4654 (string=
4655 (python-tests-visible-string)
4656 "
4657class SomeClass:
4658
4659 def __init__(self, arg, kwarg=1):
4660 self.arg = arg
4661 self.kwarg = kwarg
4662
4663 def filter(self, nums):
4664 def fn(item):
4665 return filter(fn, nums)
4666
4667 def __str__(self):
4668 return '%s-%s' % (self.arg, self.kwarg)
4669"))))
4670 (or enabled (hs-minor-mode -1)))))
4671
4672
4438 4673
4439(provide 'python-tests) 4674(provide 'python-tests)
4440 4675