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_checkers_utils.py 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. # Copyright (c) 2010 LOGILAB S.A. (Paris, FRANCE) <contact@logilab.fr>
  2. # Copyright (c) 2013-2017 Claudiu Popa <pcmanticore@gmail.com>
  3. # Copyright (c) 2013-2014 Google, Inc.
  4. # Copyright (c) 2014 Arun Persaud <arun@nubati.net>
  5. # Copyright (c) 2015 Ionel Cristian Maries <contact@ionelmc.ro>
  6. # Copyright (c) 2016 Derek Gustafson <degustaf@gmail.com>
  7. # Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  8. # For details: https://github.com/PyCQA/pylint/blob/master/COPYING
  9. """Tests for the pylint.checkers.utils module."""
  10. import astroid
  11. from pylint.checkers import utils
  12. import pytest
  13. @pytest.mark.parametrize("name,expected", [
  14. ('min', True),
  15. ('__builtins__', True),
  16. ('__path__', False),
  17. ('__file__', False),
  18. ('whatever', False),
  19. ('mybuiltin', False),
  20. ])
  21. def testIsBuiltin(name, expected):
  22. assert utils.is_builtin(name) == expected
  23. @pytest.mark.parametrize("fn,kw", [
  24. ('foo(3)', {'keyword': 'bar'}),
  25. ('foo(one=a, two=b, three=c)', {'position': 1}),
  26. ])
  27. def testGetArgumentFromCallError(fn, kw):
  28. with pytest.raises(utils.NoSuchArgumentError):
  29. node = astroid.extract_node(fn)
  30. utils.get_argument_from_call(node, **kw)
  31. @pytest.mark.parametrize("fn,kw", [
  32. ('foo(bar=3)', {'keyword': 'bar'}),
  33. ('foo(a, b, c)', {'position': 1}),
  34. ])
  35. def testGetArgumentFromCallExists(fn, kw):
  36. node = astroid.extract_node(fn)
  37. assert utils.get_argument_from_call(node, **kw) is not None
  38. def testGetArgumentFromCall():
  39. node = astroid.extract_node('foo(a, not_this_one=1, this_one=2)')
  40. arg = utils.get_argument_from_call(node, position=2, keyword='this_one')
  41. assert 2 == arg.value
  42. node = astroid.extract_node('foo(a)')
  43. with pytest.raises(utils.NoSuchArgumentError):
  44. utils.get_argument_from_call(node, position=1)
  45. with pytest.raises(ValueError):
  46. utils.get_argument_from_call(node, None, None)
  47. name = utils.get_argument_from_call(node, position=0)
  48. assert name.name == 'a'
  49. def test_error_of_type():
  50. nodes = astroid.extract_node("""
  51. try: pass
  52. except AttributeError: #@
  53. pass
  54. try: pass
  55. except Exception: #@
  56. pass
  57. except: #@
  58. pass
  59. """)
  60. assert utils.error_of_type(nodes[0], AttributeError)
  61. assert utils.error_of_type(nodes[0], (AttributeError, ))
  62. assert not utils.error_of_type(nodes[0], Exception)
  63. assert utils.error_of_type(nodes[1], Exception)
  64. assert not utils.error_of_type(nodes[2], ImportError)
  65. def test_node_ignores_exception():
  66. nodes = astroid.extract_node("""
  67. try:
  68. 1/0 #@
  69. except ZeroDivisionError:
  70. pass
  71. try:
  72. 1/0 #@
  73. except Exception:
  74. pass
  75. try:
  76. 2/0 #@
  77. except:
  78. pass
  79. try:
  80. 1/0 #@
  81. except ValueError:
  82. pass
  83. """)
  84. assert utils.node_ignores_exception(nodes[0], ZeroDivisionError)
  85. assert not utils.node_ignores_exception(nodes[1], ZeroDivisionError)
  86. assert not utils.node_ignores_exception(nodes[2], ZeroDivisionError)
  87. assert not utils.node_ignores_exception(nodes[3], ZeroDivisionError)