Update copyright year to 2014 by running admin/update-copyright.
[emacs.git] / test / automated / python-tests.el
blobdc58138ced418672ad8274f4b47185786229e6d1
1 ;;; python-tests.el --- Test suite for python.el
3 ;; Copyright (C) 2013-2014 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 <http://www.gnu.org/licenses/>.
20 ;;; Commentary:
22 ;;; Code:
24 (require 'ert)
25 (require 'python)
27 (defmacro python-tests-with-temp-buffer (contents &rest body)
28 "Create a `python-mode' enabled temp buffer with CONTENTS.
29 BODY is code to be executed within the temp buffer. Point is
30 always located at the beginning of buffer."
31 (declare (indent 1) (debug t))
32 `(with-temp-buffer
33 (python-mode)
34 (insert ,contents)
35 (goto-char (point-min))
36 ,@body))
38 (defmacro python-tests-with-temp-file (contents &rest body)
39 "Create a `python-mode' enabled file with CONTENTS.
40 BODY is code to be executed within the temp buffer. Point is
41 always located at the beginning of buffer."
42 (declare (indent 1) (debug t))
43 ;; temp-file never actually used for anything?
44 `(let* ((temp-file (make-temp-file "python-tests" nil ".py"))
45 (buffer (find-file-noselect temp-file)))
46 (unwind-protect
47 (with-current-buffer buffer
48 (python-mode)
49 (insert ,contents)
50 (goto-char (point-min))
51 ,@body)
52 (and buffer (kill-buffer buffer))
53 (delete-file temp-file))))
55 (defun python-tests-look-at (string &optional num restore-point)
56 "Move point at beginning of STRING in the current buffer.
57 Optional argument NUM defaults to 1 and is an integer indicating
58 how many occurrences must be found, when positive the search is
59 done forwards, otherwise backwards. When RESTORE-POINT is
60 non-nil the point is not moved but the position found is still
61 returned. When searching forward and point is already looking at
62 STRING, it is skipped so the next STRING occurrence is selected."
63 (let* ((num (or num 1))
64 (starting-point (point))
65 (string (regexp-quote string))
66 (search-fn (if (> num 0) #'re-search-forward #'re-search-backward))
67 (deinc-fn (if (> num 0) #'1- #'1+))
68 (found-point))
69 (prog2
70 (catch 'exit
71 (while (not (= num 0))
72 (when (and (> num 0)
73 (looking-at string))
74 ;; Moving forward and already looking at STRING, skip it.
75 (forward-char (length (match-string-no-properties 0))))
76 (and (not (funcall search-fn string nil t))
77 (throw 'exit t))
78 (when (> num 0)
79 ;; `re-search-forward' leaves point at the end of the
80 ;; occurrence, move back so point is at the beginning
81 ;; instead.
82 (forward-char (- (length (match-string-no-properties 0)))))
83 (setq
84 num (funcall deinc-fn num)
85 found-point (point))))
86 found-point
87 (and restore-point (goto-char starting-point)))))
90 ;;; Tests for your tests, so you can test while you test.
92 (ert-deftest python-tests-look-at-1 ()
93 "Test forward movement."
94 (python-tests-with-temp-buffer
95 "Lorem ipsum dolor sit amet, consectetur adipisicing elit,
96 sed do eiusmod tempor incididunt ut labore et dolore magna
97 aliqua."
98 (let ((expected (save-excursion
99 (dotimes (i 3)
100 (re-search-forward "et" nil t))
101 (forward-char -2)
102 (point))))
103 (should (= (python-tests-look-at "et" 3 t) expected))
104 ;; Even if NUM is bigger than found occurrences the point of last
105 ;; one should be returned.
106 (should (= (python-tests-look-at "et" 6 t) expected))
107 ;; If already looking at STRING, it should skip it.
108 (dotimes (i 2) (re-search-forward "et"))
109 (forward-char -2)
110 (should (= (python-tests-look-at "et") expected)))))
112 (ert-deftest python-tests-look-at-2 ()
113 "Test backward movement."
114 (python-tests-with-temp-buffer
115 "Lorem ipsum dolor sit amet, consectetur adipisicing elit,
116 sed do eiusmod tempor incididunt ut labore et dolore magna
117 aliqua."
118 (let ((expected
119 (save-excursion
120 (re-search-forward "et" nil t)
121 (forward-char -2)
122 (point))))
123 (dotimes (i 3)
124 (re-search-forward "et" nil t))
125 (should (= (python-tests-look-at "et" -3 t) expected))
126 (should (= (python-tests-look-at "et" -6 t) expected)))))
129 ;;; Bindings
132 ;;; Python specialized rx
135 ;;; Font-lock and syntax
138 ;;; Indentation
140 ;; See: http://www.python.org/dev/peps/pep-0008/#indentation
142 (ert-deftest python-indent-pep8-1 ()
143 "First pep8 case."
144 (python-tests-with-temp-buffer
145 "# Aligned with opening delimiter
146 foo = long_function_name(var_one, var_two,
147 var_three, var_four)
149 (should (eq (car (python-indent-context)) 'no-indent))
150 (should (= (python-indent-calculate-indentation) 0))
151 (python-tests-look-at "foo = long_function_name(var_one, var_two,")
152 (should (eq (car (python-indent-context)) 'after-line))
153 (should (= (python-indent-calculate-indentation) 0))
154 (python-tests-look-at "var_three, var_four)")
155 (should (eq (car (python-indent-context)) 'inside-paren))
156 (should (= (python-indent-calculate-indentation) 25))))
158 (ert-deftest python-indent-pep8-2 ()
159 "Second pep8 case."
160 (python-tests-with-temp-buffer
161 "# More indentation included to distinguish this from the rest.
162 def long_function_name(
163 var_one, var_two, var_three,
164 var_four):
165 print (var_one)
167 (should (eq (car (python-indent-context)) 'no-indent))
168 (should (= (python-indent-calculate-indentation) 0))
169 (python-tests-look-at "def long_function_name(")
170 (should (eq (car (python-indent-context)) 'after-line))
171 (should (= (python-indent-calculate-indentation) 0))
172 (python-tests-look-at "var_one, var_two, var_three,")
173 (should (eq (car (python-indent-context)) 'inside-paren))
174 (should (= (python-indent-calculate-indentation) 8))
175 (python-tests-look-at "var_four):")
176 (should (eq (car (python-indent-context)) 'inside-paren))
177 (should (= (python-indent-calculate-indentation) 8))
178 (python-tests-look-at "print (var_one)")
179 (should (eq (car (python-indent-context)) 'after-beginning-of-block))
180 (should (= (python-indent-calculate-indentation) 4))))
182 (ert-deftest python-indent-pep8-3 ()
183 "Third pep8 case."
184 (python-tests-with-temp-buffer
185 "# Extra indentation is not necessary.
186 foo = long_function_name(
187 var_one, var_two,
188 var_three, var_four)
190 (should (eq (car (python-indent-context)) 'no-indent))
191 (should (= (python-indent-calculate-indentation) 0))
192 (python-tests-look-at "foo = long_function_name(")
193 (should (eq (car (python-indent-context)) 'after-line))
194 (should (= (python-indent-calculate-indentation) 0))
195 (python-tests-look-at "var_one, var_two,")
196 (should (eq (car (python-indent-context)) 'inside-paren))
197 (should (= (python-indent-calculate-indentation) 4))
198 (python-tests-look-at "var_three, var_four)")
199 (should (eq (car (python-indent-context)) 'inside-paren))
200 (should (= (python-indent-calculate-indentation) 4))))
202 (ert-deftest python-indent-after-comment-1 ()
203 "The most simple after-comment case that shouldn't fail."
204 (python-tests-with-temp-buffer
205 "# Contents will be modified to correct indentation
206 class Blag(object):
207 def _on_child_complete(self, child_future):
208 if self.in_terminal_state():
209 pass
210 # We only complete when all our async children have entered a
211 # terminal state. At that point, if any child failed, we fail
212 # with the exception with which the first child failed.
214 (python-tests-look-at "# We only complete")
215 (should (eq (car (python-indent-context)) 'after-line))
216 (should (= (python-indent-calculate-indentation) 8))
217 (python-tests-look-at "# terminal state")
218 (should (eq (car (python-indent-context)) 'after-comment))
219 (should (= (python-indent-calculate-indentation) 8))
220 (python-tests-look-at "# with the exception")
221 (should (eq (car (python-indent-context)) 'after-comment))
222 ;; This one indents relative to previous block, even given the fact
223 ;; that it was under-indented.
224 (should (= (python-indent-calculate-indentation) 4))
225 (python-tests-look-at "# terminal state" -1)
226 ;; It doesn't hurt to check again.
227 (should (eq (car (python-indent-context)) 'after-comment))
228 (python-indent-line)
229 (should (= (current-indentation) 8))
230 (python-tests-look-at "# with the exception")
231 (should (eq (car (python-indent-context)) 'after-comment))
232 ;; Now everything should be lined up.
233 (should (= (python-indent-calculate-indentation) 8))))
235 (ert-deftest python-indent-after-comment-2 ()
236 "Test after-comment in weird cases."
237 (python-tests-with-temp-buffer
238 "# Contents will be modified to correct indentation
239 def func(arg):
240 # I don't do much
241 return arg
242 # This comment is badly indented just because.
243 # But we won't mess with the user in this line.
245 now_we_do_mess_cause_this_is_not_a_comment = 1
247 # yeah, that.
249 (python-tests-look-at "# I don't do much")
250 (should (eq (car (python-indent-context)) 'after-beginning-of-block))
251 (should (= (python-indent-calculate-indentation) 4))
252 (python-tests-look-at "return arg")
253 ;; Comment here just gets ignored, this line is not a comment so
254 ;; the rules won't apply here.
255 (should (eq (car (python-indent-context)) 'after-beginning-of-block))
256 (should (= (python-indent-calculate-indentation) 4))
257 (python-tests-look-at "# This comment is badly")
258 (should (eq (car (python-indent-context)) 'after-line))
259 ;; The return keyword moves indentation backwards 4 spaces, but
260 ;; let's assume this comment was placed there because the user
261 ;; wanted to (manually adding spaces or whatever).
262 (should (= (python-indent-calculate-indentation) 0))
263 (python-tests-look-at "# but we won't mess")
264 (should (eq (car (python-indent-context)) 'after-comment))
265 (should (= (python-indent-calculate-indentation) 4))
266 ;; Behave the same for blank lines: potentially a comment.
267 (forward-line 1)
268 (should (eq (car (python-indent-context)) 'after-comment))
269 (should (= (python-indent-calculate-indentation) 4))
270 (python-tests-look-at "now_we_do_mess")
271 ;; Here is where comment indentation starts to get ignored and
272 ;; where the user can't freely indent anymore.
273 (should (eq (car (python-indent-context)) 'after-line))
274 (should (= (python-indent-calculate-indentation) 0))
275 (python-tests-look-at "# yeah, that.")
276 (should (eq (car (python-indent-context)) 'after-line))
277 (should (= (python-indent-calculate-indentation) 0))))
279 (ert-deftest python-indent-inside-paren-1 ()
280 "The most simple inside-paren case that shouldn't fail."
281 (python-tests-with-temp-buffer
283 data = {
284 'key':
286 'objlist': [
288 'pk': 1,
289 'name': 'first',
292 'pk': 2,
293 'name': 'second',
299 (python-tests-look-at "data = {")
300 (should (eq (car (python-indent-context)) 'after-line))
301 (should (= (python-indent-calculate-indentation) 0))
302 (python-tests-look-at "'key':")
303 (should (eq (car (python-indent-context)) 'inside-paren))
304 (should (= (python-indent-calculate-indentation) 4))
305 (python-tests-look-at "{")
306 (should (eq (car (python-indent-context)) 'inside-paren))
307 (should (= (python-indent-calculate-indentation) 4))
308 (python-tests-look-at "'objlist': [")
309 (should (eq (car (python-indent-context)) 'inside-paren))
310 (should (= (python-indent-calculate-indentation) 8))
311 (python-tests-look-at "{")
312 (should (eq (car (python-indent-context)) 'inside-paren))
313 (should (= (python-indent-calculate-indentation) 12))
314 (python-tests-look-at "'pk': 1,")
315 (should (eq (car (python-indent-context)) 'inside-paren))
316 (should (= (python-indent-calculate-indentation) 16))
317 (python-tests-look-at "'name': 'first',")
318 (should (eq (car (python-indent-context)) 'inside-paren))
319 (should (= (python-indent-calculate-indentation) 16))
320 (python-tests-look-at "},")
321 (should (eq (car (python-indent-context)) 'inside-paren))
322 (should (= (python-indent-calculate-indentation) 12))
323 (python-tests-look-at "{")
324 (should (eq (car (python-indent-context)) 'inside-paren))
325 (should (= (python-indent-calculate-indentation) 12))
326 (python-tests-look-at "'pk': 2,")
327 (should (eq (car (python-indent-context)) 'inside-paren))
328 (should (= (python-indent-calculate-indentation) 16))
329 (python-tests-look-at "'name': 'second',")
330 (should (eq (car (python-indent-context)) 'inside-paren))
331 (should (= (python-indent-calculate-indentation) 16))
332 (python-tests-look-at "}")
333 (should (eq (car (python-indent-context)) 'inside-paren))
334 (should (= (python-indent-calculate-indentation) 12))
335 (python-tests-look-at "]")
336 (should (eq (car (python-indent-context)) 'inside-paren))
337 (should (= (python-indent-calculate-indentation) 8))
338 (python-tests-look-at "}")
339 (should (eq (car (python-indent-context)) 'inside-paren))
340 (should (= (python-indent-calculate-indentation) 4))
341 (python-tests-look-at "}")
342 (should (eq (car (python-indent-context)) 'inside-paren))
343 (should (= (python-indent-calculate-indentation) 0))))
345 (ert-deftest python-indent-inside-paren-2 ()
346 "Another more compact paren group style."
347 (python-tests-with-temp-buffer
349 data = {'key': {
350 'objlist': [
351 {'pk': 1,
352 'name': 'first'},
353 {'pk': 2,
354 'name': 'second'}
358 (python-tests-look-at "data = {")
359 (should (eq (car (python-indent-context)) 'after-line))
360 (should (= (python-indent-calculate-indentation) 0))
361 (python-tests-look-at "'objlist': [")
362 (should (eq (car (python-indent-context)) 'inside-paren))
363 (should (= (python-indent-calculate-indentation) 4))
364 (python-tests-look-at "{'pk': 1,")
365 (should (eq (car (python-indent-context)) 'inside-paren))
366 (should (= (python-indent-calculate-indentation) 8))
367 (python-tests-look-at "'name': 'first'},")
368 (should (eq (car (python-indent-context)) 'inside-paren))
369 (should (= (python-indent-calculate-indentation) 9))
370 (python-tests-look-at "{'pk': 2,")
371 (should (eq (car (python-indent-context)) 'inside-paren))
372 (should (= (python-indent-calculate-indentation) 8))
373 (python-tests-look-at "'name': 'second'}")
374 (should (eq (car (python-indent-context)) 'inside-paren))
375 (should (= (python-indent-calculate-indentation) 9))
376 (python-tests-look-at "]")
377 (should (eq (car (python-indent-context)) 'inside-paren))
378 (should (= (python-indent-calculate-indentation) 4))
379 (python-tests-look-at "}}")
380 (should (eq (car (python-indent-context)) 'inside-paren))
381 (should (= (python-indent-calculate-indentation) 0))
382 (python-tests-look-at "}")
383 (should (eq (car (python-indent-context)) 'inside-paren))
384 (should (= (python-indent-calculate-indentation) 0))))
386 (ert-deftest python-indent-after-block-1 ()
387 "The most simple after-block case that shouldn't fail."
388 (python-tests-with-temp-buffer
390 def foo(a, b, c=True):
392 (should (eq (car (python-indent-context)) 'no-indent))
393 (should (= (python-indent-calculate-indentation) 0))
394 (goto-char (point-max))
395 (should (eq (car (python-indent-context)) 'after-beginning-of-block))
396 (should (= (python-indent-calculate-indentation) 4))))
398 (ert-deftest python-indent-after-block-2 ()
399 "A weird (malformed) multiline block statement."
400 (python-tests-with-temp-buffer
402 def foo(a, b, c={
403 'a':
406 (goto-char (point-max))
407 (should (eq (car (python-indent-context)) 'after-beginning-of-block))
408 (should (= (python-indent-calculate-indentation) 4))))
410 (ert-deftest python-indent-dedenters-1 ()
411 "Check all dedenters."
412 (python-tests-with-temp-buffer
414 def foo(a, b, c):
415 if a:
416 print (a)
417 elif b:
418 print (b)
419 else:
420 try:
421 print (c.pop())
422 except (IndexError, AttributeError):
423 print (c)
424 finally:
425 print ('nor a, nor b are true')
427 (python-tests-look-at "if a:")
428 (should (eq (car (python-indent-context)) 'after-beginning-of-block))
429 (should (= (python-indent-calculate-indentation) 4))
430 (python-tests-look-at "print (a)")
431 (should (eq (car (python-indent-context)) 'after-beginning-of-block))
432 (should (= (python-indent-calculate-indentation) 8))
433 (python-tests-look-at "elif b:")
434 (should (eq (car (python-indent-context)) 'after-line))
435 (should (= (python-indent-calculate-indentation) 4))
436 (python-tests-look-at "print (b)")
437 (should (eq (car (python-indent-context)) 'after-beginning-of-block))
438 (should (= (python-indent-calculate-indentation) 8))
439 (python-tests-look-at "else:")
440 (should (eq (car (python-indent-context)) 'after-line))
441 (should (= (python-indent-calculate-indentation) 4))
442 (python-tests-look-at "try:")
443 (should (eq (car (python-indent-context)) 'after-beginning-of-block))
444 (should (= (python-indent-calculate-indentation) 8))
445 (python-tests-look-at "print (c.pop())")
446 (should (eq (car (python-indent-context)) 'after-beginning-of-block))
447 (should (= (python-indent-calculate-indentation) 12))
448 (python-tests-look-at "except (IndexError, AttributeError):")
449 (should (eq (car (python-indent-context)) 'after-line))
450 (should (= (python-indent-calculate-indentation) 8))
451 (python-tests-look-at "print (c)")
452 (should (eq (car (python-indent-context)) 'after-beginning-of-block))
453 (should (= (python-indent-calculate-indentation) 12))
454 (python-tests-look-at "finally:")
455 (should (eq (car (python-indent-context)) 'after-line))
456 (should (= (python-indent-calculate-indentation) 8))
457 (python-tests-look-at "print ('nor a, nor b are true')")
458 (should (eq (car (python-indent-context)) 'after-beginning-of-block))
459 (should (= (python-indent-calculate-indentation) 12))))
461 (ert-deftest python-indent-dedenters-2 ()
462 "Check one-liner block special case.."
463 (python-tests-with-temp-buffer
465 cond = True
466 if cond:
468 if cond: print 'True'
469 else: print 'False'
471 else:
472 return
474 (python-tests-look-at "else: print 'False'")
475 ;; When a block has code after ":" it's just considered a simple
476 ;; line as that's a common thing to happen in one-liners.
477 (should (eq (car (python-indent-context)) 'after-line))
478 (should (= (python-indent-calculate-indentation) 4))
479 (python-tests-look-at "else:")
480 (should (eq (car (python-indent-context)) 'after-line))
481 (should (= (python-indent-calculate-indentation) 0))))
483 (ert-deftest python-indent-after-backslash-1 ()
484 "The most common case."
485 (python-tests-with-temp-buffer
487 from foo.bar.baz import something, something_1 \\\\
488 something_2 something_3, \\\\
489 something_4, something_5
491 (python-tests-look-at "from foo.bar.baz import something, something_1")
492 (should (eq (car (python-indent-context)) 'after-line))
493 (should (= (python-indent-calculate-indentation) 0))
494 (python-tests-look-at "something_2 something_3,")
495 (should (eq (car (python-indent-context)) 'after-backslash))
496 (should (= (python-indent-calculate-indentation) 4))
497 (python-tests-look-at "something_4, something_5")
498 (should (eq (car (python-indent-context)) 'after-backslash))
499 (should (= (python-indent-calculate-indentation) 4))
500 (goto-char (point-max))
501 (should (eq (car (python-indent-context)) 'after-line))
502 (should (= (python-indent-calculate-indentation) 0))))
504 (ert-deftest python-indent-after-backslash-2 ()
505 "A pretty extreme complicated case."
506 (python-tests-with-temp-buffer
508 objects = Thing.objects.all() \\\\
509 .filter(
510 type='toy',
511 status='bought'
512 ) \\\\
513 .aggregate(
514 Sum('amount')
515 ) \\\\
516 .values_list()
518 (python-tests-look-at "objects = Thing.objects.all()")
519 (should (eq (car (python-indent-context)) 'after-line))
520 (should (= (python-indent-calculate-indentation) 0))
521 (python-tests-look-at ".filter(")
522 (should (eq (car (python-indent-context)) 'after-backslash))
523 (should (= (python-indent-calculate-indentation) 23))
524 (python-tests-look-at "type='toy',")
525 (should (eq (car (python-indent-context)) 'inside-paren))
526 (should (= (python-indent-calculate-indentation) 27))
527 (python-tests-look-at "status='bought'")
528 (should (eq (car (python-indent-context)) 'inside-paren))
529 (should (= (python-indent-calculate-indentation) 27))
530 (python-tests-look-at ") \\\\")
531 (should (eq (car (python-indent-context)) 'inside-paren))
532 (should (= (python-indent-calculate-indentation) 23))
533 (python-tests-look-at ".aggregate(")
534 (should (eq (car (python-indent-context)) 'after-backslash))
535 (should (= (python-indent-calculate-indentation) 23))
536 (python-tests-look-at "Sum('amount')")
537 (should (eq (car (python-indent-context)) 'inside-paren))
538 (should (= (python-indent-calculate-indentation) 27))
539 (python-tests-look-at ") \\\\")
540 (should (eq (car (python-indent-context)) 'inside-paren))
541 (should (= (python-indent-calculate-indentation) 23))
542 (python-tests-look-at ".values_list()")
543 (should (eq (car (python-indent-context)) 'after-backslash))
544 (should (= (python-indent-calculate-indentation) 23))
545 (forward-line 1)
546 (should (eq (car (python-indent-context)) 'after-line))
547 (should (= (python-indent-calculate-indentation) 0))))
549 (ert-deftest python-indent-block-enders-1 ()
550 "Test `python-indent-block-enders' value honoring."
551 (python-tests-with-temp-buffer
553 Class foo(object):
555 def bar(self):
556 if self.baz:
557 return (1,
561 else:
562 pass
564 (python-tests-look-at "3)")
565 (forward-line 1)
566 (should (= (python-indent-calculate-indentation) 8))
567 (python-tests-look-at "pass")
568 (forward-line 1)
569 (should (= (python-indent-calculate-indentation) 8))))
571 (ert-deftest python-indent-block-enders-2 ()
572 "Test `python-indent-block-enders' value honoring."
573 (python-tests-with-temp-buffer
575 Class foo(object):
576 '''raise lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
578 eiusmod tempor incididunt ut labore et dolore magna aliqua.
580 def bar(self):
581 \"return (1, 2, 3).\"
582 if self.baz:
583 return (1,
587 (python-tests-look-at "def")
588 (should (= (python-indent-calculate-indentation) 4))
589 (python-tests-look-at "if")
590 (should (= (python-indent-calculate-indentation) 8))))
593 ;;; Navigation
595 (ert-deftest python-nav-beginning-of-defun-1 ()
596 (python-tests-with-temp-buffer
598 def decoratorFunctionWithArguments(arg1, arg2, arg3):
599 '''print decorated function call data to stdout.
601 Usage:
603 @decoratorFunctionWithArguments('arg1', 'arg2')
604 def func(a, b, c=True):
605 pass
608 def wwrap(f):
609 print 'Inside wwrap()'
610 def wrapped_f(*args):
611 print 'Inside wrapped_f()'
612 print 'Decorator arguments:', arg1, arg2, arg3
613 f(*args)
614 print 'After f(*args)'
615 return wrapped_f
616 return wwrap
618 (python-tests-look-at "return wrap")
619 (should (= (save-excursion
620 (python-nav-beginning-of-defun)
621 (point))
622 (save-excursion
623 (python-tests-look-at "def wrapped_f(*args):" -1)
624 (beginning-of-line)
625 (point))))
626 (python-tests-look-at "def wrapped_f(*args):" -1)
627 (should (= (save-excursion
628 (python-nav-beginning-of-defun)
629 (point))
630 (save-excursion
631 (python-tests-look-at "def wwrap(f):" -1)
632 (beginning-of-line)
633 (point))))
634 (python-tests-look-at "def wwrap(f):" -1)
635 (should (= (save-excursion
636 (python-nav-beginning-of-defun)
637 (point))
638 (save-excursion
639 (python-tests-look-at "def decoratorFunctionWithArguments" -1)
640 (beginning-of-line)
641 (point))))))
643 (ert-deftest python-nav-beginning-of-defun-2 ()
644 (python-tests-with-temp-buffer
646 class C(object):
648 def m(self):
649 self.c()
651 def b():
652 pass
654 def a():
655 pass
657 def c(self):
658 pass
660 ;; Nested defuns, are handled with care.
661 (python-tests-look-at "def c(self):")
662 (should (= (save-excursion
663 (python-nav-beginning-of-defun)
664 (point))
665 (save-excursion
666 (python-tests-look-at "def m(self):" -1)
667 (beginning-of-line)
668 (point))))
669 ;; Defuns on same levels should be respected.
670 (python-tests-look-at "def a():" -1)
671 (should (= (save-excursion
672 (python-nav-beginning-of-defun)
673 (point))
674 (save-excursion
675 (python-tests-look-at "def b():" -1)
676 (beginning-of-line)
677 (point))))
678 ;; Jump to a top level defun.
679 (python-tests-look-at "def b():" -1)
680 (should (= (save-excursion
681 (python-nav-beginning-of-defun)
682 (point))
683 (save-excursion
684 (python-tests-look-at "def m(self):" -1)
685 (beginning-of-line)
686 (point))))
687 ;; Jump to a top level defun again.
688 (python-tests-look-at "def m(self):" -1)
689 (should (= (save-excursion
690 (python-nav-beginning-of-defun)
691 (point))
692 (save-excursion
693 (python-tests-look-at "class C(object):" -1)
694 (beginning-of-line)
695 (point))))))
697 (ert-deftest python-nav-end-of-defun-1 ()
698 (python-tests-with-temp-buffer
700 class C(object):
702 def m(self):
703 self.c()
705 def b():
706 pass
708 def a():
709 pass
711 def c(self):
712 pass
714 (should (= (save-excursion
715 (python-tests-look-at "class C(object):")
716 (python-nav-end-of-defun)
717 (point))
718 (save-excursion
719 (point-max))))
720 (should (= (save-excursion
721 (python-tests-look-at "def m(self):")
722 (python-nav-end-of-defun)
723 (point))
724 (save-excursion
725 (python-tests-look-at "def c(self):")
726 (forward-line -1)
727 (point))))
728 (should (= (save-excursion
729 (python-tests-look-at "def b():")
730 (python-nav-end-of-defun)
731 (point))
732 (save-excursion
733 (python-tests-look-at "def b():")
734 (forward-line 2)
735 (point))))
736 (should (= (save-excursion
737 (python-tests-look-at "def c(self):")
738 (python-nav-end-of-defun)
739 (point))
740 (save-excursion
741 (point-max))))))
743 (ert-deftest python-nav-end-of-defun-2 ()
744 (python-tests-with-temp-buffer
746 def decoratorFunctionWithArguments(arg1, arg2, arg3):
747 '''print decorated function call data to stdout.
749 Usage:
751 @decoratorFunctionWithArguments('arg1', 'arg2')
752 def func(a, b, c=True):
753 pass
756 def wwrap(f):
757 print 'Inside wwrap()'
758 def wrapped_f(*args):
759 print 'Inside wrapped_f()'
760 print 'Decorator arguments:', arg1, arg2, arg3
761 f(*args)
762 print 'After f(*args)'
763 return wrapped_f
764 return wwrap
766 (should (= (save-excursion
767 (python-tests-look-at "def decoratorFunctionWithArguments")
768 (python-nav-end-of-defun)
769 (point))
770 (save-excursion
771 (point-max))))
772 (should (= (save-excursion
773 (python-tests-look-at "@decoratorFunctionWithArguments")
774 (python-nav-end-of-defun)
775 (point))
776 (save-excursion
777 (point-max))))
778 (should (= (save-excursion
779 (python-tests-look-at "def wwrap(f):")
780 (python-nav-end-of-defun)
781 (point))
782 (save-excursion
783 (python-tests-look-at "return wwrap")
784 (line-beginning-position))))
785 (should (= (save-excursion
786 (python-tests-look-at "def wrapped_f(*args):")
787 (python-nav-end-of-defun)
788 (point))
789 (save-excursion
790 (python-tests-look-at "return wrapped_f")
791 (line-beginning-position))))
792 (should (= (save-excursion
793 (python-tests-look-at "f(*args)")
794 (python-nav-end-of-defun)
795 (point))
796 (save-excursion
797 (python-tests-look-at "return wrapped_f")
798 (line-beginning-position))))))
800 (ert-deftest python-nav-backward-defun-1 ()
801 (python-tests-with-temp-buffer
803 class A(object): # A
805 def a(self): # a
806 pass
808 def b(self): # b
809 pass
811 class B(object): # B
813 class C(object): # C
815 def d(self): # d
816 pass
818 # def e(self): # e
819 # pass
821 def c(self): # c
822 pass
824 # def d(self): # d
825 # pass
827 (goto-char (point-max))
828 (should (= (save-excursion (python-nav-backward-defun))
829 (python-tests-look-at " def c(self): # c" -1)))
830 (should (= (save-excursion (python-nav-backward-defun))
831 (python-tests-look-at " def d(self): # d" -1)))
832 (should (= (save-excursion (python-nav-backward-defun))
833 (python-tests-look-at " class C(object): # C" -1)))
834 (should (= (save-excursion (python-nav-backward-defun))
835 (python-tests-look-at " class B(object): # B" -1)))
836 (should (= (save-excursion (python-nav-backward-defun))
837 (python-tests-look-at " def b(self): # b" -1)))
838 (should (= (save-excursion (python-nav-backward-defun))
839 (python-tests-look-at " def a(self): # a" -1)))
840 (should (= (save-excursion (python-nav-backward-defun))
841 (python-tests-look-at "class A(object): # A" -1)))
842 (should (not (python-nav-backward-defun)))))
844 (ert-deftest python-nav-backward-defun-2 ()
845 (python-tests-with-temp-buffer
847 def decoratorFunctionWithArguments(arg1, arg2, arg3):
848 '''print decorated function call data to stdout.
850 Usage:
852 @decoratorFunctionWithArguments('arg1', 'arg2')
853 def func(a, b, c=True):
854 pass
857 def wwrap(f):
858 print 'Inside wwrap()'
859 def wrapped_f(*args):
860 print 'Inside wrapped_f()'
861 print 'Decorator arguments:', arg1, arg2, arg3
862 f(*args)
863 print 'After f(*args)'
864 return wrapped_f
865 return wwrap
867 (goto-char (point-max))
868 (should (= (save-excursion (python-nav-backward-defun))
869 (python-tests-look-at " def wrapped_f(*args):" -1)))
870 (should (= (save-excursion (python-nav-backward-defun))
871 (python-tests-look-at " def wwrap(f):" -1)))
872 (should (= (save-excursion (python-nav-backward-defun))
873 (python-tests-look-at "def decoratorFunctionWithArguments(arg1, arg2, arg3):" -1)))
874 (should (not (python-nav-backward-defun)))))
876 (ert-deftest python-nav-backward-defun-3 ()
877 (python-tests-with-temp-buffer
880 def u(self):
881 pass
883 def v(self):
884 pass
886 def w(self):
887 pass
890 class A(object):
891 pass
893 (goto-char (point-min))
894 (let ((point (python-tests-look-at "class A(object):")))
895 (should (not (python-nav-backward-defun)))
896 (should (= point (point))))))
898 (ert-deftest python-nav-forward-defun-1 ()
899 (python-tests-with-temp-buffer
901 class A(object): # A
903 def a(self): # a
904 pass
906 def b(self): # b
907 pass
909 class B(object): # B
911 class C(object): # C
913 def d(self): # d
914 pass
916 # def e(self): # e
917 # pass
919 def c(self): # c
920 pass
922 # def d(self): # d
923 # pass
925 (goto-char (point-min))
926 (should (= (save-excursion (python-nav-forward-defun))
927 (python-tests-look-at "(object): # A")))
928 (should (= (save-excursion (python-nav-forward-defun))
929 (python-tests-look-at "(self): # a")))
930 (should (= (save-excursion (python-nav-forward-defun))
931 (python-tests-look-at "(self): # b")))
932 (should (= (save-excursion (python-nav-forward-defun))
933 (python-tests-look-at "(object): # B")))
934 (should (= (save-excursion (python-nav-forward-defun))
935 (python-tests-look-at "(object): # C")))
936 (should (= (save-excursion (python-nav-forward-defun))
937 (python-tests-look-at "(self): # d")))
938 (should (= (save-excursion (python-nav-forward-defun))
939 (python-tests-look-at "(self): # c")))
940 (should (not (python-nav-forward-defun)))))
942 (ert-deftest python-nav-forward-defun-2 ()
943 (python-tests-with-temp-buffer
945 def decoratorFunctionWithArguments(arg1, arg2, arg3):
946 '''print decorated function call data to stdout.
948 Usage:
950 @decoratorFunctionWithArguments('arg1', 'arg2')
951 def func(a, b, c=True):
952 pass
955 def wwrap(f):
956 print 'Inside wwrap()'
957 def wrapped_f(*args):
958 print 'Inside wrapped_f()'
959 print 'Decorator arguments:', arg1, arg2, arg3
960 f(*args)
961 print 'After f(*args)'
962 return wrapped_f
963 return wwrap
965 (goto-char (point-min))
966 (should (= (save-excursion (python-nav-forward-defun))
967 (python-tests-look-at "(arg1, arg2, arg3):")))
968 (should (= (save-excursion (python-nav-forward-defun))
969 (python-tests-look-at "(f):")))
970 (should (= (save-excursion (python-nav-forward-defun))
971 (python-tests-look-at "(*args):")))
972 (should (not (python-nav-forward-defun)))))
974 (ert-deftest python-nav-forward-defun-3 ()
975 (python-tests-with-temp-buffer
977 class A(object):
978 pass
981 def u(self):
982 pass
984 def v(self):
985 pass
987 def w(self):
988 pass
991 (goto-char (point-min))
992 (let ((point (python-tests-look-at "(object):")))
993 (should (not (python-nav-forward-defun)))
994 (should (= point (point))))))
996 (ert-deftest python-nav-beginning-of-statement-1 ()
997 (python-tests-with-temp-buffer
999 v1 = 123 + \
1000 456 + \
1002 v2 = (value1,
1003 value2,
1005 value3,
1006 value4)
1007 v3 = ('this is a string'
1009 'that is continued'
1010 'between lines'
1011 'within a paren',
1012 # this is a comment, yo
1013 'continue previous line')
1014 v4 = '''
1015 a very long
1016 string
1019 (python-tests-look-at "v2 =")
1020 (python-util-forward-comment -1)
1021 (should (= (save-excursion
1022 (python-nav-beginning-of-statement)
1023 (point))
1024 (python-tests-look-at "v1 =" -1 t)))
1025 (python-tests-look-at "v3 =")
1026 (python-util-forward-comment -1)
1027 (should (= (save-excursion
1028 (python-nav-beginning-of-statement)
1029 (point))
1030 (python-tests-look-at "v2 =" -1 t)))
1031 (python-tests-look-at "v4 =")
1032 (python-util-forward-comment -1)
1033 (should (= (save-excursion
1034 (python-nav-beginning-of-statement)
1035 (point))
1036 (python-tests-look-at "v3 =" -1 t)))
1037 (goto-char (point-max))
1038 (python-util-forward-comment -1)
1039 (should (= (save-excursion
1040 (python-nav-beginning-of-statement)
1041 (point))
1042 (python-tests-look-at "v4 =" -1 t)))))
1044 (ert-deftest python-nav-end-of-statement-1 ()
1045 (python-tests-with-temp-buffer
1047 v1 = 123 + \
1048 456 + \
1050 v2 = (value1,
1051 value2,
1053 value3,
1054 value4)
1055 v3 = ('this is a string'
1057 'that is continued'
1058 'between lines'
1059 'within a paren',
1060 # this is a comment, yo
1061 'continue previous line')
1062 v4 = '''
1063 a very long
1064 string
1067 (python-tests-look-at "v1 =")
1068 (should (= (save-excursion
1069 (python-nav-end-of-statement)
1070 (point))
1071 (save-excursion
1072 (python-tests-look-at "789")
1073 (line-end-position))))
1074 (python-tests-look-at "v2 =")
1075 (should (= (save-excursion
1076 (python-nav-end-of-statement)
1077 (point))
1078 (save-excursion
1079 (python-tests-look-at "value4)")
1080 (line-end-position))))
1081 (python-tests-look-at "v3 =")
1082 (should (= (save-excursion
1083 (python-nav-end-of-statement)
1084 (point))
1085 (save-excursion
1086 (python-tests-look-at
1087 "'continue previous line')")
1088 (line-end-position))))
1089 (python-tests-look-at "v4 =")
1090 (should (= (save-excursion
1091 (python-nav-end-of-statement)
1092 (point))
1093 (save-excursion
1094 (goto-char (point-max))
1095 (python-util-forward-comment -1)
1096 (point))))))
1098 (ert-deftest python-nav-forward-statement-1 ()
1099 (python-tests-with-temp-buffer
1101 v1 = 123 + \
1102 456 + \
1104 v2 = (value1,
1105 value2,
1107 value3,
1108 value4)
1109 v3 = ('this is a string'
1111 'that is continued'
1112 'between lines'
1113 'within a paren',
1114 # this is a comment, yo
1115 'continue previous line')
1116 v4 = '''
1117 a very long
1118 string
1121 (python-tests-look-at "v1 =")
1122 (should (= (save-excursion
1123 (python-nav-forward-statement)
1124 (point))
1125 (python-tests-look-at "v2 =")))
1126 (should (= (save-excursion
1127 (python-nav-forward-statement)
1128 (point))
1129 (python-tests-look-at "v3 =")))
1130 (should (= (save-excursion
1131 (python-nav-forward-statement)
1132 (point))
1133 (python-tests-look-at "v4 =")))
1134 (should (= (save-excursion
1135 (python-nav-forward-statement)
1136 (point))
1137 (point-max)))))
1139 (ert-deftest python-nav-backward-statement-1 ()
1140 (python-tests-with-temp-buffer
1142 v1 = 123 + \
1143 456 + \
1145 v2 = (value1,
1146 value2,
1148 value3,
1149 value4)
1150 v3 = ('this is a string'
1152 'that is continued'
1153 'between lines'
1154 'within a paren',
1155 # this is a comment, yo
1156 'continue previous line')
1157 v4 = '''
1158 a very long
1159 string
1162 (goto-char (point-max))
1163 (should (= (save-excursion
1164 (python-nav-backward-statement)
1165 (point))
1166 (python-tests-look-at "v4 =" -1)))
1167 (should (= (save-excursion
1168 (python-nav-backward-statement)
1169 (point))
1170 (python-tests-look-at "v3 =" -1)))
1171 (should (= (save-excursion
1172 (python-nav-backward-statement)
1173 (point))
1174 (python-tests-look-at "v2 =" -1)))
1175 (should (= (save-excursion
1176 (python-nav-backward-statement)
1177 (point))
1178 (python-tests-look-at "v1 =" -1)))))
1180 (ert-deftest python-nav-backward-statement-2 ()
1181 :expected-result :failed
1182 (python-tests-with-temp-buffer
1184 v1 = 123 + \
1185 456 + \
1187 v2 = (value1,
1188 value2,
1190 value3,
1191 value4)
1193 ;; FIXME: For some reason `python-nav-backward-statement' is moving
1194 ;; back two sentences when starting from 'value4)'.
1195 (goto-char (point-max))
1196 (python-util-forward-comment -1)
1197 (should (= (save-excursion
1198 (python-nav-backward-statement)
1199 (point))
1200 (python-tests-look-at "v2 =" -1 t)))))
1202 (ert-deftest python-nav-beginning-of-block-1 ()
1203 (python-tests-with-temp-buffer
1205 def decoratorFunctionWithArguments(arg1, arg2, arg3):
1206 '''print decorated function call data to stdout.
1208 Usage:
1210 @decoratorFunctionWithArguments('arg1', 'arg2')
1211 def func(a, b, c=True):
1212 pass
1215 def wwrap(f):
1216 print 'Inside wwrap()'
1217 def wrapped_f(*args):
1218 print 'Inside wrapped_f()'
1219 print 'Decorator arguments:', arg1, arg2, arg3
1220 f(*args)
1221 print 'After f(*args)'
1222 return wrapped_f
1223 return wwrap
1225 (python-tests-look-at "return wwrap")
1226 (should (= (save-excursion
1227 (python-nav-beginning-of-block)
1228 (point))
1229 (python-tests-look-at "def decoratorFunctionWithArguments" -1)))
1230 (python-tests-look-at "print 'Inside wwrap()'")
1231 (should (= (save-excursion
1232 (python-nav-beginning-of-block)
1233 (point))
1234 (python-tests-look-at "def wwrap(f):" -1)))
1235 (python-tests-look-at "print 'After f(*args)'")
1236 (end-of-line)
1237 (should (= (save-excursion
1238 (python-nav-beginning-of-block)
1239 (point))
1240 (python-tests-look-at "def wrapped_f(*args):" -1)))
1241 (python-tests-look-at "return wrapped_f")
1242 (should (= (save-excursion
1243 (python-nav-beginning-of-block)
1244 (point))
1245 (python-tests-look-at "def wwrap(f):" -1)))))
1247 (ert-deftest python-nav-end-of-block-1 ()
1248 (python-tests-with-temp-buffer
1250 def decoratorFunctionWithArguments(arg1, arg2, arg3):
1251 '''print decorated function call data to stdout.
1253 Usage:
1255 @decoratorFunctionWithArguments('arg1', 'arg2')
1256 def func(a, b, c=True):
1257 pass
1260 def wwrap(f):
1261 print 'Inside wwrap()'
1262 def wrapped_f(*args):
1263 print 'Inside wrapped_f()'
1264 print 'Decorator arguments:', arg1, arg2, arg3
1265 f(*args)
1266 print 'After f(*args)'
1267 return wrapped_f
1268 return wwrap
1270 (python-tests-look-at "def decoratorFunctionWithArguments")
1271 (should (= (save-excursion
1272 (python-nav-end-of-block)
1273 (point))
1274 (save-excursion
1275 (goto-char (point-max))
1276 (python-util-forward-comment -1)
1277 (point))))
1278 (python-tests-look-at "def wwrap(f):")
1279 (should (= (save-excursion
1280 (python-nav-end-of-block)
1281 (point))
1282 (save-excursion
1283 (python-tests-look-at "return wrapped_f")
1284 (line-end-position))))
1285 (end-of-line)
1286 (should (= (save-excursion
1287 (python-nav-end-of-block)
1288 (point))
1289 (save-excursion
1290 (python-tests-look-at "return wrapped_f")
1291 (line-end-position))))
1292 (python-tests-look-at "f(*args)")
1293 (should (= (save-excursion
1294 (python-nav-end-of-block)
1295 (point))
1296 (save-excursion
1297 (python-tests-look-at "print 'After f(*args)'")
1298 (line-end-position))))))
1300 (ert-deftest python-nav-forward-block-1 ()
1301 "This also accounts as a test for `python-nav-backward-block'."
1302 (python-tests-with-temp-buffer
1304 if request.user.is_authenticated():
1305 # def block():
1306 # pass
1307 try:
1308 profile = request.user.get_profile()
1309 except Profile.DoesNotExist:
1310 profile = Profile.objects.create(user=request.user)
1311 else:
1312 if profile.stats:
1313 profile.recalculate_stats()
1314 else:
1315 profile.clear_stats()
1316 finally:
1317 profile.views += 1
1318 profile.save()
1320 (should (= (save-excursion (python-nav-forward-block))
1321 (python-tests-look-at "if request.user.is_authenticated():")))
1322 (should (= (save-excursion (python-nav-forward-block))
1323 (python-tests-look-at "try:")))
1324 (should (= (save-excursion (python-nav-forward-block))
1325 (python-tests-look-at "except Profile.DoesNotExist:")))
1326 (should (= (save-excursion (python-nav-forward-block))
1327 (python-tests-look-at "else:")))
1328 (should (= (save-excursion (python-nav-forward-block))
1329 (python-tests-look-at "if profile.stats:")))
1330 (should (= (save-excursion (python-nav-forward-block))
1331 (python-tests-look-at "else:")))
1332 (should (= (save-excursion (python-nav-forward-block))
1333 (python-tests-look-at "finally:")))
1334 ;; When point is at the last block, leave it there and return nil
1335 (should (not (save-excursion (python-nav-forward-block))))
1336 ;; Move backwards, and even if the number of moves is less than the
1337 ;; provided argument return the point.
1338 (should (= (save-excursion (python-nav-forward-block -10))
1339 (python-tests-look-at
1340 "if request.user.is_authenticated():" -1)))))
1342 (ert-deftest python-nav-forward-sexp-1 ()
1343 (python-tests-with-temp-buffer
1349 (python-tests-look-at "a()")
1350 (python-nav-forward-sexp)
1351 (should (looking-at "$"))
1352 (should (save-excursion
1353 (beginning-of-line)
1354 (looking-at "a()")))
1355 (python-nav-forward-sexp)
1356 (should (looking-at "$"))
1357 (should (save-excursion
1358 (beginning-of-line)
1359 (looking-at "b()")))
1360 (python-nav-forward-sexp)
1361 (should (looking-at "$"))
1362 (should (save-excursion
1363 (beginning-of-line)
1364 (looking-at "c()")))
1365 ;; Movement next to a paren should do what lisp does and
1366 ;; unfortunately It can't change, because otherwise
1367 ;; `blink-matching-open' breaks.
1368 (python-nav-forward-sexp -1)
1369 (should (looking-at "()"))
1370 (should (save-excursion
1371 (beginning-of-line)
1372 (looking-at "c()")))
1373 (python-nav-forward-sexp -1)
1374 (should (looking-at "c()"))
1375 (python-nav-forward-sexp -1)
1376 (should (looking-at "b()"))
1377 (python-nav-forward-sexp -1)
1378 (should (looking-at "a()"))))
1380 (ert-deftest python-nav-forward-sexp-2 ()
1381 (python-tests-with-temp-buffer
1383 def func():
1384 if True:
1385 aaa = bbb
1386 ccc = ddd
1387 eee = fff
1388 return ggg
1390 (python-tests-look-at "aa =")
1391 (python-nav-forward-sexp)
1392 (should (looking-at " = bbb"))
1393 (python-nav-forward-sexp)
1394 (should (looking-at "$"))
1395 (should (save-excursion
1396 (back-to-indentation)
1397 (looking-at "aaa = bbb")))
1398 (python-nav-forward-sexp)
1399 (should (looking-at "$"))
1400 (should (save-excursion
1401 (back-to-indentation)
1402 (looking-at "ccc = ddd")))
1403 (python-nav-forward-sexp)
1404 (should (looking-at "$"))
1405 (should (save-excursion
1406 (back-to-indentation)
1407 (looking-at "eee = fff")))
1408 (python-nav-forward-sexp)
1409 (should (looking-at "$"))
1410 (should (save-excursion
1411 (back-to-indentation)
1412 (looking-at "return ggg")))
1413 (python-nav-forward-sexp -1)
1414 (should (looking-at "def func():"))))
1416 (ert-deftest python-nav-forward-sexp-3 ()
1417 (python-tests-with-temp-buffer
1419 from some_module import some_sub_module
1420 from another_module import another_sub_module
1422 def another_statement():
1423 pass
1425 (python-tests-look-at "some_module")
1426 (python-nav-forward-sexp)
1427 (should (looking-at " import"))
1428 (python-nav-forward-sexp)
1429 (should (looking-at " some_sub_module"))
1430 (python-nav-forward-sexp)
1431 (should (looking-at "$"))
1432 (should
1433 (save-excursion
1434 (back-to-indentation)
1435 (looking-at
1436 "from some_module import some_sub_module")))
1437 (python-nav-forward-sexp)
1438 (should (looking-at "$"))
1439 (should
1440 (save-excursion
1441 (back-to-indentation)
1442 (looking-at
1443 "from another_module import another_sub_module")))
1444 (python-nav-forward-sexp)
1445 (should (looking-at "$"))
1446 (should
1447 (save-excursion
1448 (back-to-indentation)
1449 (looking-at
1450 "pass")))
1451 (python-nav-forward-sexp -1)
1452 (should (looking-at "def another_statement():"))
1453 (python-nav-forward-sexp -1)
1454 (should (looking-at "from another_module import another_sub_module"))
1455 (python-nav-forward-sexp -1)
1456 (should (looking-at "from some_module import some_sub_module"))))
1458 (ert-deftest python-nav-forward-sexp-safe-1 ()
1459 (python-tests-with-temp-buffer
1461 profile = Profile.objects.create(user=request.user)
1462 profile.notify()
1464 (python-tests-look-at "profile =")
1465 (python-nav-forward-sexp-safe 1)
1466 (should (looking-at "$"))
1467 (beginning-of-line 1)
1468 (python-tests-look-at "user=request.user")
1469 (python-nav-forward-sexp-safe -1)
1470 (should (looking-at "(user=request.user)"))
1471 (python-nav-forward-sexp-safe -4)
1472 (should (looking-at "profile ="))
1473 (python-tests-look-at "user=request.user")
1474 (python-nav-forward-sexp-safe 3)
1475 (should (looking-at ")"))
1476 (python-nav-forward-sexp-safe 1)
1477 (should (looking-at "$"))
1478 (python-nav-forward-sexp-safe 1)
1479 (should (looking-at "$"))))
1481 (ert-deftest python-nav-up-list-1 ()
1482 (python-tests-with-temp-buffer
1484 def f():
1485 if True:
1486 return [i for i in range(3)]
1488 (python-tests-look-at "3)]")
1489 (python-nav-up-list)
1490 (should (looking-at "]"))
1491 (python-nav-up-list)
1492 (should (looking-at "$"))))
1494 (ert-deftest python-nav-backward-up-list-1 ()
1495 :expected-result :failed
1496 (python-tests-with-temp-buffer
1498 def f():
1499 if True:
1500 return [i for i in range(3)]
1502 (python-tests-look-at "3)]")
1503 (python-nav-backward-up-list)
1504 (should (looking-at "(3)\\]"))
1505 (python-nav-backward-up-list)
1506 (should (looking-at
1507 "\\[i for i in range(3)\\]"))
1508 ;; FIXME: Need to move to beginning-of-statement.
1509 (python-nav-backward-up-list)
1510 (should (looking-at
1511 "return \\[i for i in range(3)\\]"))
1512 (python-nav-backward-up-list)
1513 (should (looking-at "if True:"))
1514 (python-nav-backward-up-list)
1515 (should (looking-at "def f():"))))
1518 ;;; Shell integration
1520 (defvar python-tests-shell-interpreter "python")
1522 (ert-deftest python-shell-get-process-name-1 ()
1523 "Check process name calculation on different scenarios."
1524 (python-tests-with-temp-buffer
1526 (should (string= (python-shell-get-process-name nil)
1527 python-shell-buffer-name))
1528 ;; When the `current-buffer' doesn't have `buffer-file-name', even
1529 ;; if dedicated flag is non-nil should not include its name.
1530 (should (string= (python-shell-get-process-name t)
1531 python-shell-buffer-name)))
1532 (python-tests-with-temp-file
1534 ;; `buffer-file-name' is non-nil but the dedicated flag is nil and
1535 ;; should be respected.
1536 (should (string= (python-shell-get-process-name nil)
1537 python-shell-buffer-name))
1538 (should (string=
1539 (python-shell-get-process-name t)
1540 (format "%s[%s]" python-shell-buffer-name buffer-file-name)))))
1542 (ert-deftest python-shell-internal-get-process-name-1 ()
1543 "Check the internal process name is config-unique."
1544 (let* ((python-shell-interpreter python-tests-shell-interpreter)
1545 (python-shell-interpreter-args "")
1546 (python-shell-prompt-regexp ">>> ")
1547 (python-shell-prompt-block-regexp "[.][.][.] ")
1548 (python-shell-setup-codes "")
1549 (python-shell-process-environment "")
1550 (python-shell-extra-pythonpaths "")
1551 (python-shell-exec-path "")
1552 (python-shell-virtualenv-path "")
1553 (expected (python-tests-with-temp-buffer
1554 "" (python-shell-internal-get-process-name))))
1555 ;; Same configurations should match.
1556 (should
1557 (string= expected
1558 (python-tests-with-temp-buffer
1559 "" (python-shell-internal-get-process-name))))
1560 (let ((python-shell-interpreter-args "-B"))
1561 ;; A minimal change should generate different names.
1562 (should
1563 (not (string=
1564 expected
1565 (python-tests-with-temp-buffer
1566 "" (python-shell-internal-get-process-name))))))))
1568 (ert-deftest python-shell-parse-command-1 ()
1569 "Check the command to execute is calculated correctly.
1570 Using `python-shell-interpreter' and
1571 `python-shell-interpreter-args'."
1572 (skip-unless (executable-find python-tests-shell-interpreter))
1573 (let ((python-shell-interpreter (executable-find
1574 python-tests-shell-interpreter))
1575 (python-shell-interpreter-args "-B"))
1576 (should (string=
1577 (format "%s %s"
1578 python-shell-interpreter
1579 python-shell-interpreter-args)
1580 (python-shell-parse-command)))))
1582 (ert-deftest python-shell-calculate-process-environment-1 ()
1583 "Test `python-shell-process-environment' modification."
1584 (let* ((original-process-environment process-environment)
1585 (python-shell-process-environment
1586 '("TESTVAR1=value1" "TESTVAR2=value2"))
1587 (process-environment
1588 (python-shell-calculate-process-environment)))
1589 (should (equal (getenv "TESTVAR1") "value1"))
1590 (should (equal (getenv "TESTVAR2") "value2"))))
1592 (ert-deftest python-shell-calculate-process-environment-2 ()
1593 "Test `python-shell-extra-pythonpaths' modification."
1594 (let* ((original-process-environment process-environment)
1595 (original-pythonpath (getenv "PYTHONPATH"))
1596 (paths '("path1" "path2"))
1597 (python-shell-extra-pythonpaths paths)
1598 (process-environment
1599 (python-shell-calculate-process-environment)))
1600 (should (equal (getenv "PYTHONPATH")
1601 (concat
1602 (mapconcat 'identity paths path-separator)
1603 path-separator original-pythonpath)))))
1605 (ert-deftest python-shell-calculate-process-environment-3 ()
1606 "Test `python-shell-virtualenv-path' modification."
1607 (let* ((original-process-environment process-environment)
1608 (original-path (or (getenv "PATH") ""))
1609 (python-shell-virtualenv-path
1610 (directory-file-name user-emacs-directory))
1611 (process-environment
1612 (python-shell-calculate-process-environment)))
1613 (should (not (getenv "PYTHONHOME")))
1614 (should (string= (getenv "VIRTUAL_ENV") python-shell-virtualenv-path))
1615 (should (equal (getenv "PATH")
1616 (format "%s/bin%s%s"
1617 python-shell-virtualenv-path
1618 path-separator original-path)))))
1620 (ert-deftest python-shell-calculate-exec-path-1 ()
1621 "Test `python-shell-exec-path' modification."
1622 (let* ((original-exec-path exec-path)
1623 (python-shell-exec-path '("path1" "path2"))
1624 (exec-path (python-shell-calculate-exec-path)))
1625 (should (equal
1626 exec-path
1627 (append python-shell-exec-path
1628 original-exec-path)))))
1630 (ert-deftest python-shell-calculate-exec-path-2 ()
1631 "Test `python-shell-exec-path' modification."
1632 (let* ((original-exec-path exec-path)
1633 (python-shell-virtualenv-path
1634 (directory-file-name user-emacs-directory))
1635 (exec-path (python-shell-calculate-exec-path)))
1636 (should (equal
1637 exec-path
1638 (append (cons
1639 (format "%s/bin" python-shell-virtualenv-path)
1640 original-exec-path))))))
1642 (ert-deftest python-shell-make-comint-1 ()
1643 "Check comint creation for global shell buffer."
1644 (skip-unless (executable-find python-tests-shell-interpreter))
1645 ;; The interpreter can get killed too quickly to allow it to clean
1646 ;; up the tempfiles that the default python-shell-setup-codes create,
1647 ;; so it leaves tempfiles behind, which is a minor irritation.
1648 (let* ((python-shell-setup-codes nil)
1649 (python-shell-interpreter
1650 (executable-find python-tests-shell-interpreter))
1651 (proc-name (python-shell-get-process-name nil))
1652 (shell-buffer
1653 (python-tests-with-temp-buffer
1654 "" (python-shell-make-comint
1655 (python-shell-parse-command) proc-name)))
1656 (process (get-buffer-process shell-buffer)))
1657 (unwind-protect
1658 (progn
1659 (set-process-query-on-exit-flag process nil)
1660 (should (process-live-p process))
1661 (with-current-buffer shell-buffer
1662 (should (eq major-mode 'inferior-python-mode))
1663 (should (string= (buffer-name) (format "*%s*" proc-name)))))
1664 (kill-buffer shell-buffer))))
1666 (ert-deftest python-shell-make-comint-2 ()
1667 "Check comint creation for internal shell buffer."
1668 (skip-unless (executable-find python-tests-shell-interpreter))
1669 (let* ((python-shell-setup-codes nil)
1670 (python-shell-interpreter
1671 (executable-find python-tests-shell-interpreter))
1672 (proc-name (python-shell-internal-get-process-name))
1673 (shell-buffer
1674 (python-tests-with-temp-buffer
1675 "" (python-shell-make-comint
1676 (python-shell-parse-command) proc-name nil t)))
1677 (process (get-buffer-process shell-buffer)))
1678 (unwind-protect
1679 (progn
1680 (set-process-query-on-exit-flag process nil)
1681 (should (process-live-p process))
1682 (with-current-buffer shell-buffer
1683 (should (eq major-mode 'inferior-python-mode))
1684 (should (string= (buffer-name) (format " *%s*" proc-name)))))
1685 (kill-buffer shell-buffer))))
1687 (ert-deftest python-shell-get-process-1 ()
1688 "Check dedicated shell process preference over global."
1689 (skip-unless (executable-find python-tests-shell-interpreter))
1690 (python-tests-with-temp-file
1692 (let* ((python-shell-setup-codes nil)
1693 (python-shell-interpreter
1694 (executable-find python-tests-shell-interpreter))
1695 (global-proc-name (python-shell-get-process-name nil))
1696 (dedicated-proc-name (python-shell-get-process-name t))
1697 (global-shell-buffer
1698 (python-shell-make-comint
1699 (python-shell-parse-command) global-proc-name))
1700 (dedicated-shell-buffer
1701 (python-shell-make-comint
1702 (python-shell-parse-command) dedicated-proc-name))
1703 (global-process (get-buffer-process global-shell-buffer))
1704 (dedicated-process (get-buffer-process dedicated-shell-buffer)))
1705 (unwind-protect
1706 (progn
1707 (set-process-query-on-exit-flag global-process nil)
1708 (set-process-query-on-exit-flag dedicated-process nil)
1709 ;; Prefer dedicated if global also exists.
1710 (should (equal (python-shell-get-process) dedicated-process))
1711 (kill-buffer dedicated-shell-buffer)
1712 ;; If there's only global, use it.
1713 (should (equal (python-shell-get-process) global-process))
1714 (kill-buffer global-shell-buffer)
1715 ;; No buffer available.
1716 (should (not (python-shell-get-process))))
1717 (ignore-errors (kill-buffer global-shell-buffer))
1718 (ignore-errors (kill-buffer dedicated-shell-buffer))))))
1720 (ert-deftest python-shell-get-or-create-process-1 ()
1721 "Check shell process creation fallback."
1722 :expected-result :failed
1723 (python-tests-with-temp-file
1725 ;; XXX: Break early until we can skip stuff. We need to mimic
1726 ;; user interaction because `python-shell-get-or-create-process'
1727 ;; asks for all arguments interactively when a shell process
1728 ;; doesn't exist.
1729 (should nil)
1730 (let* ((python-shell-interpreter
1731 (executable-find python-tests-shell-interpreter))
1732 (use-dialog-box)
1733 (dedicated-process-name (python-shell-get-process-name t))
1734 (dedicated-process (python-shell-get-or-create-process))
1735 (dedicated-shell-buffer (process-buffer dedicated-process)))
1736 (unwind-protect
1737 (progn
1738 (set-process-query-on-exit-flag dedicated-process nil)
1739 ;; Prefer dedicated if not buffer exist.
1740 (should (equal (process-name dedicated-process)
1741 dedicated-process-name))
1742 (kill-buffer dedicated-shell-buffer)
1743 ;; No buffer available.
1744 (should (not (python-shell-get-process))))
1745 (ignore-errors (kill-buffer dedicated-shell-buffer))))))
1747 (ert-deftest python-shell-internal-get-or-create-process-1 ()
1748 "Check internal shell process creation fallback."
1749 (skip-unless (executable-find python-tests-shell-interpreter))
1750 (python-tests-with-temp-file
1752 (should (not (process-live-p (python-shell-internal-get-process-name))))
1753 (let* ((python-shell-interpreter
1754 (executable-find python-tests-shell-interpreter))
1755 (internal-process-name (python-shell-internal-get-process-name))
1756 (internal-process (python-shell-internal-get-or-create-process))
1757 (internal-shell-buffer (process-buffer internal-process)))
1758 (unwind-protect
1759 (progn
1760 (set-process-query-on-exit-flag internal-process nil)
1761 (should (equal (process-name internal-process)
1762 internal-process-name))
1763 (should (equal internal-process
1764 (python-shell-internal-get-or-create-process)))
1765 ;; No user buffer available.
1766 (should (not (python-shell-get-process)))
1767 (kill-buffer internal-shell-buffer))
1768 (ignore-errors (kill-buffer internal-shell-buffer))))))
1771 ;;; Shell completion
1774 ;;; PDB Track integration
1777 ;;; Symbol completion
1780 ;;; Fill paragraph
1783 ;;; Skeletons
1786 ;;; FFAP
1789 ;;; Code check
1792 ;;; Eldoc
1795 ;;; Imenu
1797 (ert-deftest python-imenu-create-index-1 ()
1798 (python-tests-with-temp-buffer
1800 class Foo(models.Model):
1801 pass
1804 class Bar(models.Model):
1805 pass
1808 def decorator(arg1, arg2, arg3):
1809 '''print decorated function call data to stdout.
1811 Usage:
1813 @decorator('arg1', 'arg2')
1814 def func(a, b, c=True):
1815 pass
1818 def wrap(f):
1819 print ('wrap')
1820 def wrapped_f(*args):
1821 print ('wrapped_f')
1822 print ('Decorator arguments:', arg1, arg2, arg3)
1823 f(*args)
1824 print ('called f(*args)')
1825 return wrapped_f
1826 return wrap
1829 class Baz(object):
1831 def a(self):
1832 pass
1834 def b(self):
1835 pass
1837 class Frob(object):
1839 def c(self):
1840 pass
1842 (goto-char (point-max))
1843 (should (equal
1844 (list
1845 (cons "Foo (class)" (copy-marker 2))
1846 (cons "Bar (class)" (copy-marker 38))
1847 (list
1848 "decorator (def)"
1849 (cons "*function definition*" (copy-marker 74))
1850 (list
1851 "wrap (def)"
1852 (cons "*function definition*" (copy-marker 254))
1853 (cons "wrapped_f (def)" (copy-marker 294))))
1854 (list
1855 "Baz (class)"
1856 (cons "*class definition*" (copy-marker 519))
1857 (cons "a (def)" (copy-marker 539))
1858 (cons "b (def)" (copy-marker 570))
1859 (list
1860 "Frob (class)"
1861 (cons "*class definition*" (copy-marker 601))
1862 (cons "c (def)" (copy-marker 626)))))
1863 (python-imenu-create-index)))))
1865 (ert-deftest python-imenu-create-index-2 ()
1866 (python-tests-with-temp-buffer
1868 class Foo(object):
1869 def foo(self):
1870 def foo1():
1871 pass
1873 def foobar(self):
1874 pass
1876 (goto-char (point-max))
1877 (should (equal
1878 (list
1879 (list
1880 "Foo (class)"
1881 (cons "*class definition*" (copy-marker 2))
1882 (list
1883 "foo (def)"
1884 (cons "*function definition*" (copy-marker 21))
1885 (cons "foo1 (def)" (copy-marker 40)))
1886 (cons "foobar (def)" (copy-marker 78))))
1887 (python-imenu-create-index)))))
1889 (ert-deftest python-imenu-create-index-3 ()
1890 (python-tests-with-temp-buffer
1892 class Foo(object):
1893 def foo(self):
1894 def foo1():
1895 pass
1896 def foo2():
1897 pass
1899 (goto-char (point-max))
1900 (should (equal
1901 (list
1902 (list
1903 "Foo (class)"
1904 (cons "*class definition*" (copy-marker 2))
1905 (list
1906 "foo (def)"
1907 (cons "*function definition*" (copy-marker 21))
1908 (cons "foo1 (def)" (copy-marker 40))
1909 (cons "foo2 (def)" (copy-marker 77)))))
1910 (python-imenu-create-index)))))
1912 (ert-deftest python-imenu-create-index-4 ()
1913 (python-tests-with-temp-buffer
1915 class Foo(object):
1916 class Bar(object):
1917 def __init__(self):
1918 pass
1920 def __str__(self):
1921 pass
1923 def __init__(self):
1924 pass
1926 (goto-char (point-max))
1927 (should (equal
1928 (list
1929 (list
1930 "Foo (class)"
1931 (cons "*class definition*" (copy-marker 2))
1932 (list
1933 "Bar (class)"
1934 (cons "*class definition*" (copy-marker 21))
1935 (cons "__init__ (def)" (copy-marker 44))
1936 (cons "__str__ (def)" (copy-marker 90)))
1937 (cons "__init__ (def)" (copy-marker 135))))
1938 (python-imenu-create-index)))))
1940 (ert-deftest python-imenu-create-flat-index-1 ()
1941 (python-tests-with-temp-buffer
1943 class Foo(models.Model):
1944 pass
1947 class Bar(models.Model):
1948 pass
1951 def decorator(arg1, arg2, arg3):
1952 '''print decorated function call data to stdout.
1954 Usage:
1956 @decorator('arg1', 'arg2')
1957 def func(a, b, c=True):
1958 pass
1961 def wrap(f):
1962 print ('wrap')
1963 def wrapped_f(*args):
1964 print ('wrapped_f')
1965 print ('Decorator arguments:', arg1, arg2, arg3)
1966 f(*args)
1967 print ('called f(*args)')
1968 return wrapped_f
1969 return wrap
1972 class Baz(object):
1974 def a(self):
1975 pass
1977 def b(self):
1978 pass
1980 class Frob(object):
1982 def c(self):
1983 pass
1985 (goto-char (point-max))
1986 (should (equal
1987 (list (cons "Foo" (copy-marker 2))
1988 (cons "Bar" (copy-marker 38))
1989 (cons "decorator" (copy-marker 74))
1990 (cons "decorator.wrap" (copy-marker 254))
1991 (cons "decorator.wrap.wrapped_f" (copy-marker 294))
1992 (cons "Baz" (copy-marker 519))
1993 (cons "Baz.a" (copy-marker 539))
1994 (cons "Baz.b" (copy-marker 570))
1995 (cons "Baz.Frob" (copy-marker 601))
1996 (cons "Baz.Frob.c" (copy-marker 626)))
1997 (python-imenu-create-flat-index)))))
1999 (ert-deftest python-imenu-create-flat-index-2 ()
2000 (python-tests-with-temp-buffer
2002 class Foo(object):
2003 class Bar(object):
2004 def __init__(self):
2005 pass
2007 def __str__(self):
2008 pass
2010 def __init__(self):
2011 pass
2013 (goto-char (point-max))
2014 (should (equal
2015 (list
2016 (cons "Foo" (copy-marker 2))
2017 (cons "Foo.Bar" (copy-marker 21))
2018 (cons "Foo.Bar.__init__" (copy-marker 44))
2019 (cons "Foo.Bar.__str__" (copy-marker 90))
2020 (cons "Foo.__init__" (copy-marker 135)))
2021 (python-imenu-create-flat-index)))))
2024 ;;; Misc helpers
2026 (ert-deftest python-info-current-defun-1 ()
2027 (python-tests-with-temp-buffer
2029 def foo(a, b):
2031 (forward-line 1)
2032 (should (string= "foo" (python-info-current-defun)))
2033 (should (string= "def foo" (python-info-current-defun t)))
2034 (forward-line 1)
2035 (should (not (python-info-current-defun)))
2036 (indent-for-tab-command)
2037 (should (string= "foo" (python-info-current-defun)))
2038 (should (string= "def foo" (python-info-current-defun t)))))
2040 (ert-deftest python-info-current-defun-2 ()
2041 (python-tests-with-temp-buffer
2043 class C(object):
2045 def m(self):
2046 if True:
2047 return [i for i in range(3)]
2048 else:
2049 return []
2051 def b():
2052 do_b()
2054 def a():
2055 do_a()
2057 def c(self):
2058 do_c()
2060 (forward-line 1)
2061 (should (string= "C" (python-info-current-defun)))
2062 (should (string= "class C" (python-info-current-defun t)))
2063 (python-tests-look-at "return [i for ")
2064 (should (string= "C.m" (python-info-current-defun)))
2065 (should (string= "def C.m" (python-info-current-defun t)))
2066 (python-tests-look-at "def b():")
2067 (should (string= "C.m.b" (python-info-current-defun)))
2068 (should (string= "def C.m.b" (python-info-current-defun t)))
2069 (forward-line 2)
2070 (indent-for-tab-command)
2071 (python-indent-dedent-line-backspace 1)
2072 (should (string= "C.m" (python-info-current-defun)))
2073 (should (string= "def C.m" (python-info-current-defun t)))
2074 (python-tests-look-at "def c(self):")
2075 (forward-line -1)
2076 (indent-for-tab-command)
2077 (should (string= "C.m.a" (python-info-current-defun)))
2078 (should (string= "def C.m.a" (python-info-current-defun t)))
2079 (python-indent-dedent-line-backspace 1)
2080 (should (string= "C.m" (python-info-current-defun)))
2081 (should (string= "def C.m" (python-info-current-defun t)))
2082 (python-indent-dedent-line-backspace 1)
2083 (should (string= "C" (python-info-current-defun)))
2084 (should (string= "class C" (python-info-current-defun t)))
2085 (python-tests-look-at "def c(self):")
2086 (should (string= "C.c" (python-info-current-defun)))
2087 (should (string= "def C.c" (python-info-current-defun t)))
2088 (python-tests-look-at "do_c()")
2089 (should (string= "C.c" (python-info-current-defun)))
2090 (should (string= "def C.c" (python-info-current-defun t)))))
2092 (ert-deftest python-info-current-defun-3 ()
2093 (python-tests-with-temp-buffer
2095 def decoratorFunctionWithArguments(arg1, arg2, arg3):
2096 '''print decorated function call data to stdout.
2098 Usage:
2100 @decoratorFunctionWithArguments('arg1', 'arg2')
2101 def func(a, b, c=True):
2102 pass
2105 def wwrap(f):
2106 print 'Inside wwrap()'
2107 def wrapped_f(*args):
2108 print 'Inside wrapped_f()'
2109 print 'Decorator arguments:', arg1, arg2, arg3
2110 f(*args)
2111 print 'After f(*args)'
2112 return wrapped_f
2113 return wwrap
2115 (python-tests-look-at "def wwrap(f):")
2116 (forward-line -1)
2117 (should (not (python-info-current-defun)))
2118 (indent-for-tab-command 1)
2119 (should (string= (python-info-current-defun)
2120 "decoratorFunctionWithArguments"))
2121 (should (string= (python-info-current-defun t)
2122 "def decoratorFunctionWithArguments"))
2123 (python-tests-look-at "def wrapped_f(*args):")
2124 (should (string= (python-info-current-defun)
2125 "decoratorFunctionWithArguments.wwrap.wrapped_f"))
2126 (should (string= (python-info-current-defun t)
2127 "def decoratorFunctionWithArguments.wwrap.wrapped_f"))
2128 (python-tests-look-at "return wrapped_f")
2129 (should (string= (python-info-current-defun)
2130 "decoratorFunctionWithArguments.wwrap"))
2131 (should (string= (python-info-current-defun t)
2132 "def decoratorFunctionWithArguments.wwrap"))
2133 (end-of-line 1)
2134 (python-tests-look-at "return wwrap")
2135 (should (string= (python-info-current-defun)
2136 "decoratorFunctionWithArguments"))
2137 (should (string= (python-info-current-defun t)
2138 "def decoratorFunctionWithArguments"))))
2140 (ert-deftest python-info-current-symbol-1 ()
2141 (python-tests-with-temp-buffer
2143 class C(object):
2145 def m(self):
2146 self.c()
2148 def c(self):
2149 print ('a')
2151 (python-tests-look-at "self.c()")
2152 (should (string= "self.c" (python-info-current-symbol)))
2153 (should (string= "C.c" (python-info-current-symbol t)))))
2155 (ert-deftest python-info-current-symbol-2 ()
2156 (python-tests-with-temp-buffer
2158 class C(object):
2160 class M(object):
2162 def a(self):
2163 self.c()
2165 def c(self):
2166 pass
2168 (python-tests-look-at "self.c()")
2169 (should (string= "self.c" (python-info-current-symbol)))
2170 (should (string= "C.M.c" (python-info-current-symbol t)))))
2172 (ert-deftest python-info-current-symbol-3 ()
2173 "Keywords should not be considered symbols."
2174 :expected-result :failed
2175 (python-tests-with-temp-buffer
2177 class C(object):
2178 pass
2180 ;; FIXME: keywords are not symbols.
2181 (python-tests-look-at "class C")
2182 (should (not (python-info-current-symbol)))
2183 (should (not (python-info-current-symbol t)))
2184 (python-tests-look-at "C(object)")
2185 (should (string= "C" (python-info-current-symbol)))
2186 (should (string= "class C" (python-info-current-symbol t)))))
2188 (ert-deftest python-info-statement-starts-block-p-1 ()
2189 (python-tests-with-temp-buffer
2191 def long_function_name(
2192 var_one, var_two, var_three,
2193 var_four):
2194 print (var_one)
2196 (python-tests-look-at "def long_function_name")
2197 (should (python-info-statement-starts-block-p))
2198 (python-tests-look-at "print (var_one)")
2199 (python-util-forward-comment -1)
2200 (should (python-info-statement-starts-block-p))))
2202 (ert-deftest python-info-statement-starts-block-p-2 ()
2203 (python-tests-with-temp-buffer
2205 if width == 0 and height == 0 and \\\\
2206 color == 'red' and emphasis == 'strong' or \\\\
2207 highlight > 100:
2208 raise ValueError('sorry, you lose')
2210 (python-tests-look-at "if width == 0 and")
2211 (should (python-info-statement-starts-block-p))
2212 (python-tests-look-at "raise ValueError(")
2213 (python-util-forward-comment -1)
2214 (should (python-info-statement-starts-block-p))))
2216 (ert-deftest python-info-statement-ends-block-p-1 ()
2217 (python-tests-with-temp-buffer
2219 def long_function_name(
2220 var_one, var_two, var_three,
2221 var_four):
2222 print (var_one)
2224 (python-tests-look-at "print (var_one)")
2225 (should (python-info-statement-ends-block-p))))
2227 (ert-deftest python-info-statement-ends-block-p-2 ()
2228 (python-tests-with-temp-buffer
2230 if width == 0 and height == 0 and \\\\
2231 color == 'red' and emphasis == 'strong' or \\\\
2232 highlight > 100:
2233 raise ValueError(
2234 'sorry, you lose'
2238 (python-tests-look-at "raise ValueError(")
2239 (should (python-info-statement-ends-block-p))))
2241 (ert-deftest python-info-beginning-of-statement-p-1 ()
2242 (python-tests-with-temp-buffer
2244 def long_function_name(
2245 var_one, var_two, var_three,
2246 var_four):
2247 print (var_one)
2249 (python-tests-look-at "def long_function_name")
2250 (should (python-info-beginning-of-statement-p))
2251 (forward-char 10)
2252 (should (not (python-info-beginning-of-statement-p)))
2253 (python-tests-look-at "print (var_one)")
2254 (should (python-info-beginning-of-statement-p))
2255 (goto-char (line-beginning-position))
2256 (should (not (python-info-beginning-of-statement-p)))))
2258 (ert-deftest python-info-beginning-of-statement-p-2 ()
2259 (python-tests-with-temp-buffer
2261 if width == 0 and height == 0 and \\\\
2262 color == 'red' and emphasis == 'strong' or \\\\
2263 highlight > 100:
2264 raise ValueError(
2265 'sorry, you lose'
2269 (python-tests-look-at "if width == 0 and")
2270 (should (python-info-beginning-of-statement-p))
2271 (forward-char 10)
2272 (should (not (python-info-beginning-of-statement-p)))
2273 (python-tests-look-at "raise ValueError(")
2274 (should (python-info-beginning-of-statement-p))
2275 (goto-char (line-beginning-position))
2276 (should (not (python-info-beginning-of-statement-p)))))
2278 (ert-deftest python-info-end-of-statement-p-1 ()
2279 (python-tests-with-temp-buffer
2281 def long_function_name(
2282 var_one, var_two, var_three,
2283 var_four):
2284 print (var_one)
2286 (python-tests-look-at "def long_function_name")
2287 (should (not (python-info-end-of-statement-p)))
2288 (end-of-line)
2289 (should (not (python-info-end-of-statement-p)))
2290 (python-tests-look-at "print (var_one)")
2291 (python-util-forward-comment -1)
2292 (should (python-info-end-of-statement-p))
2293 (python-tests-look-at "print (var_one)")
2294 (should (not (python-info-end-of-statement-p)))
2295 (end-of-line)
2296 (should (python-info-end-of-statement-p))))
2298 (ert-deftest python-info-end-of-statement-p-2 ()
2299 (python-tests-with-temp-buffer
2301 if width == 0 and height == 0 and \\\\
2302 color == 'red' and emphasis == 'strong' or \\\\
2303 highlight > 100:
2304 raise ValueError(
2305 'sorry, you lose'
2309 (python-tests-look-at "if width == 0 and")
2310 (should (not (python-info-end-of-statement-p)))
2311 (end-of-line)
2312 (should (not (python-info-end-of-statement-p)))
2313 (python-tests-look-at "raise ValueError(")
2314 (python-util-forward-comment -1)
2315 (should (python-info-end-of-statement-p))
2316 (python-tests-look-at "raise ValueError(")
2317 (should (not (python-info-end-of-statement-p)))
2318 (end-of-line)
2319 (should (not (python-info-end-of-statement-p)))
2320 (goto-char (point-max))
2321 (python-util-forward-comment -1)
2322 (should (python-info-end-of-statement-p))))
2324 (ert-deftest python-info-beginning-of-block-p-1 ()
2325 (python-tests-with-temp-buffer
2327 def long_function_name(
2328 var_one, var_two, var_three,
2329 var_four):
2330 print (var_one)
2332 (python-tests-look-at "def long_function_name")
2333 (should (python-info-beginning-of-block-p))
2334 (python-tests-look-at "var_one, var_two, var_three,")
2335 (should (not (python-info-beginning-of-block-p)))
2336 (python-tests-look-at "print (var_one)")
2337 (should (not (python-info-beginning-of-block-p)))))
2339 (ert-deftest python-info-beginning-of-block-p-2 ()
2340 (python-tests-with-temp-buffer
2342 if width == 0 and height == 0 and \\\\
2343 color == 'red' and emphasis == 'strong' or \\\\
2344 highlight > 100:
2345 raise ValueError(
2346 'sorry, you lose'
2350 (python-tests-look-at "if width == 0 and")
2351 (should (python-info-beginning-of-block-p))
2352 (python-tests-look-at "color == 'red' and emphasis")
2353 (should (not (python-info-beginning-of-block-p)))
2354 (python-tests-look-at "raise ValueError(")
2355 (should (not (python-info-beginning-of-block-p)))))
2357 (ert-deftest python-info-end-of-block-p-1 ()
2358 (python-tests-with-temp-buffer
2360 def long_function_name(
2361 var_one, var_two, var_three,
2362 var_four):
2363 print (var_one)
2365 (python-tests-look-at "def long_function_name")
2366 (should (not (python-info-end-of-block-p)))
2367 (python-tests-look-at "var_one, var_two, var_three,")
2368 (should (not (python-info-end-of-block-p)))
2369 (python-tests-look-at "var_four):")
2370 (end-of-line)
2371 (should (not (python-info-end-of-block-p)))
2372 (python-tests-look-at "print (var_one)")
2373 (should (not (python-info-end-of-block-p)))
2374 (end-of-line 1)
2375 (should (python-info-end-of-block-p))))
2377 (ert-deftest python-info-end-of-block-p-2 ()
2378 (python-tests-with-temp-buffer
2380 if width == 0 and height == 0 and \\\\
2381 color == 'red' and emphasis == 'strong' or \\\\
2382 highlight > 100:
2383 raise ValueError(
2384 'sorry, you lose'
2388 (python-tests-look-at "if width == 0 and")
2389 (should (not (python-info-end-of-block-p)))
2390 (python-tests-look-at "color == 'red' and emphasis == 'strong' or")
2391 (should (not (python-info-end-of-block-p)))
2392 (python-tests-look-at "highlight > 100:")
2393 (end-of-line)
2394 (should (not (python-info-end-of-block-p)))
2395 (python-tests-look-at "raise ValueError(")
2396 (should (not (python-info-end-of-block-p)))
2397 (end-of-line 1)
2398 (should (not (python-info-end-of-block-p)))
2399 (goto-char (point-max))
2400 (python-util-forward-comment -1)
2401 (should (python-info-end-of-block-p))))
2403 (ert-deftest python-info-closing-block-1 ()
2404 (python-tests-with-temp-buffer
2406 if request.user.is_authenticated():
2407 try:
2408 profile = request.user.get_profile()
2409 except Profile.DoesNotExist:
2410 profile = Profile.objects.create(user=request.user)
2411 else:
2412 if profile.stats:
2413 profile.recalculate_stats()
2414 else:
2415 profile.clear_stats()
2416 finally:
2417 profile.views += 1
2418 profile.save()
2420 (python-tests-look-at "try:")
2421 (should (not (python-info-closing-block)))
2422 (python-tests-look-at "except Profile.DoesNotExist:")
2423 (should (= (python-tests-look-at "try:" -1 t)
2424 (python-info-closing-block)))
2425 (python-tests-look-at "else:")
2426 (should (= (python-tests-look-at "except Profile.DoesNotExist:" -1 t)
2427 (python-info-closing-block)))
2428 (python-tests-look-at "if profile.stats:")
2429 (should (not (python-info-closing-block)))
2430 (python-tests-look-at "else:")
2431 (should (= (python-tests-look-at "if profile.stats:" -1 t)
2432 (python-info-closing-block)))
2433 (python-tests-look-at "finally:")
2434 (should (= (python-tests-look-at "else:" -2 t)
2435 (python-info-closing-block)))))
2437 (ert-deftest python-info-closing-block-2 ()
2438 (python-tests-with-temp-buffer
2440 if request.user.is_authenticated():
2441 profile = Profile.objects.get_or_create(user=request.user)
2442 if profile.stats:
2443 profile.recalculate_stats()
2445 data = {
2446 'else': 'do it'
2448 'else'
2450 (python-tests-look-at "'else': 'do it'")
2451 (should (not (python-info-closing-block)))
2452 (python-tests-look-at "'else'")
2453 (should (not (python-info-closing-block)))))
2455 (ert-deftest python-info-line-ends-backslash-p-1 ()
2456 (python-tests-with-temp-buffer
2458 objects = Thing.objects.all() \\\\
2459 .filter(
2460 type='toy',
2461 status='bought'
2462 ) \\\\
2463 .aggregate(
2464 Sum('amount')
2465 ) \\\\
2466 .values_list()
2468 (should (python-info-line-ends-backslash-p 2)) ; .filter(...
2469 (should (python-info-line-ends-backslash-p 3))
2470 (should (python-info-line-ends-backslash-p 4))
2471 (should (python-info-line-ends-backslash-p 5))
2472 (should (python-info-line-ends-backslash-p 6)) ; ) \...
2473 (should (python-info-line-ends-backslash-p 7))
2474 (should (python-info-line-ends-backslash-p 8))
2475 (should (python-info-line-ends-backslash-p 9))
2476 (should (not (python-info-line-ends-backslash-p 10))))) ; .values_list()...
2478 (ert-deftest python-info-beginning-of-backslash-1 ()
2479 (python-tests-with-temp-buffer
2481 objects = Thing.objects.all() \\\\
2482 .filter(
2483 type='toy',
2484 status='bought'
2485 ) \\\\
2486 .aggregate(
2487 Sum('amount')
2488 ) \\\\
2489 .values_list()
2491 (let ((first 2)
2492 (second (python-tests-look-at ".filter("))
2493 (third (python-tests-look-at ".aggregate(")))
2494 (should (= first (python-info-beginning-of-backslash 2)))
2495 (should (= second (python-info-beginning-of-backslash 3)))
2496 (should (= second (python-info-beginning-of-backslash 4)))
2497 (should (= second (python-info-beginning-of-backslash 5)))
2498 (should (= second (python-info-beginning-of-backslash 6)))
2499 (should (= third (python-info-beginning-of-backslash 7)))
2500 (should (= third (python-info-beginning-of-backslash 8)))
2501 (should (= third (python-info-beginning-of-backslash 9)))
2502 (should (not (python-info-beginning-of-backslash 10))))))
2504 (ert-deftest python-info-continuation-line-p-1 ()
2505 (python-tests-with-temp-buffer
2507 if width == 0 and height == 0 and \\\\
2508 color == 'red' and emphasis == 'strong' or \\\\
2509 highlight > 100:
2510 raise ValueError(
2511 'sorry, you lose'
2515 (python-tests-look-at "if width == 0 and height == 0 and")
2516 (should (not (python-info-continuation-line-p)))
2517 (python-tests-look-at "color == 'red' and emphasis == 'strong' or")
2518 (should (python-info-continuation-line-p))
2519 (python-tests-look-at "highlight > 100:")
2520 (should (python-info-continuation-line-p))
2521 (python-tests-look-at "raise ValueError(")
2522 (should (not (python-info-continuation-line-p)))
2523 (python-tests-look-at "'sorry, you lose'")
2524 (should (python-info-continuation-line-p))
2525 (forward-line 1)
2526 (should (python-info-continuation-line-p))
2527 (python-tests-look-at ")")
2528 (should (python-info-continuation-line-p))
2529 (forward-line 1)
2530 (should (not (python-info-continuation-line-p)))))
2532 (ert-deftest python-info-block-continuation-line-p-1 ()
2533 (python-tests-with-temp-buffer
2535 if width == 0 and height == 0 and \\\\
2536 color == 'red' and emphasis == 'strong' or \\\\
2537 highlight > 100:
2538 raise ValueError(
2539 'sorry, you lose'
2543 (python-tests-look-at "if width == 0 and")
2544 (should (not (python-info-block-continuation-line-p)))
2545 (python-tests-look-at "color == 'red' and emphasis == 'strong' or")
2546 (should (= (python-info-block-continuation-line-p)
2547 (python-tests-look-at "if width == 0 and" -1 t)))
2548 (python-tests-look-at "highlight > 100:")
2549 (should (not (python-info-block-continuation-line-p)))))
2551 (ert-deftest python-info-block-continuation-line-p-2 ()
2552 (python-tests-with-temp-buffer
2554 def foo(a,
2557 pass
2559 (python-tests-look-at "def foo(a,")
2560 (should (not (python-info-block-continuation-line-p)))
2561 (python-tests-look-at "b,")
2562 (should (= (python-info-block-continuation-line-p)
2563 (python-tests-look-at "def foo(a," -1 t)))
2564 (python-tests-look-at "c):")
2565 (should (not (python-info-block-continuation-line-p)))))
2567 (ert-deftest python-info-assignment-continuation-line-p-1 ()
2568 (python-tests-with-temp-buffer
2570 data = foo(), bar() \\\\
2571 baz(), 4 \\\\
2572 5, 6
2574 (python-tests-look-at "data = foo(), bar()")
2575 (should (not (python-info-assignment-continuation-line-p)))
2576 (python-tests-look-at "baz(), 4")
2577 (should (= (python-info-assignment-continuation-line-p)
2578 (python-tests-look-at "foo()," -1 t)))
2579 (python-tests-look-at "5, 6")
2580 (should (not (python-info-assignment-continuation-line-p)))))
2582 (ert-deftest python-info-assignment-continuation-line-p-2 ()
2583 (python-tests-with-temp-buffer
2585 data = (foo(), bar()
2586 baz(), 4
2587 5, 6)
2589 (python-tests-look-at "data = (foo(), bar()")
2590 (should (not (python-info-assignment-continuation-line-p)))
2591 (python-tests-look-at "baz(), 4")
2592 (should (= (python-info-assignment-continuation-line-p)
2593 (python-tests-look-at "(foo()," -1 t)))
2594 (python-tests-look-at "5, 6)")
2595 (should (not (python-info-assignment-continuation-line-p)))))
2597 (ert-deftest python-info-looking-at-beginning-of-defun-1 ()
2598 (python-tests-with-temp-buffer
2600 def decorat0r(deff):
2601 '''decorates stuff.
2603 @decorat0r
2604 def foo(arg):
2607 def wrap():
2608 deff()
2609 return wwrap
2611 (python-tests-look-at "def decorat0r(deff):")
2612 (should (python-info-looking-at-beginning-of-defun))
2613 (python-tests-look-at "def foo(arg):")
2614 (should (not (python-info-looking-at-beginning-of-defun)))
2615 (python-tests-look-at "def wrap():")
2616 (should (python-info-looking-at-beginning-of-defun))
2617 (python-tests-look-at "deff()")
2618 (should (not (python-info-looking-at-beginning-of-defun)))))
2620 (ert-deftest python-info-current-line-comment-p-1 ()
2621 (python-tests-with-temp-buffer
2623 # this is a comment
2624 foo = True # another comment
2625 '#this is a string'
2626 if foo:
2627 # more comments
2628 print ('bar') # print bar
2630 (python-tests-look-at "# this is a comment")
2631 (should (python-info-current-line-comment-p))
2632 (python-tests-look-at "foo = True # another comment")
2633 (should (not (python-info-current-line-comment-p)))
2634 (python-tests-look-at "'#this is a string'")
2635 (should (not (python-info-current-line-comment-p)))
2636 (python-tests-look-at "# more comments")
2637 (should (python-info-current-line-comment-p))
2638 (python-tests-look-at "print ('bar') # print bar")
2639 (should (not (python-info-current-line-comment-p)))))
2641 (ert-deftest python-info-current-line-empty-p ()
2642 (python-tests-with-temp-buffer
2644 # this is a comment
2646 foo = True # another comment
2648 (should (python-info-current-line-empty-p))
2649 (python-tests-look-at "# this is a comment")
2650 (should (not (python-info-current-line-empty-p)))
2651 (forward-line 1)
2652 (should (python-info-current-line-empty-p))))
2655 ;;; Utility functions
2657 (ert-deftest python-util-goto-line-1 ()
2658 (python-tests-with-temp-buffer
2659 (concat
2660 "# a comment
2661 # another comment
2662 def foo(a, b, c):
2663 pass" (make-string 20 ?\n))
2664 (python-util-goto-line 10)
2665 (should (= (line-number-at-pos) 10))
2666 (python-util-goto-line 20)
2667 (should (= (line-number-at-pos) 20))))
2669 (ert-deftest python-util-clone-local-variables-1 ()
2670 (let ((buffer (generate-new-buffer
2671 "python-util-clone-local-variables-1"))
2672 (varcons
2673 '((python-fill-docstring-style . django)
2674 (python-shell-interpreter . "python")
2675 (python-shell-interpreter-args . "manage.py shell")
2676 (python-shell-prompt-regexp . "In \\[[0-9]+\\]: ")
2677 (python-shell-prompt-output-regexp . "Out\\[[0-9]+\\]: ")
2678 (python-shell-extra-pythonpaths "/home/user/pylib/")
2679 (python-shell-completion-setup-code
2680 . "from IPython.core.completerlib import module_completion")
2681 (python-shell-completion-module-string-code
2682 . "';'.join(module_completion('''%s'''))\n")
2683 (python-shell-completion-string-code
2684 . "';'.join(get_ipython().Completer.all_completions('''%s'''))\n")
2685 (python-shell-virtualenv-path
2686 . "/home/user/.virtualenvs/project"))))
2687 (with-current-buffer buffer
2688 (kill-all-local-variables)
2689 (dolist (ccons varcons)
2690 (set (make-local-variable (car ccons)) (cdr ccons))))
2691 (python-tests-with-temp-buffer
2693 (python-util-clone-local-variables buffer)
2694 (dolist (ccons varcons)
2695 (should
2696 (equal (symbol-value (car ccons)) (cdr ccons)))))
2697 (kill-buffer buffer)))
2699 (ert-deftest python-util-forward-comment-1 ()
2700 (python-tests-with-temp-buffer
2701 (concat
2702 "# a comment
2703 # another comment
2704 # bad indented comment
2705 # more comments" (make-string 9999 ?\n))
2706 (python-util-forward-comment 1)
2707 (should (= (point) (point-max)))
2708 (python-util-forward-comment -1)
2709 (should (= (point) (point-min)))))
2712 (provide 'python-tests)
2714 ;; Local Variables:
2715 ;; coding: utf-8
2716 ;; indent-tabs-mode: nil
2717 ;; End:
2719 ;;; python-tests.el ends here