Auto-commit of generated files.
[emacs.git] / test / automated / python-tests.el
blob40505cc79536d160aa35ef5affe7e7fe567a4597
1 ;;; python-tests.el --- Test suite for python.el
3 ;; Copyright (C) 2013 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 'python)
26 (defmacro python-tests-with-temp-buffer (contents &rest body)
27 "Create a `python-mode' enabled temp buffer with CONTENTS.
28 BODY is code to be executed within the temp buffer. Point is
29 always located at the beginning of buffer."
30 (declare (indent 1) (debug t))
31 `(with-temp-buffer
32 (python-mode)
33 (insert ,contents)
34 (goto-char (point-min))
35 ,@body))
37 (defmacro python-tests-with-temp-file (contents &rest body)
38 "Create a `python-mode' enabled file with CONTENTS.
39 BODY is code to be executed within the temp buffer. Point is
40 always located at the beginning of buffer."
41 (declare (indent 1) (debug t))
42 ;; temp-file never actually used for anything?
43 `(let* ((temp-file (make-temp-file "python-tests" nil ".py"))
44 (buffer (find-file-noselect temp-file)))
45 (unwind-protect
46 (with-current-buffer buffer
47 (python-mode)
48 (insert ,contents)
49 (goto-char (point-min))
50 ,@body)
51 (and buffer (kill-buffer buffer))
52 (delete-file temp-file))))
54 (defun python-tests-look-at (string &optional num restore-point)
55 "Move point at beginning of STRING in the current buffer.
56 Optional argument NUM defaults to 1 and is an integer indicating
57 how many occurrences must be found, when positive the search is
58 done forwards, otherwise backwards. When RESTORE-POINT is
59 non-nil the point is not moved but the position found is still
60 returned. When searching forward and point is already looking at
61 STRING, it is skipped so the next STRING occurrence is selected."
62 (let* ((num (or num 1))
63 (starting-point (point))
64 (string (regexp-quote string))
65 (search-fn (if (> num 0) #'re-search-forward #'re-search-backward))
66 (deinc-fn (if (> num 0) #'1- #'1+))
67 (found-point))
68 (prog2
69 (catch 'exit
70 (while (not (= num 0))
71 (when (and (> num 0)
72 (looking-at string))
73 ;; Moving forward and already looking at STRING, skip it.
74 (forward-char (length (match-string-no-properties 0))))
75 (and (not (funcall search-fn string nil t))
76 (throw 'exit t))
77 (when (> num 0)
78 ;; `re-search-forward' leaves point at the end of the
79 ;; occurrence, move back so point is at the beginning
80 ;; instead.
81 (forward-char (- (length (match-string-no-properties 0)))))
82 (setq
83 num (funcall deinc-fn num)
84 found-point (point))))
85 found-point
86 (and restore-point (goto-char starting-point)))))
89 ;;; Tests for your tests, so you can test while you test.
91 (ert-deftest python-tests-look-at-1 ()
92 "Test forward movement."
93 (python-tests-with-temp-buffer
94 "Lorem ipsum dolor sit amet, consectetur adipisicing elit,
95 sed do eiusmod tempor incididunt ut labore et dolore magna
96 aliqua."
97 (let ((expected (save-excursion
98 (dotimes (i 3)
99 (re-search-forward "et" nil t))
100 (forward-char -2)
101 (point))))
102 (should (= (python-tests-look-at "et" 3 t) expected))
103 ;; Even if NUM is bigger than found occurrences the point of last
104 ;; one should be returned.
105 (should (= (python-tests-look-at "et" 6 t) expected))
106 ;; If already looking at STRING, it should skip it.
107 (dotimes (i 2) (re-search-forward "et"))
108 (forward-char -2)
109 (should (= (python-tests-look-at "et") expected)))))
111 (ert-deftest python-tests-look-at-2 ()
112 "Test backward movement."
113 (python-tests-with-temp-buffer
114 "Lorem ipsum dolor sit amet, consectetur adipisicing elit,
115 sed do eiusmod tempor incididunt ut labore et dolore magna
116 aliqua."
117 (let ((expected
118 (save-excursion
119 (re-search-forward "et" nil t)
120 (forward-char -2)
121 (point))))
122 (dotimes (i 3)
123 (re-search-forward "et" nil t))
124 (should (= (python-tests-look-at "et" -3 t) expected))
125 (should (= (python-tests-look-at "et" -6 t) expected)))))
128 ;;; Bindings
131 ;;; Python specialized rx
134 ;;; Font-lock and syntax
137 ;;; Indentation
139 ;; See: http://www.python.org/dev/peps/pep-0008/#indentation
141 (ert-deftest python-indent-pep8-1 ()
142 "First pep8 case."
143 (python-tests-with-temp-buffer
144 "# Aligned with opening delimiter
145 foo = long_function_name(var_one, var_two,
146 var_three, var_four)
148 (should (eq (car (python-indent-context)) 'no-indent))
149 (should (= (python-indent-calculate-indentation) 0))
150 (python-tests-look-at "foo = long_function_name(var_one, var_two,")
151 (should (eq (car (python-indent-context)) 'after-line))
152 (should (= (python-indent-calculate-indentation) 0))
153 (python-tests-look-at "var_three, var_four)")
154 (should (eq (car (python-indent-context)) 'inside-paren))
155 (should (= (python-indent-calculate-indentation) 25))))
157 (ert-deftest python-indent-pep8-2 ()
158 "Second pep8 case."
159 (python-tests-with-temp-buffer
160 "# More indentation included to distinguish this from the rest.
161 def long_function_name(
162 var_one, var_two, var_three,
163 var_four):
164 print (var_one)
166 (should (eq (car (python-indent-context)) 'no-indent))
167 (should (= (python-indent-calculate-indentation) 0))
168 (python-tests-look-at "def long_function_name(")
169 (should (eq (car (python-indent-context)) 'after-line))
170 (should (= (python-indent-calculate-indentation) 0))
171 (python-tests-look-at "var_one, var_two, var_three,")
172 (should (eq (car (python-indent-context)) 'inside-paren))
173 (should (= (python-indent-calculate-indentation) 8))
174 (python-tests-look-at "var_four):")
175 (should (eq (car (python-indent-context)) 'inside-paren))
176 (should (= (python-indent-calculate-indentation) 8))
177 (python-tests-look-at "print (var_one)")
178 (should (eq (car (python-indent-context)) 'after-beginning-of-block))
179 (should (= (python-indent-calculate-indentation) 4))))
181 (ert-deftest python-indent-pep8-3 ()
182 "Third pep8 case."
183 (python-tests-with-temp-buffer
184 "# Extra indentation is not necessary.
185 foo = long_function_name(
186 var_one, var_two,
187 var_three, var_four)
189 (should (eq (car (python-indent-context)) 'no-indent))
190 (should (= (python-indent-calculate-indentation) 0))
191 (python-tests-look-at "foo = long_function_name(")
192 (should (eq (car (python-indent-context)) 'after-line))
193 (should (= (python-indent-calculate-indentation) 0))
194 (python-tests-look-at "var_one, var_two,")
195 (should (eq (car (python-indent-context)) 'inside-paren))
196 (should (= (python-indent-calculate-indentation) 4))
197 (python-tests-look-at "var_three, var_four)")
198 (should (eq (car (python-indent-context)) 'inside-paren))
199 (should (= (python-indent-calculate-indentation) 4))))
201 (ert-deftest python-indent-inside-paren-1 ()
202 "The most simple inside-paren case that shouldn't fail."
203 (python-tests-with-temp-buffer
205 data = {
206 'key':
208 'objlist': [
210 'pk': 1,
211 'name': 'first',
214 'pk': 2,
215 'name': 'second',
221 (python-tests-look-at "data = {")
222 (should (eq (car (python-indent-context)) 'after-line))
223 (should (= (python-indent-calculate-indentation) 0))
224 (python-tests-look-at "'key':")
225 (should (eq (car (python-indent-context)) 'inside-paren))
226 (should (= (python-indent-calculate-indentation) 4))
227 (python-tests-look-at "{")
228 (should (eq (car (python-indent-context)) 'inside-paren))
229 (should (= (python-indent-calculate-indentation) 4))
230 (python-tests-look-at "'objlist': [")
231 (should (eq (car (python-indent-context)) 'inside-paren))
232 (should (= (python-indent-calculate-indentation) 8))
233 (python-tests-look-at "{")
234 (should (eq (car (python-indent-context)) 'inside-paren))
235 (should (= (python-indent-calculate-indentation) 12))
236 (python-tests-look-at "'pk': 1,")
237 (should (eq (car (python-indent-context)) 'inside-paren))
238 (should (= (python-indent-calculate-indentation) 16))
239 (python-tests-look-at "'name': 'first',")
240 (should (eq (car (python-indent-context)) 'inside-paren))
241 (should (= (python-indent-calculate-indentation) 16))
242 (python-tests-look-at "},")
243 (should (eq (car (python-indent-context)) 'inside-paren))
244 (should (= (python-indent-calculate-indentation) 12))
245 (python-tests-look-at "{")
246 (should (eq (car (python-indent-context)) 'inside-paren))
247 (should (= (python-indent-calculate-indentation) 12))
248 (python-tests-look-at "'pk': 2,")
249 (should (eq (car (python-indent-context)) 'inside-paren))
250 (should (= (python-indent-calculate-indentation) 16))
251 (python-tests-look-at "'name': 'second',")
252 (should (eq (car (python-indent-context)) 'inside-paren))
253 (should (= (python-indent-calculate-indentation) 16))
254 (python-tests-look-at "}")
255 (should (eq (car (python-indent-context)) 'inside-paren))
256 (should (= (python-indent-calculate-indentation) 12))
257 (python-tests-look-at "]")
258 (should (eq (car (python-indent-context)) 'inside-paren))
259 (should (= (python-indent-calculate-indentation) 8))
260 (python-tests-look-at "}")
261 (should (eq (car (python-indent-context)) 'inside-paren))
262 (should (= (python-indent-calculate-indentation) 4))
263 (python-tests-look-at "}")
264 (should (eq (car (python-indent-context)) 'inside-paren))
265 (should (= (python-indent-calculate-indentation) 0))))
267 (ert-deftest python-indent-inside-paren-2 ()
268 "Another more compact paren group style."
269 (python-tests-with-temp-buffer
271 data = {'key': {
272 'objlist': [
273 {'pk': 1,
274 'name': 'first'},
275 {'pk': 2,
276 'name': 'second'}
280 (python-tests-look-at "data = {")
281 (should (eq (car (python-indent-context)) 'after-line))
282 (should (= (python-indent-calculate-indentation) 0))
283 (python-tests-look-at "'objlist': [")
284 (should (eq (car (python-indent-context)) 'inside-paren))
285 (should (= (python-indent-calculate-indentation) 4))
286 (python-tests-look-at "{'pk': 1,")
287 (should (eq (car (python-indent-context)) 'inside-paren))
288 (should (= (python-indent-calculate-indentation) 8))
289 (python-tests-look-at "'name': 'first'},")
290 (should (eq (car (python-indent-context)) 'inside-paren))
291 (should (= (python-indent-calculate-indentation) 9))
292 (python-tests-look-at "{'pk': 2,")
293 (should (eq (car (python-indent-context)) 'inside-paren))
294 (should (= (python-indent-calculate-indentation) 8))
295 (python-tests-look-at "'name': 'second'}")
296 (should (eq (car (python-indent-context)) 'inside-paren))
297 (should (= (python-indent-calculate-indentation) 9))
298 (python-tests-look-at "]")
299 (should (eq (car (python-indent-context)) 'inside-paren))
300 (should (= (python-indent-calculate-indentation) 4))
301 (python-tests-look-at "}}")
302 (should (eq (car (python-indent-context)) 'inside-paren))
303 (should (= (python-indent-calculate-indentation) 0))
304 (python-tests-look-at "}")
305 (should (eq (car (python-indent-context)) 'inside-paren))
306 (should (= (python-indent-calculate-indentation) 0))))
308 (ert-deftest python-indent-after-block-1 ()
309 "The most simple after-block case that shouldn't fail."
310 (python-tests-with-temp-buffer
312 def foo(a, b, c=True):
314 (should (eq (car (python-indent-context)) 'no-indent))
315 (should (= (python-indent-calculate-indentation) 0))
316 (goto-char (point-max))
317 (should (eq (car (python-indent-context)) 'after-beginning-of-block))
318 (should (= (python-indent-calculate-indentation) 4))))
320 (ert-deftest python-indent-after-block-2 ()
321 "A weird (malformed) multiline block statement."
322 (python-tests-with-temp-buffer
324 def foo(a, b, c={
325 'a':
328 (goto-char (point-max))
329 (should (eq (car (python-indent-context)) 'after-beginning-of-block))
330 (should (= (python-indent-calculate-indentation) 4))))
332 (ert-deftest python-indent-dedenters-1 ()
333 "Check all dedenters."
334 (python-tests-with-temp-buffer
336 def foo(a, b, c):
337 if a:
338 print (a)
339 elif b:
340 print (b)
341 else:
342 try:
343 print (c.pop())
344 except (IndexError, AttributeError):
345 print (c)
346 finally:
347 print ('nor a, nor b are true')
349 (python-tests-look-at "if a:")
350 (should (eq (car (python-indent-context)) 'after-beginning-of-block))
351 (should (= (python-indent-calculate-indentation) 4))
352 (python-tests-look-at "print (a)")
353 (should (eq (car (python-indent-context)) 'after-beginning-of-block))
354 (should (= (python-indent-calculate-indentation) 8))
355 (python-tests-look-at "elif b:")
356 (should (eq (car (python-indent-context)) 'after-line))
357 (should (= (python-indent-calculate-indentation) 4))
358 (python-tests-look-at "print (b)")
359 (should (eq (car (python-indent-context)) 'after-beginning-of-block))
360 (should (= (python-indent-calculate-indentation) 8))
361 (python-tests-look-at "else:")
362 (should (eq (car (python-indent-context)) 'after-line))
363 (should (= (python-indent-calculate-indentation) 4))
364 (python-tests-look-at "try:")
365 (should (eq (car (python-indent-context)) 'after-beginning-of-block))
366 (should (= (python-indent-calculate-indentation) 8))
367 (python-tests-look-at "print (c.pop())")
368 (should (eq (car (python-indent-context)) 'after-beginning-of-block))
369 (should (= (python-indent-calculate-indentation) 12))
370 (python-tests-look-at "except (IndexError, AttributeError):")
371 (should (eq (car (python-indent-context)) 'after-line))
372 (should (= (python-indent-calculate-indentation) 8))
373 (python-tests-look-at "print (c)")
374 (should (eq (car (python-indent-context)) 'after-beginning-of-block))
375 (should (= (python-indent-calculate-indentation) 12))
376 (python-tests-look-at "finally:")
377 (should (eq (car (python-indent-context)) 'after-line))
378 (should (= (python-indent-calculate-indentation) 8))
379 (python-tests-look-at "print ('nor a, nor b are true')")
380 (should (eq (car (python-indent-context)) 'after-beginning-of-block))
381 (should (= (python-indent-calculate-indentation) 12))))
383 (ert-deftest python-indent-after-backslash-1 ()
384 "The most common case."
385 (python-tests-with-temp-buffer
387 from foo.bar.baz import something, something_1 \\\\
388 something_2 something_3, \\\\
389 something_4, something_5
391 (python-tests-look-at "from foo.bar.baz import something, something_1")
392 (should (eq (car (python-indent-context)) 'after-line))
393 (should (= (python-indent-calculate-indentation) 0))
394 (python-tests-look-at "something_2 something_3,")
395 (should (eq (car (python-indent-context)) 'after-backslash))
396 (should (= (python-indent-calculate-indentation) 4))
397 (python-tests-look-at "something_4, something_5")
398 (should (eq (car (python-indent-context)) 'after-backslash))
399 (should (= (python-indent-calculate-indentation) 4))
400 (goto-char (point-max))
401 (should (eq (car (python-indent-context)) 'after-line))
402 (should (= (python-indent-calculate-indentation) 0))))
404 (ert-deftest python-indent-after-backslash-2 ()
405 "A pretty extreme complicated case."
406 (python-tests-with-temp-buffer
408 objects = Thing.objects.all() \\\\
409 .filter(
410 type='toy',
411 status='bought'
412 ) \\\\
413 .aggregate(
414 Sum('amount')
415 ) \\\\
416 .values_list()
418 (python-tests-look-at "objects = Thing.objects.all()")
419 (should (eq (car (python-indent-context)) 'after-line))
420 (should (= (python-indent-calculate-indentation) 0))
421 (python-tests-look-at ".filter(")
422 (should (eq (car (python-indent-context)) 'after-backslash))
423 (should (= (python-indent-calculate-indentation) 23))
424 (python-tests-look-at "type='toy',")
425 (should (eq (car (python-indent-context)) 'inside-paren))
426 (should (= (python-indent-calculate-indentation) 27))
427 (python-tests-look-at "status='bought'")
428 (should (eq (car (python-indent-context)) 'inside-paren))
429 (should (= (python-indent-calculate-indentation) 27))
430 (python-tests-look-at ") \\\\")
431 (should (eq (car (python-indent-context)) 'inside-paren))
432 (should (= (python-indent-calculate-indentation) 23))
433 (python-tests-look-at ".aggregate(")
434 (should (eq (car (python-indent-context)) 'after-backslash))
435 (should (= (python-indent-calculate-indentation) 23))
436 (python-tests-look-at "Sum('amount')")
437 (should (eq (car (python-indent-context)) 'inside-paren))
438 (should (= (python-indent-calculate-indentation) 27))
439 (python-tests-look-at ") \\\\")
440 (should (eq (car (python-indent-context)) 'inside-paren))
441 (should (= (python-indent-calculate-indentation) 23))
442 (python-tests-look-at ".values_list()")
443 (should (eq (car (python-indent-context)) 'after-backslash))
444 (should (= (python-indent-calculate-indentation) 23))
445 (forward-line 1)
446 (should (eq (car (python-indent-context)) 'after-line))
447 (should (= (python-indent-calculate-indentation) 0))))
449 (ert-deftest python-indent-block-enders ()
450 "Test `python-indent-block-enders' value honoring."
451 (python-tests-with-temp-buffer
453 Class foo(object):
455 def bar(self):
456 if self.baz:
457 return (1,
461 else:
462 pass
464 (python-tests-look-at "3)")
465 (forward-line 1)
466 (= (python-indent-calculate-indentation) 12)
467 (python-tests-look-at "pass")
468 (forward-line 1)
469 (= (python-indent-calculate-indentation) 8)))
472 ;;; Navigation
474 (ert-deftest python-nav-beginning-of-defun-1 ()
475 (python-tests-with-temp-buffer
477 def decoratorFunctionWithArguments(arg1, arg2, arg3):
478 '''print decorated function call data to stdout.
480 Usage:
482 @decoratorFunctionWithArguments('arg1', 'arg2')
483 def func(a, b, c=True):
484 pass
487 def wwrap(f):
488 print 'Inside wwrap()'
489 def wrapped_f(*args):
490 print 'Inside wrapped_f()'
491 print 'Decorator arguments:', arg1, arg2, arg3
492 f(*args)
493 print 'After f(*args)'
494 return wrapped_f
495 return wwrap
497 (python-tests-look-at "return wrap")
498 (should (= (save-excursion
499 (python-nav-beginning-of-defun)
500 (point))
501 (save-excursion
502 (python-tests-look-at "def wrapped_f(*args):" -1)
503 (beginning-of-line)
504 (point))))
505 (python-tests-look-at "def wrapped_f(*args):" -1)
506 (should (= (save-excursion
507 (python-nav-beginning-of-defun)
508 (point))
509 (save-excursion
510 (python-tests-look-at "def wwrap(f):" -1)
511 (beginning-of-line)
512 (point))))
513 (python-tests-look-at "def wwrap(f):" -1)
514 (should (= (save-excursion
515 (python-nav-beginning-of-defun)
516 (point))
517 (save-excursion
518 (python-tests-look-at "def decoratorFunctionWithArguments" -1)
519 (beginning-of-line)
520 (point))))))
522 (ert-deftest python-nav-beginning-of-defun-2 ()
523 (python-tests-with-temp-buffer
525 class C(object):
527 def m(self):
528 self.c()
530 def b():
531 pass
533 def a():
534 pass
536 def c(self):
537 pass
539 ;; Nested defuns, are handled with care.
540 (python-tests-look-at "def c(self):")
541 (should (= (save-excursion
542 (python-nav-beginning-of-defun)
543 (point))
544 (save-excursion
545 (python-tests-look-at "def m(self):" -1)
546 (beginning-of-line)
547 (point))))
548 ;; Defuns on same levels should be respected.
549 (python-tests-look-at "def a():" -1)
550 (should (= (save-excursion
551 (python-nav-beginning-of-defun)
552 (point))
553 (save-excursion
554 (python-tests-look-at "def b():" -1)
555 (beginning-of-line)
556 (point))))
557 ;; Jump to a top level defun.
558 (python-tests-look-at "def b():" -1)
559 (should (= (save-excursion
560 (python-nav-beginning-of-defun)
561 (point))
562 (save-excursion
563 (python-tests-look-at "def m(self):" -1)
564 (beginning-of-line)
565 (point))))
566 ;; Jump to a top level defun again.
567 (python-tests-look-at "def m(self):" -1)
568 (should (= (save-excursion
569 (python-nav-beginning-of-defun)
570 (point))
571 (save-excursion
572 (python-tests-look-at "class C(object):" -1)
573 (beginning-of-line)
574 (point))))))
576 (ert-deftest python-nav-end-of-defun-1 ()
577 (python-tests-with-temp-buffer
579 class C(object):
581 def m(self):
582 self.c()
584 def b():
585 pass
587 def a():
588 pass
590 def c(self):
591 pass
593 (should (= (save-excursion
594 (python-tests-look-at "class C(object):")
595 (python-nav-end-of-defun)
596 (point))
597 (save-excursion
598 (point-max))))
599 (should (= (save-excursion
600 (python-tests-look-at "def m(self):")
601 (python-nav-end-of-defun)
602 (point))
603 (save-excursion
604 (python-tests-look-at "def c(self):")
605 (forward-line -1)
606 (point))))
607 (should (= (save-excursion
608 (python-tests-look-at "def b():")
609 (python-nav-end-of-defun)
610 (point))
611 (save-excursion
612 (python-tests-look-at "def b():")
613 (forward-line 2)
614 (point))))
615 (should (= (save-excursion
616 (python-tests-look-at "def c(self):")
617 (python-nav-end-of-defun)
618 (point))
619 (save-excursion
620 (point-max))))))
622 (ert-deftest python-nav-end-of-defun-2 ()
623 (python-tests-with-temp-buffer
625 def decoratorFunctionWithArguments(arg1, arg2, arg3):
626 '''print decorated function call data to stdout.
628 Usage:
630 @decoratorFunctionWithArguments('arg1', 'arg2')
631 def func(a, b, c=True):
632 pass
635 def wwrap(f):
636 print 'Inside wwrap()'
637 def wrapped_f(*args):
638 print 'Inside wrapped_f()'
639 print 'Decorator arguments:', arg1, arg2, arg3
640 f(*args)
641 print 'After f(*args)'
642 return wrapped_f
643 return wwrap
645 (should (= (save-excursion
646 (python-tests-look-at "def decoratorFunctionWithArguments")
647 (python-nav-end-of-defun)
648 (point))
649 (save-excursion
650 (point-max))))
651 (should (= (save-excursion
652 (python-tests-look-at "@decoratorFunctionWithArguments")
653 (python-nav-end-of-defun)
654 (point))
655 (save-excursion
656 (point-max))))
657 (should (= (save-excursion
658 (python-tests-look-at "def wwrap(f):")
659 (python-nav-end-of-defun)
660 (point))
661 (save-excursion
662 (python-tests-look-at "return wwrap")
663 (line-beginning-position))))
664 (should (= (save-excursion
665 (python-tests-look-at "def wrapped_f(*args):")
666 (python-nav-end-of-defun)
667 (point))
668 (save-excursion
669 (python-tests-look-at "return wrapped_f")
670 (line-beginning-position))))
671 (should (= (save-excursion
672 (python-tests-look-at "f(*args)")
673 (python-nav-end-of-defun)
674 (point))
675 (save-excursion
676 (python-tests-look-at "return wrapped_f")
677 (line-beginning-position))))))
679 (ert-deftest python-nav-backward-defun-1 ()
680 (python-tests-with-temp-buffer
682 class A(object): # A
684 def a(self): # a
685 pass
687 def b(self): # b
688 pass
690 class B(object): # B
692 class C(object): # C
694 def d(self): # d
695 pass
697 # def e(self): # e
698 # pass
700 def c(self): # c
701 pass
703 # def d(self): # d
704 # pass
706 (goto-char (point-max))
707 (should (= (save-excursion (python-nav-backward-defun))
708 (python-tests-look-at " def c(self): # c" -1)))
709 (should (= (save-excursion (python-nav-backward-defun))
710 (python-tests-look-at " def d(self): # d" -1)))
711 (should (= (save-excursion (python-nav-backward-defun))
712 (python-tests-look-at " class C(object): # C" -1)))
713 (should (= (save-excursion (python-nav-backward-defun))
714 (python-tests-look-at " class B(object): # B" -1)))
715 (should (= (save-excursion (python-nav-backward-defun))
716 (python-tests-look-at " def b(self): # b" -1)))
717 (should (= (save-excursion (python-nav-backward-defun))
718 (python-tests-look-at " def a(self): # a" -1)))
719 (should (= (save-excursion (python-nav-backward-defun))
720 (python-tests-look-at "class A(object): # A" -1)))
721 (should (not (python-nav-backward-defun)))))
723 (ert-deftest python-nav-backward-defun-2 ()
724 (python-tests-with-temp-buffer
726 def decoratorFunctionWithArguments(arg1, arg2, arg3):
727 '''print decorated function call data to stdout.
729 Usage:
731 @decoratorFunctionWithArguments('arg1', 'arg2')
732 def func(a, b, c=True):
733 pass
736 def wwrap(f):
737 print 'Inside wwrap()'
738 def wrapped_f(*args):
739 print 'Inside wrapped_f()'
740 print 'Decorator arguments:', arg1, arg2, arg3
741 f(*args)
742 print 'After f(*args)'
743 return wrapped_f
744 return wwrap
746 (goto-char (point-max))
747 (should (= (save-excursion (python-nav-backward-defun))
748 (python-tests-look-at " def wrapped_f(*args):" -1)))
749 (should (= (save-excursion (python-nav-backward-defun))
750 (python-tests-look-at " def wwrap(f):" -1)))
751 (should (= (save-excursion (python-nav-backward-defun))
752 (python-tests-look-at "def decoratorFunctionWithArguments(arg1, arg2, arg3):" -1)))
753 (should (not (python-nav-backward-defun)))))
755 (ert-deftest python-nav-backward-defun-3 ()
756 (python-tests-with-temp-buffer
759 def u(self):
760 pass
762 def v(self):
763 pass
765 def w(self):
766 pass
769 class A(object):
770 pass
772 (goto-char (point-min))
773 (let ((point (python-tests-look-at "class A(object):")))
774 (should (not (python-nav-backward-defun)))
775 (should (= point (point))))))
777 (ert-deftest python-nav-forward-defun-1 ()
778 (python-tests-with-temp-buffer
780 class A(object): # A
782 def a(self): # a
783 pass
785 def b(self): # b
786 pass
788 class B(object): # B
790 class C(object): # C
792 def d(self): # d
793 pass
795 # def e(self): # e
796 # pass
798 def c(self): # c
799 pass
801 # def d(self): # d
802 # pass
804 (goto-char (point-min))
805 (should (= (save-excursion (python-nav-forward-defun))
806 (python-tests-look-at "(object): # A")))
807 (should (= (save-excursion (python-nav-forward-defun))
808 (python-tests-look-at "(self): # a")))
809 (should (= (save-excursion (python-nav-forward-defun))
810 (python-tests-look-at "(self): # b")))
811 (should (= (save-excursion (python-nav-forward-defun))
812 (python-tests-look-at "(object): # B")))
813 (should (= (save-excursion (python-nav-forward-defun))
814 (python-tests-look-at "(object): # C")))
815 (should (= (save-excursion (python-nav-forward-defun))
816 (python-tests-look-at "(self): # d")))
817 (should (= (save-excursion (python-nav-forward-defun))
818 (python-tests-look-at "(self): # c")))
819 (should (not (python-nav-forward-defun)))))
821 (ert-deftest python-nav-forward-defun-2 ()
822 (python-tests-with-temp-buffer
824 def decoratorFunctionWithArguments(arg1, arg2, arg3):
825 '''print decorated function call data to stdout.
827 Usage:
829 @decoratorFunctionWithArguments('arg1', 'arg2')
830 def func(a, b, c=True):
831 pass
834 def wwrap(f):
835 print 'Inside wwrap()'
836 def wrapped_f(*args):
837 print 'Inside wrapped_f()'
838 print 'Decorator arguments:', arg1, arg2, arg3
839 f(*args)
840 print 'After f(*args)'
841 return wrapped_f
842 return wwrap
844 (goto-char (point-min))
845 (should (= (save-excursion (python-nav-forward-defun))
846 (python-tests-look-at "(arg1, arg2, arg3):")))
847 (should (= (save-excursion (python-nav-forward-defun))
848 (python-tests-look-at "(f):")))
849 (should (= (save-excursion (python-nav-forward-defun))
850 (python-tests-look-at "(*args):")))
851 (should (not (python-nav-forward-defun)))))
853 (ert-deftest python-nav-forward-defun-3 ()
854 (python-tests-with-temp-buffer
856 class A(object):
857 pass
860 def u(self):
861 pass
863 def v(self):
864 pass
866 def w(self):
867 pass
870 (goto-char (point-min))
871 (let ((point (python-tests-look-at "(object):")))
872 (should (not (python-nav-forward-defun)))
873 (should (= point (point))))))
875 (ert-deftest python-nav-beginning-of-statement-1 ()
876 (python-tests-with-temp-buffer
878 v1 = 123 + \
879 456 + \
881 v2 = (value1,
882 value2,
884 value3,
885 value4)
886 v3 = ('this is a string'
888 'that is continued'
889 'between lines'
890 'within a paren',
891 # this is a comment, yo
892 'continue previous line')
893 v4 = '''
894 a very long
895 string
898 (python-tests-look-at "v2 =")
899 (python-util-forward-comment -1)
900 (should (= (save-excursion
901 (python-nav-beginning-of-statement)
902 (point))
903 (python-tests-look-at "v1 =" -1 t)))
904 (python-tests-look-at "v3 =")
905 (python-util-forward-comment -1)
906 (should (= (save-excursion
907 (python-nav-beginning-of-statement)
908 (point))
909 (python-tests-look-at "v2 =" -1 t)))
910 (python-tests-look-at "v4 =")
911 (python-util-forward-comment -1)
912 (should (= (save-excursion
913 (python-nav-beginning-of-statement)
914 (point))
915 (python-tests-look-at "v3 =" -1 t)))
916 (goto-char (point-max))
917 (python-util-forward-comment -1)
918 (should (= (save-excursion
919 (python-nav-beginning-of-statement)
920 (point))
921 (python-tests-look-at "v4 =" -1 t)))))
923 (ert-deftest python-nav-end-of-statement-1 ()
924 (python-tests-with-temp-buffer
926 v1 = 123 + \
927 456 + \
929 v2 = (value1,
930 value2,
932 value3,
933 value4)
934 v3 = ('this is a string'
936 'that is continued'
937 'between lines'
938 'within a paren',
939 # this is a comment, yo
940 'continue previous line')
941 v4 = '''
942 a very long
943 string
946 (python-tests-look-at "v1 =")
947 (should (= (save-excursion
948 (python-nav-end-of-statement)
949 (point))
950 (save-excursion
951 (python-tests-look-at "789")
952 (line-end-position))))
953 (python-tests-look-at "v2 =")
954 (should (= (save-excursion
955 (python-nav-end-of-statement)
956 (point))
957 (save-excursion
958 (python-tests-look-at "value4)")
959 (line-end-position))))
960 (python-tests-look-at "v3 =")
961 (should (= (save-excursion
962 (python-nav-end-of-statement)
963 (point))
964 (save-excursion
965 (python-tests-look-at
966 "'continue previous line')")
967 (line-end-position))))
968 (python-tests-look-at "v4 =")
969 (should (= (save-excursion
970 (python-nav-end-of-statement)
971 (point))
972 (save-excursion
973 (goto-char (point-max))
974 (python-util-forward-comment -1)
975 (point))))))
977 (ert-deftest python-nav-forward-statement-1 ()
978 (python-tests-with-temp-buffer
980 v1 = 123 + \
981 456 + \
983 v2 = (value1,
984 value2,
986 value3,
987 value4)
988 v3 = ('this is a string'
990 'that is continued'
991 'between lines'
992 'within a paren',
993 # this is a comment, yo
994 'continue previous line')
995 v4 = '''
996 a very long
997 string
1000 (python-tests-look-at "v1 =")
1001 (should (= (save-excursion
1002 (python-nav-forward-statement)
1003 (point))
1004 (python-tests-look-at "v2 =")))
1005 (should (= (save-excursion
1006 (python-nav-forward-statement)
1007 (point))
1008 (python-tests-look-at "v3 =")))
1009 (should (= (save-excursion
1010 (python-nav-forward-statement)
1011 (point))
1012 (python-tests-look-at "v4 =")))
1013 (should (= (save-excursion
1014 (python-nav-forward-statement)
1015 (point))
1016 (point-max)))))
1018 (ert-deftest python-nav-backward-statement-1 ()
1019 (python-tests-with-temp-buffer
1021 v1 = 123 + \
1022 456 + \
1024 v2 = (value1,
1025 value2,
1027 value3,
1028 value4)
1029 v3 = ('this is a string'
1031 'that is continued'
1032 'between lines'
1033 'within a paren',
1034 # this is a comment, yo
1035 'continue previous line')
1036 v4 = '''
1037 a very long
1038 string
1041 (goto-char (point-max))
1042 (should (= (save-excursion
1043 (python-nav-backward-statement)
1044 (point))
1045 (python-tests-look-at "v4 =" -1)))
1046 (should (= (save-excursion
1047 (python-nav-backward-statement)
1048 (point))
1049 (python-tests-look-at "v3 =" -1)))
1050 (should (= (save-excursion
1051 (python-nav-backward-statement)
1052 (point))
1053 (python-tests-look-at "v2 =" -1)))
1054 (should (= (save-excursion
1055 (python-nav-backward-statement)
1056 (point))
1057 (python-tests-look-at "v1 =" -1)))))
1059 (ert-deftest python-nav-backward-statement-2 ()
1060 :expected-result :failed
1061 (python-tests-with-temp-buffer
1063 v1 = 123 + \
1064 456 + \
1066 v2 = (value1,
1067 value2,
1069 value3,
1070 value4)
1072 ;; FIXME: For some reason `python-nav-backward-statement' is moving
1073 ;; back two sentences when starting from 'value4)'.
1074 (goto-char (point-max))
1075 (python-util-forward-comment -1)
1076 (should (= (save-excursion
1077 (python-nav-backward-statement)
1078 (point))
1079 (python-tests-look-at "v2 =" -1 t)))))
1081 (ert-deftest python-nav-beginning-of-block-1 ()
1082 (python-tests-with-temp-buffer
1084 def decoratorFunctionWithArguments(arg1, arg2, arg3):
1085 '''print decorated function call data to stdout.
1087 Usage:
1089 @decoratorFunctionWithArguments('arg1', 'arg2')
1090 def func(a, b, c=True):
1091 pass
1094 def wwrap(f):
1095 print 'Inside wwrap()'
1096 def wrapped_f(*args):
1097 print 'Inside wrapped_f()'
1098 print 'Decorator arguments:', arg1, arg2, arg3
1099 f(*args)
1100 print 'After f(*args)'
1101 return wrapped_f
1102 return wwrap
1104 (python-tests-look-at "return wwrap")
1105 (should (= (save-excursion
1106 (python-nav-beginning-of-block)
1107 (point))
1108 (python-tests-look-at "def decoratorFunctionWithArguments" -1)))
1109 (python-tests-look-at "print 'Inside wwrap()'")
1110 (should (= (save-excursion
1111 (python-nav-beginning-of-block)
1112 (point))
1113 (python-tests-look-at "def wwrap(f):" -1)))
1114 (python-tests-look-at "print 'After f(*args)'")
1115 (end-of-line)
1116 (should (= (save-excursion
1117 (python-nav-beginning-of-block)
1118 (point))
1119 (python-tests-look-at "def wrapped_f(*args):" -1)))
1120 (python-tests-look-at "return wrapped_f")
1121 (should (= (save-excursion
1122 (python-nav-beginning-of-block)
1123 (point))
1124 (python-tests-look-at "def wwrap(f):" -1)))))
1126 (ert-deftest python-nav-end-of-block-1 ()
1127 (python-tests-with-temp-buffer
1129 def decoratorFunctionWithArguments(arg1, arg2, arg3):
1130 '''print decorated function call data to stdout.
1132 Usage:
1134 @decoratorFunctionWithArguments('arg1', 'arg2')
1135 def func(a, b, c=True):
1136 pass
1139 def wwrap(f):
1140 print 'Inside wwrap()'
1141 def wrapped_f(*args):
1142 print 'Inside wrapped_f()'
1143 print 'Decorator arguments:', arg1, arg2, arg3
1144 f(*args)
1145 print 'After f(*args)'
1146 return wrapped_f
1147 return wwrap
1149 (python-tests-look-at "def decoratorFunctionWithArguments")
1150 (should (= (save-excursion
1151 (python-nav-end-of-block)
1152 (point))
1153 (save-excursion
1154 (goto-char (point-max))
1155 (python-util-forward-comment -1)
1156 (point))))
1157 (python-tests-look-at "def wwrap(f):")
1158 (should (= (save-excursion
1159 (python-nav-end-of-block)
1160 (point))
1161 (save-excursion
1162 (python-tests-look-at "return wrapped_f")
1163 (line-end-position))))
1164 (end-of-line)
1165 (should (= (save-excursion
1166 (python-nav-end-of-block)
1167 (point))
1168 (save-excursion
1169 (python-tests-look-at "return wrapped_f")
1170 (line-end-position))))
1171 (python-tests-look-at "f(*args)")
1172 (should (= (save-excursion
1173 (python-nav-end-of-block)
1174 (point))
1175 (save-excursion
1176 (python-tests-look-at "print 'After f(*args)'")
1177 (line-end-position))))))
1179 (ert-deftest python-nav-forward-block-1 ()
1180 "This also accounts as a test for `python-nav-backward-block'."
1181 (python-tests-with-temp-buffer
1183 if request.user.is_authenticated():
1184 # def block():
1185 # pass
1186 try:
1187 profile = request.user.get_profile()
1188 except Profile.DoesNotExist:
1189 profile = Profile.objects.create(user=request.user)
1190 else:
1191 if profile.stats:
1192 profile.recalculate_stats()
1193 else:
1194 profile.clear_stats()
1195 finally:
1196 profile.views += 1
1197 profile.save()
1199 (should (= (save-excursion (python-nav-forward-block))
1200 (python-tests-look-at "if request.user.is_authenticated():")))
1201 (should (= (save-excursion (python-nav-forward-block))
1202 (python-tests-look-at "try:")))
1203 (should (= (save-excursion (python-nav-forward-block))
1204 (python-tests-look-at "except Profile.DoesNotExist:")))
1205 (should (= (save-excursion (python-nav-forward-block))
1206 (python-tests-look-at "else:")))
1207 (should (= (save-excursion (python-nav-forward-block))
1208 (python-tests-look-at "if profile.stats:")))
1209 (should (= (save-excursion (python-nav-forward-block))
1210 (python-tests-look-at "else:")))
1211 (should (= (save-excursion (python-nav-forward-block))
1212 (python-tests-look-at "finally:")))
1213 ;; When point is at the last block, leave it there and return nil
1214 (should (not (save-excursion (python-nav-forward-block))))
1215 ;; Move backwards, and even if the number of moves is less than the
1216 ;; provided argument return the point.
1217 (should (= (save-excursion (python-nav-forward-block -10))
1218 (python-tests-look-at
1219 "if request.user.is_authenticated():" -1)))))
1221 (ert-deftest python-nav-lisp-forward-sexp-safe-1 ()
1222 (python-tests-with-temp-buffer
1224 profile = Profile.objects.create(user=request.user)
1225 profile.notify()
1227 (python-tests-look-at "profile =")
1228 (python-nav-lisp-forward-sexp-safe 4)
1229 (should (looking-at "(user=request.user)"))
1230 (python-tests-look-at "user=request.user")
1231 (python-nav-lisp-forward-sexp-safe -1)
1232 (should (looking-at "(user=request.user)"))
1233 (python-nav-lisp-forward-sexp-safe -4)
1234 (should (looking-at "profile ="))
1235 (python-tests-look-at "user=request.user")
1236 (python-nav-lisp-forward-sexp-safe 3)
1237 (should (looking-at ")"))
1238 (python-nav-lisp-forward-sexp-safe 1)
1239 (should (looking-at "$"))
1240 (python-nav-lisp-forward-sexp-safe 1)
1241 (should (looking-at ".notify()"))))
1243 (ert-deftest python-nav-forward-sexp-1 ()
1244 (python-tests-with-temp-buffer
1250 (python-tests-look-at "a()")
1251 (python-nav-forward-sexp)
1252 (should (looking-at "$"))
1253 (should (save-excursion
1254 (beginning-of-line)
1255 (looking-at "a()")))
1256 (python-nav-forward-sexp)
1257 (should (looking-at "$"))
1258 (should (save-excursion
1259 (beginning-of-line)
1260 (looking-at "b()")))
1261 (python-nav-forward-sexp)
1262 (should (looking-at "$"))
1263 (should (save-excursion
1264 (beginning-of-line)
1265 (looking-at "c()")))
1266 ;; Movement next to a paren should do what lisp does and
1267 ;; unfortunately It can't change, because otherwise
1268 ;; `blink-matching-open' breaks.
1269 (python-nav-forward-sexp -1)
1270 (should (looking-at "()"))
1271 (should (save-excursion
1272 (beginning-of-line)
1273 (looking-at "c()")))
1274 (python-nav-forward-sexp -1)
1275 (should (looking-at "c()"))
1276 (python-nav-forward-sexp -1)
1277 (should (looking-at "b()"))
1278 (python-nav-forward-sexp -1)
1279 (should (looking-at "a()"))))
1281 (ert-deftest python-nav-forward-sexp-2 ()
1282 (python-tests-with-temp-buffer
1284 def func():
1285 if True:
1286 aaa = bbb
1287 ccc = ddd
1288 eee = fff
1289 return ggg
1291 (python-tests-look-at "aa =")
1292 (python-nav-forward-sexp)
1293 (should (looking-at " = bbb"))
1294 (python-nav-forward-sexp)
1295 (should (looking-at "$"))
1296 (should (save-excursion
1297 (back-to-indentation)
1298 (looking-at "aaa = bbb")))
1299 (python-nav-forward-sexp)
1300 (should (looking-at "$"))
1301 (should (save-excursion
1302 (back-to-indentation)
1303 (looking-at "ccc = ddd")))
1304 (python-nav-forward-sexp)
1305 (should (looking-at "$"))
1306 (should (save-excursion
1307 (back-to-indentation)
1308 (looking-at "eee = fff")))
1309 (python-nav-forward-sexp)
1310 (should (looking-at "$"))
1311 (should (save-excursion
1312 (back-to-indentation)
1313 (looking-at "return ggg")))
1314 (python-nav-forward-sexp -1)
1315 (should (looking-at "def func():"))))
1317 (ert-deftest python-nav-forward-sexp-3 ()
1318 (python-tests-with-temp-buffer
1320 from some_module import some_sub_module
1321 from another_module import another_sub_module
1323 def another_statement():
1324 pass
1326 (python-tests-look-at "some_module")
1327 (python-nav-forward-sexp)
1328 (should (looking-at " import"))
1329 (python-nav-forward-sexp)
1330 (should (looking-at " some_sub_module"))
1331 (python-nav-forward-sexp)
1332 (should (looking-at "$"))
1333 (should
1334 (save-excursion
1335 (back-to-indentation)
1336 (looking-at
1337 "from some_module import some_sub_module")))
1338 (python-nav-forward-sexp)
1339 (should (looking-at "$"))
1340 (should
1341 (save-excursion
1342 (back-to-indentation)
1343 (looking-at
1344 "from another_module import another_sub_module")))
1345 (python-nav-forward-sexp)
1346 (should (looking-at "$"))
1347 (should
1348 (save-excursion
1349 (back-to-indentation)
1350 (looking-at
1351 "pass")))
1352 (python-nav-forward-sexp -1)
1353 (should (looking-at "def another_statement():"))
1354 (python-nav-forward-sexp -1)
1355 (should (looking-at "from another_module import another_sub_module"))
1356 (python-nav-forward-sexp -1)
1357 (should (looking-at "from some_module import some_sub_module"))))
1359 (ert-deftest python-nav-up-list-1 ()
1360 (python-tests-with-temp-buffer
1362 def f():
1363 if True:
1364 return [i for i in range(3)]
1366 (python-tests-look-at "3)]")
1367 (python-nav-up-list)
1368 (should (looking-at "]"))
1369 (python-nav-up-list)
1370 (should (looking-at "$"))))
1372 (ert-deftest python-nav-backward-up-list-1 ()
1373 :expected-result :failed
1374 (python-tests-with-temp-buffer
1376 def f():
1377 if True:
1378 return [i for i in range(3)]
1380 (python-tests-look-at "3)]")
1381 (python-nav-backward-up-list)
1382 (should (looking-at "(3)\\]"))
1383 (python-nav-backward-up-list)
1384 (should (looking-at
1385 "\\[i for i in range(3)\\]"))
1386 ;; FIXME: Need to move to beginning-of-statement.
1387 (python-nav-backward-up-list)
1388 (should (looking-at
1389 "return \\[i for i in range(3)\\]"))
1390 (python-nav-backward-up-list)
1391 (should (looking-at "if True:"))
1392 (python-nav-backward-up-list)
1393 (should (looking-at "def f():"))))
1396 ;;; Shell integration
1398 (defvar python-tests-shell-interpreter "python")
1400 (ert-deftest python-shell-get-process-name-1 ()
1401 "Check process name calculation on different scenarios."
1402 (python-tests-with-temp-buffer
1404 (should (string= (python-shell-get-process-name nil)
1405 python-shell-buffer-name))
1406 ;; When the `current-buffer' doesn't have `buffer-file-name', even
1407 ;; if dedicated flag is non-nil should not include its name.
1408 (should (string= (python-shell-get-process-name t)
1409 python-shell-buffer-name)))
1410 (python-tests-with-temp-file
1412 ;; `buffer-file-name' is non-nil but the dedicated flag is nil and
1413 ;; should be respected.
1414 (should (string= (python-shell-get-process-name nil)
1415 python-shell-buffer-name))
1416 (should (string=
1417 (python-shell-get-process-name t)
1418 (format "%s[%s]" python-shell-buffer-name buffer-file-name)))))
1420 (ert-deftest python-shell-internal-get-process-name-1 ()
1421 "Check the internal process name is config-unique."
1422 (let* ((python-shell-interpreter python-tests-shell-interpreter)
1423 (python-shell-interpreter-args "")
1424 (python-shell-prompt-regexp ">>> ")
1425 (python-shell-prompt-block-regexp "[.][.][.] ")
1426 (python-shell-setup-codes "")
1427 (python-shell-process-environment "")
1428 (python-shell-extra-pythonpaths "")
1429 (python-shell-exec-path "")
1430 (python-shell-virtualenv-path "")
1431 (expected (python-tests-with-temp-buffer
1432 "" (python-shell-internal-get-process-name))))
1433 ;; Same configurations should match.
1434 (should
1435 (string= expected
1436 (python-tests-with-temp-buffer
1437 "" (python-shell-internal-get-process-name))))
1438 (let ((python-shell-interpreter-args "-B"))
1439 ;; A minimal change should generate different names.
1440 (should
1441 (not (string=
1442 expected
1443 (python-tests-with-temp-buffer
1444 "" (python-shell-internal-get-process-name))))))))
1446 (ert-deftest python-shell-parse-command-1 ()
1447 "Check the command to execute is calculated correctly.
1448 Using `python-shell-interpreter' and
1449 `python-shell-interpreter-args'."
1450 :expected-result (if (executable-find python-tests-shell-interpreter)
1451 :passed
1452 :failed)
1453 (let ((python-shell-interpreter (executable-find
1454 python-tests-shell-interpreter))
1455 (python-shell-interpreter-args "-B"))
1456 (should (string=
1457 (format "%s %s"
1458 python-shell-interpreter
1459 python-shell-interpreter-args)
1460 (python-shell-parse-command)))))
1462 (ert-deftest python-shell-calculate-process-environment-1 ()
1463 "Test `python-shell-process-environment' modification."
1464 (let* ((original-process-environment process-environment)
1465 (python-shell-process-environment
1466 '("TESTVAR1=value1" "TESTVAR2=value2"))
1467 (process-environment
1468 (python-shell-calculate-process-environment)))
1469 (should (equal (getenv "TESTVAR1") "value1"))
1470 (should (equal (getenv "TESTVAR2") "value2"))))
1472 (ert-deftest python-shell-calculate-process-environment-2 ()
1473 "Test `python-shell-extra-pythonpaths' modification."
1474 (let* ((original-process-environment process-environment)
1475 (original-pythonpath (getenv "PYTHONPATH"))
1476 (paths '("path1" "path2"))
1477 (python-shell-extra-pythonpaths paths)
1478 (process-environment
1479 (python-shell-calculate-process-environment)))
1480 (should (equal (getenv "PYTHONPATH")
1481 (concat
1482 (mapconcat 'identity paths path-separator)
1483 path-separator original-pythonpath)))))
1485 (ert-deftest python-shell-calculate-process-environment-3 ()
1486 "Test `python-shell-virtualenv-path' modification."
1487 (let* ((original-process-environment process-environment)
1488 (original-path (or (getenv "PATH") ""))
1489 (python-shell-virtualenv-path
1490 (directory-file-name user-emacs-directory))
1491 (process-environment
1492 (python-shell-calculate-process-environment)))
1493 (should (not (getenv "PYTHONHOME")))
1494 (should (string= (getenv "VIRTUAL_ENV") python-shell-virtualenv-path))
1495 (should (equal (getenv "PATH")
1496 (format "%s/bin%s%s"
1497 python-shell-virtualenv-path
1498 path-separator original-path)))))
1500 (ert-deftest python-shell-calculate-exec-path-1 ()
1501 "Test `python-shell-exec-path' modification."
1502 (let* ((original-exec-path exec-path)
1503 (python-shell-exec-path '("path1" "path2"))
1504 (exec-path (python-shell-calculate-exec-path)))
1505 (should (equal
1506 exec-path
1507 (append python-shell-exec-path
1508 original-exec-path)))))
1510 (ert-deftest python-shell-calculate-exec-path-2 ()
1511 "Test `python-shell-exec-path' modification."
1512 (let* ((original-exec-path exec-path)
1513 (python-shell-virtualenv-path
1514 (directory-file-name user-emacs-directory))
1515 (exec-path (python-shell-calculate-exec-path)))
1516 (should (equal
1517 exec-path
1518 (append (cons
1519 (format "%s/bin" python-shell-virtualenv-path)
1520 original-exec-path))))))
1522 (ert-deftest python-shell-make-comint-1 ()
1523 "Check comint creation for global shell buffer."
1524 :expected-result (if (executable-find python-tests-shell-interpreter)
1525 :passed
1526 :failed)
1527 (let* ((python-shell-interpreter
1528 (executable-find python-tests-shell-interpreter))
1529 (proc-name (python-shell-get-process-name nil))
1530 (shell-buffer
1531 (python-tests-with-temp-buffer
1532 "" (python-shell-make-comint
1533 (python-shell-parse-command) proc-name)))
1534 (process (get-buffer-process shell-buffer)))
1535 (unwind-protect
1536 (progn
1537 (set-process-query-on-exit-flag process nil)
1538 (should (process-live-p process))
1539 (with-current-buffer shell-buffer
1540 (should (eq major-mode 'inferior-python-mode))
1541 (should (string= (buffer-name) (format "*%s*" proc-name)))))
1542 (kill-buffer shell-buffer))))
1544 (ert-deftest python-shell-make-comint-2 ()
1545 "Check comint creation for internal shell buffer."
1546 :expected-result (if (executable-find python-tests-shell-interpreter)
1547 :passed
1548 :failed)
1549 (let* ((python-shell-interpreter
1550 (executable-find python-tests-shell-interpreter))
1551 (proc-name (python-shell-internal-get-process-name))
1552 (shell-buffer
1553 (python-tests-with-temp-buffer
1554 "" (python-shell-make-comint
1555 (python-shell-parse-command) proc-name nil t)))
1556 (process (get-buffer-process shell-buffer)))
1557 (unwind-protect
1558 (progn
1559 (set-process-query-on-exit-flag process nil)
1560 (should (process-live-p process))
1561 (with-current-buffer shell-buffer
1562 (should (eq major-mode 'inferior-python-mode))
1563 (should (string= (buffer-name) (format " *%s*" proc-name)))))
1564 (kill-buffer shell-buffer))))
1566 (ert-deftest python-shell-get-process-1 ()
1567 "Check dedicated shell process preference over global."
1568 :expected-result (if (executable-find python-tests-shell-interpreter)
1569 :passed
1570 :failed)
1571 (python-tests-with-temp-file
1573 (let* ((python-shell-interpreter
1574 (executable-find python-tests-shell-interpreter))
1575 (global-proc-name (python-shell-get-process-name nil))
1576 (dedicated-proc-name (python-shell-get-process-name t))
1577 (global-shell-buffer
1578 (python-shell-make-comint
1579 (python-shell-parse-command) global-proc-name))
1580 (dedicated-shell-buffer
1581 (python-shell-make-comint
1582 (python-shell-parse-command) dedicated-proc-name))
1583 (global-process (get-buffer-process global-shell-buffer))
1584 (dedicated-process (get-buffer-process dedicated-shell-buffer)))
1585 (unwind-protect
1586 (progn
1587 (set-process-query-on-exit-flag global-process nil)
1588 (set-process-query-on-exit-flag dedicated-process nil)
1589 ;; Prefer dedicated if global also exists.
1590 (should (equal (python-shell-get-process) dedicated-process))
1591 (kill-buffer dedicated-shell-buffer)
1592 ;; If there's only global, use it.
1593 (should (equal (python-shell-get-process) global-process))
1594 (kill-buffer global-shell-buffer)
1595 ;; No buffer available.
1596 (should (not (python-shell-get-process))))
1597 (ignore-errors (kill-buffer global-shell-buffer))
1598 (ignore-errors (kill-buffer dedicated-shell-buffer))))))
1600 (ert-deftest python-shell-get-or-create-process-1 ()
1601 "Check shell process creation fallback."
1602 :expected-result :failed
1603 (python-tests-with-temp-file
1605 ;; XXX: Break early until we can skip stuff. We need to mimic
1606 ;; user interaction because `python-shell-get-or-create-process'
1607 ;; asks for all arguments interactively when a shell process
1608 ;; doesn't exist.
1609 (should nil)
1610 (let* ((python-shell-interpreter
1611 (executable-find python-tests-shell-interpreter))
1612 (use-dialog-box)
1613 (dedicated-process-name (python-shell-get-process-name t))
1614 (dedicated-process (python-shell-get-or-create-process))
1615 (dedicated-shell-buffer (process-buffer dedicated-process)))
1616 (unwind-protect
1617 (progn
1618 (set-process-query-on-exit-flag dedicated-process nil)
1619 ;; Prefer dedicated if not buffer exist.
1620 (should (equal (process-name dedicated-process)
1621 dedicated-process-name))
1622 (kill-buffer dedicated-shell-buffer)
1623 ;; No buffer available.
1624 (should (not (python-shell-get-process))))
1625 (ignore-errors (kill-buffer dedicated-shell-buffer))))))
1627 (ert-deftest python-shell-internal-get-or-create-process-1 ()
1628 "Check internal shell process creation fallback."
1629 :expected-result (if (executable-find python-tests-shell-interpreter)
1630 :passed
1631 :failed)
1632 (python-tests-with-temp-file
1634 (should (not (process-live-p (python-shell-internal-get-process-name))))
1635 (let* ((python-shell-interpreter
1636 (executable-find python-tests-shell-interpreter))
1637 (internal-process-name (python-shell-internal-get-process-name))
1638 (internal-process (python-shell-internal-get-or-create-process))
1639 (internal-shell-buffer (process-buffer internal-process)))
1640 (unwind-protect
1641 (progn
1642 (set-process-query-on-exit-flag internal-process nil)
1643 (should (equal (process-name internal-process)
1644 internal-process-name))
1645 (should (equal internal-process
1646 (python-shell-internal-get-or-create-process)))
1647 ;; No user buffer available.
1648 (should (not (python-shell-get-process)))
1649 (kill-buffer internal-shell-buffer))
1650 (ignore-errors (kill-buffer internal-shell-buffer))))))
1653 ;;; Shell completion
1656 ;;; PDB Track integration
1659 ;;; Symbol completion
1662 ;;; Fill paragraph
1665 ;;; Skeletons
1668 ;;; FFAP
1671 ;;; Code check
1674 ;;; Eldoc
1677 ;;; Imenu
1679 (ert-deftest python-imenu-create-index-1 ()
1680 (python-tests-with-temp-buffer
1682 class Foo(models.Model):
1683 pass
1686 class Bar(models.Model):
1687 pass
1690 def decorator(arg1, arg2, arg3):
1691 '''print decorated function call data to stdout.
1693 Usage:
1695 @decorator('arg1', 'arg2')
1696 def func(a, b, c=True):
1697 pass
1700 def wrap(f):
1701 print ('wrap')
1702 def wrapped_f(*args):
1703 print ('wrapped_f')
1704 print ('Decorator arguments:', arg1, arg2, arg3)
1705 f(*args)
1706 print ('called f(*args)')
1707 return wrapped_f
1708 return wrap
1711 class Baz(object):
1713 def a(self):
1714 pass
1716 def b(self):
1717 pass
1719 class Frob(object):
1721 def c(self):
1722 pass
1724 (goto-char (point-max))
1725 (should (equal
1726 (list
1727 (cons "Foo (class)" (copy-marker 2))
1728 (cons "Bar (class)" (copy-marker 38))
1729 (list
1730 "decorator (def)"
1731 (cons "*function definition*" (copy-marker 74))
1732 (list
1733 "wrap (def)"
1734 (cons "*function definition*" (copy-marker 254))
1735 (cons "wrapped_f (def)" (copy-marker 294))))
1736 (list
1737 "Baz (class)"
1738 (cons "*class definition*" (copy-marker 519))
1739 (cons "a (def)" (copy-marker 539))
1740 (cons "b (def)" (copy-marker 570))
1741 (list
1742 "Frob (class)"
1743 (cons "*class definition*" (copy-marker 601))
1744 (cons "c (def)" (copy-marker 626)))))
1745 (python-imenu-create-index)))))
1747 (ert-deftest python-imenu-create-flat-index-1 ()
1748 (python-tests-with-temp-buffer
1750 class Foo(models.Model):
1751 pass
1754 class Bar(models.Model):
1755 pass
1758 def decorator(arg1, arg2, arg3):
1759 '''print decorated function call data to stdout.
1761 Usage:
1763 @decorator('arg1', 'arg2')
1764 def func(a, b, c=True):
1765 pass
1768 def wrap(f):
1769 print ('wrap')
1770 def wrapped_f(*args):
1771 print ('wrapped_f')
1772 print ('Decorator arguments:', arg1, arg2, arg3)
1773 f(*args)
1774 print ('called f(*args)')
1775 return wrapped_f
1776 return wrap
1779 class Baz(object):
1781 def a(self):
1782 pass
1784 def b(self):
1785 pass
1787 class Frob(object):
1789 def c(self):
1790 pass
1792 (goto-char (point-max))
1793 (should (equal
1794 (list (cons "Foo" (copy-marker 2))
1795 (cons "Bar" (copy-marker 38))
1796 (cons "decorator" (copy-marker 74))
1797 (cons "decorator.wrap" (copy-marker 254))
1798 (cons "decorator.wrap.wrapped_f" (copy-marker 294))
1799 (cons "Baz" (copy-marker 519))
1800 (cons "Baz.a" (copy-marker 539))
1801 (cons "Baz.b" (copy-marker 570))
1802 (cons "Baz.Frob" (copy-marker 601))
1803 (cons "Baz.Frob.c" (copy-marker 626)))
1804 (python-imenu-create-flat-index)))))
1807 ;;; Misc helpers
1809 (ert-deftest python-info-current-defun-1 ()
1810 (python-tests-with-temp-buffer
1812 def foo(a, b):
1814 (forward-line 1)
1815 (should (string= "foo" (python-info-current-defun)))
1816 (should (string= "def foo" (python-info-current-defun t)))
1817 (forward-line 1)
1818 (should (not (python-info-current-defun)))
1819 (indent-for-tab-command)
1820 (should (string= "foo" (python-info-current-defun)))
1821 (should (string= "def foo" (python-info-current-defun t)))))
1823 (ert-deftest python-info-current-defun-2 ()
1824 (python-tests-with-temp-buffer
1826 class C(object):
1828 def m(self):
1829 if True:
1830 return [i for i in range(3)]
1831 else:
1832 return []
1834 def b():
1835 do_b()
1837 def a():
1838 do_a()
1840 def c(self):
1841 do_c()
1843 (forward-line 1)
1844 (should (string= "C" (python-info-current-defun)))
1845 (should (string= "class C" (python-info-current-defun t)))
1846 (python-tests-look-at "return [i for ")
1847 (should (string= "C.m" (python-info-current-defun)))
1848 (should (string= "def C.m" (python-info-current-defun t)))
1849 (python-tests-look-at "def b():")
1850 (should (string= "C.m.b" (python-info-current-defun)))
1851 (should (string= "def C.m.b" (python-info-current-defun t)))
1852 (forward-line 2)
1853 (indent-for-tab-command)
1854 (python-indent-dedent-line-backspace 1)
1855 (should (string= "C.m" (python-info-current-defun)))
1856 (should (string= "def C.m" (python-info-current-defun t)))
1857 (python-tests-look-at "def c(self):")
1858 (forward-line -1)
1859 (indent-for-tab-command)
1860 (should (string= "C.m.a" (python-info-current-defun)))
1861 (should (string= "def C.m.a" (python-info-current-defun t)))
1862 (python-indent-dedent-line-backspace 1)
1863 (should (string= "C.m" (python-info-current-defun)))
1864 (should (string= "def C.m" (python-info-current-defun t)))
1865 (python-indent-dedent-line-backspace 1)
1866 (should (string= "C" (python-info-current-defun)))
1867 (should (string= "class C" (python-info-current-defun t)))
1868 (python-tests-look-at "def c(self):")
1869 (should (string= "C.c" (python-info-current-defun)))
1870 (should (string= "def C.c" (python-info-current-defun t)))
1871 (python-tests-look-at "do_c()")
1872 (should (string= "C.c" (python-info-current-defun)))
1873 (should (string= "def C.c" (python-info-current-defun t)))))
1875 (ert-deftest python-info-current-defun-3 ()
1876 (python-tests-with-temp-buffer
1878 def decoratorFunctionWithArguments(arg1, arg2, arg3):
1879 '''print decorated function call data to stdout.
1881 Usage:
1883 @decoratorFunctionWithArguments('arg1', 'arg2')
1884 def func(a, b, c=True):
1885 pass
1888 def wwrap(f):
1889 print 'Inside wwrap()'
1890 def wrapped_f(*args):
1891 print 'Inside wrapped_f()'
1892 print 'Decorator arguments:', arg1, arg2, arg3
1893 f(*args)
1894 print 'After f(*args)'
1895 return wrapped_f
1896 return wwrap
1898 (python-tests-look-at "def wwrap(f):")
1899 (forward-line -1)
1900 (should (not (python-info-current-defun)))
1901 (indent-for-tab-command 1)
1902 (should (string= (python-info-current-defun)
1903 "decoratorFunctionWithArguments"))
1904 (should (string= (python-info-current-defun t)
1905 "def decoratorFunctionWithArguments"))
1906 (python-tests-look-at "def wrapped_f(*args):")
1907 (should (string= (python-info-current-defun)
1908 "decoratorFunctionWithArguments.wwrap.wrapped_f"))
1909 (should (string= (python-info-current-defun t)
1910 "def decoratorFunctionWithArguments.wwrap.wrapped_f"))
1911 (python-tests-look-at "return wrapped_f")
1912 (should (string= (python-info-current-defun)
1913 "decoratorFunctionWithArguments.wwrap"))
1914 (should (string= (python-info-current-defun t)
1915 "def decoratorFunctionWithArguments.wwrap"))
1916 (end-of-line 1)
1917 (python-tests-look-at "return wwrap")
1918 (should (string= (python-info-current-defun)
1919 "decoratorFunctionWithArguments"))
1920 (should (string= (python-info-current-defun t)
1921 "def decoratorFunctionWithArguments"))))
1923 (ert-deftest python-info-current-symbol-1 ()
1924 (python-tests-with-temp-buffer
1926 class C(object):
1928 def m(self):
1929 self.c()
1931 def c(self):
1932 print ('a')
1934 (python-tests-look-at "self.c()")
1935 (should (string= "self.c" (python-info-current-symbol)))
1936 (should (string= "C.c" (python-info-current-symbol t)))))
1938 (ert-deftest python-info-current-symbol-2 ()
1939 (python-tests-with-temp-buffer
1941 class C(object):
1943 class M(object):
1945 def a(self):
1946 self.c()
1948 def c(self):
1949 pass
1951 (python-tests-look-at "self.c()")
1952 (should (string= "self.c" (python-info-current-symbol)))
1953 (should (string= "C.M.c" (python-info-current-symbol t)))))
1955 (ert-deftest python-info-current-symbol-3 ()
1956 "Keywords should not be considered symbols."
1957 :expected-result :failed
1958 (python-tests-with-temp-buffer
1960 class C(object):
1961 pass
1963 ;; FIXME: keywords are not symbols.
1964 (python-tests-look-at "class C")
1965 (should (not (python-info-current-symbol)))
1966 (should (not (python-info-current-symbol t)))
1967 (python-tests-look-at "C(object)")
1968 (should (string= "C" (python-info-current-symbol)))
1969 (should (string= "class C" (python-info-current-symbol t)))))
1971 (ert-deftest python-info-statement-starts-block-p-1 ()
1972 (python-tests-with-temp-buffer
1974 def long_function_name(
1975 var_one, var_two, var_three,
1976 var_four):
1977 print (var_one)
1979 (python-tests-look-at "def long_function_name")
1980 (should (python-info-statement-starts-block-p))
1981 (python-tests-look-at "print (var_one)")
1982 (python-util-forward-comment -1)
1983 (should (python-info-statement-starts-block-p))))
1985 (ert-deftest python-info-statement-starts-block-p-2 ()
1986 (python-tests-with-temp-buffer
1988 if width == 0 and height == 0 and \\\\
1989 color == 'red' and emphasis == 'strong' or \\\\
1990 highlight > 100:
1991 raise ValueError('sorry, you lose')
1993 (python-tests-look-at "if width == 0 and")
1994 (should (python-info-statement-starts-block-p))
1995 (python-tests-look-at "raise ValueError(")
1996 (python-util-forward-comment -1)
1997 (should (python-info-statement-starts-block-p))))
1999 (ert-deftest python-info-statement-ends-block-p-1 ()
2000 (python-tests-with-temp-buffer
2002 def long_function_name(
2003 var_one, var_two, var_three,
2004 var_four):
2005 print (var_one)
2007 (python-tests-look-at "print (var_one)")
2008 (should (python-info-statement-ends-block-p))))
2010 (ert-deftest python-info-statement-ends-block-p-2 ()
2011 (python-tests-with-temp-buffer
2013 if width == 0 and height == 0 and \\\\
2014 color == 'red' and emphasis == 'strong' or \\\\
2015 highlight > 100:
2016 raise ValueError(
2017 'sorry, you lose'
2021 (python-tests-look-at "raise ValueError(")
2022 (should (python-info-statement-ends-block-p))))
2024 (ert-deftest python-info-beginning-of-statement-p-1 ()
2025 (python-tests-with-temp-buffer
2027 def long_function_name(
2028 var_one, var_two, var_three,
2029 var_four):
2030 print (var_one)
2032 (python-tests-look-at "def long_function_name")
2033 (should (python-info-beginning-of-statement-p))
2034 (forward-char 10)
2035 (should (not (python-info-beginning-of-statement-p)))
2036 (python-tests-look-at "print (var_one)")
2037 (should (python-info-beginning-of-statement-p))
2038 (goto-char (line-beginning-position))
2039 (should (not (python-info-beginning-of-statement-p)))))
2041 (ert-deftest python-info-beginning-of-statement-p-2 ()
2042 (python-tests-with-temp-buffer
2044 if width == 0 and height == 0 and \\\\
2045 color == 'red' and emphasis == 'strong' or \\\\
2046 highlight > 100:
2047 raise ValueError(
2048 'sorry, you lose'
2052 (python-tests-look-at "if width == 0 and")
2053 (should (python-info-beginning-of-statement-p))
2054 (forward-char 10)
2055 (should (not (python-info-beginning-of-statement-p)))
2056 (python-tests-look-at "raise ValueError(")
2057 (should (python-info-beginning-of-statement-p))
2058 (goto-char (line-beginning-position))
2059 (should (not (python-info-beginning-of-statement-p)))))
2061 (ert-deftest python-info-end-of-statement-p-1 ()
2062 (python-tests-with-temp-buffer
2064 def long_function_name(
2065 var_one, var_two, var_three,
2066 var_four):
2067 print (var_one)
2069 (python-tests-look-at "def long_function_name")
2070 (should (not (python-info-end-of-statement-p)))
2071 (end-of-line)
2072 (should (not (python-info-end-of-statement-p)))
2073 (python-tests-look-at "print (var_one)")
2074 (python-util-forward-comment -1)
2075 (should (python-info-end-of-statement-p))
2076 (python-tests-look-at "print (var_one)")
2077 (should (not (python-info-end-of-statement-p)))
2078 (end-of-line)
2079 (should (python-info-end-of-statement-p))))
2081 (ert-deftest python-info-end-of-statement-p-2 ()
2082 (python-tests-with-temp-buffer
2084 if width == 0 and height == 0 and \\\\
2085 color == 'red' and emphasis == 'strong' or \\\\
2086 highlight > 100:
2087 raise ValueError(
2088 'sorry, you lose'
2092 (python-tests-look-at "if width == 0 and")
2093 (should (not (python-info-end-of-statement-p)))
2094 (end-of-line)
2095 (should (not (python-info-end-of-statement-p)))
2096 (python-tests-look-at "raise ValueError(")
2097 (python-util-forward-comment -1)
2098 (should (python-info-end-of-statement-p))
2099 (python-tests-look-at "raise ValueError(")
2100 (should (not (python-info-end-of-statement-p)))
2101 (end-of-line)
2102 (should (not (python-info-end-of-statement-p)))
2103 (goto-char (point-max))
2104 (python-util-forward-comment -1)
2105 (should (python-info-end-of-statement-p))))
2107 (ert-deftest python-info-beginning-of-block-p-1 ()
2108 (python-tests-with-temp-buffer
2110 def long_function_name(
2111 var_one, var_two, var_three,
2112 var_four):
2113 print (var_one)
2115 (python-tests-look-at "def long_function_name")
2116 (should (python-info-beginning-of-block-p))
2117 (python-tests-look-at "var_one, var_two, var_three,")
2118 (should (not (python-info-beginning-of-block-p)))
2119 (python-tests-look-at "print (var_one)")
2120 (should (not (python-info-beginning-of-block-p)))))
2122 (ert-deftest python-info-beginning-of-block-p-2 ()
2123 (python-tests-with-temp-buffer
2125 if width == 0 and height == 0 and \\\\
2126 color == 'red' and emphasis == 'strong' or \\\\
2127 highlight > 100:
2128 raise ValueError(
2129 'sorry, you lose'
2133 (python-tests-look-at "if width == 0 and")
2134 (should (python-info-beginning-of-block-p))
2135 (python-tests-look-at "color == 'red' and emphasis")
2136 (should (not (python-info-beginning-of-block-p)))
2137 (python-tests-look-at "raise ValueError(")
2138 (should (not (python-info-beginning-of-block-p)))))
2140 (ert-deftest python-info-end-of-block-p-1 ()
2141 (python-tests-with-temp-buffer
2143 def long_function_name(
2144 var_one, var_two, var_three,
2145 var_four):
2146 print (var_one)
2148 (python-tests-look-at "def long_function_name")
2149 (should (not (python-info-end-of-block-p)))
2150 (python-tests-look-at "var_one, var_two, var_three,")
2151 (should (not (python-info-end-of-block-p)))
2152 (python-tests-look-at "var_four):")
2153 (end-of-line)
2154 (should (not (python-info-end-of-block-p)))
2155 (python-tests-look-at "print (var_one)")
2156 (should (not (python-info-end-of-block-p)))
2157 (end-of-line 1)
2158 (should (python-info-end-of-block-p))))
2160 (ert-deftest python-info-end-of-block-p-2 ()
2161 (python-tests-with-temp-buffer
2163 if width == 0 and height == 0 and \\\\
2164 color == 'red' and emphasis == 'strong' or \\\\
2165 highlight > 100:
2166 raise ValueError(
2167 'sorry, you lose'
2171 (python-tests-look-at "if width == 0 and")
2172 (should (not (python-info-end-of-block-p)))
2173 (python-tests-look-at "color == 'red' and emphasis == 'strong' or")
2174 (should (not (python-info-end-of-block-p)))
2175 (python-tests-look-at "highlight > 100:")
2176 (end-of-line)
2177 (should (not (python-info-end-of-block-p)))
2178 (python-tests-look-at "raise ValueError(")
2179 (should (not (python-info-end-of-block-p)))
2180 (end-of-line 1)
2181 (should (not (python-info-end-of-block-p)))
2182 (goto-char (point-max))
2183 (python-util-forward-comment -1)
2184 (should (python-info-end-of-block-p))))
2186 (ert-deftest python-info-closing-block-1 ()
2187 (python-tests-with-temp-buffer
2189 if request.user.is_authenticated():
2190 try:
2191 profile = request.user.get_profile()
2192 except Profile.DoesNotExist:
2193 profile = Profile.objects.create(user=request.user)
2194 else:
2195 if profile.stats:
2196 profile.recalculate_stats()
2197 else:
2198 profile.clear_stats()
2199 finally:
2200 profile.views += 1
2201 profile.save()
2203 (python-tests-look-at "try:")
2204 (should (not (python-info-closing-block)))
2205 (python-tests-look-at "except Profile.DoesNotExist:")
2206 (should (= (python-tests-look-at "try:" -1 t)
2207 (python-info-closing-block)))
2208 (python-tests-look-at "else:")
2209 (should (= (python-tests-look-at "except Profile.DoesNotExist:" -1 t)
2210 (python-info-closing-block)))
2211 (python-tests-look-at "if profile.stats:")
2212 (should (not (python-info-closing-block)))
2213 (python-tests-look-at "else:")
2214 (should (= (python-tests-look-at "if profile.stats:" -1 t)
2215 (python-info-closing-block)))
2216 (python-tests-look-at "finally:")
2217 (should (= (python-tests-look-at "else:" -2 t)
2218 (python-info-closing-block)))))
2220 (ert-deftest python-info-closing-block-2 ()
2221 (python-tests-with-temp-buffer
2223 if request.user.is_authenticated():
2224 profile = Profile.objects.get_or_create(user=request.user)
2225 if profile.stats:
2226 profile.recalculate_stats()
2228 data = {
2229 'else': 'do it'
2231 'else'
2233 (python-tests-look-at "'else': 'do it'")
2234 (should (not (python-info-closing-block)))
2235 (python-tests-look-at "'else'")
2236 (should (not (python-info-closing-block)))))
2238 (ert-deftest python-info-line-ends-backslash-p-1 ()
2239 (python-tests-with-temp-buffer
2241 objects = Thing.objects.all() \\\\
2242 .filter(
2243 type='toy',
2244 status='bought'
2245 ) \\\\
2246 .aggregate(
2247 Sum('amount')
2248 ) \\\\
2249 .values_list()
2251 (should (python-info-line-ends-backslash-p 2)) ; .filter(...
2252 (should (python-info-line-ends-backslash-p 3))
2253 (should (python-info-line-ends-backslash-p 4))
2254 (should (python-info-line-ends-backslash-p 5))
2255 (should (python-info-line-ends-backslash-p 6)) ; ) \...
2256 (should (python-info-line-ends-backslash-p 7))
2257 (should (python-info-line-ends-backslash-p 8))
2258 (should (python-info-line-ends-backslash-p 9))
2259 (should (not (python-info-line-ends-backslash-p 10))))) ; .values_list()...
2261 (ert-deftest python-info-beginning-of-backslash-1 ()
2262 (python-tests-with-temp-buffer
2264 objects = Thing.objects.all() \\\\
2265 .filter(
2266 type='toy',
2267 status='bought'
2268 ) \\\\
2269 .aggregate(
2270 Sum('amount')
2271 ) \\\\
2272 .values_list()
2274 (let ((first 2)
2275 (second (python-tests-look-at ".filter("))
2276 (third (python-tests-look-at ".aggregate(")))
2277 (should (= first (python-info-beginning-of-backslash 2)))
2278 (should (= second (python-info-beginning-of-backslash 3)))
2279 (should (= second (python-info-beginning-of-backslash 4)))
2280 (should (= second (python-info-beginning-of-backslash 5)))
2281 (should (= second (python-info-beginning-of-backslash 6)))
2282 (should (= third (python-info-beginning-of-backslash 7)))
2283 (should (= third (python-info-beginning-of-backslash 8)))
2284 (should (= third (python-info-beginning-of-backslash 9)))
2285 (should (not (python-info-beginning-of-backslash 10))))))
2287 (ert-deftest python-info-continuation-line-p-1 ()
2288 (python-tests-with-temp-buffer
2290 if width == 0 and height == 0 and \\\\
2291 color == 'red' and emphasis == 'strong' or \\\\
2292 highlight > 100:
2293 raise ValueError(
2294 'sorry, you lose'
2298 (python-tests-look-at "if width == 0 and height == 0 and")
2299 (should (not (python-info-continuation-line-p)))
2300 (python-tests-look-at "color == 'red' and emphasis == 'strong' or")
2301 (should (python-info-continuation-line-p))
2302 (python-tests-look-at "highlight > 100:")
2303 (should (python-info-continuation-line-p))
2304 (python-tests-look-at "raise ValueError(")
2305 (should (not (python-info-continuation-line-p)))
2306 (python-tests-look-at "'sorry, you lose'")
2307 (should (python-info-continuation-line-p))
2308 (forward-line 1)
2309 (should (python-info-continuation-line-p))
2310 (python-tests-look-at ")")
2311 (should (python-info-continuation-line-p))
2312 (forward-line 1)
2313 (should (not (python-info-continuation-line-p)))))
2315 (ert-deftest python-info-block-continuation-line-p-1 ()
2316 (python-tests-with-temp-buffer
2318 if width == 0 and height == 0 and \\\\
2319 color == 'red' and emphasis == 'strong' or \\\\
2320 highlight > 100:
2321 raise ValueError(
2322 'sorry, you lose'
2326 (python-tests-look-at "if width == 0 and")
2327 (should (not (python-info-block-continuation-line-p)))
2328 (python-tests-look-at "color == 'red' and emphasis == 'strong' or")
2329 (should (= (python-info-block-continuation-line-p)
2330 (python-tests-look-at "if width == 0 and" -1 t)))
2331 (python-tests-look-at "highlight > 100:")
2332 (should (not (python-info-block-continuation-line-p)))))
2334 (ert-deftest python-info-block-continuation-line-p-2 ()
2335 (python-tests-with-temp-buffer
2337 def foo(a,
2340 pass
2342 (python-tests-look-at "def foo(a,")
2343 (should (not (python-info-block-continuation-line-p)))
2344 (python-tests-look-at "b,")
2345 (should (= (python-info-block-continuation-line-p)
2346 (python-tests-look-at "def foo(a," -1 t)))
2347 (python-tests-look-at "c):")
2348 (should (not (python-info-block-continuation-line-p)))))
2350 (ert-deftest python-info-assignment-continuation-line-p-1 ()
2351 (python-tests-with-temp-buffer
2353 data = foo(), bar() \\\\
2354 baz(), 4 \\\\
2355 5, 6
2357 (python-tests-look-at "data = foo(), bar()")
2358 (should (not (python-info-assignment-continuation-line-p)))
2359 (python-tests-look-at "baz(), 4")
2360 (should (= (python-info-assignment-continuation-line-p)
2361 (python-tests-look-at "foo()," -1 t)))
2362 (python-tests-look-at "5, 6")
2363 (should (not (python-info-assignment-continuation-line-p)))))
2365 (ert-deftest python-info-assignment-continuation-line-p-2 ()
2366 (python-tests-with-temp-buffer
2368 data = (foo(), bar()
2369 baz(), 4
2370 5, 6)
2372 (python-tests-look-at "data = (foo(), bar()")
2373 (should (not (python-info-assignment-continuation-line-p)))
2374 (python-tests-look-at "baz(), 4")
2375 (should (= (python-info-assignment-continuation-line-p)
2376 (python-tests-look-at "(foo()," -1 t)))
2377 (python-tests-look-at "5, 6)")
2378 (should (not (python-info-assignment-continuation-line-p)))))
2380 (ert-deftest python-info-looking-at-beginning-of-defun-1 ()
2381 (python-tests-with-temp-buffer
2383 def decorat0r(deff):
2384 '''decorates stuff.
2386 @decorat0r
2387 def foo(arg):
2390 def wrap():
2391 deff()
2392 return wwrap
2394 (python-tests-look-at "def decorat0r(deff):")
2395 (should (python-info-looking-at-beginning-of-defun))
2396 (python-tests-look-at "def foo(arg):")
2397 (should (not (python-info-looking-at-beginning-of-defun)))
2398 (python-tests-look-at "def wrap():")
2399 (should (python-info-looking-at-beginning-of-defun))
2400 (python-tests-look-at "deff()")
2401 (should (not (python-info-looking-at-beginning-of-defun)))))
2403 (ert-deftest python-info-current-line-comment-p-1 ()
2404 (python-tests-with-temp-buffer
2406 # this is a comment
2407 foo = True # another comment
2408 '#this is a string'
2409 if foo:
2410 # more comments
2411 print ('bar') # print bar
2413 (python-tests-look-at "# this is a comment")
2414 (should (python-info-current-line-comment-p))
2415 (python-tests-look-at "foo = True # another comment")
2416 (should (not (python-info-current-line-comment-p)))
2417 (python-tests-look-at "'#this is a string'")
2418 (should (not (python-info-current-line-comment-p)))
2419 (python-tests-look-at "# more comments")
2420 (should (python-info-current-line-comment-p))
2421 (python-tests-look-at "print ('bar') # print bar")
2422 (should (not (python-info-current-line-comment-p)))))
2424 (ert-deftest python-info-current-line-empty-p ()
2425 (python-tests-with-temp-buffer
2427 # this is a comment
2429 foo = True # another comment
2431 (should (python-info-current-line-empty-p))
2432 (python-tests-look-at "# this is a comment")
2433 (should (not (python-info-current-line-empty-p)))
2434 (forward-line 1)
2435 (should (python-info-current-line-empty-p))))
2438 ;;; Utility functions
2440 (ert-deftest python-util-goto-line-1 ()
2441 (python-tests-with-temp-buffer
2442 (concat
2443 "# a comment
2444 # another comment
2445 def foo(a, b, c):
2446 pass" (make-string 20 ?\n))
2447 (python-util-goto-line 10)
2448 (should (= (line-number-at-pos) 10))
2449 (python-util-goto-line 20)
2450 (should (= (line-number-at-pos) 20))))
2452 (ert-deftest python-util-clone-local-variables-1 ()
2453 (let ((buffer (generate-new-buffer
2454 "python-util-clone-local-variables-1"))
2455 (varcons
2456 '((python-fill-docstring-style . django)
2457 (python-shell-interpreter . "python")
2458 (python-shell-interpreter-args . "manage.py shell")
2459 (python-shell-prompt-regexp . "In \\[[0-9]+\\]: ")
2460 (python-shell-prompt-output-regexp . "Out\\[[0-9]+\\]: ")
2461 (python-shell-extra-pythonpaths "/home/user/pylib/")
2462 (python-shell-completion-setup-code
2463 . "from IPython.core.completerlib import module_completion")
2464 (python-shell-completion-module-string-code
2465 . "';'.join(module_completion('''%s'''))\n")
2466 (python-shell-completion-string-code
2467 . "';'.join(get_ipython().Completer.all_completions('''%s'''))\n")
2468 (python-shell-virtualenv-path
2469 . "/home/user/.virtualenvs/project"))))
2470 (with-current-buffer buffer
2471 (kill-all-local-variables)
2472 (dolist (ccons varcons)
2473 (set (make-local-variable (car ccons)) (cdr ccons))))
2474 (python-tests-with-temp-buffer
2476 (python-util-clone-local-variables buffer)
2477 (dolist (ccons varcons)
2478 (should
2479 (equal (symbol-value (car ccons)) (cdr ccons)))))
2480 (kill-buffer buffer)))
2482 (ert-deftest python-util-forward-comment-1 ()
2483 (python-tests-with-temp-buffer
2484 (concat
2485 "# a comment
2486 # another comment
2487 # bad indented comment
2488 # more comments" (make-string 9999 ?\n))
2489 (python-util-forward-comment 1)
2490 (should (= (point) (point-max)))
2491 (python-util-forward-comment -1)
2492 (should (= (point) (point-min)))))
2495 (provide 'python-tests)
2497 ;; Local Variables:
2498 ;; coding: utf-8
2499 ;; indent-tabs-mode: nil
2500 ;; End:
2502 ;;; python-tests.el ends here