test_StringIO.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. # Tests StringIO and cStringIO
  2. import unittest
  3. import StringIO
  4. #import cStringIO
  5. #import types
  6. #import array
  7. import sys
  8. #from test import test_support
  9. class TestGenericStringIO(unittest.TestCase):
  10. # use a class variable MODULE to define which module is being tested
  11. # Line of data to test as string
  12. _line = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!'
  13. _lines = str((_line + '\n') * 5)
  14. def setUp(self):
  15. self._fp = StringIO.StringIO(self._lines)
  16. def test_input(self):
  17. #with prompt
  18. teststdout = StringIO.StringIO();
  19. sys.stdin = StringIO.StringIO("test input")
  20. sys.stdout = teststdout
  21. res = raw_input("test prompt")
  22. sys.stdout = sys.__stdout__
  23. self.assertEqual(res, "test input")
  24. self.assertEqual(teststdout.getvalue(), "test prompt")
  25. #without prompt
  26. sys.stdin = StringIO.StringIO("test input")
  27. teststdout = StringIO.StringIO();
  28. sys.stdout = teststdout
  29. res = raw_input()
  30. sys.stdout = sys.__stdout__
  31. sys.stdin = sys.__stdin__
  32. self.assertEqual(res, "test input")
  33. def test_reads(self):
  34. eq = self.assertEqual
  35. self.assertRaises(TypeError, self._fp.seek)
  36. eq(self._fp.read(10), self._line[:10])
  37. eq(self._fp.read(0), '')
  38. eq(self._fp.readline(0), '')
  39. eq(self._fp.readline(), self._line[10:] + '\n')
  40. eq(len(self._fp.readlines(60)), 2)
  41. self._fp.seek(0)
  42. eq(self._fp.readline(-1), self._line + '\n')
  43. def test_writes(self):
  44. f = StringIO.StringIO()
  45. self.assertRaises(TypeError, f.seek)
  46. f.write(self._line[:6])
  47. f.seek(3)
  48. f.write(self._line[20:26])
  49. f.write(self._line[52])
  50. self.assertEqual(f.getvalue(), 'abcuvwxyz!')
  51. def test_writelines(self):
  52. f = StringIO.StringIO()
  53. f.writelines([self._line[0], self._line[1], self._line[2]])
  54. f.seek(0)
  55. self.assertEqual(f.getvalue(), 'abc')
  56. # def test_writelines_error(self):
  57. # def errorGen():
  58. # yield 'a'
  59. # raise KeyboardInterrupt()
  60. # f = StringIO.StringIO()
  61. # self.assertRaises(KeyboardInterrupt, f.writelines, errorGen())
  62. def test_truncate(self):
  63. eq = self.assertEqual
  64. f = StringIO.StringIO()
  65. f.write(self._lines)
  66. f.seek(10)
  67. f.truncate()
  68. eq(f.getvalue(), 'abcdefghij')
  69. f.truncate(5)
  70. eq(f.getvalue(), 'abcde')
  71. f.write('xyz')
  72. eq(f.getvalue(), 'abcdexyz')
  73. self.assertRaises(IOError, f.truncate, -1)
  74. f.close()
  75. self.assertRaises(ValueError, f.write, 'frobnitz')
  76. def test_closed_flag(self):
  77. f = StringIO.StringIO()
  78. self.assertEqual(f.closed, False)
  79. f.close()
  80. self.assertEqual(f.closed, True)
  81. f = StringIO.StringIO("abc")
  82. self.assertEqual(f.closed, False)
  83. f.close()
  84. self.assertEqual(f.closed, True)
  85. def test_isatty(self):
  86. f = StringIO.StringIO()
  87. self.assertRaises(TypeError, f.isatty, None)
  88. self.assertEqual(f.isatty(), False)
  89. f.close()
  90. self.assertRaises(ValueError, f.isatty)
  91. # def test_iterator(self):
  92. # eq = self.assertEqual
  93. # unless = self.assertTrue
  94. # eq(iter(self._fp), self._fp)
  95. # # Does this object support the iteration protocol?
  96. # unless(hasattr(self._fp, '__iter__'))
  97. # unless(hasattr(self._fp, 'next'))
  98. # i = 0
  99. # for line in self._fp:
  100. # eq(line, self._line + '\n')
  101. # i += 1
  102. # eq(i, 5)
  103. # self._fp.close()
  104. # self.assertRaises(ValueError, self._fp.next)
  105. def test_getvalue(self):
  106. self._fp.close()
  107. self.assertRaises(ValueError, self._fp.getvalue)
  108. # def test_unicode(self):
  109. #
  110. # #if not test_support.have_unicode: return
  111. #
  112. # # The StringIO module also supports concatenating Unicode
  113. # # snippets to larger Unicode strings. This is tested by this
  114. # # method. Note that cStringIO does not support this extension.
  115. #
  116. # f = StringIO.StringIO()
  117. # f.write(self._line[:6])
  118. # f.seek(3)
  119. # f.write(unicode(self._line[20:26]))
  120. # f.write(unicode(self._line[52]))
  121. # s = f.getvalue()
  122. # self.assertEqual(s, unicode('abcuvwxyz!'))
  123. # #self.assertEqual(type(s), types.UnicodeType)
  124. if __name__ == '__main__':
  125. unittest.main()