Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

unittest_checker_exceptions.py 2.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. # Copyright (c) 2015-2017 Claudiu Popa <pcmanticore@gmail.com>
  2. # Copyright (c) 2015 Rene Zhang <rz99@cornell.edu>
  3. # Copyright (c) 2015 Steven Myint <hg@stevenmyint.com>
  4. # Copyright (c) 2015 Pavel Roskin <proski@gnu.org>
  5. # Copyright (c) 2015 Ionel Cristian Maries <contact@ionelmc.ro>
  6. # Copyright (c) 2016 Derek Gustafson <degustaf@gmail.com>
  7. # Copyright (c) 2018 Brian Shaginaw <brian.shaginaw@warbyparker.com>
  8. # Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  9. # For details: https://github.com/PyCQA/pylint/blob/master/COPYING
  10. """Tests for pylint.checkers.exceptions."""
  11. import sys
  12. import pytest
  13. import astroid
  14. from pylint.checkers import exceptions
  15. from pylint.testutils import CheckerTestCase, Message
  16. class TestExceptionsChecker(CheckerTestCase):
  17. """Tests for pylint.checkers.exceptions."""
  18. CHECKER_CLASS = exceptions.ExceptionsChecker
  19. # These tests aren't in the functional test suite,
  20. # since they will be converted with 2to3 for Python 3
  21. # and `raise (Error, ...)` will be converted to
  22. # `raise Error(...)`, so it beats the purpose of the test.
  23. @pytest.mark.skipif(sys.version_info[0] != 3,
  24. reason="The test should emit an error on Python 3.")
  25. def test_raising_bad_type_python3(self):
  26. node = astroid.extract_node('raise (ZeroDivisionError, None) #@')
  27. message = Message('raising-bad-type', node=node, args='tuple')
  28. with self.assertAddsMessages(message):
  29. self.checker.visit_raise(node)
  30. @pytest.mark.skipif(sys.version_info[0] != 2,
  31. reason="The test is valid only on Python 2.")
  32. def test_raising_bad_type_python2(self):
  33. nodes = astroid.extract_node('''
  34. raise (ZeroDivisionError, None) #@
  35. from something import something
  36. raise (something, None) #@
  37. raise (4, None) #@
  38. raise () #@
  39. ''')
  40. with self.assertNoMessages():
  41. self.checker.visit_raise(nodes[0])
  42. with self.assertNoMessages():
  43. self.checker.visit_raise(nodes[1])
  44. message = Message('raising-bad-type', node=nodes[2], args='tuple')
  45. with self.assertAddsMessages(message):
  46. self.checker.visit_raise(nodes[2])
  47. message = Message('raising-bad-type', node=nodes[3], args='tuple')
  48. with self.assertAddsMessages(message):
  49. self.checker.visit_raise(nodes[3])
  50. @pytest.mark.skipif(sys.version_info[0] != 3,
  51. reason="The test is valid only on Python 3.")
  52. def test_bad_exception_context_function(self):
  53. node = astroid.extract_node("""
  54. def function():
  55. pass
  56. try:
  57. pass
  58. except function as exc:
  59. raise Exception from exc #@
  60. """)
  61. message = Message('bad-exception-context', node=node)
  62. with self.assertAddsMessages(message):
  63. self.checker.visit_raise(node)