PageRenderTime 51ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/lib-python/2.7/test/test_future.py

https://bitbucket.org/yrttyr/pypy
Python | 119 lines | 92 code | 21 blank | 6 comment | 28 complexity | d8ce009d161f708da214b147317073a4 MD5 | raw file
  1. # Test various flavors of legal and illegal future statements
  2. import unittest
  3. from test import test_support
  4. import re
  5. rx = re.compile('\((\S+).py, line (\d+)')
  6. def get_error_location(msg):
  7. mo = rx.search(str(msg))
  8. return mo.group(1, 2)
  9. class FutureTest(unittest.TestCase):
  10. def test_future1(self):
  11. test_support.unload('test_future1')
  12. from test import test_future1
  13. self.assertEqual(test_future1.result, 6)
  14. def test_future2(self):
  15. test_support.unload('test_future2')
  16. from test import test_future2
  17. self.assertEqual(test_future2.result, 6)
  18. def test_future3(self):
  19. test_support.unload('test_future3')
  20. from test import test_future3
  21. def test_badfuture3(self):
  22. try:
  23. from test import badsyntax_future3
  24. except SyntaxError, msg:
  25. self.assertEqual(get_error_location(msg), ("badsyntax_future3", '3'))
  26. else:
  27. self.fail("expected exception didn't occur")
  28. def test_badfuture4(self):
  29. try:
  30. from test import badsyntax_future4
  31. except SyntaxError, msg:
  32. self.assertEqual(get_error_location(msg), ("badsyntax_future4", '3'))
  33. else:
  34. self.fail("expected exception didn't occur")
  35. def test_badfuture5(self):
  36. try:
  37. from test import badsyntax_future5
  38. except SyntaxError, msg:
  39. self.assertEqual(get_error_location(msg), ("badsyntax_future5", '4'))
  40. else:
  41. self.fail("expected exception didn't occur")
  42. def test_badfuture6(self):
  43. try:
  44. from test import badsyntax_future6
  45. except SyntaxError, msg:
  46. self.assertEqual(get_error_location(msg), ("badsyntax_future6", '3'))
  47. else:
  48. self.fail("expected exception didn't occur")
  49. def test_badfuture7(self):
  50. try:
  51. from test import badsyntax_future7
  52. except SyntaxError, msg:
  53. self.assertEqual(get_error_location(msg), ("badsyntax_future7", '3'))
  54. else:
  55. self.fail("expected exception didn't occur")
  56. def test_badfuture8(self):
  57. try:
  58. from test import badsyntax_future8
  59. except SyntaxError, msg:
  60. self.assertEqual(get_error_location(msg), ("badsyntax_future8", '3'))
  61. else:
  62. self.fail("expected exception didn't occur")
  63. def test_badfuture9(self):
  64. try:
  65. from test import badsyntax_future9
  66. except SyntaxError, msg:
  67. self.assertEqual(get_error_location(msg), ("badsyntax_future9", '3'))
  68. else:
  69. self.fail("expected exception didn't occur")
  70. def test_parserhack(self):
  71. # test that the parser.c::future_hack function works as expected
  72. # Note: although this test must pass, it's not testing the original
  73. # bug as of 2.6 since the with statement is not optional and
  74. # the parser hack disabled. If a new keyword is introduced in
  75. # 2.6, change this to refer to the new future import.
  76. try:
  77. exec "from __future__ import print_function; print 0"
  78. except SyntaxError:
  79. pass
  80. else:
  81. self.fail("syntax error didn't occur")
  82. try:
  83. exec "from __future__ import (print_function); print 0"
  84. except SyntaxError:
  85. pass
  86. else:
  87. self.fail("syntax error didn't occur")
  88. def test_multiple_features(self):
  89. test_support.unload("test.test_future5")
  90. from test import test_future5
  91. def test_unicode_literals_exec(self):
  92. scope = {}
  93. exec "from __future__ import unicode_literals; x = ''" in scope
  94. self.assertIsInstance(scope["x"], unicode)
  95. def test_main():
  96. test_support.run_unittest(FutureTest)
  97. if __name__ == "__main__":
  98. test_main()