clipboard.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #!/usr/bin/env python
  2. # License: GPLv3 Copyright: 2022, Kovid Goyal <kovid at kovidgoyal.net>
  3. from base64 import standard_b64decode, standard_b64encode
  4. from kitty.clipboard import WriteRequest
  5. from kitty.fast_data_types import StreamingBase64Decoder
  6. from . import BaseTest
  7. class TestClipboard(BaseTest):
  8. def test_clipboard_write_request(self):
  9. def t(data, expected):
  10. wr = WriteRequest(max_size=64)
  11. wr.add_base64_data(data)
  12. self.ae(wr.data_for(), expected)
  13. t('dGl0bGU=', b'title')
  14. t('dGl0bGU', b'title')
  15. t('dGl0bG', b'titl')
  16. t('dGl0bG==', b'titl')
  17. t('dGl0b', b'tit')
  18. t('bGlnaHQgd29yaw', b'light work')
  19. t('bGlnaHQgd29yaw==', b'light work')
  20. wr = WriteRequest(max_size=64)
  21. wr.add_base64_data('bGlnaHQgd29')
  22. for x in b'y', b'a', b'y', b'4', b'=':
  23. wr.add_base64_data(x)
  24. self.ae(wr.data_for(), b'light work.')
  25. wr = WriteRequest(max_size=64)
  26. for x in 'bGlnaHQgd29y':
  27. wr.add_base64_data(x)
  28. self.ae(wr.data_for(), b'light wor')
  29. def test_base64_streaming_decoder(self):
  30. d = StreamingBase64Decoder()
  31. c = standard_b64encode(b'abcdef')
  32. self.ae(b'abcdef', d.decode(c))
  33. self.assertFalse(d.needs_more_data())
  34. a = d.decode(c[:4])
  35. self.assertFalse(d.needs_more_data())
  36. self.ae(b'abcdef', a + d.decode(c[4:]))
  37. self.assertFalse(d.needs_more_data())
  38. a = d.decode(c[:1])
  39. self.assertTrue(d.needs_more_data())
  40. self.ae(b'abcdef', a + d.decode(c[1:4]) + d.decode(c[4:]))
  41. self.assertFalse(d.needs_more_data())
  42. c = standard_b64encode(b'abcd')
  43. self.ae(b'abcd', d.decode(c[:2]) + d.decode(c[2:]))
  44. c1 = standard_b64encode(b'1' * 4096)
  45. c2 = standard_b64encode(b'2' * 4096)
  46. self.ae(standard_b64decode(c1) + standard_b64decode(c2), d.decode(c1) + d.decode(c2))
  47. self.assertFalse(d.needs_more_data())