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/>.
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
))
35 (goto-char (point-min))
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
)))
47 (with-current-buffer buffer
50 (goto-char (point-min))
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+))
71 (while (not (= num
0))
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
))
79 ;; `re-search-forward' leaves point at the end of the
80 ;; occurrence, move back so point is at the beginning
82 (forward-char (- (length (match-string-no-properties 0)))))
84 num
(funcall deinc-fn num
)
85 found-point
(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
98 (let ((expected (save-excursion
100 (re-search-forward "et" nil t
))
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"))
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
120 (re-search-forward "et" nil t
)
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
)))))
132 ;;; Python specialized rx
135 ;;; Font-lock and syntax
140 ;; See: http://www.python.org/dev/peps/pep-0008/#indentation
142 (ert-deftest python-indent-pep8-1
()
144 (python-tests-with-temp-buffer
145 "# Aligned with opening delimiter
146 foo = long_function_name(var_one, var_two,
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
()
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,
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
()
184 (python-tests-with-temp-buffer
185 "# Extra indentation is not necessary.
186 foo = long_function_name(
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
207 def _on_child_complete(self, child_future):
208 if self.in_terminal_state():
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
))
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
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
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.
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
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
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
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
422 except (IndexError, AttributeError):
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
468 if cond: print 'True'
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() \\\\
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))
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
564 (python-tests-look-at "3)")
566 (should (= (python-indent-calculate-indentation) 8))
567 (python-tests-look-at "pass")
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
576 '''raise lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
578 eiusmod tempor incididunt ut labore et dolore magna aliqua.
581 \"return (1, 2, 3).\"
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))))
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.
603 @decoratorFunctionWithArguments('arg1', 'arg2')
604 def func(a, b, c=True):
609 print 'Inside wwrap()'
610 def wrapped_f(*args):
611 print 'Inside wrapped_f()'
612 print 'Decorator arguments:', arg1, arg2, arg3
614 print 'After f(*args)'
618 (python-tests-look-at "return wrap")
619 (should (= (save-excursion
620 (python-nav-beginning-of-defun)
623 (python-tests-look-at "def wrapped_f(*args):" -
1)
626 (python-tests-look-at "def wrapped_f(*args):" -
1)
627 (should (= (save-excursion
628 (python-nav-beginning-of-defun)
631 (python-tests-look-at "def wwrap(f):" -
1)
634 (python-tests-look-at "def wwrap(f):" -
1)
635 (should (= (save-excursion
636 (python-nav-beginning-of-defun)
639 (python-tests-look-at "def decoratorFunctionWithArguments" -
1)
643 (ert-deftest python-nav-beginning-of-defun-2
()
644 (python-tests-with-temp-buffer
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)
666 (python-tests-look-at "def m(self):" -
1)
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)
675 (python-tests-look-at "def b():" -
1)
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)
684 (python-tests-look-at "def m(self):" -
1)
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)
693 (python-tests-look-at "class C(object):" -
1)
697 (ert-deftest python-nav-end-of-defun-1
()
698 (python-tests-with-temp-buffer
714 (should (= (save-excursion
715 (python-tests-look-at "class C(object):")
716 (python-nav-end-of-defun)
720 (should (= (save-excursion
721 (python-tests-look-at "def m(self):")
722 (python-nav-end-of-defun)
725 (python-tests-look-at "def c(self):")
728 (should (= (save-excursion
729 (python-tests-look-at "def b():")
730 (python-nav-end-of-defun)
733 (python-tests-look-at "def b():")
736 (should (= (save-excursion
737 (python-tests-look-at "def c(self):")
738 (python-nav-end-of-defun)
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.
751 @decoratorFunctionWithArguments('arg1', 'arg2')
752 def func(a, b, c=True):
757 print 'Inside wwrap()'
758 def wrapped_f(*args):
759 print 'Inside wrapped_f()'
760 print 'Decorator arguments:', arg1, arg2, arg3
762 print 'After f(*args)'
766 (should (= (save-excursion
767 (python-tests-look-at "def decoratorFunctionWithArguments")
768 (python-nav-end-of-defun)
772 (should (= (save-excursion
773 (python-tests-look-at "@decoratorFunctionWithArguments")
774 (python-nav-end-of-defun)
778 (should (= (save-excursion
779 (python-tests-look-at "def wwrap(f):")
780 (python-nav-end-of-defun)
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)
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)
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
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.
852 @decoratorFunctionWithArguments('arg1', 'arg2')
853 def func(a, b, c=True):
858 print 'Inside wwrap()'
859 def wrapped_f(*args):
860 print 'Inside wrapped_f()'
861 print 'Decorator arguments:', arg1, arg2, arg3
863 print 'After f(*args)'
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
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
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.
950 @decoratorFunctionWithArguments('arg1', 'arg2')
951 def func(a, b, c=True):
956 print 'Inside wwrap()'
957 def wrapped_f(*args):
958 print 'Inside wrapped_f()'
959 print 'Decorator arguments:', arg1, arg2, arg3
961 print 'After f(*args)'
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
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
1007 v3 = ('this is a string'
1012 # this is a comment, yo
1013 'continue previous line')
1019 (python-tests-look-at "v2 =")
1020 (python-util-forward-comment -
1)
1021 (should (= (save-excursion
1022 (python-nav-beginning-of-statement)
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)
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)
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)
1042 (python-tests-look-at "v4 =" -
1 t
)))))
1044 (ert-deftest python-nav-end-of-statement-1
()
1045 (python-tests-with-temp-buffer
1055 v3 = ('this is a string'
1060 # this is a comment, yo
1061 'continue previous line')
1067 (python-tests-look-at "v1 =")
1068 (should (= (save-excursion
1069 (python-nav-end-of-statement)
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)
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)
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)
1094 (goto-char (point-max))
1095 (python-util-forward-comment -
1)
1098 (ert-deftest python-nav-forward-statement-1
()
1099 (python-tests-with-temp-buffer
1109 v3 = ('this is a string'
1114 # this is a comment, yo
1115 'continue previous line')
1121 (python-tests-look-at "v1 =")
1122 (should (= (save-excursion
1123 (python-nav-forward-statement)
1125 (python-tests-look-at "v2 =")))
1126 (should (= (save-excursion
1127 (python-nav-forward-statement)
1129 (python-tests-look-at "v3 =")))
1130 (should (= (save-excursion
1131 (python-nav-forward-statement)
1133 (python-tests-look-at "v4 =")))
1134 (should (= (save-excursion
1135 (python-nav-forward-statement)
1139 (ert-deftest python-nav-backward-statement-1
()
1140 (python-tests-with-temp-buffer
1150 v3 = ('this is a string'
1155 # this is a comment, yo
1156 'continue previous line')
1162 (goto-char (point-max))
1163 (should (= (save-excursion
1164 (python-nav-backward-statement)
1166 (python-tests-look-at "v4 =" -
1)))
1167 (should (= (save-excursion
1168 (python-nav-backward-statement)
1170 (python-tests-look-at "v3 =" -
1)))
1171 (should (= (save-excursion
1172 (python-nav-backward-statement)
1174 (python-tests-look-at "v2 =" -
1)))
1175 (should (= (save-excursion
1176 (python-nav-backward-statement)
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
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)
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.
1210 @decoratorFunctionWithArguments('arg1', 'arg2')
1211 def func(a, b, c=True):
1216 print 'Inside wwrap()'
1217 def wrapped_f(*args):
1218 print 'Inside wrapped_f()'
1219 print 'Decorator arguments:', arg1, arg2, arg3
1221 print 'After f(*args)'
1225 (python-tests-look-at "return wwrap")
1226 (should (= (save-excursion
1227 (python-nav-beginning-of-block)
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)
1234 (python-tests-look-at "def wwrap(f):" -
1)))
1235 (python-tests-look-at "print 'After f(*args)'")
1237 (should (= (save-excursion
1238 (python-nav-beginning-of-block)
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)
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.
1255 @decoratorFunctionWithArguments('arg1', 'arg2')
1256 def func(a, b, c=True):
1261 print 'Inside wwrap()'
1262 def wrapped_f(*args):
1263 print 'Inside wrapped_f()'
1264 print 'Decorator arguments:', arg1, arg2, arg3
1266 print 'After f(*args)'
1270 (python-tests-look-at "def decoratorFunctionWithArguments")
1271 (should (= (save-excursion
1272 (python-nav-end-of-block)
1275 (goto-char (point-max))
1276 (python-util-forward-comment -
1)
1278 (python-tests-look-at "def wwrap(f):")
1279 (should (= (save-excursion
1280 (python-nav-end-of-block)
1283 (python-tests-look-at "return wrapped_f")
1284 (line-end-position))))
1286 (should (= (save-excursion
1287 (python-nav-end-of-block)
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)
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():
1308 profile = request.user.get_profile()
1309 except Profile.DoesNotExist:
1310 profile = Profile.objects.create(user=request.user)
1313 profile.recalculate_stats()
1315 profile.clear_stats()
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
1354 (looking-at "a()")))
1355 (python-nav-forward-sexp)
1356 (should (looking-at "$"))
1357 (should (save-excursion
1359 (looking-at "b()")))
1360 (python-nav-forward-sexp)
1361 (should (looking-at "$"))
1362 (should (save-excursion
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
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
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():
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 "$"))
1434 (back-to-indentation)
1436 "from some_module import some_sub_module")))
1437 (python-nav-forward-sexp)
1438 (should (looking-at "$"))
1441 (back-to-indentation)
1443 "from another_module import another_sub_module")))
1444 (python-nav-forward-sexp)
1445 (should (looking-at "$"))
1448 (back-to-indentation)
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)
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
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
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)
1507 "\\[i for i in range(3)\\]"))
1508 ;; FIXME: Need to move to beginning-of-statement.
1509 (python-nav-backward-up-list)
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
))
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.
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.
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"))
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")
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)))
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)))
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
))
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
)))
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))
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
)))
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
)))
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
1730 (let* ((python-shell-interpreter
1731 (executable-find python-tests-shell-interpreter
))
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
)))
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
)))
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
1797 (ert-deftest python-imenu-create-index-1
()
1798 (python-tests-with-temp-buffer
1800 class Foo(models.Model):
1804 class Bar(models.Model):
1808 def decorator(arg1, arg2, arg3):
1809 '''print decorated function call data to stdout.
1813 @decorator('arg1', 'arg2')
1814 def func(a, b, c=True):
1820 def wrapped_f(*args):
1822 print ('Decorator arguments:', arg1, arg2, arg3)
1824 print ('called f(*args)')
1842 (goto-char (point-max))
1845 (cons "Foo (class)" (copy-marker 2))
1846 (cons "Bar (class)" (copy-marker 38))
1849 (cons "*function definition*" (copy-marker 74))
1852 (cons "*function definition*" (copy-marker 254))
1853 (cons "wrapped_f (def)" (copy-marker 294))))
1856 (cons "*class definition*" (copy-marker 519))
1857 (cons "a (def)" (copy-marker 539))
1858 (cons "b (def)" (copy-marker 570))
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
1876 (goto-char (point-max))
1881 (cons "*class definition*" (copy-marker 2))
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
1899 (goto-char (point-max))
1904 (cons "*class definition*" (copy-marker 2))
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
1926 (goto-char (point-max))
1931 (cons "*class definition*" (copy-marker 2))
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):
1947 class Bar(models.Model):
1951 def decorator(arg1, arg2, arg3):
1952 '''print decorated function call data to stdout.
1956 @decorator('arg1', 'arg2')
1957 def func(a, b, c=True):
1963 def wrapped_f(*args):
1965 print ('Decorator arguments:', arg1, arg2, arg3)
1967 print ('called f(*args)')
1985 (goto-char (point-max))
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
2013 (goto-char (point-max))
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)))))
2026 (ert-deftest python-info-current-defun-1
()
2027 (python-tests-with-temp-buffer
2032 (should (string= "foo" (python-info-current-defun)))
2033 (should (string= "def foo" (python-info-current-defun t)))
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
2047 return [i for i in range(3)]
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)))
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):")
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.
2100 @decoratorFunctionWithArguments('arg1', 'arg2')
2101 def func(a, b, c=True):
2106 print 'Inside wwrap()'
2107 def wrapped_f(*args):
2108 print 'Inside wrapped_f()'
2109 print 'Decorator arguments:', arg1, arg2, arg3
2111 print 'After f(*args)'
2115 (python-tests-look-at "def wwrap(f):")
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"))
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
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
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
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,
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 \\\\
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,
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 \\\\
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,
2249 (python-tests-look-at "def long_function_name")
2250 (should (python-info-beginning-of-statement-p))
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 \\\\
2269 (python-tests-look-at "if width == 0 and")
2270 (should (python-info-beginning-of-statement-p))
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,
2286 (python-tests-look-at "def long_function_name")
2287 (should (not (python-info-end-of-statement-p)))
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)))
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 \\\\
2309 (python-tests-look-at "if width == 0 and")
2310 (should (not (python-info-end-of-statement-p)))
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)))
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,
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 \\\\
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,
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):")
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)))
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 \\\\
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:")
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)))
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():
2408 profile = request.user.get_profile()
2409 except Profile.DoesNotExist:
2410 profile = Profile.objects.create(user=request.user)
2413 profile.recalculate_stats()
2415 profile.clear_stats()
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)
2443 profile.recalculate_stats()
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() \\\\
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() \\\\
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 \\\\
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))
2526 (should (python-info-continuation-line-p))
2527 (python-tests-look-at ")")
2528 (should (python-info-continuation-line-p))
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 \\\\
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
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() \\\\
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()
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):
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
2624 foo = True # another comment
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
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)))
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
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"))
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
)
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
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
)
2716 ;; indent-tabs-mode: nil
2719 ;;; python-tests.el ends here