test_print.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. """Test correct operation of the print function.
  2. """
  3. # In 2.6, this gives us the behavior we want. In 3.0, it has
  4. # no function, but it still must parse correctly.
  5. from __future__ import print_function
  6. import unittest
  7. from test import test_support
  8. from StringIO import StringIO
  9. NotDefined = object()
  10. # A dispatch table all 8 combinations of providing
  11. # sep, end, and file
  12. # I use this machinery so that I'm not just passing default
  13. # values to print, I'm either passing or not passing in the
  14. # arguments
  15. dispatch = {
  16. (False, False, False):
  17. lambda args, sep, end, file: print(*args),
  18. (False, False, True):
  19. lambda args, sep, end, file: print(file=file, *args),
  20. (False, True, False):
  21. lambda args, sep, end, file: print(end=end, *args),
  22. (False, True, True):
  23. lambda args, sep, end, file: print(end=end, file=file, *args),
  24. (True, False, False):
  25. lambda args, sep, end, file: print(sep=sep, *args),
  26. (True, False, True):
  27. lambda args, sep, end, file: print(sep=sep, file=file, *args),
  28. (True, True, False):
  29. lambda args, sep, end, file: print(sep=sep, end=end, *args),
  30. (True, True, True):
  31. lambda args, sep, end, file: print(sep=sep, end=end, file=file, *args),
  32. }
  33. # Class used to test __str__ and print
  34. class ClassWith__str__:
  35. def __init__(self, x):
  36. self.x = x
  37. def __str__(self):
  38. return self.x
  39. class TestPrint(unittest.TestCase):
  40. def check(self, expected, args,
  41. sep=NotDefined, end=NotDefined, file=NotDefined):
  42. # Capture sys.stdout in a StringIO. Call print with args,
  43. # and with sep, end, and file, if they're defined. Result
  44. # must match expected.
  45. # Look up the actual function to call, based on if sep, end, and file
  46. # are defined
  47. fn = dispatch[(sep is not NotDefined,
  48. end is not NotDefined,
  49. file is not NotDefined)]
  50. with test_support.captured_stdout() as t:
  51. fn(args, sep, end, file)
  52. self.assertEqual(t.getvalue(), expected)
  53. def test_print(self):
  54. def x(expected, args, sep=NotDefined, end=NotDefined):
  55. # Run the test 2 ways: not using file, and using
  56. # file directed to a StringIO
  57. self.check(expected, args, sep=sep, end=end)
  58. # When writing to a file, stdout is expected to be empty
  59. o = StringIO()
  60. self.check('', args, sep=sep, end=end, file=o)
  61. # And o will contain the expected output
  62. self.assertEqual(o.getvalue(), expected)
  63. x('\n', ())
  64. x('a\n', ('a',))
  65. x('None\n', (None,))
  66. x('1 2\n', (1, 2))
  67. x('1 2\n', (1, ' ', 2))
  68. x('1*2\n', (1, 2), sep='*')
  69. x('1 s', (1, 's'), end='')
  70. x('a\nb\n', ('a', 'b'), sep='\n')
  71. x('1.01', (1.0, 1), sep='', end='')
  72. x('1*a*1.3+', (1, 'a', 1.3), sep='*', end='+')
  73. x('a\n\nb\n', ('a\n', 'b'), sep='\n')
  74. x('\0+ +\0\n', ('\0', ' ', '\0'), sep='+')
  75. x('a\n b\n', ('a\n', 'b'))
  76. x('a\n b\n', ('a\n', 'b'), sep=None)
  77. x('a\n b\n', ('a\n', 'b'), end=None)
  78. x('a\n b\n', ('a\n', 'b'), sep=None, end=None)
  79. x('*\n', (ClassWith__str__('*'),))
  80. x('abc 1\n', (ClassWith__str__('abc'), 1))
  81. # 2.x unicode tests
  82. x(u'1 2\n', ('1', u'2'))
  83. x(u'u\1234\n', (u'u\1234',))
  84. x(u' abc 1\n', (' ', ClassWith__str__(u'abc'), 1))
  85. # errors
  86. self.assertRaises(TypeError, print, '', sep=3)
  87. self.assertRaises(TypeError, print, '', end=3)
  88. self.assertRaises(AttributeError, print, '', file='')
  89. def test_mixed_args(self):
  90. # If a unicode arg is passed, sep and end should be unicode, too.
  91. class Recorder(object):
  92. def __init__(self, must_be_unicode):
  93. self.buf = []
  94. self.force_unicode = must_be_unicode
  95. def write(self, what):
  96. if self.force_unicode and not isinstance(what, unicode):
  97. raise AssertionError("{0!r} is not unicode".format(what))
  98. self.buf.append(what)
  99. buf = Recorder(True)
  100. print(u'hi', file=buf)
  101. self.assertEqual(u''.join(buf.buf), 'hi\n')
  102. del buf.buf[:]
  103. print(u'hi', u'nothing', file=buf)
  104. self.assertEqual(u''.join(buf.buf), 'hi nothing\n')
  105. buf = Recorder(False)
  106. print('hi', 'bye', end=u'\n', file=buf)
  107. self.assertIsInstance(buf.buf[1], unicode)
  108. self.assertIsInstance(buf.buf[3], unicode)
  109. del buf.buf[:]
  110. print(sep=u'x', file=buf)
  111. self.assertIsInstance(buf.buf[-1], unicode)
  112. def test_main():
  113. test_support.run_unittest(TestPrint)
  114. if __name__ == "__main__":
  115. test_main()