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_peephole.py 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. # Copyright (c) 2015-2016 Claudiu Popa <pcmanticore@gmail.com>
  2. # Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html
  3. # For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER
  4. """Tests for the astroid AST peephole optimizer."""
  5. import ast
  6. import textwrap
  7. import unittest
  8. import astroid
  9. from astroid import astpeephole
  10. from astroid import builder
  11. from astroid import manager
  12. from astroid import test_utils
  13. from astroid.tests import resources
  14. MANAGER = manager.AstroidManager()
  15. class PeepholeOptimizer(unittest.TestCase):
  16. @classmethod
  17. def setUpClass(cls):
  18. MANAGER.optimize_ast = True
  19. @classmethod
  20. def tearDownClass(cls):
  21. MANAGER.optimize_ast = False
  22. def setUp(self):
  23. self._optimizer = astpeephole.ASTPeepholeOptimizer()
  24. @staticmethod
  25. def _get_binops(code):
  26. module = ast.parse(textwrap.dedent(code))
  27. return [node.value for node in module.body
  28. if isinstance(node, ast.Expr)]
  29. @test_utils.require_version(maxver='3.0')
  30. def test_optimize_binop_unicode(self):
  31. nodes = self._get_binops("""
  32. u"a" + u"b" + u"c"
  33. u"a" + "c" + "b"
  34. u"a" + b"c"
  35. """)
  36. result = self._optimizer.optimize_binop(nodes[0])
  37. self.assertIsInstance(result, astroid.Const)
  38. self.assertEqual(result.value, u"abc")
  39. self.assertIsNone(self._optimizer.optimize_binop(nodes[1]))
  40. self.assertIsNone(self._optimizer.optimize_binop(nodes[2]))
  41. def test_optimize_binop(self):
  42. nodes = self._get_binops("""
  43. "a" + "b" + "c" + "d"
  44. b"a" + b"b" + b"c" + b"d"
  45. "a" + "b"
  46. "a" + "b" + 1 + object
  47. var = 4
  48. "a" + "b" + var + "c"
  49. "a" + "b" + "c" - "4"
  50. "a" + "b" + "c" + "d".format()
  51. "a" - "b"
  52. "a"
  53. 1 + 4 + 5 + 6
  54. """)
  55. result = self._optimizer.optimize_binop(nodes[0])
  56. self.assertIsInstance(result, astroid.Const)
  57. self.assertEqual(result.value, "abcd")
  58. result = self._optimizer.optimize_binop(nodes[1])
  59. self.assertIsInstance(result, astroid.Const)
  60. self.assertEqual(result.value, b"abcd")
  61. for node in nodes[2:]:
  62. self.assertIsNone(self._optimizer.optimize_binop(node))
  63. def test_big_binop_crash(self):
  64. # Test that we don't fail on a lot of joined strings
  65. # through the addition operator.
  66. module = resources.build_file('data/joined_strings.py')
  67. element = next(module['x'].infer())
  68. self.assertIsInstance(element, astroid.Const)
  69. self.assertEqual(len(element.value), 61660)
  70. def test_optimisation_disabled(self):
  71. try:
  72. MANAGER.optimize_ast = False
  73. module = builder.parse("""
  74. '1' + '2' + '3'
  75. """)
  76. self.assertIsInstance(module.body[0], astroid.Expr)
  77. self.assertIsInstance(module.body[0].value, astroid.BinOp)
  78. self.assertIsInstance(module.body[0].value.left, astroid.BinOp)
  79. self.assertIsInstance(module.body[0].value.left.left,
  80. astroid.Const)
  81. finally:
  82. MANAGER.optimize_ast = True
  83. if __name__ == '__main__':
  84. unittest.main()