diff options
| author | Fabián Ezequiel Gallina | 2014-12-27 01:30:21 -0300 |
|---|---|---|
| committer | Fabián Ezequiel Gallina | 2014-12-27 01:30:21 -0300 |
| commit | 2dd5163d764f395eb31a2306dba385d123af4aba (patch) | |
| tree | 27b1ade1b6af5e984040a8acc840f81ee4f5adaa /test/automated/python-tests.el | |
| parent | 7aa506eed8881788485a9774165454404bac2623 (diff) | |
| download | emacs-2dd5163d764f395eb31a2306dba385d123af4aba.tar.gz emacs-2dd5163d764f395eb31a2306dba385d123af4aba.zip | |
python.el: Handle file encoding for shell.
* lisp/progmodes/python.el (python-rx-constituents): Add coding-cookie.
(python-shell--save-temp-file): Write file with proper encoding.
(python-shell-buffer-substring): Add coding cookie for detected
encoding to generated content. Fix blank lines when removing
if-name-main block.
(python-shell-send-file): Handle file encoding.
(python-info-encoding-from-cookie)
(python-info-encoding): New functions.
* test/automated/python-tests.el (python-shell-buffer-substring-1)
(python-shell-buffer-substring-2, python-shell-buffer-substring-3)
(python-shell-buffer-substring-4, python-shell-buffer-substring-5)
(python-shell-buffer-substring-6, python-shell-buffer-substring-7)
(python-shell-buffer-substring-8)
(python-info-encoding-from-cookie-1)
(python-info-encoding-from-cookie-2)
(python-info-encoding-from-cookie-3)
(python-info-encoding-from-cookie-4)
(python-info-encoding-from-cookie-5)
(python-info-encoding-from-cookie-6)
(python-info-encoding-from-cookie-7, python-info-encoding-1)
(python-info-encoding-2): New tests.
Diffstat (limited to 'test/automated/python-tests.el')
| -rw-r--r-- | test/automated/python-tests.el | 271 |
1 files changed, 271 insertions, 0 deletions
diff --git a/test/automated/python-tests.el b/test/automated/python-tests.el index d1713ac1851..8fcda58e1e0 100644 --- a/test/automated/python-tests.el +++ b/test/automated/python-tests.el | |||
| @@ -2459,6 +2459,198 @@ and `python-shell-interpreter-args' in the new shell buffer." | |||
| 2459 | "^\\(o\\.t \\|\\)"))) | 2459 | "^\\(o\\.t \\|\\)"))) |
| 2460 | (ignore-errors (delete-file startup-file))))) | 2460 | (ignore-errors (delete-file startup-file))))) |
| 2461 | 2461 | ||
| 2462 | (ert-deftest python-shell-buffer-substring-1 () | ||
| 2463 | "Selecting a substring of the whole buffer must match its contents." | ||
| 2464 | (python-tests-with-temp-buffer | ||
| 2465 | " | ||
| 2466 | class Foo(models.Model): | ||
| 2467 | pass | ||
| 2468 | |||
| 2469 | |||
| 2470 | class Bar(models.Model): | ||
| 2471 | pass | ||
| 2472 | " | ||
| 2473 | (should (string= (buffer-string) | ||
| 2474 | (python-shell-buffer-substring (point-min) (point-max)))))) | ||
| 2475 | |||
| 2476 | (ert-deftest python-shell-buffer-substring-2 () | ||
| 2477 | "Main block should be removed if NOMAIN is non-nil." | ||
| 2478 | (python-tests-with-temp-buffer | ||
| 2479 | " | ||
| 2480 | class Foo(models.Model): | ||
| 2481 | pass | ||
| 2482 | |||
| 2483 | class Bar(models.Model): | ||
| 2484 | pass | ||
| 2485 | |||
| 2486 | if __name__ == \"__main__\": | ||
| 2487 | foo = Foo() | ||
| 2488 | print (foo) | ||
| 2489 | " | ||
| 2490 | (should (string= (python-shell-buffer-substring (point-min) (point-max) t) | ||
| 2491 | " | ||
| 2492 | class Foo(models.Model): | ||
| 2493 | pass | ||
| 2494 | |||
| 2495 | class Bar(models.Model): | ||
| 2496 | pass | ||
| 2497 | |||
| 2498 | |||
| 2499 | |||
| 2500 | |||
| 2501 | ")))) | ||
| 2502 | |||
| 2503 | (ert-deftest python-shell-buffer-substring-3 () | ||
| 2504 | "Main block should be removed if NOMAIN is non-nil." | ||
| 2505 | (python-tests-with-temp-buffer | ||
| 2506 | " | ||
| 2507 | class Foo(models.Model): | ||
| 2508 | pass | ||
| 2509 | |||
| 2510 | if __name__ == \"__main__\": | ||
| 2511 | foo = Foo() | ||
| 2512 | print (foo) | ||
| 2513 | |||
| 2514 | class Bar(models.Model): | ||
| 2515 | pass | ||
| 2516 | " | ||
| 2517 | (should (string= (python-shell-buffer-substring (point-min) (point-max) t) | ||
| 2518 | " | ||
| 2519 | class Foo(models.Model): | ||
| 2520 | pass | ||
| 2521 | |||
| 2522 | |||
| 2523 | |||
| 2524 | |||
| 2525 | |||
| 2526 | class Bar(models.Model): | ||
| 2527 | pass | ||
| 2528 | ")))) | ||
| 2529 | |||
| 2530 | (ert-deftest python-shell-buffer-substring-4 () | ||
| 2531 | "Coding cookie should be added for substrings." | ||
| 2532 | (python-tests-with-temp-buffer | ||
| 2533 | "# coding: latin-1 | ||
| 2534 | |||
| 2535 | class Foo(models.Model): | ||
| 2536 | pass | ||
| 2537 | |||
| 2538 | if __name__ == \"__main__\": | ||
| 2539 | foo = Foo() | ||
| 2540 | print (foo) | ||
| 2541 | |||
| 2542 | class Bar(models.Model): | ||
| 2543 | pass | ||
| 2544 | " | ||
| 2545 | (should (string= (python-shell-buffer-substring | ||
| 2546 | (python-tests-look-at "class Foo(models.Model):") | ||
| 2547 | (progn (python-nav-forward-sexp) (point))) | ||
| 2548 | "# -*- coding: latin-1 -*- | ||
| 2549 | |||
| 2550 | class Foo(models.Model): | ||
| 2551 | pass")))) | ||
| 2552 | |||
| 2553 | (ert-deftest python-shell-buffer-substring-5 () | ||
| 2554 | "The proper amount of blank lines is added for a substring." | ||
| 2555 | (python-tests-with-temp-buffer | ||
| 2556 | "# coding: latin-1 | ||
| 2557 | |||
| 2558 | class Foo(models.Model): | ||
| 2559 | pass | ||
| 2560 | |||
| 2561 | if __name__ == \"__main__\": | ||
| 2562 | foo = Foo() | ||
| 2563 | print (foo) | ||
| 2564 | |||
| 2565 | class Bar(models.Model): | ||
| 2566 | pass | ||
| 2567 | " | ||
| 2568 | (should (string= (python-shell-buffer-substring | ||
| 2569 | (python-tests-look-at "class Bar(models.Model):") | ||
| 2570 | (progn (python-nav-forward-sexp) (point))) | ||
| 2571 | "# -*- coding: latin-1 -*- | ||
| 2572 | |||
| 2573 | |||
| 2574 | |||
| 2575 | |||
| 2576 | |||
| 2577 | |||
| 2578 | |||
| 2579 | |||
| 2580 | class Bar(models.Model): | ||
| 2581 | pass")))) | ||
| 2582 | |||
| 2583 | (ert-deftest python-shell-buffer-substring-6 () | ||
| 2584 | "Handle substring with coding cookie in the second line." | ||
| 2585 | (python-tests-with-temp-buffer | ||
| 2586 | " | ||
| 2587 | # coding: latin-1 | ||
| 2588 | |||
| 2589 | class Foo(models.Model): | ||
| 2590 | pass | ||
| 2591 | |||
| 2592 | if __name__ == \"__main__\": | ||
| 2593 | foo = Foo() | ||
| 2594 | print (foo) | ||
| 2595 | |||
| 2596 | class Bar(models.Model): | ||
| 2597 | pass | ||
| 2598 | " | ||
| 2599 | (should (string= (python-shell-buffer-substring | ||
| 2600 | (python-tests-look-at "# coding: latin-1") | ||
| 2601 | (python-tests-look-at "if __name__ == \"__main__\":")) | ||
| 2602 | "# -*- coding: latin-1 -*- | ||
| 2603 | |||
| 2604 | |||
| 2605 | class Foo(models.Model): | ||
| 2606 | pass | ||
| 2607 | |||
| 2608 | ")))) | ||
| 2609 | |||
| 2610 | (ert-deftest python-shell-buffer-substring-7 () | ||
| 2611 | "Ensure first coding cookie gets precedence." | ||
| 2612 | (python-tests-with-temp-buffer | ||
| 2613 | "# coding: utf-8 | ||
| 2614 | # coding: latin-1 | ||
| 2615 | |||
| 2616 | class Foo(models.Model): | ||
| 2617 | pass | ||
| 2618 | |||
| 2619 | if __name__ == \"__main__\": | ||
| 2620 | foo = Foo() | ||
| 2621 | print (foo) | ||
| 2622 | |||
| 2623 | class Bar(models.Model): | ||
| 2624 | pass | ||
| 2625 | " | ||
| 2626 | (should (string= (python-shell-buffer-substring | ||
| 2627 | (python-tests-look-at "# coding: latin-1") | ||
| 2628 | (python-tests-look-at "if __name__ == \"__main__\":")) | ||
| 2629 | "# -*- coding: utf-8 -*- | ||
| 2630 | |||
| 2631 | |||
| 2632 | class Foo(models.Model): | ||
| 2633 | pass | ||
| 2634 | |||
| 2635 | ")))) | ||
| 2636 | |||
| 2637 | (ert-deftest python-shell-buffer-substring-8 () | ||
| 2638 | "Ensure first coding cookie gets precedence when sending whole buffer." | ||
| 2639 | (python-tests-with-temp-buffer | ||
| 2640 | "# coding: utf-8 | ||
| 2641 | # coding: latin-1 | ||
| 2642 | |||
| 2643 | class Foo(models.Model): | ||
| 2644 | pass | ||
| 2645 | " | ||
| 2646 | (should (string= (python-shell-buffer-substring (point-min) (point-max)) | ||
| 2647 | "# coding: utf-8 | ||
| 2648 | |||
| 2649 | |||
| 2650 | class Foo(models.Model): | ||
| 2651 | pass | ||
| 2652 | ")))) | ||
| 2653 | |||
| 2462 | 2654 | ||
| 2463 | ;;; Shell completion | 2655 | ;;; Shell completion |
| 2464 | 2656 | ||
| @@ -3773,6 +3965,85 @@ foo = True # another comment | |||
| 3773 | (forward-line 1) | 3965 | (forward-line 1) |
| 3774 | (should (python-info-current-line-empty-p)))) | 3966 | (should (python-info-current-line-empty-p)))) |
| 3775 | 3967 | ||
| 3968 | (ert-deftest python-info-encoding-from-cookie-1 () | ||
| 3969 | "Should detect it on first line." | ||
| 3970 | (python-tests-with-temp-buffer | ||
| 3971 | "# coding=latin-1 | ||
| 3972 | |||
| 3973 | foo = True # another comment | ||
| 3974 | " | ||
| 3975 | (should (eq (python-info-encoding-from-cookie) 'latin-1)))) | ||
| 3976 | |||
| 3977 | (ert-deftest python-info-encoding-from-cookie-2 () | ||
| 3978 | "Should detect it on second line." | ||
| 3979 | (python-tests-with-temp-buffer | ||
| 3980 | " | ||
| 3981 | # coding=latin-1 | ||
| 3982 | |||
| 3983 | foo = True # another comment | ||
| 3984 | " | ||
| 3985 | (should (eq (python-info-encoding-from-cookie) 'latin-1)))) | ||
| 3986 | |||
| 3987 | (ert-deftest python-info-encoding-from-cookie-3 () | ||
| 3988 | "Should not be detected on third line (and following ones)." | ||
| 3989 | (python-tests-with-temp-buffer | ||
| 3990 | " | ||
| 3991 | |||
| 3992 | # coding=latin-1 | ||
| 3993 | foo = True # another comment | ||
| 3994 | " | ||
| 3995 | (should (not (python-info-encoding-from-cookie))))) | ||
| 3996 | |||
| 3997 | (ert-deftest python-info-encoding-from-cookie-4 () | ||
| 3998 | "Should detect Emacs style." | ||
| 3999 | (python-tests-with-temp-buffer | ||
| 4000 | "# -*- coding: latin-1 -*- | ||
| 4001 | |||
| 4002 | foo = True # another comment" | ||
| 4003 | (should (eq (python-info-encoding-from-cookie) 'latin-1)))) | ||
| 4004 | |||
| 4005 | (ert-deftest python-info-encoding-from-cookie-5 () | ||
| 4006 | "Should detect Vim style." | ||
| 4007 | (python-tests-with-temp-buffer | ||
| 4008 | "# vim: set fileencoding=latin-1 : | ||
| 4009 | |||
| 4010 | foo = True # another comment" | ||
| 4011 | (should (eq (python-info-encoding-from-cookie) 'latin-1)))) | ||
| 4012 | |||
| 4013 | (ert-deftest python-info-encoding-from-cookie-6 () | ||
| 4014 | "First cookie wins." | ||
| 4015 | (python-tests-with-temp-buffer | ||
| 4016 | "# -*- coding: iso-8859-1 -*- | ||
| 4017 | # vim: set fileencoding=latin-1 : | ||
| 4018 | |||
| 4019 | foo = True # another comment" | ||
| 4020 | (should (eq (python-info-encoding-from-cookie) 'iso-8859-1)))) | ||
| 4021 | |||
| 4022 | (ert-deftest python-info-encoding-from-cookie-7 () | ||
| 4023 | "First cookie wins." | ||
| 4024 | (python-tests-with-temp-buffer | ||
| 4025 | "# vim: set fileencoding=latin-1 : | ||
| 4026 | # -*- coding: iso-8859-1 -*- | ||
| 4027 | |||
| 4028 | foo = True # another comment" | ||
| 4029 | (should (eq (python-info-encoding-from-cookie) 'latin-1)))) | ||
| 4030 | |||
| 4031 | (ert-deftest python-info-encoding-1 () | ||
| 4032 | "Should return the detected encoding from cookie." | ||
| 4033 | (python-tests-with-temp-buffer | ||
| 4034 | "# vim: set fileencoding=latin-1 : | ||
| 4035 | |||
| 4036 | foo = True # another comment" | ||
| 4037 | (should (eq (python-info-encoding) 'latin-1)))) | ||
| 4038 | |||
| 4039 | (ert-deftest python-info-encoding-2 () | ||
| 4040 | "Should default to utf-8." | ||
| 4041 | (python-tests-with-temp-buffer | ||
| 4042 | "# No encoding for you | ||
| 4043 | |||
| 4044 | foo = True # another comment" | ||
| 4045 | (should (eq (python-info-encoding) 'utf-8)))) | ||
| 4046 | |||
| 3776 | 4047 | ||
| 3777 | ;;; Utility functions | 4048 | ;;; Utility functions |
| 3778 | 4049 | ||