/Lib/test/test_print.py

http://unladen-swallow.googlecode.com/ · Python · 142 lines · 94 code · 26 blank · 22 comment · 4 complexity · 4d28f9a2f652d3bacd525ad1f9d3bf7f MD5 · raw file

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