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