fix_encoding_test.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #!/usr/bin/env python3
  2. # coding=utf-8
  3. # Copyright (c) 2011 The Chromium Authors. All rights reserved.
  4. # Use of this source code is governed by a BSD-style license that can be
  5. # found in the LICENSE file.
  6. """Unit tests for fix_encoding.py."""
  7. import os
  8. import sys
  9. import unittest
  10. sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  11. import fix_encoding
  12. class FixEncodingTest(unittest.TestCase):
  13. # Nice mix of latin, hebrew, arabic and chinese. Doesn't mean anything.
  14. text = u'Héllô 偉大 سيد'
  15. def test_code_page(self):
  16. # Make sure printing garbage won't throw.
  17. print(self.text.encode() + b'\xff')
  18. print(self.text.encode() + b'\xff', file=sys.stderr)
  19. def test_utf8(self):
  20. # Make sure printing utf-8 works.
  21. print(self.text.encode('utf-8'))
  22. print(self.text.encode('utf-8'), file=sys.stderr)
  23. @unittest.skipIf(os.name == 'nt', 'Does not work on Windows')
  24. def test_unicode(self):
  25. # Make sure printing unicode works.
  26. print(self.text)
  27. print(self.text, file=sys.stderr)
  28. @unittest.skipIf(os.name == 'nt', 'Does not work on Windows')
  29. def test_default_encoding(self):
  30. self.assertEqual('utf-8', sys.getdefaultencoding())
  31. def test_win_console(self):
  32. if sys.platform != 'win32':
  33. return
  34. # This should fail if not redirected, e.g. run directly instead of
  35. # through the presubmit check. Can be checked with: python
  36. # tests\fix_encoding_test.py
  37. self.assertEqual(sys.stdout.__class__, fix_encoding.WinUnicodeOutput)
  38. self.assertEqual(sys.stderr.__class__, fix_encoding.WinUnicodeOutput)
  39. self.assertEqual(sys.stdout.encoding, sys.getdefaultencoding())
  40. self.assertEqual(sys.stderr.encoding, sys.getdefaultencoding())
  41. def test_multiple_calls(self):
  42. # Shouldn't do anything.
  43. self.assertEqual(False, fix_encoding.fix_encoding())
  44. if __name__ == '__main__':
  45. fix_encoding.fix_encoding()
  46. unittest.main()