]> code.delx.au - gnu-emacs/blob - test/automated/python-tests.el
Merge from trunk.
[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 honouring."
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
678 (ert-deftest python-nav-beginning-of-statement-1 ()
679 (python-tests-with-temp-buffer
680 "
681 v1 = 123 + \
682 456 + \
683 789
684 v2 = (value1,
685 value2,
686
687 value3,
688 value4)
689 v3 = ('this is a string'
690
691 'that is continued'
692 'between lines'
693 'within a paren',
694 # this is a comment, yo
695 'continue previous line')
696 v4 = '''
697 a very long
698 string
699 '''
700 "
701 (python-tests-look-at "v2 =")
702 (python-util-forward-comment -1)
703 (should (= (save-excursion
704 (python-nav-beginning-of-statement)
705 (point))
706 (python-tests-look-at "v1 =" -1 t)))
707 (python-tests-look-at "v3 =")
708 (python-util-forward-comment -1)
709 (should (= (save-excursion
710 (python-nav-beginning-of-statement)
711 (point))
712 (python-tests-look-at "v2 =" -1 t)))
713 (python-tests-look-at "v4 =")
714 (python-util-forward-comment -1)
715 (should (= (save-excursion
716 (python-nav-beginning-of-statement)
717 (point))
718 (python-tests-look-at "v3 =" -1 t)))
719 (goto-char (point-max))
720 (python-util-forward-comment -1)
721 (should (= (save-excursion
722 (python-nav-beginning-of-statement)
723 (point))
724 (python-tests-look-at "v4 =" -1 t)))))
725
726 (ert-deftest python-nav-end-of-statement-1 ()
727 (python-tests-with-temp-buffer
728 "
729 v1 = 123 + \
730 456 + \
731 789
732 v2 = (value1,
733 value2,
734
735 value3,
736 value4)
737 v3 = ('this is a string'
738
739 'that is continued'
740 'between lines'
741 'within a paren',
742 # this is a comment, yo
743 'continue previous line')
744 v4 = '''
745 a very long
746 string
747 '''
748 "
749 (python-tests-look-at "v1 =")
750 (should (= (save-excursion
751 (python-nav-end-of-statement)
752 (point))
753 (save-excursion
754 (python-tests-look-at "789")
755 (line-end-position))))
756 (python-tests-look-at "v2 =")
757 (should (= (save-excursion
758 (python-nav-end-of-statement)
759 (point))
760 (save-excursion
761 (python-tests-look-at "value4)")
762 (line-end-position))))
763 (python-tests-look-at "v3 =")
764 (should (= (save-excursion
765 (python-nav-end-of-statement)
766 (point))
767 (save-excursion
768 (python-tests-look-at
769 "'continue previous line')")
770 (line-end-position))))
771 (python-tests-look-at "v4 =")
772 (should (= (save-excursion
773 (python-nav-end-of-statement)
774 (point))
775 (save-excursion
776 (goto-char (point-max))
777 (python-util-forward-comment -1)
778 (point))))))
779
780 (ert-deftest python-nav-forward-statement-1 ()
781 (python-tests-with-temp-buffer
782 "
783 v1 = 123 + \
784 456 + \
785 789
786 v2 = (value1,
787 value2,
788
789 value3,
790 value4)
791 v3 = ('this is a string'
792
793 'that is continued'
794 'between lines'
795 'within a paren',
796 # this is a comment, yo
797 'continue previous line')
798 v4 = '''
799 a very long
800 string
801 '''
802 "
803 (python-tests-look-at "v1 =")
804 (should (= (save-excursion
805 (python-nav-forward-statement)
806 (point))
807 (python-tests-look-at "v2 =")))
808 (should (= (save-excursion
809 (python-nav-forward-statement)
810 (point))
811 (python-tests-look-at "v3 =")))
812 (should (= (save-excursion
813 (python-nav-forward-statement)
814 (point))
815 (python-tests-look-at "v4 =")))
816 (should (= (save-excursion
817 (python-nav-forward-statement)
818 (point))
819 (point-max)))))
820
821 (ert-deftest python-nav-backward-statement-1 ()
822 (python-tests-with-temp-buffer
823 "
824 v1 = 123 + \
825 456 + \
826 789
827 v2 = (value1,
828 value2,
829
830 value3,
831 value4)
832 v3 = ('this is a string'
833
834 'that is continued'
835 'between lines'
836 'within a paren',
837 # this is a comment, yo
838 'continue previous line')
839 v4 = '''
840 a very long
841 string
842 '''
843 "
844 (goto-char (point-max))
845 (should (= (save-excursion
846 (python-nav-backward-statement)
847 (point))
848 (python-tests-look-at "v4 =" -1)))
849 (should (= (save-excursion
850 (python-nav-backward-statement)
851 (point))
852 (python-tests-look-at "v3 =" -1)))
853 (should (= (save-excursion
854 (python-nav-backward-statement)
855 (point))
856 (python-tests-look-at "v2 =" -1)))
857 (should (= (save-excursion
858 (python-nav-backward-statement)
859 (point))
860 (python-tests-look-at "v1 =" -1)))))
861
862 (ert-deftest python-nav-backward-statement-2 ()
863 :expected-result :failed
864 (python-tests-with-temp-buffer
865 "
866 v1 = 123 + \
867 456 + \
868 789
869 v2 = (value1,
870 value2,
871
872 value3,
873 value4)
874 "
875 ;; FIXME: For some reason `python-nav-backward-statement' is moving
876 ;; back two sentences when starting from 'value4)'.
877 (goto-char (point-max))
878 (python-util-forward-comment -1)
879 (should (= (save-excursion
880 (python-nav-backward-statement)
881 (point))
882 (python-tests-look-at "v2 =" -1 t)))))
883
884 (ert-deftest python-nav-beginning-of-block-1 ()
885 (python-tests-with-temp-buffer
886 "
887 def decoratorFunctionWithArguments(arg1, arg2, arg3):
888 '''print decorated function call data to stdout.
889
890 Usage:
891
892 @decoratorFunctionWithArguments('arg1', 'arg2')
893 def func(a, b, c=True):
894 pass
895 '''
896
897 def wwrap(f):
898 print 'Inside wwrap()'
899 def wrapped_f(*args):
900 print 'Inside wrapped_f()'
901 print 'Decorator arguments:', arg1, arg2, arg3
902 f(*args)
903 print 'After f(*args)'
904 return wrapped_f
905 return wwrap
906 "
907 (python-tests-look-at "return wwrap")
908 (should (= (save-excursion
909 (python-nav-beginning-of-block)
910 (point))
911 (python-tests-look-at "def decoratorFunctionWithArguments" -1)))
912 (python-tests-look-at "print 'Inside wwrap()'")
913 (should (= (save-excursion
914 (python-nav-beginning-of-block)
915 (point))
916 (python-tests-look-at "def wwrap(f):" -1)))
917 (python-tests-look-at "print 'After f(*args)'")
918 (end-of-line)
919 (should (= (save-excursion
920 (python-nav-beginning-of-block)
921 (point))
922 (python-tests-look-at "def wrapped_f(*args):" -1)))
923 (python-tests-look-at "return wrapped_f")
924 (should (= (save-excursion
925 (python-nav-beginning-of-block)
926 (point))
927 (python-tests-look-at "def wwrap(f):" -1)))))
928
929 (ert-deftest python-nav-end-of-block-1 ()
930 (python-tests-with-temp-buffer
931 "
932 def decoratorFunctionWithArguments(arg1, arg2, arg3):
933 '''print decorated function call data to stdout.
934
935 Usage:
936
937 @decoratorFunctionWithArguments('arg1', 'arg2')
938 def func(a, b, c=True):
939 pass
940 '''
941
942 def wwrap(f):
943 print 'Inside wwrap()'
944 def wrapped_f(*args):
945 print 'Inside wrapped_f()'
946 print 'Decorator arguments:', arg1, arg2, arg3
947 f(*args)
948 print 'After f(*args)'
949 return wrapped_f
950 return wwrap
951 "
952 (python-tests-look-at "def decoratorFunctionWithArguments")
953 (should (= (save-excursion
954 (python-nav-end-of-block)
955 (point))
956 (save-excursion
957 (goto-char (point-max))
958 (python-util-forward-comment -1)
959 (point))))
960 (python-tests-look-at "def wwrap(f):")
961 (should (= (save-excursion
962 (python-nav-end-of-block)
963 (point))
964 (save-excursion
965 (python-tests-look-at "return wrapped_f")
966 (line-end-position))))
967 (end-of-line)
968 (should (= (save-excursion
969 (python-nav-end-of-block)
970 (point))
971 (save-excursion
972 (python-tests-look-at "return wrapped_f")
973 (line-end-position))))
974 (python-tests-look-at "f(*args)")
975 (should (= (save-excursion
976 (python-nav-end-of-block)
977 (point))
978 (save-excursion
979 (python-tests-look-at "print 'After f(*args)'")
980 (line-end-position))))))
981
982 (ert-deftest python-nav-forward-block-1 ()
983 "This also accounts as a test for `python-nav-backward-block'."
984 (python-tests-with-temp-buffer
985 "
986 if request.user.is_authenticated():
987 # def block():
988 # pass
989 try:
990 profile = request.user.get_profile()
991 except Profile.DoesNotExist:
992 profile = Profile.objects.create(user=request.user)
993 else:
994 if profile.stats:
995 profile.recalculate_stats()
996 else:
997 profile.clear_stats()
998 finally:
999 profile.views += 1
1000 profile.save()
1001 "
1002 (should (= (save-excursion (python-nav-forward-block))
1003 (python-tests-look-at "if request.user.is_authenticated():")))
1004 (should (= (save-excursion (python-nav-forward-block))
1005 (python-tests-look-at "try:")))
1006 (should (= (save-excursion (python-nav-forward-block))
1007 (python-tests-look-at "except Profile.DoesNotExist:")))
1008 (should (= (save-excursion (python-nav-forward-block))
1009 (python-tests-look-at "else:")))
1010 (should (= (save-excursion (python-nav-forward-block))
1011 (python-tests-look-at "if profile.stats:")))
1012 (should (= (save-excursion (python-nav-forward-block))
1013 (python-tests-look-at "else:")))
1014 (should (= (save-excursion (python-nav-forward-block))
1015 (python-tests-look-at "finally:")))
1016 ;; When point is at the last block, leave it there and return nil
1017 (should (not (save-excursion (python-nav-forward-block))))
1018 ;; Move backwards, and even if the number of moves is less than the
1019 ;; provided argument return the point.
1020 (should (= (save-excursion (python-nav-forward-block -10))
1021 (python-tests-look-at
1022 "if request.user.is_authenticated():" -1)))))
1023
1024 (ert-deftest python-nav-lisp-forward-sexp-safe-1 ()
1025 (python-tests-with-temp-buffer
1026 "
1027 profile = Profile.objects.create(user=request.user)
1028 profile.notify()
1029 "
1030 (python-tests-look-at "profile =")
1031 (python-nav-lisp-forward-sexp-safe 4)
1032 (should (looking-at "(user=request.user)"))
1033 (python-tests-look-at "user=request.user")
1034 (python-nav-lisp-forward-sexp-safe -1)
1035 (should (looking-at "(user=request.user)"))
1036 (python-nav-lisp-forward-sexp-safe -4)
1037 (should (looking-at "profile ="))
1038 (python-tests-look-at "user=request.user")
1039 (python-nav-lisp-forward-sexp-safe 3)
1040 (should (looking-at ")"))
1041 (python-nav-lisp-forward-sexp-safe 1)
1042 (should (looking-at "$"))
1043 (python-nav-lisp-forward-sexp-safe 1)
1044 (should (looking-at ".notify()"))))
1045
1046 (ert-deftest python-nav-forward-sexp-1 ()
1047 (python-tests-with-temp-buffer
1048 "
1049 a()
1050 b()
1051 c()
1052 "
1053 (python-tests-look-at "a()")
1054 (python-nav-forward-sexp)
1055 (should (looking-at "$"))
1056 (should (save-excursion
1057 (beginning-of-line)
1058 (looking-at "a()")))
1059 (python-nav-forward-sexp)
1060 (should (looking-at "$"))
1061 (should (save-excursion
1062 (beginning-of-line)
1063 (looking-at "b()")))
1064 (python-nav-forward-sexp)
1065 (should (looking-at "$"))
1066 (should (save-excursion
1067 (beginning-of-line)
1068 (looking-at "c()")))
1069 ;; Movement next to a paren should do what lisp does and
1070 ;; unfortunately It can't change, because otherwise
1071 ;; `blink-matching-open' breaks.
1072 (python-nav-forward-sexp -1)
1073 (should (looking-at "()"))
1074 (should (save-excursion
1075 (beginning-of-line)
1076 (looking-at "c()")))
1077 (python-nav-forward-sexp -1)
1078 (should (looking-at "c()"))
1079 (python-nav-forward-sexp -1)
1080 (should (looking-at "b()"))
1081 (python-nav-forward-sexp -1)
1082 (should (looking-at "a()"))))
1083
1084 (ert-deftest python-nav-forward-sexp-2 ()
1085 (python-tests-with-temp-buffer
1086 "
1087 def func():
1088 if True:
1089 aaa = bbb
1090 ccc = ddd
1091 eee = fff
1092 return ggg
1093 "
1094 (python-tests-look-at "aa =")
1095 (python-nav-forward-sexp)
1096 (should (looking-at " = bbb"))
1097 (python-nav-forward-sexp)
1098 (should (looking-at "$"))
1099 (should (save-excursion
1100 (back-to-indentation)
1101 (looking-at "aaa = bbb")))
1102 (python-nav-forward-sexp)
1103 (should (looking-at "$"))
1104 (should (save-excursion
1105 (back-to-indentation)
1106 (looking-at "ccc = ddd")))
1107 (python-nav-forward-sexp)
1108 (should (looking-at "$"))
1109 (should (save-excursion
1110 (back-to-indentation)
1111 (looking-at "eee = fff")))
1112 (python-nav-forward-sexp)
1113 (should (looking-at "$"))
1114 (should (save-excursion
1115 (back-to-indentation)
1116 (looking-at "return ggg")))
1117 (python-nav-forward-sexp -1)
1118 (should (looking-at "def func():"))))
1119
1120 (ert-deftest python-nav-forward-sexp-3 ()
1121 (python-tests-with-temp-buffer
1122 "
1123 from some_module import some_sub_module
1124 from another_module import another_sub_module
1125
1126 def another_statement():
1127 pass
1128 "
1129 (python-tests-look-at "some_module")
1130 (python-nav-forward-sexp)
1131 (should (looking-at " import"))
1132 (python-nav-forward-sexp)
1133 (should (looking-at " some_sub_module"))
1134 (python-nav-forward-sexp)
1135 (should (looking-at "$"))
1136 (should
1137 (save-excursion
1138 (back-to-indentation)
1139 (looking-at
1140 "from some_module import some_sub_module")))
1141 (python-nav-forward-sexp)
1142 (should (looking-at "$"))
1143 (should
1144 (save-excursion
1145 (back-to-indentation)
1146 (looking-at
1147 "from another_module import another_sub_module")))
1148 (python-nav-forward-sexp)
1149 (should (looking-at "$"))
1150 (should
1151 (save-excursion
1152 (back-to-indentation)
1153 (looking-at
1154 "pass")))
1155 (python-nav-forward-sexp -1)
1156 (should (looking-at "def another_statement():"))
1157 (python-nav-forward-sexp -1)
1158 (should (looking-at "from another_module import another_sub_module"))
1159 (python-nav-forward-sexp -1)
1160 (should (looking-at "from some_module import some_sub_module"))))
1161
1162 (ert-deftest python-nav-up-list-1 ()
1163 (python-tests-with-temp-buffer
1164 "
1165 def f():
1166 if True:
1167 return [i for i in range(3)]
1168 "
1169 (python-tests-look-at "3)]")
1170 (python-nav-up-list)
1171 (should (looking-at "]"))
1172 (python-nav-up-list)
1173 (should (looking-at "$"))))
1174
1175 (ert-deftest python-nav-backward-up-list-1 ()
1176 :expected-result :failed
1177 (python-tests-with-temp-buffer
1178 "
1179 def f():
1180 if True:
1181 return [i for i in range(3)]
1182 "
1183 (python-tests-look-at "3)]")
1184 (python-nav-backward-up-list)
1185 (should (looking-at "(3)\\]"))
1186 (python-nav-backward-up-list)
1187 (should (looking-at
1188 "\\[i for i in range(3)\\]"))
1189 ;; FIXME: Need to move to beginning-of-statement.
1190 (python-nav-backward-up-list)
1191 (should (looking-at
1192 "return \\[i for i in range(3)\\]"))
1193 (python-nav-backward-up-list)
1194 (should (looking-at "if True:"))
1195 (python-nav-backward-up-list)
1196 (should (looking-at "def f():"))))
1197
1198 \f
1199 ;;; Shell integration
1200
1201 (defvar python-tests-shell-interpreter "python")
1202
1203 (ert-deftest python-shell-get-process-name-1 ()
1204 "Check process name calculation on different scenarios."
1205 (python-tests-with-temp-buffer
1206 ""
1207 (should (string= (python-shell-get-process-name nil)
1208 python-shell-buffer-name))
1209 ;; When the `current-buffer' doesn't have `buffer-file-name', even
1210 ;; if dedicated flag is non-nil should not include its name.
1211 (should (string= (python-shell-get-process-name t)
1212 python-shell-buffer-name)))
1213 (python-tests-with-temp-file
1214 ""
1215 ;; `buffer-file-name' is non-nil but the dedicated flag is nil and
1216 ;; should be respected.
1217 (should (string= (python-shell-get-process-name nil)
1218 python-shell-buffer-name))
1219 (should (string=
1220 (python-shell-get-process-name t)
1221 (format "%s[%s]" python-shell-buffer-name buffer-file-name)))))
1222
1223 (ert-deftest python-shell-internal-get-process-name-1 ()
1224 "Check the internal process name is config-unique."
1225 (let* ((python-shell-interpreter python-tests-shell-interpreter)
1226 (python-shell-interpreter-args "")
1227 (python-shell-prompt-regexp ">>> ")
1228 (python-shell-prompt-block-regexp "[.][.][.] ")
1229 (python-shell-setup-codes "")
1230 (python-shell-process-environment "")
1231 (python-shell-extra-pythonpaths "")
1232 (python-shell-exec-path "")
1233 (python-shell-virtualenv-path "")
1234 (expected (python-tests-with-temp-buffer
1235 "" (python-shell-internal-get-process-name))))
1236 ;; Same configurations should match.
1237 (should
1238 (string= expected
1239 (python-tests-with-temp-buffer
1240 "" (python-shell-internal-get-process-name))))
1241 (let ((python-shell-interpreter-args "-B"))
1242 ;; A minimal change should generate different names.
1243 (should
1244 (not (string=
1245 expected
1246 (python-tests-with-temp-buffer
1247 "" (python-shell-internal-get-process-name))))))))
1248
1249 (ert-deftest python-shell-parse-command-1 ()
1250 "Check the command to execute is calculated correctly.
1251 Using `python-shell-interpreter' and
1252 `python-shell-interpreter-args'."
1253 :expected-result (if (executable-find python-tests-shell-interpreter)
1254 :passed
1255 :failed)
1256 (let ((python-shell-interpreter (executable-find
1257 python-tests-shell-interpreter))
1258 (python-shell-interpreter-args "-B"))
1259 (should (string=
1260 (format "%s %s"
1261 python-shell-interpreter
1262 python-shell-interpreter-args)
1263 (python-shell-parse-command)))))
1264
1265 (ert-deftest python-shell-calculate-process-environment-1 ()
1266 "Test `python-shell-process-environment' modification."
1267 (let* ((original-process-environment process-environment)
1268 (python-shell-process-environment
1269 '("TESTVAR1=value1" "TESTVAR2=value2"))
1270 (process-environment
1271 (python-shell-calculate-process-environment)))
1272 (should (equal (getenv "TESTVAR1") "value1"))
1273 (should (equal (getenv "TESTVAR2") "value2"))))
1274
1275 (ert-deftest python-shell-calculate-process-environment-2 ()
1276 "Test `python-shell-extra-pythonpaths' modification."
1277 (let* ((original-process-environment process-environment)
1278 (original-pythonpath (getenv "PYTHONPATH"))
1279 (paths '("path1" "path2"))
1280 (python-shell-extra-pythonpaths paths)
1281 (process-environment
1282 (python-shell-calculate-process-environment)))
1283 (should (equal (getenv "PYTHONPATH")
1284 (concat
1285 (mapconcat 'identity paths path-separator)
1286 path-separator original-pythonpath)))))
1287
1288 (ert-deftest python-shell-calculate-process-environment-3 ()
1289 "Test `python-shell-virtualenv-path' modification."
1290 (let* ((original-process-environment process-environment)
1291 (original-path (or (getenv "PATH") ""))
1292 (python-shell-virtualenv-path
1293 (directory-file-name user-emacs-directory))
1294 (process-environment
1295 (python-shell-calculate-process-environment)))
1296 (should (not (getenv "PYTHONHOME")))
1297 (should (string= (getenv "VIRTUAL_ENV") python-shell-virtualenv-path))
1298 (should (equal (getenv "PATH")
1299 (format "%s/bin%s%s"
1300 python-shell-virtualenv-path
1301 path-separator original-path)))))
1302
1303 (ert-deftest python-shell-calculate-exec-path-1 ()
1304 "Test `python-shell-exec-path' modification."
1305 (let* ((original-exec-path exec-path)
1306 (python-shell-exec-path '("path1" "path2"))
1307 (exec-path (python-shell-calculate-exec-path)))
1308 (should (equal
1309 exec-path
1310 (append python-shell-exec-path
1311 original-exec-path)))))
1312
1313 (ert-deftest python-shell-calculate-exec-path-2 ()
1314 "Test `python-shell-exec-path' modification."
1315 (let* ((original-exec-path exec-path)
1316 (python-shell-virtualenv-path
1317 (directory-file-name user-emacs-directory))
1318 (exec-path (python-shell-calculate-exec-path)))
1319 (should (equal
1320 exec-path
1321 (append (cons
1322 (format "%s/bin" python-shell-virtualenv-path)
1323 original-exec-path))))))
1324
1325 (ert-deftest python-shell-make-comint-1 ()
1326 "Check comint creation for global shell buffer."
1327 :expected-result (if (executable-find python-tests-shell-interpreter)
1328 :passed
1329 :failed)
1330 (let* ((python-shell-interpreter
1331 (executable-find python-tests-shell-interpreter))
1332 (proc-name (python-shell-get-process-name nil))
1333 (shell-buffer
1334 (python-tests-with-temp-buffer
1335 "" (python-shell-make-comint
1336 (python-shell-parse-command) proc-name)))
1337 (process (get-buffer-process shell-buffer)))
1338 (unwind-protect
1339 (progn
1340 (set-process-query-on-exit-flag process nil)
1341 (should (process-live-p process))
1342 (with-current-buffer shell-buffer
1343 (should (eq major-mode 'inferior-python-mode))
1344 (should (string= (buffer-name) (format "*%s*" proc-name)))))
1345 (kill-buffer shell-buffer))))
1346
1347 (ert-deftest python-shell-make-comint-2 ()
1348 "Check comint creation for internal shell buffer."
1349 :expected-result (if (executable-find python-tests-shell-interpreter)
1350 :passed
1351 :failed)
1352 (let* ((python-shell-interpreter
1353 (executable-find python-tests-shell-interpreter))
1354 (proc-name (python-shell-internal-get-process-name))
1355 (shell-buffer
1356 (python-tests-with-temp-buffer
1357 "" (python-shell-make-comint
1358 (python-shell-parse-command) proc-name nil t)))
1359 (process (get-buffer-process shell-buffer)))
1360 (unwind-protect
1361 (progn
1362 (set-process-query-on-exit-flag process nil)
1363 (should (process-live-p process))
1364 (with-current-buffer shell-buffer
1365 (should (eq major-mode 'inferior-python-mode))
1366 (should (string= (buffer-name) (format " *%s*" proc-name)))))
1367 (kill-buffer shell-buffer))))
1368
1369 (ert-deftest python-shell-get-process-1 ()
1370 "Check dedicated shell process preference over global."
1371 :expected-result (if (executable-find python-tests-shell-interpreter)
1372 :passed
1373 :failed)
1374 (python-tests-with-temp-file
1375 ""
1376 (let* ((python-shell-interpreter
1377 (executable-find python-tests-shell-interpreter))
1378 (global-proc-name (python-shell-get-process-name nil))
1379 (dedicated-proc-name (python-shell-get-process-name t))
1380 (global-shell-buffer
1381 (python-shell-make-comint
1382 (python-shell-parse-command) global-proc-name))
1383 (dedicated-shell-buffer
1384 (python-shell-make-comint
1385 (python-shell-parse-command) dedicated-proc-name))
1386 (global-process (get-buffer-process global-shell-buffer))
1387 (dedicated-process (get-buffer-process dedicated-shell-buffer)))
1388 (unwind-protect
1389 (progn
1390 (set-process-query-on-exit-flag global-process nil)
1391 (set-process-query-on-exit-flag dedicated-process nil)
1392 ;; Prefer dedicated if global also exists.
1393 (should (equal (python-shell-get-process) dedicated-process))
1394 (kill-buffer dedicated-shell-buffer)
1395 ;; If there's only global, use it.
1396 (should (equal (python-shell-get-process) global-process))
1397 (kill-buffer global-shell-buffer)
1398 ;; No buffer available.
1399 (should (not (python-shell-get-process))))
1400 (ignore-errors (kill-buffer global-shell-buffer))
1401 (ignore-errors (kill-buffer dedicated-shell-buffer))))))
1402
1403 (ert-deftest python-shell-get-or-create-process-1 ()
1404 "Check shell process creation fallback."
1405 :expected-result :failed
1406 (python-tests-with-temp-file
1407 ""
1408 ;; XXX: Break early until we can skip stuff. We need to mimic
1409 ;; user interaction because `python-shell-get-or-create-process'
1410 ;; asks for all arguments interactively when a shell process
1411 ;; doesn't exist.
1412 (should nil)
1413 (let* ((python-shell-interpreter
1414 (executable-find python-tests-shell-interpreter))
1415 (use-dialog-box)
1416 (dedicated-process-name (python-shell-get-process-name t))
1417 (dedicated-process (python-shell-get-or-create-process))
1418 (dedicated-shell-buffer (process-buffer dedicated-process)))
1419 (unwind-protect
1420 (progn
1421 (set-process-query-on-exit-flag dedicated-process nil)
1422 ;; Prefer dedicated if not buffer exist.
1423 (should (equal (process-name dedicated-process)
1424 dedicated-process-name))
1425 (kill-buffer dedicated-shell-buffer)
1426 ;; No buffer available.
1427 (should (not (python-shell-get-process))))
1428 (ignore-errors (kill-buffer dedicated-shell-buffer))))))
1429
1430 (ert-deftest python-shell-internal-get-or-create-process-1 ()
1431 "Check internal shell process creation fallback."
1432 :expected-result (if (executable-find python-tests-shell-interpreter)
1433 :passed
1434 :failed)
1435 (python-tests-with-temp-file
1436 ""
1437 (should (not (process-live-p (python-shell-internal-get-process-name))))
1438 (let* ((python-shell-interpreter
1439 (executable-find python-tests-shell-interpreter))
1440 (internal-process-name (python-shell-internal-get-process-name))
1441 (internal-process (python-shell-internal-get-or-create-process))
1442 (internal-shell-buffer (process-buffer internal-process)))
1443 (unwind-protect
1444 (progn
1445 (set-process-query-on-exit-flag internal-process nil)
1446 (should (equal (process-name internal-process)
1447 internal-process-name))
1448 (should (equal internal-process
1449 (python-shell-internal-get-or-create-process)))
1450 ;; No user buffer available.
1451 (should (not (python-shell-get-process)))
1452 (kill-buffer internal-shell-buffer))
1453 (ignore-errors (kill-buffer internal-shell-buffer))))))
1454
1455 \f
1456 ;;; Shell completion
1457
1458 \f
1459 ;;; PDB Track integration
1460
1461 \f
1462 ;;; Symbol completion
1463
1464 \f
1465 ;;; Fill paragraph
1466
1467 \f
1468 ;;; Skeletons
1469
1470 \f
1471 ;;; FFAP
1472
1473 \f
1474 ;;; Code check
1475
1476 \f
1477 ;;; Eldoc
1478
1479 \f
1480 ;;; Imenu
1481 (ert-deftest python-imenu-prev-index-position-1 ()
1482 (require 'imenu)
1483 (python-tests-with-temp-buffer
1484 "
1485 def decoratorFunctionWithArguments(arg1, arg2, arg3):
1486 '''print decorated function call data to stdout.
1487
1488 Usage:
1489
1490 @decoratorFunctionWithArguments('arg1', 'arg2')
1491 def func(a, b, c=True):
1492 pass
1493 '''
1494
1495 def wwrap(f):
1496 print 'Inside wwrap()'
1497 def wrapped_f(*args):
1498 print 'Inside wrapped_f()'
1499 print 'Decorator arguments:', arg1, arg2, arg3
1500 f(*args)
1501 print 'After f(*args)'
1502 return wrapped_f
1503 return wwrap
1504
1505 def test(): # Some comment
1506 'This is a test function'
1507 print 'test'
1508
1509 class C(object):
1510
1511 def m(self):
1512 self.c()
1513
1514 def b():
1515 pass
1516
1517 def a():
1518 pass
1519
1520 def c(self):
1521 pass
1522 "
1523 (let ((expected
1524 '(("*Rescan*" . -99)
1525 ("decoratorFunctionWithArguments" . 2)
1526 ("decoratorFunctionWithArguments.wwrap" . 224)
1527 ("decoratorFunctionWithArguments.wwrap.wrapped_f" . 273)
1528 ("test" . 500)
1529 ("C" . 575)
1530 ("C.m" . 593)
1531 ("C.m.b" . 628)
1532 ("C.m.a" . 663)
1533 ("C.c" . 698))))
1534 (mapc
1535 (lambda (elt)
1536 (should (= (cdr (assoc-string (car elt) expected))
1537 (if (markerp (cdr elt))
1538 (marker-position (cdr elt))
1539 (cdr elt)))))
1540 (imenu--make-index-alist)))))
1541
1542 \f
1543 ;;; Misc helpers
1544
1545 (ert-deftest python-info-current-defun-1 ()
1546 (python-tests-with-temp-buffer
1547 "
1548 def foo(a, b):
1549 "
1550 (forward-line 1)
1551 (should (string= "foo" (python-info-current-defun)))
1552 (should (string= "def foo" (python-info-current-defun t)))
1553 (forward-line 1)
1554 (should (not (python-info-current-defun)))
1555 (indent-for-tab-command)
1556 (should (string= "foo" (python-info-current-defun)))
1557 (should (string= "def foo" (python-info-current-defun t)))))
1558
1559 (ert-deftest python-info-current-defun-2 ()
1560 (python-tests-with-temp-buffer
1561 "
1562 class C(object):
1563
1564 def m(self):
1565 if True:
1566 return [i for i in range(3)]
1567 else:
1568 return []
1569
1570 def b():
1571 do_b()
1572
1573 def a():
1574 do_a()
1575
1576 def c(self):
1577 do_c()
1578 "
1579 (forward-line 1)
1580 (should (string= "C" (python-info-current-defun)))
1581 (should (string= "class C" (python-info-current-defun t)))
1582 (python-tests-look-at "return [i for ")
1583 (should (string= "C.m" (python-info-current-defun)))
1584 (should (string= "def C.m" (python-info-current-defun t)))
1585 (python-tests-look-at "def b():")
1586 (should (string= "C.m.b" (python-info-current-defun)))
1587 (should (string= "def C.m.b" (python-info-current-defun t)))
1588 (forward-line 2)
1589 (indent-for-tab-command)
1590 (python-indent-dedent-line-backspace 1)
1591 (should (string= "C.m" (python-info-current-defun)))
1592 (should (string= "def C.m" (python-info-current-defun t)))
1593 (python-tests-look-at "def c(self):")
1594 (forward-line -1)
1595 (indent-for-tab-command)
1596 (should (string= "C.m.a" (python-info-current-defun)))
1597 (should (string= "def C.m.a" (python-info-current-defun t)))
1598 (python-indent-dedent-line-backspace 1)
1599 (should (string= "C.m" (python-info-current-defun)))
1600 (should (string= "def C.m" (python-info-current-defun t)))
1601 (python-indent-dedent-line-backspace 1)
1602 (should (string= "C" (python-info-current-defun)))
1603 (should (string= "class C" (python-info-current-defun t)))
1604 (python-tests-look-at "def c(self):")
1605 (should (string= "C.c" (python-info-current-defun)))
1606 (should (string= "def C.c" (python-info-current-defun t)))
1607 (python-tests-look-at "do_c()")
1608 (should (string= "C.c" (python-info-current-defun)))
1609 (should (string= "def C.c" (python-info-current-defun t)))))
1610
1611 (ert-deftest python-info-current-defun-3 ()
1612 (python-tests-with-temp-buffer
1613 "
1614 def decoratorFunctionWithArguments(arg1, arg2, arg3):
1615 '''print decorated function call data to stdout.
1616
1617 Usage:
1618
1619 @decoratorFunctionWithArguments('arg1', 'arg2')
1620 def func(a, b, c=True):
1621 pass
1622 '''
1623
1624 def wwrap(f):
1625 print 'Inside wwrap()'
1626 def wrapped_f(*args):
1627 print 'Inside wrapped_f()'
1628 print 'Decorator arguments:', arg1, arg2, arg3
1629 f(*args)
1630 print 'After f(*args)'
1631 return wrapped_f
1632 return wwrap
1633 "
1634 (python-tests-look-at "def wwrap(f):")
1635 (forward-line -1)
1636 (should (not (python-info-current-defun)))
1637 (indent-for-tab-command 1)
1638 (should (string= (python-info-current-defun)
1639 "decoratorFunctionWithArguments"))
1640 (should (string= (python-info-current-defun t)
1641 "def decoratorFunctionWithArguments"))
1642 (python-tests-look-at "def wrapped_f(*args):")
1643 (should (string= (python-info-current-defun)
1644 "decoratorFunctionWithArguments.wwrap.wrapped_f"))
1645 (should (string= (python-info-current-defun t)
1646 "def decoratorFunctionWithArguments.wwrap.wrapped_f"))
1647 (python-tests-look-at "return wrapped_f")
1648 (should (string= (python-info-current-defun)
1649 "decoratorFunctionWithArguments.wwrap"))
1650 (should (string= (python-info-current-defun t)
1651 "def decoratorFunctionWithArguments.wwrap"))
1652 (end-of-line 1)
1653 (python-tests-look-at "return wwrap")
1654 (should (string= (python-info-current-defun)
1655 "decoratorFunctionWithArguments"))
1656 (should (string= (python-info-current-defun t)
1657 "def decoratorFunctionWithArguments"))))
1658
1659 (ert-deftest python-info-current-symbol-1 ()
1660 (python-tests-with-temp-buffer
1661 "
1662 class C(object):
1663
1664 def m(self):
1665 self.c()
1666
1667 def c(self):
1668 print ('a')
1669 "
1670 (python-tests-look-at "self.c()")
1671 (should (string= "self.c" (python-info-current-symbol)))
1672 (should (string= "C.c" (python-info-current-symbol t)))))
1673
1674 (ert-deftest python-info-current-symbol-2 ()
1675 (python-tests-with-temp-buffer
1676 "
1677 class C(object):
1678
1679 class M(object):
1680
1681 def a(self):
1682 self.c()
1683
1684 def c(self):
1685 pass
1686 "
1687 (python-tests-look-at "self.c()")
1688 (should (string= "self.c" (python-info-current-symbol)))
1689 (should (string= "C.M.c" (python-info-current-symbol t)))))
1690
1691 (ert-deftest python-info-current-symbol-3 ()
1692 "Keywords should not be considered symbols."
1693 :expected-result :failed
1694 (python-tests-with-temp-buffer
1695 "
1696 class C(object):
1697 pass
1698 "
1699 ;; FIXME: keywords are not symbols.
1700 (python-tests-look-at "class C")
1701 (should (not (python-info-current-symbol)))
1702 (should (not (python-info-current-symbol t)))
1703 (python-tests-look-at "C(object)")
1704 (should (string= "C" (python-info-current-symbol)))
1705 (should (string= "class C" (python-info-current-symbol t)))))
1706
1707 (ert-deftest python-info-statement-starts-block-p-1 ()
1708 (python-tests-with-temp-buffer
1709 "
1710 def long_function_name(
1711 var_one, var_two, var_three,
1712 var_four):
1713 print (var_one)
1714 "
1715 (python-tests-look-at "def long_function_name")
1716 (should (python-info-statement-starts-block-p))
1717 (python-tests-look-at "print (var_one)")
1718 (python-util-forward-comment -1)
1719 (should (python-info-statement-starts-block-p))))
1720
1721 (ert-deftest python-info-statement-starts-block-p-2 ()
1722 (python-tests-with-temp-buffer
1723 "
1724 if width == 0 and height == 0 and \\\\
1725 color == 'red' and emphasis == 'strong' or \\\\
1726 highlight > 100:
1727 raise ValueError('sorry, you lose')
1728 "
1729 (python-tests-look-at "if width == 0 and")
1730 (should (python-info-statement-starts-block-p))
1731 (python-tests-look-at "raise ValueError(")
1732 (python-util-forward-comment -1)
1733 (should (python-info-statement-starts-block-p))))
1734
1735 (ert-deftest python-info-statement-ends-block-p-1 ()
1736 (python-tests-with-temp-buffer
1737 "
1738 def long_function_name(
1739 var_one, var_two, var_three,
1740 var_four):
1741 print (var_one)
1742 "
1743 (python-tests-look-at "print (var_one)")
1744 (should (python-info-statement-ends-block-p))))
1745
1746 (ert-deftest python-info-statement-ends-block-p-2 ()
1747 (python-tests-with-temp-buffer
1748 "
1749 if width == 0 and height == 0 and \\\\
1750 color == 'red' and emphasis == 'strong' or \\\\
1751 highlight > 100:
1752 raise ValueError(
1753 'sorry, you lose'
1754
1755 )
1756 "
1757 (python-tests-look-at "raise ValueError(")
1758 (should (python-info-statement-ends-block-p))))
1759
1760 (ert-deftest python-info-beginning-of-statement-p-1 ()
1761 (python-tests-with-temp-buffer
1762 "
1763 def long_function_name(
1764 var_one, var_two, var_three,
1765 var_four):
1766 print (var_one)
1767 "
1768 (python-tests-look-at "def long_function_name")
1769 (should (python-info-beginning-of-statement-p))
1770 (forward-char 10)
1771 (should (not (python-info-beginning-of-statement-p)))
1772 (python-tests-look-at "print (var_one)")
1773 (should (python-info-beginning-of-statement-p))
1774 (goto-char (line-beginning-position))
1775 (should (not (python-info-beginning-of-statement-p)))))
1776
1777 (ert-deftest python-info-beginning-of-statement-p-2 ()
1778 (python-tests-with-temp-buffer
1779 "
1780 if width == 0 and height == 0 and \\\\
1781 color == 'red' and emphasis == 'strong' or \\\\
1782 highlight > 100:
1783 raise ValueError(
1784 'sorry, you lose'
1785
1786 )
1787 "
1788 (python-tests-look-at "if width == 0 and")
1789 (should (python-info-beginning-of-statement-p))
1790 (forward-char 10)
1791 (should (not (python-info-beginning-of-statement-p)))
1792 (python-tests-look-at "raise ValueError(")
1793 (should (python-info-beginning-of-statement-p))
1794 (goto-char (line-beginning-position))
1795 (should (not (python-info-beginning-of-statement-p)))))
1796
1797 (ert-deftest python-info-end-of-statement-p-1 ()
1798 (python-tests-with-temp-buffer
1799 "
1800 def long_function_name(
1801 var_one, var_two, var_three,
1802 var_four):
1803 print (var_one)
1804 "
1805 (python-tests-look-at "def long_function_name")
1806 (should (not (python-info-end-of-statement-p)))
1807 (end-of-line)
1808 (should (not (python-info-end-of-statement-p)))
1809 (python-tests-look-at "print (var_one)")
1810 (python-util-forward-comment -1)
1811 (should (python-info-end-of-statement-p))
1812 (python-tests-look-at "print (var_one)")
1813 (should (not (python-info-end-of-statement-p)))
1814 (end-of-line)
1815 (should (python-info-end-of-statement-p))))
1816
1817 (ert-deftest python-info-end-of-statement-p-2 ()
1818 (python-tests-with-temp-buffer
1819 "
1820 if width == 0 and height == 0 and \\\\
1821 color == 'red' and emphasis == 'strong' or \\\\
1822 highlight > 100:
1823 raise ValueError(
1824 'sorry, you lose'
1825
1826 )
1827 "
1828 (python-tests-look-at "if width == 0 and")
1829 (should (not (python-info-end-of-statement-p)))
1830 (end-of-line)
1831 (should (not (python-info-end-of-statement-p)))
1832 (python-tests-look-at "raise ValueError(")
1833 (python-util-forward-comment -1)
1834 (should (python-info-end-of-statement-p))
1835 (python-tests-look-at "raise ValueError(")
1836 (should (not (python-info-end-of-statement-p)))
1837 (end-of-line)
1838 (should (not (python-info-end-of-statement-p)))
1839 (goto-char (point-max))
1840 (python-util-forward-comment -1)
1841 (should (python-info-end-of-statement-p))))
1842
1843 (ert-deftest python-info-beginning-of-block-p-1 ()
1844 (python-tests-with-temp-buffer
1845 "
1846 def long_function_name(
1847 var_one, var_two, var_three,
1848 var_four):
1849 print (var_one)
1850 "
1851 (python-tests-look-at "def long_function_name")
1852 (should (python-info-beginning-of-block-p))
1853 (python-tests-look-at "var_one, var_two, var_three,")
1854 (should (not (python-info-beginning-of-block-p)))
1855 (python-tests-look-at "print (var_one)")
1856 (should (not (python-info-beginning-of-block-p)))))
1857
1858 (ert-deftest python-info-beginning-of-block-p-2 ()
1859 (python-tests-with-temp-buffer
1860 "
1861 if width == 0 and height == 0 and \\\\
1862 color == 'red' and emphasis == 'strong' or \\\\
1863 highlight > 100:
1864 raise ValueError(
1865 'sorry, you lose'
1866
1867 )
1868 "
1869 (python-tests-look-at "if width == 0 and")
1870 (should (python-info-beginning-of-block-p))
1871 (python-tests-look-at "color == 'red' and emphasis")
1872 (should (not (python-info-beginning-of-block-p)))
1873 (python-tests-look-at "raise ValueError(")
1874 (should (not (python-info-beginning-of-block-p)))))
1875
1876 (ert-deftest python-info-end-of-block-p-1 ()
1877 (python-tests-with-temp-buffer
1878 "
1879 def long_function_name(
1880 var_one, var_two, var_three,
1881 var_four):
1882 print (var_one)
1883 "
1884 (python-tests-look-at "def long_function_name")
1885 (should (not (python-info-end-of-block-p)))
1886 (python-tests-look-at "var_one, var_two, var_three,")
1887 (should (not (python-info-end-of-block-p)))
1888 (python-tests-look-at "var_four):")
1889 (end-of-line)
1890 (should (not (python-info-end-of-block-p)))
1891 (python-tests-look-at "print (var_one)")
1892 (should (not (python-info-end-of-block-p)))
1893 (end-of-line 1)
1894 (should (python-info-end-of-block-p))))
1895
1896 (ert-deftest python-info-end-of-block-p-2 ()
1897 (python-tests-with-temp-buffer
1898 "
1899 if width == 0 and height == 0 and \\\\
1900 color == 'red' and emphasis == 'strong' or \\\\
1901 highlight > 100:
1902 raise ValueError(
1903 'sorry, you lose'
1904
1905 )
1906 "
1907 (python-tests-look-at "if width == 0 and")
1908 (should (not (python-info-end-of-block-p)))
1909 (python-tests-look-at "color == 'red' and emphasis == 'strong' or")
1910 (should (not (python-info-end-of-block-p)))
1911 (python-tests-look-at "highlight > 100:")
1912 (end-of-line)
1913 (should (not (python-info-end-of-block-p)))
1914 (python-tests-look-at "raise ValueError(")
1915 (should (not (python-info-end-of-block-p)))
1916 (end-of-line 1)
1917 (should (not (python-info-end-of-block-p)))
1918 (goto-char (point-max))
1919 (python-util-forward-comment -1)
1920 (should (python-info-end-of-block-p))))
1921
1922 (ert-deftest python-info-closing-block-1 ()
1923 (python-tests-with-temp-buffer
1924 "
1925 if request.user.is_authenticated():
1926 try:
1927 profile = request.user.get_profile()
1928 except Profile.DoesNotExist:
1929 profile = Profile.objects.create(user=request.user)
1930 else:
1931 if profile.stats:
1932 profile.recalculate_stats()
1933 else:
1934 profile.clear_stats()
1935 finally:
1936 profile.views += 1
1937 profile.save()
1938 "
1939 (python-tests-look-at "try:")
1940 (should (not (python-info-closing-block)))
1941 (python-tests-look-at "except Profile.DoesNotExist:")
1942 (should (= (python-tests-look-at "try:" -1 t)
1943 (python-info-closing-block)))
1944 (python-tests-look-at "else:")
1945 (should (= (python-tests-look-at "except Profile.DoesNotExist:" -1 t)
1946 (python-info-closing-block)))
1947 (python-tests-look-at "if profile.stats:")
1948 (should (not (python-info-closing-block)))
1949 (python-tests-look-at "else:")
1950 (should (= (python-tests-look-at "if profile.stats:" -1 t)
1951 (python-info-closing-block)))
1952 (python-tests-look-at "finally:")
1953 (should (= (python-tests-look-at "else:" -2 t)
1954 (python-info-closing-block)))))
1955
1956 (ert-deftest python-info-closing-block-2 ()
1957 (python-tests-with-temp-buffer
1958 "
1959 if request.user.is_authenticated():
1960 profile = Profile.objects.get_or_create(user=request.user)
1961 if profile.stats:
1962 profile.recalculate_stats()
1963
1964 data = {
1965 'else': 'do it'
1966 }
1967 'else'
1968 "
1969 (python-tests-look-at "'else': 'do it'")
1970 (should (not (python-info-closing-block)))
1971 (python-tests-look-at "'else'")
1972 (should (not (python-info-closing-block)))))
1973
1974 (ert-deftest python-info-line-ends-backslash-p-1 ()
1975 (python-tests-with-temp-buffer
1976 "
1977 objects = Thing.objects.all() \\\\
1978 .filter(
1979 type='toy',
1980 status='bought'
1981 ) \\\\
1982 .aggregate(
1983 Sum('amount')
1984 ) \\\\
1985 .values_list()
1986 "
1987 (should (python-info-line-ends-backslash-p 2)) ; .filter(...
1988 (should (python-info-line-ends-backslash-p 3))
1989 (should (python-info-line-ends-backslash-p 4))
1990 (should (python-info-line-ends-backslash-p 5))
1991 (should (python-info-line-ends-backslash-p 6)) ; ) \...
1992 (should (python-info-line-ends-backslash-p 7))
1993 (should (python-info-line-ends-backslash-p 8))
1994 (should (python-info-line-ends-backslash-p 9))
1995 (should (not (python-info-line-ends-backslash-p 10))))) ; .values_list()...
1996
1997 (ert-deftest python-info-beginning-of-backslash-1 ()
1998 (python-tests-with-temp-buffer
1999 "
2000 objects = Thing.objects.all() \\\\
2001 .filter(
2002 type='toy',
2003 status='bought'
2004 ) \\\\
2005 .aggregate(
2006 Sum('amount')
2007 ) \\\\
2008 .values_list()
2009 "
2010 (let ((first 2)
2011 (second (python-tests-look-at ".filter("))
2012 (third (python-tests-look-at ".aggregate(")))
2013 (should (= first (python-info-beginning-of-backslash 2)))
2014 (should (= second (python-info-beginning-of-backslash 3)))
2015 (should (= second (python-info-beginning-of-backslash 4)))
2016 (should (= second (python-info-beginning-of-backslash 5)))
2017 (should (= second (python-info-beginning-of-backslash 6)))
2018 (should (= third (python-info-beginning-of-backslash 7)))
2019 (should (= third (python-info-beginning-of-backslash 8)))
2020 (should (= third (python-info-beginning-of-backslash 9)))
2021 (should (not (python-info-beginning-of-backslash 10))))))
2022
2023 (ert-deftest python-info-continuation-line-p-1 ()
2024 (python-tests-with-temp-buffer
2025 "
2026 if width == 0 and height == 0 and \\\\
2027 color == 'red' and emphasis == 'strong' or \\\\
2028 highlight > 100:
2029 raise ValueError(
2030 'sorry, you lose'
2031
2032 )
2033 "
2034 (python-tests-look-at "if width == 0 and height == 0 and")
2035 (should (not (python-info-continuation-line-p)))
2036 (python-tests-look-at "color == 'red' and emphasis == 'strong' or")
2037 (should (python-info-continuation-line-p))
2038 (python-tests-look-at "highlight > 100:")
2039 (should (python-info-continuation-line-p))
2040 (python-tests-look-at "raise ValueError(")
2041 (should (not (python-info-continuation-line-p)))
2042 (python-tests-look-at "'sorry, you lose'")
2043 (should (python-info-continuation-line-p))
2044 (forward-line 1)
2045 (should (python-info-continuation-line-p))
2046 (python-tests-look-at ")")
2047 (should (python-info-continuation-line-p))
2048 (forward-line 1)
2049 (should (not (python-info-continuation-line-p)))))
2050
2051 (ert-deftest python-info-block-continuation-line-p-1 ()
2052 (python-tests-with-temp-buffer
2053 "
2054 if width == 0 and height == 0 and \\\\
2055 color == 'red' and emphasis == 'strong' or \\\\
2056 highlight > 100:
2057 raise ValueError(
2058 'sorry, you lose'
2059
2060 )
2061 "
2062 (python-tests-look-at "if width == 0 and")
2063 (should (not (python-info-block-continuation-line-p)))
2064 (python-tests-look-at "color == 'red' and emphasis == 'strong' or")
2065 (should (= (python-info-block-continuation-line-p)
2066 (python-tests-look-at "if width == 0 and" -1 t)))
2067 (python-tests-look-at "highlight > 100:")
2068 (should (not (python-info-block-continuation-line-p)))))
2069
2070 (ert-deftest python-info-block-continuation-line-p-2 ()
2071 (python-tests-with-temp-buffer
2072 "
2073 def foo(a,
2074 b,
2075 c):
2076 pass
2077 "
2078 (python-tests-look-at "def foo(a,")
2079 (should (not (python-info-block-continuation-line-p)))
2080 (python-tests-look-at "b,")
2081 (should (= (python-info-block-continuation-line-p)
2082 (python-tests-look-at "def foo(a," -1 t)))
2083 (python-tests-look-at "c):")
2084 (should (not (python-info-block-continuation-line-p)))))
2085
2086 (ert-deftest python-info-assignment-continuation-line-p-1 ()
2087 (python-tests-with-temp-buffer
2088 "
2089 data = foo(), bar() \\\\
2090 baz(), 4 \\\\
2091 5, 6
2092 "
2093 (python-tests-look-at "data = foo(), bar()")
2094 (should (not (python-info-assignment-continuation-line-p)))
2095 (python-tests-look-at "baz(), 4")
2096 (should (= (python-info-assignment-continuation-line-p)
2097 (python-tests-look-at "foo()," -1 t)))
2098 (python-tests-look-at "5, 6")
2099 (should (not (python-info-assignment-continuation-line-p)))))
2100
2101 (ert-deftest python-info-assignment-continuation-line-p-2 ()
2102 (python-tests-with-temp-buffer
2103 "
2104 data = (foo(), bar()
2105 baz(), 4
2106 5, 6)
2107 "
2108 (python-tests-look-at "data = (foo(), bar()")
2109 (should (not (python-info-assignment-continuation-line-p)))
2110 (python-tests-look-at "baz(), 4")
2111 (should (= (python-info-assignment-continuation-line-p)
2112 (python-tests-look-at "(foo()," -1 t)))
2113 (python-tests-look-at "5, 6)")
2114 (should (not (python-info-assignment-continuation-line-p)))))
2115
2116 (ert-deftest python-info-looking-at-beginning-of-defun-1 ()
2117 (python-tests-with-temp-buffer
2118 "
2119 def decorat0r(deff):
2120 '''decorates stuff.
2121
2122 @decorat0r
2123 def foo(arg):
2124 ...
2125 '''
2126 def wrap():
2127 deff()
2128 return wwrap
2129 "
2130 (python-tests-look-at "def decorat0r(deff):")
2131 (should (python-info-looking-at-beginning-of-defun))
2132 (python-tests-look-at "def foo(arg):")
2133 (should (not (python-info-looking-at-beginning-of-defun)))
2134 (python-tests-look-at "def wrap():")
2135 (should (python-info-looking-at-beginning-of-defun))
2136 (python-tests-look-at "deff()")
2137 (should (not (python-info-looking-at-beginning-of-defun)))))
2138
2139 (ert-deftest python-info-current-line-comment-p-1 ()
2140 (python-tests-with-temp-buffer
2141 "
2142 # this is a comment
2143 foo = True # another comment
2144 '#this is a string'
2145 if foo:
2146 # more comments
2147 print ('bar') # print bar
2148 "
2149 (python-tests-look-at "# this is a comment")
2150 (should (python-info-current-line-comment-p))
2151 (python-tests-look-at "foo = True # another comment")
2152 (should (not (python-info-current-line-comment-p)))
2153 (python-tests-look-at "'#this is a string'")
2154 (should (not (python-info-current-line-comment-p)))
2155 (python-tests-look-at "# more comments")
2156 (should (python-info-current-line-comment-p))
2157 (python-tests-look-at "print ('bar') # print bar")
2158 (should (not (python-info-current-line-comment-p)))))
2159
2160 (ert-deftest python-info-current-line-empty-p ()
2161 (python-tests-with-temp-buffer
2162 "
2163 # this is a comment
2164
2165 foo = True # another comment
2166 "
2167 (should (python-info-current-line-empty-p))
2168 (python-tests-look-at "# this is a comment")
2169 (should (not (python-info-current-line-empty-p)))
2170 (forward-line 1)
2171 (should (python-info-current-line-empty-p))))
2172
2173 \f
2174 ;;; Utility functions
2175
2176 (ert-deftest python-util-goto-line-1 ()
2177 (python-tests-with-temp-buffer
2178 (concat
2179 "# a comment
2180 # another comment
2181 def foo(a, b, c):
2182 pass" (make-string 20 ?\n))
2183 (python-util-goto-line 10)
2184 (should (= (line-number-at-pos) 10))
2185 (python-util-goto-line 20)
2186 (should (= (line-number-at-pos) 20))))
2187
2188 (ert-deftest python-util-clone-local-variables-1 ()
2189 (let ((buffer (generate-new-buffer
2190 "python-util-clone-local-variables-1"))
2191 (varcons
2192 '((python-fill-docstring-style . django)
2193 (python-shell-interpreter . "python")
2194 (python-shell-interpreter-args . "manage.py shell")
2195 (python-shell-prompt-regexp . "In \\[[0-9]+\\]: ")
2196 (python-shell-prompt-output-regexp . "Out\\[[0-9]+\\]: ")
2197 (python-shell-extra-pythonpaths "/home/user/pylib/")
2198 (python-shell-completion-setup-code
2199 . "from IPython.core.completerlib import module_completion")
2200 (python-shell-completion-module-string-code
2201 . "';'.join(module_completion('''%s'''))\n")
2202 (python-shell-completion-string-code
2203 . "';'.join(get_ipython().Completer.all_completions('''%s'''))\n")
2204 (python-shell-virtualenv-path
2205 . "/home/user/.virtualenvs/project"))))
2206 (with-current-buffer buffer
2207 (kill-all-local-variables)
2208 (dolist (ccons varcons)
2209 (set (make-local-variable (car ccons)) (cdr ccons))))
2210 (python-tests-with-temp-buffer
2211 ""
2212 (python-util-clone-local-variables buffer)
2213 (dolist (ccons varcons)
2214 (should
2215 (equal (symbol-value (car ccons)) (cdr ccons)))))
2216 (kill-buffer buffer)))
2217
2218 (ert-deftest python-util-forward-comment-1 ()
2219 (python-tests-with-temp-buffer
2220 (concat
2221 "# a comment
2222 # another comment
2223 # bad indented comment
2224 # more comments" (make-string 9999 ?\n))
2225 (python-util-forward-comment 1)
2226 (should (= (point) (point-max)))
2227 (python-util-forward-comment -1)
2228 (should (= (point) (point-min)))))
2229
2230
2231 (provide 'python-tests)
2232
2233 ;; Local Variables:
2234 ;; coding: utf-8
2235 ;; indent-tabs-mode: nil
2236 ;; End:
2237
2238 ;;; python-tests.el ends here