split_buffer_test.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. from .. import unittest
  2. from compose.utils import split_buffer
  3. class SplitBufferTest(unittest.TestCase):
  4. def test_single_line_chunks(self):
  5. def reader():
  6. yield b'abc\n'
  7. yield b'def\n'
  8. yield b'ghi\n'
  9. self.assert_produces(reader, ['abc\n', 'def\n', 'ghi\n'])
  10. def test_no_end_separator(self):
  11. def reader():
  12. yield b'abc\n'
  13. yield b'def\n'
  14. yield b'ghi'
  15. self.assert_produces(reader, ['abc\n', 'def\n', 'ghi'])
  16. def test_multiple_line_chunk(self):
  17. def reader():
  18. yield b'abc\ndef\nghi'
  19. self.assert_produces(reader, ['abc\n', 'def\n', 'ghi'])
  20. def test_chunked_line(self):
  21. def reader():
  22. yield b'a'
  23. yield b'b'
  24. yield b'c'
  25. yield b'\n'
  26. yield b'd'
  27. self.assert_produces(reader, ['abc\n', 'd'])
  28. def test_preserves_unicode_sequences_within_lines(self):
  29. string = u"a\u2022c\n"
  30. def reader():
  31. yield string.encode('utf-8')
  32. self.assert_produces(reader, [string])
  33. def assert_produces(self, reader, expectations):
  34. split = split_buffer(reader())
  35. for (actual, expected) in zip(split, expectations):
  36. assert type(actual) == type(expected)
  37. assert actual == expected