fix_encoding_test.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. from __future__ import print_function
  8. import os
  9. import sys
  10. import unittest
  11. sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  12. import fix_encoding
  13. class FixEncodingTest(unittest.TestCase):
  14. # Nice mix of latin, hebrew, arabic and chinese. Doesn't mean anything.
  15. text = u'Héllô 偉大 سيد'
  16. def test_code_page(self):
  17. # Make sure printing garbage won't throw.
  18. print(self.text.encode() + b'\xff')
  19. print(self.text.encode() + b'\xff', file=sys.stderr)
  20. def test_utf8(self):
  21. # Make sure printing utf-8 works.
  22. print(self.text.encode('utf-8'))
  23. print(self.text.encode('utf-8'), file=sys.stderr)
  24. def test_unicode(self):
  25. # Make sure printing unicode works.
  26. print(self.text)
  27. print(self.text, file=sys.stderr)
  28. def test_default_encoding(self):
  29. self.assertEqual('utf-8', sys.getdefaultencoding())
  30. def test_win_console(self):
  31. if sys.platform != 'win32':
  32. return
  33. # This should fail if not redirected, e.g. run directly instead of through
  34. # the presubmit check. Can be checked with:
  35. # python tests\fix_encoding_test.py
  36. self.assertEqual(
  37. sys.stdout.__class__, fix_encoding.WinUnicodeOutput)
  38. self.assertEqual(
  39. sys.stderr.__class__, fix_encoding.WinUnicodeOutput)
  40. self.assertEqual(sys.stdout.encoding, sys.getdefaultencoding())
  41. self.assertEqual(sys.stderr.encoding, sys.getdefaultencoding())
  42. def test_multiple_calls(self):
  43. # Shouldn't do anything.
  44. self.assertEqual(False, fix_encoding.fix_encoding())
  45. if __name__ == '__main__':
  46. fix_encoding.fix_encoding()
  47. unittest.main()